diff --git a/ai/tex/texaiplayer.c b/ai/tex/texaiplayer.c index cc1c14d973..f9a6082963 100644 --- a/ai/tex/texaiplayer.c +++ b/ai/tex/texaiplayer.c @@ -25,6 +25,9 @@ #include "map.h" #include "unit.h" +/* server */ +#include "srv_main.h" + /* server/advisors */ #include "advchoice.h" #include "infracache.h" @@ -323,14 +326,19 @@ void texai_control_gained(struct ai_type *ait, struct player *pplayer) fc_init_mutex(&exthrai.msgs_to.mutex); fc_thread_start(&exthrai.ait, texai_thread_start, ait); - players_iterate(oplayer) { - city_list_iterate(oplayer->cities, pcity) { - texai_city_created(pcity); - } city_list_iterate_end; - unit_list_iterate(oplayer->units, punit) { - texai_unit_created(punit); - } unit_list_iterate_end; - } players_iterate_end; + if (S_S_RUNNING == server_state()) { + /* We do not do this while loading a savegame */ + log_verbose("registering all cities and units"); + players_iterate(oplayer) { + city_list_iterate(oplayer->cities, pcity) { + log_verbose("control city_created %d", pcity->id); + texai_city_created(pcity); + } city_list_iterate_end; + unit_list_iterate(oplayer->units, punit) { + texai_unit_created(punit); + } unit_list_iterate_end; + } players_iterate_end; + } } } diff --git a/doc/README.AI_modules b/doc/README.AI_modules index d1d90b18b8..8712390714 100644 --- a/doc/README.AI_modules +++ b/doc/README.AI_modules @@ -157,3 +157,57 @@ New in Freeciv 2.6: - Added want_to_explore, called for AI type of the unit owner, when it is about to autoexplore - Added currently unused 'reserved' pointers that we can populate with optional callbacks without need to change the mandatory capability of the modules + +7. AI callbacks on startup +----------------------------------- + +In a new game +------------- + +- server_state() is S_S_INITIAL +- gained_control() +- map_alloc() +- the map is created +- map_ready() +- server_state() is set to S_S_RUNNING +- tile_info() for each visible tile +- initial units and cities (if any) are created +- unit_created(), unit_got() are called for each starting unit +- city_created(), city_got() are called for each starting city (if any) +- game_start() + + + +When a savegame is being loaded +------------------------------- + +The game loading sequence is: + +- server_state() is S_S_INITIAL +- map_alloc() +- map loaded +- players loaded, calling player_load(), player_load_relations(), gained_control() +- cities and units loaded, calling city_load() and unit_load() +- unit_created(each unit) +- unit_got(each own unit) +- city_created(each city) +- city_got(each own city) +- map_ready() +- server_state() goes to S_S_RUNNING + +Note that gained_control(), player_load(), player_load_relations(), +city_load() and unit_load() are called while the savefile is being +loaded - they allow the AI module to gather extra information from the +savefile, but the game is not yet in a consistent state (i.e. cities +and units are not all loaded) + +When a player is switched to AI or to away mode +----------------------------------------------- + +- server_state() is S_S_RUNNING +- gained_control() +- restart_phase() + +In this case, gained_control() will have to gather all the necessary +information from the globals, it won't receive direct updates about +units, cities or anything. diff --git a/server/unittools.c b/server/unittools.c index ada9e1299c..a9f946e552 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -1614,7 +1614,7 @@ void unit_get_goods(struct unit *punit) } /**********************************************************************//** - Creates a unit, and set it's initial values, and put it into the right + Creates a unit, and set its initial values, and put it into the right lists. If moves_left is less than zero, unit will get max moves. **************************************************************************/