public static void OpenWindow(AIScenarioAgent agent)
        {
            GoapAIDebuggerWindow window =
                ( GoapAIDebuggerWindow )EditorWindow.GetWindow(typeof(GoapAIDebuggerWindow), false,
                                                               "AI Debugger");

            window.autoRepaintOnSceneChange = true;
            window.SetupAgent(agent);
        }
        private void DescribeCurrentWorldState(AIScenarioAgent agent, ref List <string> aResult)
        {
            bool value;

            foreach (var kv in agent.GetConditions())
            {
                if (kv.Value == null)
                {
                    continue;
                }

                value = kv.Value.OnCheck();
                aResult.Add(string.Format("      '<color={2}>{0}</color>' = <color={2}>{1}</color>",
                                          kv.Key, value, (value) ? _trueColor : _falseColor));
            }
        }
        /// <summary>
        /// Обновляет информацию уже существующей ноды описывающей состояние мира (условий ИИ).
        /// </summary>
        private void UpdateWorldStateNode(AIScenarioAgent agent, GoapAIDebuggerNode aNode)
        {
            List <string> desc = new List <string> ();

            desc.Add(string.Format("<b><color={0}>WORLD STATE</color></b>", _titleColor));
            desc.Add("   <b>Current Conditions</b>");
            DescribeCurrentWorldState(agent, ref desc);

            StringBuilder text = new StringBuilder();

            for (int i = 0, n = desc.Count; i < n; i++)
            {
                text.AppendLine(desc[i]);
            }

            aNode.title       = text.ToString();
            aNode.rect.height = CalcHeight(desc.Count);
        }
        private void SetupAgent(AIScenarioAgent agent)
        {
            if (agent == null)
            {
                _nodes.Clear();
                _genericNodes.Clear();
                _goalNodes.Clear();
                _titles.Clear();

                if (_agent != null)
                {
                    _agent._planner.PlanUpdated -= OnPlanUpdated;
                    _agent = null;
                }
            }
            else if (!System.Object.ReferenceEquals(agent, _agent))
            {
                _agent = agent;

                _nodes.Clear();
                _goalNodes.Clear();
                _titles.Clear();

                CreateTitle(0.0f, 0.0f, string.Format("{0}: Actions and Goals", Selection.activeGameObject.name));

                float actionsHeight = 0.0f;
                RebuildActionNodes(new Vector2(_totalDrag.x, _totalDrag.y + 35.0f), out actionsHeight);
                actionsHeight += 35.0f;

                float goalsHeight = 0.0f;
                RebuildGoalNodes(new Vector2(_totalDrag.x, _totalDrag.y + actionsHeight), out goalsHeight);

                CreateTitle(0.0f, actionsHeight + goalsHeight + 15.0f, string.Format("{0}: Current Plan", Selection.activeGameObject.name));
                _planNodesPosition = new Vector2(0.0f, actionsHeight + goalsHeight + 45.0f);

                RebuildBlackboardNode(_totalDrag + _planNodesPosition);

                _agent._planner.PlanUpdated += OnPlanUpdated;
            }
        }
        /// <summary>
        /// Создает новую ноду описывающуюу текущее состояние мира (условия ИИ).
        /// </summary>
        private GoapAIDebuggerNode CreateWorldStateNode(AIScenarioAgent agent, ref Vector2 aNodePosition)
        {
            List <string> desc = new List <string> ();

            desc.Add(string.Format("<b><color={0}>WORLD STATE</color></b>", _titleColor));
            desc.Add("   <b>Current Conditions</b>");
            DescribeCurrentWorldState(agent, ref desc);

            StringBuilder text = new StringBuilder();

            for (int i = 0, n = desc.Count; i < n; i++)
            {
                text.AppendLine(desc[i]);
            }

            var node = AddNode(text.ToString(), 220.0f, CalcHeight(desc.Count), _nodeStyle, _nodeStyle,
                               ref aNodePosition, false);

            node.SetOutput(node.rect.width - 10.0f, node.rect.height * 0.5f);
            node.SetInput(10.0f, node.rect.height * 0.5f);
            _genericNodes.Add(node);
            return(node);
        }
 private void Awake()
 {
     agent = new AIScenarioAgent();
 }
        /// <summary>
        /// Описывает конкретное действие из плана ИИ.
        /// </summary>
        private string DescribePlanAction(AIScenarioAgent agent, Action action, out int aNumLines)
        {
            var lines = new List <string> ();

            lines.Add(string.Format("<b><color={1}>ACTION</color> '<color={2}>{0}</color>'</b>",
                                    action.name, _titleColor, _nameColor));
            lines.Add("   <b>Post Conditions</b>");

            bool value;

            foreach (var kv in agent.GetConditions())
            {
                if (kv.Value == null)
                {
                    continue;
                }

                value = kv.Value.OnCheck();

                bool diff = false;
                foreach (var set in action._preConditions)
                {
                    if (set.Item1 == kv.Key && set.Item2 != value)
                    {
                        diff  = true;
                        value = set.Item2;
                        break;
                    }
                }
                foreach (var set in action._postConditions)
                {
                    if (set.Item1 == kv.Key && set.Item2 != value)
                    {
                        diff  = true;
                        value = set.Item2;
                        break;
                    }
                }

                if (diff)
                {
                    lines.Add(string.Format(
                                  "      <color=#a873dd><b>></b></color> <i>'<color={2}>{0}</color>' = <color={2}>{1}</color></i>",
                                  kv.Key, value, value ? _trueColor : _falseColor));
                }
                else
                {
                    lines.Add(string.Format("      '<color={2}>{0}</color>' = <color={2}>{1}</color>",
                                            kv.Key, value, value ? _trueColor : _falseColor));
                }
            }

            StringBuilder text = new StringBuilder();

            for (int i = 0, n = lines.Count; i < n; i++)
            {
                text.AppendLine(lines[i]);
            }

            aNumLines = lines.Count;
            return(text.ToString());
        }
        private void OnGUI()
        {
            if (Selection.activeGameObject != null)
            {
                var p = Selection.activeGameObject.GetComponent <AIScenarioAgentComponent> ();
                if (p == null)
                {
                    _nodes.Clear();
                    _genericNodes.Clear();
                    _goalNodes.Clear();
                    _titles.Clear();

                    if (_agent != null)
                    {
                        _agent._planner.PlanUpdated -= OnPlanUpdated;
                        _agent = null;
                    }
                }
                else if (!System.Object.ReferenceEquals(p.agent, _agent))
                {
                    _agent = p.agent;

                    _nodes.Clear();
                    _goalNodes.Clear();
                    _titles.Clear();

                    CreateTitle(0.0f, 0.0f, string.Format("{0}: Actions and Goals", Selection.activeGameObject.name));

                    float actionsHeight = 0.0f;
                    RebuildActionNodes(new Vector2(_totalDrag.x, _totalDrag.y + 35.0f), out actionsHeight);
                    actionsHeight += 35.0f;

                    float goalsHeight = 0.0f;
                    RebuildGoalNodes(new Vector2(_totalDrag.x, _totalDrag.y + actionsHeight), out goalsHeight);

                    CreateTitle(0.0f, actionsHeight + goalsHeight + 15.0f, string.Format("{0}: Current Plan", Selection.activeGameObject.name));
                    _planNodesPosition = new Vector2(0.0f, actionsHeight + goalsHeight + 45.0f);

                    RebuildBlackboardNode(_totalDrag + _planNodesPosition);

                    _agent._planner.PlanUpdated += OnPlanUpdated;
                }
            }

            if (_agent != null)
            {
                DrawGrid(20, Color.gray, 0.05f);
                DrawGrid(100, Color.gray, 0.05f);

                if (Event.current.type == EventType.Repaint)
                {
                    DrawTitles(_titles);
                    DrawLinks(_nodes, true);
                    DrawLinks(_genericNodes, false);
                    DrawCurrentStateLink();
                    DrawNodes(_nodes);
                    DrawNodes(_genericNodes);
                    Repaint();
                }

                ProcessEvents(Event.current);
            }
            else
            {
                if (Event.current.type == EventType.Repaint)
                {
                    GUI.Label(new Rect(10.0f, 10.0f, 200.0f, 50.0f), "Object with AI Not Selected.", _titleStyle);
                }
            }
        }
 public override void Awake()
 {
     _agent = agent as AIScenarioAgent;
 }