Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2001:
[Freeciv-Dev] Re: [PATCH] more small directional cleanups
Home

[Freeciv-Dev] Re: [PATCH] more small directional cleanups

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Trent Piepho <xyzzy@xxxxxxxxxxxxx>
Cc: Jason Dorje Short <jshort@xxxxxxxxxxxxx>, freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: [PATCH] more small directional cleanups
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 21 Aug 2001 10:32:10 +0200
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Mon, Aug 20, 2001 at 03:08:52PM -0700, Trent Piepho wrote:
> 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);
> }

I like it.

> 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";
>   }
> }

This is a little bit better than Jason's version.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "SIGDANGER - The System is likely to crash soon"


[Prev in Thread] Current Thread [Next in Thread]