added check for custom elements being "next to" player or element
authorHolger Schemel <holger.schemel@virtion.de>
Tue, 21 Sep 2021 21:06:34 +0000 (23:06 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 22 Jan 2022 16:58:27 +0000 (17:58 +0100)
Custom elements are usually only checked for change events at a certain
point in time (for example, when a moving element hits another element).

This is also true for the already existing check "touching", which can
only trigger a custom element change at the exact moment something
happens, like a finished movement action or the change of another
element, at which time a check is performed if that action results in
an element touching the player or another element.

In contrast, the new change condition "next to" can cause an element
change at any time, independently of movements or changes of other
elements, as it permanently checks the static state of adjacent game
elements (or an element and the player). This can be used, for example,
to test for a non-moving, non-changing custom element being next to
another element. (This even works after starting a level, when it is
not possible without tricks (like changing a custom element to itself)
to check if two elements are next to each others.)

src/editor.c
src/game.c
src/main.h

index 282c530025ca3a893370b3080e65f4392f798d89..7f63d89eec53de1db4fc33587be48a05be5f7abb 100644 (file)
@@ -2009,6 +2009,9 @@ static struct ValueTextInfo options_change_direct_action[] =
   { CE_HEADLINE_SPECIAL_EVENTS,        "[mouse events]"                },
   { CE_CLICKED_BY_MOUSE,       "clicked by mouse"              },
   { CE_PRESSED_BY_MOUSE,       "pressed by mouse"              },
+  { CE_UNDEFINED,              " "                             },
+  { CE_HEADLINE_SPECIAL_EVENTS,        "[static states]"               },
+  { CE_NEXT_TO_PLAYER,         "next to player"                },
 
   { -1,                                NULL                            }
 };
@@ -2042,6 +2045,10 @@ static struct ValueTextInfo options_change_other_action[] =
   { CE_HEADLINE_SPECIAL_EVENTS,        "[mouse events]"                },
   { CE_MOUSE_CLICKED_ON_X,     "mouse clicked on"              },
   { CE_MOUSE_PRESSED_ON_X,     "mouse pressed on"              },
+  { CE_UNDEFINED,              " "                             },
+  { CE_HEADLINE_SPECIAL_EVENTS,        "[static states]"               },
+  { CE_PLAYER_NEXT_TO_X,       "player next to"                },
+  { CE_NEXT_TO_X,              "next to"                       },
 
   { -1,                                NULL                            }
 };
@@ -8205,7 +8212,8 @@ static void CopyCustomElementPropertiesToEditor(int element)
 
   // set "change by direct action" selectbox help value
   custom_element_change.direct_action =
-    (HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
+    (HAS_CHANGE_EVENT(element, CE_NEXT_TO_PLAYER) ? CE_NEXT_TO_PLAYER :
+     HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
      HAS_CHANGE_EVENT(element, CE_SWITCHED_BY_PLAYER) ? CE_SWITCHED_BY_PLAYER :
      HAS_CHANGE_EVENT(element, CE_SNAPPED_BY_PLAYER) ? CE_SNAPPED_BY_PLAYER :
@@ -8229,7 +8237,8 @@ static void CopyCustomElementPropertiesToEditor(int element)
 
   // set "change by other element action" selectbox help value
   custom_element_change.other_action =
-    (HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
+    (HAS_CHANGE_EVENT(element, CE_PLAYER_NEXT_TO_X) ? CE_PLAYER_NEXT_TO_X :
+     HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
      HAS_CHANGE_EVENT(element, CE_PLAYER_PRESSES_X) ? CE_PLAYER_PRESSES_X :
      HAS_CHANGE_EVENT(element, CE_PLAYER_SWITCHES_X) ? CE_PLAYER_SWITCHES_X :
      HAS_CHANGE_EVENT(element, CE_PLAYER_SNAPS_X) ? CE_PLAYER_SNAPS_X :
@@ -8239,6 +8248,7 @@ static void CopyCustomElementPropertiesToEditor(int element)
      HAS_CHANGE_EVENT(element, CE_PLAYER_DIGS_X) ? CE_PLAYER_DIGS_X :
      HAS_CHANGE_EVENT(element, CE_PLAYER_COLLECTS_X) ? CE_PLAYER_COLLECTS_X :
      HAS_CHANGE_EVENT(element, CE_PLAYER_DROPS_X) ? CE_PLAYER_DROPS_X :
+     HAS_CHANGE_EVENT(element, CE_NEXT_TO_X) ? CE_NEXT_TO_X :
      HAS_CHANGE_EVENT(element, CE_TOUCHING_X) ? CE_TOUCHING_X :
      HAS_CHANGE_EVENT(element, CE_HITTING_X) ? CE_HITTING_X :
      HAS_CHANGE_EVENT(element, CE_DIGGING_X) ? CE_DIGGING_X :
@@ -8367,6 +8377,7 @@ static void CopyCustomElementPropertiesToGame(int element)
   // ---------- element settings: advanced (custom elements) ------------------
 
   // set player change event from checkbox and selectbox
+  custom_element_change_events[CE_NEXT_TO_PLAYER] = FALSE;
   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
   custom_element_change_events[CE_SWITCHED_BY_PLAYER] = FALSE;
@@ -8391,6 +8402,7 @@ static void CopyCustomElementPropertiesToGame(int element)
     custom_element_change_events[CE_BY_DIRECT_ACTION];
 
   // set other element action change event from checkbox and selectbox
+  custom_element_change_events[CE_PLAYER_NEXT_TO_X] = FALSE;
   custom_element_change_events[CE_PLAYER_TOUCHES_X] = FALSE;
   custom_element_change_events[CE_PLAYER_PRESSES_X] = FALSE;
   custom_element_change_events[CE_PLAYER_SWITCHES_X] = FALSE;
@@ -8401,6 +8413,7 @@ static void CopyCustomElementPropertiesToGame(int element)
   custom_element_change_events[CE_PLAYER_DIGS_X] = FALSE;
   custom_element_change_events[CE_PLAYER_COLLECTS_X] = FALSE;
   custom_element_change_events[CE_PLAYER_DROPS_X] = FALSE;
+  custom_element_change_events[CE_NEXT_TO_X] = FALSE;
   custom_element_change_events[CE_TOUCHING_X] = FALSE;
   custom_element_change_events[CE_HITTING_X] = FALSE;
   custom_element_change_events[CE_DIGGING_X] = FALSE;
index d4cc5ea8e0c5572bb737e62cb9202bdad128b344..efc81e4e56214c58ac33a36004e828df3303d4e9 100644 (file)
@@ -1058,7 +1058,10 @@ static void CheckGravityMovementWhenNotMoving(struct PlayerInfo *);
 static void KillPlayerUnlessEnemyProtected(int, int);
 static void KillPlayerUnlessExplosionProtected(int, int);
 
+static void CheckNextToConditions(int, int);
+static void TestIfPlayerNextToCustomElement(int, int);
 static void TestIfPlayerTouchesCustomElement(int, int);
+static void TestIfElementNextToCustomElement(int, int);
 static void TestIfElementTouchesCustomElement(int, int);
 static void TestIfElementHitsCustomElement(int, int, int);
 
@@ -11150,7 +11153,8 @@ static boolean CheckElementChangeExt(int x, int y,
        different to element changes that affect other elements to change on the
        whole playfield (which is handeld by CheckTriggeredElementChangeExt()) */
     boolean check_trigger_element =
-      (trigger_event == CE_TOUCHING_X ||
+      (trigger_event == CE_NEXT_TO_X ||
+       trigger_event == CE_TOUCHING_X ||
        trigger_event == CE_HITTING_X ||
        trigger_event == CE_HIT_BY_X ||
        trigger_event == CE_DIGGING_X); // this one was forgotten until 3.2.3
@@ -12316,6 +12320,8 @@ void GameActions_RND(void)
       graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
     }
 
+    CheckNextToConditions(x, y);
+
     if (!IS_MOVING(x, y) && (CAN_FALL(element) || CAN_MOVE(element)))
     {
       StartMoving(x, y);
@@ -13293,6 +13299,76 @@ void ScrollScreen(struct PlayerInfo *player, int mode)
     ScreenMovDir = MV_NONE;
 }
 
+void CheckNextToConditions(int x, int y)
+{
+  int element = Tile[x][y];
+
+  if (IS_PLAYER(x, y))
+    TestIfPlayerNextToCustomElement(x, y);
+
+  if (CAN_CHANGE_OR_HAS_ACTION(element) &&
+      HAS_ANY_CHANGE_EVENT(element, CE_NEXT_TO_X))
+    TestIfElementNextToCustomElement(x, y);
+}
+
+void TestIfPlayerNextToCustomElement(int x, int y)
+{
+  static int xy[4][2] =
+  {
+    { 0, -1 },
+    { -1, 0 },
+    { +1, 0 },
+    { 0, +1 }
+  };
+  static int trigger_sides[4][2] =
+  {
+    // center side       border side
+    { CH_SIDE_TOP,     CH_SIDE_BOTTOM  },      // check top
+    { CH_SIDE_LEFT,    CH_SIDE_RIGHT   },      // check left
+    { CH_SIDE_RIGHT,   CH_SIDE_LEFT    },      // check right
+    { CH_SIDE_BOTTOM,  CH_SIDE_TOP     }       // check bottom
+  };
+  int i;
+
+  if (!IS_PLAYER(x, y))
+    return;
+
+  struct PlayerInfo *player = PLAYERINFO(x, y);
+
+  if (player->is_moving)
+    return;
+
+  for (i = 0; i < NUM_DIRECTIONS; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+    int border_side = trigger_sides[i][1];
+    int border_element;
+
+    if (!IN_LEV_FIELD(xx, yy))
+      continue;
+
+    if (IS_MOVING(xx, yy) || IS_BLOCKED(xx, yy))
+      continue;                // center and border element not connected
+
+    border_element = Tile[xx][yy];
+
+    CheckElementChangeByPlayer(xx, yy, border_element, CE_NEXT_TO_PLAYER,
+                               player->index_bit, border_side);
+    CheckTriggeredElementChangeByPlayer(xx, yy, border_element,
+                                        CE_PLAYER_NEXT_TO_X,
+                                        player->index_bit, border_side);
+
+    /* use player element that is initially defined in the level playfield,
+       not the player element that corresponds to the runtime player number
+       (example: a level that contains EL_PLAYER_3 as the only player would
+       incorrectly give EL_PLAYER_1 for "player->element_nr") */
+
+    CheckElementChangeBySide(xx, yy, border_element, player->initial_element,
+                             CE_NEXT_TO_X, border_side);
+  }
+}
+
 void TestIfPlayerTouchesCustomElement(int x, int y)
 {
   static int xy[4][2] =
@@ -13393,6 +13469,51 @@ void TestIfPlayerTouchesCustomElement(int x, int y)
   }
 }
 
+void TestIfElementNextToCustomElement(int x, int y)
+{
+  static int xy[4][2] =
+  {
+    { 0, -1 },
+    { -1, 0 },
+    { +1, 0 },
+    { 0, +1 }
+  };
+  static int trigger_sides[4][2] =
+  {
+    // center side     border side
+    { CH_SIDE_TOP,     CH_SIDE_BOTTOM  },      // check top
+    { CH_SIDE_LEFT,    CH_SIDE_RIGHT   },      // check left
+    { CH_SIDE_RIGHT,   CH_SIDE_LEFT    },      // check right
+    { CH_SIDE_BOTTOM,  CH_SIDE_TOP     }       // check bottom
+  };
+  int center_element = Tile[x][y];     // should always be non-moving!
+  int i;
+
+  if (IS_MOVING(x, y) || IS_BLOCKED(x, y))
+    return;
+
+  for (i = 0; i < NUM_DIRECTIONS; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+    int border_side = trigger_sides[i][1];
+    int border_element;
+
+    if (!IN_LEV_FIELD(xx, yy))
+      continue;
+
+    if (IS_MOVING(xx, yy) || IS_BLOCKED(xx, yy))
+      continue;                        // center and border element not connected
+
+    border_element = Tile[xx][yy];
+
+    // check for change of center element (but change it only once)
+    if (CheckElementChangeBySide(x, y, center_element, border_element,
+                                 CE_NEXT_TO_X, border_side))
+      break;
+  }
+}
+
 void TestIfElementTouchesCustomElement(int x, int y)
 {
   static int xy[4][2] =
index 5566a215d6540e969c650a04fbc09857639cc80d..ea915a9ac73cef17e7aabe42a1fff9ee245d2373 100644 (file)
 #define CE_PRESSED_BY_MOUSE            45
 #define CE_MOUSE_CLICKED_ON_X          46
 #define CE_MOUSE_PRESSED_ON_X          47
+#define CE_NEXT_TO_PLAYER              48
+#define CE_NEXT_TO_X                   49
+#define CE_PLAYER_NEXT_TO_X            50
 
-#define NUM_CHANGE_EVENTS              48
+#define NUM_CHANGE_EVENTS              51
 
 #define NUM_CE_BITFIELDS               ((NUM_CHANGE_EVENTS + 31) / 32)