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 11 May 2003 22:57:03 -0000 @@ -293,3 +293,25 @@ } 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->govt_reeval_turns = 10; + 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 11 May 2003 22:57:03 -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.80 diff -u -r1.80 aihand.c --- ai/aihand.c 10 May 2003 18:11:25 -0000 1.80 +++ ai/aihand.c 11 May 2003 22:57:04 -0000 @@ -312,7 +312,8 @@ 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) { @@ -332,7 +333,8 @@ 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; @@ -357,7 +359,7 @@ * this is a rather big CPU operation, we'd rather not. */ check_player_government_rates(pplayer); city_list_iterate(pplayer->cities, acity) { - generic_city_refresh(acity, TRUE); + generic_city_refresh(acity, FALSE, NULL); auto_arrange_workers(acity); if (ai_fix_unhappy(acity)) { ai_scientists_taxmen(acity); @@ -406,15 +408,7 @@ 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->goal.revolution = i; - } - if (val > ai->goal.govt.val) { - ai->goal.govt.idx = i; - ai->goal.govt.val = val; - ai->goal.govt.req = gov->required_tech; - } + ai->government_want[i] = val; /* Save want */ #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]", @@ -422,12 +416,37 @@ shield_surplus, luxury_total, tax_total, science_total, ppl_happy, ppl_unhappy, ppl_angry, pollution); #endif + } + /* 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 (ai->government_want[i] > ai->goal.govt.val) { + ai->goal.govt.idx = i; + ai->goal.govt.val = ai->government_want[i]; + ai->goal.govt.req = gov->required_tech; + } } /* 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; } /************************************************************************** Index: common/city.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/city.c,v retrieving revision 1.187 diff -u -r1.187 city.c --- common/city.c 6 May 2003 05:18:49 -0000 1.187 +++ common/city.c 11 May 2003 22:57:05 -0000 @@ -46,7 +46,9 @@ static void unhappy_city_check(struct city *pcity); static void set_pollution(struct city *pcity); static void set_food_trade_shields(struct city *pcity); -static void city_support(struct city *pcity); +static void city_support(struct city *pcity, + void (*send_unit_info) (struct player *pplayer, + struct unit *punit)); /* end helper functions for generic_city_refresh */ static int improvement_upkeep_asmiths(struct city *pcity, Impr_Type_id i, @@ -70,9 +72,6 @@ struct citystyle *city_styles = NULL; -/* from server/unittools.h */ -void send_unit_info(struct player *dest, struct unit *punit); - /************************************************************************** ... **************************************************************************/ @@ -1941,7 +1940,9 @@ /************************************************************************** ... **************************************************************************/ -static void city_support(struct city *pcity) +static void city_support(struct city *pcity, + void (*send_unit_info) (struct player *pplayer, + struct unit *punit)) { struct government *g = get_gov_pcity(pcity); @@ -2063,10 +2064,11 @@ } /* Send unit info if anything has changed */ - if ((this_unit->unhappiness != old_unhappiness - || this_unit->upkeep != old_upkeep - || this_unit->upkeep_food != old_upkeep_food - || this_unit->upkeep_gold != old_upkeep_gold) && is_server) { + if (send_unit_info + && (this_unit->unhappiness != old_unhappiness + || this_unit->upkeep != old_upkeep + || this_unit->upkeep_food != old_upkeep_food + || this_unit->upkeep_gold != old_upkeep_gold)) { send_unit_info(unit_owner(this_unit), this_unit); } } @@ -2077,7 +2079,9 @@ ... **************************************************************************/ void generic_city_refresh(struct city *pcity, - bool refresh_trade_route_cities) + bool refresh_trade_route_cities, + void (*send_unit_info) (struct player *pplayer, + struct unit *punit)) { int prev_tile_trade = pcity->tile_trade; @@ -2088,7 +2092,7 @@ set_pollution(pcity); citizen_happy_luxury(pcity); /* with our new found luxuries */ citizen_happy_buildings(pcity); /* temple cathedral colosseum */ - city_support(pcity); /* manage settlers, and units */ + city_support(pcity, send_unit_info); /* manage settlers, and units */ citizen_happy_wonders(pcity); /* happy wonders & fundamentalism */ unhappy_city_check(pcity); @@ -2099,7 +2103,7 @@ struct city *pcity2 = find_city_by_id(pcity->trade[i]); if (pcity2) { - generic_city_refresh(pcity2, FALSE); + generic_city_refresh(pcity2, FALSE, send_unit_info); } } } Index: common/city.h =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/city.h,v retrieving revision 1.128 diff -u -r1.128 city.h --- common/city.h 8 May 2003 03:06:58 -0000 1.128 +++ common/city.h 11 May 2003 22:57:05 -0000 @@ -436,7 +436,9 @@ /* city update functions */ void generic_city_refresh(struct city *pcity, - bool refresh_trade_route_cities); + bool refresh_trade_route_cities, + void (*send_unit_info) (struct player *pplayer, + struct unit *punit)); void adjust_city_free_cost(int *num_free, int *this_cost); int city_corruption(struct city *pcity, int trade); int city_waste(struct city *pcity, int shields); Index: common/aicore/cm.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/aicore/cm.c,v retrieving revision 1.7 diff -u -r1.7 cm.c --- common/aicore/cm.c 17 Apr 2003 15:51:07 -0000 1.7 +++ common/aicore/cm.c 11 May 2003 22:57:05 -0000 @@ -614,7 +614,7 @@ pcity->ppl_taxman = result->taxmen; /* Do a local recalculation of the city */ - generic_city_refresh(pcity, FALSE); + generic_city_refresh(pcity, FALSE, NULL); copy_stats(pcity, result); 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 11 May 2003 22:57:05 -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/cityturn.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v retrieving revision 1.211 diff -u -r1.211 cityturn.c --- server/cityturn.c 6 May 2003 05:18:49 -0000 1.211 +++ server/cityturn.c 11 May 2003 22:57:06 -0000 @@ -85,7 +85,7 @@ **************************************************************************/ void city_refresh(struct city *pcity) { - generic_city_refresh(pcity, TRUE); + generic_city_refresh(pcity, TRUE, send_unit_info); /* AI would calculate this 1000 times otherwise; better to do it once -- Syela */ pcity->ai.trade_want = 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 11 May 2003 22:57:06 -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