public void PlayMove() { while (finalPlan.Count > 0) { PrimitiveTask nextAction = finalPlan.Pop(); if (nextAction.PrimitiveConditionsMet(game.GetCurrentState())) { nextAction.executeTask(this); } else { plan(game.GetCurrentState()); break; } } plan(game.GetCurrentState()); }
public void plan(GameController.WorldState currentWorldState) { finalPlan = new Stack <PrimitiveTask>(); Stack <PlannerState> decompHistory = new Stack <PlannerState>(); GameController.WorldState WorkingWS = currentWorldState.Copy(); tasksToProcess.Push(new PlayGame()); while (tasksToProcess.Count > 0) { Task CurrentTask = tasksToProcess.Pop(); if (CurrentTask.GetType().IsSubclassOf(typeof(CompoundTask))) { CompoundTask CurrentCompoundTask = (CompoundTask)CurrentTask; Method SatisfiedMethod = CurrentCompoundTask.FindSatisfiedMethod(WorkingWS); if (SatisfiedMethod != null) { //PlannerState currentState = new PlannerState(CurrentCompoundTask, finalPlan, tasksToProcess, SatisfiedMethod); // decompHistory.Push(currentState); SatisfiedMethod.subTasks.Reverse(); foreach (Task t in SatisfiedMethod.subTasks) { tasksToProcess.Push(t); } SatisfiedMethod.subTasks.Reverse(); } else if (decompHistory.Count > 0) { //RestoreToLastDecomposedTask(): PlannerState lastState = decompHistory.Pop(); CompoundTask lastCompoundTask = lastState.currentTask; finalPlan = lastState.finalPlan; tasksToProcess = lastState.tasksToProcess; // Remove the failed method and return CompoundTask to the stack. On the next iteration, it will be checked again for a valid method. lastCompoundTask.InvalidateMethod(lastState.currentMethod); tasksToProcess.Push(lastCompoundTask); } } else//Primitive Task { PrimitiveTask CurrentPrimitiveTask = (PrimitiveTask)CurrentTask; if (CurrentPrimitiveTask.PrimitiveConditionsMet(WorkingWS)) { // CurrentPrimitiveTask.ApplyEffects(this, WorkingWS); // Add this PrimitiveTask to the bottom of the finalPlan Stack <PrimitiveTask> temp = new Stack <PrimitiveTask>(); for (int i = 0; i < finalPlan.Count; i++) { PrimitiveTask nextTask = finalPlan.Pop(); temp.Push(nextTask); } temp.Push(CurrentPrimitiveTask); finalPlan = new Stack <PrimitiveTask>(); for (int i = 0; i < temp.Count; i++) { PrimitiveTask nextTask = temp.Pop(); finalPlan.Push(nextTask); } } else if (decompHistory.Count > 0) { //RestoreToLastDecomposedTask(); PlannerState lastState = decompHistory.Pop(); CompoundTask lastCompoundTask = lastState.currentTask; finalPlan = lastState.finalPlan; tasksToProcess = lastState.tasksToProcess; // Remove the failed method and return CompoundTask to the stack. On the next iteration, it will be checked again for a valid method. lastCompoundTask.InvalidateMethod(lastState.currentMethod); tasksToProcess.Push(lastCompoundTask); } } } }