[Freeciv-Dev] Re: [PATCH] more small directional cleanups
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
On Mon, 20 Aug 2001, Jason Dorje Short wrote:
> I added an assertion to make sure the loop terminates (if debugging;
> otherwise we assume it terminates). Bailing out won't really do any
> good since it would leave the game in an unstable state.
Here are I think better ways to do it. My rand_neighbor function will run in
a bounded time, and will aways find a neighbor if one exists.
void rand_neighbor(int x0, int y0, int *x, int *y)
{
int n, choice;
int dirs[8] = {DIR8_NORTH, DIR8_NORTHEAST, DIR8_EAST, DIR8_SOUTHEAST,
DIR8_SOUTH, DIR8_SOUTHWEST, DIR8_WEST, DIR8_NORTHWEST};
for(n=8;n>0;n--) { /* We get 8 chances to find a spot */
choice = myrand(n);
*x = x0 + DIR_DX[dirs[choice]];
*y = y0 + DIR_DY[dirs[choice]];
if(normalize_map_pos(x, y)) return; /* Found a valid spot! */
/* Choice was bad, so replace it with the last direction in the list.
* On the next iteration, one fewer choices will remain. */
dirs[choice] = dirs[n-1];
}
/* What the hell? No valid neighbor tiles. This is a 1x1 map? */
assert(0);
}
This is I think a little nicer way to do dir_get_name().
const char *dir_get_name(enum direction8 dir)
{
/* a switch statement is used so the ordering can be changed easily */
switch(dir) {
case DIR8_NORTH: return "N";
case DIR8_NORTHEAST: return "NE";
case DIR8_EAST: return "E";
case DIR8_SOUTHEAST: return "SE";
case DIR8_SOUTH: return "S";
case DIR8_SOUTHWEST: return "SW";
case DIR8_WEST: return "W";
default:
assert(0);
return "Bad Direction";
}
}
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, (continued)
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Ross W. Wetmore, 2001/08/20
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Trent Piepho, 2001/08/20
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Raimar Falke, 2001/08/21
- Message not available
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Ross W. Wetmore, 2001/08/20
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Trent Piepho, 2001/08/20
- Message not available
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Ross W. Wetmore, 2001/08/21
[Freeciv-Dev] Re: [PATCH] more small directional cleanups, Jason Dorje Short, 2001/08/20
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups,
Trent Piepho <=
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Raimar Falke, 2001/08/21
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Jason Dorje Short, 2001/08/21
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Raimar Falke, 2001/08/22
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Jason Dorje Short, 2001/08/22
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Trent Piepho, 2001/08/22
- [Freeciv-Dev] Re: [PATCH] more small directional cleanups, Raimar Falke, 2001/08/22
Message not available[Freeciv-Dev] Re: [PATCH] more small directional cleanups, Ross W. Wetmore, 2001/08/20
[Freeciv-Dev] Re: [PATCH] more small directional cleanups, Ross W. Wetmore, 2001/08/20
|
|