Alright, best pathing system aside, I had a look at the links you gave me, did some reading, and yes, my method wasn't the best. A better way is to only block them from moving on to or through each other when they try, though my implementation is causing some rather fatal recursion problems.
Essentially, I want all of the entities to path to the PC, overlapping pathing is fine. But when one tries to move in to a space occupied by another, stop it, set the other entities as unpathable, then generate another path. If they can find one, follow that one instead, otherwise stand there and do nothing.
That's not what happens. As soon as one tries to step on another, they go AAAAAAAAAAAAAAAAAAAAAAAAAAAAHH and the stack overflows.
Two functions are causing the problem:
Code: Select all
function npcMoveTo(foo, x, y)
-- Is the player there?
if player.grid_x/tileSize == x and player.grid_y/tileSize == y then
-- Attack the player.
enemyAttackPC(foo)
return
end
-- Is there a closed door at the destination?
if mapCurrent.scenery[y][x] == 1 then
-- If so, open it.
entityOpenDoor(foo, x, y)
return
end
-- Is there already an entity there?
for i, v in ipairs(enemies) do
if x*tileSize == v.grid_x and y*tileSize == v.grid_y then
-- If so, tag every tile containing an entity as unpathable
for i, v in ipairs(enemies) do
mapCurrent.pathing[v.grid_y/tileSize][v.grid_x/tileSize] = 0
end
-- Then generate a new path
approachPC(foo)
mapCurrent.pathing = deepCopy(mapCurrent.terrain)
return
end
end
-- Otherwise, move closer.
foo.grid_x = x * tileSize
foo.grid_y = y * tileSize
foo.energy = foo.energy - foo.moveCost
end
and
Code: Select all
function approachPC(foo)
calculatePath(foo, player)
if foo.path == false or foo.path == nil then -- Can the enemy path to the PC?
-- If not, then do nothing
foo.energy = foo.energy - foo.moveCost
else
-- Then take the first step
local nextStep = foo.path[2]
npcMoveTo(foo, nextStep.x, nextStep.y)
end
end
I have poured over these two functions several times, found one error (that approachPC(foo) was approachPC(v) about twenty minutes ago, causing all sorts of problems), but nothing that solves the stack overflow. Can anyone see where i'm messing up in these two? Or is the problem somewhere else that's causing the stack overflow?