func _pathFinding(start, goal):
var openList = []
var closedList = []
openList.append([start, 0, _h(start, goal), _f(start, goal)])
while not openList.empty():
var m = _m(openList)
if m == goal:
print("End of pathfinding at: " + String(goal / 8))
closedList.append(m)
for i in closedList:
if not i == goal:
var mark = O.instance()
get_node("/root/Title/TileMap").add_child(mark)
mark.position = i
mark.add_to_group(mark_group)
return closedList
closedList.append(m)
openList.pop_front()
print(m[1])
for n in _sides(m):
if closedList.has(n):
continue
var cost = int(_g(n) + _distance(m, n))
print("Cost of: " + String(n / 8) + " is: " + String(cost))
for i in openList:
if i[0] == n:
openList.erase(i)
elif cost > m[1]:
continue
if closedList.has(n):
closedList.erase(n)
elif cost < _g(n):
continue
openList.append([Vector2(n.x, n.y), cost, _h(n, goal), _f(n, goal)])
func _m(array):
var minV = 9999
var vec
for i in array:
if i[2] < minV:
minV = i[2]
vec = i[0]
return vec
func _f(node, goal):
return _g(node) + _h(node, goal)
func _h(node, goal):
var dx = abs(node.x - goal.x)
var dy = abs(node.y - goal.y)
return (dx + dy)
func _distance(a, b):
var dx = abs(a.x - b.x)
var dy = abs(a.y - b.y)
return int(dx + dy)
func _g(node):
var c
if get_node("/root/Title/TileMap").map.get_cellv(get_node("/root/Title/TileMap").map.world_to_map(node)) == 12:
c = 7
elif get_node("/root/Title/TileMap").map.get_cellv(get_node("/root/Title/TileMap").map.world_to_map(node)) == 11:
c = 1
else:
c = 1
return c
func _sides(node):
var s = [Vector2(0, -8), Vector2(-8, 0), Vector2(0, 8), Vector2(8, 0)]
var childN = [(node + s[0]), (node + s[1]), (node + s[2]), (node + s[3])]
return childN
I am getting wrong numbers and the character is not avoiding higher cost tiles. ´var cost´ always returns me the value 9 or 15, while those make no sense. Sorry for anything if it appears to be dumb. It is my first try at complex code.
I wasn't able to make the code formatation for the entire code for some reason.