Esempio n. 1
0
 // Constructor
 public Node(Node parent, float cost, Dictionary <string, int> allStates, GAction action)
 {
     this.parent = parent;
     this.cost   = cost;
     this.state  = new Dictionary <string, int>(allStates);
     this.action = action;
 }
Esempio n. 2
0
        //remove and action from a list of actions
        private List <GAction> ActionSubset(List <GAction> actions, GAction removeMe)
        {
            List <GAction> subset = new List <GAction>();

            foreach (GAction a in actions)
            {
                if (!a.Equals(removeMe))
                {
                    subset.Add(a);
                }
            }
            return(subset);
        }
Esempio n. 3
0
        // Overloaded Constructor
        public Node(Node parent, float cost, Dictionary <string, int> allStates, Dictionary <string, int> beliefStates, GAction action)
        {
            this.parent = parent;
            this.cost   = cost;
            this.state  = new Dictionary <string, int>(allStates);

            //as well as the world states add the agents beliefs as states that can be
            //used to match preconditions
            foreach (KeyValuePair <string, int> b in beliefStates)
            {
                if (!this.state.ContainsKey(b.Key))
                {
                    this.state.Add(b.Key, b.Value);
                }
            }
            this.action = action;
        }
Esempio n. 4
0
        void LateUpdate()
        {
            //if there's a current action and it is still running
            if (currentAction != null && currentAction.running)
            {
                // Find the distance to the target
                float distanceToTarget = Vector2.Distance(destination, this.transform.position);

                // Check the agent has a goal and has reached that goal
                if (distanceToTarget < distanceForComplete)   // currentAction.agent.remainingDistance < 1.0f)
                ///
                {
                    if (isAgentStop)
                    {
                        currentAction.agent.isStopped = true;
                    }
                    ///
                    if (!invoked)
                    {
                        //if the action movement is complete wait
                        //a certain duration for it to be completed
                        Invoke("CompleteAction", currentAction.duration);
                        invoked = true;
                    }
                }
                return;
            }

            // Check we have a planner and an actionQueue
            if (planner == null || actionQueue == null)
            {
                // If planner is null then create a new one
                planner = new GPlanner();

                // Sort the goals in descending order and store them in sortedGoals
                var sortedGoals = from entry in goals orderby entry.Value descending select entry;

                //look through each goal to find one that has an achievable plan
                foreach (KeyValuePair <SubGoal, int> sg in sortedGoals)
                {
                    actionQueue = planner.plan(actions, sg.Key.sGoals, beliefs);
                    // If actionQueue is not = null then we must have a plan
                    if (actionQueue != null)
                    {
                        // Set the current goal
                        currentGoal = sg.Key;
                        break;
                    }
                }
            }

            // Have we an actionQueue
            if (actionQueue != null && actionQueue.Count == 0)
            {
                // Check if currentGoal is removable
                if (currentGoal.remove)
                {
                    // Remove it
                    goals.Remove(currentGoal);
                }
                // Set planner = null so it will trigger a new one
                planner = null;
            }

            // Do we still have actions
            if (actionQueue != null && actionQueue.Count > 0)
            {
                // Remove the top action of the queue and put it in currentAction
                currentAction = actionQueue.Dequeue();

                if (currentAction.PrePerform())
                {
                    // Get our current object
                    if (currentAction.target == null && currentAction.targetTag != "")
                    {
                        currentAction.target = GameObject.FindWithTag(currentAction.targetTag);
                    }

                    if (currentAction.target != null)
                    {
                        // Activate the current action
                        currentAction.running = true;
                        //print(currentAction.target);
                        // InvokeRepeating("GoToTarget",0f,0.5f);
                        currentAction.agent.isStopped = false;
                        // // Pass Unities AI the destination for the agent
                        destination = (Vector2)currentAction.target.transform.position;
                        Transform isDestExist = currentAction.target.transform.Find("Destination");
                        if (isDestExist)
                        {
                            destination = (Vector2)isDestExist.position;
                        }
                        currentAction.agent.SetDestination(new Vector2(destination.x, destination.y));
                    }
                }
                else
                {
                    // Force a new plan
                    actionQueue = null;
                }
            }
        }