# Restricted Movement

## Recommended Posts

Poskusin    160
I'm have a little, ok, maybe a lot of trouble with how to do this problem. It seems like it should be so simple but I just can't figure it out. Let me try to explain. If you've ever played Shining Force (I or II) for sega genesis (and I'm sure many other games) then you will understand what I'm trying to do. In battle when you get to move, if your movement stat is equal to 1 then: XOX OOO XOX Your allowed movement grid looks like this, for 2 it would be: XXOXX XOOOX OOOOO XOOOX XXOXX 3: XXXOXXX XXOOOXX XOOOOOX OOOOOOO XOOOOOX XXOOOXX XXXOXXX ect. I'm having so much trouble figure out how to do this either in an embedded loop such as for x = -move to move for y = -move to move Am I going about this all wrong? Is there an easier way to do this? If it is a tile that you can walk on, it blt's a square to the x,y. I'm sorry, this is probably making no sence, but that goes to show how confused I am about it. So if you understand please lend me a hand. I'm writing it in VB, but I understand enough about c/c++ to try and translate if it's easier for you to explain it that way. Thanks. :)

##### Share on other sites
Kest    547
They just can't move diagonally. If you count the number of straight steps to each tile, it always matches the number you specified.

If you're just having trouble coding it, try using a rectangle. Size the rectangle by the number of steps in both deminsions, then add 1 for the center. For each tile of this rectangle, simply try to move back to the center tile and count the number of steps it took.

The pathfinding can be done really generically by reducing the path directions to two (toward the center, like upper-left tile can only move either down or right - just check for walls and other characters), but then your characters won't be able to get around objects. The possible paths will be more like line of sight. To do this correctly, you'll have to impliment some kind of path finding routine.

For a generic system, something like this for each tile in the rectangle..

loop (for each step){  if( center.y < current.y )    movedown();  else if( center.y > current.y )    moveup();  else if( center.x > current.x )    moveright();  else if( center.x < current.x )    moveleft();  else    finishwithtile();}

Count the number of steps, and if it becomes too much, break from the loop and mark it as though they can't move to it.

edit: Oh yeah, up might be negative in your coordinates, so moveup() and movedown() might need swapped.

##### Share on other sites
JohnBolton    1372
I'm not sure what you are trying to do, but the following code will enumerate all the possible spots you could move to (assuming the maximum distance is measured by the "manhattan" distance):
    for ( int i = -move; i <= move; ++i )    {        y = center.y + i;                int move_x = move - abs(i);        for ( int j = -move_x; j <= move_x; ++j )        {            x = center.x + j;            // do something with x,y        }    }

##### Share on other sites
Poskusin    160
Thanks you guys. Took some trying but I finally got it. Hope you will check out the screenshots in the announcments. :)