+ /* if digged element was about to explode, prevent the explosion */
+ ExplodeField[x][y] = EX_TYPE_NONE;
+
+ PlayLevelSoundAction(x, y, action);
+ }
+
+ Store[x][y] = EL_EMPTY;
+
+ /* this makes it possible to leave the removed element again */
+ if (IS_EQUAL_OR_IN_GROUP(element, MOVE_ENTER_EL(digging_element)))
+ Store[x][y] = element;
+
+ return TRUE;
+}
+
+static boolean SnapField(struct PlayerInfo *player, int dx, int dy)
+{
+ int jx = player->jx, jy = player->jy;
+ int x = jx + dx, y = jy + dy;
+ int snap_direction = (dx == -1 ? MV_LEFT :
+ dx == +1 ? MV_RIGHT :
+ dy == -1 ? MV_UP :
+ dy == +1 ? MV_DOWN : MV_NONE);
+ boolean can_continue_snapping = (level.continuous_snapping &&
+ WasJustFalling[x][y] < CHECK_DELAY_FALLING);
+
+ if (player->MovPos != 0 && game.engine_version >= VERSION_IDENT(2,2,0,0))
+ return FALSE;
+
+ if (!player->active || !IN_LEV_FIELD(x, y))
+ return FALSE;
+
+ if (dx && dy)
+ return FALSE;
+
+ if (!dx && !dy)
+ {
+ if (player->MovPos == 0)
+ player->is_pushing = FALSE;
+
+ player->is_snapping = FALSE;
+
+ if (player->MovPos == 0)
+ {
+ player->is_moving = FALSE;
+ player->is_digging = FALSE;
+ player->is_collecting = FALSE;
+ }
+
+ return FALSE;
+ }
+
+ /* prevent snapping with already pressed snap key when not allowed */
+ if (player->is_snapping && !can_continue_snapping)
+ return FALSE;
+
+ player->MovDir = snap_direction;
+
+ if (player->MovPos == 0)
+ {
+ player->is_moving = FALSE;
+ player->is_digging = FALSE;
+ player->is_collecting = FALSE;
+ }
+
+ player->is_dropping = FALSE;
+ player->is_dropping_pressed = FALSE;
+ player->drop_pressed_delay = 0;
+
+ if (DigField(player, jx, jy, x, y, 0, 0, DF_SNAP) == MP_NO_ACTION)
+ return FALSE;
+
+ player->is_snapping = TRUE;
+ player->is_active = TRUE;
+
+ if (player->MovPos == 0)
+ {
+ player->is_moving = FALSE;
+ player->is_digging = FALSE;
+ player->is_collecting = FALSE;
+ }
+
+ if (player->MovPos != 0) /* prevent graphic bugs in versions < 2.2.0 */
+ TEST_DrawLevelField(player->last_jx, player->last_jy);
+
+ TEST_DrawLevelField(x, y);
+
+ return TRUE;
+}
+
+static boolean DropElement(struct PlayerInfo *player)
+{
+ int old_element, new_element;
+ int dropx = player->jx, dropy = player->jy;
+ int drop_direction = player->MovDir;
+ int drop_side = drop_direction;
+ int drop_element = get_next_dropped_element(player);
+
+ /* do not drop an element on top of another element; when holding drop key
+ pressed without moving, dropped element must move away before the next
+ element can be dropped (this is especially important if the next element
+ is dynamite, which can be placed on background for historical reasons) */
+ if (PLAYER_DROPPING(player, dropx, dropy) && Feld[dropx][dropy] != EL_EMPTY)
+ return MP_ACTION;
+
+ if (IS_THROWABLE(drop_element))
+ {
+ dropx += GET_DX_FROM_DIR(drop_direction);
+ dropy += GET_DY_FROM_DIR(drop_direction);
+
+ if (!IN_LEV_FIELD(dropx, dropy))
+ return FALSE;
+ }
+
+ old_element = Feld[dropx][dropy]; /* old element at dropping position */
+ new_element = drop_element; /* default: no change when dropping */
+
+ /* check if player is active, not moving and ready to drop */
+ if (!player->active || player->MovPos || player->drop_delay > 0)
+ return FALSE;
+
+ /* check if player has anything that can be dropped */
+ if (new_element == EL_UNDEFINED)
+ return FALSE;
+
+ /* only set if player has anything that can be dropped */
+ player->is_dropping_pressed = TRUE;
+
+ /* check if drop key was pressed long enough for EM style dynamite */
+ if (new_element == EL_EM_DYNAMITE && player->drop_pressed_delay < 40)
+ return FALSE;
+
+ /* check if anything can be dropped at the current position */
+ if (IS_ACTIVE_BOMB(old_element) || old_element == EL_EXPLOSION)
+ return FALSE;
+
+ /* collected custom elements can only be dropped on empty fields */
+ if (IS_CUSTOM_ELEMENT(new_element) && old_element != EL_EMPTY)
+ return FALSE;
+
+ if (old_element != EL_EMPTY)
+ Back[dropx][dropy] = old_element; /* store old element on this field */
+
+ ResetGfxAnimation(dropx, dropy);
+ ResetRandomAnimationValue(dropx, dropy);
+
+ if (player->inventory_size > 0 ||
+ player->inventory_infinite_element != EL_UNDEFINED)
+ {
+ if (player->inventory_size > 0)
+ {
+ player->inventory_size--;
+
+ DrawGameDoorValues();
+
+ if (new_element == EL_DYNAMITE)
+ new_element = EL_DYNAMITE_ACTIVE;
+ else if (new_element == EL_EM_DYNAMITE)
+ new_element = EL_EM_DYNAMITE_ACTIVE;
+ else if (new_element == EL_SP_DISK_RED)
+ new_element = EL_SP_DISK_RED_ACTIVE;
+ }
+
+ Feld[dropx][dropy] = new_element;
+
+ if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
+ DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
+ el2img(Feld[dropx][dropy]), 0);
+
+ PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
+
+ /* needed if previous element just changed to "empty" in the last frame */
+ ChangeCount[dropx][dropy] = 0; /* allow at least one more change */
+
+ CheckElementChangeByPlayer(dropx, dropy, new_element, CE_DROPPED_BY_PLAYER,
+ player->index_bit, drop_side);
+ CheckTriggeredElementChangeByPlayer(dropx, dropy, new_element,
+ CE_PLAYER_DROPS_X,
+ player->index_bit, drop_side);
+
+ TestIfElementTouchesCustomElement(dropx, dropy);
+ }
+ else /* player is dropping a dyna bomb */
+ {
+ player->dynabombs_left--;
+
+ Feld[dropx][dropy] = new_element;
+
+ if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
+ DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
+ el2img(Feld[dropx][dropy]), 0);
+
+ PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
+ }
+
+ if (Feld[dropx][dropy] == new_element) /* uninitialized unless CE change */
+ InitField_WithBug1(dropx, dropy, FALSE);
+
+ new_element = Feld[dropx][dropy]; /* element might have changed */
+
+ if (IS_CUSTOM_ELEMENT(new_element) && CAN_MOVE(new_element) &&
+ element_info[new_element].move_pattern == MV_WHEN_DROPPED)
+ {
+ if (element_info[new_element].move_direction_initial == MV_START_AUTOMATIC)
+ MovDir[dropx][dropy] = drop_direction;
+
+ ChangeCount[dropx][dropy] = 0; /* allow at least one more change */
+
+ /* do not cause impact style collision by dropping elements that can fall */
+ CheckCollision[dropx][dropy] = CHECK_DELAY_COLLISION;
+ }
+
+ player->drop_delay = GET_NEW_DROP_DELAY(drop_element);
+ player->is_dropping = TRUE;
+
+ player->drop_pressed_delay = 0;
+ player->is_dropping_pressed = FALSE;
+
+ player->drop_x = dropx;
+ player->drop_y = dropy;
+
+ return TRUE;
+}
+
+/* ------------------------------------------------------------------------- */
+/* game sound playing functions */
+/* ------------------------------------------------------------------------- */
+
+static int *loop_sound_frame = NULL;
+static int *loop_sound_volume = NULL;
+
+void InitPlayLevelSound()
+{
+ int num_sounds = getSoundListSize();
+
+ checked_free(loop_sound_frame);
+ checked_free(loop_sound_volume);
+
+ loop_sound_frame = checked_calloc(num_sounds * sizeof(int));
+ loop_sound_volume = checked_calloc(num_sounds * sizeof(int));
+}
+
+static void PlayLevelSound(int x, int y, int nr)
+{
+ int sx = SCREENX(x), sy = SCREENY(y);
+ int volume, stereo_position;
+ int max_distance = 8;
+ int type = (IS_LOOP_SOUND(nr) ? SND_CTRL_PLAY_LOOP : SND_CTRL_PLAY_SOUND);
+
+ if ((!setup.sound_simple && !IS_LOOP_SOUND(nr)) ||
+ (!setup.sound_loops && IS_LOOP_SOUND(nr)))
+ return;
+
+ if (!IN_LEV_FIELD(x, y) ||
+ sx < -max_distance || sx >= SCR_FIELDX + max_distance ||
+ sy < -max_distance || sy >= SCR_FIELDY + max_distance)
+ return;
+
+ volume = SOUND_MAX_VOLUME;
+
+ if (!IN_SCR_FIELD(sx, sy))
+ {
+ int dx = ABS(sx - SCR_FIELDX / 2) - SCR_FIELDX / 2;
+ int dy = ABS(sy - SCR_FIELDY / 2) - SCR_FIELDY / 2;
+
+ volume -= volume * (dx > dy ? dx : dy) / max_distance;
+ }
+
+ stereo_position = (SOUND_MAX_LEFT +
+ (sx + max_distance) * SOUND_MAX_LEFT2RIGHT /
+ (SCR_FIELDX + 2 * max_distance));
+
+ if (IS_LOOP_SOUND(nr))
+ {
+ /* This assures that quieter loop sounds do not overwrite louder ones,
+ while restarting sound volume comparison with each new game frame. */
+
+ if (loop_sound_volume[nr] > volume && loop_sound_frame[nr] == FrameCounter)
+ return;
+
+ loop_sound_volume[nr] = volume;
+ loop_sound_frame[nr] = FrameCounter;
+ }
+
+ PlaySoundExt(nr, volume, stereo_position, type);
+}
+
+static void PlayLevelSoundNearest(int x, int y, int sound_action)
+{
+ 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 PlayLevelSoundAction(int x, int y, int action)
+{
+ PlayLevelSoundElementAction(x, y, Feld[x][y], action);
+}
+
+static void PlayLevelSoundElementAction(int x, int y, int element, int action)
+{
+ int sound_effect = element_info[SND_ELEMENT(element)].sound[action];
+
+ if (sound_effect != SND_UNDEFINED)
+ PlayLevelSound(x, y, sound_effect);
+}
+
+static void PlayLevelSoundElementActionIfLoop(int x, int y, int element,
+ int action)
+{
+ int sound_effect = element_info[SND_ELEMENT(element)].sound[action];
+
+ if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
+ PlayLevelSound(x, y, sound_effect);
+}
+
+static void PlayLevelSoundActionIfLoop(int x, int y, int action)
+{
+ int sound_effect = element_info[SND_ELEMENT(Feld[x][y])].sound[action];
+
+ if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
+ PlayLevelSound(x, y, sound_effect);
+}
+
+static void StopLevelSoundActionIfLoop(int x, int y, int action)
+{
+ int sound_effect = element_info[SND_ELEMENT(Feld[x][y])].sound[action];
+
+ if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
+ StopSound(sound_effect);
+}
+
+static int getLevelMusicNr()
+{
+ if (levelset.music[level_nr] != MUS_UNDEFINED)
+ return levelset.music[level_nr]; /* from config file */
+ else
+ return MAP_NOCONF_MUSIC(level_nr); /* from music dir */
+}
+
+static void FadeLevelSounds()
+{
+ FadeSounds();
+}
+
+static void FadeLevelMusic()
+{
+ int music_nr = getLevelMusicNr();
+ char *curr_music = getCurrentlyPlayingMusicFilename();
+ char *next_music = getMusicInfoEntryFilename(music_nr);
+
+ if (!strEqual(curr_music, next_music))
+ FadeMusic();
+}
+
+void FadeLevelSoundsAndMusic()
+{
+ FadeLevelSounds();
+ FadeLevelMusic();
+}
+
+static void PlayLevelMusic()
+{
+ int music_nr = getLevelMusicNr();
+ char *curr_music = getCurrentlyPlayingMusicFilename();
+ char *next_music = getMusicInfoEntryFilename(music_nr);
+
+ if (!strEqual(curr_music, next_music))
+ PlayMusic(music_nr);
+}
+
+void PlayLevelSound_EM(int xx, int yy, int element_em, int sample)
+{
+ int element = (element_em > -1 ? map_element_EM_to_RND(element_em) : 0);
+ int offset = (BorderElement == EL_STEELWALL ? 1 : 0);
+ int x = xx - 1 - offset;
+ int y = yy - 1 - offset;
+
+ switch (sample)
+ {
+ case SAMPLE_blank:
+ PlayLevelSoundElementAction(x, y, element, ACTION_WALKING);
+ break;
+
+ case SAMPLE_roll:
+ PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
+ break;
+
+ case SAMPLE_stone:
+ PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
+ break;
+
+ case SAMPLE_nut:
+ PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
+ break;
+
+ case SAMPLE_crack:
+ PlayLevelSoundElementAction(x, y, element, ACTION_BREAKING);
+ break;
+
+ case SAMPLE_bug:
+ PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
+ break;
+
+ case SAMPLE_tank:
+ PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
+ break;
+
+ case SAMPLE_android_clone:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
+ break;
+
+ case SAMPLE_android_move:
+ PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
+ break;
+
+ case SAMPLE_spring:
+ PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
+ break;
+
+ case SAMPLE_slurp:
+ PlayLevelSoundElementAction(x, y, element, ACTION_EATING);
+ break;
+
+ case SAMPLE_eater:
+ PlayLevelSoundElementAction(x, y, element, ACTION_WAITING);
+ break;
+
+ case SAMPLE_eater_eat:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
+ break;
+
+ case SAMPLE_alien:
+ PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
+ break;
+
+ case SAMPLE_collect:
+ PlayLevelSoundElementAction(x, y, element, ACTION_COLLECTING);
+ break;
+
+ case SAMPLE_diamond:
+ PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
+ break;
+
+ case SAMPLE_squash:
+ /* !!! CHECK THIS !!! */
+#if 1
+ PlayLevelSoundElementAction(x, y, element, ACTION_BREAKING);
+#else
+ PlayLevelSoundElementAction(x, y, element, ACTION_SMASHED_BY_ROCK);
+#endif
+ break;
+
+ case SAMPLE_wonderfall:
+ PlayLevelSoundElementAction(x, y, element, ACTION_FILLING);
+ break;
+
+ case SAMPLE_drip:
+ PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
+ break;
+
+ case SAMPLE_push:
+ PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
+ break;
+
+ case SAMPLE_dirt:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
+ break;
+
+ case SAMPLE_acid:
+ PlayLevelSoundElementAction(x, y, element, ACTION_SPLASHING);
+ break;
+
+ case SAMPLE_ball:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
+ break;
+
+ case SAMPLE_grow:
+ PlayLevelSoundElementAction(x, y, element, ACTION_GROWING);
+ break;
+
+ case SAMPLE_wonder:
+ PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
+ break;
+
+ case SAMPLE_door:
+ PlayLevelSoundElementAction(x, y, element, ACTION_PASSING);
+ break;
+
+ case SAMPLE_exit_open:
+ PlayLevelSoundElementAction(x, y, element, ACTION_OPENING);
+ break;
+
+ case SAMPLE_exit_leave:
+ PlayLevelSoundElementAction(x, y, element, ACTION_PASSING);
+ break;
+
+ case SAMPLE_dynamite:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
+ break;
+
+ case SAMPLE_tick:
+ PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
+ break;
+
+ case SAMPLE_press:
+ PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVATING);
+ break;
+
+ case SAMPLE_wheel:
+ PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
+ break;
+
+ case SAMPLE_boom:
+ PlayLevelSoundElementAction(x, y, element, ACTION_EXPLODING);
+ break;
+
+ case SAMPLE_die:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DYING);
+ break;
+
+ case SAMPLE_time:
+ PlaySound(SND_GAME_RUNNING_OUT_OF_TIME);
+ break;
+
+ default:
+ PlayLevelSoundElementAction(x, y, element, ACTION_DEFAULT);
+ break;
+ }
+}
+
+void PlayLevelSound_SP(int xx, int yy, int element_sp, int action_sp)
+{
+ int element = map_element_SP_to_RND(element_sp);
+ int action = map_action_SP_to_RND(action_sp);
+ int offset = (setup.sp_show_border_elements ? 0 : 1);
+ int x = xx - offset;
+ int y = yy - offset;
+
+ PlayLevelSoundElementAction(x, y, element, action);
+}
+
+void RaiseScore(int value)
+{
+ local_player->score += value;
+
+ game_panel_controls[GAME_PANEL_SCORE].value = local_player->score;
+
+ DisplayGameControlValues();
+}
+
+void RaiseScoreElement(int element)
+{
+ switch (element)
+ {
+ case EL_EMERALD:
+ case EL_BD_DIAMOND:
+ case EL_EMERALD_YELLOW:
+ case EL_EMERALD_RED:
+ case EL_EMERALD_PURPLE:
+ case EL_SP_INFOTRON:
+ RaiseScore(level.score[SC_EMERALD]);
+ break;
+ case EL_DIAMOND:
+ RaiseScore(level.score[SC_DIAMOND]);
+ break;
+ case EL_CRYSTAL:
+ RaiseScore(level.score[SC_CRYSTAL]);
+ break;
+ case EL_PEARL:
+ RaiseScore(level.score[SC_PEARL]);
+ break;
+ case EL_BUG:
+ case EL_BD_BUTTERFLY:
+ case EL_SP_ELECTRON:
+ RaiseScore(level.score[SC_BUG]);
+ break;
+ case EL_SPACESHIP:
+ case EL_BD_FIREFLY:
+ case EL_SP_SNIKSNAK:
+ RaiseScore(level.score[SC_SPACESHIP]);
+ break;
+ case EL_YAMYAM:
+ case EL_DARK_YAMYAM:
+ RaiseScore(level.score[SC_YAMYAM]);
+ break;
+ case EL_ROBOT:
+ RaiseScore(level.score[SC_ROBOT]);
+ break;
+ case EL_PACMAN:
+ RaiseScore(level.score[SC_PACMAN]);
+ break;
+ case EL_NUT:
+ RaiseScore(level.score[SC_NUT]);
+ break;
+ case EL_DYNAMITE:
+ case EL_EM_DYNAMITE:
+ case EL_SP_DISK_RED:
+ case EL_DYNABOMB_INCREASE_NUMBER:
+ case EL_DYNABOMB_INCREASE_SIZE:
+ case EL_DYNABOMB_INCREASE_POWER:
+ RaiseScore(level.score[SC_DYNAMITE]);
+ break;
+ case EL_SHIELD_NORMAL:
+ case EL_SHIELD_DEADLY:
+ RaiseScore(level.score[SC_SHIELD]);
+ break;
+ case EL_EXTRA_TIME:
+ RaiseScore(level.extra_time_score);
+ break;
+ case EL_KEY_1:
+ case EL_KEY_2:
+ case EL_KEY_3:
+ case EL_KEY_4:
+ case EL_EM_KEY_1:
+ case EL_EM_KEY_2:
+ case EL_EM_KEY_3:
+ case EL_EM_KEY_4:
+ case EL_EMC_KEY_5:
+ case EL_EMC_KEY_6:
+ case EL_EMC_KEY_7:
+ case EL_EMC_KEY_8:
+ case EL_DC_KEY_WHITE:
+ RaiseScore(level.score[SC_KEY]);
+ break;
+ default:
+ RaiseScore(element_info[element].collect_score);
+ break;
+ }
+}
+
+void RequestQuitGameExt(boolean skip_request, boolean quick_quit, char *message)
+{
+ if (skip_request || Request(message, REQ_ASK | REQ_STAY_CLOSED))
+ {
+ /* closing door required in case of envelope style request dialogs */
+ if (!skip_request)
+ CloseDoor(DOOR_CLOSE_1);