fixed nasty bug that stopped animations when defining 'door_2' off-screen
[rocksndiamonds.git] / src / tools.c
index 7888dca70b1addb1f994aae8d73035ee69c35e75..55a28fc40d0cf13ef90ec1df29c3910bb15e5661 100644 (file)
@@ -192,17 +192,197 @@ static char *print_if_not_empty(int element)
   return s;
 }
 
-void DumpTile(int x, int y)
+int correctLevelPosX_EM(int lx)
 {
-  int sx = SCREENX(x);
-  int sy = SCREENY(y);
+  lx -= 1;
+  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
 
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+  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);
+  int dx = (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
+  int dx_var = dx * TILESIZE_VAR / TILESIZE;
+  int fx = FX;
+
+  if (EVEN(SCR_FIELDX))
+  {
+    int ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
+
+    if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
+      fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
+    else
+      fx += (dx_var > 0 ? TILEX_VAR : 0);
+  }
+  else
+  {
+    fx += dx_var;
+  }
+
+  if (full_lev_fieldx <= SCR_FIELDX)
+  {
+    if (EVEN(SCR_FIELDX))
+      fx = 2 * TILEX_VAR - (ODD(lev_fieldx)  ? TILEX_VAR / 2 : 0);
+    else
+      fx = 2 * TILEX_VAR - (EVEN(lev_fieldx) ? TILEX_VAR / 2 : 0);
+  }
+
+  return fx;
+}
+
+static int getFieldbufferOffsetY_RND()
+{
+  int full_lev_fieldy = lev_fieldy + (BorderElement != EL_EMPTY ? 2 : 0);
+  int dy = (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
+  int dy_var = dy * TILESIZE_VAR / TILESIZE;
+  int fy = FY;
+
+  if (EVEN(SCR_FIELDY))
+  {
+    int ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
+
+    if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
+      fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
+    else
+      fy += (dy_var > 0 ? TILEY_VAR : 0);
+  }
+  else
+  {
+    fy += dy_var;
+  }
+
+  if (full_lev_fieldy <= SCR_FIELDY)
   {
-    x--;
-    y--;
+    if (EVEN(SCR_FIELDY))
+      fy = 2 * TILEY_VAR - (ODD(lev_fieldy)  ? TILEY_VAR / 2 : 0);
+    else
+      fy = 2 * TILEY_VAR - (EVEN(lev_fieldy) ? TILEY_VAR / 2 : 0);
   }
 
+  return fy;
+}
+
+static int getLevelFromScreenX_RND(int sx)
+{
+  int fx = getFieldbufferOffsetX_RND();
+  int dx = fx - FX;
+  int px = sx - SX;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
+
+  return lx;
+}
+
+static int getLevelFromScreenY_RND(int sy)
+{
+  int fy = getFieldbufferOffsetY_RND();
+  int dy = fy - FY;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  return ly;
+}
+
+static int getLevelFromScreenX_EM(int sx)
+{
+  int level_xsize = level.native_em_level->lev->width;
+  int full_xsize = level_xsize * TILESIZE_VAR;
+
+  sx -= (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
+
+  int fx = getFieldbufferOffsetX_EM();
+  int dx = fx;
+  int px = sx - SX;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
+
+  lx = correctLevelPosX_EM(lx);
+
+  return lx;
+}
+
+static int getLevelFromScreenY_EM(int sy)
+{
+  int level_ysize = level.native_em_level->lev->height;
+  int full_ysize = level_ysize * TILESIZE_VAR;
+
+  sy -= (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
+
+  int fy = getFieldbufferOffsetY_EM();
+  int dy = fy;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  ly = correctLevelPosY_EM(ly);
+
+  return ly;
+}
+
+static int getLevelFromScreenX_SP(int sx)
+{
+  int menBorder = setup.sp_show_border_elements;
+  int level_xsize = level.native_sp_level->width;
+  int full_xsize = (level_xsize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
+
+  sx += (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
+
+  int fx = getFieldbufferOffsetX_SP();
+  int dx = fx - FX;
+  int px = sx - SX;
+  int lx = LEVELX((px + dx) / TILESIZE_VAR);
+
+  return lx;
+}
+
+static int getLevelFromScreenY_SP(int sy)
+{
+  int menBorder = setup.sp_show_border_elements;
+  int level_ysize = level.native_sp_level->height;
+  int full_ysize = (level_ysize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
+
+  sy += (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
+
+  int fy = getFieldbufferOffsetY_SP();
+  int dy = fy - FY;
+  int py = sy - SY;
+  int ly = LEVELY((py + dy) / TILESIZE_VAR);
+
+  return ly;
+}
+
+int getLevelFromScreenX(int x)
+{
+  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+    return getLevelFromScreenX_EM(x);
+  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
+    return getLevelFromScreenX_SP(x);
+  else
+    return getLevelFromScreenX_RND(x);
+}
+
+int getLevelFromScreenY(int y)
+{
+  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
+    return getLevelFromScreenY_EM(y);
+  if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
+    return getLevelFromScreenY_SP(y);
+  else
+    return getLevelFromScreenY_RND(y);
+}
+
+void DumpTile(int x, int y)
+{
+  int sx = SCREENX(x);
+  int sy = SCREENY(y);
+
   printf_line("-", 79);
   printf("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
   printf_line("-", 79);
@@ -229,9 +409,18 @@ 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");
 }
 
+void DumpTileFromScreen(int sx, int sy)
+{
+  int lx = getLevelFromScreenX(sx);
+  int ly = getLevelFromScreenY(sy);
+
+  DumpTile(lx, ly);
+}
+
 void SetDrawtoField(int mode)
 {
   if (mode == DRAW_TO_FIELDBUFFER)
@@ -410,58 +599,8 @@ void DrawMaskedBorderToTarget(int draw_target)
 
 void BlitScreenToBitmap_RND(Bitmap *target_bitmap)
 {
-  int fx = FX, fy = FY;
-  int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
-  int full_lev_fieldy = lev_fieldy + (BorderElement != EL_EMPTY ? 2 : 0);
-
-  int dx = (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
-  int dy = (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
-  int dx_var = dx * TILESIZE_VAR / TILESIZE;
-  int dy_var = dy * TILESIZE_VAR / TILESIZE;
-  int ffx, ffy;
-
-  ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
-  ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
-
-  if (EVEN(SCR_FIELDX))
-  {
-    if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
-      fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
-    else
-      fx += (dx_var > 0 ? TILEX_VAR : 0);
-  }
-  else
-  {
-    fx += dx_var;
-  }
-
-  if (EVEN(SCR_FIELDY))
-  {
-    if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
-      fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
-    else
-      fy += (dy_var > 0 ? TILEY_VAR : 0);
-  }
-  else
-  {
-    fy += dy_var;
-  }
-
-  if (full_lev_fieldx <= SCR_FIELDX)
-  {
-    if (EVEN(SCR_FIELDX))
-      fx = 2 * TILEX_VAR - (ODD(lev_fieldx)  ? TILEX_VAR / 2 : 0);
-    else
-      fx = 2 * TILEX_VAR - (EVEN(lev_fieldx) ? TILEX_VAR / 2 : 0);
-  }
-
-  if (full_lev_fieldy <= SCR_FIELDY)
-  {
-    if (EVEN(SCR_FIELDY))
-      fy = 2 * TILEY_VAR - (ODD(lev_fieldy)  ? TILEY_VAR / 2 : 0);
-    else
-      fy = 2 * TILEY_VAR - (EVEN(lev_fieldy) ? TILEY_VAR / 2 : 0);
-  }
+  int fx = getFieldbufferOffsetX_RND();
+  int fy = getFieldbufferOffsetY_RND();
 
   BlitBitmap(drawto_field, target_bitmap, fx, fy, SXSIZE, SYSIZE, SX, SY);
 }
@@ -620,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);
   }
 
@@ -787,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
@@ -802,6 +952,8 @@ void FadeOut(int fade_mask)
 
   SetScreenStates_BeforeFadingOut();
 
+  SetOverlayActive(FALSE);
+
 #if 0
   DrawMaskedBorder(REDRAW_ALL);
 #endif
@@ -2183,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))
@@ -4068,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)
@@ -8240,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()
@@ -8349,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);