fixed nasty bug that stopped animations when defining 'door_2' off-screen
[rocksndiamonds.git] / src / tools.c
index 6ae0780f5bd2f6fb0437df23b1bac0dbc0caf8f1..55a28fc40d0cf13ef90ec1df29c3910bb15e5661 100644 (file)
@@ -192,6 +192,22 @@ static char *print_if_not_empty(int element)
   return s;
 }
 
+int correctLevelPosX_EM(int lx)
+{
+  lx -= 1;
+  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
+
+  return lx;
+}
+
+int correctLevelPosY_EM(int ly)
+{
+  ly -= 1;
+  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
+
+  return ly;
+}
+
 static int getFieldbufferOffsetX_RND()
 {
   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
@@ -256,26 +272,6 @@ static int getFieldbufferOffsetY_RND()
   return fy;
 }
 
-int getFieldbufferOffsetX()
-{
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
-    return getFieldbufferOffsetX_EM();
-  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
-    return getFieldbufferOffsetX_SP();
-  else
-    return getFieldbufferOffsetX_RND();
-}
-
-int getFieldbufferOffsetY()
-{
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
-    return getFieldbufferOffsetY_EM();
-  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
-    return getFieldbufferOffsetY_SP();
-  else
-    return getFieldbufferOffsetY_RND();
-}
-
 static int getLevelFromScreenX_RND(int sx)
 {
   int fx = getFieldbufferOffsetX_RND();
@@ -296,7 +292,7 @@ static int getLevelFromScreenY_RND(int sy)
   return ly;
 }
 
-int getLevelFromScreenX_EM(int sx)
+static int getLevelFromScreenX_EM(int sx)
 {
   int level_xsize = level.native_em_level->lev->width;
   int full_xsize = level_xsize * TILESIZE_VAR;
@@ -306,14 +302,14 @@ int getLevelFromScreenX_EM(int sx)
   int fx = getFieldbufferOffsetX_EM();
   int dx = fx;
   int px = sx - SX;
-  int lx = LEVELX((px + dx) / TILESIZE_VAR) - 1;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
 
-  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
+  lx = correctLevelPosX_EM(lx);
 
   return lx;
 }
 
-int getLevelFromScreenY_EM(int sy)
+static int getLevelFromScreenY_EM(int sy)
 {
   int level_ysize = level.native_em_level->lev->height;
   int full_ysize = level_ysize * TILESIZE_VAR;
@@ -323,14 +319,14 @@ int getLevelFromScreenY_EM(int sy)
   int fy = getFieldbufferOffsetY_EM();
   int dy = fy;
   int py = sy - SY;
-  int ly = LEVELY((py + dy) / TILESIZE_VAR) - 1;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
 
-  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
+  ly = correctLevelPosY_EM(ly);
 
   return ly;
 }
 
-int getLevelFromScreenX_SP(int sx)
+static int getLevelFromScreenX_SP(int sx)
 {
   int menBorder = setup.sp_show_border_elements;
   int level_xsize = level.native_sp_level->width;
@@ -346,7 +342,7 @@ int getLevelFromScreenX_SP(int sx)
   return lx;
 }
 
-int getLevelFromScreenY_SP(int sy)
+static int getLevelFromScreenY_SP(int sy)
 {
   int menBorder = setup.sp_show_border_elements;
   int level_ysize = level.native_sp_level->height;
@@ -387,14 +383,6 @@ void DumpTile(int x, int y)
   int sx = SCREENX(x);
   int sy = SCREENY(y);
 
-#if 0
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
-  {
-    x--;
-    y--;
-  }
-#endif
-
   printf_line("-", 79);
   printf("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
   printf_line("-", 79);
@@ -421,6 +409,7 @@ void DumpTile(int x, int y)
   printf("  GfxElement:  %d\n", GfxElement[x][y]);
   printf("  GfxAction:   %d\n", GfxAction[x][y]);
   printf("  GfxFrame:    %d [%d]\n", GfxFrame[x][y], FrameCounter);
+  printf("  Player x/y:  %d, %d\n", local_player->jx, local_player->jy);
   printf("\n");
 }
 
@@ -770,6 +759,13 @@ void BackToFront()
       y2 = MAX(y2, EY + EYSIZE);
     }
 
+    // make sure that at least one pixel is blitted, and inside the screen
+    // (else nothing is blitted, causing the animations not to be updated)
+    x1 = MIN(MAX(0, x1), WIN_XSIZE - 1);
+    y1 = MIN(MAX(0, y1), WIN_YSIZE - 1);
+    x2 = MIN(MAX(1, x2), WIN_XSIZE);
+    y2 = MIN(MAX(1, y2), WIN_YSIZE);
+
     BlitBitmap(backbuffer, window, x1, y1, x2 - x1, y2 - y1, x1, y1);
   }
 
@@ -937,6 +933,10 @@ void FadeIn(int fade_mask)
   FADE_SXSIZE = FULL_SXSIZE;
   FADE_SYSIZE = FULL_SYSIZE;
 
+  if (game_status == GAME_MODE_PLAYING &&
+      strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
+    SetOverlayActive(TRUE);
+
   SetScreenStates_AfterFadingIn();
 
   // force update of global animation status in case of rapid screen changes
@@ -952,6 +952,8 @@ void FadeOut(int fade_mask)
 
   SetScreenStates_BeforeFadingOut();
 
+  SetOverlayActive(FALSE);
+
 #if 0
   DrawMaskedBorder(REDRAW_ALL);
 #endif
@@ -2333,6 +2335,10 @@ void DrawScreenField(int x, int y)
       int newly = ly + (dir == MV_UP   ? -1 : dir == MV_DOWN  ? +1 : 0);
 
       DrawLevelElementThruMask(newlx, newly, EL_ACID);
+
+      // prevent target field from being drawn again (but without masking)
+      // (this would happen if target field is scanned after moving element)
+      Stop[newlx][newly] = TRUE;
     }
   }
   else if (IS_BLOCKED(lx, ly))
@@ -4218,10 +4224,19 @@ static boolean RequestEnvelope(char *text, unsigned int req_state)
 
 boolean Request(char *text, unsigned int req_state)
 {
+  boolean overlay_active = GetOverlayActive();
+  boolean result;
+
+  SetOverlayActive(FALSE);
+
   if (global.use_envelope_request)
-    return RequestEnvelope(text, req_state);
+    result = RequestEnvelope(text, req_state);
   else
-    return RequestDoor(text, req_state);
+    result = RequestDoor(text, req_state);
+
+  SetOverlayActive(overlay_active);
+
+  return result;
 }
 
 static int compareDoorPartOrderInfo(const void *object1, const void *object2)
@@ -8390,7 +8405,37 @@ void PlayMenuMusicExt(int music)
 
 void PlayMenuMusic()
 {
-  PlayMenuMusicExt(menu.music[game_status]);
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    PlayMenuMusicExt(menu.music[game_status]);
+}
+
+void PlayMenuSoundsAndMusic()
+{
+  PlayMenuSound();
+  PlayMenuMusic();
+}
+
+static void FadeMenuSounds()
+{
+  FadeSounds();
+}
+
+static void FadeMenuMusic()
+{
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    FadeMusic();
+}
+
+void FadeMenuSoundsAndMusic()
+{
+  FadeMenuSounds();
+  FadeMenuMusic();
 }
 
 void PlaySoundActivating()
@@ -8499,6 +8544,9 @@ void SetAnimStatus(int anim_status_new)
 
 void SetGameStatus(int game_status_new)
 {
+  if (game_status_new != game_status)
+    game_status_last_screen = game_status;
+
   game_status = game_status_new;
 
   SetAnimStatus(game_status_new);