Example #1
0
	public void planAborted (GoapAction aborter)
	{
		// An action bailed out of the plan. State has been reset to plan again.
		// Take note of what happened and make sure if you run the same goal again
		// that it can succeed.
		Debug.Log ("<color=red>Plan Aborted</color> "+GoapAgent.prettyPrint(aborter));
	}
Example #2
0
	public GoapStateGraphNode(List<GoapWorldState> unsatisfied, List<GoapWorldState> satisfied)
	{
		SatisfiedStates = satisfied;
		UnsatisfiedStates = unsatisfied;
		CostSinceStart = float.PositiveInfinity;
		CameFrom = null;
		CameFromAction = null;
	}
Example #3
0
 /**
  * Create a subset of the actions excluding the removeMe one. Creates a new set.
  */
 private HashSet<GoapAction> actionSubset(HashSet<GoapAction> actions, GoapAction removeMe)
 {
     HashSet<GoapAction> subset = new HashSet<GoapAction> ();
     foreach (GoapAction a in actions) {
         if (!a.Equals(removeMe))
             subset.Add(a);
     }
     return subset;
 }
Example #4
0
 public static string prettyPrint(GoapAction[] actions)
 {
     String s = "";
     foreach (GoapAction a in actions) {
         s += a.GetType().Name;
         s += ", ";
     }
     return s;
 }
Example #5
0
	public bool moveAgent(GoapAction nextAction) {
		// move towards the NextAction's target
		float step = moveSpeed * Time.deltaTime;
		gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, nextAction.target.transform.position, step);
		
		if (gameObject.transform.position.Equals(nextAction.target.transform.position) ) {
			// we are at the target location, we are done
			nextAction.setInRange(true);
			return true;
		} else
			return false;
	}
Example #6
0
    public bool moveAgent(GoapAction nextAction)
    {
        if (path!=null && currentWaypoint<path.vectorPath.Count){
        //			Debug.Log(this.gameObject);//+" "+ path.vectorPath[currentWaypoint] );
            gameObject.transform.LookAt( path.vectorPath[currentWaypoint]);
            gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, path.vectorPath[currentWaypoint], moveSpeed*Time.deltaTime);
            //Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
            //dir *= speed * Time.deltaTime;
            //controller.SimpleMove (dir);
            //Check if we are close enough to the next waypoint
            //If we are, proceed to follow the next waypoint
            if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
                currentWaypoint++;
                //	return;
            }
        }
        if (path == null && !imcalculatingthepathnigger){
            imcalculatingthepathnigger = true;
            seeker.StartPath (transform.position, nextAction.target.transform.position, OnPathComplete);}
        targetPosition=nextAction.target.transform.position;
        //		PathRequestManager.RequestPath(transform.position,nextAction.target.transform.position, OnPathFound);
        // move towards the NextAction's target
        //	float step = moveSpeed * Time.deltaTime;
        //	gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, nextAction.target.transform.position, step);
        //		Debug.Log ("target:"+nextAction.target.gameObject + " position: "+nextAction.target.transform.position+" and distant:" +Vector2.Distance(gameObject.transform.position, nextAction.target.transform.position));
        //Debug.Log(Vector2.Distance(gameObject.transform.position, nextAction.target.transform.position));

        if (Vector2.Distance(gameObject.transform.position, nextAction.target.transform.position) < 1f ) {
        //			Debug.Log ("ARRIVED");
            path=null;
            seeker.StartPath (transform.position, nextAction.target.transform.position, OnPathComplete);
            currentWaypoint = 0;
            imcalculatingthepathnigger = false;
            //			Debug.Log("walao");
            // we are at the target location, we are done
            nextAction.setInRange(true);

            return true;
        } else{
            return false;}

        //		if (path == null) {
        //			//We have no path to move after yet
        //			return;
        //		}
        //		if (currentWaypoint >= path.vectorPath.Count) {
        //			Debug.Log ("End Of Path Reached");
        //			return;
        //		}
        //Direction to the next waypoint
    }
Example #7
0
    public bool CheckActionViable(GoapAction action)
    {
        //check if working memory's list of failed actions
        WorkingMemoryFact fact = WorkingMemory.FindExistingFact(FactType.FailedAction, action.Name);
        if(fact != null)
        {
            return false;
        }
        else
        {
            return true;
        }

        return true;
    }
Example #8
0
	IEnumerator WaitAndExecuteNextAction(float waitTime)
	{
		yield return new WaitForSeconds(waitTime);

		//Debug.Log("Triggering action complete " + _parentCharacter.name);

		if(_actionQueue != null && _actionQueue.Count > 0)
		{
			_currentAction = _actionQueue.Dequeue();
			//CsDebug.Inst.CharLog(_parentCharacter, "After action completion, dequeued new action " + _currentAction.Name);
			if(_currentAction.ExecuteAction())
			{
				//action executed successfully
				return true;
			}
			else
			{
				_currentAction = null;
				FindAndExecuteAction();
			}
		}
		else
		{
			//CsDebug.Inst.CharLog(_parentCharacter, "No more actions, evaluating goal");
			//now there are no more actions; evaluate the goal again. if goal is reached, then find new goal; if goal has not reached, rerun planner
			//CsDebug.Inst.CharLog(_parentCharacter, "After action completion, looking for new action " + _currentAction.Name);
			_currentAction = null;
			FindAndExecuteAction();
		}

	}
 public void addAction(GoapAction a)
 {
     availableActions.Add(a);
 }
Example #10
0
	IEnumerator WaitAndCheckImportantEvent(float waitTime, float priority)
	{
		yield return new WaitForSeconds(waitTime);

		if(ControlType == AIControlType.Player)
		{
			return true;
		}

		//when an important event happens, stop current action and reevaluate all the goals and pick the highest priority goal
		Debug.Log("Triggering important event " + _parentCharacter.name);
		if(_currentAction != null)
		{
			if(_currentAction.AbortAction(priority))
			{
				_currentAction = null;
				_currentGoal = null;
				FindAndExecuteAction();
			}
		}
		else
		{
			_currentGoal = null;
			FindAndExecuteAction();
		}
	}
Example #11
0
 public void OnCurrentActionComplete()
 {
     //Debug.Log("Action completed");
     if(_actionQueue.Count > 0)
     {
         _currentAction = _actionQueue.Dequeue();
         if(_currentAction.ExecuteAction())
         {
             //action executed successfully
             return;
         }
         else
         {
             _currentAction = null;
             FindAndExecuteAction();
         }
     }
     else
     {
         //Debug.Log("No more actions, evaluating goal");
         //now there are no more actions; evaluate the goal again. if goal is reached, then find new goal; if goal has not reached, rerun planner
         _currentAction = null;
         FindAndExecuteAction();
     }
 }
Example #12
0
    private void FindAndExecuteAction()
    {
        //check if there is current goal and if the current goal is met
        //if met, find next goal
        //if not met, check if there's current action. if there is action leave it alone. if no action then calculate planner and execute first action
        //if no current goal, get next goal

        //Debug.Log("Start finding and executing action");

        if(_currentGoal == null)
        {
            Debug.Log("no current goal, getting a new one");
            _currentGoal = GetNextGoal();

            Debug.Log("found new goal " + _currentGoal.Name);
            _currentAction = null;
        }

        int counter = _goals.Count;
        while(counter > 0 && _currentGoal != null)
        {
            //Debug.Log("Find&Execute: checking goal " + _currentGoal.Name);
            bool needNextGoal = false;
            if(_currentAction == null && _currentGoal != null)
            {
                if(!EvaluateGoal(_currentGoal))
                {
                    //CsDebug.Inst.Log("current goal " + _currentGoal.Name + " is not met, running planner", CsDLevel.Info, CsDComponent.AI);
                    _actionQueue = Planner.GetActionQueue(_currentGoal, _actions);
                    if(_actionQueue != null && _actionQueue.Count > 0)
                    {
                        _currentAction = _actionQueue.Dequeue();
                        //Debug.Log("Found current action " + _currentAction.Name + " for goal " + _currentGoal.Name);
                        if(_currentAction.ExecuteAction())
                        {
                            //action executed successfully
                            return;
                        }
                    }
                    //failed to find action for current goal, get next goal
                    needNextGoal = true;
                }
                else
                {
                    needNextGoal = true;
                }
            }
            else
            {
                //there's action; leave it alone
                return;
            }

            if(needNextGoal)
            {
                _currentGoal = GetNextGoal();
                //Debug.Log("getting next goal; result: " + _currentGoal.Name);
                _currentAction = null;
            }

            counter --;
        }
    }
Example #13
0
 public GoapPathEdge(GoapAction action)
 {
     Action = action;
 }
Example #14
0
 public void planAborted(GoapAction aborter)
 {
     throw new System.NotImplementedException();
 }
Example #15
0
	public void removeAction(GoapAction action) {
		availableActions.Remove (action);
	}
Example #16
0
		public Node(Node parent, float runningCost, HashSet<KeyValuePair<string,object>> state, GoapAction action) {
			this.parent = parent;
			this.runningCost = runningCost;
			this.state = state;
			this.action = action;
		}
Example #17
0
	public static string prettyPrint(GoapAction action) {
		String s = ""+action.GetType().Name;
		return s;
	}
Example #18
0
	public void addAction(GoapAction a) {
		availableActions.Add (a);
	}
 public Node(Node parent, float runningCost, HashSet <KeyValuePair <string, object> > state, GoapAction action)
 {
     this.parent      = parent;
     this.runningCost = runningCost;
     this.state       = state;
     this.action      = action;
 }
Example #20
0
 public void PlanAborted(GoapAction abortedAction)
 {
 }
 public void removeAction(GoapAction action)
 {
     availableActions.Remove(action);
 }
Example #22
0
	public void OnOneSecondTimer()
	{
		//check if there is current goal. if there is none then get one
		if(ControlType != AIControlType.Player && _currentGoal == null)
		{
			_currentAction = null;

			FindAndExecuteAction();
		}
	}
Example #23
0
 public bool moveAgent(GoapAction nextAction)
 {
     throw new System.NotImplementedException();
 }
Example #24
0
	public void OnDeath()
	{
		if(_currentAction != null)
		{
			_currentAction.AbortAction(1);
			_currentAction = null;
		}

		_currentGoal = null;

		_parentCharacter.MyEventHandler.OnOneSecondTimer -= OnOneSecondTimer;
		TargetingSystem.OnDeath();
		Sensor.OnDeath();
		WorkingMemory.OnDeath();
	}
Example #25
0
 public void planAborted(GoapAction aborter)
 {
     Debug.Log("<color=red>Plan Aborted</color> " + GoapAgent.prettyPrint(aborter));
 }
Example #26
0
	public WorkingMemoryFact FindFailedActionFact(GoapAction action, object target)
	{
		foreach(WorkingMemoryFact f in Facts)
		{
			if(f.FactType == FactType.FailedAction && Object.Equals(f.Target, target) && f.PastAction == action.Name)
			{
				return f;
			}
		}

		return null;
	}
Example #27
0
	public void ForceStopCurrentAction()
	{
		if(_currentAction != null)
		{
			_currentAction.StopAction();
		}

		_currentAction = null;
		_currentGoal = null;
	}
Example #28
0
    public void Initialize(Character character)
    {
        _parentCharacter = character;

        WorkingMemory = new WorkingMemory();
        WorkingMemory.Initialize(_parentCharacter);

        BlackBoard = new BlackBoard();
        Sensor = new AISensor();
        Sensor.Initialize(_parentCharacter);
        TargetingSystem = new AITargeting();
        TargetingSystem.Initialize(_parentCharacter);
        WeaponSystem = new AIWeapon();
        WeaponSystem.Initialize(_parentCharacter);
        Planner = new GoapPlanner(this);

        _goals = GameManager.Inst.DBManager.DBHandlerAI.GetCharacterGoalSet(_parentCharacter.ID);
        _actions = GameManager.Inst.DBManager.DBHandlerAI.GetCharacterActionSet(_parentCharacter.ID);
        _currentWorldStates = new List<GoapWorldState>();

        _parentCharacter.MyEventHandler.OnNewEnemyTargetFound += OnImportantEvent;
        _parentCharacter.MyEventHandler.OnCurrentActionComplete += OnCurrentActionComplete;

        //update parent character for each action
        foreach(GoapAction action in _actions)
        {
            action.ParentCharacter = _parentCharacter;
        }

        BlackBoard.PatrolLoc = new Vector3(-15, 0, -15);
        BlackBoard.PatrolRange = new Vector3(20, 10, 20);
        BlackBoard.HasPatrolInfo = true;

        if(ControlType != AIControlType.Player)
        {
            _currentGoal = null;
            _currentAction = null;

            FindAndExecuteAction();
        }
    }
Example #29
0
	public bool CheckActionViable(GoapAction action)
	{
		//check if working memory's list of failed actions
		WorkingMemoryFact fact = WorkingMemory.FindExistingFact(FactType.FailedAction, action.Name);
		if(fact != null)
		{
			//CsDebug.Inst.CharLog(_parentCharacter, "Determined action " + action.Name + " as not viable");
			return false;
		}
		else
		{
			return true;
		}
	
		return true;
	}
Example #30
0
    //AI Events
    public void OnImportantEvent()
    {
        //when an important event happens, stop current action and reevaluate all the goals and pick the highest priority goal
        if(_currentAction != null)
        {
            _currentAction.StopAction();
            _currentAction = null;
        }

        _currentGoal = null;
        FindAndExecuteAction();
    }
Example #31
0
	public void Initialize(Character character)
	{
		_parentCharacter = character;

		WorkingMemory = new WorkingMemory();
		WorkingMemory.Initialize(_parentCharacter);

		BlackBoard = new BlackBoard();
		Sensor = new AISensor();
		Sensor.Initialize(_parentCharacter);
		TargetingSystem = new AITargeting();
		TargetingSystem.Initialize(_parentCharacter);
		WeaponSystem = new AIWeapon();
		WeaponSystem.Initialize(_parentCharacter);
		Planner = new GoapPlanner(this);


		_goals = GameManager.Inst.DBManager.DBHandlerAI.GetCharacterGoalSet(_parentCharacter.GoapID);
		_actions = GameManager.Inst.DBManager.DBHandlerAI.GetCharacterActionSet(_parentCharacter.GoapID);
		_currentWorldStates = new List<GoapWorldState>();

		_parentCharacter.MyEventHandler.OnCurrentActionComplete += OnCurrentActionComplete;
		_parentCharacter.MyEventHandler.OnPerFrameTimer += PerFrameUpdate;

		//update parent character for each action
		foreach(GoapAction action in _actions)
		{
			action.ParentCharacter = _parentCharacter;
		}

		//BlackBoard.PatrolLoc = new Vector3(63.9f, 0.3f, -13.3f);
		//BlackBoard.PatrolRange = new Vector3(30, 10, 15);

		if(ControlType != AIControlType.Player)
		{
			BlackBoard.GuardLevel = 1;
			_parentCharacter.SendCommand(CharacterCommands.SetAlert);
		}

		_currentGoal = null;
		_currentAction = null;


		_parentCharacter.MyEventHandler.OnOneSecondTimer += OnOneSecondTimer;
	
	}
Example #32
0
	public void SetDynamicyGoal(GoapGoal newGoal, int priority)
	{
		//CsDebug.Inst.CharLog(_parentCharacter, "Setting top priority goal " + newGoal.Name + " for " + _parentCharacter.name);
		List<GoapGoal> goalsCopy = new List<GoapGoal>(_goals);
		foreach(GoapGoal goal in goalsCopy)
		{
			if(goal.Priority == priority)
			{
				_goals.Remove(goal);
			}
		}

		_goals.Add(newGoal);

		if(_currentAction != null)
		{
			_currentAction.StopAction();
		}
		_currentAction = null;
		_currentGoal = newGoal;
		FindAndExecuteAction();
	}
    public static string prettyPrint(GoapAction action)
    {
        string s = "" + action.GetType().Name;

        return(s);
    }
Example #34
0
	public void ClearDynamicGoal(int priority)
	{
		//CsDebug.Inst.CharLog(_parentCharacter, "Clearing top priority goal for " + _parentCharacter.name);
		List<GoapGoal> goalsCopy = new List<GoapGoal>(_goals);
		foreach(GoapGoal goal in goalsCopy)
		{
			if(goal.Priority == priority)
			{
				if(_currentGoal == goal)
				{
					_currentAction.StopAction();
				}
				_goals.Remove(goal);
				_currentAction = null;
				_currentGoal = null;
				FindAndExecuteAction();
			}
		}

	}
Example #35
0
    public void ProcessMessage(string message)
    {
        Debug.Log("Json Message: " + message);
        GoapResposeObject obj = JsonConvert.DeserializeObject <GoapResposeObject>(message);

        Debug.Log(obj.ID);

        // enqueue the action list
        Queue <GoapAction> queue = new Queue <GoapAction>();
        List <GoapAction>  temp  = new List <GoapAction>();

        Debug.Log(obj.solutionList.Count);

        foreach (var a in obj.solutionList)
        {
            GoapAction tempAction;

            switch (a.type)
            {
            case "ChopFirewoodAction":
                tempAction = new ChopFirewoodAction();
                break;

            case "ChopTreeAction":
                tempAction = new ChopTreeAction();
                break;

            case "DropOffFirewoodAction":
                tempAction = new DropOffFirewoodAction();
                break;

            case "DropOffLogsAction":
                tempAction = new DropOffLogsAction();
                break;

            case "DropOffOreAction":
                tempAction = new DropOffOreAction();
                break;

            case "DropOffToolsAction":
                tempAction = new DropOffToolsAction();
                break;

            case "ForgeToolAction":
                tempAction = new ForgeToolAction();
                break;

            case "MineOreAction":
                tempAction = new MineOreAction();
                break;

            case "PickUpLogsAction":
                tempAction = new PickUpLogsAction();
                break;

            case "PickUpOreAction":
                tempAction = new PickUpOreAction();
                break;

            case "PickUpToolAction":
                tempAction = new PickUpToolAction();
                break;

            default:
                tempAction = new GoapAction();
                break;
            }

            foreach (var subList in a.preconditions)
            {
                if (subList[1] == "true")
                {
                    tempAction.addPrecondition(subList[0], true);
                }
                else
                {
                    tempAction.addPrecondition(subList[0], false);
                }
            }

            foreach (var subList in a.effects)
            {
                if (subList[1] == "true")
                {
                    tempAction.addEffect(subList[0], true);
                    Debug.Log("Effect key" + subList[0]);
                }
                else
                {
                    tempAction.addEffect(subList[0], false);
                }
            }

            tempAction.cost = a.cost;

            //Debug.Log(a.targetID);
            for (int i = 0; i < targets.Count; i++)
            {
                if (targetsID[i] == a.targetID)
                {
                    tempAction.targetID = a.targetID;
                    //Debug.Log("Match");
                }
            }

            //tempAction.checkProceduralPrecondition()
            temp.Add(tempAction);
            queue.Enqueue(tempAction);
        }

        foreach (var item in agents)
        {
            if (item.ID == obj.ID)
            {
                item.hasPlan = true;
                item.plan    = queue;
                Debug.Log("Set plan");
            }
        }
    }