Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #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
  #2  
Old 12-05-2007, 06:29 AM
Kyranki Kyranki is offline
Freelance Coder
Join Date: Aug 2007
Location: At the end of the rainbow, in the pot of gold.
Posts: 202
Kyranki is on a distinguished road
Send a message via AIM to Kyranki Send a message via MSN to Kyranki
I see you found a way to transfer it into Gscript
__________________
Stan.
Reply With Quote
  #3  
Old 12-06-2007, 01:21 AM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
PHP Code:
/**
 * Nodes Interface for PathFinder
 *
 * (+) void connect( Node node );
 * (+) void disconnect( Node nodes );
 * (+) Node[] getNodes();
 **/
 
/**
  * This connects a node to the node-list.
  *
  * @param node Node, any object that could be treated as a Node.
  *                If used to connect afterwards, add Node interface!
  **/
public function connectnode )
{
  if ( 
node == null )
    return;
  if ( 
node in this.nodes )
    return;
  
  
this.nodes.addnode );
}

/**
  * This removes a node to the node-list.
  *
  * @param node Node, any object that could be treated as a Node.
  **/

public function disconnectnode )
{
  
this.nodes.removenode );
}

/**
  * This obtains the nodes that are currently connected to the object.
  *
  * @return Node[] List of nodes attached to object
  **/

public function getNodes()
{
  return 
this.nodes;
}

/**
  * This calculates the shortest distance to a particular node.
  *
  * @return Node[] An ordered list of the nodes that need to be traveled
  *                    to reach given destination.
  **/
function findPathendNode )
{
  
temp.nodeList.add( {this, 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.getNodes() )
    {
      if ( 
temp.subnode in temp.nodeList )
          continue;

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

  return 
null;

For Stan... And anyone else... It elaborates onto the original code and encapsulates it. You could use node.findPath( endNode ); instead... As a shortcut. Just don't mix-and-match the findPath and Node... ( they'd conflict on the function declaration. )
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 04:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.