MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / SolveForNearStates

Method SolveForNearStates

Source/Utils/micropather.cpp:787–889  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

785
786
787int MicroPather::SolveForNearStates( cPosition* startState, MP_VECTOR< StateCost >* near, float maxCost )
788{
789 /* http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
790
791 1 function Dijkstra(Graph, source):
792 2 for each vertex v in Graph: // Initializations
793 3 dist[v] := infinity // Unknown distance function from source to v
794 4 previous[v] := undefined // Previous node in optimal path from source
795 5 dist[source] := 0 // Distance from source to source
796 6 Q := the set of all nodes in Graph
797 // All nodes in the graph are unoptimized - thus are in Q
798 7 while Q is not empty: // The main loop
799 8 u := vertex in Q with smallest dist[]
800 9 if dist[u] = infinity:
801 10 break // all remaining vertices are inaccessible from source
802 11 remove u from Q
803 12 for each neighbor v of u: // where v has not yet been removed from Q.
804 13 alt := dist[u] + dist_between(u, v)
805 14 if alt < dist[v]: // Relax (u,v,a)
806 15 dist[v] := alt
807 16 previous[v] := u
808 17 return dist[]
809 */
810
811 ++frame;
812
813 OpenQueue open( graph ); // nodes to look at
814 ClosedSet closed( graph );
815
816 nodeCostVec.resize(0);
817 stateCostVec.resize(0);
818
819 PathNode closedSentinel;
820 closedSentinel.Clear();
821 closedSentinel.Init( frame, cPosition(0,0), FLT_MAX, FLT_MAX, 0 );
822 closedSentinel.next = closedSentinel.prev = &closedSentinel;
823
824 PathNode* newPathNode = pathNodePool.GetPathNode( frame, *startState, 0, 0, 0 );
825 open.Push( newPathNode );
826
827 while ( !open.Empty() )
828 {
829 PathNode* node = open.Pop(); // smallest dist
830 closed.Add( node ); // add to the things we've looked at
831 closedSentinel.AddBefore( node );
832
833 if ( node->totalCost > maxCost )
834 continue; // Too far away to ever get here.
835
836 GetNodeNeighbors( node, &nodeCostVec );
837
838 for( int i=0; i<node->numAdjacent; ++i )
839 {
840 MPASSERT( node->costFromStart < FLT_MAX );
841 float newCost = node->costFromStart + nodeCostVec[i].cost;
842
843 PathNode* inOpen = nodeCostVec[i].node->inOpen ? nodeCostVec[i].node : 0;
844 PathNode* inClosed = nodeCostVec[i].node->inClosed ? nodeCostVec[i].node : 0;

Callers

nothing calls this directly

Calls 14

resizeMethod · 0.80
InitMethod · 0.80
GetPathNodeMethod · 0.80
PushMethod · 0.80
EmptyMethod · 0.80
PopMethod · 0.80
AddMethod · 0.80
AddBeforeMethod · 0.80
UpdateMethod · 0.80
cPositionClass · 0.70
ClearMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected