rnd-20030105-1-src
[rocksndiamonds.git] / src / game.c
index 4f3f3cab928b23f3146b9353c9fa6cff061bfac2..17473731b737bf0b128d41559df8682fb5d84eb5 100644 (file)
 #define NUM_GAME_BUTTONS               6
 
 /* forward declaration for internal use */
+static void InitBeltMovement(void);
 static void CloseAllOpenTimegates(void);
 static void CheckGravityMovement(struct PlayerInfo *);
 static void KillHeroUnlessProtected(int, int);
@@ -487,7 +488,7 @@ void DrawGameDoorValues()
     for (j=0; j<4; j++)
       if (stored_player[i].key[j])
        DrawMiniGraphicExt(drawto, DX_KEYS + j * MINI_TILEX, DY_KEYS,
-                          GFX_SCHLUESSEL1 + j);
+                          IMG_KEY1 + j);
 
   DrawText(DX + XX_EMERALDS, DY + YY_EMERALDS,
           int2str(local_player->gems_still_needed, 3), FS_SMALL, FC_YELLOW);
@@ -820,6 +821,8 @@ void InitGame()
     }
   }
 
+  InitBeltMovement();
+
   game.emulation = (emulate_bd ? EMU_BOULDERDASH :
                    emulate_sb ? EMU_SOKOBAN :
                    emulate_sp ? EMU_SUPAPLEX : EMU_NONE);
@@ -959,7 +962,6 @@ void InitGame()
 
   DrawLevel();
   DrawAllPlayers();
-  FadeToFront();
 
   /* after drawing the level, correct some elements */
   if (game.timegate_time_left == 0)
@@ -969,6 +971,7 @@ void InitGame()
     BlitBitmap(fieldbuffer, backbuffer, FX, FY, SXSIZE, SYSIZE, SX, SY);
 
   redraw_mask |= REDRAW_FROM_BACKBUFFER;
+  FadeToFront();
 
   /* copy default game door content to main double buffer */
   BlitBitmap(new_graphic_info[IMG_MENU_DOOR].bitmap, drawto,
@@ -980,7 +983,7 @@ void InitGame()
   else
   {
     DrawTextExt(drawto, DX + XX_EMERALDS, DY + YY_EMERALDS,
-               int2str(level_nr, 3), FS_SMALL, FC_SPECIAL3);
+               int2str(level_nr, 3), FS_SMALL, FC_SPECIAL3, FONT_OPAQUE);
     BlitBitmap(drawto, drawto,
               DX + XX_EMERALDS, DY + YY_EMERALDS + 1,
               FONT5_XSIZE * 3, FONT5_YSIZE - 1,
@@ -1154,6 +1157,9 @@ void GameWon()
   if (local_player->MovPos)
     return;
 
+  if (tape.playing && tape.auto_play)
+    tape.auto_play_level_solved = TRUE;
+
   local_player->LevelSolved = FALSE;
 
   PlaySoundStereo(SND_GAME_WINNING, SOUND_MAX_RIGHT);
@@ -1879,6 +1885,67 @@ void Blurb(int x, int y)
   }
 }
 
+static void InitBeltMovement()
+{
+  static int belt_base_element[4] =
+  {
+    EL_CONVEYOR_BELT1_LEFT,
+    EL_CONVEYOR_BELT2_LEFT,
+    EL_CONVEYOR_BELT3_LEFT,
+    EL_CONVEYOR_BELT4_LEFT
+  };
+  static int belt_base_active_element[4] =
+  {
+    EL_CONVEYOR_BELT1_LEFT_ACTIVE,
+    EL_CONVEYOR_BELT2_LEFT_ACTIVE,
+    EL_CONVEYOR_BELT3_LEFT_ACTIVE,
+    EL_CONVEYOR_BELT4_LEFT_ACTIVE
+  };
+
+  int x, y, i, j;
+
+  /* set frame order for belt animation graphic according to belt direction */
+  for (i=0; i<4; i++)
+  {
+    int belt_nr = i;
+
+    for (j=0; j<3; j++)
+    {
+      int element = belt_base_active_element[belt_nr] + j;
+      int graphic = el2img(element);
+
+      if (game.belt_dir[i] == MV_LEFT)
+       new_graphic_info[graphic].anim_mode &= ~ANIM_REVERSE;
+      else
+       new_graphic_info[graphic].anim_mode |=  ANIM_REVERSE;
+    }
+  }
+
+  for(y=0; y<lev_fieldy; y++)
+  {
+    for(x=0; x<lev_fieldx; x++)
+    {
+      int element = Feld[x][y];
+
+      for (i=0; i<4; i++)
+      {
+       if (IS_BELT(element) && game.belt_dir[i] != MV_NO_MOVING)
+       {
+         int e_belt_nr = getBeltNrFromBeltElement(element);
+         int belt_nr = i;
+
+         if (e_belt_nr == belt_nr)
+         {
+           int belt_part = Feld[x][y] - belt_base_element[belt_nr];
+
+           Feld[x][y] = belt_base_active_element[belt_nr] + belt_part;
+         }
+       }
+      }
+    }
+  }
+}
+
 static void ToggleBeltSwitch(int x, int y)
 {
   static int belt_base_element[4] =
@@ -2740,6 +2807,8 @@ static boolean JustBeingPushed(int x, int y)
 
 void StartMoving(int x, int y)
 {
+  static boolean use_spring_bug = TRUE;
+  boolean started_moving = FALSE;      /* some elements can fall _and_ move */
   int element = Feld[x][y];
 
   if (Stop[x][y])
@@ -2747,7 +2816,7 @@ void StartMoving(int x, int y)
 
   GfxAction[x][y] = GFX_ACTION_DEFAULT;
 
-  if (CAN_FALL(element) && y<lev_fieldy-1)
+  if (CAN_FALL(element) && y < lev_fieldy - 1)
   {
     if ((x>0 && IS_PLAYER(x-1, y)) || (x<lev_fieldx-1 && IS_PLAYER(x+1, y)))
       if (JustBeingPushed(x, y))
@@ -2758,6 +2827,8 @@ void StartMoving(int x, int y)
       if (IS_FREE(x, y+1))
       {
        InitMovingField(x, y, MV_DOWN);
+       started_moving = TRUE;
+
        Feld[x][y] = EL_QUICKSAND_EMPTYING;
        Store[x][y] = EL_ROCK;
        PlaySoundLevel(x, y, SND_QUICKSAND_EMPTYING);
@@ -2785,6 +2856,8 @@ void StartMoving(int x, int y)
             Feld[x][y+1] == EL_QUICKSAND_EMPTY)
     {
       InitMovingField(x, y, MV_DOWN);
+      started_moving = TRUE;
+
       Feld[x][y] = EL_QUICKSAND_FILLING;
       Store[x][y] = element;
       PlaySoundLevel(x, y, SND_QUICKSAND_FILLING);
@@ -2794,6 +2867,8 @@ void StartMoving(int x, int y)
       if (IS_FREE(x, y+1))
       {
        InitMovingField(x, y, MV_DOWN);
+       started_moving = TRUE;
+
        Feld[x][y] = EL_MAGIC_WALL_EMPTYING;
        Store[x][y] = EL_CHANGED(Store[x][y]);
       }
@@ -2820,6 +2895,8 @@ void StartMoving(int x, int y)
       if (IS_FREE(x, y+1))
       {
        InitMovingField(x, y, MV_DOWN);
+       started_moving = TRUE;
+
        Feld[x][y] = EL_BD_MAGIC_WALL_EMPTYING;
        Store[x][y] = EL_CHANGED2(Store[x][y]);
       }
@@ -2846,6 +2923,8 @@ void StartMoving(int x, int y)
              Feld[x][y+1] == EL_BD_MAGIC_WALL_ACTIVE))
     {
       InitMovingField(x, y, MV_DOWN);
+      started_moving = TRUE;
+
       Feld[x][y] =
        (Feld[x][y+1] == EL_MAGIC_WALL_ACTIVE ? EL_MAGIC_WALL_FILLING :
         EL_BD_MAGIC_WALL_FILLING);
@@ -2854,7 +2933,10 @@ void StartMoving(int x, int y)
     else if (CAN_SMASH(element) && Feld[x][y+1] == EL_ACID)
     {
       Blurb(x, y);
+
       InitMovingField(x, y, MV_DOWN);
+      started_moving = TRUE;
+
       Store[x][y] = EL_ACID;
     }
     else if (CAN_SMASH(element) && Feld[x][y+1] == EL_BLOCKED &&
@@ -2862,9 +2944,18 @@ void StartMoving(int x, int y)
     {
       Impact(x, y);
     }
+    else if (IS_FREE(x, y+1) && element == EL_SPRING && use_spring_bug)
+    {
+      if (MovDir[x][y] == MV_NO_MOVING)
+      {
+       InitMovingField(x, y, MV_DOWN);
+       started_moving = TRUE;
+      }
+    }
     else if (IS_FREE(x, y+1))
     {
       InitMovingField(x, y, MV_DOWN);
+      started_moving = TRUE;
     }
     else if (element == EL_AMOEBA_DROP)
     {
@@ -2902,6 +2993,7 @@ void StartMoving(int x, int y)
          left = !(right = RND(2));
 
        InitMovingField(x, y, left ? MV_LEFT : MV_RIGHT);
+       started_moving = TRUE;
       }
     }
     else if (IS_BELT_ACTIVE(Feld[x][y+1]))
@@ -2915,12 +3007,15 @@ void StartMoving(int x, int y)
          (belt_dir == MV_RIGHT && right_is_free))
       {
        InitMovingField(x, y, belt_dir);
+       started_moving = TRUE;
+
        GfxAction[x][y] = GFX_ACTION_DEFAULT;
       }
     }
   }
 
-  if (CAN_MOVE(element))       /* not "else if" because of EL_SPRING */
+  /* not "else if" because of EL_SPRING */
+  if (CAN_MOVE(element) && !started_moving)
   {
     int newx, newy;
 
@@ -2930,8 +3025,19 @@ void StartMoving(int x, int y)
        && JustBeingPushed(x, y))
       return;
 
+#if 0
+#if 0
     if (element == EL_SPRING && MovDir[x][y] == MV_DOWN)
       Feld[x][y + 1] = EL_EMPTY;       /* was set to EL_BLOCKED above */
+#else
+    if (element == EL_SPRING && MovDir[x][y] != MV_NO_MOVING)
+    {
+      Moving2Blocked(x, y, &newx, &newy);
+      if (Feld[newx][newy] == EL_BLOCKED)
+       Feld[newx][newy] = EL_EMPTY;    /* was set to EL_BLOCKED above */
+    }
+#endif
+#endif
 
     if (!MovDelay[x][y])       /* start new movement phase */
     {
@@ -4077,9 +4183,10 @@ void AusgangstuerOeffnen(int x, int y)
 
        DrawGraphic(SCREENX(x), SCREENY(y), IMG_EXIT_OPENING, frame);
       }
+    }
 
+    if (MovDelay[x][y])
       return;
-    }
   }
 
   Feld[x][y] = EL_EXIT_OPEN;
@@ -4106,9 +4213,10 @@ void OpenSwitchgate(int x, int y)
 
        DrawGraphic(SCREENX(x), SCREENY(y), IMG_SWITCHGATE_OPENING, frame);
       }
+    }
 
+    if (MovDelay[x][y])
       return;
-    }
   }
 
   Feld[x][y] = EL_SWITCHGATE_OPEN;
@@ -4135,9 +4243,10 @@ void CloseSwitchgate(int x, int y)
 
        DrawGraphic(SCREENX(x), SCREENY(y), IMG_SWITCHGATE_CLOSING, frame);
       }
+    }
 
+    if (MovDelay[x][y])
       return;
-    }
   }
 
   Feld[x][y] = EL_SWITCHGATE_CLOSED;
@@ -4164,9 +4273,10 @@ void OpenTimegate(int x, int y)
 
        DrawGraphic(SCREENX(x), SCREENY(y), IMG_TIMEGATE_OPENING, frame);
       }
+    }
 
+    if (MovDelay[x][y])
       return;
-    }
   }
 
   Feld[x][y] = EL_TIMEGATE_OPEN;
@@ -4193,9 +4303,10 @@ void CloseTimegate(int x, int y)
 
        DrawGraphic(SCREENX(x), SCREENY(y), IMG_TIMEGATE_CLOSING, frame);
       }
+    }
 
+    if (MovDelay[x][y])
       return;
-    }
   }
 
   Feld[x][y] = EL_TIMEGATE_CLOSED;
@@ -4751,7 +4862,7 @@ void GameActions()
   static unsigned long action_delay = 0;
   unsigned long action_delay_value;
   int sieb_x = 0, sieb_y = 0;
-  int i, x, y, element;
+  int i, x, y, element, graphic;
   byte *recorded_player_action;
   byte summarized_player_action = 0;
 
@@ -4855,8 +4966,6 @@ void GameActions()
 #endif
 #endif
 
-
-
   FrameCounter++;
   TimeFrames++;
 
@@ -4886,9 +4995,18 @@ void GameActions()
   for (y=0; y<lev_fieldy; y++) for (x=0; x<lev_fieldx; x++)
   {
     element = Feld[x][y];
+    graphic = el2img(element);
 
     if (IS_INACTIVE(element))
+    {
+
+#if 1
+      if (new_graphic_info[graphic].anim_frames > 1)
+       DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
+#endif
+
       continue;
+    }
 
     if (!IS_MOVING(x, y) && (CAN_FALL(element) || CAN_MOVE(element)))
     {
@@ -4896,14 +5014,28 @@ void GameActions()
 
 #if 1
       if (Feld[x][y] == EL_EMERALD &&
-         new_graphic_info[IMG_EMERALD].anim_frames > 1 &&
+         new_graphic_info[graphic].anim_frames > 1 &&
          !IS_MOVING(x, y))
-       DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_EMERALD);
+       DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
 #endif
 
       if (IS_GEM(element) || element == EL_SP_INFOTRON)
        EdelsteinFunkeln(x, y);
     }
+
+#if 1
+    else if ((element == EL_ACID ||
+             element == EL_EXIT_OPEN ||
+             element == EL_SP_EXIT_OPEN ||
+             element == EL_SP_TERMINAL ||
+             element == EL_SP_TERMINAL_ACTIVE ||
+             element == EL_EXTRA_TIME ||
+             element == EL_SHIELD_NORMAL ||
+             element == EL_SHIELD_DEADLY) &&
+            new_graphic_info[graphic].anim_frames > 1)
+      DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
+#endif
+
     else if (IS_MOVING(x, y))
       ContinueMoving(x, y);
     else if (IS_ACTIVE_BOMB(element))
@@ -4928,8 +5060,6 @@ void GameActions()
       RobotWheel(x, y);
     else if (element == EL_TIMEGATE_SWITCH_ACTIVE)
       TimegateWheel(x, y);
-    else if (element == EL_ACID)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_ACID);
     else if (element == EL_ACID_SPLASH_LEFT ||
             element == EL_ACID_SPLASH_RIGHT)
       Blurb(x, y);
@@ -4943,10 +5073,6 @@ void GameActions()
       AusgangstuerPruefen_SP(x, y);
     else if (element == EL_EXIT_OPENING)
       AusgangstuerOeffnen(x, y);
-    else if (element == EL_EXIT_OPEN)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_EXIT_OPEN);
-    else if (element == EL_SP_EXIT_OPEN)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_SP_EXIT_OPEN);
     else if (element == EL_WALL_GROWING_ACTIVE)
       MauerWaechst(x, y);
     else if (element == EL_WALL_GROWING ||
@@ -4960,10 +5086,6 @@ void GameActions()
       CheckBuggyBase(x, y);
     else if (element == EL_TRAP || element == EL_TRAP_ACTIVE)
       CheckTrap(x, y);
-    else if (element == EL_SP_TERMINAL)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_SP_TERMINAL);
-    else if (element == EL_SP_TERMINAL_ACTIVE)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_SP_TERMINAL_ACTIVE);
     else if (IS_BELT_ACTIVE(element))
       DrawBeltAnimation(x, y, element);
     else if (element == EL_SWITCHGATE_OPENING)
@@ -4974,12 +5096,11 @@ void GameActions()
       OpenTimegate(x, y);
     else if (element == EL_TIMEGATE_CLOSING)
       CloseTimegate(x, y);
-    else if (element == EL_EXTRA_TIME)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_EXTRA_TIME);
-    else if (element == EL_SHIELD_NORMAL)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_SHIELD_NORMAL);
-    else if (element == EL_SHIELD_DEADLY)
-      DrawGraphicAnimation(SCREENX(x), SCREENY(y), IMG_SHIELD_DEADLY);
+
+#if 1
+    else if (new_graphic_info[graphic].anim_frames > 1)
+      DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
+#endif
 
     if (game.magic_wall_active)
     {
@@ -5391,7 +5512,8 @@ boolean MoveFigure(struct PlayerInfo *player, int dx, int dy)
     int original_move_delay_value = player->move_delay_value;
 
 #if DEBUG
-    printf("THIS SHOULD ONLY HAPPEN WITH PRE-1.2 LEVEL TAPES.\n");
+    printf("THIS SHOULD ONLY HAPPEN WITH PRE-1.2 LEVEL TAPES. [%ld]\n",
+          tape.counter);
 #endif
 
     /* scroll remaining steps with finest movement resolution */
@@ -6081,9 +6203,9 @@ int DigField(struct PlayerInfo *player,
       player->key[key_nr] = TRUE;
       RaiseScoreElement(element);
       DrawMiniGraphicExt(drawto, DX_KEYS + key_nr * MINI_TILEX, DY_KEYS,
-                        GFX_SCHLUESSEL1 + key_nr);
+                        IMG_KEY1 + key_nr);
       DrawMiniGraphicExt(window, DX_KEYS + key_nr * MINI_TILEX, DY_KEYS,
-                        GFX_SCHLUESSEL1 + key_nr);
+                        IMG_KEY1 + key_nr);
       PlaySoundLevel(x, y, SND_KEY_COLLECTING);
       break;
     }
@@ -6099,9 +6221,9 @@ int DigField(struct PlayerInfo *player,
       player->key[key_nr] = TRUE;
       RaiseScoreElement(element);
       DrawMiniGraphicExt(drawto, DX_KEYS + key_nr * MINI_TILEX, DY_KEYS,
-                        GFX_SCHLUESSEL1 + key_nr);
+                        IMG_KEY1 + key_nr);
       DrawMiniGraphicExt(window, DX_KEYS + key_nr * MINI_TILEX, DY_KEYS,
-                        GFX_SCHLUESSEL1 + key_nr);
+                        IMG_KEY1 + key_nr);
       PlaySoundLevel(x, y, SND_KEY_COLLECTING);
       break;
     }
@@ -6907,6 +7029,14 @@ void CreateGameButtons()
   }
 }
 
+void FreeGameButtons()
+{
+  int i;
+
+  for (i=0; i<NUM_GAME_BUTTONS; i++)
+    FreeGadget(game_gadget[i]);
+}
+
 static void MapGameButtons()
 {
   int i;