public static List <Operand> AtkAlgDamageStackRPN(List <Operand> AtkAlgOperands) { List <Operand> AtkAlgOperandsRPN = new List <Operand>(); Stack <Operand> stack = new Stack <Operand>(); for (int i = 0; i < AtkAlgOperands.Count; i++) { //token is a constant if (AtkAlgOperands[i].Operator == "C") { AtkAlgOperandsRPN.Add(AtkAlgOperands[i]); } //token is a status if (AtkAlgOperands[i].Operator == "A" || AtkAlgOperands[i].Operator == "D") { AtkAlgOperandsRPN.Add(AtkAlgOperands[i]); } //token is a operator if (AtkAlgOperands[i].isOperator() && !AtkAlgOperands[i].isParenthesis()) { while (stack.Count != 0 && ((AtkAlgOperands[i].isLeftAssociative() && AtkAlgOperands[i].OperatorPrecedence() <= BattleControllerUtility.OperatorPrecedence(stack.Peek().Operator)) || AtkAlgOperands[i].OperatorPrecedence() < BattleControllerUtility.OperatorPrecedence(stack.Peek().Operator))) { AtkAlgOperandsRPN.Add(stack.Pop()); } stack.Push(AtkAlgOperands[i]); } //token is a left parenthesis if (AtkAlgOperands[i].Operator == "(") { stack.Push(AtkAlgOperands[i]); } //token is a right parenthesis if (AtkAlgOperands[i].Operator == ")") { while (stack.Count > 0) { if (stack.Peek().Operator != "(") { AtkAlgOperandsRPN.Add(stack.Pop()); } else { break; } } if (stack.Count == 0) { Debug.Log("Mismathced parenthesis"); break; } stack.Pop(); } } while (stack.Count > 0) { if (stack.Peek().Operator == "(" || stack.Peek().Operator == ")") { Debug.Log("Mismathced parenthesis"); } AtkAlgOperandsRPN.Add(stack.Pop()); } return(AtkAlgOperandsRPN); }
public static string AtkFormulaRPNString(List <Operand> AtkAlgOperands, List <string> StatusList) { string output = string.Empty; Stack <string> stack = new Stack <string>(); for (int i = 0; i < AtkAlgOperands.Count; i++) { //token is a constant if (AtkAlgOperands[i].Operator == "C") { //Debug.Log(AtkAlgOperands[i].Number.ToString() + " added to output"); output += AtkAlgOperands[i].Number.ToString() + " "; } //token is a status if (AtkAlgOperands[i].Operator == "A" || AtkAlgOperands[i].Operator == "D") { //Debug.Log(AtkAlgOperands[i].Operator + '.' + StatusList[(int)AtkAlgOperands[i].Number].ToString() + " added to output"); output += AtkAlgOperands[i].Operator + '.' + StatusList[(int)AtkAlgOperands[i].Number].ToString() + ' '; } //token is a operator if (AtkAlgOperands[i].isOperator() && !AtkAlgOperands[i].isParenthesis()) { while (stack.Count != 0 && ((AtkAlgOperands[i].isLeftAssociative() && AtkAlgOperands[i].OperatorPrecedence() <= BattleControllerUtility.OperatorPrecedence(stack.Peek())) || AtkAlgOperands[i].OperatorPrecedence() < BattleControllerUtility.OperatorPrecedence(stack.Peek()))) { //Debug.Log (stack.Peek().ToString() + " popped from stack into output"); output += stack.Pop() + ' '; } //Debug.Log (AtkAlgOperands[i].Operator + " pushed into stack"); stack.Push(AtkAlgOperands[i].Operator); } //token is a left parenthesis if (AtkAlgOperands[i].Operator == "(") { //Debug.Log ("( pushed into stack"); stack.Push("("); } //token is a right parenthesis if (AtkAlgOperands[i].Operator == ")") { //Debug.Log (") pushed into stack"); while (stack.Count > 0) { if (stack.Peek() != "(") { //Debug.Log (stack.Peek().ToString() + " popped from stack into output"); output += stack.Pop() + ' '; } else { break; } } if (stack.Count == 0) { Debug.Log("Mismathced parenthesis"); break; } //Debug.Log (stack.Peek().ToString() + " popped from stack"); stack.Pop(); } } while (stack.Count > 0) { if (stack.Peek() == "(" || stack.Peek() == ")") { Debug.Log("Mismathced parenthesis"); } //Debug.Log (stack.Peek().ToString() + " popped from stack into output"); output += stack.Pop() + ' '; } return(output); }
void AtkAlgCalculator() { AtkAlgFoldout = EditorGUILayout.Foldout(AtkAlgFoldout, "Attack Algorithm Calculator: "); if (AtkAlgFoldout) { Undo.RecordObject(target, "Moddified attack algorithm "); //Attacker Buttons GUILayout.Label("Attacker: "); GUILayout.BeginHorizontal(); for (int i = 0; i < Controller.StatusList.Count; i++) { if (i % 4 == 0) { GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); } if (GUILayout.Button(BattleControllerUtility.TruncateLongString(Controller.StatusList[i], 6))) { Controller.AtkAlgOperands.Add(new Operand("A", i)); } } GUILayout.EndHorizontal(); //Defender Buttons GUILayout.Label("Defender: "); GUILayout.BeginHorizontal(); for (int i = 0; i < Controller.StatusList.Count; i++) { if (i % 4 == 0) { GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); } if (GUILayout.Button(BattleControllerUtility.TruncateLongString(Controller.StatusList[i], 6))) { Controller.AtkAlgOperands.Add(new Operand("D", i)); } } GUILayout.EndHorizontal(); //Constant Buttons GUILayout.Label("Constant: "); GUILayout.BeginHorizontal(); Constant = EditorGUILayout.FloatField(Constant); if (GUILayout.Button("Add")) { Controller.AtkAlgOperands.Add(new Operand("C", Constant)); } GUILayout.EndHorizontal(); //Operators Buttons GUILayout.Label("Operators: "); GUILayout.BeginHorizontal(); if (GUILayout.Button("+")) { Controller.AtkAlgOperands.Add(new Operand("+", 0)); } if (GUILayout.Button("-")) { Controller.AtkAlgOperands.Add(new Operand("-", 0)); } if (GUILayout.Button("*")) { Controller.AtkAlgOperands.Add(new Operand("*", 0)); } if (GUILayout.Button("/")) { Controller.AtkAlgOperands.Add(new Operand("/", 0)); } if (GUILayout.Button("^")) { Controller.AtkAlgOperands.Add(new Operand("pw", 0)); } if (GUILayout.Button("(")) { Controller.AtkAlgOperands.Add(new Operand("(", 0)); } if (GUILayout.Button(")")) { Controller.AtkAlgOperands.Add(new Operand(")", 0)); } GUILayout.EndHorizontal(); //Misc Buttons GUILayout.BeginHorizontal(); if (GUILayout.Button("←")) { } if (GUILayout.Button("→")) { } if (GUILayout.Button("Erase")) { if (Controller.AtkAlgOperands.Count > 0) { Controller.AtkAlgOperands.RemoveAt(Controller.AtkAlgOperands.Count - 1); } } if (GUILayout.Button("Clear")) { Controller.AtkAlgOperands.Clear(); } GUILayout.EndHorizontal(); Controller.AFormInf = BattleControllerUtility.AtkFormulaInfixString(Controller.AtkAlgOperands, Controller.StatusList); if (GUI.changed) { EditorUtility.SetDirty(target); } GUILayout.Label("Attack formula: "); GUILayout.TextArea(Controller.AFormInf); //Test if (Controller.UnitsList.Count >= 2) { GUILayout.Label("Test Damage:\n" + Controller.UnitsList[0].UnitName + " attacking " + Controller.UnitsList[1].UnitName + ": " + Controller.CalcDamage.ToString()); } if (GUILayout.Button("Update attack formula")) { Controller.UpdateAtkAlg(); Controller.CalcDamage = Controller.Damage(Controller.UnitsList[0], Controller.UnitsList[1]); } } }