- Joined
- Jul 10, 2007
- Messages
- 6,306
Run the map in wc3 (do not open) and then build a tower in upper right corner. You'll see footmen created like 2x a second, which is the pathing algorithm searching for a path. This is the best way to see how the algorithm currently works.
I want to know if there is any way I can improve this. If you watch it, you'll see it branch out until it finds the path.
I'm essentially using a more optimal version of this algorithm
http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm
If you notice, only 1 footman is created at each 32x32 cell.
32x32 is the minimum size this can be for path finding.
My problem is that it is lagging =(. I want to know if there is a way to improve the speed of this.
Thank you all =)
edit
Here's a look at the pathing algorithm I'm attempting =)
A* ^)^
The first table in action
The above tables use a movement cost of 10 and a diag movement cost of 14.
In the actual algorithm, a movement cost of 5 and diagonal cost of 7 will be used.
The maximum movement cost across the biggest possible map is 28672
edit
I want to know if there is any way I can improve this. If you watch it, you'll see it branch out until it finds the path.
I'm essentially using a more optimal version of this algorithm
http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm
If you notice, only 1 footman is created at each 32x32 cell.
32x32 is the minimum size this can be for path finding.
My problem is that it is lagging =(. I want to know if there is a way to improve the speed of this.
Thank you all =)
JASS:
static method isBlocked takes integer x, integer y, integer x2, integer y2, integer pt returns boolean
local integer c=0 //instance count of local lists
local integer array n //list next
local integer array p //list previous
local integer array lx //point x
local integer array ly //point y
local integer array dx //direction x
local integer array dy //direction y
local thistype k=0 //current list
local thistype m=0 //new list
local hashtable ht //local hashtable for seeing if a coordinate was hit
local integer o //open paths
local boolean f=false //initially found path already?
//have to work in a 32x32 grid, so format the coordinates
set x=(x+32)/32*32
set y=(y+32)/32*32
set x2=(x2+32)/32*32
set y2=(y2+32)/32*32
//if the start and end points are pathable, continue
if (IsPathable(x,y,pt) and IsPathable(x2,y2,pt)) then
set ht=InitHashtable()
call SaveBoolean(ht,x,y,true) //mark start point as hit
//create first 4 path nodes branching out in 4 directions like a cross
//from start point
//! runtextmacro INI_PATH_NODE("32","0")
//! runtextmacro INI_PATH_NODE("-32","0")
//! runtextmacro INI_PATH_NODE("0","32")
//! runtextmacro INI_PATH_NODE("0","-32")
//if none of the path nodes were the end point, then continue
if (not f) then
//if none of the path nodes were valid, end
set k=n[0]
if (0==k) then
call FlushParentHashtable(ht)
set ht=null
return true
endif
//loop through each open path node and branch them out
loop
//reset the open paths. If this hits 0, path ended up being
//a dead end
set o=4
//branch out
//! runtextmacro ALLOCATE_PATH_NODE_2("0","32")
//! runtextmacro ALLOCATE_PATH_NODE_2("0","-32")
//! runtextmacro ALLOCATE_PATH_NODE_2("32","0")
//! runtextmacro ALLOCATE_PATH_NODE_2("-32","0")
//dead end
if (0==o) then
set n[p[k]]=n[k]
set p[n[k]]=p[k]
endif
set k=n[k]
//if k is at 0, then it's at the head
if (0==k) then
set k=n[k]
//if thee are no more lists, return blocked
if (0==k) then
call FlushParentHashtable(ht)
set ht=null
return true
endif
//flip pathing
//goes between going straight and branching
set f=not f
endif
endloop
endif
call FlushParentHashtable(ht)
set ht=null
//not blocked if at this point
return false
endif
//blocked if at this point
return true
endmethod
//this is for initializing the first branching nodes
//! textmacro INI_PATH_NODE takes DX,DY
//if no branch nodes have hit the end
//and the branch point is pathable
if not f and IsPathable(x+$DX$,y+$DY$,pt) then
//mark as hit
call SaveBoolean(ht,x+$DX$,y+$DY$,true)
//allocate
set k=c+1
set c=k
//store point
set lx[k]=x+$DX$
set ly[k]=y+$DY$
//add to branch list
set p[n[0]]=k
set n[k]=n[0]
set n[0]=k
//store direction*
set dx[k]=$DX$
set dy[k]=$DY$
//if the branch point was the goal, mark as goal
if lx[k]==x2 and ly[k]==y2 then
set m=k //store k to m
set f=true //mark (ini nodes only)
endif
endif
//! endtextmacro
//! textmacro ALLOCATE_PATH_NODE_2 takes DX,DY
//if the branched point was hit or the point wasn't pathable, mark as dead end
if (HaveSavedBoolean(ht,lx[k]+$DX$,ly[k]+$DY$) or not IsPathable(lx[k]+$DX$,ly[k]+$DY$,pt)) then
set o=o-1 //dead end counter
else
//if a branch (not in same direction as current node)
if ($DX$!=dx[k] or $DY$!=dy[k]) then
//only branch if the branching flag is marked true
//this is to give priority to straight paths so that
//the shortest path doesn't end up being a spiraling mess
if (f) then
//mark branch point as hit
call SaveBoolean(ht,lx[k]+$DX$,ly[k]+$DY$,true)
//allocate
set m=c+1
set c=m
//add to branch list
set p[n[0]]=m
set n[m]=n[0]
set n[0]=m
//store direction
set dx[m]=$DX$
set dy[m]=$DY$
//store coordinate
set lx[m]=lx[k]+$DX$
set ly[m]=ly[k]+$DY$
//if this node hit the goal, exit loop
exitwhen lx[m]==x2 and ly[m]==y2
endif
//else if the path was straight, enter if flagged for straight paths
elseif (not f) then
//mark point as hit
call SaveBoolean(ht,lx[k]+$DX$,ly[k]+$DY$,true)
//move the node forward
set lx[k]=lx[k]+$DX$
set ly[k]=ly[k]+$DY$
//if the node hit the goal, exit loop
exitwhen lx[k]==x2 and ly[k]==y2
endif
endif
//! endtextmacro
endlibrary
edit
Here's a look at the pathing algorithm I'm attempting =)
A* ^)^
Code:
82 S 74 80 94 108
76 68 68 B 88 102
76 68 68 B 88 102
76 68 68 B 88 B
82 68 B B 88 B
88 B B B 88 108
B B 00 G 100 114
-----------------------------------------
82 S * * 94 108
76 68 68 B * 102
76 68 68 B * 102
76 68 68 B * B
82 68 B B * B
88 B B B * 108
B B 00 G 00 104
Code:
82 S 74 80 00 00
76 68 68 74 00 00
76 68 68 74 00 00
76 68 68 74 00 00
82 68 68 74 00 00
88 74 68 74 00 00
00 80 74 G 00 00
-----------------------------------------
82 S 74 80 00 00
76 68 * 74 00 00
76 68 * 74 00 00
76 68 * 74 00 00
82 68 * 74 00 00
88 74 * 74 00 00
00 80 74 G 00 00
The first table in action
Code:
The start node's total movement cost to goal is estimated and it is added to binary heap
The min cost is set to 68
* 68 * * * *
* * * B * *
* * * B * *
* * * B * B
* * B B * B
* B B B * *
B B * G * *
-----------------------------------------
The first node branches out. 5 new nodes are added
and the first node is removed from the binary heap.
82 X 74 * * *
76 68 68 B * *
* * * B * *
* * * B * B
* * B B * B
* B B B * *
B B * G * *
-----------------------------------------
All nodes that match the min cost branch out and
are removed
82 X 74 80 * *
76 X X B * *
76 68 68 B * *
* * * B * B
* * B B * B
* B B B * *
B B * G * *
-----------------------------------------
82 X 74 80 * *
76 X X B * *
76 X X B * *
76 68 68 B * B
* * B B * B
* B B B * *
B B * G * *
-----------------------------------------
82 X 74 80 * *
76 X X B * *
76 X X B * *
76 X X B * B
82 68 B B * B
88 B B B * *
B B * G * *
-----------------------------------------
82 X 74 80 * *
76 X X B * *
76 X X B * *
76 X X B * B
82 X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
In this case, a new min cost is found, which is 74
All nodes that match the min cost branch out
and are removed.
82 X X 80 * *
76 X X B * *
76 X X B * *
76 X X B * B
82 X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
82 X X 80 * *
X X X B * *
X X X B * *
76 X X B * B
82 X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
82 X X 80 * *
X X X B * *
X X X B * *
X X X B * B
82 X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
82 X X X 94 *
X X X B 88 *
X X X B * *
X X X B * B
82 X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
X X X X 94 *
X X X B 88 *
X X X B * *
X X X B * B
X X B B * B
88 B B B * *
B B * G * *
-----------------------------------------
X X X X 94 108
X X X B X 102
X X X B 88 102
X X X B * B
X X B B * B
X B B B * *
B B * G * *
-----------------------------------------
X X X X 94 108
X X X B X 102
X X X B X 102
X X X B 88 B
X X B B * B
X B B B * *
B B * G * *
-----------------------------------------
X X X X 94 108
X X X B X 102
X X X B X 102
X X X B X B
X X B B 88 B
X B B B * *
B B * G * *
-----------------------------------------
X X X X 94 108
X X X B X 102
X X X B X 102
X X X B X B
X X B B X B
X B B B 88 108
B B * G * *
-----------------------------------------
X X X X 94 108
X X X B X 102
X X X B X 102
X X X B X B
X X B B X B
X B B B X 108
B B * G 100 114
-----------------------------------------
X S O O 94 108
X X X B O 102
X X X B O 102
X X X B O B
X X B B O B
X B B B O 108
B B * G 100 114
-----------------------------------------
The above tables use a movement cost of 10 and a diag movement cost of 14.
In the actual algorithm, a movement cost of 5 and diagonal cost of 7 will be used.
The maximum movement cost across the biggest possible map is 28672
edit
Code:
Nothing
* S * * * *
* * * B * *
* * * B * *
* * * B * B
* * B B * B
* B B B * *
B B * G * *
First calculate initial point
-----------------------------------------
* 68 * * * *
* * 68 B * *
* * * B68 * *
* * * B68 * B
* * B B68 * B
* B B B68 * *
B B * G * *
Loop through lowest queue and branch off of each node
Remove all hit nodes
-----------------------------------------
82 68 74 * * *
76 68 68 B74 * *
* 68 68 B68 * *
* * 68 B68 * B
* * B B68 * B
* B B B68 * *
B B * G * *
Loop through lowest queue and branch off of each node
Remove all hit nodes
-----------------------------------------
82 X 74 84 * *
76 X X B74 * *
76 X X BX * *
76 68 X BX * B
* 68 B BX * B
* B B BX * *
B B * G * *
Loop through lowest queue and branch off of each node
Remove all hit nodes
-----------------------------------------
82 X 74 84 * *
76 X X B74 * *
76 X X BX * *
76 X X BX * B
82 X B BX * B
88 B B BX * *
B B * G * *
The queue is now empty, so remove queue from heap
Go to next lowest number, which is 74
Note that the second 74 is on an unpathable area, so
it will just be removed
The first one hits all dead ends (all places already hit)
-----------------------------------------
82 X X 84 * *
76 X X BX * *
76 X X BX * *
76 X X BX * B
82 X B BX * B
88 B B BX * *
B B * G * *
Remove 74 from the heap and go to 76
-----------------------------------------
82 X X 84 * *
X X X BX * *
X X X BX * *
X X X BX * B
82 X B BX * B
88 B B BX * *
B B * G * *
Remove 76 from the heap and go to 82
-----------------------------------------
X X X 84 * *
X X X BX * *
X X X BX * *
X X X BX * B
X X B BX * B
88 B B BX * *
B B * G * *
Remove 82 from the heap and go to 84
-----------------------------------------
X X X X 98 *
X X X BX 92 *
X X X BX * *
X X X BX * B
X X B BX * B
88 B B BX * *
B B * G * *
-----------------------------------------
Remove 84 from the heap and go to 88
X X X X 98 *
X X X BX 92 *
X X X BX * *
X X X BX * B
X X B BX * B
X B B BX * *
B B * G * *
-----------------------------------------
Remove 88 from the heap and go to 92
X X X X 98 112
X X X BX X 106
X X X BX 92 106
X X X BX * B
X X B BX * B
X B B BX * *
B B * G * *
-----------------------------------------
X X X X 98 112
X X X BX X 106
X X X BX X 106
X X X BX 92 B
X X B BX * B
X B B BX * *
B B * G * *
-----------------------------------------
X X X X 98 112
X X X BX X 106
X X X BX X 106
X X X BX X B
X X B BX 92 B
X B B BX * *
B B * G * *
-----------------------------------------
X X X X 98 112
X X X BX X 106
X X X BX X 106
X X X BX X B
X X B BX X B
X B B BX 92 112
B B * G * *
-----------------------------------------
X X X X 98 112
X X X BX X 106
X X X BX X 106
X X X BX X B
X X B BX X B
X B B BX X 112
B B * G 104 118
-----------------------------------------
X * X * 98 112
X X * BX * 106
X X X BX * 106
X X X BX * B
X X B BX * B
X B B BX * 112
B B * G 104 118
-----------------------------------------
Attachments
Last edited: