rnd-20031124-1-src
[rocksndiamonds.git] / src / game.c
index f5c787f6d07a37fc606f0426e3877f04cea1f626..3e8a721b7fc743ea31aade60c3f63ab0322a3e1f 100644 (file)
@@ -182,12 +182,14 @@ static boolean CheckTriggeredElementChange(int, int, int, int);
 static boolean CheckElementSideChange(int, int, int, int, int, int);
 static boolean CheckElementChange(int, int, int, int);
 
-static void PlaySoundLevel(int, int, int);
-static void PlaySoundLevelNearest(int, int, int);
-static void PlaySoundLevelAction(int, int, int);
-static void PlaySoundLevelElementAction(int, int, int, int);
-static void PlaySoundLevelActionIfLoop(int, int, int);
-static void StopSoundLevelActionIfLoop(int, int, int);
+static void PlayLevelSound(int, int, int);
+static void PlayLevelSoundNearest(int, int, int);
+static void PlayLevelSoundAction(int, int, int);
+static void PlayLevelSoundElementAction(int, int, int, int);
+static void PlayLevelSoundElementActionIfLoop(int, int, int, int);
+static void PlayLevelSoundActionIfLoop(int, int, int);
+static void StopLevelSoundActionIfLoop(int, int, int);
+static void PlayLevelMusic();
 
 static void MapGameButtons();
 static void HandleGameButtons(struct GadgetInfo *);
@@ -1058,6 +1060,12 @@ void InitGame()
     player->is_pushing = FALSE;
     player->is_switching = FALSE;
 
+    player->is_bored = FALSE;
+    player->is_sleeping = FALSE;
+
+    player->frame_counter_bored = -1;
+    player->frame_counter_sleeping = -1;
+
     player->switch_x = -1;
     player->switch_y = -1;
 
@@ -1449,8 +1457,9 @@ void InitGame()
   OpenDoor(DOOR_OPEN_ALL);
 
   PlaySoundStereo(SND_GAME_STARTING, SOUND_MIDDLE);
+
   if (setup.sound_music)
-    PlayMusic(level_nr);
+    PlayLevelMusic();
 
   KeyboardAutoRepeatOffUnlessAutoplay();
 
@@ -1717,7 +1726,7 @@ void GameWon()
     Feld[ExitX][ExitY] = (element == EL_EXIT_OPEN ? EL_EXIT_CLOSING :
                          EL_SP_EXIT_CLOSING);
 
-    PlaySoundLevelElementAction(ExitX, ExitY, element, ACTION_CLOSING);
+    PlayLevelSoundElementAction(ExitX, ExitY, element, ACTION_CLOSING);
   }
 
   /* Hero disappears */
@@ -2054,14 +2063,14 @@ void CheckDynamite(int x, int y)
     if (MovDelay[x][y] != 0)
     {
       DrawDynamite(x, y);
-      PlaySoundLevelActionIfLoop(x, y, ACTION_ACTIVE);
+      PlayLevelSoundActionIfLoop(x, y, ACTION_ACTIVE);
 
       return;
     }
   }
 
 #if 1
-  StopSoundLevelActionIfLoop(x, y, ACTION_ACTIVE);
+  StopLevelSoundActionIfLoop(x, y, ACTION_ACTIVE);
 #else
   if (Feld[x][y] == EL_DYNAMITE_ACTIVE ||
       Feld[x][y] == EL_SP_DISK_RED_ACTIVE)
@@ -2174,7 +2183,7 @@ void Explode(int ex, int ey, int phase, int mode)
 #endif
 
     if (mode == EX_NORMAL || mode == EX_CENTER)
-      PlaySoundLevelAction(ex, ey, ACTION_EXPLODING);
+      PlayLevelSoundAction(ex, ey, ACTION_EXPLODING);
 
     /* remove things displayed in background while burning dynamite */
     if (Back[ex][ey] != EL_EMPTY && !IS_INDESTRUCTIBLE(Back[ex][ey]))
@@ -2559,12 +2568,12 @@ void Bang(int x, int y)
 
 #if 0
 #if 1
-  PlaySoundLevelAction(x, y, ACTION_EXPLODING);
+  PlayLevelSoundAction(x, y, ACTION_EXPLODING);
 #else
   if (game.emulation == EMU_SUPAPLEX)
-    PlaySoundLevel(x, y, SND_SP_ELEMENT_EXPLODING);
+    PlayLevelSound(x, y, SND_SP_ELEMENT_EXPLODING);
   else
-    PlaySoundLevel(x, y, SND_ELEMENT_EXPLODING);
+    PlayLevelSound(x, y, SND_ELEMENT_EXPLODING);
 #endif
 #endif
 
@@ -2622,7 +2631,7 @@ void SplashAcid(int x, int y)
   if (element != EL_ACID_SPLASH_LEFT &&
       element != EL_ACID_SPLASH_RIGHT)
   {
-    PlaySoundLevel(x, y, SND_ACID_SPLASHING);
+    PlayLevelSound(x, y, SND_ACID_SPLASHING);
 
     if (IN_LEV_FIELD(x-1, y) && IS_FREE(x-1, y) &&
        (!IN_LEV_FIELD(x-1, y-1) ||
@@ -2822,9 +2831,9 @@ static void ToggleSwitchgateSwitch(int x, int y)
       {
        Feld[xx][yy] = EL_SWITCHGATE_CLOSING;
 #if 1
-       PlaySoundLevelAction(xx, yy, ACTION_CLOSING);
+       PlayLevelSoundAction(xx, yy, ACTION_CLOSING);
 #else
-       PlaySoundLevel(xx, yy, SND_SWITCHGATE_CLOSING);
+       PlayLevelSound(xx, yy, SND_SWITCHGATE_CLOSING);
 #endif
       }
       else if (element == EL_SWITCHGATE_CLOSED ||
@@ -2832,9 +2841,9 @@ static void ToggleSwitchgateSwitch(int x, int y)
       {
        Feld[xx][yy] = EL_SWITCHGATE_OPENING;
 #if 1
-       PlaySoundLevelAction(xx, yy, ACTION_OPENING);
+       PlayLevelSoundAction(xx, yy, ACTION_OPENING);
 #else
-       PlaySoundLevel(xx, yy, SND_SWITCHGATE_OPENING);
+       PlayLevelSound(xx, yy, SND_SWITCHGATE_OPENING);
 #endif
       }
     }
@@ -2928,7 +2937,7 @@ static void ActivateTimegateSwitch(int x, int y)
          element == EL_TIMEGATE_CLOSING)
       {
        Feld[xx][yy] = EL_TIMEGATE_OPENING;
-       PlaySoundLevel(xx, yy, SND_TIMEGATE_OPENING);
+       PlayLevelSound(xx, yy, SND_TIMEGATE_OPENING);
       }
 
       /*
@@ -3018,12 +3027,12 @@ void Impact(int x, int y)
   else if (impact && element == EL_PEARL)
   {
     Feld[x][y] = EL_PEARL_BREAKING;
-    PlaySoundLevel(x, y, SND_PEARL_BREAKING);
+    PlayLevelSound(x, y, SND_PEARL_BREAKING);
     return;
   }
   else if (impact && CheckElementChange(x, y, element, CE_IMPACT))
   {
-    PlaySoundLevelElementAction(x, y, element, ACTION_IMPACT);
+    PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
 
     return;
   }
@@ -3064,7 +3073,7 @@ void Impact(int x, int y)
       game.magic_wall_time_left = level.time_magic_wall * FRAMES_PER_SECOND;
       game.magic_wall_active = TRUE;
 
-      PlaySoundLevel(x, y, (smashed == EL_MAGIC_WALL ?
+      PlayLevelSound(x, y, (smashed == EL_MAGIC_WALL ?
                            SND_MAGIC_WALL_ACTIVATING :
                            SND_BD_MAGIC_WALL_ACTIVATING));
     }
@@ -3128,20 +3137,20 @@ void Impact(int x, int y)
        else if (smashed == EL_NUT)
        {
          Feld[x][y + 1] = EL_NUT_BREAKING;
-         PlaySoundLevel(x, y, SND_NUT_BREAKING);
+         PlayLevelSound(x, y, SND_NUT_BREAKING);
          RaiseScoreElement(EL_NUT);
          return;
        }
        else if (smashed == EL_PEARL)
        {
          Feld[x][y + 1] = EL_PEARL_BREAKING;
-         PlaySoundLevel(x, y, SND_PEARL_BREAKING);
+         PlayLevelSound(x, y, SND_PEARL_BREAKING);
          return;
        }
        else if (smashed == EL_DIAMOND)
        {
          Feld[x][y + 1] = EL_DIAMOND_BREAKING;
-         PlaySoundLevel(x, y, SND_DIAMOND_BREAKING);
+         PlayLevelSound(x, y, SND_DIAMOND_BREAKING);
          return;
        }
        else if (IS_BELT_SWITCH(smashed))
@@ -3181,16 +3190,16 @@ void Impact(int x, int y)
        Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE))
   {
     if (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE)
-      PlaySoundLevel(x, y, SND_MAGIC_WALL_FILLING);
+      PlayLevelSound(x, y, SND_MAGIC_WALL_FILLING);
     else if (Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE)
-      PlaySoundLevel(x, y, SND_BD_MAGIC_WALL_FILLING);
+      PlayLevelSound(x, y, SND_BD_MAGIC_WALL_FILLING);
 
     return;
   }
 
   /* play sound of object that hits the ground */
   if (lastline || object_hit)
-    PlaySoundLevelElementAction(x, y, element, ACTION_IMPACT);
+    PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
 }
 
 inline static void TurnRoundExt(int x, int y)
@@ -3837,9 +3846,9 @@ void StartMoving(int x, int y)
        Feld[x][y] = EL_QUICKSAND_EMPTYING;
        Store[x][y] = EL_ROCK;
 #if 1
-       PlaySoundLevelAction(x, y, ACTION_EMPTYING);
+       PlayLevelSoundAction(x, y, ACTION_EMPTYING);
 #else
-       PlaySoundLevel(x, y, SND_QUICKSAND_EMPTYING);
+       PlayLevelSound(x, y, SND_QUICKSAND_EMPTYING);
 #endif
       }
       else if (Feld[x][y + 1] == EL_QUICKSAND_EMPTY)
@@ -3859,9 +3868,9 @@ void StartMoving(int x, int y)
        Store[x][y + 1] = Store[x][y];
        Store[x][y] = 0;
 #if 1
-       PlaySoundLevelAction(x, y, ACTION_FILLING);
+       PlayLevelSoundAction(x, y, ACTION_FILLING);
 #else
-       PlaySoundLevel(x, y, SND_QUICKSAND_FILLING);
+       PlayLevelSound(x, y, SND_QUICKSAND_FILLING);
 #endif
       }
     }
@@ -3874,9 +3883,9 @@ void StartMoving(int x, int y)
       Feld[x][y] = EL_QUICKSAND_FILLING;
       Store[x][y] = element;
 #if 1
-      PlaySoundLevelAction(x, y, ACTION_FILLING);
+      PlayLevelSoundAction(x, y, ACTION_FILLING);
 #else
-      PlaySoundLevel(x, y, SND_QUICKSAND_FILLING);
+      PlayLevelSound(x, y, SND_QUICKSAND_FILLING);
 #endif
     }
     else if (element == EL_MAGIC_WALL_FULL)
@@ -4193,7 +4202,7 @@ void StartMoving(int x, int y)
 #else
        DrawLevelElementAnimationIfNeeded(x, y, element);
 #endif
-       PlaySoundLevelAction(x, y, ACTION_WAITING);
+       PlayLevelSoundAction(x, y, ACTION_WAITING);
       }
       else if (element == EL_SP_ELECTRON)
        DrawLevelElementAnimationIfNeeded(x, y, element);
@@ -4220,7 +4229,7 @@ void StartMoving(int x, int y)
        else
          DrawLevelField(x, y);
 
-       PlaySoundLevelActionIfLoop(x, y, ACTION_ATTACKING);
+       PlayLevelSoundActionIfLoop(x, y, ACTION_ATTACKING);
 
        for (i=1; i <= 3; i++)
        {
@@ -4260,7 +4269,7 @@ void StartMoving(int x, int y)
 
       if (MovDelay[x][y])      /* element still has to wait some time */
       {
-       PlaySoundLevelAction(x, y, ACTION_WAITING);
+       PlayLevelSoundAction(x, y, ACTION_WAITING);
 
        return;
       }
@@ -4310,7 +4319,7 @@ void StartMoving(int x, int y)
        Feld[x][y] = EL_EMPTY;
        DrawLevelField(x, y);
 
-       PlaySoundLevel(newx, newy, SND_PENGUIN_PASSING);
+       PlayLevelSound(newx, newy, SND_PENGUIN_PASSING);
        if (IN_SCR_FIELD(SCREENX(newx), SCREENY(newy)))
          DrawGraphicThruMask(SCREENX(newx),SCREENY(newy), el2img(element), 0);
 
@@ -4351,7 +4360,7 @@ void StartMoving(int x, int y)
          DrawLevelField(newx, newy);
        }
 
-       PlaySoundLevel(x, y, SND_PIG_DIGGING);
+       PlayLevelSound(x, y, SND_PIG_DIGGING);
       }
       else if (!IS_FREE(newx, newy))
       {
@@ -4400,7 +4409,7 @@ void StartMoving(int x, int y)
          else
            DrawLevelField(x, y);
 
-         PlaySoundLevel(x, y, SND_DRAGON_ATTACKING);
+         PlayLevelSound(x, y, SND_DRAGON_ATTACKING);
 
          MovDelay[x][y] = 50;
 
@@ -4425,7 +4434,7 @@ void StartMoving(int x, int y)
        DrawLevelField(newx, newy);
       }
 
-      PlaySoundLevel(x, y, SND_YAMYAM_DIGGING);
+      PlayLevelSound(x, y, SND_YAMYAM_DIGGING);
     }
     else if (element == EL_DARK_YAMYAM && IN_LEV_FIELD(newx, newy) &&
             IS_FOOD_DARK_YAMYAM(Feld[newx][newy]))
@@ -4446,7 +4455,7 @@ void StartMoving(int x, int y)
        DrawLevelField(newx, newy);
       }
 
-      PlaySoundLevel(x, y, SND_DARK_YAMYAM_DIGGING);
+      PlayLevelSound(x, y, SND_DARK_YAMYAM_DIGGING);
     }
     else if ((element == EL_PACMAN || element == EL_MOLE)
             && IN_LEV_FIELD(newx, newy) && IS_AMOEBOID(Feld[newx][newy]))
@@ -4462,7 +4471,7 @@ void StartMoving(int x, int y)
       if (element == EL_MOLE)
       {
        Feld[newx][newy] = EL_AMOEBA_SHRINKING;
-       PlaySoundLevel(x, y, SND_MOLE_DIGGING);
+       PlayLevelSound(x, y, SND_MOLE_DIGGING);
 
        ResetGfxAnimation(x, y);
        GfxAction[x][y] = ACTION_DIGGING;
@@ -4475,7 +4484,7 @@ void StartMoving(int x, int y)
       {
        Feld[newx][newy] = EL_EMPTY;
        DrawLevelField(newx, newy);
-       PlaySoundLevel(x, y, SND_PACMAN_DIGGING);
+       PlayLevelSound(x, y, SND_PACMAN_DIGGING);
       }
     }
     else if (element == EL_MOLE && IN_LEV_FIELD(newx, newy) &&
@@ -4514,7 +4523,7 @@ void StartMoving(int x, int y)
        TestIfBadThingTouchesHero(x, y);
 
 #if 0
-      PlaySoundLevelAction(x, y, ACTION_WAITING);
+      PlayLevelSoundAction(x, y, ACTION_WAITING);
 #endif
 
       return;
@@ -4522,7 +4531,7 @@ void StartMoving(int x, int y)
 
     InitMovingField(x, y, MovDir[x][y]);
 
-    PlaySoundLevelAction(x, y, ACTION_MOVING);
+    PlayLevelSoundAction(x, y, ACTION_MOVING);
   }
 
   if (MovDir[x][y])
@@ -4827,7 +4836,7 @@ void AmoebeUmwandeln(int ax, int ay)
        }
       }
     }
-    PlaySoundLevel(ax, ay, (IS_GEM(level.amoeba_content) ?
+    PlayLevelSound(ax, ay, (IS_GEM(level.amoeba_content) ?
                            SND_AMOEBA_TURNING_TO_GEM :
                            SND_AMOEBA_TURNING_TO_ROCK));
     Bang(ax, ay);
@@ -4852,7 +4861,7 @@ void AmoebeUmwandeln(int ax, int ay)
 
       if (Feld[x][y] == EL_AMOEBA_TO_DIAMOND)
       {
-       PlaySoundLevel(x, y, (IS_GEM(level.amoeba_content) ?
+       PlayLevelSound(x, y, (IS_GEM(level.amoeba_content) ?
                              SND_AMOEBA_TURNING_TO_GEM :
                              SND_AMOEBA_TURNING_TO_ROCK));
        Bang(x, y);
@@ -4895,7 +4904,7 @@ void AmoebeUmwandelnBD(int ax, int ay, int new_element)
   }
 
   if (done)
-    PlaySoundLevel(ax, ay, (new_element == EL_BD_ROCK ?
+    PlayLevelSound(ax, ay, (new_element == EL_BD_ROCK ?
                            SND_BD_AMOEBA_TURNING_TO_ROCK :
                            SND_BD_AMOEBA_TURNING_TO_GEM));
 }
@@ -4912,12 +4921,12 @@ void AmoebeWaechst(int x, int y)
     if (DelayReached(&sound_delay, sound_delay_value))
     {
 #if 1
-      PlaySoundLevelElementAction(x, y, Store[x][y], ACTION_GROWING);
+      PlayLevelSoundElementAction(x, y, Store[x][y], ACTION_GROWING);
 #else
       if (Store[x][y] == EL_BD_AMOEBA)
-       PlaySoundLevel(x, y, SND_BD_AMOEBA_GROWING);
+       PlayLevelSound(x, y, SND_BD_AMOEBA_GROWING);
       else
-       PlaySoundLevel(x, y, SND_AMOEBA_GROWING);
+       PlayLevelSound(x, y, SND_AMOEBA_GROWING);
 #endif
       sound_delay_value = 30;
     }
@@ -5117,9 +5126,9 @@ void AmoebeAbleger(int ax, int ay)
   {
     Feld[newax][neway] = EL_AMOEBA_DROP;       /* drop left/right of amoeba */
 #if 1
-    PlaySoundLevelAction(newax, neway, ACTION_GROWING);
+    PlayLevelSoundAction(newax, neway, ACTION_GROWING);
 #else
-    PlaySoundLevel(newax, neway, SND_AMOEBA_GROWING);
+    PlayLevelSound(newax, neway, SND_AMOEBA_GROWING);
 #endif
   }
   else
@@ -5208,7 +5217,7 @@ void Life(int ax, int ay)
   }
 
   if (changed)
-    PlaySoundLevel(ax, ay, element == EL_BIOMAZE ? SND_BIOMAZE_GROWING :
+    PlayLevelSound(ax, ay, element == EL_BIOMAZE ? SND_BIOMAZE_GROWING :
                   SND_GAME_OF_LIFE_GROWING);
 }
 
@@ -5219,7 +5228,7 @@ static void InitRobotWheel(int x, int y)
 
 static void RunRobotWheel(int x, int y)
 {
-  PlaySoundLevel(x, y, SND_ROBOT_WHEEL_ACTIVE);
+  PlayLevelSound(x, y, SND_ROBOT_WHEEL_ACTIVE);
 }
 
 static void StopRobotWheel(int x, int y)
@@ -5235,7 +5244,7 @@ static void InitTimegateWheel(int x, int y)
 
 static void RunTimegateWheel(int x, int y)
 {
-  PlaySoundLevel(x, y, SND_TIMEGATE_SWITCH_ACTIVE);
+  PlayLevelSound(x, y, SND_TIMEGATE_SWITCH_ACTIVE);
 }
 
 void CheckExit(int x, int y)
@@ -5258,7 +5267,7 @@ void CheckExit(int x, int y)
 
   Feld[x][y] = EL_EXIT_OPENING;
 
-  PlaySoundLevelNearest(x, y, SND_CLASS_EXIT_OPENING);
+  PlayLevelSoundNearest(x, y, SND_CLASS_EXIT_OPENING);
 }
 
 void CheckExitSP(int x, int y)
@@ -5279,7 +5288,7 @@ void CheckExitSP(int x, int y)
 
   Feld[x][y] = EL_SP_EXIT_OPENING;
 
-  PlaySoundLevelNearest(x, y, SND_CLASS_SP_EXIT_OPENING);
+  PlayLevelSoundNearest(x, y, SND_CLASS_SP_EXIT_OPENING);
 }
 
 static void CloseAllOpenTimegates()
@@ -5296,9 +5305,9 @@ static void CloseAllOpenTimegates()
       {
        Feld[x][y] = EL_TIMEGATE_CLOSING;
 #if 1
-       PlaySoundLevelAction(x, y, ACTION_CLOSING);
+       PlayLevelSoundAction(x, y, ACTION_CLOSING);
 #else
-       PlaySoundLevel(x, y, SND_TIMEGATE_CLOSING);
+       PlayLevelSound(x, y, SND_TIMEGATE_CLOSING);
 #endif
       }
     }
@@ -5502,9 +5511,9 @@ void MauerAbleger(int ax, int ay)
 
   if (new_wall)
 #if 1
-    PlaySoundLevelAction(ax, ay, ACTION_GROWING);
+    PlayLevelSoundAction(ax, ay, ACTION_GROWING);
 #else
-    PlaySoundLevel(ax, ay, SND_EXPANDABLE_WALL_GROWING);
+    PlayLevelSound(ax, ay, SND_EXPANDABLE_WALL_GROWING);
 #endif
 }
 
@@ -5588,7 +5597,7 @@ static void WarnBuggyBase(int x, int y)
 
     if (IS_PLAYER(xx, yy))
     {
-      PlaySoundLevel(x, y, SND_SP_BUGGY_BASE_ACTIVE);
+      PlayLevelSound(x, y, SND_SP_BUGGY_BASE_ACTIVE);
 
       break;
     }
@@ -5602,7 +5611,7 @@ static void InitTrap(int x, int y)
 
 static void ActivateTrap(int x, int y)
 {
-  PlaySoundLevel(x, y, SND_TRAP_ACTIVATING);
+  PlayLevelSound(x, y, SND_TRAP_ACTIVATING);
 }
 
 static void ChangeActiveTrap(int x, int y)
@@ -5759,14 +5768,14 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
       }
 
       if (something_has_changed)
-       PlaySoundLevelElementAction(x, y, element, ACTION_CHANGING);
+       PlayLevelSoundElementAction(x, y, element, ACTION_CHANGING);
     }
   }
   else
   {
     ChangeElementNowExt(x, y, change->target_element);
 
-    PlaySoundLevelElementAction(x, y, element, ACTION_CHANGING);
+    PlayLevelSoundElementAction(x, y, element, ACTION_CHANGING);
   }
 
   return TRUE;
@@ -5942,6 +5951,58 @@ static boolean CheckElementChange(int x, int y, int element, int trigger_event)
   return CheckElementSideChange(x, y, element, CH_SIDE_ANY, trigger_event, -1);
 }
 
+static void SetPlayerWaiting(struct PlayerInfo *player, boolean is_waiting)
+{
+  boolean was_waiting = player->is_waiting;
+
+  if (is_waiting)
+  {
+    int jx = player->jx, jy = player->jy;
+    int element = player->element_nr;
+    int action;
+
+    if (!was_waiting)          /* not waiting -> waiting */
+    {
+      player->is_waiting = TRUE;
+      player->is_bored = FALSE;
+      player->is_sleeping = FALSE;
+
+      player->frame_counter_bored =
+       (FrameCounter +
+        game.player_boring_delay_fixed +
+        SimpleRND(game.player_boring_delay_random));
+      player->frame_counter_sleeping =
+       (FrameCounter +
+        game.player_sleeping_delay_fixed +
+        SimpleRND(game.player_sleeping_delay_random));
+
+      InitPlayerGfxAnimation(player, ACTION_WAITING, player->MovDir);
+    }
+
+    if (FrameCounter >= player->frame_counter_bored)
+      player->is_bored = TRUE;
+    if (FrameCounter >= player->frame_counter_sleeping)
+      player->is_sleeping = TRUE;
+
+    action = (player->is_sleeping ? ACTION_SLEEPING :
+             player->is_bored ? ACTION_BORING : ACTION_WAITING);
+
+    if (!was_waiting)
+      PlayLevelSoundElementAction(jx, jy, element, action);
+    else
+      PlayLevelSoundElementActionIfLoop(jx, jy, element, action);
+  }
+  else if (was_waiting)                /* waiting -> not waiting */
+  {
+    player->is_waiting = FALSE;
+    player->is_bored = FALSE;
+    player->is_sleeping = FALSE;
+
+    player->frame_counter_bored = -1;
+    player->frame_counter_sleeping = -1;
+  }
+}
+
 #if 1
 static byte PlayerActions(struct PlayerInfo *player, byte player_action)
 {
@@ -5996,6 +6057,8 @@ static byte PlayerActions(struct PlayerInfo *player, byte player_action)
       }
     }
 
+    SetPlayerWaiting(player, FALSE);
+
 #if 1
     return player_action;
 #else
@@ -6015,7 +6078,7 @@ static byte PlayerActions(struct PlayerInfo *player, byte player_action)
     CheckGravityMovement(player);
 
     if (player->MovPos == 0)
-      InitPlayerGfxAnimation(player, ACTION_DEFAULT, player->MovDir);
+      SetPlayerWaiting(player, TRUE);
 
     if (player->MovPos == 0)   /* needed for tape.playing */
       player->is_moving = FALSE;
@@ -6199,6 +6262,9 @@ void GameActions()
 
     tape_action[i] = PlayerActions(&stored_player[i], actual_player_action);
 
+    if (tape.recording && tape_action[i] && !tape.player_participates[i])
+      tape.player_participates[i] = TRUE;    /* player just appeared from CE */
+
     ScrollPlayer(&stored_player[i], SCROLL_GO_ON);
   }
 
@@ -6321,7 +6387,7 @@ void GameActions()
     SetRandomAnimationValue(x, y);
 
 #if 1
-    PlaySoundLevelActionIfLoop(x, y, GfxAction[x][y]);
+    PlayLevelSoundActionIfLoop(x, y, GfxAction[x][y]);
 #endif
 
     if (IS_INACTIVE(element))
@@ -6451,7 +6517,7 @@ void GameActions()
 #endif
 
     if (IS_BELT_ACTIVE(element))
-      PlaySoundLevelAction(x, y, ACTION_ACTIVE);
+      PlayLevelSoundAction(x, y, ACTION_ACTIVE);
 
     if (game.magic_wall_active)
     {
@@ -6541,9 +6607,9 @@ void GameActions()
       if (element == EL_BD_MAGIC_WALL_FULL ||
          element == EL_BD_MAGIC_WALL_ACTIVE ||
          element == EL_BD_MAGIC_WALL_EMPTYING)
-       PlaySoundLevel(magic_wall_x, magic_wall_y, SND_BD_MAGIC_WALL_ACTIVE);
+       PlayLevelSound(magic_wall_x, magic_wall_y, SND_BD_MAGIC_WALL_ACTIVE);
       else
-       PlaySoundLevel(magic_wall_x, magic_wall_y, SND_MAGIC_WALL_ACTIVE);
+       PlayLevelSound(magic_wall_x, magic_wall_y, SND_MAGIC_WALL_ACTIVE);
     }
 
     if (game.magic_wall_time_left > 0)
@@ -6597,9 +6663,9 @@ void GameActions()
     if (SHIELD_ON(player))
     {
       if (player->shield_deadly_time_left)
-       PlaySoundLevel(player->jx, player->jy, SND_SHIELD_DEADLY_ACTIVE);
+       PlayLevelSound(player->jx, player->jy, SND_SHIELD_DEADLY_ACTIVE);
       else if (player->shield_normal_time_left)
-       PlaySoundLevel(player->jx, player->jy, SND_SHIELD_NORMAL_ACTIVE);
+       PlayLevelSound(player->jx, player->jy, SND_SHIELD_NORMAL_ACTIVE);
     }
   }
 
@@ -7664,11 +7730,11 @@ void BuryHero(struct PlayerInfo *player)
     return;
 
 #if 1
-  PlaySoundLevelElementAction(jx, jy, player->element_nr, ACTION_DYING);
+  PlayLevelSoundElementAction(jx, jy, player->element_nr, ACTION_DYING);
 #else
-  PlaySoundLevel(jx, jy, SND_CLASS_PLAYER_DYING);
+  PlayLevelSound(jx, jy, SND_CLASS_PLAYER_DYING);
 #endif
-  PlaySoundLevel(jx, jy, SND_GAME_LOSING);
+  PlayLevelSound(jx, jy, SND_GAME_LOSING);
 
   player->GameOver = TRUE;
   RemoveHero(player);
@@ -7863,7 +7929,7 @@ int DigField(struct PlayerInfo *player,
       player->programmed_action = move_direction;
       DOUBLE_PLAYER_SPEED(player);
 
-      PlaySoundLevel(x, y, SND_CLASS_SP_PORT_PASSING);
+      PlayLevelSound(x, y, SND_CLASS_SP_PORT_PASSING);
       break;
 
     case EL_TUBE_ANY:
@@ -7905,7 +7971,7 @@ int DigField(struct PlayerInfo *player,
        if (!(tube_enter_directions[i][1] & move_direction))
          return MF_NO_ACTION;  /* tube has no opening in this direction */
 
-       PlaySoundLevel(x, y, SND_CLASS_TUBE_WALKING);
+       PlayLevelSound(x, y, SND_CLASS_TUBE_WALKING);
       }
       break;
 
@@ -7938,9 +8004,9 @@ int DigField(struct PlayerInfo *player,
 
        /* play sound from background or player, whatever is available */
        if (element_info[element].sound[sound_action] != SND_UNDEFINED)
-         PlaySoundLevelElementAction(x, y, element, sound_action);
+         PlayLevelSoundElementAction(x, y, element, sound_action);
        else
-         PlaySoundLevelElementAction(x, y, player->element_nr, sound_action);
+         PlayLevelSoundElementAction(x, y, player->element_nr, sound_action);
 
        break;
       }
@@ -7969,7 +8035,7 @@ int DigField(struct PlayerInfo *player,
        player->programmed_action = move_direction;
        DOUBLE_PLAYER_SPEED(player);
 
-       PlaySoundLevelAction(x, y, ACTION_PASSING);
+       PlayLevelSoundAction(x, y, ACTION_PASSING);
 
        break;
       }
@@ -7988,7 +8054,7 @@ int DigField(struct PlayerInfo *player,
          player->is_digging = TRUE;
        }
 
-       PlaySoundLevelElementAction(x, y, element, ACTION_DIGGING);
+       PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
 
        CheckTriggeredElementChange(x, y, element, CE_OTHER_GETS_DIGGED);
 
@@ -8086,7 +8152,7 @@ int DigField(struct PlayerInfo *player,
        }
 
        RaiseScoreElement(element);
-       PlaySoundLevelElementAction(x, y, element, ACTION_COLLECTING);
+       PlayLevelSoundElementAction(x, y, element, ACTION_COLLECTING);
 
        CheckTriggeredElementChange(x, y, element, CE_OTHER_GETS_COLLECTED);
 
@@ -8210,23 +8276,23 @@ int DigField(struct PlayerInfo *player,
          Feld[x][y] = EL_SOKOBAN_OBJECT;
 
          if (Back[x][y] == Back[nextx][nexty])
-           PlaySoundLevelAction(x, y, ACTION_PUSHING);
+           PlayLevelSoundAction(x, y, ACTION_PUSHING);
          else if (Back[x][y] != 0)
-           PlaySoundLevelElementAction(x, y, EL_SOKOBAN_FIELD_FULL,
+           PlayLevelSoundElementAction(x, y, EL_SOKOBAN_FIELD_FULL,
                                        ACTION_EMPTYING);
          else
-           PlaySoundLevelElementAction(nextx, nexty, EL_SOKOBAN_FIELD_EMPTY,
+           PlayLevelSoundElementAction(nextx, nexty, EL_SOKOBAN_FIELD_EMPTY,
                                        ACTION_FILLING);
 
          if (local_player->sokobanfields_still_needed == 0 &&
              game.emulation == EMU_SOKOBAN)
          {
            player->LevelSolved = player->GameOver = TRUE;
-           PlaySoundLevel(x, y, SND_GAME_SOKOBAN_SOLVING);
+           PlayLevelSound(x, y, SND_GAME_SOKOBAN_SOLVING);
          }
        }
        else
-         PlaySoundLevelElementAction(x, y, element, ACTION_PUSHING);
+         PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
 
        InitMovingField(x, y, move_direction);
        GfxAction[x][y] = ACTION_PUSHING;
@@ -8260,7 +8326,7 @@ int DigField(struct PlayerInfo *player,
        player->switch_x = x;
        player->switch_y = y;
 
-       PlaySoundLevelElementAction(x, y, element, ACTION_ACTIVATING);
+       PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVATING);
 
        if (element == EL_ROBOT_WHEEL)
        {
@@ -8297,7 +8363,7 @@ int DigField(struct PlayerInfo *player,
          ToggleLightSwitch(x, y);
 
 #if 0
-         PlaySoundLevel(x, y, element == EL_LIGHT_SWITCH ?
+         PlayLevelSound(x, y, element == EL_LIGHT_SWITCH ?
                         SND_LIGHT_SWITCH_ACTIVATING :
                         SND_LIGHT_SWITCH_DEACTIVATING);
 #endif
@@ -8478,7 +8544,7 @@ boolean DropElement(struct PlayerInfo *player)
     if (IN_SCR_FIELD(SCREENX(jx), SCREENY(jy)))
       DrawGraphicThruMask(SCREENX(jx), SCREENY(jy), el2img(Feld[jx][jy]), 0);
 
-    PlaySoundLevelAction(jx, jy, ACTION_DROPPING);
+    PlayLevelSoundAction(jx, jy, ACTION_DROPPING);
 
     CheckTriggeredElementChange(jx, jy, new_element, CE_OTHER_GETS_DROPPED);
     CheckElementChange(jx, jy, new_element, CE_DROPPED_BY_PLAYER);
@@ -8495,7 +8561,7 @@ boolean DropElement(struct PlayerInfo *player)
     if (IN_SCR_FIELD(SCREENX(jx), SCREENY(jy)))
       DrawGraphicThruMask(SCREENX(jx), SCREENY(jy), el2img(Feld[jx][jy]), 0);
 
-    PlaySoundLevelAction(jx, jy, ACTION_DROPPING);
+    PlayLevelSoundAction(jx, jy, ACTION_DROPPING);
   }
 
   return TRUE;
@@ -8508,7 +8574,7 @@ boolean DropElement(struct PlayerInfo *player)
 static int *loop_sound_frame = NULL;
 static int *loop_sound_volume = NULL;
 
-void InitPlaySoundLevel()
+void InitPlayLevelSound()
 {
   int num_sounds = getSoundListSize();
 
@@ -8522,7 +8588,7 @@ void InitPlaySoundLevel()
   loop_sound_volume = checked_calloc(num_sounds * sizeof(int));
 }
 
-static void PlaySoundLevel(int x, int y, int nr)
+static void PlayLevelSound(int x, int y, int nr)
 {
   int sx = SCREENX(x), sy = SCREENY(y);
   int volume, stereo_position;
@@ -8567,37 +8633,46 @@ static void PlaySoundLevel(int x, int y, int nr)
   PlaySoundExt(nr, volume, stereo_position, type);
 }
 
-static void PlaySoundLevelNearest(int x, int y, int sound_action)
+static void PlayLevelSoundNearest(int x, int y, int sound_action)
 {
-  PlaySoundLevel(x < LEVELX(BX1) ? LEVELX(BX1) :
+  PlayLevelSound(x < LEVELX(BX1) ? LEVELX(BX1) :
                 x > LEVELX(BX2) ? LEVELX(BX2) : x,
                 y < LEVELY(BY1) ? LEVELY(BY1) :
                 y > LEVELY(BY2) ? LEVELY(BY2) : y,
                 sound_action);
 }
 
-static void PlaySoundLevelAction(int x, int y, int action)
+static void PlayLevelSoundAction(int x, int y, int action)
 {
-  PlaySoundLevelElementAction(x, y, Feld[x][y], action);
+  PlayLevelSoundElementAction(x, y, Feld[x][y], action);
 }
 
-static void PlaySoundLevelElementAction(int x, int y, int element, int action)
+static void PlayLevelSoundElementAction(int x, int y, int element, int action)
 {
   int sound_effect = element_info[element].sound[action];
 
   if (sound_effect != SND_UNDEFINED)
-    PlaySoundLevel(x, y, sound_effect);
+    PlayLevelSound(x, y, sound_effect);
+}
+
+static void PlayLevelSoundElementActionIfLoop(int x, int y, int element,
+                                             int action)
+{
+  int sound_effect = element_info[element].sound[action];
+
+  if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
+    PlayLevelSound(x, y, sound_effect);
 }
 
-static void PlaySoundLevelActionIfLoop(int x, int y, int action)
+static void PlayLevelSoundActionIfLoop(int x, int y, int action)
 {
   int sound_effect = element_info[Feld[x][y]].sound[action];
 
   if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
-    PlaySoundLevel(x, y, sound_effect);
+    PlayLevelSound(x, y, sound_effect);
 }
 
-static void StopSoundLevelActionIfLoop(int x, int y, int action)
+static void StopLevelSoundActionIfLoop(int x, int y, int action)
 {
   int sound_effect = element_info[Feld[x][y]].sound[action];
 
@@ -8605,6 +8680,18 @@ static void StopSoundLevelActionIfLoop(int x, int y, int action)
     StopSoundExt(sound_effect, SND_CTRL_STOP_SOUND);
 }
 
+static void PlayLevelMusic()
+{
+#if 1
+  if (levelset.music[level_nr] != MUS_UNDEFINED)
+    PlayMusic(levelset.music[level_nr]);       /* from config file */
+  else
+    PlayMusic(-(level_nr + 1));                        /* from music dir */
+#else
+  PlayMusic(level_nr);
+#endif
+}
+
 void RaiseScore(int value)
 {
   local_player->score += value;
@@ -8905,7 +8992,8 @@ static void HandleGameButtons(struct GadgetInfo *gi)
        setup.sound = setup.sound_music = TRUE;
 
        SetAudioMode(setup.sound);
-       PlayMusic(level_nr);
+
+       PlayLevelMusic();
       }
       break;