A* help!

Blitz3D Forums/Blitz3D Beginners Area/A* help!

chwaga(Posted 2008) [#1]
I'm near done with my first A* test. But this dang bug won't leave me alone! After much testing, I have found that the potentialnodes (surrounding 8 nodes to be checked) are not being created. Any ideas?
(sorry about those long spots in the code screwing up the window)

Graphics3D 1280, 1024, 32, 1

Const gridscale = 1
Global goalx, goaly, startx, starty
Global currentx, currenty

Global lf = 10000000000

startx = 2
starty = 2
currentx = startx
currenty = starty
goalx = 5
goaly = 6


Type goodnode
	Field x
	Field y
	Field open = False 
End Type

Type badnode
	Field x
	Field y
End Type

Type potentialnode
	Field x
	Field y
	Field g
	Field h
	Field lf
End Type

setupgrid()


While Not KeyHit(1)
	Cls()
	RenderWorld()
	drawgrid()
	
	
	If KeyDown(29) Then
		Stop 
	EndIf
	
	If KeyHit(57) Then updatepath()
	Flip()

Wend


End

Function setupgrid()
	For w = 0 To GraphicsWidth()-(gridscale*100) Step gridscale*100
		For h = 0 To GraphicsHeight()-(gridscale*100) Step gridscale*100
			node.goodnode = New goodnode
			node\x = w
			node\y = h 
		Next
	Next
End Function

Function drawgrid()
	Color 0, 0, 200
	For node.goodnode = Each goodnode
		Rect node\x, node\y, gridscale*100, gridscale*100, 0
	Next
	Color 255, 255, 255
End Function 

;g = 10 if vertical or horizontal
;g = 14 if diagonal

;h = 10*(abs(currentx - goalx) + abs(currenty - goaly))


Function updatepath()
	Repeat
		If KeyHit(29) Then Stop 
		Text 0, 0, currentx + ", " + currenty + ",         " + Rnd(0, 100)
		;find the surrounding 8 nodes and replace them with potentialnodes (closing the normal ones)
		For node.goodnode = Each goodnode
			If ((node\x = currentx - 1) And (node\y = currenty)) Or ((node\x = currentx) + 1 And (node\y = currenty)) Or ((node\y = currenty - 1) And (node\x = currentx)) Or ((node\y = currenty + 1) And (node\x = currentx)) Then 
				pnode.potentialnode = New potentialnode
				pnode\x = node\x
				pnode\y = node\y
				pnode\g = 10
				node\open = False  
			EndIf
			If ((node\x = currentx + 1) And (node\y = currenty + 1)) Or ((node\x = currentx + 1) And (node\y = currenty - 1)) Or ((node\x = currentx - 1) And (node\y = currenty + 1)) Or ((node\x = currentx - 1) And (node\y = currenty - 1)) Then 
				pnode.potentialnode = New potentialnode
				pnode\x = node\x
				pnode\y = node\y
				pnode\g = 14
				node\open = False
			EndIf	
		Next
		;determine h, f (g was found above), and lf(lowest f)
		For pnode.potentialnode = Each potentialnode
			pnode\h = 10 * (Abs(currentx - goalx) + Abs(currenty - goaly))
			If pnode\h + pnode\g < lf Then 
				lf = pnode\h + pnode\g
				pnode\lf = True
			EndIf
			n = n + 1
		Next
		Text 0, 20, n
		n=0
		;delete all potentialnodes, open up the closed nodes, and set currentx and y to the location of the lowest f
		For node.goodnode = Each goodnode
			If ((node\x = currentx - 1) And (node\y = currenty)) Or ((node\x = currentx) + 1 And (node\y = currenty)) Or ((node\y = currenty - 1) And (node\x = currentx)) Or ((node\y = currenty + 1) And (node\x = currentx)) Then node\open = True 
		Next 
		For pnode.potentialnode = Each potentialnode
			If pnode\lf = True Then
				currentx = pnode\x
				currenty = pnode\y
				Stop 
			EndIf
			
			Delete pnode
			
		Next
		Flip()
		
	Until currentx = goalx And currenty = goaly Or KeyHit(1)
End Function 


any help is greatly appreciated, I'll be debugging while i wait for a response to tell me that it was a typo. Thanks for helping me get to sleep! (I'll have trouble sleeping until this works)


chwaga(Posted 2008) [#2]
omg


chwaga(Posted 2008) [#3]
i am


chwaga(Posted 2008) [#4]
so stupid


chwaga(Posted 2008) [#5]
lol...fixing it...


chwaga(Posted 2008) [#6]
well, i fixed the TYPO, but now I appear to be having logic flaws (?)
kinda hard to explain, cause I have NO idea what's going on.



**EDIT**updated the code


Ross C(Posted 2008) [#7]
I'm sorry i can't help you, i did my a* pathfinding using a slightly watered down version, cause i was still getting my head round it. Xyle did a pathfinding thing, that seems to work fairly well.

I feel your pain. Pathfinding is quite hard to get to grips with at first. Good luck!


chwaga(Posted 2008) [#8]
lol, i DID get to sleep (after a while)....

pathfinding = pain, i'll go check out xyle's stuff


Moraldi(Posted 2008) [#9]
check this out (bin and source)

http://www.moraldigames.com/Temp/Map.zip

I don't have the time to explain but I think its worth the time to examine the code...
Hope this helps!

Link will be valid for a few days


YellBellzDotCom(Posted 2008) [#10]
I agree, pathfinding is def the most difficult thing I've touched so far. The main thing that helped me get to the next part of figuring out the code was using steps when moving my character.

Once I clicked my target, my debuglog would show all the info I needed to make sure it was accurate, then I would hit space bar to go to the next step, check the debuglog again, and so on until the player was moved to the target.

This allowed me to varify all the g, h and f values and make sure the program was going in the right direction.

Hope thatll help you somewhat.

Moraldi, thats a pretty nice start there, great job. I havent looked at the code yet, but the game looked pretty good.


Moraldi(Posted 2008) [#11]
Actually it is a 4 years old abandoned project.
Of course all these graphic tiles are draft.
I searched my drive after your interesting posts in A*...