public void TestMethod1()
        {
            ExpressionParser Parser = ExpressionParser.CreateParser();

            Parser.ParsingContext.Parameters.Add("x", Parameter.NewParameter(0));
            Parser.ParsingContext.Parameters.Add("c", Parameter.NewParameter(new List <int> {
                1, 2, 3
            }));
            DummyAction dummy  = new DummyAction();
            ForAction   action = new ForAction
            {
                Parser = Parser,
                CollectionExpression = "c$",
                ItemVarName          = "i",
                InnerAction          = new BlockAction
                {
                    ActionList = new List <IAction>
                    {
                        dummy,
                        new SetVariableAction
                        {
                            VariableName = "x",
                            Expression   = "x + i$"
                        }
                    }
                }
            };

            action.ExecuteAction();
            Assert.AreEqual(3, dummy.Count);
            Assert.AreEqual(6.0, (double)Parser.ParsingContext.Parameters["x"].ParameterValue);
        }
Beispiel #2
0
    private void onNewStep(GameObject unused)
    {
        if (unused == null || scriptIsRunning.Count != 0)         // player has next action
        {
            Debug.Log("on new step");
            GameObject    nextAction;
            CurrentAction current;
            foreach (GameObject currentAction in currentActions)
            {
                current = currentAction.GetComponent <CurrentAction>();
                if (current != null)                 //current not in gameData.actionsHistory
                {
                    nextAction = getNextAction(currentAction, current.agent);
                    Debug.Log("nextAction = " + nextAction);

                    /*
                     * //loop drone script
                     * if(nextAction == null && current.agent.CompareTag("Drone")){
                     *      nextAction = getFirstActionOf(current.agent.GetComponent<ScriptRef>().scriptContainer.transform.GetChild(0).gameObject, current.agent);
                     * }
                     */
                    if (nextAction == null && currentAction.GetComponent <CurrentAction>().agent.CompareTag("Drone"))
                    {
                        currentAction.GetComponent <CurrentAction>().agent.GetComponent <ScriptRef>().scriptFinished = true;
                    }
                    else if (nextAction != null)
                    {
                        //parent = for & first loop and first child, currentfor = 0 -> currentfor = 1
                        if (nextAction.transform.parent.GetComponent <ForAction>() && nextAction.transform.parent.GetComponent <ForAction>().currentFor == 0 &&
                            nextAction.Equals(nextAction.transform.parent.GetComponent <ForAction>().firstChild))
                        {
                            ForAction forAct = nextAction.transform.parent.GetComponent <ForAction>();
                            forAct.currentFor++;
                            forAct.transform.GetChild(0).GetChild(1).GetComponent <TMP_InputField>().text = (forAct.currentFor).ToString() + " / " + forAct.nbFor.ToString();
                        }
                        //ask to add CurrentAction on next frame
                        MainLoop.instance.StartCoroutine(delayAddCurrentAction(nextAction, current.agent));
                    }

                    GameObjectManager.removeComponent <CurrentAction>(currentAction);
                }
            }
        }
    }
Beispiel #3
0
        //return true to continue the macro, false to stop (macro's over)
        public bool ExecNext()
        {
            try
            {
                if (!m_Playing)
                {
                    return(false);
                }

                if (m_Wait != null)
                {
                    TimeSpan waitLen = DateTime.UtcNow - m_Wait.StartTime;
                    if (!(m_Wait is PauseAction) && waitLen >= m_Wait.Timeout)
                    {
                        if (Loop)
                        {
                            if (Engine.MainWindow.WaitDisplay != null)
                            {
                                Engine.MainWindow.WaitDisplay.SafeAction(s => s.Text = string.Empty);
                            }
                            m_CurrentAction = -1;
                            m_IfStatus.Clear();
                            PauseB4Loop.Perform();
                            PauseB4Loop.Parent = this;
                            m_Wait             = PauseB4Loop;
                            return(true);
                        }
                        else
                        {
                            Stop();
                            return(false);
                        }
                    }
                    else
                    {
                        if (!m_Wait.PerformWait())
                        {
                            m_Wait = null; // done waiting
                            if (Engine.MainWindow.WaitDisplay != null)
                            {
                                Engine.MainWindow.WaitDisplay.SafeAction(s => s.Text = string.Empty);
                            }
                        }
                        else
                        {
                            if (waitLen >= TimeSpan.FromSeconds(4.0) && Engine.MainWindow.WaitDisplay != null)
                            {
                                StringBuilder sb = new StringBuilder(Language.GetString(LocString.WaitingTimeout));

                                sb.AppendLine("\n");

                                int s = (int)(m_Wait.Timeout - waitLen).TotalSeconds;
                                int m = 0;

                                if (s > 60)
                                {
                                    m  = s / 60;
                                    s %= 60;
                                    if (m > 60)
                                    {
                                        sb.AppendFormat("{0}:", m / 60);
                                        m %= 60;
                                    }
                                }

                                sb.AppendFormat("{0:00}:{1:00}", m, s);

                                Engine.MainWindow.WaitDisplay.SafeAction(w => w.Text = sb.ToString());
                            }

                            return(true); // keep waiting
                        }
                    }
                }

                m_CurrentAction++;
                //MacroManager.ActionUpdate( this, m_CurrentAction );
                if (m_ListBox != null)
                {
                    if (m_CurrentAction < m_ListBox.Items.Count)
                    {
                        m_ListBox.SafeAction(s => s.SelectedIndex = m_CurrentAction);
                    }
                    else
                    {
                        m_ListBox.SafeAction(s => s.SelectedIndex = -1);
                    }
                }

                if (m_CurrentAction >= 0 && m_CurrentAction < m_Actions.Count)
                {
                    MacroAction action = (MacroAction)m_Actions[m_CurrentAction];

                    if (action is IfAction)
                    {
                        bool val = ((IfAction)action).Evaluate();
                        m_IfStatus.Push(val);

                        if (!val)
                        {
                            // false so skip to an else or an endif
                            int ifcount = 0;
                            while (m_CurrentAction + 1 < m_Actions.Count)
                            {
                                if (m_Actions[m_CurrentAction + 1] is IfAction)
                                {
                                    ifcount++;
                                }
                                else if (m_Actions[m_CurrentAction + 1] is ElseAction && ifcount <= 0)
                                {
                                    break;
                                }
                                else if (m_Actions[m_CurrentAction + 1] is EndIfAction)
                                {
                                    if (ifcount <= 0)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        ifcount--;
                                    }
                                }

                                m_CurrentAction++;
                            }
                        }
                    }
                    else if (action is ElseAction && m_IfStatus.Count > 0)
                    {
                        bool val = (bool)m_IfStatus.Peek();
                        if (val)
                        {
                            // the if was true, so skip to an endif
                            int ifcount = 0;
                            while (m_CurrentAction + 1 < m_Actions.Count)
                            {
                                if (m_Actions[m_CurrentAction + 1] is IfAction)
                                {
                                    ifcount++;
                                }
                                else if (m_Actions[m_CurrentAction + 1] is EndIfAction)
                                {
                                    if (ifcount <= 0)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        ifcount--;
                                    }
                                }

                                m_CurrentAction++;
                            }
                        }
                    }
                    else if (action is EndIfAction && m_IfStatus.Count > 0)
                    {
                        m_IfStatus.Pop();
                    }
                    else if (action is ForAction)
                    {
                        ForAction fa = (ForAction)action;
                        fa.Count++;

                        if (fa.Count > fa.Max)
                        {
                            fa.Count = 0;

                            int forcount = 0;
                            m_CurrentAction++;
                            while (m_CurrentAction < m_Actions.Count)
                            {
                                if (m_Actions[m_CurrentAction] is ForAction)
                                {
                                    forcount++;
                                }
                                else if (m_Actions[m_CurrentAction] is EndForAction)
                                {
                                    if (forcount <= 0)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        forcount--;
                                    }
                                }

                                m_CurrentAction++;
                            }

                            if (m_CurrentAction < m_Actions.Count)
                            {
                                action = (MacroAction)m_Actions[m_CurrentAction];
                            }
                        }
                    }
                    else if (action is EndForAction && Client.Instance.AllowBit(FeatureBit.LoopingMacros))
                    {
                        int ca       = m_CurrentAction - 1;
                        int forcount = 0;

                        while (ca >= 0)
                        {
                            if (m_Actions[ca] is EndForAction)
                            {
                                forcount--;
                            }
                            else if (m_Actions[ca] is ForAction)
                            {
                                if (forcount >= 0)
                                {
                                    break;
                                }
                                else
                                {
                                    forcount++;
                                }
                            }

                            ca--;
                        }

                        if (ca >= 0 && m_Actions[ca] is ForAction)
                        {
                            m_CurrentAction = ca - 1;
                        }
                    }
                    else if (action is WhileAction && Client.Instance.AllowBit(FeatureBit.LoopingMacros))
                    {
                        bool val = ((WhileAction)action).Evaluate();

                        if (!val)
                        {
                            // false so skip to the endwhile
                            int whilecount = 0;
                            while (m_CurrentAction + 1 < m_Actions.Count)
                            {
                                if (m_Actions[m_CurrentAction + 1] is WhileAction)
                                {
                                    whilecount++;
                                }
                                else if (m_Actions[m_CurrentAction + 1] is EndWhileAction)
                                {
                                    if (whilecount <= 0)
                                    {
                                        // Skip over the end while
                                        m_CurrentAction++;
                                        break;
                                    }

                                    whilecount--;
                                }

                                m_CurrentAction++;
                            }
                        }
                    }
                    else if (action is EndWhileAction && Client.Instance.AllowBit(FeatureBit.LoopingMacros))
                    {
                        int ca         = m_CurrentAction - 1;
                        int whilecount = 0;

                        while (ca >= 0)
                        {
                            if (m_Actions[ca] is EndWhileAction)
                            {
                                whilecount--;
                            }
                            else if (m_Actions[ca] is WhileAction)
                            {
                                if (whilecount >= 0)
                                {
                                    break;
                                }
                                else
                                {
                                    whilecount++;
                                }
                            }

                            ca--;
                        }

                        if (ca >= 0 && m_Actions[ca] is WhileAction)
                        {
                            m_CurrentAction = ca - 1;
                        }
                    }
                    else if (action is DoWhileAction && Client.Instance.AllowBit(FeatureBit.LoopingMacros))
                    {
                        bool val = ((DoWhileAction)action).Evaluate();

                        if (val)
                        {
                            int ca           = m_CurrentAction - 1;
                            int dowhilecount = 0;

                            while (ca >= 0)
                            {
                                if (m_Actions[ca] is DoWhileAction)
                                {
                                    dowhilecount--;
                                }
                                else if (m_Actions[ca] is StartDoWhileAction)
                                {
                                    if (dowhilecount >= 0)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        dowhilecount++;
                                    }
                                }

                                ca--;
                            }

                            if (ca >= 0 && m_Actions[ca] is StartDoWhileAction)
                            {
                                m_CurrentAction = ca - 1;
                            }
                        }
                    }

                    bool isWait = action is MacroWaitAction;
                    if (!action.Perform() && isWait)
                    {
                        m_Wait           = (MacroWaitAction)action;
                        m_Wait.StartTime = DateTime.UtcNow;
                    }
                    else if (NextIsInstantWait() && !isWait)
                    {
                        return(ExecNext());
                    }
                }
                else
                {
                    if (Engine.MainWindow.WaitDisplay != null)
                    {
                        Engine.MainWindow.WaitDisplay.SafeAction(s => s.Text = string.Empty);
                    }

                    if (Loop)
                    {
                        m_CurrentAction = -1;

                        Reset();

                        PauseB4Loop.Perform();
                        PauseB4Loop.Parent = this;
                        m_Wait             = PauseB4Loop;
                    }
                    else
                    {
                        Stop();
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                new MessageDialog("Macro Exception", true, e.ToString()).Show();
                return(false);
            }

            return(true);
        }
Beispiel #4
0
    public GameObject getNextAction(GameObject currentAction, GameObject agent)
    {
        if (currentAction.GetComponent <BasicAction>())
        {
            if (currentAction.GetComponent <BasicAction>().next == null || currentAction.GetComponent <BasicAction>().next.GetComponent <BasicAction>())
            {
                return(currentAction.GetComponent <BasicAction>().next);
            }
            else
            {
                return(getNextAction(currentAction.GetComponent <BasicAction>().next, agent));
            }
        }

        else if (currentAction.GetComponent <ForAction>())
        {
            ForAction forAct = currentAction.GetComponent <ForAction>();
            //do next action
            if (currentAction.GetComponent <ForAction>().currentFor == currentAction.GetComponent <ForAction>().nbFor)
            {
                if (currentAction.GetComponent <CurrentAction>())
                {
                    GameObjectManager.removeComponent <CurrentAction>(currentAction);
                }
                if (currentAction.GetComponent <ForAction>().next == null || currentAction.GetComponent <ForAction>().next.GetComponent <BasicAction>())
                {
                    return(currentAction.GetComponent <ForAction>().next);
                }
                else
                {
                    return(getNextAction(currentAction.GetComponent <ForAction>().next, agent));
                }
            }
            //loop
            else
            {
                if (!currentAction.GetComponent <CurrentAction>())
                {
                    GameObjectManager.addComponent <CurrentAction>(currentAction, new{ agent = agent });
                }
                forAct.currentFor++;
                forAct.transform.GetChild(0).GetChild(1).GetComponent <TMP_InputField>().text = (forAct.currentFor).ToString() + " / " + forAct.nbFor.ToString();
                if (currentAction.GetComponent <ForAction>().firstChild == null || currentAction.GetComponent <ForAction>().firstChild.GetComponent <BasicAction>())
                {
                    return(currentAction.GetComponent <ForAction>().firstChild);
                }
                else
                {
                    return(getNextAction(currentAction.GetComponent <ForAction>().firstChild, agent));
                }
            }
        }
        else if (currentAction.GetComponent <IfAction>())
        {
            if (currentAction.GetComponent <IfAction>().firstChild != null && ifValid(currentAction.GetComponent <IfAction>(), agent))
            {
                if (currentAction.GetComponent <IfAction>().firstChild.GetComponent <BasicAction>())
                {
                    return(currentAction.GetComponent <IfAction>().firstChild);
                }
                else
                {
                    return(getNextAction(currentAction.GetComponent <IfAction>().firstChild, agent));
                }
            }
            else
            {
                if (currentAction.GetComponent <IfAction>().next == null || currentAction.GetComponent <IfAction>().next.GetComponent <BasicAction>())
                {
                    return(currentAction.GetComponent <IfAction>().next);
                }
                else
                {
                    return(getNextAction(currentAction.GetComponent <IfAction>().next, agent));
                }
            }
        }
        else if (currentAction.GetComponent <ForeverAction>())
        {
            //loop
            if (!currentAction.GetComponent <CurrentAction>())
            {
                GameObjectManager.addComponent <CurrentAction>(currentAction, new{ agent = agent });
            }

            if (currentAction.GetComponent <ForeverAction>().firstChild == null || currentAction.GetComponent <ForeverAction>().firstChild.GetComponent <BasicAction>())
            {
                return(currentAction.GetComponent <ForeverAction>().firstChild);
            }

            else
            {
                return(getNextAction(currentAction.GetComponent <ForeverAction>().firstChild, agent));
            }
        }

        return(null);
    }
Beispiel #5
0
    private IEnumerator delayInit()
    {
        yield return(null);

        GameObject firstAction = null;

        foreach (GameObject robot in playerGO)
        {
            if (robot.GetComponent <ScriptRef>().scriptContainer.transform.childCount > 0)
            {
                firstAction = getFirstActionOf(robot.GetComponent <ScriptRef>().scriptContainer.transform.GetChild(0).gameObject, robot);
            }
            Debug.Log("firstAction = " + firstAction);
            if (firstAction != null)
            {
                GameObjectManager.addComponent <CurrentAction>(firstAction, new{ agent = robot });

                //Debug.Log("firstAction robot = "+firstAction.name);
                if (firstAction.transform.parent.GetComponent <ForAction>())
                {
                    ForAction forAct = firstAction.transform.parent.GetComponent <ForAction>();
                    GameObjectManager.addComponent <CurrentAction>(firstAction.transform.parent.gameObject, new{ agent = robot });
                    forAct.currentFor++;
                    forAct.transform.GetChild(0).GetChild(1).GetComponent <TMP_InputField>().text = (forAct.currentFor).ToString() + " / " + forAct.nbFor.ToString();
                }
                else if (firstAction.transform.parent.GetComponent <ForeverAction>())
                {
                    GameObjectManager.addComponent <CurrentAction>(firstAction.transform.parent.gameObject, new{ agent = robot });
                }
            }

            else
            {
                GameObjectManager.addComponent <EmptyExecution>(MainLoop.instance.gameObject);
            }
        }
        if (firstAction != null)         //not an empty execution
        //current action drone(s)
        //Debug.Log("drones "+droneGO.Count);
        {
            firstAction = null;
            foreach (GameObject drone in droneGO)
            {
                if (!drone.GetComponent <ScriptRef>().scriptContainer.GetComponentInChildren <CurrentAction>() && !drone.GetComponent <ScriptRef>().scriptFinished)
                {
                    if (drone.GetComponent <ScriptRef>().scriptContainer.transform.childCount > 0)
                    {
                        firstAction = getFirstActionOf(drone.GetComponent <ScriptRef>().scriptContainer.transform.GetChild(0).gameObject, drone);
                        //Debug.Log("firstAction drone = "+firstAction.name);
                    }
                    //Debug.Log(firstAction);
                    if (firstAction != null)
                    {
                        GameObjectManager.addComponent <CurrentAction>(firstAction, new{ agent = drone });
                        Debug.Log("firstAction drone = " + firstAction.name);
                        if (firstAction.transform.parent.GetComponent <ForAction>())
                        {
                            ForAction forAct = firstAction.transform.parent.GetComponent <ForAction>();
                            GameObjectManager.addComponent <CurrentAction>(firstAction.transform.parent.gameObject, new{ agent = drone });
                            forAct.currentFor++;
                            forAct.transform.GetChild(0).GetChild(1).GetComponent <TMP_InputField>().text = (forAct.currentFor).ToString() + " / " + forAct.nbFor.ToString();
                        }
                        else if (firstAction.transform.parent.GetComponent <ForeverAction>())
                        {
                            GameObjectManager.addComponent <CurrentAction>(firstAction.transform.parent.gameObject, new{ agent = drone });
                        }
                    }
                }
                else
                {
                    onNewStep(null);
                }
            }
        }
    }