// execute.c // // Version Date Author Description // -------------------------------------------------------------------------- // 0.01 28/07/01 BWW Execute the current plan #include #include "planner.h" #include "funcprototypes.h" extern Plan_Struct Plan; extern ActionEffects_Struct ActionEffects; extern Agent_Struct Agent; extern Key_Struct Key; extern Route_Struct Route; extern SearchTree_Struct SearchTree; extern SearchGoalNodes_Struct SearchGoalNodes; extern Door_Struct Door[DOORNUM]; extern UnmetPreconditions_Struct UnmetPreconditions; extern CausalLinks_Struct CausalLinks; extern Orderings_Struct Orderings; extern AvailablePrecons_Struct AvailablePrecons; extern AvailableActions_Struct AvailableActions; // Global variables for search routines ..... Temporary measure to get over parameter passing problems extern int startx, starty,destinationx, destinationy; // -------------------------------------------------------------------------- // Execute the plan // -------------------------------------------------------------------------- // Description: Execute the current plan // // Parameters: None // // Returns: None void ExecutePlan() { int index, index2; // int returncode; printf("ExecutePlan started\n"); // Check for a valid plan, if there isn't one, then create one if ((Plan.status==NOPLAN)||(Plan.status==FAILEDPLAN)) { printf("No valid plan, so create one\n"); InitExecution(); printf("InitExecution Done\n"); PlanBuild(); printf("Build Plan Done\n"); Plan.status=PLAN; } return; printf("Should have returned by now. Shouldn't see this message\n"); // Check for a current ActionEffect, get the next if there isn't one if (Plan.actioneffectindex==-1) { GetNextActionEffect(); } // Check to see that we still have a valid plan and excute the current step if (Plan.status == PLAN) { // Decrement the counter that indicates the length of time an actioneffect // takes to apply. Endeavour to apply the effects of the action to the // environment if it has reached zero. Plan.duration--; if (Plan.duration == 0) { // Check whether the current ActionEffect is a MOVETO. If it is then it has // multiple parts that need to be applied before it is considered complete if (ActionEffects.effect[Plan.actioneffectindex].condition == EFFECT_MOVETO) { // If the location is inaccessible, then the plan has failed. Otherwise move there if (CheckLocationForAccess(Route.x[Plan.currentstep],Route.y[Plan.currentstep])==0) { Plan.status = FAILEDPLAN; } else { Agent.x = Route.x[Plan.currentstep]; Agent.y = Route.y[Plan.currentstep]; // If the agent has the key then set it's location as well if (Agent.gotkey==true) { Key.x = Agent.x; Key.y = Agent.y; } } // Imcrement the current location along the route. Check for the end and get the duration // of the next step Plan.currentstep++; if (Plan.currentstep == Route.numsteps) { Plan.actioneffectindex = -1; } else { Plan.duration = Route.cost[Plan.currentstep]; } } // Check again that the duration is still zero to see whether we have // reached the end of the MOVETO. MOVETO may also have failed, so also // check for plan failure if ((Plan.duration == 0)&&(Plan.status==PLAN)) { // Apply the effects of the action to the environment switch(ActionEffects.effect[Plan.actioneffectindex].condition) { case EFFECT_MOVETO: break; case EFFECT_UNLOCK: Door[ActionEffects.effect[Plan.actioneffectindex].value1].lockstatus=DOORUNLOCKED; break; case EFFECT_OPEN: Door[ActionEffects.effect[Plan.actioneffectindex].value1].doorstatus=DOOROPEN; break; case EFFECT_PICKUP: Agent.gotkey=true; break; case EFFECT_WAIT: break; } // Update the Causal Links for (index=0;index