Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] (PR#12766) add index values to tech/building/nation/unitty
Home

[Freeciv-Dev] (PR#12766) add index values to tech/building/nation/unitty

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12766) add index values to tech/building/nation/unittype structs
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 10 Apr 2005 22:51:23 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12766 >

This patch adds an index value to the tech, building, nation, and 
unit_type structures.  This corresponds to ptile->index or 
pgovernment->index.  Note units and cities and players have an "id" or 
"player_no" value rather than an "index" (maybe this should be changed...).

The reason for doing this is to allow passing of pointers in some 
functions.  Maybe we can eventually pass pointers in all functions (this 
should be discussed first - we don't really want mixed pointer/integer 
issues like there supposedly were with players for a while), but for the 
moment I just want to do it to the tilespec functions.  These have to 
know the index so they can do their own lookup, so right now it's 
*impossible* to pass the struct pointer.

I used the same stupid const tricks here that I used in map.c.  Take a 
look; if this is too much of a hack we can remove it.  Note the 
government, city, unit, and player structs don't have const.

-jason

Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.204
diff -u -r1.204 game.c
--- common/game.c       10 Apr 2005 23:55:24 -0000      1.204
+++ common/game.c       11 Apr 2005 05:50:20 -0000
@@ -279,6 +279,9 @@
 
   init_our_capability();    
   map_init();
+  improvements_init();
+  techs_init();
+  unit_types_init();
   idex_init();
   cm_init();
   
Index: common/improvement.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/improvement.c,v
retrieving revision 1.52
diff -u -r1.52 improvement.c
--- common/improvement.c        9 Mar 2005 18:49:06 -0000       1.52
+++ common/improvement.c        11 Apr 2005 05:50:20 -0000
@@ -100,6 +100,21 @@
   return i;
 }
 
+/****************************************************************************
+  Inialize building structures.
+****************************************************************************/
+void improvements_init(void)
+{
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(improvement_types); i++) {
+    /* HACK: this field is declared const to keep anyone from changing
+     * them.  But we have to set it somewhere!  This should be the only
+     * place. */
+    *(int *)&improvement_types[i].index = i;
+  }
+}
+
 /**************************************************************************
   Frees the memory associated with this improvement.
 **************************************************************************/
Index: common/improvement.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/improvement.h,v
retrieving revision 1.41
diff -u -r1.41 improvement.h
--- common/improvement.h        6 Apr 2005 17:36:15 -0000       1.41
+++ common/improvement.h        11 Apr 2005 05:50:20 -0000
@@ -65,6 +65,7 @@
 
 /* Type of improvement. (Read from buildings.ruleset file.) */
 struct impr_type {
+  const int index;  /* Index in improvement_types array */
   enum impr_genus_id genus;            /* genus; e.g. GreatWonder */
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];                /* untranslated */
@@ -95,6 +96,7 @@
 enum impr_genus_id impr_genus_from_str(const char *s);
 
 /* improvement functions */
+void improvements_init(void);
 void improvements_free(void);
 struct impr_type *get_improvement_type(Impr_Type_id id);
 bool improvement_exists(Impr_Type_id id);
Index: common/nation.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.c,v
retrieving revision 1.44
diff -u -r1.44 nation.c
--- common/nation.c     27 Mar 2005 09:17:16 -0000      1.44
+++ common/nation.c     11 Apr 2005 05:50:20 -0000
@@ -206,8 +206,17 @@
 ***************************************************************/
 void nations_alloc(int num)
 {
+  int i;
+
   nations = (struct nation_type *)fc_calloc(num, sizeof(struct nation_type));
   game.nation_count = num;
+
+  for (i = 0; i < num; i++) {
+    /* HACK: this field is declared const to keep anyone from changing
+     * them.  But we have to set it somewhere!  This should be the only
+     * place. */
+    *(int *)&nations[i].index = i;
+  }
 }
 
 /***************************************************************
Index: common/nation.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/nation.h,v
retrieving revision 1.39
diff -u -r1.39 nation.h
--- common/nation.h     29 Mar 2005 12:28:26 -0000      1.39
+++ common/nation.h     11 Apr 2005 05:50:20 -0000
@@ -76,6 +76,7 @@
 };
 
 struct nation_type {
+  const int index;
   /* Pointer values are allocated on load then freed in free_nations(). */
   const char *name; /* Translated string - doesn't need freeing. */
   const char *name_plural; /* Translated string - doesn't need freeing. */
Index: common/tech.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tech.c,v
retrieving revision 1.86
diff -u -r1.86 tech.c
--- common/tech.c       31 Mar 2005 17:37:57 -0000      1.86
+++ common/tech.c       11 Apr 2005 05:50:21 -0000
@@ -613,6 +613,21 @@
          && game.rgame.tech_leakage == 0);
 }
 
+/****************************************************************************
+  Inialize tech structures.
+****************************************************************************/
+void techs_init(void)
+{
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(advances); i++) {
+    /* HACK: this field is declared const to keep anyone from changing
+     * them.  But we have to set it somewhere!  This should be the only
+     * place. */
+    *(int *)&advances[i].index = i;
+  }
+}
+
 /***************************************************************
  De-allocate resources associated with the given tech.
 ***************************************************************/
Index: common/tech.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/tech.h,v
retrieving revision 1.52
diff -u -r1.52 tech.h
--- common/tech.h       30 Mar 2005 23:06:52 -0000      1.52
+++ common/tech.h       11 Apr 2005 05:50:21 -0000
@@ -79,6 +79,7 @@
 };
 
 struct advance {
+  const int index; /* Tech index in tech array. */
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];              /* untranslated */
   char graphic_str[MAX_LEN_NAME];      /* which named sprite to use */
@@ -140,6 +141,7 @@
 
 void precalc_tech_data(void);
 
+void techs_init(void);
 void techs_free(void);
 
 extern struct advance advances[];
Index: common/unittype.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.c,v
retrieving revision 1.51
diff -u -r1.51 unittype.c
--- common/unittype.c   4 Apr 2005 21:45:48 -0000       1.51
+++ common/unittype.c   11 Apr 2005 05:50:21 -0000
@@ -629,6 +629,21 @@
   return U_LAST;
 }
 
+/****************************************************************************
+  Inialize unit-type structures.
+****************************************************************************/
+void unit_types_init(void)
+{
+  int i;
+
+  for (i = 0; i < ARRAY_SIZE(unit_types); i++) {
+    /* HACK: this field is declared const to keep anyone from changing
+     * them.  But we have to set it somewhere!  This should be the only
+     * place. */
+    *(int *)&unit_types[i].index = i;
+  }
+}
+
 /**************************************************************************
   Frees the memory associated with this unit type.
 **************************************************************************/
Index: common/unittype.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/unittype.h,v
retrieving revision 1.44
diff -u -r1.44 unittype.h
--- common/unittype.h   10 Apr 2005 01:59:31 -0000      1.44
+++ common/unittype.h   11 Apr 2005 05:50:21 -0000
@@ -169,6 +169,7 @@
 };
 
 struct unit_type {
+  const int index;
   const char *name; /* Translated string - doesn't need freeing. */
   char name_orig[MAX_LEN_NAME];              /* untranslated */
   char graphic_str[MAX_LEN_NAME];
@@ -265,6 +266,7 @@
 Unit_Type_id first_role_unit_for_player(const struct player *pplayer,
                                        int role);
 
+void unit_types_init(void);
 void unit_types_free(void);
 
 #define unit_type_iterate(m_i)                                                \

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12766) add index values to tech/building/nation/unittype structs, Jason Short <=