Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2003:
[Freeciv-Dev] (PR#5252) Wishlist: Defined civil war nations
Home

[Freeciv-Dev] (PR#5252) Wishlist: Defined civil war nations

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: morgan.jones@xxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#5252) Wishlist: Defined civil war nations
From: "Remi Bonnet" <remi.bonnet@xxxxxxxxxxx>
Date: Thu, 11 Sep 2003 13:10:02 -0700
Reply-to: rt@xxxxxxxxxxxxxx

Here's the new patch.

Some issues have been fixed.
diff -u -r -Xdiff_ignore ../freeciv/common/nation.c ./common/nation.c
--- ../freeciv/common/nation.c  2003-07-28 06:19:12.000000000 +0200
+++ ./common/nation.c   2003-09-11 18:09:47.000000000 +0200
@@ -121,6 +121,15 @@
 }
 
 /***************************************************************
+  Returns pointer to the array of nations that can fork from the
+ nation
+***************************************************************/
+Nation_Type_id* get_nation_civilwar(Nation_Type_id nation)
+{
+  return nations[nation].civilwar_nations;
+}
+
+/***************************************************************
 Returns sex of given leader name. If names is not found,
 return 1 (meaning male).
 ***************************************************************/
@@ -226,6 +235,11 @@
     p->legend = NULL;
   }
 
+  if (p->civilwar_nations) {
+    free(p->civilwar_nations);
+    p->civilwar_nations = NULL;
+  }
+
   nation_city_names_free(p->city_names);
   p->city_names = NULL;
 }
diff -u -r -Xdiff_ignore ../freeciv/common/nation.h ./common/nation.h
--- ../freeciv/common/nation.h  2003-07-11 19:01:40.000000000 +0200
+++ ./common/nation.h   2003-09-11 18:38:43.000000000 +0200
@@ -78,6 +78,11 @@
   char *class;                         /* may be empty */
   char *legend;                                /* may be empty */
 
+  /* NO_NATION_SELECTED terminated list of index of the nations that can fork
+   * from this one.
+   * malloced in load_ruleset_nations() then freed in free_nations() */
+  Nation_Type_id *civilwar_nations;                
+
   /* untranslated copies: */
   char name_orig[MAX_LEN_NAME];
   char name_plural_orig[MAX_LEN_NAME];
@@ -117,6 +122,7 @@
 const char *get_nation_name_plural(Nation_Type_id nation);
 const char *get_nation_name_orig(Nation_Type_id nation);
 struct leader *get_nation_leaders(Nation_Type_id nation, int *dim);
+Nation_Type_id* get_nation_civilwar(Nation_Type_id nation);
 bool get_nation_leader_sex(Nation_Type_id nation, const char *name);
 struct nation_type *get_nation_by_plr(struct player *plr);
 struct nation_type *get_nation_by_idx(Nation_Type_id nation);
Seulement dans ./data/nation: Makefile.am
diff -u -r -Xdiff_ignore ../freeciv/server/plrhand.c ./server/plrhand.c
--- ../freeciv/server/plrhand.c 2003-09-09 17:52:47.000000000 +0200
+++ ./server/plrhand.c  2003-09-11 18:30:13.000000000 +0200
@@ -61,6 +61,7 @@
                                 struct packet_player_info *packet,
                                 struct player *receiver,
                                 enum plr_info_level min_info_level);
+static Nation_Type_id pick_available_nation(Nation_Type_id *choices);
 static void tech_researched(struct player* plr);
 static bool choose_goal_tech(struct player *plr);
 static enum plr_info_level player_info_level(struct player *plr,
@@ -1553,6 +1554,65 @@
   }
 }
 
+/**********************************************************************
+ This function return one of the nations available from the 
+NO_NATION_SELECTED-terminated choices list. If no available nations in this
+file were found, return a random nation. If no nations are available, die.
+**********************************************************************/
+static Nation_Type_id pick_available_nation(Nation_Type_id *choices)
+{
+  int *nations_used, i, num_nations_avail = game.playable_nation_count, pick;
+  int looking_for, pref_nations_avail = 0; 
+
+  /* Values of nations_used: 
+   * 0: not available
+   * 1: available
+   * 2: preferred choice
+   */
+  nations_used = fc_calloc(game.playable_nation_count,sizeof(int));
+
+  for (i = 0; i < game.playable_nation_count; i++) {
+    nations_used[i] = 1;
+  }
+
+  for (i = 0; choices[i] != NO_NATION_SELECTED; i++) {
+    pref_nations_avail++;
+    nations_used[choices[i]] = 2;
+  }
+
+  players_iterate(other_player) {
+    if (other_player->nation < game.playable_nation_count) {
+      if (nations_used[other_player->nation] == 2) {
+       pref_nations_avail--;
+      } 
+      nations_used[other_player->nation] = 0;
+      num_nations_avail--;
+    } 
+  } players_iterate_end;
+
+  assert(num_nations_avail > 0);
+
+  if (pref_nations_avail == 0) {
+    pick = myrand(num_nations_avail);
+    looking_for = 1;
+  } else {
+    pick = myrand(pref_nations_avail);
+    looking_for = 2;
+  }
+
+  for (i = 0; i < game.playable_nation_count; i++){ 
+    if (nations_used[i] == looking_for) {
+      pick--;
+      
+      if (pick < 0) {
+       break;
+      }
+    }
+  }
+
+  free(nations_used);
+  return i;
+}
 
 /**********************************************************************
 This function creates a new player and copies all of it's science
@@ -1562,44 +1622,18 @@
 ***********************************************************************/
 static struct player *split_player(struct player *pplayer)
 {
-  int *nations_used, i, num_nations_avail=game.playable_nation_count, pick;
   int newplayer = game.nplayers;
   struct player *cplayer = &game.players[newplayer];
+  Nation_Type_id *civilwar_nations = get_nation_civilwar(pplayer->nation);
 
-  nations_used = fc_calloc(game.playable_nation_count,sizeof(int));
-  
   /* 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<game.playable_nation_count;i++){
-    nations_used[i]=i;
-  }
-
-  players_iterate(other_player) {
-    if (other_player->nation < game.playable_nation_count) {
-      nations_used[other_player->nation] = -1;
-      num_nations_avail--;
-    }
-  } players_iterate_end;
-
-  pick = myrand(num_nations_avail);
-
-  for(i=0; i<game.playable_nation_count; i++){ 
-    if(nations_used[i] != -1)
-      pick--;
-    if(pick < 0) break;
-  }
-  
   /* Rebel will always be an AI player */
-
-  cplayer->nation = nations_used[i];
-  free(nations_used);
-  nations_used = NULL;
-  pick_ai_player_name(cplayer->nation,cplayer->name);
+  cplayer->nation = pick_available_nation(civilwar_nations);
+  pick_ai_player_name(cplayer->nation, cplayer->name);
 
   sz_strlcpy(cplayer->username, ANON_USER_NAME);
   cplayer->is_connected = FALSE;
diff -u -r -Xdiff_ignore ../freeciv/server/ruleset.c ./server/ruleset.c
--- ../freeciv/server/ruleset.c 2003-08-26 11:01:17.000000000 +0200
+++ ./server/ruleset.c  2003-09-11 18:12:58.000000000 +0200
@@ -2048,9 +2048,9 @@
   char *datafile_options, *bad_leader, *g;
   struct nation_type *pl;
   struct government *gov;
-  int *res, dim, val, i, j, nval;
+  int *res, dim, val, i, j, k, nval;
   char temp_name[MAX_LEN_NAME];
-  char **techs, **leaders, **sec;
+  char **techs, **leaders, **sec, **civilwar_nations;
   const char *filename = secfile_filename(file);
 
   datafile_options = check_ruleset_capabilities(file, "+1.9", filename);
@@ -2174,6 +2174,33 @@
       pl->city_style = 0;
     }
 
+    /* Civilwar nations */
+    
+    civilwar_nations = secfile_lookup_str_vec(file, &dim,
+                                             "%s.civilwar_nations", sec[i]);
+    pl->civilwar_nations = fc_malloc(sizeof(Nation_Type_id) * (dim + 1));
+
+    for (j = 0, k = 0; k < dim; j++, k++) {
+      /* Seems that at this time, all the names are untranslated and name_orig
+       * field is empty so we must call find_nation_by_name instead of
+       * find_nation_by_name_orig. */
+      pl->civilwar_nations[j] = find_nation_by_name(civilwar_nations[k]);
+
+      if (pl->civilwar_nations[j] == -1) {
+       j--;
+       freelog(LOG_ERROR, "Civil war nation %s for nation %s not defined.",
+               civilwar_nations[k], pl->name);
+      }
+    }
+
+    /* No test for duplicate nations are performed. This is normally
+     * supported by the code naturally without any lost of speed/memory
+     * The only difference is that the duplicate nation will have a 
+     * probability rate x2. So I can say:
+     * "That's not a bug, that's a feature" :-) */
+
+    pl->civilwar_nations[j] = NO_NATION_SELECTED;
+
     /* AI stuff */
 
     pl->attack = secfile_lookup_int_default(file, 2, "%s.attack", sec[i]);

[Prev in Thread] Current Thread [Next in Thread]