Index: ai/aitools.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aitools.c,v retrieving revision 1.57 diff -u -r1.57 aitools.c --- ai/aitools.c 2002/09/28 21:58:15 1.57 +++ ai/aitools.c 2002/10/31 13:20:21 @@ -29,6 +29,7 @@ #include "cityturn.h" #include "maphand.h" #include "plrhand.h" +#include "settlers.h" #include "unittools.h" #include "aicity.h" @@ -38,17 +39,33 @@ #include "aitools.h" /************************************************************************** - Ensure unit sanity + Ensure unit sanity by telling charge that we won't bodyguard it anymore, + add and remove city spot reservation, and set destination. **************************************************************************/ -void ai_unit_new_role(struct unit *punit, enum ai_unit_task task) +void ai_unit_new_role(struct unit *punit, enum ai_unit_task task, int x, int y) { struct unit *charge = find_unit_by_id(punit->ai.charge); + + if (punit->ai.ai_role == AIUNIT_BUILD_CITY) { + assert(is_normal_map_pos(punit->goto_dest_x, punit->goto_dest_y)); + remove_city_from_minimap(punit->goto_dest_x, punit->goto_dest_y); + } + if (charge && (charge->ai.bodyguard == punit->id)) { /* ensure we don't let the unit believe we bodyguard it */ - charge->ai.bodyguard = BODYGUARD_NONE; + charge->ai.bodyguard = BODYGUARD_NONE; } punit->ai.charge = BODYGUARD_NONE; + punit->ai.ai_role = task; +/* TODO: + punit->goto_dest_x = x; + punit->goto_dest_y = y; */ + + if (punit->ai.ai_role == AIUNIT_BUILD_CITY) { + assert(is_normal_map_pos(x, y)); + add_city_to_minimap(x, y); + } } /************************************************************************** Index: ai/aitools.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aitools.h,v retrieving revision 1.25 diff -u -r1.25 aitools.h --- ai/aitools.h 2002/09/28 21:58:15 1.25 +++ ai/aitools.h 2002/10/31 13:20:21 @@ -26,7 +26,7 @@ BODYGUARD_NONE }; -void ai_unit_new_role(struct unit *punit, enum ai_unit_task utask); +void ai_unit_new_role(struct unit *punit, enum ai_unit_task task, int x, int y); bool ai_unit_make_homecity(struct unit *punit, struct city *pcity); void ai_unit_attack(struct unit *punit, int x, int y); bool ai_unit_move(struct unit *punit, int x, int y); Index: ai/aiunit.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v retrieving revision 1.222 diff -u -r1.222 aiunit.c --- ai/aiunit.c 2002/10/28 17:06:12 1.222 +++ ai/aiunit.c 2002/10/31 13:20:21 @@ -647,7 +647,7 @@ /* change homecity to this city */ if (ai_unit_make_homecity(punit, pcity)) { /* Very important, or will not stay -- Syela */ - ai_unit_new_role(punit, AIUNIT_DEFEND_HOME); + ai_unit_new_role(punit, AIUNIT_DEFEND_HOME, -1, -1); return TRUE; } } @@ -1093,7 +1093,7 @@ y = acity->y; } else { /* should be impossible */ - ai_unit_new_role(punit, AIUNIT_NONE); + ai_unit_new_role(punit, AIUNIT_NONE, -1 , -1); return; } @@ -1115,7 +1115,7 @@ } } else { /* can't possibly get there to help */ - ai_unit_new_role(punit, AIUNIT_NONE); + ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); } } else { /* I had these guys set to just fortify, which is so dumb. -- Syela */ @@ -1352,9 +1352,6 @@ /* Dead unit shouldn't reach this point */ assert(player_find_unit_by_id(pplayer, id) != NULL); - /* in case we need to change */ - punit->ai.ai_role = AIUNIT_NONE; /* this can't be right -- Per */ - if (!same_pos(punit->x, punit->y, x, y)) { return 1; /* moved */ } else { @@ -1481,9 +1478,9 @@ if (can_unit_do_activity(punit, ACTIVITY_PILLAGE) && is_land_barbarian(pplayer)) /* land barbarians pillage */ - ai_unit_new_role(punit, AIUNIT_PILLAGE); + ai_unit_new_role(punit, AIUNIT_PILLAGE, -1, -1); else - ai_unit_new_role(punit, AIUNIT_ATTACK); + ai_unit_new_role(punit, AIUNIT_ATTACK, -1, -1); return; } @@ -1500,7 +1497,7 @@ punit->ai.ai_role = AIUNIT_ESCORT; /* do not use ai_unit_new_role() */ return; } else { - ai_unit_new_role(punit, AIUNIT_NONE); + ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); } } @@ -1512,7 +1509,7 @@ if (q > 0) { if (pcity->ai.urgency != 0) { - ai_unit_new_role(punit, AIUNIT_DEFEND_HOME); + ai_unit_new_role(punit, AIUNIT_DEFEND_HOME, -1, -1); return; } } @@ -1538,27 +1535,27 @@ } if (q > val && ai_fuzzy(pplayer, TRUE)) { - ai_unit_new_role(punit, AIUNIT_DEFEND_HOME); + ai_unit_new_role(punit, AIUNIT_DEFEND_HOME, -1, -1); return; } /* this is bad; riflemen might rather attack if val is low -- Syela */ if (acity) { - ai_unit_new_role(punit, AIUNIT_ESCORT); + ai_unit_new_role(punit, AIUNIT_ESCORT, acity->x, acity->y); punit->ai.charge = acity->id; freelog(LOG_DEBUG, "%s@(%d, %d) going to defend %s@(%d, %d)", unit_type(punit)->name, punit->x, punit->y, acity->name, acity->x, acity->y); } else if (aunit) { - ai_unit_new_role(punit, AIUNIT_ESCORT); + ai_unit_new_role(punit, AIUNIT_ESCORT, aunit->x, aunit->y); punit->ai.charge = aunit->id; freelog(LOG_DEBUG, "%s@(%d, %d) going to defend %s@(%d, %d)", unit_type(punit)->name, punit->x, punit->y, unit_type(aunit)->name, aunit->x, aunit->y); } else if (ai_unit_attack_desirability(punit->type) != 0 || (pcity && !same_pos(pcity->x, pcity->y, punit->x, punit->y))) - ai_unit_new_role(punit, AIUNIT_ATTACK); + ai_unit_new_role(punit, AIUNIT_ATTACK, -1, -1); else { - ai_unit_new_role(punit, AIUNIT_DEFEND_HOME); /* for default */ + ai_unit_new_role(punit, AIUNIT_DEFEND_HOME, -1, -1); /* for default */ } } @@ -1574,7 +1571,7 @@ punit->id,punit->x,punit->y,pcity->x,pcity->y); if (same_pos(punit->x, punit->y, pcity->x, pcity->y)) { freelog(LOG_DEBUG, "INHOUSE. GOTO AI_NONE(%d)", punit->id); - ai_unit_new_role(punit, AIUNIT_NONE); + ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); /* aggro defense goes here -- Syela */ punit = ai_military_rampage(punit, 2); /* 2 is better than pillage */ } else { @@ -2255,7 +2252,6 @@ if (punit->activity != ACTIVITY_IDLE) handle_unit_activity_request(punit, ACTIVITY_IDLE); - punit->ai.ai_role = AIUNIT_NONE; /* this can't be right -- Per */ /* was getting a bad bug where a settlers caused a defender to leave home */ /* and then all other supported units went on DEFEND_HOME/goto */ ai_military_findjob(pplayer, punit); @@ -2274,7 +2270,7 @@ switch (punit->ai.ai_role) { case AIUNIT_AUTO_SETTLER: case AIUNIT_BUILD_CITY: - ai_unit_new_role(punit, AIUNIT_NONE); + ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); break; case AIUNIT_DEFEND_HOME: ai_military_gohome(pplayer, punit); Index: server/settlers.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v retrieving revision 1.147 diff -u -r1.147 settlers.c --- server/settlers.c 2002/10/30 21:19:41 1.147 +++ server/settlers.c 2002/10/31 13:20:22 @@ -33,6 +33,7 @@ #include "aicity.h" #include "aiunit.h" #include "aidata.h" +#include "aitools.h" #include "settlers.h" @@ -427,7 +428,7 @@ punit->ai.control = TRUE; /* if BUILD_CITY must remain BUILD_CITY, otherwise turn into autosettler */ if (punit->ai.ai_role == AIUNIT_NONE) { - punit->ai.ai_role = AIUNIT_AUTO_SETTLER; + ai_unit_new_role(punit, AIUNIT_AUTO_SETTLER, -1, -1); } return; } @@ -915,10 +916,9 @@ generate_warmap(mycity, punit); - if (punit->ai.ai_role == AIUNIT_BUILD_CITY) { - remove_city_from_minimap(punit->goto_dest_x, punit->goto_dest_y); - } - punit->ai.ai_role = AIUNIT_AUTO_SETTLER; /* here and not before! -- Syela */ + /* A default, and also resets minimap if we reserved it previously */ + ai_unit_new_role(punit, AIUNIT_AUTO_SETTLER, -1, -1); + /* hope 11 is far enough -- Syela */ square_iterate(punit->x, punit->y, 11, x, y) { int near = real_map_distance(punit->x, punit->y, x, y); @@ -971,7 +971,7 @@ build a city at Y */ moves *= 2; /* and then build its NEXT city halfway between X and Y. -- Syela */ - b = city_desirability(pplayer, x, y) * (ai_fuzzy(pplayer, TRUE) ? 1 : 0); + b = city_desirability(pplayer, x, y); newv = amortize(b, moves); b = (food_upkeep * FOOD_WEIGHTING) * MORT; @@ -1244,10 +1244,6 @@ int best_act = 0; /* ACTIVITY_ of best target so far */ struct unit *ferryboat = NULL; /* if non-null, boatid boat at unit's x,y */ -#ifdef DEBUG - int save_newv; -#endif - /* First find the best square to upgrade, * results in: gx, gy, best_newv, best_act */ @@ -1257,16 +1253,11 @@ /* Found the best square to upgrade, have gx, gy, best_newv, best_act */ -#ifdef DEBUG - save_newv = best_newv; -#endif - /* Decide whether to build a new city: * if so, modify: gx, gy, best_newv, best_act */ if (unit_flag(punit, F_CITIES) && - pplayer->ai.control && - ai_fuzzy(pplayer, TRUE)) { /* don't want to make cities otherwise */ + pplayer->ai.control) { /* don't want to make cities otherwise */ int nx, ny; int want = evaluate_city_building(punit, &nx, &ny, &ferryboat); @@ -1278,45 +1269,30 @@ } } -#ifdef DEBUG - if ((best_newv != save_newv) || - (map_get_terrain(punit->x, punit->y) == T_OCEAN)) { - freelog(LOG_DEBUG, - "%s %d@(%d,%d) wants to %s at (%d,%d) with desire %d", - unit_name(punit->type), punit->id, punit->x, punit->y, - get_activity_text(best_act), gx, gy, best_newv); - } -#endif - /* Mark the square as taken. */ if (gx!=-1 && gy!=-1) { map_get_tile(gx, gy)->assigned = map_get_tile(gx, gy)->assigned | 1<player_no; - } else if (pplayer->ai.control) { /* settler has no purpose */ - /* possibly add Gilligan's Island here */ - ; + } else { + /* This line makes non-AI autosettlers go off auto when they run + out of squares to improve. I would like keep them on, prepared for + future pollution and warming, but there wasn't consensus to do so. */ + punit->ai.control = FALSE; + return; } /* If we intent on building a city then reserve the square. */ if (unit_flag(punit, F_CITIES) && best_act == ACTIVITY_UNKNOWN /* flag */) { - punit->ai.ai_role = AIUNIT_BUILD_CITY; - /* FIXME: is the unit taken off the minimap if it dies? */ - add_city_to_minimap(gx, gy); + ai_unit_new_role(punit, AIUNIT_BUILD_CITY, gx, gy); } else { - punit->ai.ai_role = AIUNIT_AUTO_SETTLER; + ai_unit_new_role(punit, AIUNIT_AUTO_SETTLER, gx, gy); } /* We've now worked out what to do; go to it! */ - if (gx != -1 && gy != -1) { - bool survived = ai_gothere(punit, gx, gy, ferryboat); - if (!survived) - return; - } else { - /* This line makes non-AI autosettlers go off auto when they run - out of squares to improve. I would like keep them on, prepared for - future pollution and warming, but there wasn't consensus to do so. */ - punit->ai.control = FALSE; + if (!ai_gothere(punit, gx, gy, ferryboat)) { + /* died */ + return; } /* If we are at the destination then do the activity. */ Index: server/unittools.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v retrieving revision 1.185 diff -u -r1.185 unittools.c --- server/unittools.c 2002/09/28 23:00:51 1.185 +++ server/unittools.c 2002/10/31 13:20:22 @@ -1770,11 +1770,8 @@ are built, so that no two settlers head towards the same city spot, we need to ensure this reservation is cleared should the settler die on the way. */ - if (unit_owner(punit)->ai.control - && punit->ai.ai_role == AIUNIT_AUTO_SETTLER) { - if (normalize_map_pos(&punit->goto_dest_x, &punit->goto_dest_y)) { - remove_city_from_minimap(punit->goto_dest_x, punit->goto_dest_y); - } + if (unit_owner(punit)->ai.control) { + ai_unit_new_role(punit, AIUNIT_NONE, -1, -1); } remove_unit_sight_points(punit);