Complete.Org: Mailing Lists: Archives: freeciv-dev: January 2003:
[Freeciv-Dev] Re: (PR#2598) Increase leader limit
Home

[Freeciv-Dev] Re: (PR#2598) Increase leader limit

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#2598) Increase leader limit
From: "Raimar Falke via RT" <rt@xxxxxxxxxxxxxx>
Date: Wed, 8 Jan 2003 02:18:43 -0800
Reply-to: rt@xxxxxxxxxxxxxx

On Tue, Dec 17, 2002 at 11:26:12PM -0800, Jason Short via RT wrote:
> 
> [rfalke - Tue Dec 17 22:54:08 2002]:
> 
> > 
> > To take use of the extra leader in the american ruleset you need the
> > attached patch.
> 
> 62 nations * 48 leaders/nation * 8 bytes/leader = 24k of extra memory
> this change will take.  IMO it is better to dynamically allocate this
> array on a per-nation basis.

And the patch. It is this big because I took the liberty to change
  char *leader_name[MAX_NUM_LEADERS];
  bool  leader_is_male[MAX_NUM_LEADERS];
to a more OO like
  struct leader *leaders;
This way we can stick a picture to a leader or a description or
something else.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 A life? Cool! Where can I download one?

Index: client/packhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/packhand.c,v
retrieving revision 1.270
diff -u -u -r1.270 packhand.c
--- client/packhand.c   2003/01/05 23:24:51     1.270
+++ client/packhand.c   2003/01/08 10:10:15
@@ -2156,9 +2156,10 @@
   sz_strlcpy(pl->flag_graphic_str, p->graphic_str);
   sz_strlcpy(pl->flag_graphic_alt, p->graphic_alt);
   pl->leader_count = p->leader_count;
-  for( i=0; i<pl->leader_count; i++) {
-    pl->leader_name[i] = mystrdup(p->leader_name[i]);
-    pl->leader_is_male[i] = p->leader_sex[i];
+  pl->leaders = fc_malloc(sizeof(*pl->leaders) * pl->leader_count);
+  for (i = 0; i < pl->leader_count; i++) {
+    pl->leaders[i].name = mystrdup(p->leader_name[i]);
+    pl->leaders[i].is_male = p->leader_sex[i];
   }
   pl->city_style = p->city_style;
 
Index: common/nation.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.c,v
retrieving revision 1.31
diff -u -u -r1.31 nation.c
--- common/nation.c     2002/12/21 09:45:42     1.31
+++ common/nation.c     2003/01/08 10:10:17
@@ -88,11 +88,18 @@
 ***************************************************************/
 char **get_nation_leader_names(Nation_Type_id nation, int *dim)
 {
+  static char **result = NULL;
+  int i;
+
   if (!bounds_check_nation_id(nation, LOG_FATAL, "get_nation_leader_names")) {
     die("wrong nation %d", nation);
   }
   *dim = nations[nation].leader_count;
-  return nations[nation].leader_name;
+  result = fc_realloc(result, sizeof(char *) * (*dim));
+  for (i = 0; i < *dim; i++) {
+    result[i] = nations[nation].leaders[i].name;
+  }
+  return result;
 }
 
 /***************************************************************
@@ -105,15 +112,13 @@
   
   if (!bounds_check_nation_id(nation, LOG_ERROR, "get_nation_leader_sex")) {
     return FALSE;
+  }
+  for (i = 0; i < nations[nation].leader_count; i++) {
+    if (strcmp(nations[nation].leaders[i].name, name) == 0) {
+      return nations[nation].leaders[i].is_male;
+    }
   }
-  for( i=0; i < nations[nation].leader_count; i++ ) {
-    if (strcmp(nations[nation].leader_name[i], name) == 0)
-      break;
-  }
-  if( i <  nations[nation].leader_count )
-    return nations[nation].leader_is_male[i];
-  else
-    return TRUE;
+  return TRUE;
 }
 
 /***************************************************************
@@ -122,16 +127,16 @@
 bool check_nation_leader_name(Nation_Type_id nation, const char *name)
 {
   int i;
-  bool found = FALSE;
   
   if (!bounds_check_nation_id(nation, LOG_ERROR, "check_nation_leader_name")) {
     return TRUE;                       /* ? */
   }
-  for( i=0; i<nations[nation].leader_count; i++) {
-    if (strcmp(name, nations[nation].leader_name[i]) == 0)
-      found = TRUE;
+  for (i = 0; i < nations[nation].leader_count; i++) {
+    if (strcmp(name, nations[nation].leaders[i].name) == 0) {
+      return TRUE;
+    }
   }
-  return found;
+  return FALSE;
 }
 
 /***************************************************************
@@ -186,8 +191,10 @@
   struct nation_type *p = get_nation_by_idx(nation);
 
   for (i = 0; i < p->leader_count; i++) {
-    free(p->leader_name[i]);
+    free(p->leaders[i].name);
   }
+  free(p->leaders);
+  p->leaders = NULL;
 
   nation_city_names_free(p->city_names);
   p->city_names = NULL;
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.18
diff -u -u -r1.18 nation.h
--- common/nation.h     2002/12/18 17:36:19     1.18
+++ common/nation.h     2003/01/08 10:10:17
@@ -18,7 +18,13 @@
 
 #define MAX_NUM_TECH_GOALS 10
 #define MAX_NUM_NATIONS  63
-#define MAX_NUM_LEADERS  16
+
+/* 
+ * Purpose of this constant is to catch invalid ruleset and network
+ * data and to allow static allocation of the nation_info packet.
+ */
+#define MAX_NUM_LEADERS MAX_NUM_ITEMS
+
 #define MAX_NUM_TEAMS MAX_NUM_PLAYERS
 #define TEAM_NONE 255
 
@@ -52,14 +58,18 @@
   ternary terrain[T_COUNT];    
 };
 
+struct leader {
+  char *name;
+  bool is_male;
+};
+
 struct nation_type {
   char name[MAX_LEN_NAME];
   char name_plural[MAX_LEN_NAME];
   char flag_graphic_str[MAX_LEN_NAME];
   char flag_graphic_alt[MAX_LEN_NAME];
   int  leader_count;
-  char *leader_name[MAX_NUM_LEADERS];
-  bool  leader_is_male[MAX_NUM_LEADERS];
+  struct leader *leaders;
   int city_style;
   struct city_name *city_names;                /* The default city names. */
   struct Sprite *flag_sprite;
Index: data/nation/american.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/nation/american.ruleset,v
retrieving revision 1.6
diff -u -u -r1.6 american.ruleset
--- data/nation/american.ruleset        2002/12/17 22:40:57     1.6
+++ data/nation/american.ruleset        2003/01/08 10:10:18
@@ -17,45 +17,43 @@
        "Millard Filmore",
        "Franklin Pierce",
        "James Buchanan",
-       "Abraham Lincoln"
-
-#       "Andew Johnson", 
-#       "Ulysses S. Grant",
-#       "Rutherford Hayes",
-#       "James Garfield",
-#       "Chester Alan Arthur",
-#       "Grover Cleveland",
-#       "Benjamin Harrison",
-#       "William McKinley",
-#       "Theodore Roosevelt",
-#       "William Howard Taft",
-#       "Woodrow Wilson",
-#       "Warren Harding",
-#       "Calvin Coolidge",
-#       "Herbert Hoover",
-#       "Franklin D. Roosevelt",
-#       "Harry S. Truman",
-#       "Dwight Eisenhower",
-#       "John F. Kennedy", 
-#       "Lyndon Johnson",
-#       "Richard Nixon",
-#       "Gerald W. Ford",
-#       "Jimmy Carter",
-#       "Ronald Reagan",
-#       "George H. W. Bush",
-#       "Bill Clinton",
-#       "George W. Bush",
-#       "Eleanor Roosevelt"
+       "Abraham Lincoln",
+       "Andew Johnson", 
+       "Ulysses S. Grant",
+       "Rutherford Hayes",
+       "James Garfield",
+       "Chester Alan Arthur",
+       "Grover Cleveland",
+       "Benjamin Harrison",
+       "William McKinley",
+       "Theodore Roosevelt",
+       "William Howard Taft",
+       "Woodrow Wilson",
+       "Warren Harding",
+       "Calvin Coolidge",
+       "Herbert Hoover",
+       "Franklin D. Roosevelt",
+       "Harry S. Truman",
+       "Dwight Eisenhower",
+       "John F. Kennedy", 
+       "Lyndon Johnson",
+       "Richard Nixon",
+       "Gerald W. Ford",
+       "Jimmy Carter",
+       "Ronald Reagan",
+       "George H. W. Bush",
+       "Bill Clinton",
+       "George W. Bush",
+       "Eleanor Roosevelt"
 leader_sex="Male", "Male", "Male", "Male", "Male", 
            "Male", "Male", "Male", "Male", "Male", 
            "Male", "Male", "Male", "Male", "Male", 
-           "Male"#, "Male", "Male", "Male", "Male", 
-           #"Male", "Male", "Male", "Male", "Male", 
-           #"Male", "Male", "Male", "Male", "Male", 
-           #"Male", "Male", "Male", "Male", "Male", 
-           #"Male", "Male", "Male", "Male", "Male", 
-           #"Male", "Male", "Male",
-           #"Female"
+           "Male", "Male", "Male", "Male", "Male", 
+           "Male", "Male", "Male", "Male", "Male", 
+           "Male", "Male", "Male", "Male", "Male", 
+           "Male", "Male", "Male", "Male", "Male", 
+           "Male", "Male", "Male", "Male", "Male", 
+           "Male", "Male", "Female"
 flag="f.usa"
 flag_alt = "-"
 city_style = "European"
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.128
diff -u -u -r1.128 ruleset.c
--- server/ruleset.c    2003/01/03 08:58:48     1.128
+++ server/ruleset.c    2003/01/08 10:10:25
@@ -1752,24 +1752,29 @@
 previous nation, or twice in its own leader name table.
 If not return NULL, if yes return pointer to name which is repeated.
 **************************************************************************/
-static char *check_leader_names(int pos)
+static char *check_leader_names(Nation_Type_id nation)
 {
-  int i, j, k;
-  struct nation_type *n, *nation;
-  char *leader;
-
-  nation = get_nation_by_idx(pos);
-  for( k = 0; k < nation->leader_count; k++) {
-    leader = nation->leader_name[k];
-    for( i=0; i<k; i++ ) {
-      if( 0 == strcmp(leader, nation->leader_name[i]) )
-          return leader;
-    }
-    for( j = 0; j < pos; j++) {
-      n = get_nation_by_idx(j);
-      for( i=0; i < n->leader_count; i++) {
-        if( 0 == strcmp(leader, n->leader_name[i]) )
-          return leader;
+  int k;
+  struct nation_type *pnation = get_nation_by_idx(nation);
+
+  for (k = 0; k < pnation->leader_count; k++) {
+    char *leader = pnation->leaders[k].name;
+    int i;
+    Nation_Type_id nation2;
+
+    for (i = 0; i < k; i++) {
+      if (0 == strcmp(leader, pnation->leaders[i].name)) {
+       return leader;
+      }
+    }
+
+    for (nation2 = 0; nation2 < nation; nation2++) {
+      struct nation_type *pnation2 = get_nation_by_idx(nation2);
+
+      for (i = 0; i < pnation2->leader_count; i++) {
+       if (0 == strcmp(leader, pnation2->leaders[i].name)) {
+         return leader;
+       }
       }
     }
   }
@@ -1994,10 +1999,11 @@
       exit(EXIT_FAILURE);
     }
     pl->leader_count = dim;
+    pl->leaders = fc_malloc(sizeof(*pl->leaders) * pl->leader_count);
     for(j = 0; j < dim; j++) {
-      pl->leader_name[j] = mystrdup(leaders[j]);
+      pl->leaders[j].name = mystrdup(leaders[j]);
       if (check_name(leaders[j])) {
-       pl->leader_name[j][MAX_LEN_NAME - 1] = 0;
+       pl->leaders[j].name[MAX_LEN_NAME - 1] = '\0';
       }
     }
     free(leaders);
@@ -2019,15 +2025,15 @@
     }
     for (j = 0; j < dim; j++) {
       if (0 == mystrcasecmp(leaders[j], "Male")) {
-        pl->leader_is_male[j] = TRUE;
+        pl->leaders[j].is_male = TRUE;
       } else if (0 == mystrcasecmp(leaders[j], "Female")) {
-        pl->leader_is_male[j] = FALSE;
+        pl->leaders[j].is_male = FALSE;
       } else {
         freelog(LOG_ERROR,
                "Nation %s, leader %s: sex must be either Male or Female; "
                "assuming Male",
-               pl->name, pl->leader_name[j]);
-       pl->leader_is_male[j] = TRUE;
+               pl->name, pl->leaders[j].name);
+       pl->leaders[j].is_male = TRUE;
       }
     }
     free(leaders);
@@ -2659,8 +2665,8 @@
     sz_strlcpy(packet.graphic_alt, n->flag_graphic_alt);
     packet.leader_count = n->leader_count;
     for(i=0; i < n->leader_count; i++) {
-      sz_strlcpy(packet.leader_name[i], n->leader_name[i]);
-      packet.leader_sex[i] = n->leader_is_male[i];
+      sz_strlcpy(packet.leader_name[i], n->leaders[i].name);
+      packet.leader_sex[i] = n->leaders[i].is_male;
     }
     packet.city_style = n->city_style;
     memcpy(packet.init_techs, n->init_techs, sizeof(packet.init_techs));

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] Re: (PR#2598) Increase leader limit, Raimar Falke via RT <=