From b6d1daba6ef7f0d4ca9188800e761c3e8a4a19b8 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 16 Nov 2024 01:29:40 +0200
Subject: [PATCH 63/75] Animate "3Layer" extras foreground

See RM #1123

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/tilespec.c             | 37 +++++++++++++++++++++++------------
 data/alio/fortresses.spec     |  4 ++--
 data/amplio/moderncities.spec |  4 ++--
 data/amplio2/bases.spec       |  8 ++++----
 data/hex2t/items.spec         |  4 ++--
 data/hexemplio/bases.spec     |  4 ++--
 data/isotrident/cities.spec   |  4 ++--
 7 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/client/tilespec.c b/client/tilespec.c
index 2ccc78b92d..c27e69c014 100644
--- a/client/tilespec.c
+++ b/client/tilespec.c
@@ -371,7 +371,8 @@ struct named_sprites {
       struct {
         struct sprite
           *background,
-          *middleground,
+          *middleground;
+        struct anim
           *foreground;
       } bmf;
       struct {
@@ -4292,12 +4293,11 @@ static void tileset_setup_base(struct tileset *t,
 
   sz_strlcpy(full_tag_name, tag);
   strcat(full_tag_name, "_fg");
-  t->sprites.extras[id].u.bmf.foreground = load_sprite(t, full_tag_name,
-                                                       TRUE, TRUE, FALSE);
+  t->sprites.extras[id].u.bmf.foreground = anim_load(t, full_tag_name, 0);
 
-  if (t->sprites.extras[id].u.bmf.background == NULL
-      && t->sprites.extras[id].u.bmf.middleground == NULL
-      && t->sprites.extras[id].u.bmf.foreground == NULL) {
+  if (t->sprites.extras[id].u.bmf.background == nullptr
+      && t->sprites.extras[id].u.bmf.middleground == nullptr
+      && t->sprites.extras[id].u.bmf.foreground == nullptr) {
     /* There was an extra style definition but no matching graphics */
     tileset_error(LOG_FATAL, tileset_name_get(t),
                   _("No graphics with tag \"%s_bg/mg/fg\" for extra \"%s\""),
@@ -4664,6 +4664,13 @@ static struct sprite *get_unit_nation_flag_sprite(const struct tileset *t,
   ADD_SPRITE(anim_get_current_frame(s), draw_fog, x_offset, y_offset)
 #define ADD_ANIM_SPRITE_SIMPLE(s)                                           \
   ADD_SPRITE(anim_get_current_frame(s), TRUE, 0, 0)
+#define ADD_ANIM_FULL(s)                                                    \
+  ADD_SPRITE(anim_get_current_frame(s), TRUE,                               \
+             FULL_TILE_X_OFFSET, FULL_TILE_Y_OFFSET)
+
+#define ADD_FRAME0_SIMPLE(s) ADD_SPRITE_SIMPLE(s->sprites[0])
+#define ADD_FRAME0_FULL(s)                                                  \
+  ADD_SPRITE_FULL(s->sprites[0])
 
 /************************************************************************//**
   Assemble some data that is used in building the tile sprite arrays.
@@ -4701,8 +4708,6 @@ static void build_tile_data(const struct tile *ptile,
   }
 }
 
-#define ADD_FRAME0_SIMPLE(s) ADD_SPRITE_SIMPLE(s->sprites[0])
-
 /************************************************************************//**
   Fill in the sprite array for the unit type.
 ****************************************************************************/
@@ -6393,7 +6398,7 @@ int fill_sprite_array(struct tileset *t,
         extra_type_list_iterate(t->style_lists[ESTYLE_3LAYER], pextra) {
           if (is_extra_drawing_enabled(pextra)
               && tile_has_extra(ptile, pextra)
-              && t->sprites.extras[extra_index(pextra)].u.bmf.foreground) {
+              && t->sprites.extras[extra_index(pextra)].u.bmf.foreground != nullptr) {
             bool hidden = FALSE;
 
             extra_type_list_iterate(pextra->hiders, phider) {
@@ -6404,8 +6409,8 @@ int fill_sprite_array(struct tileset *t,
             } extra_type_list_iterate_end;
 
             if (!hidden) {
-              if (t->sprites.extras[extra_index(pextra)].u.bmf.foreground) {
-                ADD_SPRITE_FULL(t->sprites.extras[extra_index(pextra)].u.bmf.foreground);
+              if (t->sprites.extras[extra_index(pextra)].u.bmf.foreground != nullptr) {
+                ADD_ANIM_FULL(t->sprites.extras[extra_index(pextra)].u.bmf.foreground);
               }
             }
           }
@@ -7495,12 +7500,19 @@ int fill_basic_base_sprite_array(const struct tileset *t,
   }\
 } while (FALSE)
 
+#define ADD_FRAME0_IF_NOT_NULL(x) do {\
+  if ((x) != nullptr) {\
+    ADD_FRAME0_FULL(x);\
+  }\
+} while (FALSE)
+
   /* Corresponds to LAYER_SPECIAL{1,2,3} order. */
   ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.background);
   ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.middleground);
-  ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.foreground);
+  ADD_FRAME0_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.foreground);
 
 #undef ADD_SPRITE_IF_NOT_NULL
+#undef ADD_FRAME0_IF_NOT_NULL
 
   return sprs - saved_sprs;
 }
@@ -7511,6 +7523,7 @@ int fill_basic_base_sprite_array(const struct tileset *t,
 enum mapview_layer tileset_get_layer(const struct tileset *t, int n)
 {
   fc_assert(n < LAYER_COUNT);
+
   return t->layer_order[n];
 }
 
diff --git a/data/alio/fortresses.spec b/data/alio/fortresses.spec
index 05cebdf0d5..c7f34b4086 100644
--- a/data/alio/fortresses.spec
+++ b/data/alio/fortresses.spec
@@ -25,8 +25,8 @@ pixel_border = 1
 tiles = { "row", "column", "tag"
 
  0,  0, "base.force_fortress_bg"
- 1,  0, "base.force_fortress_fg"
+ 1,  0, "base.force_fortress_fg_0"
 
  0,  1, "base.tower_bg"
- 1,  1, "base.tower_fg"
+ 1,  1, "base.tower_fg_0"
 }
diff --git a/data/amplio/moderncities.spec b/data/amplio/moderncities.spec
index a56fc4345d..395b7b0984 100644
--- a/data/amplio/moderncities.spec
+++ b/data/amplio/moderncities.spec
@@ -63,7 +63,7 @@ tiles = { "row", "column", "tag"
  0,  0, "city.disorder"
  0,  1, "base.airbase_mg"
  0,  2, "tx.airbase_full"
- 0,  4, "base.outpost_fg"
+ 0,  4, "base.outpost_fg_0"
  0,  5, "base.outpost_bg"
  0,  6, "city.electricage_occupied_0"
  0,  6, "city.modern_occupied_0"
@@ -72,7 +72,7 @@ tiles = { "row", "column", "tag"
  0,  9, "extra.ruins_mg"
 
  1,  1, "base.airstrip_mg"
- 1,  4, "base.fortress_fg"
+ 1,  4, "base.fortress_fg_0"
  1,  5, "base.fortress_bg"
 ;
 ; city tiles
diff --git a/data/amplio2/bases.spec b/data/amplio2/bases.spec
index fbaf3a9b4e..ecbe6f49f4 100644
--- a/data/amplio2/bases.spec
+++ b/data/amplio2/bases.spec
@@ -30,7 +30,7 @@ tiles = { "row", "column", "tag"
  0,  0, "base.airbase_mg"
  0,  1, "tx.airbase_full"
 ;[HH]
- 0,  3, "base.fortress_fg"
+ 0,  3, "base.fortress_fg_0"
  0,  4, "base.fortress_bg"
 ;[HH][GS]
  1,  0, "base.airstrip_mg"
@@ -39,14 +39,14 @@ tiles = { "row", "column", "tag"
 ;[VC]
  1,  2, "extra.ruins_mg"
 ;[HH][GS]
- 1,  3, "base.outpost_fg"
+ 1,  3, "base.outpost_fg_0"
  1,  4, "base.outpost_bg"
 ; [SK]
  1,  5, "extra.transport_hub_mg"
  ;[Lexxie]
- 2,  2, "base.castle2_fg"        ; alternate castle (larger 'closed' version which makes hidden units more plausible ...
+ 2,  2, "base.castle2_fg_0"      ; Alternate castle (larger 'closed' version which makes hidden units more plausible ...
  2,  3, "base.castle2_bg"        ; ... but also hides the terrain graphics)
- 2,  4, "base.castle_fg"
+ 2,  4, "base.castle_fg_0"
  2,  5, "base.castle_bg"
 ;[HH]
  0,  2, "cd.occupied",
diff --git a/data/hex2t/items.spec b/data/hex2t/items.spec
index b75e8a07a6..c5aab7a31b 100644
--- a/data/hex2t/items.spec
+++ b/data/hex2t/items.spec
@@ -41,9 +41,9 @@ tiles = { "row", "column", "tag"
  1,  3, "base.airstrip_mg"
  1,  5, "base.buoy_mg"
  1,  6, "extra.ruins_mg"
- 3,  8, "base.outpost_fg"
+ 3,  8, "base.outpost_fg_0"
  3,  9, "base.outpost_bg"
- 4,  8, "base.fortress_fg"
+ 4,  8, "base.fortress_fg_0"
  4,  9, "base.fortress_bg"
 
 ; default city tiles
diff --git a/data/hexemplio/bases.spec b/data/hexemplio/bases.spec
index 0405bf1683..18fdf0fb5a 100644
--- a/data/hexemplio/bases.spec
+++ b/data/hexemplio/bases.spec
@@ -57,10 +57,10 @@ tiles = { "row", "column", "tag"
  0,  1, "base.airbase_mg"
  1,  1, "tx.airbase_full"
 ;[HH][GS]
- 1,  2, "base.outpost_fg"
+ 1,  2, "base.outpost_fg_0"
  0,  2, "base.outpost_bg"
 ;[HH]
- 1,  3, "base.fortress_fg"
+ 1,  3, "base.fortress_fg_0"
  0,  3, "base.fortress_bg"
 ;[HH]
  0,  4, "city.disorder"
diff --git a/data/isotrident/cities.spec b/data/isotrident/cities.spec
index c1bb96f59e..777168982f 100644
--- a/data/isotrident/cities.spec
+++ b/data/isotrident/cities.spec
@@ -65,11 +65,11 @@ tiles = { "row", "column", "tag"
  0,  1, "base.airbase_mg"
  0,  2, "tx.airbase_full"
  0,  3, "base.airstrip_mg"
- 0,  4, "base.fortress_fg"
+ 0,  4, "base.fortress_fg_0"
  0,  5, "base.fortress_bg"
  0,  6, "extra.ruins_mg"
  0,  7, "base.buoy_mg"
- 0,  8, "base.outpost_fg"
+ 0,  8, "base.outpost_fg_0"
  0,  9, "base.outpost_bg"
 
 ;
-- 
2.45.2

