@ mrHandy
Not my most efficient coding, I tried expanding it so it would be easy to understand. You should add another dimension to every array so you don't have to type things 3 times.
rem ---------------------------------------------------------------------------
rem Pathfinding with different sized units
rem ---------------------------------------------------------------------------
rem author : TheComet
rem description : Fatter units won't fit through thin gaps, where thinner
rem units fit through. In this example I will attempt to solve
rem this by using different pathfinding data for every unit
rem ---------------------------------------------------------------------------
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
show mouse
disable escapekey
randomize timer()
rem ---------------------------------------------------------------------------
rem UDTs
rem ---------------------------------------------------------------------------
type MapAT
Dat
endtype
rem ---------------------------------------------------------------------------
rem Global Variables
rem ---------------------------------------------------------------------------
rem ---------------------------------------------------------------------------
rem Global Arrays
rem ---------------------------------------------------------------------------
rem get dimensions
restore Dimensions
read Width
read Height
rem define map
global dim Map( Width-1 , Height-1 ) as MapAT
rem paths of units
global dim PathSmall( Width-1 , Height-1 ) as MapAT
global dim PathMedium( Width-1 , Height-1 ) as MapAT
global dim PathLarge( Width-1 , Height-1 ) as MapAT
rem define layers
global dim Layer1( Width-1 , Height-1 ) as MapAT
global dim Layer2( Width-1 , Height-1 ) as MapAT
global dim Layer3( Width-1 , Height-1 ) as MapAT
rem ---------------------------------------------------------------------------
rem Initialise
rem ---------------------------------------------------------------------------
rem initialise pathfinder
Pathfinder = iaa_create pathfinder( Width , Height , 1 , 1 )
rem read map data
restore Layer1
for y = 0 to Height-1
for x = 0 to Width-1
read Map( x , y ).Dat
Layer1( x , y ).Dat = Map( x , y ).Dat
next x
next y
rem layer 2
restore Layer2
for y = 0 to Height-1
for x = 0 to Width-1
read Layer2( x , y ).Dat
next x
next y
rem layer 3
restore Layer3
for y = 0 to Height-1
for x = 0 to Width-1
read Layer3( x , y ).Dat
next x
next y
rem set positions of end
repeat
x = rnd(Width-1)
y = rnd(Height-1)
EndX = x
EndY = y
until MapIsValid( x , y , 1 , 1 )
rem set positions of small unit
repeat
x = rnd(Width-1)
y = rnd(Height-1)
SmallX = x
SmallY = y
until MapIsValid( x , y , 1 , 1 )
rem set positions of medium unit
repeat
x = rnd(Width-1)
y = rnd(Height-1)
MediumX = x
MediumY = y
until MapIsValid( x , y , 2 , 2 )
rem set positions of large unit
repeat
x = rnd(Width-1)
y = rnd(Height-1)
LargeX = x
LargeY = y
until MapIsValid( x , y , 3 , 3 )
rem main loop
Tool = 1
repeat
FindPath( Pathfinder , 1 , Width , Height , SmallX , SmallY , EndX , EndY )
FindPath( Pathfinder , 2 , Width , Height , MediumX , MediumY , EndX , EndY )
FindPath( Pathfinder , 3 , Width , Height , LargeX , LargeY , EndX , EndY )
rem user info
set cursor 0,Height*20 + 40
print "1 - Place end"
print "2 - Place small unit"
print "3 - Place medium unit"
print "4 - Place large unit"
print
print "Selected: ";Tool
rem select tool
if keystate(2) then Tool = 1
if keystate(3) then Tool = 2
if keystate(4) then Tool = 3
if keystate(5) then Tool = 4
rem calculate mouse coordinates and convert to grid coordinates
GridX = range( mousex()/20 , 0 , Width - 1 )
GridY = range( mousey()/20 , 0 , Height - 1 )
rem change end
if Tool = 1 and mouseclick()
if MapIsValid( GridX , GridY , 1 , 1 )
EndX = GridX
EndY = GridY
endif
endif
rem chang small unit
if Tool = 2 and mouseclick()
if MapIsValid( GridX , GridY , 1 , 1 )
SmallX = GridX
SmallY = GridY
endif
endif
rem change medium unit
if Tool = 3 and mouseclick()
if MapIsValid( GridX , GridY , 2 , 2 )
MediumX = GridX
MediumY = GridY
endif
endif
rem change large unit
if Tool = 4 and mouseclick()
if MapIsValid( GridX , GridY , 3 , 3 )
LargeX = GridX
LargeY = GridY
endif
endif
rem draw map
for y = 0 to Height-1
for x = 0 to Width-1
if Map(x,y).Dat = 0 then a2fillbox x*20 + 2 , y*20 + 2 , (x+1)*20 - 2 , (y+1)*20 - 2 , 0xFF00FF00
if Map(x,y).Dat = 1 then a2fillbox x*20 + 2 , y*20 + 2 , (x+1)*20 - 2 , (y+1)*20 - 2 , 0xFF0000FF
next x
next y
rem draw path map for small units
for y = 0 to Height-1
for x = 0 to Width-1
if PathSmall(x,y).Dat = 1 then a2fillbox x*20 + 2 , y*20 + 2 , (x+1)*20 - 2 , (y+1)*20 - 2 , 0xFF700000
next x
next y
rem draw path map for medium units
for y = 0 to Height-1
for x = 0 to Width-1
if PathMedium(x,y).Dat = 1 then a2fillbox x*20 + 2 , y*20 + 2 , (x+1)*20 - 2 , (y+1)*20 - 2 , 0xFF700000
next x
next y
rem draw path map for large units
for y = 0 to Height-1
for x = 0 to Width-1
if PathLarge(x,y).Dat = 1 then a2fillbox x*20 + 2 , y*20 + 2 , (x+1)*20 - 2 , (y+1)*20 - 2 , 0xFF700000
next x
next y
rem draw end
a2fillbox EndX*20 + 2 , EndY*20 + 2 , (EndX+1)*20 - 2 , (EndY+1)*20 - 2 , 0xFF005000
ink 0xFFFFFFFF , 0
text EndX*20 + 2 , EndY*20 + 2 , "P"
rem draw units
a2fillbox SmallX*20 + 2 , SmallY*20 + 2 , (SmallX+1)*20 - 2 , (SmallY+1)*20 - 2 , 0xFFFF0000
a2fillbox MediumX*20 + 2 , MediumY*20 + 2 , (MediumX+2)*20 - 2 , (MediumY+2)*20 - 2 , 0xFFFF0000
a2fillbox LargeX*20 + 2 , LargeY*20 + 2 , (LargeX+3)*20 - 2 , (LargeY+3)*20 - 2 , 0xFFFF0000
rem refresh screen
sync
rem end of main loop
until escapekey()
rem stop pathfinder
iaa_delete pathfinder Pathfinder
rem end
end
rem ---------------------------------------------------------------------------
rem Functions
rem ---------------------------------------------------------------------------
function MapIsValid( px , py , w , h )
for x = px to px+w-1
for y = py to py+h-1
if Map(x,y).Dat = 1 then exitfunction 0
next y
next x
endfunction 1
function FindPath( Pathfinder , layer , Width , Height , sx , sy , ex , ey )
rem clear last pathmap
for x = 0 to Width-1
for y = 0 to Height-1
if layer = 1 then PathSmall( x , y ).Dat = 0
if layer = 2 then PathMedium( x , y ).Dat = 0
if layer = 3 then PathLarge( x , y ).Dat = 0
next y
next x
rem copy layer into pathfinder
for x = 0 to Width-1
for y = 0 to Height-1
if layer = 1 then iaa_set pathmap value Pathfinder , x , y , Layer1(x,y).Dat
if layer = 2 then iaa_set pathmap value Pathfinder , x , y , Layer2(x,y).Dat
if layer = 3 then iaa_set pathmap value Pathfinder , x , y , Layer3(x,y).Dat
next y
next x
rem search for path
success = iaa_search path( Pathfinder , 0 , sx , sy , ex , ey )
rem path found
if success = 1
rem store path for small units
if layer = 1
for n = 0 to iaa_get total waytiles( Pathfinder , 0 )-1
PathSmall( iaa_get waytile x( Pathfinder , 0 , n ) , iaa_get waytile y( Pathfinder , 0 , n ) ).Dat = 1
next n
endif
rem store path for medium units
if layer = 2
for n = 0 to iaa_get total waytiles( Pathfinder , 0 )-1
PathMedium( iaa_get waytile x( Pathfinder , 0 , n ) , iaa_get waytile y( Pathfinder , 0 , n ) ).Dat = 1
next n
endif
rem store path for large units
if layer = 3
for n = 0 to iaa_get total waytiles( Pathfinder , 0 )-1
PathLarge( iaa_get waytile x( Pathfinder , 0 , n ) , iaa_get waytile y( Pathfinder , 0 , n ) ).Dat = 1
next n
endif
endif
endfunction
rem ---------------------------------------------------------------------------
rem Data statements
rem ---------------------------------------------------------------------------
Dimensions:
data 24,24
Layer1:
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1
data 1,0,0,1,1,1,0,1,1,0,0,1,1,1,1,0,0,0,0,1,0,0,0,1
data 1,0,0,1,1,1,0,1,1,0,0,1,1,1,1,0,0,0,1,1,0,0,0,1
data 1,0,0,1,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1,1,1,1,1,1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Next thing to do would be to come up with an algorithm that can procedurally generate the other layers based on the first layer. I already have an idea and will see if it works or not. You'll hear from me again either way.
TheComet