void CreateIdleState() { _idleState = (fsm, gameObj) => { // GOAP planning // get the world state and the goal we want to plan for var worldState = _dataProvider.GetWorldState(); var goal = _dataProvider.CreateGoalState(); // Plan Queue <GoapAction> plan = _planner.Plan(gameObject, _availableActions, worldState, goal); if (plan != null) { // we have a plan, hooray! _currentActions = plan; _dataProvider.PlanFound(goal, plan); fsm.PopState(); // move to PerformAction state fsm.PushState(_performActionState); } else { // ugh, we couldn't get a plan Debug.Log($"<color=orange>Failed Plan:</color>{PrettyPrint(goal)}"); _dataProvider.PlanFailed(goal); fsm.PopState(); // move back to IdleAction state fsm.PushState(_idleState); } }; }
void CreateIdleState() { idleState = (fsm, gameObj) => { HashSet <KeyValuePair <string, object> > worldState = dataProvider.GetWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.CreateGoalState(); Queue <GoapAction> plan = planner.Plan(gameObject, availableActions, worldState, goal); if (plan != null) { currentActions = plan; dataProvider.PlanFound(goal, plan); fsm.PopState(); fsm.PushState(performActionState); } else { Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal)); dataProvider.PlanFailed(goal); fsm.PopState(); fsm.PushState(idleState); } }; }
/// <summary> /// Formulates the plan based on availble actions, world state, etc /// </summary> private void CreateIdleState() { idleState = (fsm, gameObj) => { // GOAP planning // get the world state and the goal we want to plan for HashSet <KeyValuePair <string, object> > worldState = dataProvider.GetWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.CreateGoalState(); if (goal.Contains(new KeyValuePair <string, object>("expandStorage", true))) { Debug.Log("found builder"); } // Plan Queue <GOAPAction> plan = planner.Plan(gameObject, availableActions, worldState, goal); if (plan != null) { // we have a plan, hooray! currentActions = plan; dataProvider.PlanFound(goal, plan); fsm.PopState(); // move to PerformAction state fsm.PushState(prefromActionState); } else { // ugh, we couldn't get a plan // Debug.Log("<color=orange>Failed Plan:</color> " + prettyPrint(goal)); dataProvider.PlanFailed(goal, currentActions); fsm.PopState(); // move back to IdleAction state fsm.PushState(idleState); } }; }
private void createIdleState() { _idleState = (fsm, gameObj) => { // GOAP planning // get the world state and the goal we want to plan for Dictionary <string, object> worldState = _dataProvider.GetWorldState(); Dictionary <Int64, Int64> resourceState = _dataProvider.GetResourceState(); Dictionary <string, object> goal = _dataProvider.CreateGoalState(); Dictionary <Int64, Int64> resourceGoal = _dataProvider.CreateResourceGoal(); loadActions(); // Plan Queue <GoapAction> plan = _planner.Plan(this, _availableActions, worldState, resourceState, goal, resourceGoal); if (plan != null) { // we have a plan, hooray! _currentActions = plan; _dataProvider.PlanFound(goal, plan); fsm.popState(); // move to PerformAction state fsm.pushState(_performActionState); } else { // ugh, we couldn't get a plan // Console.WriteLine("<color=orange>Failed Plan:</color>" + PrettyPrint(goal)); _dataProvider.PlanFailed(goal); fsm.popState(); // move back to IdleAction state fsm.pushState(_idleState); } }; }
private void ActionState() { act = (fsm, gameObj) => { if (currentActions.Count == 0) { fsm.popState(); fsm.pushState(idle); return; } GoapAction action = currentActions.Peek(); if (!action.init) { if (!action.inWait) { bool success = action.IsSucc(); if (!success) { fsm.popState(); fsm.pushState(idle); goapAgent.PlanAborted(action); action.DoReset(); return; } if (action.IsDone()) { action.DoReset(); goapAgent.ActionFinished(action); currentActions.Dequeue(); } } else { return; } } if (currentActions.Count > 0) { action = currentActions.Peek(); if (action.IsInRange()) { action.DoAction(gameObj); action.init = false; } else { fsm.pushState(move); } } else { fsm.popState(); fsm.pushState(idle); } }; }
private void CreateIdleState() { IdleState = (fsm, agent) => { Dictionary <Tuple <string, object>, object> WorldState = DataProvider.GetWorldState(); List <KeyValuePair <Tuple <string, object>, object> > Goal = DataProvider.CreateGoalState(); Queue <GOAPAction> Plan = Planner.Plan(agent, AvailableActions, WorldState, Goal); if (Plan != null) { //AddThought("Plan created for goal " + Goal.Keys.First(), Color.DarkBlue); AddThought("Plan created for goal " + Goal[0].Key.ToString(), Color.DarkBlue); CurrentActions = Plan; DataProvider.PlanFound(Goal[0], Plan); fsm.PopState(); fsm.PushState(PerformActionState); } else { DataProvider.PlanFailed(Goal[0]); fsm.PopState(); fsm.PushState(IdleState); } }; }
public void CrearEstadoActuar() { ActState = (fsm, gameObj) => { //ejecutar la accion if (AccionesActuales.Count <= 0) // no tengo un plan { fsm.popState(); fsm.pushState(idleState); goapData.ActionsFinished(); return; } //si si tengo acciones entronces objetco la primeroa GoapAction accion = AccionesActuales.Peek(); if (accion.isDone()) { //si ya se termino la accion la lquito AccionesActuales.Dequeue(); } //si no ha terminad oy quedan acciones hay que ejecutrarl if (AccionesActuales.Count > 0) { accion = AccionesActuales.Peek(); //verifico si requiere estar en un rango}(cerca de su objetivo) bool enRango = accion.requiresInRange() ? accion.IsInRange() : true; if (enRango) { bool exito = accion.Perform(gameObj); // sil a accion no se ejecuto if (!exito) { //planeo otra vez fsm.popState(); //sjalgo de acturar y vuelvo a idle fsm.pushState(idleState); goapData.PlanAborted(accion); } } else { //no esta en donde deberia estar, no esta en rango Debug.Log("estoy lejos del objetivo"); fsm.pushState(MoveState); } } else { //no quedan acciones, entonces puedo volver a planear fsm.popState(); fsm.pushState(idleState); goapData.ActionsFinished(); } }; }
private void createIdleState() { idleState = (fsm, gameObj) => { // GOAP planning // Get the world state and the goal we want to plan for HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.createGoalState(); // Make a plan with the actions the AI wants to do in order Queue <GOAPAction> plan = planner.plan(gameObject, availableActions, worldState, goal); if (plan != null) { // We have a plan! currentActions = plan; dataProvider.planFound(goal, plan); fsm.popState(); // Clear current state fsm.pushState(performActionState); // Change to performActionState } else { // We don't have a plan dataProvider.planFailed(goal); fsm.popState(); fsm.pushState(idleState); // Back to square one } }; }
public void CrearEstadoMoverse() { MoveState = (fsm, gameObj) => { GoapAction accion = AccionesActuales.Peek(); // mover el agenbte hacia el objetivo de la accion si es que tiene if (accion.requiresInRange() && accion.Target == null) { Debug.Log("la accion drequere un targetm pero no tiene"); fsm.popState(); // salri de actuar fsm.popState(); //salir de movers fsm.pushState(idleState); return; } //que se mueva if (goapData.MoveAgent(accion)) { fsm.popState(); } ////////////////////////////////////////////////////////////script de movimiento o de lo que quieras///////////////////////////////////////// gameObj.transform.position = Vector3.MoveTowards( gameObj.transform.position, accion.Target.transform.position, Time.deltaTime * 5 ); if (Vector3.Distance(gameObj.transform.position, accion.Target.transform.position) < 1f) { //llega al objetivo accion.SetInRange(true); fsm.popState();//salirde moverse } }; }
public void CrearEstadoIdle() { //Este estado lo usa el agente para calcular un plan idleState = (fsm, gameObj) => { //planeacion goap //obtener el estado del mundo Dictionary <string, object> worldState = goapData.GetWorldState(); //obtener la meta dle agente Dictionary <string, object> goal = goapData.CreateGoalState(); //crear un plan Queue <GoapAction> plan = Planeador.ElPlan( gameObject, AccionesDisponibles, worldState, goal); //logramos tener un plan? if (plan != null) { Debug.Log("tengo un plan"); AccionesActuales = plan; goapData.PlanFound(goal, plan); maquinaDeEstados.popState(); //saca el estado idle maquinaDeEstados.pushState(ActState); //pasa al estado de actuar } else { Debug.Log("no tengo plan"); goapData.PlanFailed(goal); maquinaDeEstados.popState(); maquinaDeEstados.pushState(idleState); // para que vuelva a intentar calcular el plan } }; }
private void createMoveToState() { moveToState = (fsm, gameObj) => { // move the game object GoapAction action = currentActions.Peek(); if (action.requiresInRange() && action.target == null) { Debug.Log( "<color=red>Fatal error:</color> Action requires a target but has none. Planning failed." + " You did not assign the target in your Action.checkProceduralPrecondition()"); fsm.popState(); // move fsm.popState(); // perform fsm.pushState(idleState); return; } // get the agent to move itself if (dataProvider.moveAgent(action)) { fsm.popState(); } }; }
private void CreateIdleState() { idleState = (fsm, gameObj) => { // GOAP planning // get the world state and the goal we want to plan for Dictionary <string, object> worldState = dataProvider.GetWorldState(); KeyValuePair <string, object> goal = dataProvider.GetCurrentGoal(); // search enable Plan Queue <GoapAction> plan = planner.Plan(gameObject, availableActions, worldState, goal, dataProvider); if (plan != null) { // we have a plan, hooray! currentActions = plan; dataProvider.PlanFound(goal, plan); fsm.PopState(); // move to PerformAction state fsm.PushState(performActionState); } else { // ugh, we couldn't get a plan Debug.Log("[" + this.name + "] " + "<color=orange>Plan failed:</color> " + PrettyPrint(goal) + " = NO PLAN"); dataProvider.PlanFailed(goal); fsm.PopState(); // move back to IdleAction state fsm.PushState(idleState); } }; }
private void CreateIdleState() { idleState = (fsm, gameObj) => { // GOAP Planning Dictionary <string, object> worldState = agent.WorldState(); Dictionary <string, object> goal = agent.CreateGoalState(); Queue <GoapAction> plan = GoapPlanner.Plan(gameObject, availableActions, worldState, goal); if (plan != null) { currentPlan = plan; agent.PlanFound(goal, plan); fsm.PopState(); fsm.PushState(performActionState); } else { Debug.Log("<color=orange>Failed Plan:</color>" + goal); agent.PlanFailed(goal); fsm.PopState(); fsm.PushState(idleState); } }; }
private void createIdleState() { idleState = (fsm, gameObj) => { // Get the world state and the goal Dictionary <string, object> worldState = dataProvider.getWorldState(); Dictionary <string, object> goal = dataProvider.createGoalState(); // Generate the plan using GOAP Planner Queue <GoapAction> plan = planner.plan(gameObject, availableActions, worldState, goal); if (isSelected) { setMetrics(); } if (plan != null) { // Plan found currentActions = plan; if (isSelected) { generatePlanPanel(); } dataProvider.planFound(goal, plan); fsm.popState(); // move to PerformAction state fsm.pushState(performActionState); } else { // No plan Debug.Log("<color=orange>Failed Plan:</color>" + prettyPrint(goal)); dataProvider.planFailed(goal); fsm.popState(); // move back to IdleAction state fsm.pushState(idleState); } }; }
// Creating states for the GOAP FSM. private void createIdleState() { idleState = (fsm, gameObj) => { // Get world state and goal from data provider. Dictionary <string, object> worldState = dataProvider.getWorldState(); Dictionary <string, object> goal = dataProvider.createGoalState(); Queue <GoapAction> plan = planner.plan(gameObj, availableActions, worldState, goal); if (plan != null) { // Found a plan. Providing it to the data provider. currentActions = plan; dataProvider.planFound(goal, plan); // Move FSM to performAction state. fsm.popState(); fsm.pushState(performActionState); } else { // No plan found for goal. Debug.Log("<color=orange>Failed plan:</color> " + prettyPrint(goal)); dataProvider.planFailed(goal); // Going back to idleState. fsm.popState(); fsm.pushState(idleState); } }; }
private void createIdleState() { //this is a lambda expression nameless/anonymous delegate thingy of type FSM.FSMState idleState = (fsm, gameObj) => { //HashSet<KeyValuePair<string, object>> worldState = dataProvider.getWorldState(); //HashSet<KeyValuePair<string, object>> goal = dataProvider.createGoalState(); Profiler.BeginSample("Gathering and ordering goals by priority "); List <Condition> originalState = dataProvider.GetWorldState(); List <Goal> goals = dataProvider.GetGoalState(); goals = OrderByPriority(goals); //Debug.Log("WORLD STATE " + prettyPrint(originalState)); //Debug.Log("GOAL STATE " + prettyPrint(goals)); Profiler.EndSample(); Profiler.BeginSample("Begin Planning -- for loop through goals and calling planner"); Queue <GoapAction> plan = null; foreach (Goal ourGoal in goals) { //this cycles through the goals by priority until it finds one that works with a plan plan = planner_.Plan(ourGoal, availableActions_, originalState, this.gameObject); if (plan != null) { //if you find a plan that works, break and continue on //Debug.Log("<color=cyan>Plan found!:</color>" + prettyPrint(plan) + " for " + gameObj.name); break; } else { } } Profiler.EndSample(); // planner_.Plan(goals, availableActions_, originalState, this.gameObject); Profiler.BeginSample("Checking to see if plan is null/failed and returning to idle if so"); if (plan != null) { currentActions = plan; dataProvider.PlanFound(goals, plan); fsm.popState(); // move to PerformAction state fsm.pushState(performActionState); } else { // ugh, we couldn't get a plan //Debug.Log("<color=orange>Failed Plan:</color>" + prettyPrint(goals) + " for " + gameObj.name); dataProvider.PlanFailed(goals); fsm.popState(); // move back to IdleAction state fsm.pushState(idleState); } Profiler.EndSample(); }; }
private void createIdleState() { idleState = (fsm, obj) => { //Get the world state and goals HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.createGoalState(); //Get the plan Queue <AbstractGOAPAction> plan = planner.plan(gameObject, availableActions, worldState, goal); if (plan != null) { //Set the actions to the actions in the plan currentActions = plan; //Tell agent it found a plan dataProvider.planFound(goal, plan); //Pop the idlestate fsm.popState(); //Start performing actions fsm.pushState(performActionState); } else { //Tell agent plan failed dataProvider.planFailed(goal); //Try to find a new plan fsm.popState(); fsm.pushState(idleState); } }; }
private void IdleState() { idle = (fsm, gameObj) => { List <KeyValuePair <string, object> > worldState = goapAgent.GetWorldState(); KeyValuePair <string, object> goal = goapAgent.GetSubGoals(); planner.Reset(); if (!goal.Equals(new KeyValuePair <string, object>())) { Queue <GoapAction> plan = planner.Plan(availableActions, worldState, goal); if (plan != null) { // we have a plan, hooray! currentActions = plan; goapAgent.PlanFound(goal, plan); fsm.popState(); fsm.pushState(act); } else { goapAgent.PlanFailed(goal); fsm.popState(); fsm.pushState(idle); } } else { goapAgent.GameFinished(); } }; }
private void createIdleState() { idleState = (fsm, gameObj) => { // GOAP planning // get the world state and the goal we want to plan for HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.createGoalState(); // Plan Queue <GoapAction> plan = planner.plan(gameObject, availableActions, worldState, goal); if (plan != null) { // we have a plan, hooray! currentActions = plan; dataProvider.planFound(goal, plan); fsm.popState(); // move to PerformAction state fsm.pushState(performActionState); } else { // ugh, we couldn't get a plan Debug.Log("<color=orange>Failed Plan:</color>" + prettyPrint(goal)); dataProvider.planFailed(goal); fsm.popState(); // move back to IdleAction state fsm.pushState(idleState); } }; }
private void createPerformActionState() { performActionState = (fsm, gameObj) => { // perform the action //Debug.Log("PERFORMstate"); if (!hasActionPlan()) { // no actions to perform Debug.Log("<color=red>Done actions</color>"); fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); return; } GoapAction action = currentActions.Peek(); if (action.isDone()) { // the action is done. Remove it so we can perform the next one currentActions.Dequeue(); } if (hasActionPlan()) { // perform the next action action = currentActions.Peek(); bool inRange = action.requiresInRange() ? action.isInRange() : true; //Debug.Log(inRange); if (inRange) { // we are in range, so perform the action bool success = action.perform(gameObj); if (!success) { // action failed, we need to plan again fsm.popState(); fsm.pushState(idleState); dataProvider.planAborted(action); } } else { // we need to move there first // push moveTo state fsm.pushState(moveToState); } } else { // no actions left, move to Plan state fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); } }; }
private void createPerformActionState() { performActionState = (fsm, gameObj) => { if (!hasActionPlan()) { // No actions Debug.Log("<color=red>Done actions</color>"); fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); return; } GoapAction action = currentActions.Peek(); if (action.isDone()) { // Action is done currentActions.Dequeue(); metrics["actionsDone"]++; if (isSelected) { generatePlanPanel(); } } if (hasActionPlan()) { // Perform the next action action = currentActions.Peek(); bool inRange = action.requiresInRange() ? action.isInRange() : true; if (inRange) { bool success = action.perform(gameObj); if (!success) { // Action failed fsm.popState(); fsm.pushState(idleState); dataProvider.planAborted(action); metrics["abortedPlans"]++; } } else { // Move to target fsm.pushState(moveToState); } } else { // No actions fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); } }; }
public void Intro() { introState = (FSM, gameObj) => { nemesisTrigger.Scene(); stateMachine.Push(wanderState); }; }
public void ResetPlans() { resetActionState = (fsm, obj) => { fsm.popState(); fsm.pushState(idleState); }; }
//bool isPlanning = false; //HashSet<KeyValuePair<string, object>> worldState; //HashSet<KeyValuePair<string, object>> goal; void CreateIdleState() { idleState = (fsm, gameObj) => { HashSet <KeyValuePair <string, object> > worldState = dataProvider.GetWorldState(); HashSet <KeyValuePair <string, object> > goal = dataProvider.CreateGoalState(); Queue <GoapAction> plan = planner.Plan(gameObject, availableActions, worldState, goal); if (plan != null) { currentActions = plan; dataProvider.PlanFound(goal, plan); fsm.PopState(); fsm.PushState(performActionState); } else { Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal)); dataProvider.PlanFailed(goal); fsm.PopState(); fsm.PushState(idleState); } //if (!isPlanning) //{ // worldState = dataProvider.GetWorldState(); // goal = dataProvider.CreateGoalState(); // planner.donePlanning = false; // planner.finalPlan = null; // StartCoroutine(planner.Plan(gameObject, availableActions, worldState, goal, this)); // isPlanning = true; //} //else //{ // if (planner.donePlanning) // { // if (planner.finalPlan != null) // { // currentActions = planner.finalPlan; // dataProvider.PlanFound(goal, planner.finalPlan); // fsm.PopState(); // fsm.PushState(performActionState); // } // else // { // Debug.Log("<color=orange>Failed Plan</color>" + PrettyPrint(goal)); // dataProvider.PlanFailed(goal); // fsm.PopState(); // fsm.PushState(idleState); // } // isPlanning = false; // } //} }; }
private void CreateFleeState() { FleeState = (fsm, agent) => { if (ShouldFlee()) { } }; }
void CreatePerformActionState() { _performActionState = (fsm, gameObj) => { // perform the action if (!HasActionPlan()) { // no actions to perform Debug.Log("<color=red>Done actions</color>"); fsm.PopState(); fsm.PushState(_idleState); _dataProvider.ActionsFinished(); return; } GoapAction action = _currentActions.Peek(); if (action.IsDone()) { // the action is done. Remove it so we can perform the next one _currentActions.Dequeue(); } if (HasActionPlan()) { // perform the next action action = _currentActions.Peek(); bool inRange = action.RequiresInRange() ? action.IsInRange() : true; if (inRange) { // we are in range, so perform the action bool success = action.Perform(gameObj); if (!success) { // action failed, we need to plan again fsm.PopState(); fsm.PushState(_idleState); _dataProvider.PlanAborted(action); } } else { // we need to move there first // push moveTo state fsm.PushState(_moveToState); } } else { // no actions left, move to Plan state fsm.PopState(); fsm.PushState(_idleState); _dataProvider.ActionsFinished(); } }; }
private void CreatePerformActionState() { _performState = (fsm, gameObject) => { Debug.Log("Performing --"); // Perform the action if (!HasActionPlan()) { // no actions left _fsm.PopState(); _fsm.PushState(_idleState); _dataProvider.ActionsCompleted(); return; } var action = _currentActions.Peek(); if (action.IsDone()) { // action completed, remove and continue w/ sequence _currentActions.Dequeue(); } if (HasActionPlan()) { action = _currentActions.Peek(); bool inRange = !action.RequiresInRange || action.IsInRange; if (inRange) { bool actionSuccess = action.Perform(gameObject); if (!actionSuccess) { // action failed, plan again _fsm.PopState(); _fsm.PushState(_idleState); _dataProvider.PlanAborted(action); } } else { // we ened to move there first? // what? _fsm.PushState(_moveState); } } else { // no actions left _fsm.PopState(); _fsm.PushState(_idleState); _dataProvider.ActionsCompleted(); } }; }
private void MoveState() { move = (fsm, gameObj) => { GoapAction action = currentActions.Peek(); if (goapAgent.MoveAgent(action)) { fsm.popState(); } }; }
//Animation state plays an animation such as shooting, swinging sword. The effect of the animation should be programmed outside of the GOAP system. //The GOAP system only plays animations and moves the agent void animateStateInit() { animateState = (fsm, gameObj) => { GOAPAction action = actionQueue.Peek(); if (action.isDone(this) && actionActivated) { // the action is done. Remove it so we can perform the next one actionActivated = false; actionQueue.Dequeue(); if (actionQueue.Count == 0) { fsm.popState(); fsm.pushState(idleState); } } else { if (action.requiresInRange) { //If an action requires to be in range of a type of object (set by the actions targetTag), we must find one and go to it before playing the animation target = FindClosestTarget(action.targetTag); if (target == null) { //No target. Action failed fsm.popState(); fsm.pushState(idleState); } else { //Only go to the target object if out of range if (Vector3.Distance(target.transform.position, this.transform.position) > action.range) { targetSet = false; fsm.pushState(goToState); } else if (action != currentAction) { //Otherwise activate the action currentAction = action; currentAction.activate(this); actionActivated = true; } } } else if (currentAction != action) { //Activate the current action currentAction = action; currentAction.activate(this); actionActivated = true; } } }; }
private void createPerformActionState() { performActionState = (fsm, gameObj) => { // Perform the action if (!hasActionPlan()) { // No actions to perform fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); return; } GOAPAction action = currentActions.Peek(); if (action.isDone()) { // The action is done. We can remove it, onto the next one! currentActions.Dequeue(); } if (hasActionPlan()) { // Perform the next action action = currentActions.Peek(); bool inRange = action.requiresInRange() ? action.isInRange() : true; if (inRange) { // We're in range, so perform the action bool success = action.perform(gameObj); if (!success) { // Action failed, we need to plan again fsm.popState(); fsm.pushState(idleState); dataProvider.planAborted(action); } } else { // We need to move to our target first // Let's make the AI move fsm.pushState(moveToTargetState); } } // If all fails, change back to idleState (planning stage) else { fsm.popState(); fsm.pushState(idleState); dataProvider.actionsFinished(); } }; }