Thread: Pathfinder
View Single Post
  #1  
Old 12-05-2007, 06:22 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Pathfinder

Edges are of equal value.

PHP Code:
/**
  * Finds the path from one object to another through the nodes variable.
  *
  * Node is an object which has nodes variable containing a list of connected node.
  * Nodes are single directions. In order to make it two-directions, you have to define
  *   them separately.
**/

function findPathstartNodeendNode )
{
  
temp.nodeList.add( {startNode, new[0]} );
  
temp.nodeIndex 0;
  while ( 
temp.nodeList.size() > temp.nodeIndex )
  {
    
temp.node temp.nodeListtemp.nodeIndex ][0];
    
temp.nodePath temp.nodeListtemp.nodeIndex ][1];
    
temp.nodePath.addtemp.node );
    if ( 
temp.node == endNode )
      return 
temp.nodePath;

    for ( 
temp.subnodenode.nodes )
    {
      if ( 
temp.subnode in temp.nodeList )
        continue;

      
temp.nodeList.add( { temp.subnodetemp.nodePath } );
    }
    
temp.nodeIndex ++;
  }

  return 
null;

Example of Usage:
PHP Code:
function onCreated()
{
  
node1 = new TStaticVar("Node-1");
  
node2 = new TStaticVar("Node-2");
  
node3 = new TStaticVar("Node-3");
  
node4 = new TStaticVar("Node-4");

  
node1.nodes = { node2node3 };
  
node2.nodes = { node1node3 };
  
node3.nodes = { node4 };
  
node4.nodes = { node1 };
  
  
path findPathnode1node4 );
  for ( 
nodepath )
    echo( 
node.name );

Reply With Quote