Index: ai/aidata.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v retrieving revision 1.14 diff -u -r1.14 aidata.c --- ai/aidata.c 6 May 2003 05:18:49 -0000 1.14 +++ ai/aidata.c 15 May 2003 13:22:37 -0000 @@ -263,6 +263,9 @@ ai->pollution_priority = POLLUTION_WEIGHTING; /* Goals */ + ai->govt_reeval_turns = city_list_size(&pplayer->cities); + ai->govt_reeval_turns = MAX(5, ai->govt_reeval_turns); + ai->govt_reeval_turns = MIN(20, ai->govt_reeval_turns); ai_best_government(pplayer); } @@ -292,4 +295,25 @@ ai_data_turn_init(pplayer); } return ai; +} + +/************************************************************************** + Initialize with sane values. +**************************************************************************/ +void ai_data_init(struct player *pplayer) +{ + struct ai_data *ai = &aidata[pplayer->player_no]; + + ai->govt_reeval = 0; + ai->government_want = fc_calloc(game.government_count + 1, sizeof(int)); +} + +/************************************************************************** + Deinitialize data +**************************************************************************/ +void ai_data_done(struct player *pplayer) +{ + struct ai_data *ai = &aidata[pplayer->player_no]; + + free(ai->government_want); } Index: ai/aidata.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aidata.h,v retrieving revision 1.8 diff -u -r1.8 aidata.h --- ai/aidata.h 6 May 2003 05:18:49 -0000 1.8 +++ ai/aidata.h 15 May 2003 13:22:37 -0000 @@ -80,6 +80,11 @@ int angry_priority; int pollution_priority; + /* Government data */ + int *government_want; + short govt_reeval; + short govt_reeval_turns; + /* Goals */ struct { struct { @@ -93,6 +98,9 @@ void ai_data_turn_init(struct player *pplayer); void ai_data_turn_done(struct player *pplayer); + +void ai_data_init(struct player *pplayer); +void ai_data_done(struct player *pplayer); struct ai_data *ai_data_get(struct player *pplayer); Index: ai/aihand.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/ai/aihand.c,v retrieving revision 1.81 diff -u -r1.81 aihand.c --- ai/aihand.c 13 May 2003 07:04:28 -0000 1.81 +++ ai/aihand.c 15 May 2003 13:22:37 -0000 @@ -312,11 +312,11 @@ best of the governments is not available to us (it is not yet discovered), we record it in the goal.gov structure with the aim of wanting the necessary tech more. The best of the available governments is recorded - in goal.revolution. + in goal.revolution. We record the want of each government, and only + recalculate this data every ai->govt_reeval_turns turns. **************************************************************************/ void ai_best_government(struct player *pplayer) { -#undef ANALYSE struct ai_data *ai = ai_data_get(pplayer); int best_val = 0; int i; @@ -328,27 +328,16 @@ ai->goal.govt.req = A_UNSET; ai->goal.revolution = pplayer->government; - if (ai_handicap(pplayer, H_AWAY)) { + if (ai_handicap(pplayer, H_AWAY) || !pplayer->is_alive) { return; } - for (i = 0; i < game.government_count; i++) { + if (ai->govt_reeval == 0) { + for (i = 0; i < game.government_count; i++) { struct government *gov = &governments[i]; int val = 0; int dist; -#ifdef ANALYSE - int food_surplus = 0; - int shield_surplus = 0; - int luxury_total = 0; - int tax_total = 0; - int science_total = 0; - int ppl_happy = 0; - int ppl_unhappy = 0; - int ppl_angry = 0; - int pollution = 0; -#endif - if (i == game.government_when_anarchy) { continue; /* pointless */ } @@ -365,17 +354,6 @@ } city_list_iterate_end; city_list_iterate(pplayer->cities, pcity) { val += ai_eval_calc_city(pcity, ai); -#ifdef ANALYSE - food_surplus += pcity->food_surplus; - shield_surplus += pcity->shield_surplus; - luxury_total += pcity->luxury_total; - tax_total += pcity->tax_total; - science_total += pcity->science_total; - ppl_happy += pcity->ppl_happy[4]; - ppl_unhappy += pcity->ppl_unhappy[4]; - ppl_angry += pcity->ppl_angry[4]; - pollution += pcity->pollution; -#endif } city_list_iterate_end; /* Bonuses for non-economic abilities. We increase val by @@ -406,28 +384,38 @@ dist = MAX(1, num_unknown_techs_for_goal(pplayer, gov->required_tech)); val = amortize(val, dist); - if (val > best_val && can_change_to_government(pplayer, i)) { - best_val = val; + ai->government_want[i] = val; /* Save want */ + } + /* Now reset our gov to it's real state. */ + pplayer->government = current_gov; + city_list_iterate(pplayer->cities, acity) { + generic_city_refresh(acity, FALSE, NULL); + auto_arrange_workers(acity); + if (ai_fix_unhappy(acity)) { + ai_scientists_taxmen(acity); + } + } city_list_iterate_end; + } + ai->govt_reeval = (ai->govt_reeval + 1) % ai->govt_reeval_turns; + + /* Figure out which government is the best for us this turn. */ + for (i = 0; i < game.government_count; i++) { + struct government *gov = &governments[i]; + + if (ai->government_want[i] > best_val + && can_change_to_government(pplayer, i)) { + best_val = ai->government_want[i]; ai->goal.revolution = i; } - if (val > ai->goal.govt.val) { + if (ai->government_want[i] > ai->goal.govt.val) { ai->goal.govt.idx = i; - ai->goal.govt.val = val; + ai->goal.govt.val = ai->government_want[i]; ai->goal.govt.req = gov->required_tech; } -#ifdef ANALYSE - freelog(LOG_NORMAL, "%s govt eval %s (dist %d): " - "%d [f%d|sh%d|l%d|g%d|sc%d|h%d|u%d|a%d|p%d]", - pplayer->name, gov->name, dist, val, food_surplus, - shield_surplus, luxury_total, tax_total, science_total, - ppl_happy, ppl_unhappy, ppl_angry, pollution); -#endif } /* Goodness of the ideal gov is calculated relative to the goodness of the * best of the available ones. */ ai->goal.govt.val -= best_val; - /* Now reset our gov to it's real state. */ - pplayer->government = current_gov; } /************************************************************************** @@ -436,6 +424,10 @@ static void ai_manage_government(struct player *pplayer) { struct ai_data *ai = ai_data_get(pplayer); + + if (!pplayer->is_alive || ai_handicap(pplayer, H_AWAY)) { + return; + } if (ai->goal.revolution != pplayer->government) { ai_government_change(pplayer, ai->goal.revolution); /* change */ Index: server/barbarian.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/barbarian.c,v retrieving revision 1.61 diff -u -r1.61 barbarian.c --- server/barbarian.c 12 Apr 2003 18:24:42 -0000 1.61 +++ server/barbarian.c 15 May 2003 13:22:38 -0000 @@ -44,6 +44,7 @@ #include "unithand.h" #include "unittools.h" +#include "aidata.h" #include "aitools.h" #include "barbarian.h" @@ -129,6 +130,7 @@ } set_ai_level_directer(barbarians, game.skill_level); init_tech(barbarians, game.tech); + ai_data_init(barbarians); /* Ensure that we are at war with everyone else */ players_iterate(pplayer) { Index: server/plrhand.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v retrieving revision 1.271 diff -u -r1.271 plrhand.c --- server/plrhand.c 10 May 2003 19:20:16 -0000 1.271 +++ server/plrhand.c 15 May 2003 13:22:38 -0000 @@ -1328,6 +1328,7 @@ } } conn_list_iterate_end; + ai_data_done(pplayer); game_remove_player(pplayer); game_renumber_players(pplayer->player_no); } @@ -1468,7 +1469,8 @@ /* make a new player */ server_player_init(cplayer, TRUE); - + ai_data_init(cplayer); + /* select a new name and nation for the copied player. */ for(i=0; i