Results 1 to 2 of 2

Thread: Few quetions about astar waypoints pathfinding

  1. #1
    Private First Class RobsoN's Avatar
    Join Date
    Jan 2013
    Location
    /home/cod2/
    Posts
    230
    Thanks
    119
    Thanked 95 Times in 64 Posts

    Lightbulb Few quetions about astar waypoints pathfinding

    Hello, I'm new in pathfinding. I basically know how it is working in a netting thing (1block = node), but I have no idea how to implement that into cod2. I want to try easiest method i.e. astar waypoints. I did my pathfinding script but it was really messed up (It required a lot of things to add into script manually). I have few questions about that:
    1. What exactly is a node (in cod2 ver)?
    2. How can I check on which node entity is?
    3. What do I need to add manually into script if i would use astar waypoint algorithm (node origin?, node connections? waypoint origin?, waypoint connections?)

    Regards RobsoN.
    "Don't worry if your code doesn't work correctly - if everything worked, you would not work" ~Mosher's right

  2. #2
    Brigadier General
    Join Date
    Oct 2012
    Posts
    994
    Thanks
    20
    Thanked 588 Times in 388 Posts
    Quote Originally Posted by RobsoN View Post
    Hello, I'm new in pathfinding. I basically know how it is working in a netting thing (1block = node), but I have no idea how to implement that into cod2. I want to try easiest method i.e. astar waypoints. I did my pathfinding script but it was really messed up (It required a lot of things to add into script manually). I have few questions about that:
    1. What exactly is a node (in cod2 ver)?
    2. How can I check on which node entity is?
    3. What do I need to add manually into script if i would use astar waypoint algorithm (node origin?, node connections? waypoint origin?, waypoint connections?)
    1. a "node" is a point in a map which AI can intersect with. In COD terms it is a spawn_struct which an AI will follow.

    2. If I take your meaning correctly, you are asking how to check which node an entity should follow. For that you need an algorithm which determines which node will be best for the AI. So, I will post Pezzalucifer's astarSearch() function from Pezbot:

    PHP Code:
    ////////////////////////////////////////////////////////////
    // AStarSearch, performs an astar search
    ///////////////////////////////////////////////////////////
    /*

        The best-established algorithm for the general searching of optimal paths is A* (pronounced “A-star”). 
        This heuristic search ranks each node by an estimate of the best route that goes through that node. The typical formula is expressed as:

        f(n) = g(n) + h(n)

        where: f(n) is the score assigned to node n g(n) is the actual cheapest cost of arriving at n from the start h(n) is the heuristic 
        estimate of the cost to the goal from n 

        priorityqueue Open
        list Closed


        AStarSearch
           s.g = 0  // s is the start node
           s.h = GoalDistEstimate( s )
           s.f = s.g + s.h
           s.parent = null
           push s on Open
           while Open is not empty
              pop node n from Open  // n has the lowest f
              if n is a goal node 
                 construct path 
                 return success
              for each successor n' of n
                 newg = n.g + cost(n,n')
                 if n' is in Open or Closed,
                  and n'.g < = newg
                   skip
                 n'.parent = n
                 n'.g = newg
                 n'.h = GoalDistEstimate( n' )
                 n'.f = n'.g + n'.h
                 if n' is in Closed
                    remove it from Closed
                 if n' is not yet in Open
                    push n' on Open
              push n onto Closed
           return failure // if no path found 
    */
    AStarSearchstartWpgoalWp )
    {
        
    pQOpen = [];
        
    pQSize 0;
        
    closedList = [];
        
    listSize 0;
        
    spawnstruct();
        
    s.0//start node
        
    s.distancelevel.waypoints[startWp].originlevel.waypoints[goalWp].origin );
        
    s.s.s.h;
        
    s.wpIdx startWp;
        
    s.parent spawnstruct();
        
    s.parent.wpIdx = -1;
      
        
    //push s on Open
        
    pQOpen[pQSize] = spawnstruct();
        
    pQOpen[pQSize] = s//push s on Open
        
    pQSize++;

        
    //while Open is not empty  
        
    while( !PQIsEmpty(pQOpenpQSize) )
        {
            
    //pop node n from Open  // n has the lowest f
            
    pQOpen[0];
            
    highestPriority 9999999999;
            
    bestNode = -1;
            for( 
    0pQSizei++ )
            {
                if( 
    pQOpen[i].highestPriority )
                {
                    
    bestNode i;
                    
    highestPriority pQOpen[i].f;
                }
            } 
        
            if( 
    bestNode != -)
            {
                
    pQOpen[bestNode];
                
    //remove node from queue    
                
    for( bestNodepQSize-1i++ )
                    
    pQOpen[i] = pQOpen[i+1];
                
                
    pQSize--;
            }
            else
            {
                return -
    1;
            }
        
            
    //if n is a goal node; construct path, return success
            
    if( n.wpIdx == goalWp )
            { 
                
    n;
                for( 
    01000z++ )
                {
                    
    parent x.parent;
                    if( 
    parent.parent.wpIdx == -)
                    {
                        return 
    x.wpIdx;
                    }

                    
    parent;
                }

                return -
    1;      
            }

            
    //for each successor nc of n
            
    for( 0level.waypoints[n.wpIdx].childCounti++ )
            {
                
    //newg = n.g + cost(n,nc)
                
    newg n.distancelevel.waypoints[n.wpIdx].originlevel.waypoints[level.waypoints[n.wpIdx].children[i]].origin );
          
                
    //if nc is in Open or Closed, and nc.g <= newg then skip
                
    if( PQExistspQOpenlevel.waypoints[n.wpIdx].children[i], pQSize ) )
                {
                    
    //find nc in open
                    
    nc spawnstruct();
                    for( 
    0pQSizep++ )
                    {
                        if( 
    pQOpen[p].wpIdx == level.waypoints[n.wpIdx].children[i] )
                        {
                            
    nc pQOpen[p];
                            break;
                        }
                    }
           
                    if( 
    nc.<= newg )
                    {
                        continue;
                    }
                }
                else if( 
    ListExistsclosedListlevel.waypoints[n.wpIdx].children[i], listSize ) ) 
                {
                    
    //find nc in closed list
                    
    nc spawnstruct();
                    for( 
    0listSizep++ )
                    {
                        if( 
    closedList[p].wpIdx == level.waypoints[n.wpIdx].children[i] )
                        {
                            
    nc closedList[p];
                            break;
                        }
                    }
            
                    if( 
    nc.<= newg )
                    {
                        continue;
                    }
                }
          
                
    nc spawnstruct();
                
    nc.parent spawnstruct();
                
    nc.parent n;
                
    nc.newg;
                
    nc.distancelevel.waypoints[level.waypoints[n.wpIdx].children[i]].originlevel.waypoints[goalWp].origin );
                
    nc.nc.nc.h;
                
    nc.wpIdx level.waypoints[n.wpIdx].children[i];

                
    //if nc is in Closed,
                
    if( ListExistsclosedListnc.wpIdxlistSize ) )
                {
                    
    //remove it from Closed
                    
    deleted false;
                    for( 
    0listSizep++ )
                    {
                        if( 
    closedList[p].wpIdx == nc.wpIdx )
                        {
                            for( 
    plistSize-1x++ )
                            {
                                
    closedList[x] = closedList[x+1];
                            }
                            
    deleted true;
                            break;
                        }
                        
                        if( 
    deleted )
                        {
                            break;
                        }
                    }
                    
                    
    listSize--;
                }
            
                
    //if nc is not yet in Open, 
                
    if( !PQExistspQOpennc.wpIdxpQSize ) )
                {
                    
    //push nc on Open
                    
    pQOpen[pQSize] = spawnstruct();
                    
    pQOpen[pQSize] = nc;
                    
    pQSize++;
                }
            }
          
            
    //Done with children, push n onto Closed
            
    if( !ListExistsclosedListn.wpIdxlistSize ) )
            {
                
    closedList[listSize] = spawnstruct();
                
    closedList[listSize] = n;
                
    listSize++;
            }
        }

    3. To setup your wayfinding, you simply add script_origins to a map. This is the method used by both Meatbot and Pezbot. Each script_origin becomes part of the astar waypoint struct array, and the AI are guided through them using various procedures to determine their paths.

    My advice for you is to use either Meatbot or Pezbot as a starting point in your AI pathfinding experiments. I knew nothing at all about pathfinding before I sunk my teeth into Pezbot, but it was blooding interesting. Pezbot in particular has some really cool utility functions to help you develop your code. They save me heaps of time and energy.
    Last edited by Tally; 23rd February 2014 at 19:39.

  3. The Following 4 Users Say Thank You to Tally For This Useful Post:

    Invictus (26th June 2014),Ni3ls (23rd February 2014),RobsoN (23rd February 2014),smect@ (24th February 2014)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •