Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2004:
[Freeciv-Dev] (PR#9759) terrain "identifiers" for savegames
Home

[Freeciv-Dev] (PR#9759) terrain "identifiers" for savegames

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9759) terrain "identifiers" for savegames
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 21 Aug 2004 17:22:50 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9759 >

In savegame.c there is a string terrain_chars that holds the savegame 
identifiers for each terrain.

Obviously for the terrain types to be entirely configurable in the 
ruleset, this value needs to come from the ruleset as well.  This patch 
does that.  A modest hack is needed to handle T_UNKNOWN which is stored 
in the savegame but doesn't come from the ruleset.

See also PR#6007.

jason

Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.204
diff -u -r1.204 map.h
--- common/map.h        13 Aug 2004 15:59:12 -0000      1.204
+++ common/map.h        22 Aug 2004 00:16:34 -0000
@@ -72,6 +72,10 @@
   char graphic_str[MAX_LEN_NAME];
   char graphic_alt[MAX_LEN_NAME];
 
+  /* Server-only. */
+  char identifier; /* Single-character identifier used in savegames. */
+#define UNKNOWN_TERRAIN_IDENTIFIER 'u'
+
   int movement_cost;
   int defense_bonus;
 
Index: data/civ1/terrain.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/civ1/terrain.ruleset,v
retrieving revision 1.25
diff -u -r1.25 terrain.ruleset
--- data/civ1/terrain.ruleset   9 Jul 2004 17:33:55 -0000       1.25
+++ data/civ1/terrain.ruleset   22 Aug 2004 00:16:34 -0000
@@ -158,6 +158,7 @@
 terrain_name         = _("Arctic")
 graphic              = "arctic"
 graphic_alt         = "-"
+identifier          = "a"
 movement_cost        = 2
 defense_bonus        = 10
 food                 = 0
@@ -196,6 +197,7 @@
 terrain_name         = _("Desert")
 graphic              = "desert"
 graphic_alt         = "-"
+identifier          = "d"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 0
@@ -232,6 +234,7 @@
 terrain_name         = _("Forest")
 graphic              = "forest"
 graphic_alt         = "-"
+identifier          = "f"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -268,6 +271,7 @@
 terrain_name         = _("Grassland")
 graphic              = "grassland"
 graphic_alt         = "-"
+identifier          = "g"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 2
@@ -304,6 +308,7 @@
 terrain_name         = _("Hills")
 graphic              = "hills"
 graphic_alt         = "-"
+identifier          = "h"
 movement_cost        = 2
 defense_bonus        = 20
 food                 = 1
@@ -340,6 +345,7 @@
 terrain_name         = _("Jungle")
 graphic              = "jungle"
 graphic_alt         = "-"
+identifier          = "j"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -376,6 +382,7 @@
 terrain_name         = _("Mountains")
 graphic              = "mountains"
 graphic_alt         = "-"
+identifier          = "m"
 movement_cost        = 3
 defense_bonus        = 30
 food                 = 0
@@ -412,6 +419,7 @@
 terrain_name         = _("Ocean")
 graphic              = "ocean"
 graphic_alt         = "-"
+identifier          = " "
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -451,6 +459,7 @@
 terrain_name         = _("Plains")
 graphic              = "plains"
 graphic_alt         = "-"
+identifier          = "p"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -488,6 +497,7 @@
 terrain_name         = "unused"
 graphic              = "-"
 graphic_alt         = "-"
+identifier          = "r"
 movement_cost        = 0
 defense_bonus        = 00
 food                 = 0
@@ -521,6 +531,7 @@
 terrain_name         = _("Swamp")
 graphic              = "swamp"
 graphic_alt         = "-"
+identifier          = "s"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -557,6 +568,7 @@
 terrain_name         = _("Tundra")
 graphic              = "tundra"
 graphic_alt         = "-"
+identifier          = "t"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
Index: data/civ2/terrain.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/civ2/terrain.ruleset,v
retrieving revision 1.27
diff -u -r1.27 terrain.ruleset
--- data/civ2/terrain.ruleset   9 Jul 2004 17:33:55 -0000       1.27
+++ data/civ2/terrain.ruleset   22 Aug 2004 00:16:34 -0000
@@ -166,6 +166,7 @@
 terrain_name         = _("Glacier")
 graphic              = "arctic"
 graphic_alt         = "-"
+identifier          = "a"
 movement_cost        = 2
 defense_bonus        = 10
 food                 = 0
@@ -204,6 +205,7 @@
 terrain_name         = _("Desert")
 graphic              = "desert"
 graphic_alt         = "-"
+identifier          = "d"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 0
@@ -241,6 +243,7 @@
 terrain_name         = _("Forest")
 graphic              = "forest"
 graphic_alt         = "-"
+identifier          = "f"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -278,6 +281,7 @@
 terrain_name         = _("Grassland")
 graphic              = "grassland"
 graphic_alt         = "-"
+identifier          = "g"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 2
@@ -314,6 +318,7 @@
 terrain_name         = _("Hills")
 graphic              = "hills"
 graphic_alt         = "-"
+identifier          = "h"
 movement_cost        = 2
 defense_bonus        = 20
 food                 = 1
@@ -351,6 +356,7 @@
 terrain_name         = _("Jungle")
 graphic              = "jungle"
 graphic_alt         = "-"
+identifier          = "j"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -388,6 +394,7 @@
 terrain_name         = _("Mountains")
 graphic              = "mountains"
 graphic_alt         = "-"
+identifier          = "m"
 movement_cost        = 3
 defense_bonus        = 30
 food                 = 0
@@ -425,6 +432,7 @@
 terrain_name         = _("Ocean")
 graphic              = "ocean"
 graphic_alt         = "-"
+identifier          = " "
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -464,6 +472,7 @@
 terrain_name         = _("Plains")
 graphic              = "plains"
 graphic_alt         = "-"
+identifier          = "p"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -501,6 +510,7 @@
 terrain_name         = "unused"
 graphic              = "-"
 graphic_alt         = "-"
+identifier          = "r"
 movement_cost        = 0
 defense_bonus        = 00
 food                 = 0
@@ -533,6 +543,7 @@
 terrain_name         = _("Swamp")
 graphic              = "swamp"
 graphic_alt         = "-"
+identifier          = "s"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -570,6 +581,7 @@
 terrain_name         = _("Tundra")
 graphic              = "tundra"
 graphic_alt         = "-"
+identifier          = "t"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
Index: data/default/terrain.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/default/terrain.ruleset,v
retrieving revision 1.28
diff -u -r1.28 terrain.ruleset
--- data/default/terrain.ruleset        9 Jul 2004 17:33:55 -0000       1.28
+++ data/default/terrain.ruleset        22 Aug 2004 00:16:34 -0000
@@ -109,6 +109,9 @@
 ; graphic_alt          = alternate graphics tag if preferred is not found;
 ;                        should be a standard tag if preferred is not;
 ;                       otherwise may be "-"
+; identifier           = single-character identifier used in savegames.  This
+;                        must be unique for each terrain, and changing it will
+;                        break savegame compatibility.
 ; movement_cost        = typically 1 to 3
 ; defense_bonus        = 10 times actual; typically 10 to 30 (1.0 to 3.0)
 ; food                 = normal food production
@@ -166,6 +169,7 @@
 terrain_name         = _("Glacier")
 graphic              = "arctic"
 graphic_alt         = "-"
+identifier          = "a"
 movement_cost        = 2
 defense_bonus        = 10
 food                 = 0
@@ -209,6 +213,7 @@
 terrain_name         = _("Desert")
 graphic              = "desert"
 graphic_alt         = "-"
+identifier          = "d"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 0
@@ -251,6 +256,7 @@
 terrain_name         = _("Forest")
 graphic              = "forest"
 graphic_alt         = "-"
+identifier          = "f"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -293,6 +299,7 @@
 terrain_name         = _("Grassland")
 graphic              = "grassland"
 graphic_alt         = "-"
+identifier          = "g"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 2
@@ -334,6 +341,7 @@
 terrain_name         = _("Hills")
 graphic              = "hills"
 graphic_alt         = "-"
+identifier          = "h"
 movement_cost        = 2
 defense_bonus        = 20
 food                 = 1
@@ -376,6 +384,7 @@
 terrain_name         = _("Jungle")
 graphic              = "jungle"
 graphic_alt         = "-"
+identifier          = "j"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -418,6 +427,7 @@
 terrain_name         = _("Mountains")
 graphic              = "mountains"
 graphic_alt         = "-"
+identifier          = "m"
 movement_cost        = 3
 defense_bonus        = 30
 food                 = 0
@@ -460,6 +470,7 @@
 terrain_name         = _("Ocean")
 graphic              = "ocean"
 graphic_alt         = "-"
+identifier          = " "
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -504,6 +515,7 @@
 terrain_name         = _("Plains")
 graphic              = "plains"
 graphic_alt         = "-"
+identifier          = "p"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -546,6 +558,7 @@
 terrain_name         = "unused"
 graphic              = "-"
 graphic_alt         = "-"
+identifier          = "r"
 movement_cost        = 0
 defense_bonus        = 00
 food                 = 0
@@ -583,6 +596,7 @@
 terrain_name         = _("Swamp")
 graphic              = "swamp"
 graphic_alt         = "-"
+identifier          = "s"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -625,6 +639,7 @@
 terrain_name         = _("Tundra")
 graphic              = "tundra"
 graphic_alt         = "-"
+identifier          = "t"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
Index: data/history/terrain.ruleset
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/history/terrain.ruleset,v
retrieving revision 1.10
diff -u -r1.10 terrain.ruleset
--- data/history/terrain.ruleset        9 Jul 2004 17:33:55 -0000       1.10
+++ data/history/terrain.ruleset        22 Aug 2004 00:16:35 -0000
@@ -166,6 +166,7 @@
 terrain_name         = _("Glacier")
 graphic              = "arctic"
 graphic_alt         = "-"
+identifier          = "a"
 movement_cost        = 2
 defense_bonus        = 10
 food                 = 0
@@ -204,6 +205,7 @@
 terrain_name         = _("Desert")
 graphic              = "desert"
 graphic_alt         = "-"
+identifier          = "d"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 0
@@ -241,6 +243,7 @@
 terrain_name         = _("Forest")
 graphic              = "forest"
 graphic_alt         = "-"
+identifier          = "f"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -278,6 +281,7 @@
 terrain_name         = _("Grassland")
 graphic              = "grassland"
 graphic_alt         = "-"
+identifier          = "g"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 2
@@ -314,6 +318,7 @@
 terrain_name         = _("Hills")
 graphic              = "hills"
 graphic_alt         = "-"
+identifier          = "h"
 movement_cost        = 2
 defense_bonus        = 20
 food                 = 1
@@ -351,6 +356,7 @@
 terrain_name         = _("Jungle")
 graphic              = "jungle"
 graphic_alt         = "-"
+identifier          = "j"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -388,6 +394,7 @@
 terrain_name         = _("Mountains")
 graphic              = "mountains"
 graphic_alt         = "-"
+identifier          = "m"
 movement_cost        = 3
 defense_bonus        = 30
 food                 = 0
@@ -425,6 +432,7 @@
 terrain_name         = _("Ocean")
 graphic              = "ocean"
 graphic_alt         = "-"
+identifier          = " "
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -464,6 +472,7 @@
 terrain_name         = _("Plains")
 graphic              = "plains"
 graphic_alt         = "-"
+identifier          = "p"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
@@ -501,6 +510,7 @@
 terrain_name         = "unused"
 graphic              = "-"
 graphic_alt         = "-"
+identifier          = "r"
 movement_cost        = 0
 defense_bonus        = 00
 food                 = 0
@@ -533,6 +543,7 @@
 terrain_name         = _("Swamp")
 graphic              = "swamp"
 graphic_alt         = "-"
+identifier          = "s"
 movement_cost        = 2
 defense_bonus        = 15
 food                 = 1
@@ -569,6 +580,7 @@
 terrain_name         = _("Tundra")
 graphic              = "tundra"
 graphic_alt         = "-"
+identifier          = "t"
 movement_cost        = 1
 defense_bonus        = 10
 food                 = 1
Index: server/ruleset.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/ruleset.c,v
retrieving revision 1.186
diff -u -r1.186 ruleset.c
--- server/ruleset.c    20 Aug 2004 20:13:18 -0000      1.186
+++ server/ruleset.c    22 Aug 2004 00:16:35 -0000
@@ -1598,6 +1598,23 @@
       sz_strlcpy(t->graphic_alt,
                 secfile_lookup_str(file,"%s.graphic_alt", sec[i]));
 
+      t->identifier = secfile_lookup_str(file, "%s.identifier", sec[i])[0];
+      for (j = T_FIRST; j < i; j++) {
+       if (t->identifier == tile_types[j].identifier) {
+         /* TRANS: message for an obscure ruleset error. */
+         freelog(LOG_FATAL,
+                 _("Terrains %s and %s have the same identifier."),
+                 t->terrain_name, tile_types[j].terrain_name);
+         exit(EXIT_FAILURE);
+       }
+      }
+      if (t->identifier == UNKNOWN_TERRAIN_IDENTIFIER) {
+       /* TRANS: message for an obscure ruleset error. */
+       freelog(LOG_FATAL, _("'%c' cannot be used as a terrain identifier; "
+                            "it is reserved."), UNKNOWN_TERRAIN_IDENTIFIER);
+       exit(EXIT_FAILURE);
+      }
+
       t->movement_cost = secfile_lookup_int(file, "%s.movement_cost", sec[i]);
       t->defense_bonus = secfile_lookup_int(file, "%s.defense_bonus", sec[i]);
 
Index: server/savegame.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/savegame.c,v
retrieving revision 1.176
diff -u -r1.176 savegame.c
--- server/savegame.c   18 Aug 2004 18:11:05 -0000      1.176
+++ server/savegame.c   22 Aug 2004 00:16:36 -0000
@@ -180,7 +180,6 @@
 "startunits turn_last_built improvement_order technology_order"
 
 static const char hex_chars[] = "0123456789abcdef";
-static const char terrain_chars[] = "adfghjm prstu";
 
 /***************************************************************
 This returns an ascii hex value of the given half-byte of the binary
@@ -219,17 +218,36 @@
 }
 
 /***************************************************************
-Dereferences the terrain character.  See terrain_chars[].
-  example: char2terrain('a') == 0
+Dereferences the terrain character.  See tile_types[].identifier
+  example: char2terrain('a') => T_ARCTIC
 ***************************************************************/
-static int char2terrain(char ch)
+static enum tile_terrain_type char2terrain(char ch)
 {
-  char *pch = strchr(terrain_chars, ch);
+  if (ch == UNKNOWN_TERRAIN_IDENTIFIER) {
+    return T_UNKNOWN;
+  }
+  terrain_type_iterate(id) {
+    if (tile_types[id].identifier == ch) {
+      return id;
+    }
+  } terrain_type_iterate_end;
 
-  if (!pch || ch == '\0') {
-    die("Unknown terrain type: '%c' %d", ch, ch);
+  /* TRANS: message for an obscure savegame error. */
+  freelog(LOG_FATAL, _("Unknown terrain identifier '%c' in savegame."), ch);
+  exit(EXIT_FAILURE);
+}
+
+/***************************************************************
+References the terrain character.  See tile_types[].identifier
+  example: terrain2char(T_ARCTIC) => 'a'
+***************************************************************/
+static char terrain2char(enum tile_terrain_type terr)
+{
+  if (terr == T_UNKNOWN) {
+    return UNKNOWN_TERRAIN_IDENTIFIER;
+  } else {
+    return tile_types[terr].identifier;
   }
-  return pch - terrain_chars;
 }
 
 /***************************************************************
@@ -504,7 +522,7 @@
     
   /* put the terrain type */
   SAVE_NORMAL_MAP_DATA(x, y, file, "map.t%03d",
-                      terrain_chars[map_get_tile(x, y)->terrain]);
+                      terrain2char(map_get_tile(x, y)->terrain));
 
   if (!map.have_specials) {
     if (map.have_rivers_overlay) {
@@ -2555,8 +2573,8 @@
 
     /* put the terrain type */
     SAVE_PLAYER_MAP_DATA(x, y, file,"player%d.map_t%03d", plrno, 
-                        terrain_chars[map_get_player_tile
-                                      (x, y, plr)->terrain]);
+                        terrain2char(map_get_player_tile
+                                     (x, y, plr)->terrain));
 
     /* put 4-bit segments of 12-bit "special flags" field */
     SAVE_PLAYER_MAP_DATA(x, y, file,"player%d.map_l%03d", plrno,

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