rnd-20040305-1-src
[rocksndiamonds.git] / src / game.c
index 7ff1a5e48146453f1cab477ddfdd45d8e54e7b38..d55562340ab92cbe435c632ea060fb1ac9ef694e 100644 (file)
@@ -241,12 +241,16 @@ int DigField(struct PlayerInfo *, int, int, int, int, int, int, int);
 static void InitBeltMovement(void);
 static void CloseAllOpenTimegates(void);
 static void CheckGravityMovement(struct PlayerInfo *);
+static void CheckGravityMovementWhenNotMoving(struct PlayerInfo *);
 static void KillHeroUnlessEnemyProtected(int, int);
 static void KillHeroUnlessExplosionProtected(int, int);
 
 static void TestIfPlayerTouchesCustomElement(int, int);
 static void TestIfElementTouchesCustomElement(int, int);
 static void TestIfElementHitsCustomElement(int, int, int);
+#if 0
+static void TestIfElementSmashesCustomElement(int, int, int);
+#endif
 
 static void ChangeElement(int, int, int);
 
@@ -1471,6 +1475,7 @@ void InitGame()
     player->shield_normal_time_left = 0;
     player->shield_deadly_time_left = 0;
 
+    player->inventory_infinite_element = EL_UNDEFINED;
     player->inventory_size = 0;
 
     DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
@@ -1494,6 +1499,7 @@ void InitGame()
   TimeFrames = 0;
   TimePlayed = 0;
   TimeLeft = level.time;
+  TapeTime = 0;
 
   ScreenMovDir = MV_NO_MOVING;
   ScreenMovPos = 0;
@@ -1628,6 +1634,10 @@ void InitGame()
          some_player->present = FALSE;
          some_player->active = FALSE;
 
+#if 0
+         player->element_nr = some_player->element_nr;
+#endif
+
          StorePlayer[jx][jy] = player->element_nr;
          player->jx = player->last_jx = jx;
          player->jy = player->last_jy = jy;
@@ -2637,6 +2647,10 @@ void Explode(int ex, int ey, int phase, int mode)
   {
     int center_element = Feld[ex][ey];
 
+#if 0
+    printf("::: start explosion %d,%d [%d]\n", ex, ey, FrameCounter);
+#endif
+
 #if 0
     /* --- This is only really needed (and now handled) in "Impact()". --- */
     /* do not explode moving elements that left the explode field in time */
@@ -2977,6 +2991,10 @@ void Explode(int ex, int ey, int phase, int mode)
   {
     int element;
 
+#if 0
+    printf("::: explosion %d,%d done [%d]\n", x, y, FrameCounter);
+#endif
+
     element = Feld[x][y] = Store[x][y];
     Store[x][y] = Store2[x][y] = 0;
     GfxElement[x][y] = EL_UNDEFINED;
@@ -3785,6 +3803,10 @@ void Impact(int x, int y)
        }
        else
        {
+#if 0
+         TestIfElementSmashesCustomElement(x, y, MV_DOWN);
+#endif
+
          CheckElementChange(x, y + 1, smashed, CE_SMASHED);
 
          CheckTriggeredElementChangeSide(x, y + 1, smashed,
@@ -7177,6 +7199,10 @@ static byte PlayerActions(struct PlayerInfo *player, byte player_action)
     printf("::: player %d acts [%d]\n", player->index_nr, FrameCounter);
 #endif
 
+#if 0
+    /* !!! TEST !!! */
+    CheckGravityMovement(player);
+#endif
     if (button1)
       snapped = SnapField(player, dx, dy);
     else
@@ -7214,7 +7240,7 @@ static byte PlayerActions(struct PlayerInfo *player, byte player_action)
 
     DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
     SnapField(player, 0, 0);
-    CheckGravityMovement(player);
+    CheckGravityMovementWhenNotMoving(player);
 
     if (player->MovPos == 0)
       SetPlayerWaiting(player, TRUE);
@@ -7295,7 +7321,7 @@ static void PlayerActions(struct PlayerInfo *player, byte player_action)
 
     DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
     SnapField(player, 0, 0);
-    CheckGravityMovement(player);
+    CheckGravityMovementWhenNotMoving(player);
 
     if (player->MovPos == 0)
       InitPlayerGfxAnimation(player, ACTION_DEFAULT, player->MovDir);
@@ -7375,6 +7401,12 @@ void GameActions()
 
   recorded_player_action = (tape.playing ? TapePlayAction() : NULL);
 
+#if 1
+  if (recorded_player_action != NULL)
+    for (i = 0; i < MAX_PLAYERS; i++)
+      stored_player[i].action = recorded_player_action[i];
+#endif
+
   for (i = 0; i < MAX_PLAYERS; i++)
   {
     summarized_player_action |= stored_player[i].action;
@@ -7395,11 +7427,31 @@ void GameActions()
   {
     int actual_player_action = stored_player[i].effective_action;
 
+#if 1
+    /* OLD: overwrite programmed action with tape action (BAD!!!) */
     if (stored_player[i].programmed_action)
       actual_player_action = stored_player[i].programmed_action;
+#endif
 
     if (recorded_player_action)
+    {
+#if 0
+      if (stored_player[i].programmed_action &&
+         stored_player[i].programmed_action != recorded_player_action[i])
+       printf("::: %d: %d <-> %d\n", i,
+              stored_player[i].programmed_action, recorded_player_action[i]);
+#endif
+
+#if 0
       actual_player_action = recorded_player_action[i];
+#endif
+    }
+
+#if 0
+    /* NEW: overwrite tape action with programmed action */
+    if (stored_player[i].programmed_action)
+      actual_player_action = stored_player[i].programmed_action;
+#endif
 
     tape_action[i] = PlayerActions(&stored_player[i], actual_player_action);
 
@@ -7833,39 +7885,44 @@ void GameActions()
   if (TimeFrames >= FRAMES_PER_SECOND)
   {
     TimeFrames = 0;
-    TimePlayed++;
+    TapeTime++;
 
-    for (i = 0; i < MAX_PLAYERS; i++)
+    if (!level.use_step_counter)
     {
-      struct PlayerInfo *player = &stored_player[i];
+      TimePlayed++;
 
-      if (SHIELD_ON(player))
+      for (i = 0; i < MAX_PLAYERS; i++)
       {
-       player->shield_normal_time_left--;
+       struct PlayerInfo *player = &stored_player[i];
 
-       if (player->shield_deadly_time_left > 0)
-         player->shield_deadly_time_left--;
-      }
-    }
+       if (SHIELD_ON(player))
+       {
+         player->shield_normal_time_left--;
 
-    if (tape.recording || tape.playing)
-      DrawVideoDisplay(VIDEO_STATE_TIME_ON, TimePlayed);
+         if (player->shield_deadly_time_left > 0)
+           player->shield_deadly_time_left--;
+       }
+      }
 
-    if (TimeLeft > 0)
-    {
-      TimeLeft--;
+      if (TimeLeft > 0)
+      {
+       TimeLeft--;
 
-      if (TimeLeft <= 10 && setup.time_limit)
-       PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
+       if (TimeLeft <= 10 && setup.time_limit)
+         PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
 
-      DrawGameValue_Time(TimeLeft);
+       DrawGameValue_Time(TimeLeft);
 
-      if (!TimeLeft && setup.time_limit)
-       for (i = 0; i < MAX_PLAYERS; i++)
-         KillHero(&stored_player[i]);
+       if (!TimeLeft && setup.time_limit)
+         for (i = 0; i < MAX_PLAYERS; i++)
+           KillHero(&stored_player[i]);
+      }
+      else if (level.time == 0 && !AllPlayersGone) /* level w/o time limit */
+       DrawGameValue_Time(TimePlayed);
     }
-    else if (level.time == 0 && !AllPlayersGone) /* level without time limit */
-      DrawGameValue_Time(TimePlayed);
+
+    if (tape.recording || tape.playing)
+      DrawVideoDisplay(VIDEO_STATE_TIME_ON, TapeTime);
   }
 
   DrawAllPlayers();
@@ -8035,20 +8092,33 @@ static void CheckGravityMovement(struct PlayerInfo *player)
 {
   if (game.gravity && !player->programmed_action)
   {
-    int move_dir_vertical = player->action & (MV_UP | MV_DOWN);
-    int move_dir_horizontal = player->action & (MV_LEFT | MV_RIGHT);
+    int move_dir_horizontal = player->action & MV_HORIZONTAL;
+    int move_dir_vertical   = player->action & MV_VERTICAL;
     int move_dir =
-      (player->last_move_dir & (MV_LEFT | MV_RIGHT) ?
+      (player->last_move_dir & MV_HORIZONTAL ?
        (move_dir_vertical ? move_dir_vertical : move_dir_horizontal) :
        (move_dir_horizontal ? move_dir_horizontal : move_dir_vertical));
     int jx = player->jx, jy = player->jy;
     int dx = (move_dir & MV_LEFT ? -1 : move_dir & MV_RIGHT ? +1 : 0);
     int dy = (move_dir & MV_UP ? -1 : move_dir & MV_DOWN ? +1 : 0);
     int new_jx = jx + dx, new_jy = jy + dy;
-    boolean field_under_player_is_free =
-      (IN_LEV_FIELD(jx, jy + 1) && IS_FREE(jx, jy + 1));
+    boolean player_is_snapping = player->action & JOY_BUTTON_1;
+#if 0
+    /* !!! MAKE THIS CUSTOMIZABLE !!! */
+    boolean field_under_player_is_free_or_acid =
+      (IN_LEV_FIELD(jx, jy + 1) &&
+       (IS_FREE(jx, jy + 1) || Feld[jx][jy + 1] == EL_ACID));
+#else
+    boolean field_under_player_is_free_or_acid =
+      (IN_LEV_FIELD(jx, jy + 1) &&
+       (IS_FREE(jx, jy + 1)));
+#endif
     boolean player_is_moving_to_valid_field =
-      (IN_LEV_FIELD(new_jx, new_jy) &&
+      (
+#if 1
+       !player_is_snapping &&
+#endif
+       IN_LEV_FIELD(new_jx, new_jy) &&
        (Feld[new_jx][new_jy] == EL_SP_BASE ||
        Feld[new_jx][new_jy] == EL_SAND ||
        (IS_SP_PORT(Feld[new_jx][new_jy]) &&
@@ -8060,9 +8130,45 @@ static void CheckGravityMovement(struct PlayerInfo *player)
        (IS_WALKABLE(Feld[jx][jy]) &&
        !(element_info[Feld[jx][jy]].access_direction & MV_DOWN)));
 
-    if (field_under_player_is_free &&
+#if 0
+    printf("::: checking gravity NOW [%d, %d, %d] [%d] ...\n",
+          field_under_player_is_free_or_acid,
+          player_is_standing_on_valid_field,
+          player_is_moving_to_valid_field,
+          (player_is_moving_to_valid_field ? Feld[new_jx][new_jy] : -1));
+#endif
+
+    if (field_under_player_is_free_or_acid &&
        !player_is_standing_on_valid_field &&
        !player_is_moving_to_valid_field)
+    {
+#if 0
+      printf("::: setting programmed_action to MV_DOWN [%d,%d - %d] ...\n",
+            jx, jy, FrameCounter);
+#endif
+
+      player->programmed_action = MV_DOWN;
+    }
+  }
+}
+
+static void CheckGravityMovementWhenNotMoving(struct PlayerInfo *player)
+{
+#if 1
+  return CheckGravityMovement(player);
+#endif
+
+  if (game.gravity && !player->programmed_action)
+  {
+    int jx = player->jx, jy = player->jy;
+    boolean field_under_player_is_free =
+      (IN_LEV_FIELD(jx, jy + 1) && IS_FREE(jx, jy + 1));
+    boolean player_is_standing_on_valid_field =
+      (IS_WALKABLE_INSIDE(Feld[jx][jy]) ||
+       (IS_WALKABLE(Feld[jx][jy]) &&
+       !(element_info[Feld[jx][jy]].access_direction & MV_DOWN)));
+
+    if (field_under_player_is_free && !player_is_standing_on_valid_field)
       player->programmed_action = MV_DOWN;
   }
 }
@@ -8214,9 +8320,16 @@ boolean MovePlayer(struct PlayerInfo *player, int dx, int dy)
       !tape.playing)
     return FALSE;
 #else
+
+#if 1
+  if (!FrameReached(&player->move_delay, player->move_delay_value))
+    return FALSE;
+#else
   if (!FrameReached(&player->move_delay, player->move_delay_value) &&
       !(tape.playing && tape.file_version < FILE_VERSION_2_0))
     return FALSE;
+#endif
+
 #endif
 
   /* remove the last programmed player action */
@@ -8249,7 +8362,7 @@ boolean MovePlayer(struct PlayerInfo *player, int dx, int dy)
     player->move_delay_value = original_move_delay_value;
   }
 
-  if (player->last_move_dir & (MV_LEFT | MV_RIGHT))
+  if (player->last_move_dir & MV_HORIZONTAL)
   {
     if (!(moved |= MovePlayerOneStep(player, 0, dy, dx, dy)))
       moved |= MovePlayerOneStep(player, dx, 0, dx, dy);
@@ -8407,7 +8520,7 @@ boolean MovePlayer(struct PlayerInfo *player, int dx, int dy)
   }
   else
   {
-    CheckGravityMovement(player);
+    CheckGravityMovementWhenNotMoving(player);
 
     /*
     player->last_move_dir = MV_NO_MOVING;
@@ -8526,6 +8639,42 @@ void ScrollPlayer(struct PlayerInfo *player, int mode)
        RemoveHero(player);
     }
 
+    if (level.use_step_counter)
+    {
+      int i;
+
+      TimePlayed++;
+
+      for (i = 0; i < MAX_PLAYERS; i++)
+      {
+       struct PlayerInfo *player = &stored_player[i];
+
+       if (SHIELD_ON(player))
+       {
+         player->shield_normal_time_left--;
+
+         if (player->shield_deadly_time_left > 0)
+           player->shield_deadly_time_left--;
+       }
+      }
+
+      if (TimeLeft > 0)
+      {
+       TimeLeft--;
+
+       if (TimeLeft <= 10 && setup.time_limit)
+         PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
+
+       DrawGameValue_Time(TimeLeft);
+
+       if (!TimeLeft && setup.time_limit)
+         for (i = 0; i < MAX_PLAYERS; i++)
+           KillHero(&stored_player[i]);
+      }
+      else if (level.time == 0 && !AllPlayersGone) /* level w/o time limit */
+       DrawGameValue_Time(TimePlayed);
+    }
+
     if (tape.single_step && tape.recording && !tape.pausing &&
        !player->programmed_action)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
@@ -8847,6 +8996,108 @@ void TestIfElementHitsCustomElement(int x, int y, int direction)
   }
 }
 
+#if 0
+void TestIfElementSmashesCustomElement(int x, int y, int direction)
+{
+  int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
+  int dy = (direction == MV_UP   ? -1 : direction == MV_DOWN  ? +1 : 0);
+  int hitx = x + dx, hity = y + dy;
+  int hitting_element = Feld[x][y];
+#if 0
+  boolean object_hit = (IN_LEV_FIELD(hitx, hity) &&
+                       !IS_FREE(hitx, hity) &&
+                       (!IS_MOVING(hitx, hity) ||
+                        MovDir[hitx][hity] != direction ||
+                        ABS(MovPos[hitx][hity]) <= TILEY / 2));
+#endif
+
+  if (IN_LEV_FIELD(hitx, hity) && IS_FREE(hitx, hity))
+    return;
+
+#if 0
+  if (IN_LEV_FIELD(hitx, hity) && !object_hit)
+    return;
+#endif
+
+  CheckElementChangeSide(x, y, hitting_element, EP_CAN_SMASH_EVERYTHING,
+                        direction);
+
+  if (IN_LEV_FIELD(hitx, hity))
+  {
+    int opposite_direction = MV_DIR_OPPOSITE(direction);
+    int hitting_side = direction;
+    int touched_side = opposite_direction;
+    int touched_element = MovingOrBlocked2Element(hitx, hity);
+#if 1
+    boolean object_hit = (!IS_MOVING(hitx, hity) ||
+                         MovDir[hitx][hity] != direction ||
+                         ABS(MovPos[hitx][hity]) <= TILEY / 2);
+
+    object_hit = TRUE;
+#endif
+
+    if (object_hit)
+    {
+      int i;
+
+      CheckElementChangeSide(hitx, hity, touched_element,
+                            CE_SMASHED_BY_SOMETHING, opposite_direction);
+
+      if (IS_CUSTOM_ELEMENT(hitting_element) &&
+         HAS_ANY_CHANGE_EVENT(hitting_element, CE_OTHER_IS_SMASHING))
+      {
+       for (i = 0; i < element_info[hitting_element].num_change_pages; i++)
+       {
+         struct ElementChangeInfo *change =
+           &element_info[hitting_element].change_page[i];
+
+         if (change->can_change &&
+             change->events & CH_EVENT_BIT(CE_OTHER_IS_SMASHING) &&
+             change->trigger_side & touched_side &&
+         
+#if 1
+             IS_EQUAL_OR_IN_GROUP(touched_element, change->trigger_element)
+#else
+             change->trigger_element == touched_element
+#endif
+             )
+         {
+           CheckElementChangePage(x, y, hitting_element, CE_OTHER_IS_SMASHING,
+                                  i);
+           break;
+         }
+       }
+      }
+
+      if (IS_CUSTOM_ELEMENT(touched_element) &&
+         HAS_ANY_CHANGE_EVENT(touched_element, CE_OTHER_GETS_SMASHED))
+      {
+       for (i = 0; i < element_info[touched_element].num_change_pages; i++)
+       {
+         struct ElementChangeInfo *change =
+           &element_info[touched_element].change_page[i];
+
+         if (change->can_change &&
+             change->events & CH_EVENT_BIT(CE_OTHER_GETS_SMASHED) &&
+             change->trigger_side & hitting_side &&
+#if 1
+             IS_EQUAL_OR_IN_GROUP(hitting_element, change->trigger_element)
+#else
+             change->trigger_element == hitting_element
+#endif
+             )
+         {
+           CheckElementChangePage(hitx, hity, touched_element,
+                                  CE_OTHER_GETS_SMASHED, i);
+           break;
+         }
+       }
+      }
+    }
+  }
+}
+#endif
+
 void TestIfGoodThingHitsBadThing(int good_x, int good_y, int good_move_dir)
 {
   int i, kill_x = -1, kill_y = -1;
@@ -9338,6 +9589,10 @@ int DigField(struct PlayerInfo *player,
       DOUBLE_PLAYER_SPEED(player);
 #endif
 
+#if 0
+      printf("::: passing port %d,%d [%d]\n", x, y, FrameCounter);
+#endif
+
       PlayLevelSound(x, y, SND_CLASS_SP_PORT_PASSING);
       break;
 
@@ -9562,9 +9817,12 @@ int DigField(struct PlayerInfo *player,
        {
          int i;
 
-         for (i = 0; i < element_info[element].collect_count; i++)
-           if (player->inventory_size < MAX_INVENTORY_SIZE)
-             player->inventory_element[player->inventory_size++] = element;
+         if (element_info[element].collect_count == 0)
+           player->inventory_infinite_element = element;
+         else
+           for (i = 0; i < element_info[element].collect_count; i++)
+             if (player->inventory_size < MAX_INVENTORY_SIZE)
+               player->inventory_element[player->inventory_size++] = element;
 
          DrawGameValue_Dynamite(local_player->inventory_size);
        }
@@ -9945,25 +10203,43 @@ boolean DropElement(struct PlayerInfo *player)
 {
   int jx = player->jx, jy = player->jy;
   int old_element = Feld[jx][jy];
-  int new_element;
+  int new_element = (player->inventory_size > 0 ?
+                    player->inventory_element[player->inventory_size - 1] :
+                    player->inventory_infinite_element != EL_UNDEFINED ?
+                    player->inventory_infinite_element :
+                    player->dynabombs_left > 0 ?
+                    EL_DYNABOMB_PLAYER_1_ACTIVE + player->index_nr :
+                    EL_UNDEFINED);
 
   /* check if player is active, not moving and ready to drop */
   if (!player->active || player->MovPos || player->drop_delay > 0)
     return FALSE;
 
   /* check if player has anything that can be dropped */
-  if (player->inventory_size == 0 && player->dynabombs_left == 0)
+#if 1
+  if (new_element == EL_UNDEFINED)
     return FALSE;
+#else
+  if (player->inventory_size == 0 &&
+      player->inventory_infinite_element == EL_UNDEFINED &&
+      player->dynabombs_left == 0)
+    return FALSE;
+#endif
 
   /* check if anything can be dropped at the current position */
   if (IS_ACTIVE_BOMB(old_element) || old_element == EL_EXPLOSION)
     return FALSE;
 
   /* collected custom elements can only be dropped on empty fields */
+#if 1
+  if (IS_CUSTOM_ELEMENT(new_element) && old_element != EL_EMPTY)
+    return FALSE;
+#else
   if (player->inventory_size > 0 &&
       IS_CUSTOM_ELEMENT(player->inventory_element[player->inventory_size - 1])
       && old_element != EL_EMPTY)
     return FALSE;
+#endif
 
   if (old_element != EL_EMPTY)
     Back[jx][jy] = old_element;                /* store old element on this field */
@@ -9971,19 +10247,26 @@ boolean DropElement(struct PlayerInfo *player)
   ResetGfxAnimation(jx, jy);
   ResetRandomAnimationValue(jx, jy);
 
-  if (player->inventory_size > 0)
+  if (player->inventory_size > 0 ||
+      player->inventory_infinite_element != EL_UNDEFINED)
   {
-    player->inventory_size--;
-    new_element = player->inventory_element[player->inventory_size];
+    if (player->inventory_size > 0)
+    {
+      player->inventory_size--;
+
+#if 0
+      new_element = player->inventory_element[player->inventory_size];
+#endif
 
-    if (new_element == EL_DYNAMITE)
-      new_element = EL_DYNAMITE_ACTIVE;
-    else if (new_element == EL_SP_DISK_RED)
-      new_element = EL_SP_DISK_RED_ACTIVE;
+      DrawGameValue_Dynamite(local_player->inventory_size);
 
-    Feld[jx][jy] = new_element;
+      if (new_element == EL_DYNAMITE)
+       new_element = EL_DYNAMITE_ACTIVE;
+      else if (new_element == EL_SP_DISK_RED)
+       new_element = EL_SP_DISK_RED_ACTIVE;
+    }
 
-    DrawGameValue_Dynamite(local_player->inventory_size);
+    Feld[jx][jy] = new_element;
 
     if (IN_SCR_FIELD(SCREENX(jx), SCREENY(jy)))
       DrawGraphicThruMask(SCREENX(jx), SCREENY(jy), el2img(Feld[jx][jy]), 0);
@@ -10006,7 +10289,10 @@ boolean DropElement(struct PlayerInfo *player)
   else         /* player is dropping a dyna bomb */
   {
     player->dynabombs_left--;
+
+#if 0
     new_element = EL_DYNABOMB_PLAYER_1_ACTIVE + player->index_nr;
+#endif
 
     Feld[jx][jy] = new_element;