Index: ai/advdomestic.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/advdomestic.c,v retrieving revision 1.71 diff -u -r1.71 advdomestic.c --- ai/advdomestic.c 2001/08/26 13:10:11 1.71 +++ ai/advdomestic.c 2001/08/30 11:07:24 @@ -90,6 +90,7 @@ static int ocean_workers(struct city *pcity) { int i = 0; + city_map_checked_iterate(pcity->x, pcity->y, cx, cy, mx, my) { if (map_get_terrain(mx, my) == T_OCEAN) { /* this is a kluge; wasn't getting enough harbors because often @@ -99,31 +100,34 @@ if (is_worker_here(pcity, cx, cy)) i++; } - } - city_map_checked_iterate_end; - return (i / 2); + } city_map_checked_iterate_end; + return i / 2; } static int road_trade(struct city *pcity) { int i = 0; + city_map_checked_iterate(pcity->x, pcity->y, cx, cy, mx, my) { - if (map_get_special(mx, my) & S_ROAD && is_worker_here(pcity, cx, cy)) + if (map_get_special(mx, my) & S_ROAD + && is_worker_here(pcity, cx, cy)) i++; - } - city_map_checked_iterate_end; - return(i); + } city_map_checked_iterate_end; + + return i; } static int farmland_food(struct city *pcity) { int i = 0; + city_map_checked_iterate(pcity->x, pcity->y, cx, cy, mx, my) { if (map_get_special(mx, my) & S_FARMLAND - && is_worker_here(pcity, cx, cy)) i++; - } - city_map_checked_iterate_end; - return(i); + && is_worker_here(pcity, cx, cy)) + i++; + } city_map_checked_iterate_end; + + return i; } static int pollution_cost(struct player *pplayer, struct city *pcity, Index: ai/aiunit.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v retrieving revision 1.150 diff -u -r1.150 aiunit.c --- ai/aiunit.c 2001/08/26 21:45:22 1.150 +++ ai/aiunit.c 2001/08/30 11:07:31 @@ -682,10 +682,9 @@ best = 1; *dest_y = y1; *dest_x = x1; } /* next to nothing is better than nothing */ } - } - adjc_iterate_end; + } adjc_iterate_end; - return(best); + return best; } /************************************************************************* @@ -740,31 +739,35 @@ adjc_iterate(dest_x, dest_y, x1, y1) { ok = 0; t = map_get_terrain(x1, y1); - if (warmap.seacost[x1][y1] <= 6 * THRESHOLD && t != T_OCEAN) { /* accessible beachhead */ + if (warmap.seacost[x1][y1] <= 6 * THRESHOLD && t != T_OCEAN) { + /* accessible beachhead */ adjc_iterate(x1, y1, x2, y2) { - if (map_get_terrain(x2, y2) == T_OCEAN) { - if (is_my_zoc(unit_owner(punit), x2, y2)) { - ok++; - break; /* out of adjc_iterate */ - } + if (map_get_terrain(x2, y2) == T_OCEAN + && is_my_zoc(unit_owner(punit), x2, y2)) { + ok++; + break; /* out of adjc_iterate */ } - } - adjc_iterate_end; + } adjc_iterate_end; - if (ok) { /* accessible beachhead with zoc-ok water tile nearby */ + if (ok) { + /* accessible beachhead with zoc-ok water tile nearby */ ok = get_tile_type(t)->defense_bonus; if (map_get_special(x1, y1) & S_RIVER) ok += (ok * terrain_control.river_defense_bonus) / 100; if (get_tile_type(t)->movement_cost * SINGLE_MOVE < - unit_types[punit->type].move_rate) ok *= 8; + unit_types[punit->type].move_rate) + ok *= 8; ok += (6 * THRESHOLD - warmap.seacost[x1][y1]); - if (ok > best) { best = ok; *x = x1; *y = y1; } + if (ok > best) { + best = ok; + *x = x1; + *y = y1; + } } } - } - adjc_iterate_end; + } adjc_iterate_end; - return(best); + return best; } /************************************************************************** @@ -774,22 +777,23 @@ **************************************************************************/ static void find_city_beach( struct city *pc, struct unit *punit, int *x, int *y) { - int best_xx = punit->x, best_yy = punit->y; + int best_x = punit->x; + int best_y = punit->y; int dist = 100; - int search_dist = real_map_distance( pc->x, pc->y, punit->x, punit->y ); + int search_dist = real_map_distance(pc->x, pc->y, punit->x, punit->y) - 1; + + square_iterate(punit->x, punit->y, search_dist, xx, yy) { + if (map_same_continent(xx, yy, pc->x, pc->y) + && real_map_distance(punit->x, punit->y, xx, yy) <= dist) { - square_iterate(punit->x, punit->y, search_dist - 1, xx, yy) { - if (map_same_continent(xx, yy, pc->x, pc->y) && - real_map_distance(punit->x, punit->y, xx, yy) < dist) { dist = real_map_distance(punit->x, punit->y, xx, yy); - best_xx = xx; - best_yy = yy; + best_x = xx; + best_y = yy; } - } - square_iterate_end; + } square_iterate_end; - *x = best_xx; - *y = best_yy; + *x = best_x; + *y = best_y; } /************************************************************************** Index: client/goto.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/client/goto.c,v retrieving revision 1.19 diff -u -r1.19 goto.c --- client/goto.c 2001/08/26 09:28:16 1.19 +++ client/goto.c 2001/08/30 11:07:33 @@ -324,7 +324,8 @@ /* Try to move to all tiles adjacent to x,y. The coordinats of the tile we try to move to are x1,y1 */ adjc_dir_iterate(x, y, x1, y1, dir) { - if (restriction == GOTO_MOVE_CARDINAL_ONLY && !DIR_IS_CARDINAL(dir)) + if ((restriction == GOTO_MOVE_CARDINAL_ONLY) + && !DIR_IS_CARDINAL(dir)) continue; pdesttile = map_get_tile(x1, y1); @@ -451,13 +452,10 @@ abort(); break; } /****** end switch ******/ - } - adjc_dir_iterate_end; + } adjc_dir_iterate_end; } /* end while */ } - - /************************************************************************** Insert a point and draw the line on the map. Will extend the array if needed. @@ -631,11 +629,9 @@ assert(is_real_tile(x, y)); normalize_map_pos(&x, &y); - x1 = x + DIR_DX[dir]; - y1 = y + DIR_DY[dir]; + MAPSTEP(x1, y1, x, y, dir); /* It makes no sense to draw a goto line to a non-existant tile. */ assert(is_real_tile(x1, y1)); - normalize_map_pos(&x1, &y1); if (dir >= 4) { x = x1; @@ -702,6 +698,7 @@ } /* undraw the line segment */ + dir = get_direction_for_step(line_x, line_y, dest_x, dest_y); undraw_segment(line_x, line_y, dir); @@ -751,7 +748,8 @@ ***********************************************************************/ static int find_route(int x, int y) { - int i, first_index, last_x, last_y; + int last_x, last_y; + int i, first_index; if (route == NULL) { route = fc_malloc(route_length * sizeof(struct map_position)); @@ -785,8 +783,8 @@ route_index++; return find_route(new_x, new_y); /* how about recoding freeciv in MosML? */ } - } - adjc_dir_iterate_end; + } adjc_dir_iterate_end; + assert(0); /* should find direction... */ return -1; /* why can't the compiler figure out that create_goto_map() will newer create a vector that leads to a pos without a Index: common/city.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/city.h,v retrieving revision 1.86 diff -u -r1.86 city.h --- common/city.h 2001/08/26 13:10:12 1.86 +++ common/city.h 2001/08/30 11:07:36 @@ -64,36 +64,41 @@ Some places in the code hardcodes this number (yet). */ #define CITY_MAP_SIZE 5 -/* Iterate a city map. x and y will be elements of [0,CITY_MAP_SIZE). */ -#define city_map_iterate(x, y) \ -{ \ - int x, y; \ - for (y=0;y= map.ysize) \ - continue; \ + y_itr = city_y + MCMI_y - CITY_MAP_SIZE/2; \ + if (y_itr < 0 || y_itr >= map.ysize) \ + continue; \ x_itr = map_adjust_x(city_x + MCMI_x - CITY_MAP_SIZE/2); -#define map_city_radius_iterate_end \ - } \ - } \ - } \ +#define map_city_radius_iterate_end \ + } \ + } \ + } \ } Index: common/map.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/map.c,v retrieving revision 1.82 diff -u -r1.82 map.c --- common/map.c 2001/08/26 09:28:16 1.82 +++ common/map.c 2001/08/30 11:07:38 @@ -73,6 +73,9 @@ N_("Fallout") }; +#define NUM_SPECIAL_NAMES \ + (sizeof(tile_special_type_names)/sizeof(tile_special_type_names[0])) + extern int is_server; /*************************************************************** @@ -157,44 +160,32 @@ } /*************************************************************** - Returns 1 if we are at a stage of the game where the map - has not yet been generated/loaded. - (To be precise, returns 1 if map_allocate() has not yet been - called.) -***************************************************************/ -int map_is_empty(void) -{ - return map.tiles==0; -} - - -/*************************************************************** put some sensible values into the map structure Also initialize special void_tile. ***************************************************************/ void map_init(void) { - map.xsize=MAP_DEFAULT_WIDTH; - map.ysize=MAP_DEFAULT_HEIGHT; - map.seed=MAP_DEFAULT_SEED; - map.riches=MAP_DEFAULT_RICHES; - map.is_earth=0; - map.huts=MAP_DEFAULT_HUTS; - map.landpercent=MAP_DEFAULT_LANDMASS; - map.grasssize=MAP_DEFAULT_GRASS; - map.swampsize=MAP_DEFAULT_SWAMPS; - map.deserts=MAP_DEFAULT_DESERTS; - map.mountains=MAP_DEFAULT_MOUNTAINS; - map.riverlength=MAP_DEFAULT_RIVERS; - map.forestsize=MAP_DEFAULT_FORESTS; - map.generator=MAP_DEFAULT_GENERATOR; - map.tiles=0; - map.num_continents = 0; - map.num_start_positions=0; - map.fixed_start_positions=0; - map.have_specials = 0; - map.have_rivers_overlay = 0; - map.have_huts = 0; + map.xsize = MAP_DEFAULT_WIDTH; + map.ysize = MAP_DEFAULT_HEIGHT; + map.seed = MAP_DEFAULT_SEED; + map.riches = MAP_DEFAULT_RICHES; + map.is_earth = 0; + map.huts = MAP_DEFAULT_HUTS; + map.landpercent = MAP_DEFAULT_LANDMASS; + map.grasssize = MAP_DEFAULT_GRASS; + map.swampsize = MAP_DEFAULT_SWAMPS; + map.deserts = MAP_DEFAULT_DESERTS; + map.mountains = MAP_DEFAULT_MOUNTAINS; + map.riverlength = MAP_DEFAULT_RIVERS; + map.forestsize = MAP_DEFAULT_FORESTS; + map.generator = MAP_DEFAULT_GENERATOR; + map.tiles = NULL; + map.num_continents = 0; + map.num_start_positions = 0; + map.fixed_start_positions = 0; + map.have_specials = 0; + map.have_rivers_overlay = 0; + map.have_huts = 0; tile_init(&void_tile); } @@ -221,22 +212,13 @@ /*************************************************************** ... ***************************************************************/ -struct tile_type *get_tile_type(enum tile_terrain_type type) -{ - return &tile_types[type]; -} - -/*************************************************************** -... -***************************************************************/ enum tile_terrain_type get_terrain_by_name(char * name) { enum tile_terrain_type tt; - for (tt = T_FIRST; tt < T_COUNT; tt++) { - if (0 == strcmp (tile_types[tt].terrain_name, name)) { + for (tt = T_FIRST; tt < T_COUNT; tt++) + if (!strcmp (tile_types[tt].terrain_name, name)) break; - } - } + return tt; } @@ -245,11 +227,8 @@ ***************************************************************/ char *get_terrain_name(enum tile_terrain_type type) { - if (type < T_COUNT) { - return get_tile_type(type)->terrain_name; - } else { - return NULL; - } + assert(type < T_COUNT); + return tile_types[type].terrain_name; } /*************************************************************** @@ -257,15 +236,13 @@ ***************************************************************/ enum tile_special_type get_special_by_name(char * name) { - int inx; + int i; enum tile_special_type st = 1; - for (inx = 0; - inx < sizeof(tile_special_type_names)/sizeof(tile_special_type_names[0]); - inx++) { - if (0 == strcmp(name, tile_special_type_names[inx])) { + for (i = 0; i < NUM_SPECIAL_NAMES; i++) { + if (!strcmp(name, tile_special_type_names[i])) return st; - } + st <<= 1; } @@ -277,14 +254,12 @@ ***************************************************************/ const char *get_special_name(enum tile_special_type type) { - int inx; + int i; - for (inx = 0; - inx < sizeof(tile_special_type_names)/sizeof(tile_special_type_names[0]); - inx++) { - if (type & 0x1) { - return tile_special_type_names[inx]; - } + for (i = 0; i < NUM_SPECIAL_NAMES; i++) { + if (type & 0x1) + return tile_special_type_names[i]; + type >>= 1; } @@ -435,14 +410,6 @@ } /*************************************************************** -... -***************************************************************/ -int map_same_continent(int x1, int y1, int x2, int y2) -{ - return (map_get_continent(x1,y1) == map_get_continent(x2,y2)); -} - -/*************************************************************** Returns 1 if (x,y) is _not_ a good position to start from; Bad places: - Non-suitable terrain; @@ -594,22 +561,12 @@ ***************************************************************/ int get_tile_trade_base(struct tile * ptile) { - if(ptile->special&S_SPECIAL_1) - return (tile_types[ptile->terrain].trade_special_1); - else if(ptile->special&S_SPECIAL_2) - return (tile_types[ptile->terrain].trade_special_2); + if (ptile->special & S_SPECIAL_1) + return tile_types[ptile->terrain].trade_special_1; + else if (ptile->special & S_SPECIAL_2) + return tile_types[ptile->terrain].trade_special_2; else - return (tile_types[ptile->terrain].trade); -} - -/*************************************************************** -... -***************************************************************/ -int get_tile_infrastructure_set(struct tile * ptile) -{ - return - ptile->special & - (S_ROAD | S_RAILROAD | S_IRRIGATION | S_FARMLAND | S_MINE | S_FORTRESS | S_AIRBASE); + return tile_types[ptile->terrain].trade; } /*************************************************************** @@ -1007,7 +964,7 @@ static int tile_move_cost_ai(struct tile *tile0, struct tile *tile1, int x, int y, int x1, int y1, int maxcost) { - assert(y >= 0 && y <= map.ysize); + assert(is_real_tile(x, y)); assert(!is_server || (tile0->terrain != T_UNKNOWN && tile1->terrain != T_UNKNOWN)); if (tile0->terrain == T_OCEAN) { @@ -1051,9 +1008,8 @@ debug_log_move_costs("Resetting move costs for", x, y, tile0); for (dir = 0; dir < 8; dir++) { - x1 = x + DIR_DX[dir]; - y1 = y + DIR_DY[dir]; - if (normalize_map_pos(&x1, &y1)) { + MAPSTEP(x1, y1, x, y, dir); + if (is_real_tile(x1, y1)) { tile1 = map_get_tile(x1, y1); tile0->move_cost[dir] = tile_move_cost_ai(tile0, tile1, x, y, x1, y1, maxcost); @@ -1087,9 +1043,8 @@ int x1, y1; tile0 = map_get_tile(x, y); for (dir = 0; dir < 8; dir++) { - x1 = map_adjust_x(x + DIR_DX[dir]); - y1 = y + DIR_DY[dir]; - if (normalize_map_pos(&x1, &y1)) { + MAPSTEP(x1, y1, x, y, dir); + if (is_real_tile(x1, y1)) { tile1 = map_get_tile(x1, y1); tile0->move_cost[dir] = tile_move_cost_ai(tile0, tile1, x, y, x1, y1, maxcost); @@ -1123,33 +1078,31 @@ return 0; } - - /*************************************************************** ... ***************************************************************/ void tile_init(struct tile *ptile) { - ptile->terrain=T_UNKNOWN; - ptile->special=S_NO_SPECIAL; - ptile->known=0; - ptile->sent=0; - ptile->city=NULL; + ptile->terrain = T_UNKNOWN; + ptile->special = S_NO_SPECIAL; + ptile->known = 0; + ptile->sent = 0; + ptile->city = NULL; unit_list_init(&ptile->units); - ptile->worked = NULL; /* pointer to city working tile */ + ptile->worked = NULL; /* pointer to city working tile */ ptile->assigned = 0; /* bitvector */ } - /*************************************************************** ... ***************************************************************/ struct tile *map_get_tile(int x, int y) { - if(y<0 || y>=map.ysize) + x = map_adjust_x(x); + if(!is_real_tile(x, y)) return &void_tile; /* accurate fix by Syela */ else - return map.tiles+map_adjust_x(x)+y*map.xsize; + return MAP(x, y); } /*************************************************************** @@ -1157,10 +1110,11 @@ ***************************************************************/ signed short map_get_continent(int x, int y) { - if (y<0 || y>=map.ysize) + x = map_adjust_x(x); + if (!is_real_tile(x, y)) return -1; else - return (map.tiles + map_adjust_x(x) + y * map.xsize)->continent; + return MAP(x, y)->continent; } /*************************************************************** @@ -1168,20 +1122,20 @@ ***************************************************************/ void map_set_continent(int x, int y, int val) { - assert(is_real_tile(x, y)); - (map.tiles + map_adjust_x(x) + y * map.xsize)->continent=val; + x = map_adjust_x(x); + MAP(x, y)->continent = val; } - /*************************************************************** ... ***************************************************************/ enum tile_terrain_type map_get_terrain(int x, int y) { - if(y<0 || y>=map.ysize) + x = map_adjust_x(x); + if (!is_real_tile(x, y)) return T_UNKNOWN; else - return (map.tiles+map_adjust_x(x)+y*map.xsize)->terrain; + return MAP(x, y)->terrain; } /*************************************************************** @@ -1189,10 +1143,11 @@ ***************************************************************/ enum tile_special_type map_get_special(int x, int y) { + x = map_adjust_x(x); if(!is_real_tile(x, y)) return S_NO_SPECIAL; else - return (map.tiles + map_adjust_x(x) + y*map.xsize)->special; + return MAP(x, y)->special; } /*************************************************************** @@ -1200,8 +1155,8 @@ ***************************************************************/ void map_set_terrain(int x, int y, enum tile_terrain_type ter) { - (map.tiles+map_adjust_x(x)+ - map_adjust_y(y)*map.xsize)->terrain=ter; + x = map_adjust_x(x); + MAP(x, y)->terrain = ter; } /*************************************************************** @@ -1209,10 +1164,8 @@ ***************************************************************/ void map_set_special(int x, int y, enum tile_special_type spe) { - x = map_adjust_x(x); - y = map_adjust_y(y); - - (map.tiles +x + y*map.xsize)->special |= spe; + x = map_adjust_x(x); + MAP(x, y)->special |= spe; if (spe & (S_ROAD | S_RAILROAD)) reset_move_costs(x, y); @@ -1223,9 +1176,8 @@ ***************************************************************/ void map_clear_special(int x, int y, enum tile_special_type spe) { - x = map_adjust_x(x); - y = map_adjust_y(y); - (map.tiles + x + y*map.xsize)->special &= ~spe; + normalize_map_pos(&x, &y); + MAP(x, y)->special &= ~spe; if (spe & (S_ROAD | S_RAILROAD)) reset_move_costs(x, y); @@ -1236,8 +1188,8 @@ ***************************************************************/ struct city *map_get_city(int x, int y) { - x = map_adjust_x(x); y = map_adjust_y(y); - return (map.tiles + x + y*map.xsize)->city; + x = map_adjust_x(x); + return MAP(x, y)->city; } @@ -1246,7 +1198,8 @@ ***************************************************************/ void map_set_city(int x, int y, struct city *pcity) { - (map.tiles+map_adjust_x(x)+map_adjust_y(y)*map.xsize)->city=pcity; + x = map_adjust_x(x); + MAP(x, y)->city = pcity; } @@ -1255,8 +1208,9 @@ ***************************************************************/ enum known_type tile_is_known(int x, int y) { - return (enum known_type) (((map.tiles+map_adjust_x(x)+ - map_adjust_y(y)*map.xsize)->known)); + x = map_adjust_x(x); + assert(!is_server); + return (enum known_type) MAP(x, y)->known; } /*************************************************************** @@ -1265,8 +1219,8 @@ ***************************************************************/ int same_pos(int x1, int y1, int x2, int y2) { - return (map_adjust_x(x1) == map_adjust_x(x2) - && map_adjust_y(y1) == map_adjust_y(y2)); + return (map_adjust_y(y1) == map_adjust_y(y2) + && map_adjust_x(x1 - x2) == 0); } /************************************************************************** @@ -1278,8 +1232,7 @@ if (!normalize_map_pos(x, y)) { freelog(LOG_ERROR, "%d, %d is not a real tile!", *x, *y); - *x = map_adjust_x(*x); - *y = map_adjust_y(*y); + nearest_real_tile(x, y); return 0; } @@ -1289,20 +1242,12 @@ return 1; } -/*************************************************************** -If we just adjust we might light a tile twice; That would be a -Mayor problem with fog of war -***************************************************************/ -int is_real_tile(int x, int y) -{ - return 0 <= y && y < map.ysize; -} - /************************************************************************** -Normalizes the map position. Returns TRUE if it is valid, false otherwise. +Normalizes the map position. Returns TRUE if it is real, FALSE otherwise. **************************************************************************/ -int normalize_map_pos(int *x, int *y) { - if (*y < 0 || *y >= map.ysize) +int normalize_map_pos(int *x, int *y) +{ + if (!is_real_tile(*x, *y)) return FALSE; while (*x < 0) @@ -1311,6 +1256,22 @@ *x -= map.xsize; return TRUE; +} + +/************************************************************************** +Twiddle *x and *y to point the the nearest real tile. +**************************************************************************/ +void nearest_real_tile(int *x, int *y) +{ + if (*y < 0) + *y = 0; + else if (*y >= map.ysize) + *y = map.ysize - 1; + + while (*x < 0) + *x += map.xsize; + while (*x >= map.xsize) + *x -= map.xsize; } /************************************************************************** Index: common/map.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/map.h,v retrieving revision 1.87 diff -u -r1.87 map.h --- common/map.h 2001/08/26 09:28:17 1.87 +++ common/map.h 2001/08/30 11:07:40 @@ -168,7 +168,6 @@ struct map_position start_positions[MAX_NUM_NATIONS]; }; -int map_is_empty(void); void map_init(void); void map_allocate(void); @@ -201,8 +200,10 @@ #define map_adjust_y(Y) \ (((Y)<0) ? 0 : (((Y)>=map.ysize) ? map.ysize-1 : (Y))) +#if 1 #define map_inx(x,y) \ ((x)+(y)*map.xsize) +#endif struct city *map_get_city(int x, int y); void map_set_city(int x, int y, struct city *pcity); @@ -214,14 +215,13 @@ void tile_init(struct tile *ptile); enum known_type tile_is_known(int x, int y); int check_coords(int *x, int *y); -int is_real_tile(int x, int y); int normalize_map_pos(int *x, int *y); +void nearest_real_tile(int *x, int *y); void rand_neighbour(int x0, int y0, int *x, int *y); int is_water_adjacent_to_tile(int x, int y); int is_tiles_adjacent(int x0, int y0, int x1, int y1); int map_move_cost(struct unit *punit, int x, int y); -struct tile_type *get_tile_type(enum tile_terrain_type type); enum tile_terrain_type get_terrain_by_name(char * name); char *get_terrain_name(enum tile_terrain_type type); enum tile_special_type get_special_by_name(char * name); @@ -241,7 +241,6 @@ int get_tile_food_base(struct tile * ptile); int get_tile_shield_base(struct tile * ptile); int get_tile_trade_base(struct tile * ptile); -int get_tile_infrastructure_set(struct tile * ptile); char *map_get_infrastructure_text(int spe); int map_get_infrastructure_prerequisite(int spe); int get_preferred_pillage(int pset); @@ -269,6 +268,56 @@ extern struct terrain_misc terrain_control; extern struct tile_type tile_types[T_LAST]; +/*************************************************************** + Returns 1 if we are at a stage of the game where the map + has not yet been generated/loaded. + (To be precise, returns 1 if map_allocate() has not yet been + called.) +***************************************************************/ +#define map_is_empty() (map.tiles == NULL) + +/*************************************************************** +... +***************************************************************/ +#define map_same_continent(x1, y1, x2, y2) \ + (map_get_continent((x1), (y1)) == map_get_continent((x2), (y2))) + +/* FIXME: These are particularly useless, but used everywhere. */ +#define get_tile_type(tt) (&tile_types[(tt)]) +#define get_tile_infrastructure_set(ptile) \ + ((ptile)->special & S_INFRASTRUCTURE_MASK) + +#define is_real_tile(x,y) \ + (((unsigned) (y)) < ((unsigned) map.ysize)) + +#define IS_NORM_TILE(x,y) \ + (((unsigned) (x)) < ((unsigned) map.xsize)) + +#define IS_PROP_TILE(x,y) \ + (is_real_tile((x),(y)) && IS_NORM_TILE((x),(y))) + +#if 1 +#define MAP(x,y) (map.tiles + (IS_PROP_TILE((x),(y)) \ + ? ((x) + map.xsize * (y)) \ + : (abort(), 0))) +#else +#define MAP(x,y) (map.tiles + (x) + map.xsize * (y)) +#endif + +#define MAPSTEP(x,y,x1,y1,dir) \ + do { \ + (x) = (x1) + DIR_DX[(dir)]; \ + (y) = (y1) + DIR_DY[(dir)]; \ + /* if ((x) == -1) */ \ + /* (x) = map.xsize-1; */ \ + /* else if ((x) == map.xsize) */ \ + /* (x) = 0; */ \ + (x) = map_adjust_x((x)); \ + } while (0) + +#define MAPJUMP(x,y,dir) \ + MAPSTEP((x), (y), (x), (y), (dir)) + /* This iterates outwards from the starting point (Duh?). Every tile within max_dist will show up exactly once. (even takes into account wrap). All positions given correspond to real tiles. @@ -429,7 +478,8 @@ |-+-+-| |5|6|7| ------- - */ +Note that you must normalize x1 and y1 yourself. +*/ enum direction8 { /* FIXME: DIR8 is used to avoid conflict with Index: server/cityturn.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v retrieving revision 1.159 diff -u -r1.159 cityturn.c --- server/cityturn.c 2001/08/28 13:45:38 1.159 +++ server/cityturn.c 2001/08/30 11:07:45 @@ -1073,7 +1073,9 @@ x=myrand(5)-2; y=myrand(5)-2; if ( ( x != -2 && x != 2 ) || ( y != -2 && y != 2 ) ) { - x=map_adjust_x(pcity->x+x); y=map_adjust_y(pcity->y+y); + x = pcity->x + x; + y = pcity->y + y; + nearest_real_tile(&x, &y); if ( (map_get_terrain(x,y)!=T_OCEAN && map_get_terrain(x,y)<=T_TUNDRA) && (!(map_get_special(x,y)&S_POLLUTION)) ) { map_set_special(x,y, S_POLLUTION); Index: server/gotohand.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/gotohand.c,v retrieving revision 1.109 diff -u -r1.109 gotohand.c --- server/gotohand.c 2001/08/26 09:52:31 1.109 +++ server/gotohand.c 2001/08/30 11:07:49 @@ -360,8 +360,7 @@ freelog(LOG_FATAL, "Bad/unimplemented move_type in really_generate_warmap()."); abort(); } - } - adjc_dir_iterate_end; + } adjc_dir_iterate_end; } freelog(LOG_DEBUG, "Generated warmap for (%d,%d).", @@ -548,15 +547,17 @@ { int dir; - int x, y; struct player *owner = unit_owner(punit); for (dir = 0; dir < 8; dir++) { - x = map_adjust_x(src_x + DIR_DX[dir]); - y = map_adjust_y(src_y + DIR_DY[dir]); + int x, y; + MAPSTEP(x, y, src_x, src_y, dir); + y = map_adjust_y(y); /* Yuck. */ + if (!dir_ok(dest_x, dest_y, punit->goto_dest_x, punit->goto_dest_y, dir)) continue; + if ((map_get_terrain(x, y) != T_OCEAN) && is_enemy_unit_tile(map_get_tile(x, y), owner)) return 0; @@ -628,7 +629,7 @@ enum goto_move_restriction restriction) { struct player *pplayer = unit_owner(punit); - int igter, x, y, x1, y1, dir; + int igter, x, y; int orig_x, orig_y; struct tile *psrctile, *pdesttile; enum unit_move_type move_type = unit_types[punit->type].move_type; @@ -680,15 +681,11 @@ /* Try to move to all tiles adjacent to x,y. The coordinats of the tile we try to move to are x1,y1 */ - for (dir = 0; dir < 8; dir++) { + adjc_dir_iterate(x, y, x1, y1, dir) { if ((restriction == GOTO_MOVE_CARDINAL_ONLY) - && !DIR_IS_CARDINAL(dir)) continue; - - x1 = x + DIR_DX[dir]; - y1 = y + DIR_DY[dir]; - if (!normalize_map_pos(&x1, &y1)) + && !DIR_IS_CARDINAL(dir)) continue; - + pdesttile = map_get_tile(x1, y1); switch (move_type) { @@ -895,7 +892,7 @@ /* Make sure we stop searching when we have no hope of finding a shorter path */ maxcost = total_cost + 1; } - } /* end for (dir = 0; dir < 8; dir++) */ + } adjc_dir_iterate_end; } freelog(LOG_DEBUG, "GOTO: (%d, %d) -> (%d, %d), cost = %d", @@ -916,15 +913,12 @@ if (!get_from_mapqueue(&x, &y)) break; - for (dir = 0; dir < 8; dir++) { + adjc_dir_iterate(x, y, x1, y1, dir) { if ((restriction == GOTO_MOVE_CARDINAL_ONLY) - && !DIR_IS_CARDINAL(dir)) continue; + && !DIR_IS_CARDINAL(dir)) + continue; if (local_vector[x][y] & (1<x, punit->y)->move_cost[k]; else c = 3; if (unit_flag(punit->type, F_IGTER) && c) c = 1; - x = map_adjust_x(punit->x + DIR_DX[k]); y = map_adjust_y(punit->y + DIR_DY[k]); + x = map_adjust_x(punit->x + DIR_DX[k]); + y = map_adjust_y(punit->y + DIR_DY[k]); if (passenger) { freelog(LOG_DEBUG, "%d@(%d,%d) evaluating (%d,%d)[%d/%d]", punit->id, punit->x, punit->y, x, y, c, punit->moves_left); @@ -1027,8 +1022,8 @@ nearland = 0; if (!pplayer->ai.control && !map_get_known(x, y, pplayer)) nearland++; - for (n = 0; n < 8; n++) { - adjtile = map_get_tile(x + DIR_DX[n], y + DIR_DY[n]); + adjc_iterate(x, y, x1, y1) { + adjtile = map_get_tile(x1, y1); if (adjtile->terrain != T_OCEAN) nearland++; if (!((adjtile->known)&(1u<owner))) { if (punit->moves_left <= c) d[k] -= (d[k]/16); /* Avoid the unknown */ @@ -1044,16 +1039,16 @@ } unit_list_iterate_end; } /* end this-tile-is-seen else */ - } /* end tiles-adjacent-to-dest for */ + } adjc_iterate_end; if (unit_flag(punit->type, F_TRIREME) && !nearland) { if (punit->moves_left < 6) d[k] = -1; /* Tired of Kaput!! -- Syela */ else if (punit->moves_left == 6) { - for (n = 0; n < 8; n++) { - if ((warmap.vector[x][y]&(1<x, punit->y, x, y)) return 1; if (is_ground_unit(punit) && (omni || map_get_known_and_seen(x, y, pplayer->player_no))) { if (map_get_terrain(x, y) == T_OCEAN) { if (ground_unit_transporter_capacity(x, y, pplayer) > 0) { - for (k = 0; k < 8; k++) { + adjc_iterate(x, y, x1, y1) { if (map_get_continent(punit->x, punit->y) == - map_get_continent(x + DIR_DX[k], y + DIR_DY[k])) + map_get_continent(x1, y1)) possible++; - } + } adjc_iterate_end; } } else { /* going to a land tile */ if (map_get_continent(punit->x, punit->y) == map_get_continent(x, y)) possible++; else { - for (k = 0; k < 8; k++) { - if (map_get_continent(punit->x + DIR_DX[k], punit->y + DIR_DY[k]) == + adjc_iterate(punit->x, punit->y, x1, y1) { + if (map_get_continent(x1, y1) == map_get_continent(x, y)) possible++; - } + } adjc_iterate_end; } } return(possible); @@ -1598,7 +1593,7 @@ TRYFULL: freelog(LOG_DEBUG, "didn't work. Lets try full"); { - int x1, y1, dir, cost; + int cost; struct unit *penemy; init_gotomap(src_x, src_y, AIR_MOVING); @@ -1609,11 +1604,7 @@ || warmap.cost[dest_x][dest_y] != MAXCOST) /* found route */ break; - for (dir = 0; dir < 8; dir++) { - x1 = x + DIR_DX[dir]; - y1 = y + DIR_DY[dir]; - if (!normalize_map_pos(&x1, &y1)) - continue; + adjc_iterate(x, y, x1, y1) { if (warmap.cost[x1][y1] <= warmap.cost[x][y]) continue; /* No need for all the calculations */ @@ -1626,7 +1617,7 @@ add_to_mapqueue(cost, x1, y1); } } - } /* end for */ + } adjc_iterate_end; } freelog(LOG_DEBUG, "movecost: %i", warmap.cost[dest_x][dest_y]); Index: server/mapgen.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v retrieving revision 1.70 diff -u -r1.70 mapgen.c --- server/mapgen.c 2001/07/28 12:03:29 1.70 +++ server/mapgen.c 2001/08/30 11:07:54 @@ -1356,8 +1356,14 @@ int a; for (y=0;yknown)&(1u<private_map+offset)->seen; + x = map_adjust_x(x); + return (MAP(x,y)->known & (1u<private_map + map_inx(x, y))->seen; } /*************************************************************** @@ -948,12 +948,12 @@ struct player_tile *map_get_player_tile(int x, int y, struct player *pplayer) { - if (y < 0 || y >= map.ysize) { - freelog(LOG_ERROR, "Trying to get nonexistant tile at %i,%i", x, y); - return pplayer->private_map - + map_adjust_x(x) + map_adjust_y(y) * map.xsize; - } else - return pplayer->private_map + map_adjust_x(x) + y * map.xsize; + x = map_adjust_x(x); + if (!is_real_tile(x, y)) { + freelog(LOG_ERROR, "Trying to get nonexistant tile at %i,%i", x,y); + nearest_real_tile(&x, &y); + } + return pplayer->private_map + map_inx(x, y); } /*************************************************************** Index: server/settlers.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v retrieving revision 1.104 diff -u -r1.104 settlers.c --- server/settlers.c 2001/08/26 13:10:13 1.104 +++ server/settlers.c 2001/08/30 11:08:00 @@ -766,8 +766,9 @@ **************************************************************************/ int auto_settler_do_goto(struct player *pplayer, struct unit *punit, int x, int y) { - punit->goto_dest_x=map_adjust_x(x); - punit->goto_dest_y=map_adjust_y(y); + nearest_real_tile(&x, &y); + punit->goto_dest_x = x; + punit->goto_dest_y = y; set_unit_activity(punit, ACTIVITY_GOTO); send_unit_info(0, punit); do_unit_goto(punit, GOTO_MOVE_ANY, 0);