Author: cazfi
Date: Mon Oct 19 20:45:49 2015
New Revision: 30142

URL: http://svn.gna.org/viewcvs/freeciv?rev=30142&view=rev
Log:
Added a couple of AI callbacks that classic AI uses to save its information
to the same table as default AI part does.

See bug #23874

Modified:
    trunk/ai/classic/classicai.c
    trunk/ai/default/aiplayer.c
    trunk/ai/default/aiplayer.h
    trunk/common/ai.h
    trunk/doc/README.AI_modules
    trunk/server/savegame2.c
    trunk/server/savegame3.c

Modified: trunk/ai/classic/classicai.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/classic/classicai.c?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/ai/classic/classicai.c        (original)
+++ trunk/ai/classic/classicai.c        Mon Oct 19 20:45:49 2015
@@ -91,24 +91,30 @@
 
 /**************************************************************************
   Call default ai with classic ai type as parameter.
-**************************************************************************/
-static void cai_player_save(struct player *pplayer, struct section_file *file,
-                     int plrno)
-{
-  struct ai_type *deftype = classic_ai_get_self();
-
-  dai_player_save(deftype, "ai", pplayer, file, plrno);
-}
-
-/**************************************************************************
-  Call default ai with classic ai type as parameter.
-**************************************************************************/
-static void cai_player_load(struct player *pplayer,
-                            const struct section_file *file, int plrno)
-{
-  struct ai_type *deftype = classic_ai_get_self();
-
-  dai_player_load(deftype, "ai", pplayer, file, plrno);
+  Classicai stores information to "ai" like the default ai common code.
+**************************************************************************/
+static void cai_player_save_relations(struct player *pplayer,
+                                      struct player *other,
+                                      struct section_file *file,
+                                      int plrno)
+{
+  struct ai_type *deftype = classic_ai_get_self();
+
+  dai_player_save_relations(deftype, "ai", pplayer, other, file, plrno);
+}
+
+/**************************************************************************
+  Call default ai with classic ai type as parameter.
+  Classicai stores information to "ai" like the default ai common code.
+**************************************************************************/
+static void cai_player_load_relations(struct player *pplayer,
+                                      struct player *other,
+                                      const struct section_file *file,
+                                      int plrno)
+{
+  struct ai_type *deftype = classic_ai_get_self();
+
+  dai_player_load_relations(deftype, "ai", pplayer, other, file, plrno);
 }
 
 /**************************************************************************
@@ -562,8 +568,10 @@
 
   ai->funcs.player_alloc = cai_player_alloc;
   ai->funcs.player_free = cai_player_free;
-  ai->funcs.player_save = cai_player_save;
-  ai->funcs.player_load = cai_player_load;
+  /* ai->funcs.player_save = NULL; */
+  /* ai->funcs.player_load = NULL; */
+  ai->funcs.player_save_relations = cai_player_save_relations;
+  ai->funcs.player_load_relations = cai_player_load_relations;
   ai->funcs.gained_control = cai_gained_control;
   /* ai->funcs.lost_control = NULL; */
   ai->funcs.split_by_civil_war = cai_split_by_civil_war;

Modified: trunk/ai/default/aiplayer.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aiplayer.c?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/ai/default/aiplayer.c (original)
+++ trunk/ai/default/aiplayer.c Mon Oct 19 20:45:49 2015
@@ -61,65 +61,86 @@
   Store player specific data to savegame
 **************************************************************************/
 void dai_player_save(struct ai_type *ait, const char *aitstr,
-                     struct player *pplayer, struct section_file *file, int 
plrno)
+                     struct player *pplayer, struct section_file *file,
+                     int plrno)
 {
-  players_iterate(aplayer) {
-    struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
-    char buf[32];
+  players_iterate(other) {
+    dai_player_save_relations(ait, aitstr, pplayer, other, file, plrno);
+  } players_iterate_end;
+}
 
-    fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
-                player_index(aplayer));
+/**************************************************************************
+  Store player specific data to savegame
+**************************************************************************/
+void dai_player_save_relations(struct ai_type *ait, const char *aitstr,
+                               struct player *pplayer, struct player *other,
+                               struct section_file *file, int plrno)
+{
+  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
+  char buf[32];
 
-    secfile_insert_int(file, adip->spam,
-                       "%s.spam", buf);
-    secfile_insert_int(file, adip->countdown,
-                       "%s.countdown", buf);
-    secfile_insert_int(file, adip->war_reason,
-                       "%s.war_reason", buf);
-    secfile_insert_int(file, adip->ally_patience,
-                       "%s.patience", buf);
-    secfile_insert_int(file, adip->warned_about_space,
-                       "%s.warn_space", buf);
-    secfile_insert_int(file, adip->asked_about_peace,
-                       "%s.ask_peace", buf);
-    secfile_insert_int(file, adip->asked_about_alliance,
-                       "%s.ask_alliance", buf);
-    secfile_insert_int(file, adip->asked_about_ceasefire,
-                       "%s.ask_ceasefire", buf);
-  } players_iterate_end;
+  fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
+              player_index(other));
+
+  secfile_insert_int(file, adip->spam,
+                     "%s.spam", buf);
+  secfile_insert_int(file, adip->countdown,
+                     "%s.countdown", buf);
+  secfile_insert_int(file, adip->war_reason,
+                     "%s.war_reason", buf);
+  secfile_insert_int(file, adip->ally_patience,
+                     "%s.patience", buf);
+  secfile_insert_int(file, adip->warned_about_space,
+                     "%s.warn_space", buf);
+  secfile_insert_int(file, adip->asked_about_peace,
+                     "%s.ask_peace", buf);
+  secfile_insert_int(file, adip->asked_about_alliance,
+                     "%s.ask_alliance", buf);
+  secfile_insert_int(file, adip->asked_about_ceasefire,
+                     "%s.ask_ceasefire", buf);
 }
 
 /**************************************************************************
   Load player specific data from savegame
 **************************************************************************/
 void dai_player_load(struct ai_type *ait, const char *aitstr,
-                     struct player *pplayer, const struct section_file *file,
-                     int plrno)
+                     struct player *pplayer,
+                     const struct section_file *file, int plrno)
 {
-  players_iterate(aplayer) {
-    struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer);
-    char buf[32];
+  players_iterate(other) {
+    dai_player_load_relations(ait, aitstr, pplayer, other, file, plrno);
+  } players_iterate_end;
+}
 
-    fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
-                player_index(aplayer));
+/**************************************************************************
+  Load player vs player specific data from savegame
+**************************************************************************/
+void dai_player_load_relations(struct ai_type *ait, const char *aitstr,
+                               struct player *pplayer, struct player *other,
+                               const struct section_file *file, int plrno)
+{
+  struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, other);
+  char buf[32];
 
-    adip->spam
-         = secfile_lookup_int_default(file, 0, "%s.spam", buf);
-    adip->countdown
-         = secfile_lookup_int_default(file, -1, "%s.countdown", buf);
-    adip->war_reason
-         = secfile_lookup_int_default(file, 0, "%s.war_reason", buf);
-    adip->ally_patience
-         = secfile_lookup_int_default(file, 0, "%s.patience", buf);
-    adip->warned_about_space
-         = secfile_lookup_int_default(file, 0, "%s.warn_space", buf);
-    adip->asked_about_peace
-         = secfile_lookup_int_default(file, 0, "%s.ask_peace", buf);
-    adip->asked_about_alliance
-         = secfile_lookup_int_default(file, 0, "%s.ask_alliance", buf);
-    adip->asked_about_ceasefire
-         = secfile_lookup_int_default(file, 0, "%s.ask_ceasefire", buf);
-  } players_iterate_end;
+  fc_snprintf(buf, sizeof(buf), "player%d.%s%d", plrno, aitstr,
+              player_index(other));
+
+  adip->spam
+    = secfile_lookup_int_default(file, 0, "%s.spam", buf);
+  adip->countdown
+    = secfile_lookup_int_default(file, -1, "%s.countdown", buf);
+  adip->war_reason
+    = secfile_lookup_int_default(file, 0, "%s.war_reason", buf);
+  adip->ally_patience
+    = secfile_lookup_int_default(file, 0, "%s.patience", buf);
+  adip->warned_about_space
+    = secfile_lookup_int_default(file, 0, "%s.warn_space", buf);
+  adip->asked_about_peace
+    = secfile_lookup_int_default(file, 0, "%s.ask_peace", buf);
+  adip->asked_about_alliance
+    = secfile_lookup_int_default(file, 0, "%s.ask_alliance", buf);
+  adip->asked_about_ceasefire
+    = secfile_lookup_int_default(file, 0, "%s.ask_ceasefire", buf);
 }
 
 /**************************************************************************

Modified: trunk/ai/default/aiplayer.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aiplayer.h?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/ai/default/aiplayer.h (original)
+++ trunk/ai/default/aiplayer.h Mon Oct 19 20:45:49 2015
@@ -22,11 +22,17 @@
 void dai_player_alloc(struct ai_type *ait, struct player *pplayer);
 void dai_player_free(struct ai_type *ait, struct player *pplayer);
 void dai_player_save(struct ai_type *ait, const char *aitstr,
-                     struct player *pplayer,
-                     struct section_file *file, int plrno);
+                     struct player *pplayer, struct section_file *file,
+                     int plrno);
+void dai_player_save_relations(struct ai_type *ait, const char *aitstr,
+                               struct player *pplayer, struct player *other,
+                               struct section_file *file, int plrno);
 void dai_player_load(struct ai_type *ait, const char *aitstr,
-                     struct player *pplayer,
-                     const struct section_file *file, int plrno);
+                     struct player *pplayer, const struct section_file *file,
+                     int plrno);
+void dai_player_load_relations(struct ai_type *ait, const char *aitstr,
+                               struct player *pplayer, struct player *other,
+                               const struct section_file *file, int plrno);
 
 void dai_player_copy(struct ai_type *ait,
                      struct player *original, struct player *created);

Modified: trunk/common/ai.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/ai.h?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/common/ai.h   (original)
+++ trunk/common/ai.h   Mon Oct 19 20:45:49 2015
@@ -22,7 +22,7 @@
 
 /* Update this capability string when ever there is changes to ai_type
    structure below */
-#define FC_AI_MOD_CAPSTR "+Freeciv-ai-module-2015.Aug.03"
+#define FC_AI_MOD_CAPSTR "+Freeciv-ai-module-2015.Oct.15"
 
 /* Timers for all AI activities. Define it to get statistics about the AI. */
 #ifdef FREECIV_DEBUG
@@ -71,6 +71,20 @@
     /* Called for every AI type for each player in game when game loaded. */
     void (*player_load)(struct player *pplayer, const struct section_file 
*file,
                         int plrno);
+
+    /* Called for every AI type for each player in game when game saved,
+     * with each other player as parameter.
+     * In practice it's good to use player_save_relations when you
+     * want to add entries to "player%d.ai%d", but player_iterate() inside
+     * player_save is better otherwise. The difference is in how clean
+     * structure the produced savegame will have. */
+    void (*player_save_relations)(struct player *pplayer, struct player *other,
+                                  struct section_file *file, int plrno);
+
+    /* Called for every AI type for each player in game when game loaded,
+     * with each other player as parameter. */
+    void (*player_load_relations)(struct player *pplayer, struct player *other,
+                                  const struct section_file *file, int plrno);
 
     /* Called for AI type that gains control of player. */
     void (*gained_control)(struct player *pplayer);

Modified: trunk/doc/README.AI_modules
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/doc/README.AI_modules?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/doc/README.AI_modules (original)
+++ trunk/doc/README.AI_modules Mon Oct 19 20:45:49 2015
@@ -127,3 +127,6 @@
 - Added game_start, called for all AI types when game starts
 - "city_got" is called when the city is fully initialized, "city_lost" when 
city still valid
 - Added gov_value for giving AI control of deciding value of governments
+- Added player_save_relations and player_load_relations called on savegame 
handling for a
+  player once per other player in game. Old player_save and player_load are 
called only once
+  overall

Modified: trunk/server/savegame2.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/savegame2.c?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/server/savegame2.c    (original)
+++ trunk/server/savegame2.c    Mon Oct 19 20:45:49 2015
@@ -2737,6 +2737,7 @@
 
     plr->ai_common.love[player_index(aplayer)] =
         secfile_lookup_int_default(loading->file, 1, "%s.love", buf);
+    CALL_FUNC_EACH_AI(player_load_relations, plr, aplayer, loading->file, 
plrno);
   } players_iterate_end;
 
   CALL_FUNC_EACH_AI(player_load, plr, loading->file, plrno);

Modified: trunk/server/savegame3.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/savegame3.c?rev=30142&r1=30141&r2=30142&view=diff
==============================================================================
--- trunk/server/savegame3.c    (original)
+++ trunk/server/savegame3.c    Mon Oct 19 20:45:49 2015
@@ -3626,6 +3626,7 @@
 
     plr->ai_common.love[player_index(aplayer)] =
         secfile_lookup_int_default(loading->file, 1, "%s.love", buf);
+    CALL_FUNC_EACH_AI(player_load_relations, plr, aplayer, loading->file, 
plrno);
   } players_iterate_end;
 
   CALL_FUNC_EACH_AI(player_load, plr, loading->file, plrno);
@@ -3982,6 +3983,7 @@
     /* save ai data */
     secfile_insert_int(saving->file, plr->ai_common.love[i],
                        "player%d.ai%d.love", plrno, i);
+    CALL_FUNC_EACH_AI(player_save_relations, plr, aplayer, saving->file, 
plrno);
   } players_iterate_end;
 
   CALL_FUNC_EACH_AI(player_save, plr, saving->file, plrno);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to