fixed lock-up when auto-testing tape which causes loop in game engine
[rocksndiamonds.git] / src / game.c
index 4e1c699d28a5768cc56a2a6c80586f85a9f85403..abc56d238a167278eb7769778ba0edc4e5269c40 100644 (file)
@@ -1106,7 +1106,7 @@ void ContinueMoving(int, int);
 void Bang(int, int);
 void InitMovDir(int, int);
 void InitAmoebaNr(int, int);
-void NewHighScore(int);
+void NewHighScore(int, boolean);
 
 void TestIfGoodThingHitsBadThing(int, int, int);
 void TestIfBadThingHitsGoodThing(int, int, int);
@@ -4717,6 +4717,19 @@ static void LevelSolved_SetFinalGameValues(void)
   game.LevelSolved_CountingHealth = game.health_final;
 }
 
+static void LevelSolved_DisplayFinalGameValues(int time, int score, int health)
+{
+  game.LevelSolved_CountingTime = time;
+  game.LevelSolved_CountingScore = score;
+  game.LevelSolved_CountingHealth = health;
+
+  game_panel_controls[GAME_PANEL_TIME].value = time;
+  game_panel_controls[GAME_PANEL_SCORE].value = score;
+  game_panel_controls[GAME_PANEL_HEALTH].value = health;
+
+  DisplayGameControlValues();
+}
+
 static void LevelSolved(void)
 {
   if (level.game_engine_type == GAME_ENGINE_TYPE_RND &&
@@ -4777,6 +4790,9 @@ void GameWon(void)
     score = score_final = game.score_final;
     health = health_final = game.health_final;
 
+    // update game panel values before (delayed) counting of score (if any)
+    LevelSolved_DisplayFinalGameValues(time, score, health);
+
     // if level has time score defined, calculate new final game values
     if (time_score > 0)
     {
@@ -4817,13 +4833,7 @@ void GameWon(void)
       time = time_final;
       score = score_final;
 
-      game.LevelSolved_CountingTime = time;
-      game.LevelSolved_CountingScore = score;
-
-      game_panel_controls[GAME_PANEL_TIME].value = time;
-      game_panel_controls[GAME_PANEL_SCORE].value = score;
-
-      DisplayGameControlValues();
+      LevelSolved_DisplayFinalGameValues(time, score, health);
     }
 
     if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
@@ -4900,13 +4910,7 @@ void GameWon(void)
       if (time == time_final)
        score = score_final;
 
-      game.LevelSolved_CountingTime = time;
-      game.LevelSolved_CountingScore = score;
-
-      game_panel_controls[GAME_PANEL_TIME].value = time;
-      game_panel_controls[GAME_PANEL_SCORE].value = score;
-
-      DisplayGameControlValues();
+      LevelSolved_DisplayFinalGameValues(time, score, health);
 
       if (time == time_final)
        StopSound(SND_GAME_LEVELTIME_BONUS);
@@ -4932,13 +4936,7 @@ void GameWon(void)
       health += health_count_dir;
       score  += time_score;
 
-      game.LevelSolved_CountingHealth = health;
-      game.LevelSolved_CountingScore = score;
-
-      game_panel_controls[GAME_PANEL_HEALTH].value = health;
-      game_panel_controls[GAME_PANEL_SCORE].value = score;
-
-      DisplayGameControlValues();
+      LevelSolved_DisplayFinalGameValues(time, score, health);
 
       if (health == health_final)
        StopSound(SND_GAME_LEVELTIME_BONUS);
@@ -4967,6 +4965,7 @@ void GameEnd(void)
 {
   // used instead of "level_nr" (needed for network games)
   int last_level_nr = levelset.level_nr;
+  boolean tape_saved = FALSE;
 
   game.LevelSolved_GameEnd = TRUE;
 
@@ -4976,7 +4975,8 @@ void GameEnd(void)
     if (!global.use_envelope_request)
       CloseDoor(DOOR_CLOSE_1);
 
-    SaveTapeChecked_LevelSolved(tape.level_nr);                // ask to save tape
+    // ask to save tape
+    tape_saved = SaveTapeChecked_LevelSolved(tape.level_nr);
 
     // set unique basename for score tape (also saved in high score table)
     strcpy(tape.score_tape_basename, getScoreTapeBasename(setup.player_name));
@@ -5011,7 +5011,7 @@ void GameEnd(void)
   }
 
   // save score and score tape before potentially erasing tape below
-  NewHighScore(last_level_nr);
+  NewHighScore(last_level_nr, tape_saved);
 
   if (setup.increment_levels &&
       level_nr < leveldir_current->last_level &&
@@ -5119,7 +5119,7 @@ static int addScoreEntry(struct ScoreInfo *list, struct ScoreEntry *new_entry,
   return -1;
 }
 
-void NewHighScore(int level_nr)
+void NewHighScore(int level_nr, boolean tape_saved)
 {
   struct ScoreEntry new_entry = {{ 0 }}; // (prevent warning from GCC bug 53119)
   boolean one_per_name = FALSE;
@@ -5145,7 +5145,7 @@ void NewHighScore(int level_nr)
   if (game.LevelSolved_SaveTape)
   {
     SaveScoreTape(level_nr);
-    SaveServerScore(level_nr);
+    SaveServerScore(level_nr, tape_saved);
   }
 }
 
@@ -11713,7 +11713,7 @@ static void GameActionsExt(void)
     Warn("element '%s' caused endless loop in game engine",
         EL_NAME(recursion_loop_element));
 
-    RequestQuitGameExt(FALSE, level_editor_test_game, message);
+    RequestQuitGameExt(program.headless, level_editor_test_game, message);
 
     recursion_loop_detected = FALSE;   // if game should be continued
 
@@ -13970,7 +13970,11 @@ static void TestFieldAfterSnapping(int x, int y, int element, int direction,
   if (level.finish_dig_collect)
   {
     int dig_side = MV_DIR_OPPOSITE(direction);
+    int change_event = (IS_DIGGABLE(element) ? CE_PLAYER_DIGS_X :
+                       CE_PLAYER_COLLECTS_X);
 
+    CheckTriggeredElementChangeByPlayer(x, y, element, change_event,
+                                       player_index_bit, dig_side);
     CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SNAPS_X,
                                        player_index_bit, dig_side);
   }
@@ -16164,12 +16168,18 @@ static void UnmapGameButtonsAtSamePosition(int id)
 
 static void UnmapGameButtonsAtSamePosition_All(void)
 {
-  if (setup.show_snapshot_buttons)
+  if (setup.show_load_save_buttons)
   {
     UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_SAVE);
     UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_PAUSE2);
     UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_LOAD);
   }
+  else if (setup.show_undo_redo_buttons)
+  {
+    UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_UNDO);
+    UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_PAUSE2);
+    UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_REDO);
+  }
   else
   {
     UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_STOP);
@@ -16182,17 +16192,13 @@ static void UnmapGameButtonsAtSamePosition_All(void)
   }
 }
 
-static void MapGameButtonsAtSamePosition(int id)
+void MapLoadSaveButtons(void)
 {
-  int i;
-
-  for (i = 0; i < NUM_GAME_BUTTONS; i++)
-    if (i != id &&
-       gamebutton_info[i].pos->x == gamebutton_info[id].pos->x &&
-       gamebutton_info[i].pos->y == gamebutton_info[id].pos->y)
-      MapGadget(game_gadget[i]);
+  UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_LOAD);
+  UnmapGameButtonsAtSamePosition(GAME_CTRL_ID_SAVE);
 
-  UnmapGameButtonsAtSamePosition_All();
+  MapGadget(game_gadget[GAME_CTRL_ID_LOAD]);
+  MapGadget(game_gadget[GAME_CTRL_ID_SAVE]);
 }
 
 void MapUndoRedoButtons(void)
@@ -16204,15 +16210,6 @@ void MapUndoRedoButtons(void)
   MapGadget(game_gadget[GAME_CTRL_ID_REDO]);
 }
 
-void UnmapUndoRedoButtons(void)
-{
-  UnmapGadget(game_gadget[GAME_CTRL_ID_UNDO]);
-  UnmapGadget(game_gadget[GAME_CTRL_ID_REDO]);
-
-  MapGameButtonsAtSamePosition(GAME_CTRL_ID_UNDO);
-  MapGameButtonsAtSamePosition(GAME_CTRL_ID_REDO);
-}
-
 void ModifyPauseButtons(void)
 {
   static int ids[] =
@@ -16234,9 +16231,7 @@ static void MapGameButtonsExt(boolean on_tape)
   int i;
 
   for (i = 0; i < NUM_GAME_BUTTONS; i++)
-    if ((!on_tape || gamebutton_info[i].allowed_on_tape) &&
-       i != GAME_CTRL_ID_UNDO &&
-       i != GAME_CTRL_ID_REDO)
+    if (!on_tape || gamebutton_info[i].allowed_on_tape)
       MapGadget(game_gadget[i]);
 
   UnmapGameButtonsAtSamePosition_All();
@@ -16328,6 +16323,8 @@ static void GameUndoRedoExt(void)
   DrawVideoDisplay(VIDEO_STATE_FRAME_ON, FrameCounter);
   DrawVideoDisplay(VIDEO_STATE_1STEP(tape.single_step), 0);
 
+  ModifyPauseButtons();
+
   BackToFront();
 }
 
@@ -16336,8 +16333,12 @@ static void GameUndo(int steps)
   if (!CheckEngineSnapshotList())
     return;
 
+  int tape_property_bits = tape.property_bits;
+
   LoadEngineSnapshot_Undo(steps);
 
+  tape.property_bits |= tape_property_bits | TAPE_PROPERTY_SNAPSHOT;
+
   GameUndoRedoExt();
 }
 
@@ -16346,8 +16347,12 @@ static void GameRedo(int steps)
   if (!CheckEngineSnapshotList())
     return;
 
+  int tape_property_bits = tape.property_bits;
+
   LoadEngineSnapshot_Redo(steps);
 
+  tape.property_bits |= tape_property_bits | TAPE_PROPERTY_SNAPSHOT;
+
   GameUndoRedoExt();
 }