Index: ai/aiunit.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aiunit.c,v retrieving revision 1.330 diff -u -r1.330 aiunit.c --- ai/aiunit.c 29 Aug 2004 20:14:41 -0000 1.330 +++ ai/aiunit.c 29 Aug 2004 21:14:03 -0000 @@ -336,29 +336,35 @@ { struct city *pcity = map_get_city(punit->x, punit->y); bool has_defense = FALSE; - - CHECK_UNIT(punit); + int mydef, units = -2; if (!pcity) { return FALSE; } + mydef = assess_defense_unit(pcity, punit, FALSE); unit_list_iterate(map_get_tile(pcity->x, pcity->y)->units, pdef) { - if (assess_defense_unit(pcity, punit, FALSE) >= 0 + if (assess_defense_unit(pcity, pdef, FALSE) >= mydef && pdef != punit && pdef->homecity == pcity->id) { has_defense = TRUE; } + units++; } unit_list_iterate_end; /* Guess I better stay / you can live at home now */ if (!has_defense && pcity->ai.danger > 0) { - /* change homecity to this city */ + /* 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, pcity->x, pcity->y); return TRUE; - } + } /* else city cannot upkeep us! */ + } + /* Treat grave danger */ + if (pcity->ai.grave_danger > units) { + ai_unit_new_role(punit, AIUNIT_DEFEND_HOME, pcity->x, pcity->y); + return TRUE; } return FALSE; }