It looks good so I'll get started with this then. But the tiles shouldn't overlap at the edge like they do. So really each gridline should be 2 lines where the different tiles sit next to each other unless there is going to be black lines dividing up the map in the actual game.
Edit: Ok I've got my pathfinding function working in the isoworld obese made without making any changes to his code except to add the include# line and to dimension some arrays. I only had to change the way my node path to real function converted the tile numbers into screen coordinates.
Edit: Sorry, I had to leave before I could finish posting the scripts.
Here they are:
"Astar.DBA"
Function Astar_Init_Pathfinding(NodesX,NodesZ,MaxPathLength,NmbPaths)
DirectionMode(0) = 0
MaxPathLength(0) = MaxPathLength
NmbPaths(0) = NmbPaths
NodesX(0) = NodesX
NodesZ(0) = NodesZ
endfunction
Function Astar_Node_Path_to_Real(path)
for t = 1 to PathX(path,0)
OldPathX = PathX(path,t)
PathX(path,t) = Get_Iso_Tile_X(PathX(path,t),PathZ(path,t))
PathZ(path,t) = Get_Iso_Tile_Y(OldPathX,PathZ(path,t))
next t
endfunction
function Get_Iso_Tile_X(tilex,tiley)
x# = -100 + (50 * (tilex - 1))
z# = -100 + (50 * (tiley - 1))
result# = @iso_screenx(x#,z#)
endfunction result#
function Get_Iso_Tile_Y(tilex,tiley)
x# = -100 + (50 * (tilex - 1))
z# = -100 + (50 * (tiley - 1))
result# = @iso_screeny(x#,0.0,z#)
endfunction result#
Function Astar_Get_Path_Length(path)
result = PathX(path,0)
endfunction result
Function Astar_Set_Grid_Square(flag,x,z)
Astar_Grid(x,z) = flag
endfunction
Function Astar_Get_Grid_Square(x,z)
result = Astar_Grid(x,z)
endfunction result
Function Astar_Estimate_Dist(x1,z1,x2,z2)
if DirectionMode(0) = 0
Dist = (x2-x1) + (y2-y1)
else
Dist = sqrt((x2-x1)^2+(z2-z1)^2) + 1
endif
endfunction Dist
Function Astar_Set_Search_Mode(mode)
DirectionMode(0) = mode
endfunction
Function Astar_Find_Path(path,x1,z1,x2,z2)
Astar_Fval(0) = 100000
Astar_Fval(1) = 99999
DestX = x1
DestZ = z1
CurrX = x2
CurrZ = z2
Counter1 = 2
Counter2 = 1
Finish=0
Astar_OpenList(1,0) = CurrX
Astar_OpenList(1,1) = CurrZ
G1 = 10
G2 = 14
repeat
` Find lowest f-cost square
for x = 0 to Counter1 - 1
if Astar_Fval(x) < Astar_Fval(BestTile)
BestTile = x
endif
next z
` Add it to the closed list
Astar_ClosedList(Counter2,0) = Astar_OpenList(BestTile,0)
Astar_ClosedList(Counter2,1) = Astar_OpenList(BestTile,1)
Astar_ClosedList(Counter2,2) = Astar_OpenList(BestTile,2)
Astar_ClosedList(Counter2,3) = Astar_OpenList(BestTile,3)
Astar_Fval(BestTile)=99999
` Set current tile
CurrX = Astar_ClosedList(Counter2,0)
CurrZ = Astar_ClosedList(Counter2,1)
` Increment nmb of waypoints
inc Counter2
` Check for pathlength reached
if Counter2 >= MaxPathLength(0) then exitfunction
` Set Current Tile as Parent
ParentX = CurrX
ParentZ = CurrZ
` Add Adjacent Squares to Open List
for x = -1 to 1
for y = -1 to 1
` Reset values
Run = 1
Skip = 0
CheckTile = 0
Adjacent = 0
` Get the tile to check
TileX = CurrX + x
TileY = CurrZ + y
` Check if tile is adjacent
if x = 0 or y = 0 then Adjacent = 1
` Exclude diagonals in no-diagonals mode
if DirectionMode(0) <> 0 and Adjacent = 0 then CheckTile = 1
` Check if the tile is the current tile
if TileX = CurrX and TileY = CurrZ then CheckTile = 1
` Make sure the node exists
if TileX > 0 and TileX <= NodesX(0) and TileY > 0 and TileY <= NodesZ(0) and CheckTile = 0
` Check if the destination tile has been found
if TileX = DestX and TileY = DestZ then Finish = 1
` If the tile is walkable
if Astar_Grid(TileX,TileY) = 0
` Check if the tile is already on the path...
for z = 0 to Counter2 - 1
if Astar_ClosedList(z,0) = TileX and Astar_ClosedList(z,1) = TileY then Run = 0
next z
` if not add it to the open list
if Run = 1
` Check if the tile is already on the OpenList
for z = 1 to Counter1
if Astar_OpenList(z,0) = TileX and Astar_OpenList(z,1) = TileY
Skip = 1
Zval = z
endif
next z
` If not add it, else check if this path to that square is shorter
` and if so then change the values for that square to the new values
if Skip = 0
Astar_OpenList(Counter1,0) = TileX : Astar_OpenList(Counter1,1) = TileY
Astar_OpenList(Counter1,2) = ParentX : Astar_OpenList(Counter1,3) = ParentZ
ValX = abs(TileX-DestX)
ValZ = abs(TileY-DestZ)
Astar_Hval(Counter1) = (ValX+ValZ) * 10
if Adjacent = 1 then Astar_Gval(Counter1) = Astar_Gval(BestTile) + G2 else Astar_Gval(Counter1) = Astar_Gval(BestTile)+G1
Astar_Fval(Counter1) = Astar_Hval(Counter1) + Astar_Gval(Counter1)
inc Counter1
else
if Adjacent = 1 then Gval = Astar_Gval(BestTile) + G2 else Gval = Astar_Gval(BestTile) + G1
if Gval < Astar_Gval(Zval)
Astar_OpenList(Zval,2) = ParentX : Astar_OpenList(Zval,3) = ParentZ
Astar_Gval(Zval) = Gval
Astar_Fval(Zval) = Gval + Astar_Hval(Zval)
endif
endif
endif
endif
endif
next y
next x
rem Reset BestTile
BestTile=0
until Finish=1
` Save the path to the specified path array
Max_Waypoints = 1
PathX(1,1) = DestX
PathZ(1,1) = DestZ
PathX(1,2) = Astar_ClosedList(Counter2-1,0)
PathZ(1,2) = Astar_ClosedList(Counter2-1,1)
NextX = Astar_ClosedList(Counter2-1,2)
NextZ = Astar_ClosedList(Counter2-1,3)
if NextX = 0 and NextZ = 0 then Max_Waypoints = 2
if Max_Waypoints = 1
inc Max_Waypoints
repeat
inc Max_Waypoints
PathX(1,Max_Waypoints) = NextX
PathZ(1,Max_Waypoints) = NextZ
for x = 1 to Counter2 - 1
if Astar_ClosedList(x,0) = NextX and Astar_ClosedList(x,1) = NextZ
NextX = Astar_ClosedList(x,2)
NextZ = Astar_ClosedList(x,3)
endif
next x
until NextX = 0 and NextZ = 0
endif
PathX(path,0) = Max_Waypoints
endfunction
The isoworld:
#include "AStar.dba"
hide mouse
sync on : sync rate 0
cyan = rgb(0,255,255)
blue = rgb(0,0,128)
red = rgb(255,0,0)
rem *************** INITIALIZE PATHFINDING *******************
` Dimension pre-initiation arrays
Dim NodesX(0)
Dim NodesZ(0)
Dim DirectionMode(0)
Dim MaxPathLength(0)
Dim NmbPaths(0)
` Initiate pathfinding
Astar_Init_Pathfinding(5,5,25,1)
` Dimension post-initiation arrays
NmbPaths = NmbPaths(0)
NodesX = NodesX(0) : NodesZ = NodesZ(0)
TotalNodes = NodesX * NodesZ
MaxPathLength = MaxPathLength(0)
Dim Astar_Grid(NodesX,NodesZ)
Dim Astar_OpenList(TotalNodes,3)
Dim Astar_ClosedList(MaxPathLength,3)
Dim Astar_Fval(TotalNodes)
Dim Astar_Gval(TotalNodes)
Dim Astar_Hval(TotalNodes)
Dim PathX(NmbPaths,MaxPathLength)
Dim PathZ(NmbPaths,MaxPathLength)
rem **********************************************************
rem **********************************************************
rem TO FIND A PATH BETWEEN TILES JUST USE THIS:
rem **********************************************************
rem
rem Astar_Find_Path(pathnmb,x1,y1,x2,y2)
rem Astar_Node_Path_to_Real(pathnmb)
rem
rem **********************************************************
DO
`controls
if upkey()=1 then inc pz#,5
if downkey()=1 then dec pz#,5
if leftkey()=1 then dec px#,5
if rightkey()=1 then inc px#,5
if shiftkey()=1 then inc py#,5
if controlkey()=1 then dec py#,5
if spacekey()=1 then py# = 0.0
`convert character position to isometric
pscreenx# = @iso_screenx(px#,pz#)
pscreeny# = @iso_screeny(px#,py#,pz#)
`draw floor tiles
for z = -100 to 100 step 50
for x = -100 to 100 step 50
x#=x : z#=z
@iso_tile(x#,z#,cyan,blue)
next x
next z
`draw character
ink red,0
circle pscreenx#,pscreeny#,9
@iso_line(px#,py#,pz#, px#,.0,pz#) : `player height line
print "Circle Position In World"
print "X: "; px#
print "Y: "; py#
print "Z: "; pz#
sync
cls
LOOP
END
`-------------------
` FUNCTIONS
`-------------------
`Iso ScreenX
FUNCTION @iso_screenx(cx#,cz#)
scrw = screen width()
ix# = (scrw/2) + (cx#*(2/3.0)) - (cz#*(2/3.0))
ENDFUNCTION ix#
`Iso ScreenY
FUNCTION @iso_screeny(cx#,cy#,cz#)
scrh = screen height()
iy# = (scrh/2) - (cy#*(2/3.0)) - (cx#*(1/3.0)) + (cz#*(1/3.0))
ENDFUNCTION iy#
`Iso Tile
FUNCTION @iso_tile(x#,z#,hilite,lolite)
ink lolite,0
@iso_line(x#-25,.0,z#-25, x#+25,.0,z#-25) : `bottom
@iso_line(x#+25,.0,z#-25, x#+25,.0,z#+25) : `right
ink hilite,0
@iso_line(x#+25,.0,z#+25, x#-25,.0,z#+25) : `top
@iso_line(x#-25,.0,z#+25, x#-25,.0,z#-25) : `left
ENDFUNCTION
`Iso Line
FUNCTION @iso_line(sx#,sy#,sz#, ex#,ey#,ez#)
scrw = screen width()
scrh = screen height()
x1# = (sx#*(2/3.0)) - (sz#*(2/3.0))
y1# = scrh-(sy#*(2/3.0)) - (sx#*(1/3.0)) + (sz#*(1/3.0))
x2# = (ex#*(2/3.0)) - (ez#*(2/3.0))
y2# = scrh-(ey#*(2/3.0)) - (ex#*(1/3.0)) + (ez#*(1/3.0))
x1 = @RUP(x1#) : y1 = @RUP(y1#)
x2 = @RUP(x2#) : y2 = @RUP(y2#)
`draw the line
@dline(x1+scrw/2,y1-scrh/2,x2+scrw/2,y2-scrh/2)
ENDFUNCTION
`Round up
FUNCTION @RUP(n#)
if n#<>0 then f# = n# / abs(n#*2)
n# = n# + f#
n = n#
ENDFUNCTION n
`DLine Function by OBese87
FUNCTION @dline(l,t,r,b)
w = r-l : h = b-t
if w >= 0 then xstep = 1 else xstep = -1
if h >= 0 then ystep = 1 else ystep = -1
w# = ABS(w) : h# = ABS(h)
if w#=0 then w#=0.1
if h#=0 then h#=0.1
xfact# = w#/h#
yfact# = h#/w#
x = 0 : y = 0
repeat
`don't overshoot
if abs(x+xstep) > abs(w) then xstep = 0
if abs(y+ystep) > abs(h) then ystep = 0
dot x+l,y+t
if yfact# > xfact#
inc y,ystep
if ABS(x) < ABS(y*xfact#) then inc x,xstep
else
inc x,xstep
if ABS(y) < ABS(x*yfact#) then inc y,ystep
endif
until xstep = 0 and ystep = 0
ENDFUNCTION