Beispiel #1
0
 private void _EnsureArithStructValueInited(ref StructValue sv, ref StructValue proto)
 {
     if (!sv.Inited)
     {
         sv = StructValue.CopyCreate(ref proto, 0);
     }
 }
Beispiel #2
0
        private float _CalculateH()
        {
            float h = 0;

            foreach (var pr in goal.GetValues())
            {
                var pairValue = pr.Value;
                if (pairValue.tp == StructValue.EValueType.Other)
                {
                    ++h;
                }
                else if (pairValue.tp == StructValue.EValueType.Arithmetic)
                {
                    float goalValue = Convert.ToSingle(pairValue.v);
                    var   defValue  = StructValue.CopyCreate(ref pairValue, 0);
                    if (!pairValue.IsFulfilledBy(defValue))
                    {
                        h += Math.Abs(goalValue);
                    }
                }
            }
            return(h);
        }
Beispiel #3
0
        private void Init(IGoapPlanner <T, W> planner, ReGoapState <T, W> newGoal, ReGoapNode <T, W> parent, IReGoapAction <T, W> action)
        {
            expandList.Clear();
            tmpKeys.Clear();

            this.planner = planner;
            this.parent  = parent;
            this.action  = action;
            if (action != null)
            {
                actionSettings = action.GetSettings(planner.GetCurrentAgent(), newGoal);
            }

            if (parent != null)
            {
                state = parent.GetState().Clone();
                // g(node)
                g = parent.GetPathCost();
            }
            else
            {
                state = planner.GetCurrentAgent().GetMemory().GetWorldState().Clone();
            }

            var nextAction = parent == null ? null : parent.action;

            if (action != null)
            {
                // create a new instance of the goal based on the paren't goal
                goal = ReGoapState <T, W> .Instantiate();

                var tmpGoal = ReGoapState <T, W> .Instantiate(newGoal);

                var preconditions = action.GetPreconditions(tmpGoal, nextAction);
                var effects       = action.GetEffects(tmpGoal, nextAction);
                // adding the action's effects to the current node's state
                state.AddFromState(effects);
                // addding the action's cost to the node's total cost
                g += action.GetCost(tmpGoal, nextAction);

                //// add all preconditions of the current action to the goal
                //tmpGoal.AddFromState(preconditions);
                //// removes from goal all the conditions that are now fulfilled in the node's state
                //tmpGoal.ReplaceWithMissingDifference(state);
                ////goal.ReplaceWithMissingDifference(effects);

                // collect all keys from goal & precondition, unique-ed
                foreach (var pr in tmpGoal.GetValues())
                {
                    var k = pr.Key;
                    if (!tmpKeys.Contains(k))
                    {
                        tmpKeys.Add(k);
                    }
                }
                foreach (var pr in preconditions.GetValues())
                {
                    var k = pr.Key;
                    if (!tmpKeys.Contains(k))
                    {
                        tmpKeys.Add(k);
                    }
                }

                // process each keys
                foreach (var k in tmpKeys)
                {
                    StructValue goalValue, effectValue, precondValue, stateValue, protoValue;
                    tmpGoal.GetValues().TryGetValue(k, out goalValue);
                    effects.GetValues().TryGetValue(k, out effectValue);
                    preconditions.GetValues().TryGetValue(k, out precondValue);
                    state.GetValues().TryGetValue(k, out stateValue);

                    StructValue.EValueType valueType;
                    _GetValueType(ref goalValue, ref effectValue, ref precondValue, ref stateValue, out valueType, out protoValue);
                    if (valueType == StructValue.EValueType.Arithmetic)
                    {
                        //_EnsureArithStructValueInited(ref goalValue, ref protoValue);
                        _EnsureArithStructValueInited(ref effectValue, ref protoValue);
                        _EnsureArithStructValueInited(ref precondValue, ref protoValue);
                        _EnsureArithStructValueInited(ref stateValue, ref protoValue);
                        if (!goalValue.Inited)
                        {
                            goalValue = StructValue.CopyCreate(ref stateValue, -(Convert.ToSingle(stateValue.v) - Convert.ToSingle(effectValue.v)));
                        }

                        float fGoal    = Convert.ToSingle(goalValue.v);
                        float fEffect  = Convert.ToSingle(effectValue.v);
                        float fPrecond = Convert.ToSingle(precondValue.v);
                        float fState   = Convert.ToSingle(stateValue.v);

                        float finalV = Math.Max(
                            fGoal - fEffect,
                            Math.Min(fPrecond, fPrecond - fState)
                            );

                        var sv = StructValue.CopyCreate(ref protoValue, finalV);

                        goal.SetStructValue(k, sv);
                    }
                    else if (valueType == StructValue.EValueType.Other)
                    {
                        //ReplaceWithMissingDifference
                        if (stateValue.Inited && goalValue.Inited && goalValue.IsFulfilledBy(stateValue))
                        {
                            goalValue.Invalidate();
                        }

                        // AddFromPrecond
                        // 1. if the precond is satisfied by the memory start state, then discard
                        // 2. else this newly added goal from precond, should not be removed due to fulfilled by curStateValue
                        if (precondValue.Inited)
                        {
                            bool        preCondfulfilledByMem = false;
                            var         startMemoryState      = planner.GetCurrentAgent().GetMemory().GetWorldState();
                            StructValue startMemoryValue;
                            if (startMemoryState.GetValues().TryGetValue(k, out startMemoryValue))
                            {
                                if (startMemoryValue.Inited && precondValue.IsFulfilledBy(startMemoryValue))
                                {
                                    preCondfulfilledByMem = true;
                                }
                            }

                            if (!preCondfulfilledByMem)
                            {
                                if (goalValue.Inited)
                                {
                                    goalValue = goalValue.MergeWith(precondValue);
                                }
                                else
                                {
                                    goalValue = precondValue;
                                }
                            }
                        }

                        if (goalValue.Inited)
                        {
                            goal.SetStructValue(k, goalValue);
                        }
                    }
                    else
                    {
                        UnityEngine.Debug.LogError("Unexpected StructValue type: " + valueType);
                    }
                }// foreach (var k in tmpKeys)

                tmpGoal.Recycle();
            }
            else
            {
                var diff = ReGoapState <T, W> .Instantiate();

                newGoal.MissingDifference(state, ref diff);
                goal = diff;
            }

            h = _CalculateH();

            // f(node) = g(node) + h(node)
            cost = g + h * planner.GetSettings().HeuristicMultiplier;
        }