Ejemplo n.º 1
0
    public override void Precalculations(IReGoapAgent goapAgent, ReGoapState goalState)
    {
        base.Precalculations(goapAgent, goalState);
        var bankPosition = agent.GetMemory().GetWorldState().Get <Vector3>("nearestBankPosition");

        preconditions.Clear();
        effects.Clear();
        preconditions.Set("isAtPosition", bankPosition);
        effects.Set("isAtPosition", Vector3.zero);

        foreach (var pair in goalState.GetValues())
        {
            if (pair.Key.StartsWith("collectedResource"))
            {
                var resourceName = pair.Key.Substring(17);
                preconditions.Set("hasResource" + resourceName, true);
                effects.Set("collectedResource" + resourceName, true);
                settings = new AddResourceToBankSettings
                {
                    ResourceName = resourceName
                };
                break;
            }
        }
    }
Ejemplo n.º 2
0
 protected virtual string GetNeededResourceFromGoal(ReGoapState <string, object> goalState)
 {
     foreach (var pair in goalState.GetValues())
     {
         if (pair.Key.StartsWith("hasResource"))
         {
             return(pair.Key.Substring(11));
         }
     }
     return(null);
 }
Ejemplo n.º 3
0
        public override ReGoapState GetEffects(ReGoapState goalState)
        {
            effects.Clear();

            foreach (var pair in goalState.GetValues())
            {
                if (pair.Key.StartsWith("collectedResource"))
                {
                    var resourceName = pair.Key.Substring(17);
                    effects.Set("collectedResource" + resourceName, true);
                    break;
                }
            }

            return(effects);
        }
Ejemplo n.º 4
0
    public override ReGoapState <string, object> GetEffects(ReGoapState <string, object> goalState, IReGoapAction <string, object> next = null)
    {
        effects.Clear();
        effects.Set("isAtPosition", (Vector3?)Vector3.zero);

        foreach (var pair in goalState.GetValues())
        {
            if (pair.Key.StartsWith("collectedResource"))
            {
                var resourceName = pair.Key.Substring(17);
                effects.Set("collectedResource" + resourceName, true);
                break;
            }
        }

        return(effects);
    }
Ejemplo n.º 5
0
 public override IReGoapActionSettings <string, object> GetSettings(IReGoapAgent <string, object> goapAgent, ReGoapState <string, object> goalState)
 {
     settings = null;
     foreach (var pair in goalState.GetValues())
     {
         if (pair.Key.StartsWith("collectedResource"))
         {
             var resourceName = pair.Key.Substring(17);
             settings = new AddResourceToBankSettings
             {
                 ResourceName = resourceName
             };
             break;
         }
     }
     return(settings);
 }
Ejemplo n.º 6
0
    public override ReGoapState <string, object> GetPreconditions(ReGoapState <string, object> goalState, IReGoapAction <string, object> next = null)
    {
        var bankPosition = agent.GetMemory().GetWorldState().Get("nearestBankPosition") as Vector3?;

        preconditions.Clear();
        preconditions.Set("isAtPosition", bankPosition);

        foreach (var pair in goalState.GetValues())
        {
            if (pair.Key.StartsWith("collectedResource"))
            {
                var resourceName = pair.Key.Substring(17);
                preconditions.Set("hasResource" + resourceName, true);
                break;
            }
        }

        return(preconditions);
    }
Ejemplo n.º 7
0
        public override ReGoapState GetPreconditions(ReGoapState goalState)
        {
            ReGoapState reGoapState  = reGoapAgent.GetWorldState();
            Vector3?    bankPosition = reGoapState.Get("nearestBankPosition") as Vector3?;

            preconditions.Clear();
            preconditions.Set("isAtPosition", bankPosition);

            foreach (var pair in goalState.GetValues())
            {
                if (pair.Key.StartsWith("collectedResource"))
                {
                    var resourceName = pair.Key.Substring(17);
                    preconditions.Set("hasResource" + resourceName, true);
                    break;
                }
            }

            return(preconditions);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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;
        }
Ejemplo n.º 10
0
    private void UpdateGoapNodes(GameObject gameObj)
    {
        if (agent == null || !agent.IsActive() || agent.GetMemory() == null)
        {
            return;
        }

        if (nodes == null)
        {
            nodes = new List <ReGoapNodeEditor>();
        }
        nodes.Clear();
        var width        = 250f;
        var height       = 70f;
        var nodePosition = Vector2.zero;

        foreach (var goal in gameObj.GetComponents <IReGoapGoal>())
        {
            if (goal.GetGoalState() == null)
            {
                continue;
            }
            var text = string.Format("<b>GOAL</b> <i>{0}</i>\n", goal);
            foreach (var keyValue in goal.GetGoalState().GetValues())
            {
                text += string.Format("<b>'{0}'</b> = <i>'{1}'</i>", keyValue.Key, keyValue.Value);
            }
            var style = nodeStyle;
            if (agent.IsActive() && agent.GetCurrentGoal() == goal)
            {
                style = activeStyle;
            }
            DrawGenericNode(text, width, height, style, ref nodePosition);
        }

        nodePosition = new Vector2(0f, nodePosition.y + height + 10);
        height       = 66;
        var maxHeight  = height;
        var worldState = agent.GetMemory().GetWorldState();

        foreach (var action in agent.GetActionsSet())
        {
            var curHeight = height;
            var text      = string.Format("<b>POSS.ACTION</b> <i>{0}</i>\n", action.GetName());
            text += "-<b>preconditions</b>-\n";
            ReGoapState preconditionsDifferences = new ReGoapState();
            var         preconditions            = action.GetPreconditions(null);
            if (preconditions == null)
            {
                continue;
            }
            preconditions.MissingDifference(worldState, ref preconditionsDifferences);
            foreach (var preconditionPair in preconditions.GetValues())
            {
                curHeight += 13;
                var color = "#004d00";
                if (preconditionsDifferences.GetValues().ContainsKey(preconditionPair.Key))
                {
                    color = "#800000";
                }
                text += string.Format("<color={2}>'<b>{0}</b>' = '<i>{1}</i>'</color>\n", preconditionPair.Key, preconditionPair.Value, color);
            }
            text += "-<b>effects</b>-\n";
            foreach (var effectPair in action.GetEffects(null).GetValues())
            {
                curHeight += 13;
                text      += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", effectPair.Key, effectPair.Value);
            }
            curHeight += 13;
            var proceduralCheck = action.CheckProceduralCondition(agent, null);
            text += string.Format("<color={0}>-<b>proceduralCondition</b>: {1}</color>\n", proceduralCheck ? "#004d00" : "#800000", proceduralCheck);

            maxHeight = Mathf.Max(maxHeight, curHeight);
            DrawGenericNode(text, width, curHeight, possibleActionStyle, ref nodePosition);
        }
        nodePosition.x  = 0;
        nodePosition.y += maxHeight + 10;
        height          = 40;
        if (agent.GetCurrentGoal() != null)
        {
            foreach (var action in agent.GetStartingPlan().ToArray())
            {
                var style = actionNodeStyle;
                if (action.Action.IsActive())
                {
                    style = activeActionNodeStyle;
                }
                var text = string.Format("<b>ACTION</b> <i>{0}</i>\n", action.Action.GetName());
                DrawGenericNode(text, width, height, style, ref nodePosition);
            }
        }

        if (agent.GetMemory() != null)
        {
            nodePosition = new Vector2(0, nodePosition.y + height + 10);
            width        = 500;
            height       = 40;
            var nodeText = "<b>WORLD STATE</b>\n";
            foreach (var pair in agent.GetMemory().GetWorldState().GetValues())
            {
                nodeText += string.Format("'<b>{0}</b>' = '<i>{1}</i>'\n", pair.Key, pair.Value);
                height   += 13;
            }
            DrawGenericNode(nodeText, width, height, worldStateStyle, ref nodePosition);
        }
    }