home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 05 Hancock / GenerateGoals.cpp < prev    next >
Encoding:
Text File  |  2001-09-17  |  2.7 KB  |  66 lines

  1.  
  2. //returns length of path, negative if no path exists
  3. float AI::GeneratePathingGoals(const PathNode *from, const PathNode *to,
  4.                             GoalQueue &goals, float maxDistance)
  5. {
  6.     if (!from->PathExists(to, Character())) //make sure that 'to' is reachable from 'from' for our character
  7.         return -1.0f;
  8.  
  9.     std::vector<const PathLink *> path;
  10.     float pathLength = g_NodeMap.FindPath(Character(), from, to, path);
  11.     if (pathLength > 0) //if we found a valid path
  12.         GenerateGoalsFromPath(path, goals);
  13.     
  14.     return pathLength;
  15. }
  16.  
  17. void AI::GenerateGoalsFromPath(const std::vector<const PathLink *>& path, GoalQueue& goals) {
  18.     //NOTE: path[0] is final link! (the path is reversed)
  19.     //path[0]->End() is the goal node
  20.  
  21.     bool replanForDoorOrElevator = false;
  22.     int i = 0; //we start at the end of the path and keep inserting goals at the front of the queue
  23.  
  24.     const PathNode *currentGotoNode = 0;
  25.     while (i < (int)path.size() && !replanForDoorOrElevator){
  26.  
  27.         if (path[i]->flags & PathLink::flagLinkDoor){ //if this link has a door on it...
  28.             const Door *door = (path[i]->GetDoor());
  29.             replanForDoorOrElevator = true;
  30.             goals.InsertSubgoal(new Goal_GoThroughDoor(this, path[i]));
  31.         }
  32.         else if (path[i]->flags & PathLink::flagLinkElevator){ //if this link has an elevator
  33.             const Elevator *elevator  = path[i]->End()->GetElevator();
  34.             replanForDoorOrElevator = true;
  35.             const PathNode *lastNode = path[i]->End(); //this is the last node inside the elevator
  36.  
  37.             //go through multiple elevator floors until we reach the first elevator link
  38.             while ( (i < (int)path.size())  &&  path[i]->flags & PathLink::flagLinkElevator ){
  39.                 i++;
  40.             }
  41.             const PathLink *entryLink = NULL; //this is the link used to enter the elevator
  42.             if (i < (int)path.size()) //as long as we aren't past the beginning of the path, set entryLink
  43.                 entryLink = path[i];
  44.             goals.InsertSubgoal(new Goal_RideElevator(this, entryLink, lastNode));
  45.         }
  46.         else {
  47.             // if the link can be shortcut and we're not currently shortcutting or we can see the
  48.             //current shortcut goal from the link start then consider generating a shortcut goal
  49.             if (path[i]->CanBeShortcut() &&
  50.                 (!currentGotoNode || PathClear(path[i]->Start()->pos, currentGotoNode->pos) ) )
  51.             {
  52.                 //if we don't have a node we're currently shortcutting to, add one
  53.                 if (!currentGotoNode){
  54.                     //all of these goals are subgoals, so they should never trigger a stop
  55.                     goals.InsertSubgoal(new Goal_GotoNode(this, path[i]->End(), true )); //the optional last argument forces a direct path
  56.                     currentGotoNode = path[i]->End();
  57.                 }
  58.             }
  59.             else {
  60.                 goals.NewSubgoal(new Goal_FollowLink(this, path[i] ));
  61.                 currentGotoNode = 0;
  62.             }
  63.         }
  64.         i++;
  65.     }
  66. }