示例#1
0
 public Node_Goap(WorldStateSet ws, float gCost, float hCost, Node_Goap parent, ActionID actionID)
 {
     this.parent = parent;
     this.WS     = ws;
     this.ID     = actionID;
     this.hCost  = hCost;
 }
示例#2
0
 protected Goal_Goap(GameObject owner, Planner_Goap planner)
 {
     this.planner = planner;
     this.owner   = owner;
     //persona = owner.GetComponent<Persona>();
     persona         = owner.GetComponent <Goap_Controller>().Persona;
     GoalWorldstates = new WorldStateSet();
 }
示例#3
0
 public Node_Goap(WorldStateSet ws, WorldStateSymbol[] wsDiff, float gCost, float hCost, Node_Goap parent, ActionID actionID)
 {
     this.parent = parent;
     this.WS     = ws;
     this.ID     = actionID;
     this.hCost  = hCost;
     this.WSDiff = wsDiff;
 }
示例#4
0
 public void UpdateWSVariables(WorldStateSet currentWS)
 {
     WSVariableText.text = "WorldState";
     foreach (var keyVal in currentWS)
     {
         WSVariableText.text += "\n" + keyVal.Key + ": " + keyVal.Value;
     }
 }
示例#5
0
    public WorldStateSet ApplyEffects(WorldStateSet worldState)
    {
        WorldStateSet appliedWorldState = (WorldStateSet)worldState.Clone();

        foreach (WorldStateSymbol effect in Effects)
        {
            appliedWorldState[effect] = true;
        }
        return(appliedWorldState);
    }
示例#6
0
    public WorldStateSet GetEffectedWorldState(WorldStateSet worldState)
    {
        WorldStateSet appliedWorldState = (WorldStateSet)worldState.Clone();

        foreach (WorldStateSymbol effect in GoalWorldstates.Keys)
        {
            appliedWorldState[effect] = true;
        }
        return(appliedWorldState);
    }
    public object Clone()
    {
        WorldStateSet clone = new WorldStateSet();

        foreach (KeyValuePair <WorldStateSymbol, bool> kvp in this)
        {
            clone.Add(kvp.Key, kvp.Value);
        }
        return(clone);
    }
示例#8
0
 public bool IsValidInWorldState(WorldStateSet worldState)
 {
     foreach (WorldStateSymbol precondition in PreConditions)
     {
         if (!worldState[precondition])
         {
             return(false);
         }
     }
     return(true);
 }
示例#9
0
 /// <summary>
 /// Checks if this goals world state is satisfied based on the world state it
 /// in parameter
 /// </summary>
 /// <returns><c>true</c>, if satisfied was ised, <c>false</c> otherwise.</returns>
 /// <param name="worldState">World state.</param>
 public virtual bool IsSatisfied(WorldStateSet worldState)
 {
     foreach (var goalState in GoalWorldstates)
     {
         if (worldState[goalState.Key] != goalState.Value)
         {
             return(false);
         }
     }
     return(true);
 }
示例#10
0
    /// <summary>
    /// Tries the get plan.
    /// </summary>
    /// <returns>The get plan.</returns>
    /// <param name="currentState">Agentens current world state.</param>
    /// <param name="actions">Agentens tillgängliga actions.</param>
    public Queue <ActionID> TryGetPlan(WorldStateSet currentState, List <Action_Goap> actions)
    {
        var nodePlan = planner.FindPathFromGoal(currentState, this, actions);
        Queue <ActionID> actionPlan = new Queue <ActionID>(nodePlan.Count);

        foreach (Node_Goap node in nodePlan)
        {
            actionPlan.Enqueue(node.ID);
        }

        return(actionPlan);
    }
示例#11
0
    private float CalculateHCost(WorldStateSet current, Goal_Goap target)
    {
        float cost = 0;

        foreach (var stateSet in target.GoalWorldstates)
        {
            if (current[stateSet.Key] != stateSet.Value)
            {
                cost += 1;
            }
        }
        return(cost);
    }
示例#12
0
    public float CalculateHCost(WorldStateSet currentState)
    {
        float cost = 0;

        foreach (WorldStateSymbol goalState in WSDiff)
        {
            if (currentState[goalState] == false)
            {
                cost += 1;
            }
        }
        hCost = cost;
        return(cost);
    }
示例#13
0
    public List <Node_Goap> FindPathFromGoal(WorldStateSet currentState, Goal_Goap goal, List <Action_Goap> agentActions)
    {
        Dictionary <WorldStateSymbol, List <Action_Goap> > actionEffectsTable = new Dictionary <WorldStateSymbol, List <Action_Goap> >(agentActions.Count);

        foreach (Action_Goap action in agentActions)
        {
            foreach (WorldStateSymbol effect in action.Effects)
            {
                if (!actionEffectsTable.ContainsKey(effect))
                {
                    actionEffectsTable.Add(effect, new List <Action_Goap>());
                }
                actionEffectsTable[effect].Add(action);
            }
        }

        List <Node_Goap>    openSet   = new List <Node_Goap>();
        HashSet <Node_Goap> closedSet = new HashSet <Node_Goap>();

        Node_Goap start = new Node_Goap(goal.GetEffectedWorldState(currentState), goal.GoalWorldstates.Keys.ToArray(), 1000, CalculateHCost(currentState, goal), null, ActionID.None);

        openSet.Add(start);

        int iteration     = 0;
        int maxIterations = 300;

        while (openSet.Count > 0)
        {
            iteration++;
            if (iteration >= maxIterations)
            {
                Debug.LogError("To many iterations returning [Plan = null]");
                return(null);
            }
            Node_Goap currentNode = openSet[0];

            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet[i];
                }
            }

            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            ///Debug.Log("Added " + currentNode.ID + " to closed set");
            if (currentNode.IsValidInWorldState(currentState))
            {
                List <Node_Goap> path = new List <Node_Goap>();
                Node_Goap        end  = currentNode;

                while (end.parent != null)
                {
                    path.Add(end);
                    end = end.parent;
                }
                return(path);
            }

            // Get possible actions form currentNode, create new nodes from the actions
            // effected worldState.
            foreach (WorldStateSymbol symbol in currentNode.WSDiff)
            {
                //Debug.Log(symbol);
                //TODO denna checken ska antagligen bort sen
                if (!actionEffectsTable.ContainsKey(symbol))
                {
                    continue;
                }
                //Check the actions available from the current WorldState
                foreach (Action_Goap action in actionEffectsTable[symbol])
                {
                    WorldStateSet effectedWS   = action.ApplyEffects(currentNode.WS);
                    Node_Goap     possibleNode = new Node_Goap(effectedWS, action.PreConditions, action.ID);

                    if (closedSet.Contains(possibleNode))
                    {
                        continue;
                    }

                    if (!openSet.Contains(possibleNode)) //The node is not a member, just add it.
                    {
                        possibleNode.parent = currentNode;
                        possibleNode.gCost  = currentNode.gCost + action.GetCost() + CalculateHCost(effectedWS, goal);
                        openSet.Add(possibleNode);
                        // Debug.Log(action.ID + " was not visited before added to open set.");
                    }
                    else
                    {
                        if (currentNode.gCost + action.GetCost() < possibleNode.gCost)
                        {
                            int index = openSet.IndexOf(possibleNode);
                            openSet[index].parent = currentNode;
                            openSet[index].gCost  = currentNode.gCost + action.GetCost();
                            openSet[index].hCost  = CalculateHCost(effectedWS, goal);
                            openSet[index].ID     = action.ID;
                            // Debug.Log("There was a cheaper path to: " + action.ID + " updating its values");
                        }
                    }
                }
            }
        }

        return(null);
    }
示例#14
0
 public Node_Goap(WorldStateSet ws, ActionID actionID)
 {
     this.WS = ws;
     this.ID = actionID;
 }
示例#15
0
 public Node_Goap(WorldStateSet ws, WorldStateSymbol[] wsDiff, ActionID actionID)
 {
     this.WS     = ws;
     this.ID     = actionID;
     this.WSDiff = wsDiff;
 }