Author: cazfi
Date: Thu Jul  7 11:58:56 2016
New Revision: 33187

URL: http://svn.gna.org/viewcvs/freeciv?rev=33187&view=rev
Log:
Turned tech_class from a id number to a structure.

See patch #7386

Modified:
    trunk/client/helpdata.c
    trunk/client/packhand.c
    trunk/common/fc_types.h
    trunk/common/game.c
    trunk/common/tech.c
    trunk/common/tech.h
    trunk/server/ruleset.c
    trunk/tools/ruleutil/rulesave.c

Modified: trunk/client/helpdata.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/helpdata.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/client/helpdata.c     (original)
+++ trunk/client/helpdata.c     Thu Jul  7 11:58:56 2016
@@ -4641,8 +4641,13 @@
     const struct research *presearch = research_get(pplayer);
 
     if (game.info.tech_classes > 0) {
-      cat_snprintf(buf, bufsz, _("Belongs to tech class %s.\n\n"),
-                   Q_(game.info.tech_class_names[vap->tclass]));
+      if (vap->tclass == NULL) {
+        cat_snprintf(buf, bufsz, _("Belongs to the default tech class 
(%s).\n\n"),
+                     Q_(game.info.tech_class_names[0]));
+      } else {
+        cat_snprintf(buf, bufsz, _("Belongs to tech class %s.\n\n"),
+                     Q_(game.info.tech_class_names[vap->tclass->idx]));
+      }
     }
 
     if (research_invention_state(presearch, i) != TECH_KNOWN) {

Modified: trunk/client/packhand.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/client/packhand.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/client/packhand.c     (original)
+++ trunk/client/packhand.c     Thu Jul  7 11:58:56 2016
@@ -3421,7 +3421,11 @@
   i = 0;
 
   fc_assert(game.info.tech_classes == 0 || p->tclass < game.info.tech_classes);
-  a->tclass = p->tclass;
+  if (p->tclass >= 0) {
+    a->tclass = tech_class_by_number(p->tclass);
+  } else {
+    a->tclass = NULL;
+  }
 
   /* The tech requirements req1 and req2 are send inside research_reqs
    * since they too are required to be fulfilled before the tech can be

Modified: trunk/common/fc_types.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/fc_types.h?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/common/fc_types.h     (original)
+++ trunk/common/fc_types.h     Thu Jul  7 11:58:56 2016
@@ -61,6 +61,7 @@
 #define MAX_NUM_NATIONS MAX_UINT16
 #define MAX_NUM_STARTPOS_NATIONS 1024 /* Used in the network protocol. */
 #define MAX_CALENDAR_FRAGMENTS 52     /* Used in the network protocol. */
+#define MAX_NUM_TECH_CLASSES   16     /* Used in the network protocol. */
 
 /* Changing these will probably break network compatability. */
 #define MAX_LEN_NAME     48

Modified: trunk/common/game.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/game.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/common/game.c (original)
+++ trunk/common/game.c Thu Jul  7 11:58:56 2016
@@ -542,6 +542,7 @@
   user_unit_type_flags_init();
   user_terrain_flags_init();
   user_extra_flags_init();
+  tech_classes_init();
   user_tech_flags_init();
   multipliers_init();
 

Modified: trunk/common/tech.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/tech.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/common/tech.c (original)
+++ trunk/common/tech.c Thu Jul  7 11:58:56 2016
@@ -48,6 +48,8 @@
  * client/packhand.c (for the client)
  */
 struct advance advances[A_ARRAY_SIZE];
+
+struct tech_class tech_classes[MAX_NUM_TECH_CLASSES];
 
 static struct user_flag user_tech_flags[MAX_NUM_USER_TECH_FLAGS];
 
@@ -276,6 +278,68 @@
 }
 
 /**************************************************************************
+  Initialize tech classes
+**************************************************************************/
+void tech_classes_init(void)
+{
+  int i;
+
+  for (i = 0; i < MAX_NUM_TECH_CLASSES; i++) {
+    tech_classes[i].idx = i;
+  }
+}
+
+/**************************************************************************
+  Return the tech_class for the given index.
+**************************************************************************/
+struct tech_class *tech_class_by_number(const int idx)
+{
+  if (idx < 0 || idx >= game.info.tech_classes) {
+    return NULL;
+  }
+
+  return &tech_classes[idx];
+}
+
+/**************************************************************************
+  Return the (translated) name of the given tech_class
+  You must not free the return pointer.
+**************************************************************************/
+const char *tech_class_name_translation(const struct tech_class *ptclass)
+{
+  return name_translation_get(&ptclass->name);
+}
+
+/****************************************************************************
+  Return the (untranslated) rule name of tech_class
+  You must not free the return pointer.
+****************************************************************************/
+const char *tech_class_rule_name(const struct tech_class *ptclass)
+{
+  return rule_name_get(&ptclass->name);
+}
+
+/**************************************************************************
+ Does a linear search of tech_classes[].name.vernacular
+ Returns NULL when none match.
+**************************************************************************/
+struct tech_class *tech_class_by_rule_name(const char *name)
+{
+  const char *qname = Qn_(name);
+  int i;
+
+  for (i = 0; i < game.info.tech_classes; i++) {
+    struct tech_class *ptclass = tech_class_by_number(i);
+
+    if (!fc_strcasecmp(tech_class_rule_name(ptclass), qname)) {
+      return ptclass;
+    }
+  }
+
+  return NULL;
+}
+
+/**************************************************************************
   Initialize user tech flags.
 **************************************************************************/
 void user_tech_flags_init(void)

Modified: trunk/common/tech.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/tech.h?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/common/tech.h (original)
+++ trunk/common/tech.h Thu Jul  7 11:58:56 2016
@@ -114,12 +114,17 @@
   AR_SIZE
 };
 
+struct tech_class {
+  int idx;
+  struct name_translation name;
+};
+
 struct advance {
   Tech_type_id item_number;
   struct name_translation name;
   char graphic_str[MAX_LEN_NAME];      /* which named sprite to use */
   char graphic_alt[MAX_LEN_NAME];      /* alternate icon name */
-  int tclass;
+  struct tech_class *tclass;
 
   struct advance *require[AR_SIZE];
 
@@ -165,6 +170,12 @@
 const char *advance_rule_name(const struct advance *padvance);
 const char *advance_name_translation(const struct advance *padvance);
 
+void tech_classes_init(void);
+struct tech_class *tech_class_by_number(const int idx);
+const char *tech_class_name_translation(const struct tech_class *ptclass);
+const char *tech_class_rule_name(const struct tech_class *ptclass);
+struct tech_class *tech_class_by_rule_name(const char *name);
+
 void user_tech_flags_init(void);
 void user_tech_flags_free(void);
 void set_user_tech_flag_name(enum tech_flag_id id, const char *name, const 
char *helptxt);

Modified: trunk/server/ruleset.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/ruleset.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/server/ruleset.c      (original)
+++ trunk/server/ruleset.c      Thu Jul  7 11:58:56 2016
@@ -1198,7 +1198,7 @@
   } else {
     game.info.tech_classes = nval;
     for (i = 0; i < nval; i++) {
-      strncpy(game.info.tech_class_names[i], slist[i], MAX_LEN_NAME - 1);
+      names_set(&(tech_class_by_number(i)->name), "freeciv", slist[i], 
slist[i]);
     }
     free(slist);
   }
@@ -1237,29 +1237,22 @@
     }
 
     if (game.info.tech_classes == 0) {
-      a->tclass = 0;
+      a->tclass = NULL;
     } else {
       const char *classname;
 
       classname = lookup_string(file, sec_name, "class");
       if (classname != NULL) {
-        int ci;
-
         classname = Q_(classname);
-        for (ci = 0; ci < game.info.tech_classes; ci++) {
-          if (!strcasecmp(classname, game.info.tech_class_names[ci])) {
-            a->tclass = ci;
-            break;
-          }
-        }
-        if (ci == game.info.tech_classes) {
+        a->tclass = tech_class_by_rule_name(classname);
+        if (a->tclass == NULL) {
           ruleset_error(LOG_ERROR, "\"%s\" [%s] \"%s\": Uknown tech class 
\"%s\".",
                         filename, sec_name, rule_name_get(&a->name), 
classname);
           ok = FALSE;
           break;
         }
       } else {
-        a->tclass = 0; /* Default */
+        a->tclass = NULL; /* Default */
       }
     }
 
@@ -6724,7 +6717,11 @@
   advance_iterate(A_FIRST, a) {
     packet.id = advance_number(a);
     packet.removed = !valid_advance(a);
-    packet.tclass = a->tclass;
+    if (a->tclass == NULL) {
+      packet.tclass = -1;
+    } else {
+      packet.tclass = a->tclass->idx;
+    }
     sz_strlcpy(packet.name, untranslated_name(&a->name));
     sz_strlcpy(packet.rule_name, rule_name_get(&a->name));
     sz_strlcpy(packet.graphic_str, a->graphic_str);

Modified: trunk/tools/ruleutil/rulesave.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/tools/ruleutil/rulesave.c?rev=33187&r1=33186&r2=33187&view=diff
==============================================================================
--- trunk/tools/ruleutil/rulesave.c     (original)
+++ trunk/tools/ruleutil/rulesave.c     Thu Jul  7 11:58:56 2016
@@ -1770,8 +1770,10 @@
       save_name_translation(sfile, &(pa->name), path);
 
       if (game.info.tech_classes > 0) {
-        secfile_insert_str(sfile, game.info.tech_class_names[pa->tclass],
-                           "%s.class", path);
+        if (pa->tclass != NULL) {
+          secfile_insert_str(sfile, tech_class_rule_name(pa->tclass),
+                             "%s.class", path);
+        }
       }
 
       save_tech_ref(sfile, pa->require[AR_ONE], path, "req1");


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

Reply via email to