I'm working on a Fire Emblem/Final Fantasy Tactics/Advance Wars type game: a turn-based tile strategy game.
However, I'm having trouble making an efficient move function. I need to highlight the possible tiles that a unit can move to. As of now, here's how this works. If you guys need the actual code, just let me know.
I have a 2d array of Points called "highlightedTiles". This contains the current X and Y values of the highlighted tiles on the map.
When the player wants to move a unit, a function highlights the tiles that can be moved to. This function works like so.
I have 4 arrays of points. Each array is initalized using a recursive function which checks the adjacent tiles to see if they are either already highlighted in the current tree, occupied, or unoccupiable. If all of these requirements are false, then the tile is highlighted and it calls itself for each adjacent tile. If any of those requirements are true, the function simply returns. The recursive function takes the unit's speed and recurses speed - 1. If speed becomes 0, it will simply return.
So, imagine this scenario with a Unit whose speed is 3.
..X.......XX-.....XXXO|.....XXXXX.....XXX.......X.....
The O is the unit, dashes and '|'s are walls, and X's are movable locations highlighted correctly by the function. It works perfectly.
The problem is that once a unit's speed goes above 5, you start to notice the time it takes to loop through and find each tile. At about 10 speed in the middle of the map, it takes about 5 seconds. At 15 speed, it took about 10 minutes.
I know there has to be a more efficient way to do this. I'd appreciate any input you can offer me. Any links to other people's algorithms would be just as great.
If you guys want to see the code, just say the word.
Thanks!