tags:

views:

81

answers:

3

Hi all, I'm working on a game, and I have the necessity to check a closed path in a given numerical heightmap: The server and the client use this heightmap to set the right coords to move etc... Now, when an user walks on a "special" tile, it lights... My problem is: When an user, walking on these tiles creates a closed path with empty tiles in it, the server should automatically fill the tiles in this path...

It should do something like this: http://www.youtube.com/watch?v=kAVUNE2NTUQ - 1:32

I'm sure I've to use some maths here or there, but I dunno how... I could do a "for" cycle, but it would be too long, and the problem is that the server needs to do the cycle every time an user walks... Thanks in advance for your answers, hope someone could help me.

PS: I'm using C#

EDIT: When an user walks on a tile, the server automatically replaces the heightmap[X, Y] with an integer that represents the color of the user

A: 

"the problem is that the server needs to do the cycle every time an user walks"...

You can eliminate the cycling over walked tiles by assigning each tile in the game a unique integer, having an array with the number of tiles, and then for each step mark this tile in the array. Of course, also check whether the tile has been walked before (i.e. whether it's marked in the array), and if it has, you have a loop (maybe of zero area). This approach has no cycling over walked tiles, etc, but just a single look-up for each step.

tom10
Yes, when a tiles light, the server automatically replace the current heightmap[X, Y] with an integer representing the color of the user. the probles is that I have no idea on how to check if there is a closed path etc..
Antonius Magnae
Isn't visiting the same tile twice a necessary and sufficient criteria for a closed path? The only case where this might not work is for the case of backtracking (which is just a really skinny closed loop, and it depends on how one defines a closed path), but if this is a problem, it's an easy case to throw out.
tom10
No: clicking 2 times on a same tile isn't sufficient for a closed path: I have the necessity to check when an user, walking on the tiles, creates the perimeter of a polygon with empty tiles in it...
Antonius Magnae
A: 
Beta
Check fo every single neightbor could need a lot of time, because there are some rooms with over 400 tiles...
Antonius Magnae
@Antonius Magnae: no, when I say neighbors I mean the eight squares surrounding your square.
Beta
Yes, I could check for the 8 squares that surround the one the user clickes, but then I should also check other 8 tiles foreach clicked tile near the first 8 and so on...
Antonius Magnae
@Antonius Magnae: No, you misunderstand my algorithm. If one of the neighbors has already been clicked, then you check the links to see whether you have formed a closed path; this can be done locally.
Beta
A: 

Let's assume you have rectangular field with upper left corner in (0, 0) and bottom right in (M, N). Then you can use the following pseudocode fill algorithm:

find the upper and bottom cells of the path (yTop, yBottom)

for (int y = yTop; y != yBottom; ++y)
{
    find leftmost and rightmost path cells (xLeft, xRight) for that y
    bool isInside = true;
    for (int x = xLeft+1; x<xRight; ++x)
    {
        if ((x, y) is path cell)
            isInside = !isInside;
        if (isInside)
            fill(x, y);
    }
}
Grozz
This could work for polygons 8or closed paths9 of a given leght etc of each side.. But the problem is the path could be a cube with 9 tiles of area, of or 16 etc... but it also could be a casual colsed path of 30, 40,50 etc tiles.....
Antonius Magnae