Example #1
0
        /// <summary>
        /// Выбирает новое состояние на основе текущего состояния мира.
        /// </summary>
        public string SelectNewState(AntAICondition aWorldState)
        {
            string newState = "";

            if (currentGoal != null)
            {
                planner.MakePlan(ref currentPlan, aWorldState, currentGoal);
                if (currentPlan.isSuccess)
                {
                    string actionName = planner.GetAction(currentPlan[0]).name;
                    newState = planner.GetState(actionName);

                    /* Отладочный вывод плана в консоль.
                     * AntAICondition condition = aConditions.Clone();
                     * string p = string.Format("Conditions: {0}\n", _planner.NameIt(condition.Description()));
                     * for (int i = 0; i < _currentPlan.Count; i++)
                     * {
                     *      AntAIAction action = _planner.GetAction(_currentPlan[i]);
                     *      condition.Act(action.post);
                     *      p += string.Format("<color=orange>{0}</color> => {1}\n", action.name, _planner.NameIt(condition.Description()));
                     * }
                     * AntLog.Trace(p);
                     * //*/
                }
            }
            else
            {
                AntLog.Report("AntAIAgent", "Goal not defined!");
            }

            return(newState);
        }
        private void BuildPlanHandler()
        {
            // Create planner.
            var planner = new AntAIPlanner();

            // planner.DebugMode = true;
            planner.LoadScenario(_scenario);

            // Create current world state and set current conditions.
            var current = new AntAICondition();

            current.BeginUpdate(planner);
            for (int i = 0, n = _worldState.list.Length; i < n; i++)
            {
                current.Set(_scenario.conditions.GetName(_worldState.list[i].id), _worldState.list[i].value);
            }
            current.EndUpdate();

            // Create our goal world state.
            AntAIScenarioGoal defaultGoal = null;

            for (int i = 0, n = _scenario.goals.Length; i < n; i++)
            {
                if (_scenario.goals[i].isDefault)
                {
                    defaultGoal = _scenario.goals[i];
                    break;
                }
            }

            A.Assert(defaultGoal == null, "Default goal not found!");
            if (defaultGoal == null)
            {
                return;
            }

            // Copy conditions from the scenario goal to the goal conditions.
            var goal = new AntAICondition();

            goal.BeginUpdate(planner);
            for (int i = 0, n = defaultGoal.conditions.Length; i < n; i++)
            {
                goal.Set(_scenario.conditions.GetName(defaultGoal.conditions[i].id), defaultGoal.conditions[i].value);
            }
            goal.EndUpdate();

            // Create and build the plan.
            var plan = new AntAIPlan();

            planner.MakePlan(ref plan, current, goal);

            // Call event when plan is ready.
            if (EventBuildPlan != null)
            {
                EventBuildPlan(this, plan, planner);
            }
        }