added lag prevention to door/request/envelope animations
[rocksndiamonds.git] / src / tools.c
index a95b1f795ec8a65a1117732f8ab5add91214f933..edc742da651a1f87cce68b41723cf6fb7b2a4be1 100644 (file)
@@ -2127,7 +2127,7 @@ void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
   boolean no_delay = (tape.warp_forward);
   unsigned int anim_delay = 0;
   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
-  int anim_delay_value = (no_delay ? 0 : frame_delay_value);
+  int anim_delay_value = (no_delay ? 0 : frame_delay_value) / 2;
   int font_nr = FONT_ENVELOPE_1 + envelope_nr;
   int font_width = getFontWidth(font_nr);
   int font_height = getFontHeight(font_nr);
@@ -2139,10 +2139,15 @@ void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize : 0);
   int xstep = (xstart < xend ? 1 : 0);
   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
-  int x, y;
+  int start = 0;
+  int end = MAX(xend - xstart, yend - ystart);
+  int i;
 
-  for (x = xstart, y = ystart; x <= xend && y <= yend; x += xstep, y += ystep)
+  for (i = start; i <= end; i++)
   {
+    int last_frame = end;      // last frame of this "for" loop
+    int x = xstart + i * xstep;
+    int y = ystart + i * ystep;
     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
     int sx = SX + (SXSIZE - xsize * font_width)  / 2;
@@ -2168,7 +2173,7 @@ void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
     redraw_mask |= REDRAW_FIELD | REDRAW_FROM_BACKBUFFER;
     BackToFront();
 
-    WaitUntilDelayReached(&anim_delay, anim_delay_value / 2);
+    SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame);
   }
 }
 
@@ -2319,7 +2324,7 @@ void AnimateEnvelopeRequest(int anim_mode, int action)
   boolean ffwd_delay = (tape.playing && tape.fast_forward);
   boolean no_delay = (tape.warp_forward);
   int delay_value = (ffwd_delay ? delay_value_fast : delay_value_normal);
-  int anim_delay_value = (no_delay ? 0 : delay_value + 500 * 0);
+  int anim_delay_value = (no_delay ? 0 : delay_value + 500 * 0) / 2;
   unsigned int anim_delay = 0;
 
   int width = request.width;
@@ -2336,12 +2341,15 @@ void AnimateEnvelopeRequest(int anim_mode, int action)
   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize_inner : 0);
   int xstep = (xstart < xend ? 1 : 0);
   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
-  int x, y;
+  int start = 0;
+  int end = MAX(xend - xstart, yend - ystart);
+  int i;
 
   if (setup.quick_doors)
   {
     xstart = xend;
     ystart = yend;
+    end = 0;
   }
   else
   {
@@ -2351,8 +2359,11 @@ void AnimateEnvelopeRequest(int anim_mode, int action)
       PlayMenuSoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
   }
 
-  for (x = xstart, y = ystart; x <= xend && y <= yend; x += xstep, y += ystep)
+  for (i = start; i <= end; i++)
   {
+    int last_frame = end;      // last frame of this "for" loop
+    int x = xstart + i * xstep;
+    int y = ystart + i * ystep;
     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
     int sx_center = (request.x != -1 ? request.x : SX + SXSIZE / 2);
@@ -2394,7 +2405,7 @@ void AnimateEnvelopeRequest(int anim_mode, int action)
     DoAnimation();
     BackToFront();
 
-    WaitUntilDelayReached(&anim_delay, anim_delay_value / 2);
+    SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame);
   }
 }
 
@@ -4207,6 +4218,8 @@ unsigned int MoveDoor(unsigned int door_state)
 
     for (k = start; k < num_move_steps; k++)
     {
+      int last_frame = num_move_steps - 1;     // last frame of this "for" loop
+
       door_part_done_all = TRUE;
 
       for (i = 0; i < NUM_DOORS; i++)
@@ -4365,7 +4378,7 @@ unsigned int MoveDoor(unsigned int door_state)
        if (game_status == GAME_MODE_MAIN)
          DoAnimation();
 
-       WaitUntilDelayReached(&door_delay, door_delay_value);
+       SkipUntilDelayReached(&door_delay, door_delay_value, &k, last_frame);
 
        current_move_delay += max_step_delay;
       }
@@ -7794,13 +7807,61 @@ void InitGraphicInfo_EM(void)
 #endif
 }
 
+void CheckSaveEngineSnapshot_EM(byte action[MAX_PLAYERS], int frame,
+                               boolean any_player_moving,
+                               boolean any_player_snapping,
+                               boolean any_player_dropping)
+{
+  static boolean player_was_waiting = TRUE;
+
+  if (frame == 0 && !any_player_dropping)
+  {
+    if (!player_was_waiting)
+    {
+      if (!SaveEngineSnapshotToList())
+       return;
+
+      player_was_waiting = TRUE;
+    }
+  }
+  else if (any_player_moving || any_player_snapping || any_player_dropping)
+  {
+    player_was_waiting = FALSE;
+  }
+}
+
+void CheckSaveEngineSnapshot_SP(boolean murphy_is_waiting,
+                               boolean murphy_is_dropping)
+{
+  static boolean player_was_waiting = TRUE;
+
+  if (murphy_is_waiting)
+  {
+    if (!player_was_waiting)
+    {
+      if (!SaveEngineSnapshotToList())
+       return;
+
+      player_was_waiting = TRUE;
+    }
+  }
+  else
+  {
+    player_was_waiting = FALSE;
+  }
+}
+
 void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
                            boolean any_player_moving,
-                           boolean player_is_dropping)
+                           boolean any_player_snapping,
+                           boolean any_player_dropping)
 {
   if (tape.single_step && tape.recording && !tape.pausing)
-    if (frame == 0 && !player_is_dropping)
+    if (frame == 0 && !any_player_dropping)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
+
+  CheckSaveEngineSnapshot_EM(action, frame, any_player_moving,
+                            any_player_snapping, any_player_dropping);
 }
 
 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
@@ -7809,6 +7870,8 @@ void CheckSingleStepMode_SP(boolean murphy_is_waiting,
   if (tape.single_step && tape.recording && !tape.pausing)
     if (murphy_is_waiting)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
+
+  CheckSaveEngineSnapshot_SP(murphy_is_waiting, murphy_is_dropping);
 }
 
 void getGraphicSource_SP(struct GraphicInfo_SP *g_sp,
@@ -8080,7 +8143,7 @@ void ChangeViewportPropertiesIfNeeded()
       // printf("::: new_tilesize_var != TILESIZE_VAR\n");
 
       // changing tile size invalidates scroll values of engine snapshots
-      FreeEngineSnapshot();
+      FreeEngineSnapshotSingle();
 
       // changing tile size requires update of graphic mapping for EM engine
       init_em_graphics = TRUE;