diff -Nur -X/home/thue/freeciv-dev/freeciv/diff_ignore freeciv/ai/aiunit.c workdir/ai/aiunit.c --- freeciv/ai/aiunit.c Fri Jun 23 17:19:07 2000 +++ workdir/ai/aiunit.c Fri Jun 23 19:20:45 2000 @@ -62,8 +62,6 @@ static int could_be_my_zoc(struct unit *myunit, int x0, int y0) { /* Fix to bizarre did-not-find bug. Thanks, Katvrr -- Syela */ - int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int ax, ay, k; int owner=myunit->owner; @@ -76,8 +74,8 @@ return 0; for (k = 0; k < 8; k++) { - ax = map_adjust_x(myunit->x + ii[k]); - ay = map_adjust_y(myunit->y + jj[k]); + ax = map_adjust_x(myunit->x + DIR_DX[k]); + ay = map_adjust_y(myunit->y + DIR_DY[k]); if (map_get_terrain(ax,ay)!=T_OCEAN && is_non_allied_unit_tile(map_get_tile(ax,ay),owner)) @@ -201,15 +199,22 @@ **************************************************************************/ static int tile_is_accessible(struct unit *punit, int x, int y) { - int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; - int k; + int dir, x1, y1; if (unit_really_ignores_zoc(punit)) return 1; if (is_my_zoc(punit, x, y)) return 1; - for (k = 0; k < 8; k++) - if (map_get_terrain(x+ii[k], y+jj[k]) != T_OCEAN - && is_my_zoc(punit, x + ii[k], y + jj[k])) return 1; + + for (dir = 0; dir < 8; dir++) { + x1 = x + DIR_DX[dir]; + y1 = y + DIR_DY[dir]; + if (!normalize_map_pos(&x1, &y1)) + continue; + + if (map_get_terrain(x1, y1) != T_OCEAN + && is_my_zoc(punit, x1, y1)) + return 1; + } + return 0; } @@ -562,6 +567,9 @@ } #endif +/************************************************************************* +... +**************************************************************************/ static int is_my_turn(struct unit *punit, struct unit *pdef) { int val = unit_belligerence_primitive(punit), i, j, cur, d; @@ -592,15 +600,13 @@ 3. Enemy units weaker than the unit 4. Land barbarians also like unfrastructure tiles (for later pillage) If none of the following is there, nothing is chosen. -**************************************************************************/ -/* work of Syela - mostly to fix the ZOC/goto strangeness */ +work of Syela - mostly to fix the ZOC/goto strangeness +**************************************************************************/ static int ai_military_findvictim(struct player *pplayer, struct unit *punit, int *dest_x, int *dest_y) { - int xx[3], yy[3], x, y, x1, y1, k; - int ii[8] = { 0, 1, 2, 0, 2, 0, 1, 2 }; - int jj[8] = { 0, 0, 0, 1, 1, 2, 2, 2 }; + int x, y, x1, y1, k; int best = 0, a, b, c, d, e, f; struct unit *pdef; struct unit *patt; @@ -619,11 +625,12 @@ unit_list_iterate_end; } /* ferryboats do not attack. no. -- Syela */ - map_calc_adjacent_xy(x, y, xx, yy); - for (k = 0; k < 8; k++) { - x1 = xx[ii[k]]; - y1 = yy[jj[k]]; + x1 = x + DIR_DX[k]; + y1 = y + DIR_DY[k]; + if (!normalize_map_pos(&x1, &y1)) + continue; + pdef = get_defender(pplayer, punit, x1, y1); if (pdef) { patt = get_attacker(pplayer, punit, x1, y1); @@ -693,6 +700,9 @@ return(best); } +/************************************************************************* +... +**************************************************************************/ static void ai_military_bodyguard(struct player *pplayer, struct unit *punit) { struct unit *aunit = player_find_unit_by_id(pplayer, punit->ai.charge); @@ -732,32 +742,36 @@ aunit->ai.bodyguard = id; } +/************************************************************************* +... +**************************************************************************/ int find_beachhead(struct unit *punit, int dest_x, int dest_y, int *x, int *y) { - int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; - int i, j, k, l, ok, t, fu, best = 0; + int x1, y1, dir, l, ok, t, fu, best = 0; + + for (dir = 0; dir < 8; dir++) { + x1 = dest_x + DIR_DX[dir]; + y1 = dest_y + DIR_DY[dir]; + if (!normalize_map_pos(&x1, &y1)) + continue; - for (k = 0; k < 8; k++) { - i = map_adjust_x(dest_x + ii[k]); - j = map_adjust_y(dest_y + jj[k]); ok = 0; fu = 0; - t = map_get_terrain(i, j); - if (warmap.seacost[i][j] <= 6 * THRESHOLD && t != T_OCEAN) { /* accessible beachhead */ + t = map_get_terrain(x1, y1); + if (warmap.seacost[x1][y1] <= 6 * THRESHOLD && t != T_OCEAN) { /* accessible beachhead */ for (l = 0; l < 8 && !ok; l++) { - if (map_get_terrain(i + ii[l], j + jj[l]) == T_OCEAN) { + if (map_get_terrain(x1 + DIR_DX[l], y1 + DIR_DY[l]) == T_OCEAN) { fu++; - if (is_my_zoc(punit, i + ii[l], j + jj[l])) ok++; + if (is_my_zoc(punit, x1 + DIR_DX[l], y1 + DIR_DY[l])) ok++; } } if (ok) { /* accessible beachhead with zoc-ok water tile nearby */ ok = get_tile_type(t)->defense_bonus; - if (map_get_special(i, j) & S_RIVER) + if (map_get_special(x1, y1) & S_RIVER) ok += (ok * terrain_control.river_defense_bonus) / 100; if (get_tile_type(t)->movement_cost * 3 < unit_types[punit->type].move_rate) ok *= 8; - ok += (6 * THRESHOLD - warmap.seacost[i][j]); - if (ok > best) { best = ok; *x = i; *y = j; } + ok += (6 * THRESHOLD - warmap.seacost[x1][y1]); + if (ok > best) { best = ok; *x = x1; *y = y1; } } } } @@ -795,7 +809,6 @@ /************************************************************************** ... **************************************************************************/ - static int ai_military_gothere(struct player *pplayer, struct unit *punit, int dest_x, int dest_y) { @@ -944,6 +957,9 @@ return(-1); /* died */ } +/************************************************************************* +... +**************************************************************************/ int unit_can_defend(int type) { if (unit_types[type].move_type != LAND_MOVING) return 0; /* temporary kluge */ @@ -967,6 +983,9 @@ } #endif +/************************************************************************* +... +**************************************************************************/ int look_for_charge(struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity) { int d, def, val = 0, u; @@ -1142,6 +1161,9 @@ } } +/************************************************************************* +... +**************************************************************************/ int find_something_to_kill(struct player *pplayer, struct unit *punit, int *x, int *y) { int a=0, b, c, d, e, m, n, v, i, f, a0, b0, ab, g; @@ -1452,7 +1474,6 @@ If no enemies nearby find_something_to_kill() anywhere else. If there is nothing to kill, sailing units go home, others explore. **************************************************************************/ - void ai_military_attack(struct player *pplayer,struct unit *punit) { /* rewritten by Syela - old way was crashy and not smart (nor is this) */ int dest_x, dest_y; @@ -1704,7 +1725,6 @@ /************************************************************************** decides what to do with a military unit. **************************************************************************/ - void ai_manage_military(struct player *pplayer,struct unit *punit) { int id; @@ -1780,7 +1800,6 @@ they are not in cities, and they are far from any enemy units. It is to remove barbarians that do not engage into any activity for a long time. **************************************************************************/ - static int unit_can_be_retired(struct unit *punit) { int x, y, x1; @@ -1811,7 +1830,6 @@ manage one unit Careful: punit may have been destroyed upon return from this routine! **************************************************************************/ - static void ai_manage_unit(struct player *pplayer, struct unit *punit) { /* retire useless barbarian units here, before calling the management @@ -1866,7 +1884,6 @@ /************************************************************************** do all the gritty nitty chess like analysis here... (argh) **************************************************************************/ - void ai_manage_units(struct player *pplayer) { static struct timer *t = NULL; /* alloc once, never free */ @@ -2141,7 +2158,6 @@ Barbarian leader tries to stack with other barbarian units, and if it's not possible it runs away. When on coast, it may disappear with 33% chance. **************************************************************************/ - static void ai_manage_barbarian_leader(struct player *pplayer, struct unit *leader) { int con = map_get_continent(leader->x, leader->y), i, x, y, dx, dy, diff -Nur -X/home/thue/freeciv-dev/freeciv/diff_ignore freeciv/common/map.c workdir/common/map.c --- freeciv/common/map.c Fri Jun 23 17:19:28 2000 +++ workdir/common/map.c Fri Jun 23 19:19:59 2000 @@ -825,37 +825,6 @@ } /*************************************************************** - (x,y) are map coords, assumed to be already adjusted; - we calculate elements of xx[3], yy[3] for adjusted - coordinates of adjacent tiles, offsets [0]=-1, [1]=0, [2]=+1 - Here we adjust yy[] values to stay inside [0,map.ysize-1] -***************************************************************/ -void map_calc_adjacent_xy(int x, int y, int *xx, int *yy) -{ - if((xx[2]=x+1)==map.xsize) xx[2]=0; - if((xx[0]=x-1)==-1) xx[0]=map.xsize-1; - xx[1] = x; - if ((yy[0]=y-1)==-1) yy[0] = 0; - if ((yy[2]=y+1)==map.ysize) yy[2]=y; - yy[1] = y; -} - -/*************************************************************** - Like map_calc_adjacent_xy(), except don't adjust the yy[] values. - Note that if y is out of range, then map_get_tile(x,y) returns - &void_tile, hence the _void in the name of this function. -***************************************************************/ -void map_calc_adjacent_xy_void(int x, int y, int *xx, int *yy) -{ - if((xx[2]=x+1)==map.xsize) xx[2]=0; - if((xx[0]=x-1)==-1) xx[0]=map.xsize-1; - xx[1] = x; - yy[0] = y - 1; - yy[1] = y; - yy[2] = y + 1; -} - -/*************************************************************** The basic cost to move punit from tile t1 to tile t2. That is, tile_move_cost(), with pre-calculated tile pointers; the tiles are assumed to be adjacent, and the (x,y) @@ -948,26 +917,23 @@ ***************************************************************/ void reset_move_costs(int x, int y) { - int k, x1, y1, xx[3], yy[3]; - int ii[8] = { 0, 1, 2, 0, 2, 0, 1, 2 }; - int jj[8] = { 0, 0, 0, 1, 1, 2, 2, 2 }; + int dir, x1, y1; int maxcost = 72; /* should be big enough without being TOO big */ struct tile *tile0, *tile1; x = map_adjust_x(x); tile0 = map_get_tile(x, y); - map_calc_adjacent_xy_void(x, y, xx, yy); debug_log_move_costs("Resetting move costs for", x, y, tile0); - for (k = 0; k < 8; k++) { - x1 = xx[ii[k]]; - y1 = yy[jj[k]]; + for (dir = 0; dir < 8; dir++) { + x1 = map_adjust_x(x + DIR_DX[dir]); + y1 = y + DIR_DY[dir]; tile1 = map_get_tile(x1, y1); - tile0->move_cost[k] = tile_move_cost_ai(tile0, tile1, x, y, - x1, y1, maxcost); + tile0->move_cost[dir] = tile_move_cost_ai(tile0, tile1, x, y, + x1, y1, maxcost); /* reverse: not at all obfuscated now --dwp */ /* this might muck with void_tile, but who cares? */ - tile1->move_cost[7 - k] = tile_move_cost_ai(tile1, tile0, x1, y1, + tile1->move_cost[7 - dir] = tile_move_cost_ai(tile1, tile0, x1, y1, x, y, maxcost); } debug_log_move_costs("Reset move costs for", x, y, tile0); @@ -980,9 +946,7 @@ ***************************************************************/ void initialize_move_costs(void) { - int x, y, x1, y1, k, xx[3], yy[3]; - int ii[8] = { 0, 1, 2, 0, 2, 0, 1, 2 }; - int jj[8] = { 0, 0, 0, 1, 1, 2, 2, 2 }; + int x, y, x1, y1, k; int maxcost = 72; /* should be big enough without being TOO big */ struct tile *tile0, *tile1; @@ -993,10 +957,9 @@ for (x = 0; x < map.xsize; x++) { for (y = 0; y < map.ysize; y++) { tile0 = map_get_tile(x, y); - map_calc_adjacent_xy_void(x, y, xx, yy); for (k = 0; k < 8; k++) { - x1 = xx[ii[k]]; - y1 = yy[jj[k]]; + x1 = map_adjust_x(x + DIR_DX[k]); + y1 = y + DIR_DY[k]; tile1 = map_get_tile(x1, y1); tile0->move_cost[k] = tile_move_cost_ai(tile0, tile1, x, y, x1, y1, maxcost); diff -Nur -X/home/thue/freeciv-dev/freeciv/diff_ignore freeciv/common/map.h workdir/common/map.h --- freeciv/common/map.h Fri Jun 23 17:19:28 2000 +++ workdir/common/map.h Fri Jun 23 18:57:07 2000 @@ -178,9 +178,6 @@ #define map_inx(x,y) \ ((x)+(y)*map.xsize) -void map_calc_adjacent_xy(int x, int y, int *xx, int *yy); -void map_calc_adjacent_xy_void(int x, int y, int *xx, int *yy); - struct city *map_get_city(int x, int y); void map_set_city(int x, int y, struct city *pcity); enum tile_terrain_type map_get_terrain(int x, int y); @@ -325,6 +322,21 @@ } \ } +/* +using +x1 = x + DIR_DX[dir]; +y1 = y + DIR_DX[dir]; +will give you the tile as shown below. +------- +|0|1|2| +|-+-+-| +|3| |4| +|-+-+-| +|5|6|7| +------- + */ +static const int DIR_DX[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; +static const int DIR_DY[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; #define MAP_DEFAULT_HUTS 50 #define MAP_MIN_HUTS 0 diff -Nur -X/home/thue/freeciv-dev/freeciv/diff_ignore freeciv/server/gotohand.c workdir/server/gotohand.c --- freeciv/server/gotohand.c Fri Jun 23 17:19:36 2000 +++ workdir/server/gotohand.c Fri Jun 23 18:27:53 2000 @@ -167,8 +167,6 @@ int x, y, move_cost, dir, x1, y1; int orig_x, orig_y; int igter; - static const int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - static const int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int maxcost = THRESHOLD * 6 + 2; /* should be big enough without being TOO big */ struct tile *ptile; struct player *pplayer; @@ -206,8 +204,8 @@ warnodes++; ptile = map_get_tile(x, y); for (dir = 0; dir < 8; dir++) { - x1 = x + ii[dir]; - y1 = y + jj[dir]; + x1 = x + DIR_DX[dir]; + y1 = y + DIR_DY[dir]; if (!normalize_map_pos(&x1, &y1)) continue; @@ -345,7 +343,7 @@ /************************************************************************** Returns false if you are going the in opposite direction of the destination. The 3 directions most opposite the one to the target is considered wrong. -"dir" is the direction you get if you use x = ii[dir], y = jj[dir] +"dir" is the direction you get if you use x = DIR_DX[dir], y = DIR_DY[dir] **************************************************************************/ static int dir_ok(int src_x, int src_y, int dest_x, int dest_y, int dir) { @@ -396,7 +394,7 @@ /************************************************************************** Return the direction that takes us most directly to dest_x,dest_y. -The direction is a value for use in ii[] and jj[] arrays. +The direction is a value for use in DIR_DX[] and DIR_DY[] arrays. FIXME: This is a bit crude; this only gives one correct path, but sometimes there can be more than one straight path (fx going NW then W is the @@ -464,15 +462,13 @@ return 0; { - static const int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - static const int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int dir; int x, y; int owner = punit->owner; for (dir = 0; dir < 8; dir++) { - x = map_adjust_x(src_x + ii[dir]); - y = map_adjust_y(src_y + jj[dir]); + x = map_adjust_x(src_x + DIR_DX[dir]); + y = map_adjust_y(src_y + DIR_DY[dir]); if (!dir_ok(dest_x, dest_y, punit->goto_dest_x, punit->goto_dest_y, dir)) continue; @@ -546,8 +542,6 @@ enum goto_move_restriction restriction) { static const char *d[] = { "NW", "N", "NE", "W", "E", "SW", "S", "SE" }; - static const int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - static const int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int igter, x, y, x1, y1, dir; int orig_x, orig_y; struct tile *psrctile, *pdesttile; @@ -605,8 +599,8 @@ /* if the direction is N, S, E or W d[dir][1] is the /0 character... */ if ((restriction == GOTO_MOVE_CARDINAL_ONLY) && d[dir][1]) continue; - x1 = x + ii[dir]; - y1 = y + jj[dir]; + x1 = x + DIR_DX[dir]; + y1 = y + DIR_DY[dir]; if (!normalize_map_pos(&x1, &y1)) continue; @@ -823,8 +817,8 @@ 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 + ii[k]); y = map_adjust_y(punit->y + jj[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); @@ -917,7 +909,7 @@ nearland = 0; if (!pplayer->ai.control && !map_get_known(x, y, pplayer)) nearland++; for (n = 0; n < 8; n++) { - adjtile = map_get_tile(x + ii[n], y + jj[n]); + adjtile = map_get_tile(x + DIR_DX[n], y + DIR_DY[n]); if (adjtile->terrain != T_OCEAN) nearland++; if (!((adjtile->known)&(1u<owner))) { if (punit->moves_left <= c) d[k] -= (d[k]/16); /* Avoid the unknown */ @@ -940,7 +932,7 @@ 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) && @@ -984,7 +974,7 @@ if (ground_unit_transporter_capacity(x, y, pplayer->player_no) > 0) { for (k = 0; k < 8; k++) { if (map_get_continent(punit->x, punit->y) == - map_get_continent(x + ii[k], y + jj[k])) + map_get_continent(x + DIR_DX[k], y + DIR_DY[k])) possible++; } } @@ -994,7 +984,7 @@ possible++; else { for (k = 0; k < 8; k++) { - if (map_get_continent(punit->x + ii[k], punit->y + jj[k]) == + if (map_get_continent(punit->x + DIR_DX[k], punit->y + DIR_DY[k]) == map_get_continent(x, y)) possible++; } @@ -1026,8 +1016,6 @@ enum goto_move_restriction restriction) { int x, y, dir; - static const int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - static const int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; static const char *d[] = { "NW", "N", "NE", "W", "E", "SW", "S", "SE" }; int unit_id, dest_x, dest_y, waypoint_x, waypoint_y; struct unit *penemy = NULL; @@ -1077,8 +1065,8 @@ } freelog(LOG_DEBUG, "Going %s", d[dir]); - x = map_adjust_x(punit->x + ii[dir]); - y = punit->y + jj[dir]; /* no need to adjust this */ + x = map_adjust_x(punit->x + DIR_DX[dir]); + y = punit->y + DIR_DY[dir]; /* no need to adjust this */ penemy = is_enemy_unit_tile(map_get_tile(x, y), punit->owner); if (!punit->moves_left) @@ -1520,8 +1508,6 @@ TRYFULL: freelog(LOG_DEBUG, "didn't work. Lets try full"); { - static const int ii[8] = { -1, 0, 1, -1, 1, -1, 0, 1 }; - static const int jj[8] = { -1, -1, -1, 0, 0, 1, 1, 1 }; int x1, y1, dir; struct unit *penemy; @@ -1535,8 +1521,8 @@ warnodes++; for (dir = 0; dir < 8; dir++) { - y1 = y + jj[dir]; - x1 = x + ii[dir]; + x1 = x + DIR_DX[dir]; + y1 = y + DIR_DY[dir]; if (!normalize_map_pos(&x1, &y1)) continue; if (warmap.cost[x1][y1] <= warmap.cost[x][y])