I would strongly suggest to convert this method to an iterative one. Recursive method calls are almost always a bad thing.
Yes, I suggest looking for another way as well - if you got a tank leak that let's you loose a liter per kilometer, you shouldn't ask for a bigger tank - you should ask how the leak can be fixed...
Please describe what you want to do instead of how you want to do it, then we can point you to alternative solutions.
I give this method a point in the map inside a certain area, then I want it to add every tile inside that area to a table, starting from that point,
- terr_tile?(x,y) returns true if the tile in that position is not a border(a special tile which can be either created by the player or be there by default)
- !@@internal_table.include?([x,y]) is a condition I added to avoid checking the same tile twice.
When this method ends the entire table is analyzed and the tiles inside it are changed accordingly to other variables, then @@internal_table is erased until internal_table(x,y) is called again, I'd really like to do it more efficiently, but I can't come up with anything else.
e.g:
X = Border, O = free tile
OXXXOOOOXXXXOO
OXOXXXOXXOOXOO
XXOOOXXXOOOXXO
XOOOOOOOOOOXO
XXXOOXXXOOXXXO
OOXXXXOXXXXOOO
I want to get the area inside the border, can I do it using a while loop?
Assuming, that you want to cover the whole map:
(0..$game_map.width - 1).each do |x| (0..$game_map.height - 1).each do |y| @@internal_table.push([x, y]) if terr_tile?(x, y) endendI didn't actually test that, but this should iterate over every tile on the map and add it to @@internal_table if terr_tile? returns true.
In many cases, the recursion depth of your algorithm is equal to the number of elements in your result.
You could use a queue for pending elements to resolve the recursion. You can also keep the size of the queue relatively small by evaluating latest additions last:
def internal_table(x,y) included = Hash.new(false) pending = [[x, y]] # Iterate: until pending.empty? x, y = point = pending.shift if !included[point] && terr_tile?(x, y) included[point] = true pending.push([x+1, y], [x-1, y], [x, y+1], [x, y-1]) end end # Return included Points: return included.keysendEdit: Too slow, but didn't want to drop the post either...
Depending on what you actually want, this still might be unnecessary of course.
The border is created in-game by the player, if he presses a button while walking he traces a line on the ground (I should have better specified this), so he could also create complex geometric shapes, and it's impossible to pre-define them in the editor.
yes, in that case you'll have to calculate the area. I hope one of the alternatives above gives a better performance, and I think some of them need to be adjusted to differ between the inside and outside of a closed border.
Though the algorithm above is by no means the fastest solution, there might be other problems if the player is allowed to draw borders like this:
░░░░░▓▓▓▓▓▓▓▓▓
░░░░░▓░░░░░░░▓
░▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓░░░▓░░░░░░░░
▓░░░░▓░░░░░░░░
▓▓▓▓▓▓░░░░░░░░
I'm not sure I understood exactly how the algorithm works, but you also might face similar problems with these shapes then:
░░░ ░▓▓▓▓░ ▓▓▓▓▓
░░░ ▓▓▓▓▓▓ ▓░░░▓
░▓░ ▓▓░░▓▓ ▓▓▓▓▓
░▓░ ▓▓░░▓▓ ▓▓░░░
░░░ ▓▓▓▓▓▓ ▓▓▓▓▓
░░░ ░▓▓▓▓░ ▓░░░▓
░░░ ░░░░░░ ▓▓▓▓▓
In case you face performance issues, a more complex way might be to follow the border and store the left/right boundaries for each line of the shape. At least in theory this should be a much more efficient when you deal with large areas (for small areas the floodfilling should do fine I think).