Simple Recursive Tactical RPG Movement Range Issue
Posted: Fri Jun 08, 2018 2:18 am
So I'm working on a simple Fire Emblem like game. But the movement range that it calculates is a bit buggy when the movement range is 3. I'm using a recursive function that will check if a space can be reached by a unit, if it can't it jumps out of the recursion, if it does then it is added to an array of valid spaces and is marked true, then it checks left, right, up and down of that space. However, if the max number of spaces to move is three, it will think the top and bottom two spaces are not accessible when they should be. I provided the code in the main.lua added and a pic to illustrate the goal. Any suggestions would be most helpful! If you need a better explanation please ask!
Code: Select all
function ValidSpaces(x, y, movement, moveMax)
--BASE CASE
-- if movement is greater than max, or the new space is out of map bounds
if (movement > moveMax) or (x > mapW) or (x < 0) or (y < 0) or (y > mapH) then
return false
end
-- if that space has not been checked
if validMove[tostring(x..':'..y)] == nil then
validMove[tostring(x..':'..y)] = true
return
ValidSpaces(x+1, y, movement + 1, moveMax), -- Check right
ValidSpaces(x-1, y, movement + 1, moveMax), -- check left
ValidSpaces(x, y+1, movement + 1, moveMax), -- check down
ValidSpaces(x, y-1, movement + 1, moveMax) -- check up
end
end