Ejemplo n.º 1
0
        private TreeDecision <IGOAPReadOnlyAction> BuildPlansToAction(IGOAPReadOnlyAction action)
        {
            var tree = new TreeDecision <IGOAPReadOnlyAction>();

            tree.AddToRoot(action);

            var needConditions = new List <KeyValuePair <string, GOAPState> >();

            foreach (var condition in action.Preconditions)
            {
                if (!_context.Contains(condition))
                {
                    needConditions.Add(condition);
                }
            }

            if (needConditions.Count == 0)
            {
                return(tree);
            }

            foreach (var condition in needConditions)
            {
                TreeDecision <IGOAPReadOnlyAction> conditionTree = BuildPlansToGoal(condition);

                if (conditionTree == null)
                {
                    return(null);
                }

                tree.AddToLeafs(conditionTree);
            }

            return(tree);
        }
Ejemplo n.º 2
0
        private TreeDecision <IGOAPReadOnlyAction> BuildPlansToGoal(KeyValuePair <string, GOAPState> goal)
        {
            var tree = new TreeDecision <IGOAPReadOnlyAction>();

            List <IGOAPReadOnlyAction> needActions;

            if (!GOAPActionsManager.Instance.TryGetActionsWithEffect(goal, out needActions))
            {
                return(null);
            }

            for (int i = 0; i < needActions.Count; i++)
            {
                var actionTree = BuildPlansToAction(needActions[i]);

                if (actionTree != null)
                {
                    tree.AddToRoot(actionTree);
                }
            }

            if (tree.IsEmpty)
            {
                return(null);
            }

            return(tree);
        }
Ejemplo n.º 3
0
        private List <IGOAPReadOnlyAction> GetSingleBestPlan(TreeDecision <IGOAPReadOnlyAction> plans)
        {
            var bestPlans = GetAllBestPlans(plans);

            if (bestPlans.Count == 0)
            {
                return(null);
            }

            return(bestPlans.GetRandomElement());
        }
Ejemplo n.º 4
0
        internal bool TryBuildPlans(KeyValuePair <string, GOAPState> goal, out TreeDecision <IGOAPReadOnlyAction> plans)
        {
            plans = BuildPlansToGoal(goal);

            if (plans == null || plans.IsEmpty)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 5
0
        private List <List <IGOAPReadOnlyAction> > GetAllBestPlans(TreeDecision <IGOAPReadOnlyAction> plans)
        {
            var bestCost    = _comparer.BadCost;
            var currentPlan = new List <IGOAPReadOnlyAction>();
            var bestPlans   = new List <List <IGOAPReadOnlyAction> >();

            foreach (var child in plans.Root.Children)
            {
                Iterate(_comparer.ZeroCost, child);
            }

            void Iterate(IGOAPCost previusCost, TreeElement <IGOAPReadOnlyAction> element)
            {
                var newCost = previusCost.GetSumWith(element.Content.Cost);

                currentPlan.Add(element.Content);

                if (!element.HasChildren)
                {
                    int value = _comparer.Compare(bestCost, newCost);

                    if (value <= 0)
                    {
                        if (value < 0)
                        {
                            bestCost  = newCost;
                            bestPlans = new List <List <IGOAPReadOnlyAction> >();
                        }

                        bestPlans.Add(new List <IGOAPReadOnlyAction>(currentPlan.Where(x => !x.IsConnector)));
                    }

                    currentPlan.RemoveAt(currentPlan.Count - 1);
                    return;
                }

                foreach (var child in element.Children)
                {
                    Iterate(newCost, child);
                }

                while (currentPlan[currentPlan.Count - 1] != element.Content)
                {
                    currentPlan.RemoveAt(currentPlan.Count - 1);
                }

                currentPlan.RemoveAt(currentPlan.Count - 1);
            }

            return(bestPlans);
        }
Ejemplo n.º 6
0
        public bool TryGetPlan(KeyValuePair <string, GOAPState> goal, out List <GOAPAction> plan)
        {
            var plans = new TreeDecision <GOAPAction>();

            plan = null;

            if (!_builder.TryBuildPlans(goal, out plans))
            {
                return(false);
            }

            plan = GetBestPlan(plans);
            return(true);
        }
Ejemplo n.º 7
0
        private List <GOAPAction> GetBestPlan(TreeDecision <GOAPAction> plans)
        {
            var bestCost = _comparer.BadCost;
            var bestPlan = new List <GOAPAction>();
            var stack    = new Stack <GOAPAction>();

            foreach (var child in plans.Root.Children)
            {
                Iterate(_comparer.ZeroCost, child);
            }

            void Iterate(IGOAPCost previusCost, TreeElement <GOAPAction> element)
            {
                var newCost = previusCost.GetSumWith(element.Content.Cost);

                stack.Push(element.Content);

                if (!element.HasChildren)
                {
                    int value = _comparer.Compare(bestCost, newCost);

                    if (value < 0 || (value == 0 && BaseMath.Probability(0.5f)))
                    {
                        bestCost = newCost;
                        bestPlan = stack.Where(x => !x.IsConnector).ToList();
                    }

                    return;
                }

                foreach (var child in element.Children)
                {
                    Iterate(newCost, child);
                }

                while (stack.Pop() != element.Content)
                {
                    ;
                }
            }

            return(bestPlan);
        }
Ejemplo n.º 8
0
        private List <List <IGOAPReadOnlyAction> > GetAllPlans(TreeDecision <IGOAPReadOnlyAction> plans)
        {
            var currentPlan = new List <IGOAPReadOnlyAction>();
            var allPlans    = new List <List <IGOAPReadOnlyAction> >();

            foreach (var child in plans.Root.Children)
            {
                Iterate(child);
            }

            void Iterate(TreeElement <IGOAPReadOnlyAction> element)
            {
                currentPlan.Add(element.Content);

                if (!element.HasChildren)
                {
                    allPlans.Add(new List <IGOAPReadOnlyAction>(currentPlan));
                    currentPlan.RemoveAt(currentPlan.Count - 1);
                    return;
                }

                foreach (var child in element.Children)
                {
                    Iterate(child);
                }

                while (currentPlan[currentPlan.Count - 1] != element.Content)
                {
                    currentPlan.RemoveAt(currentPlan.Count - 1);
                }

                currentPlan.RemoveAt(currentPlan.Count - 1);
            }

            return(allPlans);
        }