Index: ai/aitools.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v retrieving revision 1.52 diff -u -r1.52 aitools.c --- ai/aitools.c 2002/08/17 09:00:52 1.52 +++ ai/aitools.c 2002/08/17 10:38:49 @@ -71,23 +71,17 @@ assert(punit); if (!is_tiles_adjacent(x, y, bodyguard->x, bodyguard->y)) { - freelog(LOG_BODYGUARD, "%s: %s (%d,%d) is too far from its charge (%d,%d)!", - pplayer->name, unit_type(bodyguard)->name, bodyguard->x, - bodyguard->y, x, y); + BODYGUARD_LOG(LOG_DEBUG, bodyguard, "is too far from its charge"); return; } if (bodyguard->moves_left <= 0) { /* should generally should not happen */ - freelog(LOG_BODYGUARD, "%s: %s left its %s bodyguard behind at (%d,%d)!", - pplayer->name, unit_type(punit)->name, - unit_type(bodyguard)->name, x, y); + BODYGUARD_LOG(LOG_DEBUG, bodyguard, "was left behind by charge"); return; } - freelog(LOG_BODYGUARD, "%s: Dragging %s's bodyguard %s to (%d,%d)", - pplayer->name, unit_type(punit)->name, unit_type(bodyguard)->name, - x, y); + BODYGUARD_LOG(LOG_DEBUG, bodyguard, "was dragged along by charge"); handle_unit_activity_request(bodyguard, ACTIVITY_IDLE); (void) ai_unit_move(bodyguard, x, y); @@ -105,16 +99,13 @@ if (punit->ai.bodyguard > BODYGUARD_NONE) { if ((guard = find_unit_by_id(punit->ai.bodyguard))) { if (guard->ai.charge != punit->id) { - freelog(LOG_BODYGUARD, "%s: %s didn't know it had %s for bodyguard " - "at (%d,%d)!", unit_owner(punit)->name, unit_type(punit)->name, - unit_type(guard)->name, punit->x, punit->y); + BODYGUARD_LOG(LOG_VERBOSE, guard, "my charge didn't know about me!"); } guard->ai.charge = punit->id; /* ensure sanity */ return TRUE; } else { punit->ai.bodyguard = BODYGUARD_NONE; - freelog(LOG_BODYGUARD, "%s: %s's bodyguard has disappeared at (%d,%d)!", - unit_owner(punit)->name, unit_type(punit)->name, punit->x, punit->y); + UNIT_LOG(LOG_VERBOSE, punit, "bodyguard disappeared!"); } } return FALSE; @@ -181,8 +172,7 @@ && (bodyguard = find_unit_by_id(punit->ai.bodyguard)) && same_pos(punit->x, punit->y, bodyguard->x, bodyguard->y) && bodyguard->moves_left == 0) { - freelog(LOG_BODYGUARD, "%s's %s does not want to leave its %s bodyguard.", - pplayer->name, unit_type(punit)->name, unit_type(bodyguard)->name); + UNIT_LOG(LOG_DEBUG, punit, "does not want to leave its bodyguard"); return FALSE; } @@ -191,8 +181,7 @@ && unit_type(punit)->move_rate > map_move_cost(punit, x, y) && enemies_at(punit, x, y) && !enemies_at(punit, punit->x, punit->y)) { - freelog(LOG_DEBUG, "%s's %s ending move early to stay out of trouble.", - pplayer->name, unit_type(punit)->name); + UNIT_LOG(LOG_DEBUG, punit, "ending move early to stay out of trouble"); return FALSE; } Index: ai/aitools.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aitools.h,v retrieving revision 1.21 diff -u -r1.21 aitools.h --- ai/aitools.h 2002/08/17 09:00:52 1.21 +++ ai/aitools.h 2002/08/17 10:38:49 @@ -16,12 +16,51 @@ #include "shared.h" /* bool type */ #include "unit.h" +/* Change these to watch logs from a specific part of the AI code */ +#define LOGLEVEL_BODYGUARD LOG_DEBUG +#define LOGLEVEL_UNIT LOG_DEBUG +#define LOGLEVEL_GOTO LOG_DEBUG + +/* General AI logging macros */ + +#define UNIT_LOG(level, punit, msg) \ + freelog(MIN(LOGLEVEL_UNIT, level), \ + "%s's %s[%d] (%d,%d)->(%d,%d) " ##msg##, \ + unit_owner(punit)->name, unit_type(punit)->name, \ + punit->id, punit->x, punit->y, \ + punit->goto_dest_x, punit->goto_dest_y); + +/* check death first, do NOT pass do_unit_goto as third parameter! */ +#define GOTO_LOG(level, punit, result, msg) \ +{ \ + assert(punit && result != GR_DIED); \ + if (result != GR_OUT_OF_MOVEPOINTS && result != GR_ARRIVED) { \ + freelog(MIN(LOGLEVEL_UNIT, level), \ + "%s's %s[%d] on GOTO (%d,%d)->(%d,%d) %s (" ##msg## ")",\ + unit_owner(punit)->name, unit_type(punit)->name, \ + punit->id, punit->x, punit->y, \ + punit->goto_dest_x, punit->goto_dest_y, \ + (result==GR_FAILED) ? "failed" \ + : (result==GR_FOUGHT) ? "fought" : "ERROR!"); \ + } \ +} + +#define BODYGUARD_LOG(level, punit, msg) \ +{ \ + struct unit *pcharge = find_unit_by_id(punit->ai.charge); \ + freelog(MIN(LOGLEVEL_BODYGUARD, level), \ + "%s's bodyguard %s[%d] (%d,%d)[%d@%d,%d]->(%d,%d) " ##msg##, \ + unit_owner(punit)->name, unit_type(punit)->name, \ + punit->id, punit->x, punit->y, \ + pcharge ? pcharge->id : -1, pcharge ? pcharge->x : -1,\ + pcharge ? pcharge->y : -1, \ + punit->goto_dest_x, punit->goto_dest_y); \ +} + struct ai_choice; struct city; struct government; struct player; - -#define LOG_BODYGUARD LOG_DEBUG enum bodyguard_enum { BODYGUARD_WANTED=-1, Index: ai/aiunit.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v retrieving revision 1.205 diff -u -r1.205 aiunit.c --- ai/aiunit.c 2002/08/17 09:00:52 1.205 +++ ai/aiunit.c 2002/08/17 10:38:50 @@ -222,9 +222,8 @@ default: freelog(LOG_FATAL, "In ai/aiunit.c: function unit_move_turns"); freelog(LOG_FATAL, "Illegal move type %d", unit_type(punit)->move_type); - assert(0); + assert(FALSE); exit(EXIT_FAILURE); - move_time = -1; } return move_time; } @@ -325,14 +324,18 @@ } iterate_outward_end; if (bestcost <= maxmoves * SINGLE_MOVE) { + enum goto_result result; + /* Go there! */ punit->goto_dest_x = best_x; punit->goto_dest_y = best_y; set_unit_activity(punit, ACTIVITY_GOTO); - if (do_unit_goto(punit, GOTO_MOVE_ANY, FALSE) == GR_DIED) { + result = do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + if (result == GR_DIED) { /* We're dead. */ return FALSE; } + GOTO_LOG(LOG_DEBUG, punit, result, "exploring huts"); /* TODO: Take more advantage from the do_unit_goto() return value. */ @@ -500,14 +503,18 @@ } whole_map_iterate_end; if (most_unknown > 0) { + enum goto_result result; + /* Go there! */ - punit->goto_dest_x = best_x; punit->goto_dest_y = best_y; handle_unit_activity_request(punit, ACTIVITY_GOTO); - do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + result = do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + if (result == GR_DIED) { + return FALSE; + } + GOTO_LOG(LOG_DEBUG, punit, result, "exploring territory"); - /* FIXME? Why we don't test if the unit is still alive? */ /* TODO: Take more advantage from the do_unit_goto() return value. */ if (punit->moves_left > 0) { @@ -532,9 +539,7 @@ } /* We have nothing to explore, so we can go idle. */ - - freelog(LOG_DEBUG, "%s's %s at (%d,%d) failed to explore.", - pplayer->name, unit_type(punit)->name, punit->x, punit->y); + UNIT_LOG(LOG_DEBUG, punit, "failed to explore more"); handle_unit_activity_request(punit, ACTIVITY_IDLE); /* @@ -543,21 +548,41 @@ * --pasky), we will return to our homecity, maybe even to another continent. */ - if (pplayer->ai.control && is_military_unit(punit)) { + if (pplayer->ai.control) { /* Unit's homecity */ struct city *pcity = find_city_by_id(punit->homecity); - if (pcity) { + /* No homecity? Find one! */ + if (!pcity) { + pcity = dist_nearest_city(pplayer, punit->x, punit->y, FALSE, FALSE); + if (pcity) { + struct packet_unit_request packet; + packet.unit_id = punit->id; + packet.city_id = pcity->id; + packet.x = punit->x; + packet.y = punit->y; + packet.name[0] = '\0'; + handle_unit_change_homecity(pplayer, &packet); + } + } + + if (pcity && !same_pos(punit->x, punit->y, pcity->x, pcity->y)) { if (map_get_continent(pcity->x, pcity->y) == continent) { + UNIT_LOG(LOG_DEBUG, punit, "sending explorer home by foot"); ai_military_gohome(pplayer, punit); } else { /* Sea travel */ if (find_boat(pplayer, &x, &y, 0) == 0) { punit->ai.ferryboat = -1; } else { + enum goto_result result; + UNIT_LOG(LOG_DEBUG, punit, "sending explorer home by boat"); punit->goto_dest_x = x; punit->goto_dest_y = y; - do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + result = do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + if (result != GR_DIED) { + GOTO_LOG(LOG_DEBUG, punit, result, "explorer sail home"); + } } } } @@ -991,11 +1016,9 @@ get_total_attack_power(patt, punit) * get_total_attack_power(punit, pdef) && stack_size < 2 && get_total_attack_power(patt, punit) > 0) { - freelog(LOG_DEBUG, "%s defending %s from %s's %s", - unit_type(punit)->name, - map_get_city(x, y)->name, - unit_owner(pdef)->name, unit_type(pdef)->name); - + freelog(LOG_DEBUG, "%s's %s defending %s from %s's %s at (%d, %d)", + pplayer->name, unit_type(punit)->name, map_get_city(x, y)->name, + unit_owner(pdef)->name, unit_type(pdef)->name, punit->x, punit->y); } else { /* See description of kill_desire() about this variables. */ int vuln = unit_vulnerability(punit, pdef); @@ -1100,7 +1123,7 @@ } if (aunit) { - freelog(LOG_DEBUG, "%s#%d@(%d,%d) -> %s#%d@(%d,%d) [body=%d]", + freelog(LOG_DEBUG, "%s#%d@(%d,%d) to meet charge %s#%d@(%d,%d)[body=%d]", unit_type(punit)->name, punit->id, punit->x, punit->y, unit_type(aunit)->name, aunit->id, aunit->x, aunit->y, aunit->ai.bodyguard); @@ -1108,9 +1131,13 @@ if (!same_pos(punit->x, punit->y, x, y)) { if (goto_is_sane(punit, x, y, TRUE)) { + enum goto_result result; punit->goto_dest_x = x; punit->goto_dest_y = y; - do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + result = do_unit_goto(punit, GOTO_MOVE_ANY, FALSE); + if (result != GR_DIED) { + GOTO_LOG(LOGLEVEL_BODYGUARD, punit, result, "bodyguard meet"); + } } else { /* can't possibly get there to help */ ai_unit_new_role(punit, AIUNIT_NONE); @@ -1432,7 +1459,7 @@ } } city_list_iterate_end; - freelog(LOG_BODYGUARD, "%s: %s (%d@%d,%d) looking for charge; %d/%d", + freelog(LOG_DEBUG, "%s: %s (%d@%d,%d) looking for charge; %d/%d", pplayer->name, unit_type(punit)->name, punit->id, punit->x, punit->y, best, best * 100 / toughness); @@ -1508,6 +1535,7 @@ /* ok, what if I'm somewhere new? - ugly, kludgy code by Syela */ if (stay_and_defend_city(punit)) { + UNIT_LOG(LOG_DEBUG, punit, "stays to defend city"); return; } @@ -2260,7 +2288,7 @@ ai_manage_explorer(punit); break; default: - abort(); + assert(FALSE); } if ((punit = find_unit_by_id(id))) { Index: server/gotohand.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/gotohand.c,v retrieving revision 1.148 diff -u -r1.148 gotohand.c --- server/gotohand.c 2002/08/17 08:37:40 1.148 +++ server/gotohand.c 2002/08/17 10:38:51 @@ -1364,7 +1364,6 @@ } if(punit->x!=x || punit->y!=y) { - freelog(LOG_DEBUG, "Aborting, out of movepoints."); send_unit_info(NULL, punit); return GR_OUT_OF_MOVEPOINTS; }