Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2004:
[Freeciv-Dev] (PR#9546) use tileset dirs for darkness
Home

[Freeciv-Dev] (PR#9546) use tileset dirs for darkness

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9546) use tileset dirs for darkness
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 29 Jul 2004 11:50:46 -0700
Reply-to: rt@xxxxxxxxxxx

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

This patch uses tileset dirs in the darkness drawing algorithms.  This 
will work much better in hex tilesets.  The exception is the civ2-style 
special case which only works for iso-rect tilesets.

I did some related cleanups as well.  All the NSEW code was removed 
since this was the last user.  And I renamed cardinal_str as 
cardinal_index_str to match valid_index_str.

It should be pointed out that the darkness code is pretty ugly.

jason

Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.192
diff -u -r1.192 tilespec.c
--- client/tilespec.c   28 Jul 2004 16:45:03 -0000      1.192
+++ client/tilespec.c   29 Jul 2004 18:48:20 -0000
@@ -101,19 +101,19 @@
 /* Darkness style.  Don't reorder this enum since tilesets depend on it. */
 static enum {
   /* No darkness sprites are drawn. */
-  DARKNESS0 = 0,
+  DARKNESS_NONE = 0,
 
   /* 1 sprite that is split into 4 parts and treated as a darkness4.  Only
    * works in iso-view. */
-  DARKNESS1 = 1,
+  DARKNESS_ISORECT = 1,
 
   /* 4 sprites, one per direction.  More than one sprite per tile may be
    * drawn. */
-  DARKNESS4 = 2,
+  DARKNESS_CARD_SINGLE = 2,
 
   /* 15=2^4-1 sprites.  A single sprite is drawn, chosen based on whether
    * there's darkness in _each_ of the cardinal directions. */
-  DARKNESS15 = 3
+  DARKNESS_CARD_FULL = 3
 } darkness_style;
 
 struct specfile;
@@ -841,9 +841,10 @@
   roadstyle = secfile_lookup_int_default(file, is_isometric ? 0 : 1,
                                         "tilespec.roadstyle");
   darkness_style = secfile_lookup_int(file, "tilespec.darkness_style");
-  if (darkness_style < DARKNESS0
-      || darkness_style > DARKNESS15
-      || (darkness_style == DARKNESS1 && !is_isometric)) {
+  if (darkness_style < DARKNESS_NONE
+      || darkness_style > DARKNESS_CARD_FULL
+      || (darkness_style == DARKNESS_ISORECT
+         && (!is_isometric || hex_width > 0 || hex_height > 0))) {
     freelog(LOG_FATAL, _("Invalid darkness style set in tileset."));
     exit(EXIT_FAILURE);
   }
@@ -1076,25 +1077,12 @@
   return NULL;
 }
 
-/**********************************************************************
-  Return string n0s0e0w0 such that INDEX_NSEW(n,s,e,w) = idx
-  The returned string is pointer to static memory.
-***********************************************************************/
-static char *nsew_str(int idx)
-{
-  static char c[9]; /* strlen("n0s0e0w0") + 1 == 9 */
-
-  sprintf(c, "n%ds%de%dw%d", BOOL_VAL(idx&BIT_NORTH), BOOL_VAL(idx&BIT_SOUTH),
-                            BOOL_VAL(idx&BIT_EAST),  BOOL_VAL(idx&BIT_WEST));
-  return c;
-}
-
 /****************************************************************************
   Return a directional string for the cardinal directions.  Normally the
   binary value 1000 will be converted into "n1e0s0w0".  This is in a
   clockwise ordering.
 ****************************************************************************/
-static const char *cardinal_str(int idx)
+static const char *cardinal_index_str(int idx)
 {
   static char c[64];
   int i;
@@ -1438,7 +1426,8 @@
   SET_SPRITE(tx.fog,        "tx.fog");
 
   for (i = 0; i < num_index_cardinal; i++) {
-    my_snprintf(buffer, sizeof(buffer), "tx.s_river_%s", cardinal_str(i));
+    my_snprintf(buffer, sizeof(buffer), "tx.s_river_%s",
+               cardinal_index_str(i));
     SET_SPRITE(tx.spec_river[i], buffer);
   }
 
@@ -1447,19 +1436,20 @@
    * graphics. */
   for (i = 0; i < num_index_cardinal; i++) {
     my_snprintf(buffer, sizeof(buffer), "tx.s_irrigation_%s",
-               cardinal_str(i));
+               cardinal_index_str(i));
     SET_SPRITE_ALT(tx.irrigation[i], buffer, "tx.irrigation");
   }
   for (i = 0; i < num_index_cardinal; i++) {
-    my_snprintf(buffer, sizeof(buffer), "tx.s_farmland_%s", cardinal_str(i));
+    my_snprintf(buffer, sizeof(buffer), "tx.s_farmland_%s",
+               cardinal_index_str(i));
     SET_SPRITE_ALT(tx.farmland[i], buffer, "tx.farmland");
   }
 
   switch (darkness_style) {
-  case DARKNESS0:
+  case DARKNESS_NONE:
     /* Nothing. */
     break;
-  case DARKNESS1:
+  case DARKNESS_ISORECT:
     {
       /* Isometric: take a single tx.darkness tile and split it into 4. */
       struct Sprite *darkness = load_sprite("tx.darkness");
@@ -1473,16 +1463,19 @@
       }
     }
     break;
-  case DARKNESS4:
-    for (i = 0; i < 4; i++) {
+  case DARKNESS_CARD_SINGLE:
+    for (i = 0; i < num_cardinal_tileset_dirs; i++) {
+      enum direction8 dir = cardinal_tileset_dirs[i];
+
       my_snprintf(buffer, sizeof(buffer), "tx.darkness_%s",
-                 dir_get_name(DIR4_TO_DIR8[i]));
+                 dir_get_tileset_name(dir));
       SET_SPRITE(tx.darkness[i], buffer);
     }
     break;
-  case DARKNESS15:
-    for(i = 1; i < NUM_DIRECTION_NSEW; i++) {
-      my_snprintf(buffer, sizeof(buffer), "tx.darkness_%s", nsew_str(i));
+  case DARKNESS_CARD_FULL:
+    for(i = 1; i < num_index_cardinal; i++) {
+      my_snprintf(buffer, sizeof(buffer), "tx.darkness_%s",
+                 cardinal_index_str(i));
       SET_SPRITE(tx.darkness[i], buffer);
     }
     break;
@@ -1628,7 +1621,7 @@
        /* Load 16 cardinally-matched sprites. */
        for (i = 0; i < num_index_cardinal; i++) {
          my_snprintf(buffer1, sizeof(buffer1),
-                     "t.%s_%s", draw->name, cardinal_str(i));
+                     "t.%s_%s", draw->name, cardinal_index_str(i));
          draw->layer[l].match[i] = lookup_sprite_tag_alt(buffer1, "", TRUE,
                                                          "tile_type",
                                                          tt->terrain_name);
@@ -2436,7 +2429,7 @@
   struct tile *ptile = map_get_tile(map_x, map_y);
   enum tile_terrain_type ttype = ptile->terrain;
   struct terrain_drawing_data *draw = sprites.terrain[ttype];
-  int l, dir, adjc_x, adjc_y;
+  int l, adjc_x, adjc_y, i, tileno;
 
   if (!draw_terrain) {
     return 0;
@@ -2459,8 +2452,7 @@
        ? sprites.terrain[ttype_near[(dir)]]->layer[l].match_type : -1)
 
       if (draw->layer[l].cell_type == CELL_SINGLE) {
-       int tileno = 0, i;
-
+       tileno = 0;
        assert(draw->layer[l].match_style == MATCH_BOOLEAN);
        for (i = 0; i < num_cardinal_tileset_dirs; i++) {
          enum direction8 dir = cardinal_tileset_dirs[i];
@@ -2540,44 +2532,46 @@
      * drawn, even in citymode, etc. */
     if (l == 0) {
 #define UNKNOWN(dir)                                        \
-  (MAPSTEP(adjc_x, adjc_y, map_x, map_y, DIR4_TO_DIR8[dir]) \
-   && tile_get_known(adjc_x, adjc_y) == TILE_UNKNOWN)
+      (MAPSTEP(adjc_x, adjc_y, map_x, map_y, (dir))        \
+       && tile_get_known(adjc_x, adjc_y) == TILE_UNKNOWN)
 
       switch (darkness_style) {
-      case DARKNESS0:
+      case DARKNESS_NONE:
        break;
-      case DARKNESS1:
-       for (dir = 0; dir < 4; dir++) {
+      case DARKNESS_ISORECT:
+       for (i = 0; i < 4; i++) {
          const int W = NORMAL_TILE_WIDTH, H = NORMAL_TILE_HEIGHT;
          int offsets[4][2] = {{W / 2, 0}, {0, H / 2}, {W / 2, H / 2}, {0, 0}};
 
-         if (UNKNOWN(dir)) {
-           ADD_SPRITE(sprites.tx.darkness[dir], DRAW_NORMAL, TRUE,
-                      offsets[dir][0], offsets[dir][1]);
+         if (UNKNOWN(DIR4_TO_DIR8[i])) {
+           ADD_SPRITE(sprites.tx.darkness[i], DRAW_NORMAL, TRUE,
+                      offsets[i][0], offsets[i][1]);
          }
        }
        break;
-      case DARKNESS4:
-       for (dir = 0; dir < 4; dir++) {
-         if (UNKNOWN(dir)) {
-           ADD_SPRITE_SIMPLE(sprites.tx.darkness[dir]);
+      case DARKNESS_CARD_SINGLE:
+       for (i = 0; i < num_cardinal_tileset_dirs; i++) {
+         if (UNKNOWN(cardinal_tileset_dirs[i])) {
+           ADD_SPRITE_SIMPLE(sprites.tx.darkness[i]);
          }
        }
        break;
-      case DARKNESS15:
+      case DARKNESS_CARD_FULL:
        /* We're looking to find the INDEX_NSEW for the directions that
         * are unknown.  We want to mark unknown tiles so that an unreal
         * tile will be given the same marking as our current tile - that
         * way we won't get the "unknown" dither along the edge of the
         * map. */
-       {
-         int tileno = INDEX_NSEW(UNKNOWN(DIR4_NORTH), UNKNOWN(DIR4_SOUTH),
-                                 UNKNOWN(DIR4_EAST), UNKNOWN(DIR4_WEST));
-
-         if (tileno != 0) {
-           ADD_SPRITE_SIMPLE(sprites.tx.darkness[tileno]);
+       tileno = 0;
+       for (i = 0; i < num_cardinal_tileset_dirs; i++) {
+         if (UNKNOWN(cardinal_tileset_dirs[i])) {
+           tileno |= 1 << i;
          }
        }
+
+       if (tileno != 0) {
+         ADD_SPRITE_SIMPLE(sprites.tx.darkness[tileno]);
+       }
        break;
       }
 #undef UNKNOWN
Index: client/tilespec.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.h,v
retrieving revision 1.82
diff -u -r1.82 tilespec.h
--- client/tilespec.h   28 Jul 2004 16:45:03 -0000      1.82
+++ client/tilespec.h   29 Jul 2004 18:48:20 -0000
@@ -81,21 +81,10 @@
 
 
 /* This the way directional indices are now encoded: */
-#define NUM_DIRECTION_NSEW             16
 #define MAX_INDEX_CARDINAL             64
 #define MAX_INDEX_HALF                  16
 #define MAX_INDEX_VALID                        256
 
-#define BIT_NORTH (0x01)
-#define BIT_SOUTH (0x02)
-#define BIT_EAST  (0x04)
-#define BIT_WEST  (0x08)
-
-#define INDEX_NSEW(n,s,e,w) (((n) ? BIT_NORTH : 0) | \
-                             ((s) ? BIT_SOUTH : 0) | \
-                             ((e) ? BIT_EAST  : 0) | \
-                             ((w) ? BIT_WEST  : 0))
-
 #define NUM_TILES_PROGRESS 8
 #define NUM_TILES_CITIZEN CITIZEN_LAST
 #define NUM_TILES_HP_BAR 11
@@ -253,7 +242,7 @@
       *fallout,
       *fog,
       *spec_river[MAX_INDEX_CARDINAL],
-      *darkness[NUM_DIRECTION_NSEW],         /* first unused */
+      *darkness[MAX_INDEX_CARDINAL],         /* first unused */
       *river_outlet[4];                /* indexed by enum direction4 */
   } tx;                                /* terrain extra */
 
Index: data/trident/tiles.spec
===================================================================
RCS file: /home/freeciv/CVS/freeciv/data/trident/tiles.spec,v
retrieving revision 1.20
diff -u -r1.20 tiles.spec
--- data/trident/tiles.spec     24 Jul 2004 06:21:12 -0000      1.20
+++ data/trident/tiles.spec     29 Jul 2004 18:48:21 -0000
@@ -289,21 +289,21 @@
 
 ; Darkness (unexplored) to north, south, east, west 
 
- 13,  1, "tx.darkness_n1s0e0w0"
- 13,  2, "tx.darkness_n0s0e1w0"
- 13,  3, "tx.darkness_n1s0e1w0"
- 13,  4, "tx.darkness_n0s1e0w0"
- 13,  5, "tx.darkness_n1s1e0w0"
- 13,  6, "tx.darkness_n0s1e1w0"
- 13,  7, "tx.darkness_n1s1e1w0"
- 13,  8, "tx.darkness_n0s0e0w1"
- 13,  9, "tx.darkness_n1s0e0w1"
- 13, 10, "tx.darkness_n0s0e1w1"
- 13, 11, "tx.darkness_n1s0e1w1"
- 13, 12, "tx.darkness_n0s1e0w1"
- 13, 13, "tx.darkness_n1s1e0w1"
- 13, 14, "tx.darkness_n0s1e1w1"
- 13, 15, "tx.darkness_n1s1e1w1"
+ 13,  1, "tx.darkness_n1e0s0w0"
+ 13,  2, "tx.darkness_n0e1s0w0"
+ 13,  3, "tx.darkness_n1e1s0w0"
+ 13,  4, "tx.darkness_n0e0s1w0"
+ 13,  5, "tx.darkness_n1e0s1w0"
+ 13,  6, "tx.darkness_n0e1s1w0"
+ 13,  7, "tx.darkness_n1e1s1w0"
+ 13,  8, "tx.darkness_n0e0s0w1"
+ 13,  9, "tx.darkness_n1e0s0w1"
+ 13, 10, "tx.darkness_n0e1s0w1"
+ 13, 11, "tx.darkness_n1e1s0w1"
+ 13, 12, "tx.darkness_n0e0s1w1"
+ 13, 13, "tx.darkness_n1e0s1w1"
+ 13, 14, "tx.darkness_n0e1s1w1"
+ 13, 15, "tx.darkness_n1e1s1w1"
 
 ; River outlets, river to north, south, east, west 
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#9546) use tileset dirs for darkness, Jason Short <=