1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #include "libgame/libgame.h"
24 /* EXPERIMENTAL STUFF */
25 #define USE_NEW_AMOEBA_CODE FALSE
27 /* EXPERIMENTAL STUFF */
28 #define USE_NEW_STUFF ( 1)
30 #define USE_NEW_SP_SLIPPERY (USE_NEW_STUFF * 1)
31 #define USE_NEW_CUSTOM_VALUE (USE_NEW_STUFF * 1)
32 #define USE_NEW_PLAYER_ANIM (USE_NEW_STUFF * 1)
33 #define USE_NEW_ALL_SLIPPERY (USE_NEW_STUFF * 1)
34 #define USE_NEW_PLAYER_SPEED (USE_NEW_STUFF * 1)
35 #define USE_NEW_DELAYED_ACTION (USE_NEW_STUFF * 1)
36 #define USE_NEW_SNAP_DELAY (USE_NEW_STUFF * 1)
37 #define USE_ONLY_ONE_CHANGE_PER_FRAME (USE_NEW_STUFF * 1)
38 #define USE_ONE_MORE_CHANGE_PER_FRAME (USE_NEW_STUFF * 1)
39 #define USE_FIXED_DONT_RUN_INTO (USE_NEW_STUFF * 1)
40 #define USE_NEW_SPRING_BUMPER (USE_NEW_STUFF * 1)
41 #define USE_STOP_CHANGED_ELEMENTS (USE_NEW_STUFF * 1)
42 #define USE_ELEMENT_TOUCHING_BUGFIX (USE_NEW_STUFF * 1)
43 #define USE_NEW_CONTINUOUS_SNAPPING (USE_NEW_STUFF * 1)
45 #define USE_QUICKSAND_IMPACT_BUGFIX (USE_NEW_STUFF * 0)
52 /* for MovePlayer() */
53 #define MP_NO_ACTION 0
56 #define MP_DONT_RUN_INTO (MP_MOVING | MP_ACTION)
58 /* for ScrollPlayer() */
60 #define SCROLL_GO_ON 1
62 /* for Bang()/Explode() */
63 #define EX_PHASE_START 0
64 #define EX_TYPE_NONE 0
65 #define EX_TYPE_NORMAL (1 << 0)
66 #define EX_TYPE_CENTER (1 << 1)
67 #define EX_TYPE_BORDER (1 << 2)
68 #define EX_TYPE_CROSS (1 << 3)
69 #define EX_TYPE_DYNA (1 << 4)
70 #define EX_TYPE_SINGLE_TILE (EX_TYPE_CENTER | EX_TYPE_BORDER)
72 /* special positions in the game control window (relative to control window) */
75 #define XX_EMERALDS 29
76 #define YY_EMERALDS 54
77 #define XX_DYNAMITE 29
78 #define YY_DYNAMITE 89
87 /* special positions in the game control window (relative to main window) */
88 #define DX_LEVEL (DX + XX_LEVEL)
89 #define DY_LEVEL (DY + YY_LEVEL)
90 #define DX_EMERALDS (DX + XX_EMERALDS)
91 #define DY_EMERALDS (DY + YY_EMERALDS)
92 #define DX_DYNAMITE (DX + XX_DYNAMITE)
93 #define DY_DYNAMITE (DY + YY_DYNAMITE)
94 #define DX_KEYS (DX + XX_KEYS)
95 #define DY_KEYS (DY + YY_KEYS)
96 #define DX_SCORE (DX + XX_SCORE)
97 #define DY_SCORE (DY + YY_SCORE)
98 #define DX_TIME1 (DX + XX_TIME1)
99 #define DX_TIME2 (DX + XX_TIME2)
100 #define DY_TIME (DY + YY_TIME)
102 /* values for delayed check of falling and moving elements and for collision */
103 #define CHECK_DELAY_MOVING 3
104 #define CHECK_DELAY_FALLING 3
105 #define CHECK_DELAY_COLLISION 2
107 /* values for initial player move delay (initial delay counter value) */
108 #define INITIAL_MOVE_DELAY_OFF -1
109 #define INITIAL_MOVE_DELAY_ON 0
111 /* values for player movement speed (which is in fact a delay value) */
112 #define MOVE_DELAY_MIN_SPEED 32
113 #define MOVE_DELAY_NORMAL_SPEED 8
114 #define MOVE_DELAY_HIGH_SPEED 4
115 #define MOVE_DELAY_MAX_SPEED 1
118 #define DOUBLE_MOVE_DELAY(x) (x = (x <= MOVE_DELAY_HIGH_SPEED ? x * 2 : x))
119 #define HALVE_MOVE_DELAY(x) (x = (x >= MOVE_DELAY_HIGH_SPEED ? x / 2 : x))
121 #define DOUBLE_MOVE_DELAY(x) (x = (x < MOVE_DELAY_MIN_SPEED ? x * 2 : x))
122 #define HALVE_MOVE_DELAY(x) (x = (x > MOVE_DELAY_MAX_SPEED ? x / 2 : x))
124 #define DOUBLE_PLAYER_SPEED(p) (HALVE_MOVE_DELAY((p)->move_delay_value))
125 #define HALVE_PLAYER_SPEED(p) (DOUBLE_MOVE_DELAY((p)->move_delay_value))
127 /* values for other actions */
128 #define MOVE_STEPSIZE_NORMAL (TILEX / MOVE_DELAY_NORMAL_SPEED)
129 #define MOVE_STEPSIZE_MIN (1)
130 #define MOVE_STEPSIZE_MAX (TILEX)
132 #define GET_DX_FROM_DIR(d) ((d) == MV_LEFT ? -1 : (d) == MV_RIGHT ? 1 : 0)
133 #define GET_DY_FROM_DIR(d) ((d) == MV_UP ? -1 : (d) == MV_DOWN ? 1 : 0)
135 #define INIT_GFX_RANDOM() (SimpleRND(1000000))
137 #define GET_NEW_PUSH_DELAY(e) ( (element_info[e].push_delay_fixed) + \
138 RND(element_info[e].push_delay_random))
139 #define GET_NEW_DROP_DELAY(e) ( (element_info[e].drop_delay_fixed) + \
140 RND(element_info[e].drop_delay_random))
141 #define GET_NEW_MOVE_DELAY(e) ( (element_info[e].move_delay_fixed) + \
142 RND(element_info[e].move_delay_random))
143 #define GET_MAX_MOVE_DELAY(e) ( (element_info[e].move_delay_fixed) + \
144 (element_info[e].move_delay_random))
145 #define GET_NEW_CUSTOM_VALUE(e) ( (element_info[e].ce_value_fixed_initial) +\
146 RND(element_info[e].ce_value_random_initial))
147 #define GET_CHANGE_DELAY(c) ( ((c)->delay_fixed * (c)->delay_frames) + \
148 RND((c)->delay_random * (c)->delay_frames))
149 #define GET_CE_DELAY_VALUE(c) ( ((c)->delay_fixed) + \
150 RND((c)->delay_random))
153 #define GET_VALID_RUNTIME_ELEMENT(e) \
154 ((e) >= NUM_RUNTIME_ELEMENTS ? EL_UNKNOWN : (e))
156 #define GET_VALID_FILE_ELEMENT(e) \
157 ((e) >= NUM_FILE_ELEMENTS ? EL_UNKNOWN : (e))
160 #define GET_TARGET_ELEMENT(e, ch) \
161 ((e) == EL_TRIGGER_PLAYER ? (ch)->actual_trigger_player : \
162 (e) == EL_TRIGGER_ELEMENT ? (ch)->actual_trigger_element : \
163 (e) == EL_TRIGGER_CE_VALUE ? (ch)->actual_trigger_ce_value : (e))
165 #define CAN_GROW_INTO(e) \
166 ((e) == EL_SAND || (IS_DIGGABLE(e) && level.grow_into_diggable))
168 #define ELEMENT_CAN_ENTER_FIELD_BASE_X(x, y, condition) \
169 (IN_LEV_FIELD(x, y) && (IS_FREE(x, y) || \
172 #define ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, condition) \
173 (IN_LEV_FIELD(x, y) && (IS_FREE(x, y) || \
174 (CAN_MOVE_INTO_ACID(e) && \
175 Feld[x][y] == EL_ACID) || \
178 #define ELEMENT_CAN_ENTER_FIELD_BASE_3(e, x, y, condition) \
179 (IN_LEV_FIELD(x, y) && (IS_FREE_OR_PLAYER(x, y) || \
180 (CAN_MOVE_INTO_ACID(e) && \
181 Feld[x][y] == EL_ACID) || \
184 #define ELEMENT_CAN_ENTER_FIELD_BASE_4(e, x, y, condition) \
185 (IN_LEV_FIELD(x, y) && (IS_FREE(x, y) || \
187 (CAN_MOVE_INTO_ACID(e) && \
188 Feld[x][y] == EL_ACID) || \
189 (DONT_COLLIDE_WITH(e) && \
191 !PLAYER_ENEMY_PROTECTED(x, y))))
193 #define ELEMENT_CAN_ENTER_FIELD(e, x, y) \
194 ELEMENT_CAN_ENTER_FIELD_BASE_4(e, x, y, 0)
196 #define SATELLITE_CAN_ENTER_FIELD(x, y) \
197 ELEMENT_CAN_ENTER_FIELD_BASE_2(EL_SATELLITE, x, y, 0)
199 #define ANDROID_CAN_ENTER_FIELD(e, x, y) \
200 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, Feld[x][y] == EL_EMC_PLANT)
202 #define ANDROID_CAN_CLONE_FIELD(x, y) \
203 (IN_LEV_FIELD(x, y) && (CAN_BE_CLONED_BY_ANDROID(Feld[x][y]) || \
204 CAN_BE_CLONED_BY_ANDROID(EL_TRIGGER_ELEMENT)))
206 #define ENEMY_CAN_ENTER_FIELD(e, x, y) \
207 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, 0)
209 #define YAMYAM_CAN_ENTER_FIELD(e, x, y) \
210 ELEMENT_CAN_ENTER_FIELD_BASE_3(e, x, y, Feld[x][y] == EL_DIAMOND)
212 #define DARK_YAMYAM_CAN_ENTER_FIELD(e, x, y) \
213 ELEMENT_CAN_ENTER_FIELD_BASE_3(e, x,y, IS_FOOD_DARK_YAMYAM(Feld[x][y]))
215 #define PACMAN_CAN_ENTER_FIELD(e, x, y) \
216 ELEMENT_CAN_ENTER_FIELD_BASE_3(e, x, y, IS_AMOEBOID(Feld[x][y]))
218 #define PIG_CAN_ENTER_FIELD(e, x, y) \
219 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, IS_FOOD_PIG(Feld[x][y]))
221 #define PENGUIN_CAN_ENTER_FIELD(e, x, y) \
222 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, (Feld[x][y] == EL_EXIT_OPEN ||\
223 IS_FOOD_PENGUIN(Feld[x][y])))
224 #define DRAGON_CAN_ENTER_FIELD(e, x, y) \
225 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, 0)
227 #define MOLE_CAN_ENTER_FIELD(e, x, y, condition) \
228 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, (condition))
230 #define SPRING_CAN_ENTER_FIELD(e, x, y) \
231 ELEMENT_CAN_ENTER_FIELD_BASE_2(e, x, y, 0)
233 #define SPRING_CAN_BUMP_FROM_FIELD(x, y) \
234 (IN_LEV_FIELD(x, y) && (Feld[x][y] == EL_EMC_SPRING_BUMPER || \
235 Feld[x][y] == EL_EMC_SPRING_BUMPER_ACTIVE))
238 #define GROUP_NR(e) ((e) - EL_GROUP_START)
239 #define IS_IN_GROUP(e, nr) (element_info[e].in_group[nr] == TRUE)
240 #define IS_IN_GROUP_EL(e, ge) (IS_IN_GROUP(e, (ge) - EL_GROUP_START))
242 #define IS_EQUAL_OR_IN_GROUP(e, ge) \
243 (IS_GROUP_ELEMENT(ge) ? IS_IN_GROUP(e, GROUP_NR(ge)) : (e) == (ge))
246 #define MOVE_ENTER_EL(e) (element_info[e].move_enter_element)
248 #define CE_ENTER_FIELD_COND(e, x, y) \
249 (!IS_PLAYER(x, y) && \
250 IS_EQUAL_OR_IN_GROUP(Feld[x][y], MOVE_ENTER_EL(e)))
252 #define CUSTOM_ELEMENT_CAN_ENTER_FIELD(e, x, y) \
253 ELEMENT_CAN_ENTER_FIELD_BASE_4(e, x, y, CE_ENTER_FIELD_COND(e, x, y))
255 #define IN_LEV_FIELD_AND_IS_FREE(x, y) (IN_LEV_FIELD(x, y) && IS_FREE(x, y))
256 #define IN_LEV_FIELD_AND_NOT_FREE(x, y) (IN_LEV_FIELD(x, y) && !IS_FREE(x, y))
258 #define ACCESS_FROM(e, d) (element_info[e].access_direction &(d))
259 #define IS_WALKABLE_FROM(e, d) (IS_WALKABLE(e) && ACCESS_FROM(e, d))
260 #define IS_PASSABLE_FROM(e, d) (IS_PASSABLE(e) && ACCESS_FROM(e, d))
261 #define IS_ACCESSIBLE_FROM(e, d) (IS_ACCESSIBLE(e) && ACCESS_FROM(e, d))
263 /* game button identifiers */
264 #define GAME_CTRL_ID_STOP 0
265 #define GAME_CTRL_ID_PAUSE 1
266 #define GAME_CTRL_ID_PLAY 2
267 #define SOUND_CTRL_ID_MUSIC 3
268 #define SOUND_CTRL_ID_LOOPS 4
269 #define SOUND_CTRL_ID_SIMPLE 5
271 #define NUM_GAME_BUTTONS 6
274 /* forward declaration for internal use */
276 static void CreateField(int, int, int);
278 static void SetPlayerWaiting(struct PlayerInfo *, boolean);
279 static void AdvanceFrameAndPlayerCounters(int);
281 static boolean MovePlayerOneStep(struct PlayerInfo *, int, int, int, int);
282 static boolean MovePlayer(struct PlayerInfo *, int, int);
283 static void ScrollPlayer(struct PlayerInfo *, int);
284 static void ScrollScreen(struct PlayerInfo *, int);
286 int DigField(struct PlayerInfo *, int, int, int, int, int, int, int);
288 static void InitBeltMovement(void);
289 static void CloseAllOpenTimegates(void);
290 static void CheckGravityMovement(struct PlayerInfo *);
291 static void CheckGravityMovementWhenNotMoving(struct PlayerInfo *);
292 static void KillPlayerUnlessEnemyProtected(int, int);
293 static void KillPlayerUnlessExplosionProtected(int, int);
295 static void TestIfPlayerTouchesCustomElement(int, int);
296 static void TestIfElementTouchesCustomElement(int, int);
297 static void TestIfElementHitsCustomElement(int, int, int);
299 static void TestIfElementSmashesCustomElement(int, int, int);
302 static void HandleElementChange(int, int, int);
303 static void ExecuteCustomElementAction(int, int, int, int);
304 static boolean ChangeElement(int, int, int, int);
306 static boolean CheckTriggeredElementChangeExt(int, int, int, int, int,int,int);
307 #define CheckTriggeredElementChange(x, y, e, ev) \
308 CheckTriggeredElementChangeExt(x,y,e,ev, CH_PLAYER_ANY,CH_SIDE_ANY, -1)
309 #define CheckTriggeredElementChangeByPlayer(x, y, e, ev, p, s) \
310 CheckTriggeredElementChangeExt(x, y, e, ev, p, s, -1)
311 #define CheckTriggeredElementChangeBySide(x, y, e, ev, s) \
312 CheckTriggeredElementChangeExt(x, y, e, ev, CH_PLAYER_ANY, s, -1)
313 #define CheckTriggeredElementChangeByPage(x, y, e, ev, p) \
314 CheckTriggeredElementChangeExt(x,y,e,ev, CH_PLAYER_ANY, CH_SIDE_ANY, p)
316 static boolean CheckElementChangeExt(int, int, int, int, int, int, int);
317 #define CheckElementChange(x, y, e, te, ev) \
318 CheckElementChangeExt(x, y, e, te, ev, CH_PLAYER_ANY, CH_SIDE_ANY)
319 #define CheckElementChangeByPlayer(x, y, e, ev, p, s) \
320 CheckElementChangeExt(x, y, e, EL_EMPTY, ev, p, s)
321 #define CheckElementChangeBySide(x, y, e, te, ev, s) \
322 CheckElementChangeExt(x, y, e, te, ev, CH_PLAYER_ANY, s)
324 static void PlayLevelSound(int, int, int);
325 static void PlayLevelSoundNearest(int, int, int);
326 static void PlayLevelSoundAction(int, int, int);
327 static void PlayLevelSoundElementAction(int, int, int, int);
328 static void PlayLevelSoundElementActionIfLoop(int, int, int, int);
329 static void PlayLevelSoundActionIfLoop(int, int, int);
330 static void StopLevelSoundActionIfLoop(int, int, int);
331 static void PlayLevelMusic();
333 static void MapGameButtons();
334 static void HandleGameButtons(struct GadgetInfo *);
336 int AmoebeNachbarNr(int, int);
337 void AmoebeUmwandeln(int, int);
338 void ContinueMoving(int, int);
340 void InitMovDir(int, int);
341 void InitAmoebaNr(int, int);
342 int NewHiScore(void);
344 void TestIfGoodThingHitsBadThing(int, int, int);
345 void TestIfBadThingHitsGoodThing(int, int, int);
346 void TestIfPlayerTouchesBadThing(int, int);
347 void TestIfPlayerRunsIntoBadThing(int, int, int);
348 void TestIfBadThingTouchesPlayer(int, int);
349 void TestIfBadThingRunsIntoPlayer(int, int, int);
350 void TestIfFriendTouchesBadThing(int, int);
351 void TestIfBadThingTouchesFriend(int, int);
352 void TestIfBadThingTouchesOtherBadThing(int, int);
354 void KillPlayer(struct PlayerInfo *);
355 void BuryPlayer(struct PlayerInfo *);
356 void RemovePlayer(struct PlayerInfo *);
358 boolean SnapField(struct PlayerInfo *, int, int);
359 boolean DropElement(struct PlayerInfo *);
362 static struct GadgetInfo *game_gadget[NUM_GAME_BUTTONS];
365 /* ------------------------------------------------------------------------- */
366 /* definition of elements that automatically change to other elements after */
367 /* a specified time, eventually calling a function when changing */
368 /* ------------------------------------------------------------------------- */
370 /* forward declaration for changer functions */
371 static void InitBuggyBase(int, int);
372 static void WarnBuggyBase(int, int);
374 static void InitTrap(int, int);
375 static void ActivateTrap(int, int);
376 static void ChangeActiveTrap(int, int);
378 static void InitRobotWheel(int, int);
379 static void RunRobotWheel(int, int);
380 static void StopRobotWheel(int, int);
382 static void InitTimegateWheel(int, int);
383 static void RunTimegateWheel(int, int);
385 static void InitMagicBallDelay(int, int);
386 static void ActivateMagicBall(int, int);
388 static void InitDiagonalMovingElement(int, int);
390 struct ChangingElementInfo
395 void (*pre_change_function)(int x, int y);
396 void (*change_function)(int x, int y);
397 void (*post_change_function)(int x, int y);
400 static struct ChangingElementInfo change_delay_list[] =
451 EL_SWITCHGATE_OPENING,
459 EL_SWITCHGATE_CLOSING,
460 EL_SWITCHGATE_CLOSED,
492 EL_ACID_SPLASH_RIGHT,
501 EL_SP_BUGGY_BASE_ACTIVATING,
508 EL_SP_BUGGY_BASE_ACTIVATING,
509 EL_SP_BUGGY_BASE_ACTIVE,
516 EL_SP_BUGGY_BASE_ACTIVE,
540 EL_ROBOT_WHEEL_ACTIVE,
548 EL_TIMEGATE_SWITCH_ACTIVE,
556 EL_EMC_MAGIC_BALL_ACTIVE,
557 EL_EMC_MAGIC_BALL_ACTIVE,
564 EL_EMC_SPRING_BUMPER_ACTIVE,
565 EL_EMC_SPRING_BUMPER,
572 EL_DIAGONAL_SHRINKING,
585 InitDiagonalMovingElement
601 int push_delay_fixed, push_delay_random;
606 { EL_BALLOON, 0, 0 },
608 { EL_SOKOBAN_OBJECT, 2, 0 },
609 { EL_SOKOBAN_FIELD_FULL, 2, 0 },
610 { EL_SATELLITE, 2, 0 },
611 { EL_SP_DISK_YELLOW, 2, 0 },
613 { EL_UNDEFINED, 0, 0 },
621 move_stepsize_list[] =
623 { EL_AMOEBA_DROP, 2 },
624 { EL_AMOEBA_DROPPING, 2 },
625 { EL_QUICKSAND_FILLING, 1 },
626 { EL_QUICKSAND_EMPTYING, 1 },
627 { EL_MAGIC_WALL_FILLING, 2 },
628 { EL_BD_MAGIC_WALL_FILLING, 2 },
629 { EL_MAGIC_WALL_EMPTYING, 2 },
630 { EL_BD_MAGIC_WALL_EMPTYING, 2 },
640 collect_count_list[] =
643 { EL_BD_DIAMOND, 1 },
644 { EL_EMERALD_YELLOW, 1 },
645 { EL_EMERALD_RED, 1 },
646 { EL_EMERALD_PURPLE, 1 },
648 { EL_SP_INFOTRON, 1 },
660 access_direction_list[] =
662 { EL_TUBE_ANY, MV_LEFT | MV_RIGHT | MV_UP | MV_DOWN },
663 { EL_TUBE_VERTICAL, MV_UP | MV_DOWN },
664 { EL_TUBE_HORIZONTAL, MV_LEFT | MV_RIGHT },
665 { EL_TUBE_VERTICAL_LEFT, MV_LEFT | MV_UP | MV_DOWN },
666 { EL_TUBE_VERTICAL_RIGHT, MV_RIGHT | MV_UP | MV_DOWN },
667 { EL_TUBE_HORIZONTAL_UP, MV_LEFT | MV_RIGHT | MV_UP },
668 { EL_TUBE_HORIZONTAL_DOWN, MV_LEFT | MV_RIGHT | MV_DOWN },
669 { EL_TUBE_LEFT_UP, MV_LEFT | MV_UP },
670 { EL_TUBE_LEFT_DOWN, MV_LEFT | MV_DOWN },
671 { EL_TUBE_RIGHT_UP, MV_RIGHT | MV_UP },
672 { EL_TUBE_RIGHT_DOWN, MV_RIGHT | MV_DOWN },
674 { EL_SP_PORT_LEFT, MV_RIGHT },
675 { EL_SP_PORT_RIGHT, MV_LEFT },
676 { EL_SP_PORT_UP, MV_DOWN },
677 { EL_SP_PORT_DOWN, MV_UP },
678 { EL_SP_PORT_HORIZONTAL, MV_LEFT | MV_RIGHT },
679 { EL_SP_PORT_VERTICAL, MV_UP | MV_DOWN },
680 { EL_SP_PORT_ANY, MV_LEFT | MV_RIGHT | MV_UP | MV_DOWN },
681 { EL_SP_GRAVITY_PORT_LEFT, MV_RIGHT },
682 { EL_SP_GRAVITY_PORT_RIGHT, MV_LEFT },
683 { EL_SP_GRAVITY_PORT_UP, MV_DOWN },
684 { EL_SP_GRAVITY_PORT_DOWN, MV_UP },
685 { EL_SP_GRAVITY_ON_PORT_LEFT, MV_RIGHT },
686 { EL_SP_GRAVITY_ON_PORT_RIGHT, MV_LEFT },
687 { EL_SP_GRAVITY_ON_PORT_UP, MV_DOWN },
688 { EL_SP_GRAVITY_ON_PORT_DOWN, MV_UP },
689 { EL_SP_GRAVITY_OFF_PORT_LEFT, MV_RIGHT },
690 { EL_SP_GRAVITY_OFF_PORT_RIGHT, MV_LEFT },
691 { EL_SP_GRAVITY_OFF_PORT_UP, MV_DOWN },
692 { EL_SP_GRAVITY_OFF_PORT_DOWN, MV_UP },
694 { EL_UNDEFINED, MV_NONE }
697 static boolean trigger_events[MAX_NUM_ELEMENTS][NUM_CHANGE_EVENTS];
699 #define IS_AUTO_CHANGING(e) (element_info[e].has_change_event[CE_DELAY])
700 #define IS_JUST_CHANGING(x, y) (ChangeDelay[x][y] != 0)
701 #define IS_CHANGING(x, y) (IS_AUTO_CHANGING(Feld[x][y]) || \
702 IS_JUST_CHANGING(x, y))
704 #define CE_PAGE(e, ce) (element_info[e].event_page[ce])
706 /* static variables for playfield scan mode (scanning forward or backward) */
707 static int playfield_scan_start_x = 0;
708 static int playfield_scan_start_y = 0;
709 static int playfield_scan_delta_x = 1;
710 static int playfield_scan_delta_y = 1;
712 #define SCAN_PLAYFIELD(x, y) for ((y) = playfield_scan_start_y; \
713 (y) >= 0 && (y) <= lev_fieldy - 1; \
714 (y) += playfield_scan_delta_y) \
715 for ((x) = playfield_scan_start_x; \
716 (x) >= 0 && (x) <= lev_fieldx - 1; \
717 (x) += playfield_scan_delta_x) \
720 void DEBUG_SetMaximumDynamite()
724 for (i = 0; i < MAX_INVENTORY_SIZE; i++)
725 if (local_player->inventory_size < MAX_INVENTORY_SIZE)
726 local_player->inventory_element[local_player->inventory_size++] =
731 static void InitPlayfieldScanModeVars()
733 if (game.use_reverse_scan_direction)
735 playfield_scan_start_x = lev_fieldx - 1;
736 playfield_scan_start_y = lev_fieldy - 1;
738 playfield_scan_delta_x = -1;
739 playfield_scan_delta_y = -1;
743 playfield_scan_start_x = 0;
744 playfield_scan_start_y = 0;
746 playfield_scan_delta_x = 1;
747 playfield_scan_delta_y = 1;
751 static void InitPlayfieldScanMode(int mode)
753 game.use_reverse_scan_direction =
754 (mode == CA_ARG_SCAN_MODE_REVERSE ? TRUE : FALSE);
756 InitPlayfieldScanModeVars();
759 static int get_move_delay_from_stepsize(int move_stepsize)
762 MIN(MAX(MOVE_STEPSIZE_MIN, move_stepsize), MOVE_STEPSIZE_MAX);
764 /* make sure that stepsize value is always a power of 2 */
765 move_stepsize = (1 << log_2(move_stepsize));
767 return TILEX / move_stepsize;
770 static void SetPlayerMoveSpeed(struct PlayerInfo *player, int move_stepsize,
773 int move_delay = get_move_delay_from_stepsize(move_stepsize);
774 boolean cannot_move = (move_stepsize == STEPSIZE_NOT_MOVING ? TRUE : FALSE);
776 /* do no immediately change move delay -- the player might just be moving */
777 player->move_delay_value_next = move_delay;
779 /* information if player can move must be set separately */
780 player->cannot_move = cannot_move;
784 player->move_delay = game.initial_move_delay;
785 player->move_delay_value = game.initial_move_delay_value;
787 player->move_delay_value_next = -1;
789 player->move_delay_reset_counter = 0;
793 void GetPlayerConfig()
795 if (!audio.sound_available)
796 setup.sound_simple = FALSE;
798 if (!audio.loops_available)
799 setup.sound_loops = FALSE;
801 if (!audio.music_available)
802 setup.sound_music = FALSE;
804 if (!video.fullscreen_available)
805 setup.fullscreen = FALSE;
807 setup.sound = (setup.sound_simple || setup.sound_loops || setup.sound_music);
809 SetAudioMode(setup.sound);
813 static int getBeltNrFromBeltElement(int element)
815 return (element < EL_CONVEYOR_BELT_2_LEFT ? 0 :
816 element < EL_CONVEYOR_BELT_3_LEFT ? 1 :
817 element < EL_CONVEYOR_BELT_4_LEFT ? 2 : 3);
820 static int getBeltNrFromBeltActiveElement(int element)
822 return (element < EL_CONVEYOR_BELT_2_LEFT_ACTIVE ? 0 :
823 element < EL_CONVEYOR_BELT_3_LEFT_ACTIVE ? 1 :
824 element < EL_CONVEYOR_BELT_4_LEFT_ACTIVE ? 2 : 3);
827 static int getBeltNrFromBeltSwitchElement(int element)
829 return (element < EL_CONVEYOR_BELT_2_SWITCH_LEFT ? 0 :
830 element < EL_CONVEYOR_BELT_3_SWITCH_LEFT ? 1 :
831 element < EL_CONVEYOR_BELT_4_SWITCH_LEFT ? 2 : 3);
834 static int getBeltDirNrFromBeltSwitchElement(int element)
836 static int belt_base_element[4] =
838 EL_CONVEYOR_BELT_1_SWITCH_LEFT,
839 EL_CONVEYOR_BELT_2_SWITCH_LEFT,
840 EL_CONVEYOR_BELT_3_SWITCH_LEFT,
841 EL_CONVEYOR_BELT_4_SWITCH_LEFT
844 int belt_nr = getBeltNrFromBeltSwitchElement(element);
845 int belt_dir_nr = element - belt_base_element[belt_nr];
847 return (belt_dir_nr % 3);
850 static int getBeltDirFromBeltSwitchElement(int element)
852 static int belt_move_dir[3] =
859 int belt_dir_nr = getBeltDirNrFromBeltSwitchElement(element);
861 return belt_move_dir[belt_dir_nr];
864 static void InitPlayerField(int x, int y, int element, boolean init_game)
866 if (element == EL_SP_MURPHY)
870 if (stored_player[0].present)
872 Feld[x][y] = EL_SP_MURPHY_CLONE;
878 stored_player[0].use_murphy = TRUE;
880 if (!level.use_artwork_element[0])
881 stored_player[0].artwork_element = EL_SP_MURPHY;
884 Feld[x][y] = EL_PLAYER_1;
890 struct PlayerInfo *player = &stored_player[Feld[x][y] - EL_PLAYER_1];
891 int jx = player->jx, jy = player->jy;
893 player->present = TRUE;
895 player->block_last_field = (element == EL_SP_MURPHY ?
896 level.sp_block_last_field :
897 level.block_last_field);
899 /* ---------- initialize player's last field block delay --------------- */
901 /* always start with reliable default value (no adjustment needed) */
902 player->block_delay_adjustment = 0;
904 /* special case 1: in Supaplex, Murphy blocks last field one more frame */
905 if (player->block_last_field && element == EL_SP_MURPHY)
906 player->block_delay_adjustment = 1;
908 /* special case 2: in game engines before 3.1.1, blocking was different */
909 if (game.use_block_last_field_bug)
910 player->block_delay_adjustment = (player->block_last_field ? -1 : 1);
912 if (!options.network || player->connected)
914 player->active = TRUE;
916 /* remove potentially duplicate players */
917 if (StorePlayer[jx][jy] == Feld[x][y])
918 StorePlayer[jx][jy] = 0;
920 StorePlayer[x][y] = Feld[x][y];
924 printf("Player %d activated.\n", player->element_nr);
925 printf("[Local player is %d and currently %s.]\n",
926 local_player->element_nr,
927 local_player->active ? "active" : "not active");
931 Feld[x][y] = EL_EMPTY;
933 player->jx = player->last_jx = x;
934 player->jy = player->last_jy = y;
938 static void InitField(int x, int y, boolean init_game)
940 int element = Feld[x][y];
949 InitPlayerField(x, y, element, init_game);
952 case EL_SOKOBAN_FIELD_PLAYER:
953 element = Feld[x][y] = EL_PLAYER_1;
954 InitField(x, y, init_game);
956 element = Feld[x][y] = EL_SOKOBAN_FIELD_EMPTY;
957 InitField(x, y, init_game);
960 case EL_SOKOBAN_FIELD_EMPTY:
961 local_player->sokobanfields_still_needed++;
965 if (x < lev_fieldx-1 && Feld[x+1][y] == EL_ACID)
966 Feld[x][y] = EL_ACID_POOL_TOPLEFT;
967 else if (x > 0 && Feld[x-1][y] == EL_ACID)
968 Feld[x][y] = EL_ACID_POOL_TOPRIGHT;
969 else if (y > 0 && Feld[x][y-1] == EL_ACID_POOL_TOPLEFT)
970 Feld[x][y] = EL_ACID_POOL_BOTTOMLEFT;
971 else if (y > 0 && Feld[x][y-1] == EL_ACID)
972 Feld[x][y] = EL_ACID_POOL_BOTTOM;
973 else if (y > 0 && Feld[x][y-1] == EL_ACID_POOL_TOPRIGHT)
974 Feld[x][y] = EL_ACID_POOL_BOTTOMRIGHT;
983 case EL_SPACESHIP_RIGHT:
984 case EL_SPACESHIP_UP:
985 case EL_SPACESHIP_LEFT:
986 case EL_SPACESHIP_DOWN:
987 case EL_BD_BUTTERFLY:
988 case EL_BD_BUTTERFLY_RIGHT:
989 case EL_BD_BUTTERFLY_UP:
990 case EL_BD_BUTTERFLY_LEFT:
991 case EL_BD_BUTTERFLY_DOWN:
993 case EL_BD_FIREFLY_RIGHT:
994 case EL_BD_FIREFLY_UP:
995 case EL_BD_FIREFLY_LEFT:
996 case EL_BD_FIREFLY_DOWN:
997 case EL_PACMAN_RIGHT:
1000 case EL_PACMAN_DOWN:
1002 case EL_YAMYAM_LEFT:
1003 case EL_YAMYAM_RIGHT:
1005 case EL_YAMYAM_DOWN:
1006 case EL_DARK_YAMYAM:
1009 case EL_SP_SNIKSNAK:
1010 case EL_SP_ELECTRON:
1019 case EL_AMOEBA_FULL:
1024 case EL_AMOEBA_DROP:
1025 if (y == lev_fieldy - 1)
1027 Feld[x][y] = EL_AMOEBA_GROWING;
1028 Store[x][y] = EL_AMOEBA_WET;
1032 case EL_DYNAMITE_ACTIVE:
1033 case EL_SP_DISK_RED_ACTIVE:
1034 case EL_DYNABOMB_PLAYER_1_ACTIVE:
1035 case EL_DYNABOMB_PLAYER_2_ACTIVE:
1036 case EL_DYNABOMB_PLAYER_3_ACTIVE:
1037 case EL_DYNABOMB_PLAYER_4_ACTIVE:
1038 MovDelay[x][y] = 96;
1041 case EL_EM_DYNAMITE_ACTIVE:
1042 MovDelay[x][y] = 32;
1046 local_player->lights_still_needed++;
1050 local_player->friends_still_needed++;
1055 GfxDir[x][y] = MovDir[x][y] = 1 << RND(4);
1058 case EL_CONVEYOR_BELT_1_SWITCH_LEFT:
1059 case EL_CONVEYOR_BELT_1_SWITCH_MIDDLE:
1060 case EL_CONVEYOR_BELT_1_SWITCH_RIGHT:
1061 case EL_CONVEYOR_BELT_2_SWITCH_LEFT:
1062 case EL_CONVEYOR_BELT_2_SWITCH_MIDDLE:
1063 case EL_CONVEYOR_BELT_2_SWITCH_RIGHT:
1064 case EL_CONVEYOR_BELT_3_SWITCH_LEFT:
1065 case EL_CONVEYOR_BELT_3_SWITCH_MIDDLE:
1066 case EL_CONVEYOR_BELT_3_SWITCH_RIGHT:
1067 case EL_CONVEYOR_BELT_4_SWITCH_LEFT:
1068 case EL_CONVEYOR_BELT_4_SWITCH_MIDDLE:
1069 case EL_CONVEYOR_BELT_4_SWITCH_RIGHT:
1072 int belt_nr = getBeltNrFromBeltSwitchElement(Feld[x][y]);
1073 int belt_dir = getBeltDirFromBeltSwitchElement(Feld[x][y]);
1074 int belt_dir_nr = getBeltDirNrFromBeltSwitchElement(Feld[x][y]);
1076 if (game.belt_dir_nr[belt_nr] == 3) /* initial value */
1078 game.belt_dir[belt_nr] = belt_dir;
1079 game.belt_dir_nr[belt_nr] = belt_dir_nr;
1081 else /* more than one switch -- set it like the first switch */
1083 Feld[x][y] = Feld[x][y] - belt_dir_nr + game.belt_dir_nr[belt_nr];
1088 case EL_SWITCHGATE_SWITCH_DOWN: /* always start with same switch pos */
1090 Feld[x][y] = EL_SWITCHGATE_SWITCH_UP;
1093 case EL_LIGHT_SWITCH_ACTIVE:
1095 game.light_time_left = level.time_light * FRAMES_PER_SECOND;
1098 case EL_EMC_MAGIC_BALL:
1099 if (game.ball_state)
1100 Feld[x][y] = EL_EMC_MAGIC_BALL_ACTIVE;
1103 case EL_EMC_MAGIC_BALL_SWITCH:
1104 if (game.ball_state)
1105 Feld[x][y] = EL_EMC_MAGIC_BALL_SWITCH_ACTIVE;
1110 if (IS_CUSTOM_ELEMENT(element))
1112 if (CAN_MOVE(element))
1115 #if USE_NEW_CUSTOM_VALUE
1116 if (!element_info[element].use_last_ce_value || init_game)
1117 CustomValue[x][y] = GET_NEW_CUSTOM_VALUE(Feld[x][y]);
1121 if (IS_CUSTOM_ELEMENT(element) && CAN_MOVE(element))
1124 else if (IS_GROUP_ELEMENT(element))
1126 struct ElementGroupInfo *group = element_info[element].group;
1127 int last_anim_random_frame = gfx.anim_random_frame;
1130 if (group->choice_mode == ANIM_RANDOM)
1131 gfx.anim_random_frame = RND(group->num_elements_resolved);
1133 element_pos = getAnimationFrame(group->num_elements_resolved, 1,
1134 group->choice_mode, 0,
1137 if (group->choice_mode == ANIM_RANDOM)
1138 gfx.anim_random_frame = last_anim_random_frame;
1140 group->choice_pos++;
1142 Feld[x][y] = group->element_resolved[element_pos];
1144 InitField(x, y, init_game);
1151 CheckTriggeredElementChange(x, y, element, CE_CREATION_OF_X);
1156 #if USE_NEW_CUSTOM_VALUE
1159 CustomValue[x][y] = GET_NEW_CUSTOM_VALUE(Feld[x][y]);
1161 CustomValue[x][y] = element_info[Feld[x][y]].custom_value_initial;
1169 static inline void InitField_WithBug1(int x, int y, boolean init_game)
1171 InitField(x, y, init_game);
1173 /* not needed to call InitMovDir() -- already done by InitField()! */
1174 if (game.engine_version < VERSION_IDENT(3,1,0,0) &&
1175 CAN_MOVE(Feld[x][y]))
1179 static inline void InitField_WithBug2(int x, int y, boolean init_game)
1181 int old_element = Feld[x][y];
1183 InitField(x, y, init_game);
1185 /* not needed to call InitMovDir() -- already done by InitField()! */
1186 if (game.engine_version < VERSION_IDENT(3,1,0,0) &&
1187 CAN_MOVE(old_element) &&
1188 (old_element < EL_MOLE_LEFT || old_element > EL_MOLE_DOWN))
1191 /* this case is in fact a combination of not less than three bugs:
1192 first, it calls InitMovDir() for elements that can move, although this is
1193 already done by InitField(); then, it checks the element that was at this
1194 field _before_ the call to InitField() (which can change it); lastly, it
1195 was not called for "mole with direction" elements, which were treated as
1196 "cannot move" due to (fixed) wrong element initialization in "src/init.c"
1200 inline void DrawGameValue_Emeralds(int value)
1202 DrawText(DX_EMERALDS, DY_EMERALDS, int2str(value, 3), FONT_TEXT_2);
1205 inline void DrawGameValue_Dynamite(int value)
1207 DrawText(DX_DYNAMITE, DY_DYNAMITE, int2str(value, 3), FONT_TEXT_2);
1210 inline void DrawGameValue_Keys(int key[MAX_NUM_KEYS])
1212 int base_key_graphic = EL_KEY_1;
1215 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
1216 base_key_graphic = EL_EM_KEY_1;
1218 /* currently only 4 of 8 possible keys are displayed */
1219 for (i = 0; i < STD_NUM_KEYS; i++)
1222 DrawMiniGraphicExt(drawto, DX_KEYS + i * MINI_TILEX, DY_KEYS,
1223 el2edimg(base_key_graphic + i));
1225 BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
1226 DOOR_GFX_PAGEX5 + XX_KEYS + i * MINI_TILEX, YY_KEYS,
1227 MINI_TILEX, MINI_TILEY, DX_KEYS + i * MINI_TILEX, DY_KEYS);
1231 inline void DrawGameValue_Score(int value)
1233 DrawText(DX_SCORE, DY_SCORE, int2str(value, 5), FONT_TEXT_2);
1236 inline void DrawGameValue_Time(int value)
1239 DrawText(DX_TIME1, DY_TIME, int2str(value, 3), FONT_TEXT_2);
1241 DrawText(DX_TIME2, DY_TIME, int2str(value, 4), FONT_LEVEL_NUMBER);
1244 inline void DrawGameValue_Level(int value)
1247 DrawText(DX_LEVEL, DY_LEVEL, int2str(value, 2), FONT_TEXT_2);
1250 /* misuse area for displaying emeralds to draw bigger level number */
1251 DrawTextExt(drawto, DX_EMERALDS, DY_EMERALDS,
1252 int2str(value, 3), FONT_LEVEL_NUMBER, BLIT_OPAQUE);
1254 /* now copy it to the area for displaying level number */
1255 BlitBitmap(drawto, drawto,
1256 DX_EMERALDS, DY_EMERALDS + 1,
1257 getFontWidth(FONT_LEVEL_NUMBER) * 3,
1258 getFontHeight(FONT_LEVEL_NUMBER) - 1,
1259 DX_LEVEL - 1, DY_LEVEL + 1);
1261 /* restore the area for displaying emeralds */
1262 DrawGameValue_Emeralds(local_player->gems_still_needed);
1264 /* yes, this is all really ugly :-) */
1268 void DrawAllGameValues(int emeralds, int dynamite, int score, int time,
1271 int key[MAX_NUM_KEYS];
1274 for (i = 0; i < MAX_NUM_KEYS; i++)
1275 key[i] = key_bits & (1 << i);
1277 DrawGameValue_Level(level_nr);
1279 DrawGameValue_Emeralds(emeralds);
1280 DrawGameValue_Dynamite(dynamite);
1281 DrawGameValue_Score(score);
1282 DrawGameValue_Time(time);
1284 DrawGameValue_Keys(key);
1287 void DrawGameDoorValues()
1289 int dynamite_state = 0;
1293 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
1295 DrawGameDoorValues_EM();
1301 DrawGameValue_Level(level_nr);
1303 DrawGameValue_Emeralds(local_player->gems_still_needed);
1304 DrawGameValue_Dynamite(local_player->inventory_size);
1305 DrawGameValue_Score(local_player->score);
1306 DrawGameValue_Time(TimeLeft);
1310 if (game.centered_player_nr == -1)
1312 for (i = 0; i < MAX_PLAYERS; i++)
1314 for (j = 0; j < MAX_NUM_KEYS; j++)
1315 if (stored_player[i].key[j])
1316 key_bits |= (1 << j);
1318 dynamite_state += stored_player[i].inventory_size;
1322 DrawGameValue_Keys(stored_player[i].key);
1327 int player_nr = game.centered_player_nr;
1329 for (i = 0; i < MAX_NUM_KEYS; i++)
1330 if (stored_player[player_nr].key[i])
1331 key_bits |= (1 << i);
1333 dynamite_state = stored_player[player_nr].inventory_size;
1336 DrawAllGameValues(local_player->gems_still_needed, dynamite_state,
1337 local_player->score, TimeLeft, key_bits);
1342 static void resolve_group_element(int group_element, int recursion_depth)
1344 static int group_nr;
1345 static struct ElementGroupInfo *group;
1346 struct ElementGroupInfo *actual_group = element_info[group_element].group;
1349 if (recursion_depth > NUM_GROUP_ELEMENTS) /* recursion too deep */
1351 Error(ERR_WARN, "recursion too deep when resolving group element %d",
1352 group_element - EL_GROUP_START + 1);
1354 /* replace element which caused too deep recursion by question mark */
1355 group->element_resolved[group->num_elements_resolved++] = EL_UNKNOWN;
1360 if (recursion_depth == 0) /* initialization */
1362 group = element_info[group_element].group;
1363 group_nr = group_element - EL_GROUP_START;
1365 group->num_elements_resolved = 0;
1366 group->choice_pos = 0;
1369 for (i = 0; i < actual_group->num_elements; i++)
1371 int element = actual_group->element[i];
1373 if (group->num_elements_resolved == NUM_FILE_ELEMENTS)
1376 if (IS_GROUP_ELEMENT(element))
1377 resolve_group_element(element, recursion_depth + 1);
1380 group->element_resolved[group->num_elements_resolved++] = element;
1381 element_info[element].in_group[group_nr] = TRUE;
1388 =============================================================================
1390 -----------------------------------------------------------------------------
1391 initialize game engine due to level / tape version number
1392 =============================================================================
1395 static void InitGameEngine()
1397 int i, j, k, l, x, y;
1399 /* set game engine from tape file when re-playing, else from level file */
1400 game.engine_version = (tape.playing ? tape.engine_version :
1401 level.game_version);
1403 /* ---------------------------------------------------------------------- */
1404 /* set flags for bugs and changes according to active game engine version */
1405 /* ---------------------------------------------------------------------- */
1408 Summary of bugfix/change:
1409 Fixed handling for custom elements that change when pushed by the player.
1411 Fixed/changed in version:
1415 Before 3.1.0, custom elements that "change when pushing" changed directly
1416 after the player started pushing them (until then handled in "DigField()").
1417 Since 3.1.0, these custom elements are not changed until the "pushing"
1418 move of the element is finished (now handled in "ContinueMoving()").
1420 Affected levels/tapes:
1421 The first condition is generally needed for all levels/tapes before version
1422 3.1.0, which might use the old behaviour before it was changed; known tapes
1423 that are affected are some tapes from the level set "Walpurgis Gardens" by
1425 The second condition is an exception from the above case and is needed for
1426 the special case of tapes recorded with game (not engine!) version 3.1.0 or
1427 above (including some development versions of 3.1.0), but before it was
1428 known that this change would break tapes like the above and was fixed in
1429 3.1.1, so that the changed behaviour was active although the engine version
1430 while recording maybe was before 3.1.0. There is at least one tape that is
1431 affected by this exception, which is the tape for the one-level set "Bug
1432 Machine" by Juergen Bonhagen.
1435 game.use_change_when_pushing_bug =
1436 (game.engine_version < VERSION_IDENT(3,1,0,0) &&
1438 tape.game_version >= VERSION_IDENT(3,1,0,0) &&
1439 tape.game_version < VERSION_IDENT(3,1,1,0)));
1442 Summary of bugfix/change:
1443 Fixed handling for blocking the field the player leaves when moving.
1445 Fixed/changed in version:
1449 Before 3.1.1, when "block last field when moving" was enabled, the field
1450 the player is leaving when moving was blocked for the time of the move,
1451 and was directly unblocked afterwards. This resulted in the last field
1452 being blocked for exactly one less than the number of frames of one player
1453 move. Additionally, even when blocking was disabled, the last field was
1454 blocked for exactly one frame.
1455 Since 3.1.1, due to changes in player movement handling, the last field
1456 is not blocked at all when blocking is disabled. When blocking is enabled,
1457 the last field is blocked for exactly the number of frames of one player
1458 move. Additionally, if the player is Murphy, the hero of Supaplex, the
1459 last field is blocked for exactly one more than the number of frames of
1462 Affected levels/tapes:
1463 (!!! yet to be determined -- probably many !!!)
1466 game.use_block_last_field_bug =
1467 (game.engine_version < VERSION_IDENT(3,1,1,0));
1470 Summary of bugfix/change:
1471 Changed behaviour of CE changes with multiple changes per single frame.
1473 Fixed/changed in version:
1477 Before 3.2.0-6, only one single CE change was allowed in each engine frame.
1478 This resulted in race conditions where CEs seem to behave strange in some
1479 situations (where triggered CE changes were just skipped because there was
1480 already a CE change on that tile in the playfield in that engine frame).
1481 Since 3.2.0-6, this was changed to allow up to MAX_NUM_CHANGES_PER_FRAME.
1482 (The number of changes per frame must be limited in any case, because else
1483 it is easily possible to define CE changes that would result in an infinite
1484 loop, causing the whole game to freeze. The MAX_NUM_CHANGES_PER_FRAME value
1485 should be set large enough so that it would only be reached in cases where
1486 the corresponding CE change conditions run into a loop. Therefore, it seems
1487 to be reasonable to set MAX_NUM_CHANGES_PER_FRAME to the same value as the
1488 maximal number of change pages for custom elements.)
1490 Affected levels/tapes:
1494 #if USE_ONLY_ONE_CHANGE_PER_FRAME
1495 game.max_num_changes_per_frame = 1;
1497 game.max_num_changes_per_frame =
1498 (game.engine_version < VERSION_IDENT(3,2,0,6) ? 1 : 32);
1501 /* ---------------------------------------------------------------------- */
1503 /* default scan direction: scan playfield from top/left to bottom/right */
1504 InitPlayfieldScanMode(CA_ARG_SCAN_MODE_NORMAL);
1506 /* dynamically adjust element properties according to game engine version */
1507 InitElementPropertiesEngine(game.engine_version);
1510 printf("level %d: level version == %06d\n", level_nr, level.game_version);
1511 printf(" tape version == %06d [%s] [file: %06d]\n",
1512 tape.engine_version, (tape.playing ? "PLAYING" : "RECORDING"),
1514 printf(" => game.engine_version == %06d\n", game.engine_version);
1518 /* ---------- recursively resolve group elements ------------------------- */
1520 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1521 for (j = 0; j < NUM_GROUP_ELEMENTS; j++)
1522 element_info[i].in_group[j] = FALSE;
1524 for (i = 0; i < NUM_GROUP_ELEMENTS; i++)
1525 resolve_group_element(EL_GROUP_START + i, 0);
1528 /* ---------- initialize player's initial move delay --------------------- */
1531 /* dynamically adjust player properties according to level information */
1532 game.initial_move_delay_value =
1533 get_move_delay_from_stepsize(level.initial_player_stepsize);
1535 /* dynamically adjust player properties according to level information */
1536 game.initial_move_delay_value =
1537 (level.double_speed ? MOVE_DELAY_HIGH_SPEED : MOVE_DELAY_NORMAL_SPEED);
1540 /* dynamically adjust player properties according to game engine version */
1541 game.initial_move_delay = (game.engine_version <= VERSION_IDENT(2,0,1,0) ?
1542 game.initial_move_delay_value : 0);
1544 /* ---------- initialize player's initial push delay --------------------- */
1546 /* dynamically adjust player properties according to game engine version */
1547 game.initial_push_delay_value =
1548 (game.engine_version < VERSION_IDENT(3,0,7,1) ? 5 : -1);
1550 /* ---------- initialize changing elements ------------------------------- */
1552 /* initialize changing elements information */
1553 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1555 struct ElementInfo *ei = &element_info[i];
1557 /* this pointer might have been changed in the level editor */
1558 ei->change = &ei->change_page[0];
1560 if (!IS_CUSTOM_ELEMENT(i))
1562 ei->change->target_element = EL_EMPTY_SPACE;
1563 ei->change->delay_fixed = 0;
1564 ei->change->delay_random = 0;
1565 ei->change->delay_frames = 1;
1568 for (j = 0; j < NUM_CHANGE_EVENTS; j++)
1570 ei->has_change_event[j] = FALSE;
1572 ei->event_page_nr[j] = 0;
1573 ei->event_page[j] = &ei->change_page[0];
1577 /* add changing elements from pre-defined list */
1578 for (i = 0; change_delay_list[i].element != EL_UNDEFINED; i++)
1580 struct ChangingElementInfo *ch_delay = &change_delay_list[i];
1581 struct ElementInfo *ei = &element_info[ch_delay->element];
1583 ei->change->target_element = ch_delay->target_element;
1584 ei->change->delay_fixed = ch_delay->change_delay;
1586 ei->change->pre_change_function = ch_delay->pre_change_function;
1587 ei->change->change_function = ch_delay->change_function;
1588 ei->change->post_change_function = ch_delay->post_change_function;
1590 ei->change->can_change = TRUE;
1591 ei->change->can_change_or_has_action = TRUE;
1593 ei->has_change_event[CE_DELAY] = TRUE;
1595 SET_PROPERTY(ch_delay->element, EP_CAN_CHANGE, TRUE);
1596 SET_PROPERTY(ch_delay->element, EP_CAN_CHANGE_OR_HAS_ACTION, TRUE);
1599 /* ---------- initialize internal run-time variables ------------- */
1601 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
1603 struct ElementInfo *ei = &element_info[EL_CUSTOM_START + i];
1605 for (j = 0; j < ei->num_change_pages; j++)
1607 ei->change_page[j].can_change_or_has_action =
1608 (ei->change_page[j].can_change |
1609 ei->change_page[j].has_action);
1613 /* add change events from custom element configuration */
1614 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
1616 struct ElementInfo *ei = &element_info[EL_CUSTOM_START + i];
1618 for (j = 0; j < ei->num_change_pages; j++)
1620 if (!ei->change_page[j].can_change_or_has_action)
1623 for (k = 0; k < NUM_CHANGE_EVENTS; k++)
1625 /* only add event page for the first page found with this event */
1626 if (ei->change_page[j].has_event[k] && !(ei->has_change_event[k]))
1628 ei->has_change_event[k] = TRUE;
1630 ei->event_page_nr[k] = j;
1631 ei->event_page[k] = &ei->change_page[j];
1637 /* ---------- initialize run-time trigger player and element ------------- */
1639 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
1641 struct ElementInfo *ei = &element_info[EL_CUSTOM_START + i];
1643 for (j = 0; j < ei->num_change_pages; j++)
1645 ei->change_page[j].actual_trigger_element = EL_EMPTY;
1646 ei->change_page[j].actual_trigger_player = EL_PLAYER_1;
1647 ei->change_page[j].actual_trigger_side = CH_SIDE_NONE;
1648 ei->change_page[j].actual_trigger_ce_value = 0;
1652 /* ---------- initialize trigger events ---------------------------------- */
1654 /* initialize trigger events information */
1655 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1656 for (j = 0; j < NUM_CHANGE_EVENTS; j++)
1657 trigger_events[i][j] = FALSE;
1659 /* add trigger events from element change event properties */
1660 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1662 struct ElementInfo *ei = &element_info[i];
1664 for (j = 0; j < ei->num_change_pages; j++)
1666 if (!ei->change_page[j].can_change_or_has_action)
1669 if (ei->change_page[j].has_event[CE_BY_OTHER_ACTION])
1671 int trigger_element = ei->change_page[j].trigger_element;
1673 for (k = 0; k < NUM_CHANGE_EVENTS; k++)
1675 if (ei->change_page[j].has_event[k])
1677 if (IS_GROUP_ELEMENT(trigger_element))
1679 struct ElementGroupInfo *group =
1680 element_info[trigger_element].group;
1682 for (l = 0; l < group->num_elements_resolved; l++)
1683 trigger_events[group->element_resolved[l]][k] = TRUE;
1686 trigger_events[trigger_element][k] = TRUE;
1693 /* ---------- initialize push delay -------------------------------------- */
1695 /* initialize push delay values to default */
1696 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1698 if (!IS_CUSTOM_ELEMENT(i))
1700 element_info[i].push_delay_fixed = game.default_push_delay_fixed;
1701 element_info[i].push_delay_random = game.default_push_delay_random;
1705 /* set push delay value for certain elements from pre-defined list */
1706 for (i = 0; push_delay_list[i].element != EL_UNDEFINED; i++)
1708 int e = push_delay_list[i].element;
1710 element_info[e].push_delay_fixed = push_delay_list[i].push_delay_fixed;
1711 element_info[e].push_delay_random = push_delay_list[i].push_delay_random;
1714 /* set push delay value for Supaplex elements for newer engine versions */
1715 if (game.engine_version >= VERSION_IDENT(3,1,0,0))
1717 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1719 if (IS_SP_ELEMENT(i))
1721 /* set SP push delay to just enough to push under a falling zonk */
1722 int delay = (game.engine_version >= VERSION_IDENT(3,1,1,0) ? 8 : 6);
1724 element_info[i].push_delay_fixed = delay;
1725 element_info[i].push_delay_random = 0;
1730 /* ---------- initialize move stepsize ----------------------------------- */
1732 /* initialize move stepsize values to default */
1733 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1734 if (!IS_CUSTOM_ELEMENT(i))
1735 element_info[i].move_stepsize = MOVE_STEPSIZE_NORMAL;
1737 /* set move stepsize value for certain elements from pre-defined list */
1738 for (i = 0; move_stepsize_list[i].element != EL_UNDEFINED; i++)
1740 int e = move_stepsize_list[i].element;
1742 element_info[e].move_stepsize = move_stepsize_list[i].move_stepsize;
1745 /* ---------- initialize collect score ----------------------------------- */
1747 /* initialize collect score values for custom elements from initial value */
1748 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1749 if (IS_CUSTOM_ELEMENT(i))
1750 element_info[i].collect_score = element_info[i].collect_score_initial;
1752 /* ---------- initialize collect count ----------------------------------- */
1754 /* initialize collect count values for non-custom elements */
1755 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1756 if (!IS_CUSTOM_ELEMENT(i))
1757 element_info[i].collect_count_initial = 0;
1759 /* add collect count values for all elements from pre-defined list */
1760 for (i = 0; collect_count_list[i].element != EL_UNDEFINED; i++)
1761 element_info[collect_count_list[i].element].collect_count_initial =
1762 collect_count_list[i].count;
1764 /* ---------- initialize access direction -------------------------------- */
1766 /* initialize access direction values to default (access from every side) */
1767 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1768 if (!IS_CUSTOM_ELEMENT(i))
1769 element_info[i].access_direction = MV_ALL_DIRECTIONS;
1771 /* set access direction value for certain elements from pre-defined list */
1772 for (i = 0; access_direction_list[i].element != EL_UNDEFINED; i++)
1773 element_info[access_direction_list[i].element].access_direction =
1774 access_direction_list[i].direction;
1776 /* ---------- initialize explosion content ------------------------------- */
1777 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
1779 if (IS_CUSTOM_ELEMENT(i))
1782 for (y = 0; y < 3; y++) for (x = 0; x < 3; x++)
1784 /* (content for EL_YAMYAM set at run-time with game.yamyam_content_nr) */
1786 element_info[i].content.e[x][y] =
1787 (i == EL_PLAYER_1 ? EL_EMERALD_YELLOW :
1788 i == EL_PLAYER_2 ? EL_EMERALD_RED :
1789 i == EL_PLAYER_3 ? EL_EMERALD :
1790 i == EL_PLAYER_4 ? EL_EMERALD_PURPLE :
1791 i == EL_MOLE ? EL_EMERALD_RED :
1792 i == EL_PENGUIN ? EL_EMERALD_PURPLE :
1793 i == EL_BUG ? (x == 1 && y == 1 ? EL_DIAMOND : EL_EMERALD) :
1794 i == EL_BD_BUTTERFLY ? EL_BD_DIAMOND :
1795 i == EL_SP_ELECTRON ? EL_SP_INFOTRON :
1796 i == EL_AMOEBA_TO_DIAMOND ? level.amoeba_content :
1797 i == EL_WALL_EMERALD ? EL_EMERALD :
1798 i == EL_WALL_DIAMOND ? EL_DIAMOND :
1799 i == EL_WALL_BD_DIAMOND ? EL_BD_DIAMOND :
1800 i == EL_WALL_EMERALD_YELLOW ? EL_EMERALD_YELLOW :
1801 i == EL_WALL_EMERALD_RED ? EL_EMERALD_RED :
1802 i == EL_WALL_EMERALD_PURPLE ? EL_EMERALD_PURPLE :
1803 i == EL_WALL_PEARL ? EL_PEARL :
1804 i == EL_WALL_CRYSTAL ? EL_CRYSTAL :
1810 int get_num_special_action(int element, int action_first, int action_last)
1812 int num_special_action = 0;
1815 for (i = action_first; i <= action_last; i++)
1817 boolean found = FALSE;
1819 for (j = 0; j < NUM_DIRECTIONS; j++)
1820 if (el_act_dir2img(element, i, j) !=
1821 el_act_dir2img(element, ACTION_DEFAULT, j))
1825 num_special_action++;
1831 printf("::: %d->%d: %d\n", action_first, action_last, num_special_action);
1834 return num_special_action;
1838 =============================================================================
1840 -----------------------------------------------------------------------------
1841 initialize and start new game
1842 =============================================================================
1847 boolean emulate_bd = TRUE; /* unless non-BOULDERDASH elements found */
1848 boolean emulate_sb = TRUE; /* unless non-SOKOBAN elements found */
1849 boolean emulate_sp = TRUE; /* unless non-SUPAPLEX elements found */
1854 /* don't play tapes over network */
1855 network_playing = (options.network && !tape.playing);
1857 for (i = 0; i < MAX_PLAYERS; i++)
1859 struct PlayerInfo *player = &stored_player[i];
1861 player->index_nr = i;
1862 player->index_bit = (1 << i);
1863 player->element_nr = EL_PLAYER_1 + i;
1865 player->present = FALSE;
1866 player->active = FALSE;
1869 player->effective_action = 0;
1870 player->programmed_action = 0;
1873 player->gems_still_needed = level.gems_needed;
1874 player->sokobanfields_still_needed = 0;
1875 player->lights_still_needed = 0;
1876 player->friends_still_needed = 0;
1878 for (j = 0; j < MAX_NUM_KEYS; j++)
1879 player->key[j] = FALSE;
1881 player->dynabomb_count = 0;
1882 player->dynabomb_size = 1;
1883 player->dynabombs_left = 0;
1884 player->dynabomb_xl = FALSE;
1886 player->MovDir = MV_NONE;
1889 player->GfxDir = MV_NONE;
1890 player->GfxAction = ACTION_DEFAULT;
1892 player->StepFrame = 0;
1894 player->use_murphy = FALSE;
1895 player->artwork_element =
1896 (level.use_artwork_element[i] ? level.artwork_element[i] :
1897 player->element_nr);
1899 player->block_last_field = FALSE; /* initialized in InitPlayerField() */
1900 player->block_delay_adjustment = 0; /* initialized in InitPlayerField() */
1902 player->can_fall_into_acid = CAN_MOVE_INTO_ACID(player->element_nr);
1904 player->actual_frame_counter = 0;
1906 player->step_counter = 0;
1908 player->last_move_dir = MV_NONE;
1910 player->is_waiting = FALSE;
1911 player->is_moving = FALSE;
1912 player->is_auto_moving = FALSE;
1913 player->is_digging = FALSE;
1914 player->is_snapping = FALSE;
1915 player->is_collecting = FALSE;
1916 player->is_pushing = FALSE;
1917 player->is_switching = FALSE;
1918 player->is_dropping = FALSE;
1919 player->is_dropping_pressed = FALSE;
1921 player->is_bored = FALSE;
1922 player->is_sleeping = FALSE;
1924 player->frame_counter_bored = -1;
1925 player->frame_counter_sleeping = -1;
1927 player->anim_delay_counter = 0;
1928 player->post_delay_counter = 0;
1930 player->dir_waiting = MV_NONE;
1931 player->action_waiting = ACTION_DEFAULT;
1932 player->last_action_waiting = ACTION_DEFAULT;
1933 player->special_action_bored = ACTION_DEFAULT;
1934 player->special_action_sleeping = ACTION_DEFAULT;
1937 /* cannot be set here -- could be modified in Init[Player]Field() below */
1939 /* set number of special actions for bored and sleeping animation */
1940 player->num_special_action_bored =
1941 get_num_special_action(player->artwork_element,
1942 ACTION_BORING_1, ACTION_BORING_LAST);
1943 player->num_special_action_sleeping =
1944 get_num_special_action(player->artwork_element,
1945 ACTION_SLEEPING_1, ACTION_SLEEPING_LAST);
1948 player->switch_x = -1;
1949 player->switch_y = -1;
1951 player->drop_x = -1;
1952 player->drop_y = -1;
1954 player->show_envelope = 0;
1957 SetPlayerMoveSpeed(player, level.initial_player_stepsize, TRUE);
1959 player->move_delay = game.initial_move_delay;
1960 player->move_delay_value = game.initial_move_delay_value;
1962 player->move_delay_value_next = -1;
1964 player->move_delay_reset_counter = 0;
1966 player->cannot_move = FALSE;
1969 player->push_delay = -1; /* initialized when pushing starts */
1970 player->push_delay_value = game.initial_push_delay_value;
1972 player->drop_delay = 0;
1973 player->drop_pressed_delay = 0;
1975 player->last_jx = player->last_jy = 0;
1976 player->jx = player->jy = 0;
1978 player->shield_normal_time_left = 0;
1979 player->shield_deadly_time_left = 0;
1981 player->inventory_infinite_element = EL_UNDEFINED;
1982 player->inventory_size = 0;
1984 DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
1985 SnapField(player, 0, 0);
1987 player->LevelSolved = FALSE;
1988 player->GameOver = FALSE;
1991 network_player_action_received = FALSE;
1993 #if defined(NETWORK_AVALIABLE)
1994 /* initial null action */
1995 if (network_playing)
1996 SendToServer_MovePlayer(MV_NONE);
2005 TimeLeft = level.time;
2008 ScreenMovDir = MV_NONE;
2012 ScrollStepSize = 0; /* will be correctly initialized by ScrollScreen() */
2014 AllPlayersGone = FALSE;
2016 game.yamyam_content_nr = 0;
2017 game.magic_wall_active = FALSE;
2018 game.magic_wall_time_left = 0;
2019 game.light_time_left = 0;
2020 game.timegate_time_left = 0;
2021 game.switchgate_pos = 0;
2022 game.wind_direction = level.wind_direction_initial;
2023 game.gravity = level.initial_gravity;
2024 game.explosions_delayed = TRUE;
2026 game.lenses_time_left = 0;
2027 game.magnify_time_left = 0;
2029 game.ball_state = level.ball_state_initial;
2030 game.ball_content_nr = 0;
2032 game.envelope_active = FALSE;
2034 /* set focus to local player for network games, else to all players */
2035 game.centered_player_nr = (network_playing ? local_player->index_nr : -1);
2036 game.centered_player_nr_next = game.centered_player_nr;
2037 game.set_centered_player = FALSE;
2039 if (network_playing && tape.recording)
2041 /* store client dependent player focus when recording network games */
2042 tape.centered_player_nr_next = game.centered_player_nr_next;
2043 tape.set_centered_player = TRUE;
2047 printf("::: focus set to player %d [%d]\n",
2048 game.centered_player_nr, local_player->index_nr);
2051 for (i = 0; i < NUM_BELTS; i++)
2053 game.belt_dir[i] = MV_NONE;
2054 game.belt_dir_nr[i] = 3; /* not moving, next moving left */
2057 for (i = 0; i < MAX_NUM_AMOEBA; i++)
2058 AmoebaCnt[i] = AmoebaCnt2[i] = 0;
2061 SCAN_PLAYFIELD(x, y)
2063 for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
2066 Feld[x][y] = level.field[x][y];
2067 MovPos[x][y] = MovDir[x][y] = MovDelay[x][y] = 0;
2068 ChangeDelay[x][y] = 0;
2069 ChangePage[x][y] = -1;
2070 #if USE_NEW_CUSTOM_VALUE
2071 CustomValue[x][y] = 0; /* initialized in InitField() */
2073 Store[x][y] = Store2[x][y] = StorePlayer[x][y] = Back[x][y] = 0;
2075 WasJustMoving[x][y] = 0;
2076 WasJustFalling[x][y] = 0;
2077 CheckCollision[x][y] = 0;
2079 Pushed[x][y] = FALSE;
2081 ChangeCount[x][y] = 0;
2082 ChangeEvent[x][y] = -1;
2084 ExplodePhase[x][y] = 0;
2085 ExplodeDelay[x][y] = 0;
2086 ExplodeField[x][y] = EX_TYPE_NONE;
2088 RunnerVisit[x][y] = 0;
2089 PlayerVisit[x][y] = 0;
2092 GfxRandom[x][y] = INIT_GFX_RANDOM();
2093 GfxElement[x][y] = EL_UNDEFINED;
2094 GfxAction[x][y] = ACTION_DEFAULT;
2095 GfxDir[x][y] = MV_NONE;
2099 SCAN_PLAYFIELD(x, y)
2101 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
2104 if (emulate_bd && !IS_BD_ELEMENT(Feld[x][y]))
2106 if (emulate_sb && !IS_SB_ELEMENT(Feld[x][y]))
2108 if (emulate_sp && !IS_SP_ELEMENT(Feld[x][y]))
2111 InitField(x, y, TRUE);
2116 for (i = 0; i < MAX_PLAYERS; i++)
2118 struct PlayerInfo *player = &stored_player[i];
2121 /* set number of special actions for bored and sleeping animation */
2122 player->num_special_action_bored =
2123 get_num_special_action(player->artwork_element,
2124 ACTION_BORING_1, ACTION_BORING_LAST);
2125 player->num_special_action_sleeping =
2126 get_num_special_action(player->artwork_element,
2127 ACTION_SLEEPING_1, ACTION_SLEEPING_LAST);
2132 game.emulation = (emulate_bd ? EMU_BOULDERDASH :
2133 emulate_sb ? EMU_SOKOBAN :
2134 emulate_sp ? EMU_SUPAPLEX : EMU_NONE);
2136 #if USE_NEW_ALL_SLIPPERY
2137 /* initialize type of slippery elements */
2138 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
2140 if (!IS_CUSTOM_ELEMENT(i))
2142 /* default: elements slip down either to the left or right randomly */
2143 element_info[i].slippery_type = SLIPPERY_ANY_RANDOM;
2145 /* SP style elements prefer to slip down on the left side */
2146 if (game.engine_version >= VERSION_IDENT(3,1,1,0) && IS_SP_ELEMENT(i))
2147 element_info[i].slippery_type = SLIPPERY_ANY_LEFT_RIGHT;
2149 /* BD style elements prefer to slip down on the left side */
2150 if (game.emulation == EMU_BOULDERDASH)
2151 element_info[i].slippery_type = SLIPPERY_ANY_LEFT_RIGHT;
2156 /* initialize explosion and ignition delay */
2157 for (i = 0; i < MAX_NUM_ELEMENTS; i++)
2159 if (!IS_CUSTOM_ELEMENT(i))
2162 int delay = (((IS_SP_ELEMENT(i) && i != EL_EMPTY_SPACE) &&
2163 game.engine_version >= VERSION_IDENT(3,1,0,0)) ||
2164 game.emulation == EMU_SUPAPLEX ? 3 : 2);
2165 int last_phase = (num_phase + 1) * delay;
2166 int half_phase = (num_phase / 2) * delay;
2168 element_info[i].explosion_delay = last_phase - 1;
2169 element_info[i].ignition_delay = half_phase;
2171 if (i == EL_BLACK_ORB)
2172 element_info[i].ignition_delay = 1;
2176 if (element_info[i].explosion_delay < 1) /* !!! check again !!! */
2177 element_info[i].explosion_delay = 1;
2179 if (element_info[i].ignition_delay < 1) /* !!! check again !!! */
2180 element_info[i].ignition_delay = 1;
2184 /* correct non-moving belts to start moving left */
2185 for (i = 0; i < NUM_BELTS; i++)
2186 if (game.belt_dir[i] == MV_NONE)
2187 game.belt_dir_nr[i] = 3; /* not moving, next moving left */
2189 /* check if any connected player was not found in playfield */
2190 for (i = 0; i < MAX_PLAYERS; i++)
2192 struct PlayerInfo *player = &stored_player[i];
2194 if (player->connected && !player->present)
2196 for (j = 0; j < MAX_PLAYERS; j++)
2198 struct PlayerInfo *some_player = &stored_player[j];
2199 int jx = some_player->jx, jy = some_player->jy;
2201 /* assign first free player found that is present in the playfield */
2202 if (some_player->present && !some_player->connected)
2204 player->present = TRUE;
2205 player->active = TRUE;
2207 some_player->present = FALSE;
2208 some_player->active = FALSE;
2211 player->element_nr = some_player->element_nr;
2214 player->artwork_element = some_player->artwork_element;
2216 player->block_last_field = some_player->block_last_field;
2217 player->block_delay_adjustment = some_player->block_delay_adjustment;
2219 StorePlayer[jx][jy] = player->element_nr;
2220 player->jx = player->last_jx = jx;
2221 player->jy = player->last_jy = jy;
2231 /* when playing a tape, eliminate all players who do not participate */
2233 for (i = 0; i < MAX_PLAYERS; i++)
2235 if (stored_player[i].active && !tape.player_participates[i])
2237 struct PlayerInfo *player = &stored_player[i];
2238 int jx = player->jx, jy = player->jy;
2240 player->active = FALSE;
2241 StorePlayer[jx][jy] = 0;
2242 Feld[jx][jy] = EL_EMPTY;
2246 else if (!options.network && !setup.team_mode) /* && !tape.playing */
2248 /* when in single player mode, eliminate all but the first active player */
2250 for (i = 0; i < MAX_PLAYERS; i++)
2252 if (stored_player[i].active)
2254 for (j = i + 1; j < MAX_PLAYERS; j++)
2256 if (stored_player[j].active)
2258 struct PlayerInfo *player = &stored_player[j];
2259 int jx = player->jx, jy = player->jy;
2261 player->active = FALSE;
2262 player->present = FALSE;
2264 StorePlayer[jx][jy] = 0;
2265 Feld[jx][jy] = EL_EMPTY;
2272 /* when recording the game, store which players take part in the game */
2275 for (i = 0; i < MAX_PLAYERS; i++)
2276 if (stored_player[i].active)
2277 tape.player_participates[i] = TRUE;
2282 for (i = 0; i < MAX_PLAYERS; i++)
2284 struct PlayerInfo *player = &stored_player[i];
2286 printf("Player %d: present == %d, connected == %d, active == %d.\n",
2291 if (local_player == player)
2292 printf("Player %d is local player.\n", i+1);
2296 if (BorderElement == EL_EMPTY)
2299 SBX_Right = lev_fieldx - SCR_FIELDX;
2301 SBY_Lower = lev_fieldy - SCR_FIELDY;
2306 SBX_Right = lev_fieldx - SCR_FIELDX + 1;
2308 SBY_Lower = lev_fieldy - SCR_FIELDY + 1;
2311 if (lev_fieldx + (SBX_Left == -1 ? 2 : 0) <= SCR_FIELDX)
2312 SBX_Left = SBX_Right = -1 * (SCR_FIELDX - lev_fieldx) / 2;
2314 if (lev_fieldy + (SBY_Upper == -1 ? 2 : 0) <= SCR_FIELDY)
2315 SBY_Upper = SBY_Lower = -1 * (SCR_FIELDY - lev_fieldy) / 2;
2317 /* if local player not found, look for custom element that might create
2318 the player (make some assumptions about the right custom element) */
2319 if (!local_player->present)
2321 int start_x = 0, start_y = 0;
2322 int found_rating = 0;
2323 int found_element = EL_UNDEFINED;
2324 int player_nr = local_player->index_nr;
2327 SCAN_PLAYFIELD(x, y)
2329 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
2332 int element = Feld[x][y];
2337 if (level.use_start_element[player_nr] &&
2338 level.start_element[player_nr] == element &&
2345 found_element = element;
2348 if (!IS_CUSTOM_ELEMENT(element))
2351 if (CAN_CHANGE(element))
2353 for (i = 0; i < element_info[element].num_change_pages; i++)
2355 /* check for player created from custom element as single target */
2356 content = element_info[element].change_page[i].target_element;
2357 is_player = ELEM_IS_PLAYER(content);
2359 if (is_player && (found_rating < 3 || element < found_element))
2365 found_element = element;
2370 for (yy = 0; yy < 3; yy++) for (xx = 0; xx < 3; xx++)
2372 /* check for player created from custom element as explosion content */
2373 content = element_info[element].content.e[xx][yy];
2374 is_player = ELEM_IS_PLAYER(content);
2376 if (is_player && (found_rating < 2 || element < found_element))
2378 start_x = x + xx - 1;
2379 start_y = y + yy - 1;
2382 found_element = element;
2385 if (!CAN_CHANGE(element))
2388 for (i = 0; i < element_info[element].num_change_pages; i++)
2390 /* check for player created from custom element as extended target */
2392 element_info[element].change_page[i].target_content.e[xx][yy];
2394 is_player = ELEM_IS_PLAYER(content);
2396 if (is_player && (found_rating < 1 || element < found_element))
2398 start_x = x + xx - 1;
2399 start_y = y + yy - 1;
2402 found_element = element;
2408 scroll_x = (start_x < SBX_Left + MIDPOSX ? SBX_Left :
2409 start_x > SBX_Right + MIDPOSX ? SBX_Right :
2412 scroll_y = (start_y < SBY_Upper + MIDPOSY ? SBY_Upper :
2413 start_y > SBY_Lower + MIDPOSY ? SBY_Lower :
2418 scroll_x = (local_player->jx < SBX_Left + MIDPOSX ? SBX_Left :
2419 local_player->jx > SBX_Right + MIDPOSX ? SBX_Right :
2420 local_player->jx - MIDPOSX);
2422 scroll_y = (local_player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
2423 local_player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
2424 local_player->jy - MIDPOSY);
2427 if (!game.restart_level)
2428 CloseDoor(DOOR_CLOSE_1);
2430 /* !!! FIX THIS (START) !!! */
2431 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
2433 InitGameEngine_EM();
2440 /* after drawing the level, correct some elements */
2441 if (game.timegate_time_left == 0)
2442 CloseAllOpenTimegates();
2444 if (setup.soft_scrolling)
2445 BlitBitmap(fieldbuffer, backbuffer, FX, FY, SXSIZE, SYSIZE, SX, SY);
2447 redraw_mask |= REDRAW_FROM_BACKBUFFER;
2450 /* !!! FIX THIS (END) !!! */
2452 if (!game.restart_level)
2454 /* copy default game door content to main double buffer */
2455 BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
2456 DOOR_GFX_PAGEX5, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
2459 DrawGameDoorValues();
2461 if (!game.restart_level)
2465 game_gadget[SOUND_CTRL_ID_MUSIC]->checked = setup.sound_music;
2466 game_gadget[SOUND_CTRL_ID_LOOPS]->checked = setup.sound_loops;
2467 game_gadget[SOUND_CTRL_ID_SIMPLE]->checked = setup.sound_simple;
2471 /* copy actual game door content to door double buffer for OpenDoor() */
2472 BlitBitmap(drawto, bitmap_db_door,
2473 DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
2475 OpenDoor(DOOR_OPEN_ALL);
2477 PlaySoundStereo(SND_GAME_STARTING, SOUND_MIDDLE);
2479 if (setup.sound_music)
2482 KeyboardAutoRepeatOffUnlessAutoplay();
2486 for (i = 0; i < MAX_PLAYERS; i++)
2487 printf("Player %d %sactive.\n",
2488 i + 1, (stored_player[i].active ? "" : "not "));
2492 game.restart_level = FALSE;
2495 void UpdateEngineValues(int actual_scroll_x, int actual_scroll_y)
2497 /* this is used for non-R'n'D game engines to update certain engine values */
2499 /* needed to determine if sounds are played within the visible screen area */
2500 scroll_x = actual_scroll_x;
2501 scroll_y = actual_scroll_y;
2504 void InitMovDir(int x, int y)
2506 int i, element = Feld[x][y];
2507 static int xy[4][2] =
2514 static int direction[3][4] =
2516 { MV_RIGHT, MV_UP, MV_LEFT, MV_DOWN },
2517 { MV_LEFT, MV_DOWN, MV_RIGHT, MV_UP },
2518 { MV_LEFT, MV_RIGHT, MV_UP, MV_DOWN }
2527 Feld[x][y] = EL_BUG;
2528 MovDir[x][y] = direction[0][element - EL_BUG_RIGHT];
2531 case EL_SPACESHIP_RIGHT:
2532 case EL_SPACESHIP_UP:
2533 case EL_SPACESHIP_LEFT:
2534 case EL_SPACESHIP_DOWN:
2535 Feld[x][y] = EL_SPACESHIP;
2536 MovDir[x][y] = direction[0][element - EL_SPACESHIP_RIGHT];
2539 case EL_BD_BUTTERFLY_RIGHT:
2540 case EL_BD_BUTTERFLY_UP:
2541 case EL_BD_BUTTERFLY_LEFT:
2542 case EL_BD_BUTTERFLY_DOWN:
2543 Feld[x][y] = EL_BD_BUTTERFLY;
2544 MovDir[x][y] = direction[0][element - EL_BD_BUTTERFLY_RIGHT];
2547 case EL_BD_FIREFLY_RIGHT:
2548 case EL_BD_FIREFLY_UP:
2549 case EL_BD_FIREFLY_LEFT:
2550 case EL_BD_FIREFLY_DOWN:
2551 Feld[x][y] = EL_BD_FIREFLY;
2552 MovDir[x][y] = direction[0][element - EL_BD_FIREFLY_RIGHT];
2555 case EL_PACMAN_RIGHT:
2557 case EL_PACMAN_LEFT:
2558 case EL_PACMAN_DOWN:
2559 Feld[x][y] = EL_PACMAN;
2560 MovDir[x][y] = direction[0][element - EL_PACMAN_RIGHT];
2563 case EL_YAMYAM_LEFT:
2564 case EL_YAMYAM_RIGHT:
2566 case EL_YAMYAM_DOWN:
2567 Feld[x][y] = EL_YAMYAM;
2568 MovDir[x][y] = direction[2][element - EL_YAMYAM_LEFT];
2571 case EL_SP_SNIKSNAK:
2572 MovDir[x][y] = MV_UP;
2575 case EL_SP_ELECTRON:
2576 MovDir[x][y] = MV_LEFT;
2583 Feld[x][y] = EL_MOLE;
2584 MovDir[x][y] = direction[2][element - EL_MOLE_LEFT];
2588 if (IS_CUSTOM_ELEMENT(element))
2590 struct ElementInfo *ei = &element_info[element];
2591 int move_direction_initial = ei->move_direction_initial;
2592 int move_pattern = ei->move_pattern;
2594 if (move_direction_initial == MV_START_PREVIOUS)
2596 if (MovDir[x][y] != MV_NONE)
2599 move_direction_initial = MV_START_AUTOMATIC;
2602 if (move_direction_initial == MV_START_RANDOM)
2603 MovDir[x][y] = 1 << RND(4);
2604 else if (move_direction_initial & MV_ANY_DIRECTION)
2605 MovDir[x][y] = move_direction_initial;
2606 else if (move_pattern == MV_ALL_DIRECTIONS ||
2607 move_pattern == MV_TURNING_LEFT ||
2608 move_pattern == MV_TURNING_RIGHT ||
2609 move_pattern == MV_TURNING_LEFT_RIGHT ||
2610 move_pattern == MV_TURNING_RIGHT_LEFT ||
2611 move_pattern == MV_TURNING_RANDOM)
2612 MovDir[x][y] = 1 << RND(4);
2613 else if (move_pattern == MV_HORIZONTAL)
2614 MovDir[x][y] = (RND(2) ? MV_LEFT : MV_RIGHT);
2615 else if (move_pattern == MV_VERTICAL)
2616 MovDir[x][y] = (RND(2) ? MV_UP : MV_DOWN);
2617 else if (move_pattern & MV_ANY_DIRECTION)
2618 MovDir[x][y] = element_info[element].move_pattern;
2619 else if (move_pattern == MV_ALONG_LEFT_SIDE ||
2620 move_pattern == MV_ALONG_RIGHT_SIDE)
2622 /* use random direction as default start direction */
2623 if (game.engine_version >= VERSION_IDENT(3,1,0,0))
2624 MovDir[x][y] = 1 << RND(4);
2626 for (i = 0; i < NUM_DIRECTIONS; i++)
2628 int x1 = x + xy[i][0];
2629 int y1 = y + xy[i][1];
2631 if (!IN_LEV_FIELD(x1, y1) || !IS_FREE(x1, y1))
2633 if (move_pattern == MV_ALONG_RIGHT_SIDE)
2634 MovDir[x][y] = direction[0][i];
2636 MovDir[x][y] = direction[1][i];
2645 MovDir[x][y] = 1 << RND(4);
2647 if (element != EL_BUG &&
2648 element != EL_SPACESHIP &&
2649 element != EL_BD_BUTTERFLY &&
2650 element != EL_BD_FIREFLY)
2653 for (i = 0; i < NUM_DIRECTIONS; i++)
2655 int x1 = x + xy[i][0];
2656 int y1 = y + xy[i][1];
2658 if (!IN_LEV_FIELD(x1, y1) || !IS_FREE(x1, y1))
2660 if (element == EL_BUG || element == EL_BD_BUTTERFLY)
2662 MovDir[x][y] = direction[0][i];
2665 else if (element == EL_SPACESHIP || element == EL_BD_FIREFLY ||
2666 element == EL_SP_SNIKSNAK || element == EL_SP_ELECTRON)
2668 MovDir[x][y] = direction[1][i];
2677 GfxDir[x][y] = MovDir[x][y];
2680 void InitAmoebaNr(int x, int y)
2683 int group_nr = AmoebeNachbarNr(x, y);
2687 for (i = 1; i < MAX_NUM_AMOEBA; i++)
2689 if (AmoebaCnt[i] == 0)
2697 AmoebaNr[x][y] = group_nr;
2698 AmoebaCnt[group_nr]++;
2699 AmoebaCnt2[group_nr]++;
2705 boolean raise_level = FALSE;
2707 if (local_player->MovPos)
2710 if (tape.auto_play) /* tape might already be stopped here */
2711 tape.auto_play_level_solved = TRUE;
2713 local_player->LevelSolved = FALSE;
2715 PlaySoundStereo(SND_GAME_WINNING, SOUND_MIDDLE);
2719 if (!tape.playing && setup.sound_loops)
2720 PlaySoundExt(SND_GAME_LEVELTIME_BONUS, SOUND_MAX_VOLUME, SOUND_MIDDLE,
2721 SND_CTRL_PLAY_LOOP);
2723 while (TimeLeft > 0)
2725 if (!tape.playing && !setup.sound_loops)
2726 PlaySoundStereo(SND_GAME_LEVELTIME_BONUS, SOUND_MIDDLE);
2728 if (TimeLeft > 100 && TimeLeft % 10 == 0)
2731 RaiseScore(level.score[SC_TIME_BONUS] * 10);
2736 RaiseScore(level.score[SC_TIME_BONUS]);
2739 DrawGameValue_Time(TimeLeft);
2747 if (!tape.playing && setup.sound_loops)
2748 StopSound(SND_GAME_LEVELTIME_BONUS);
2750 else if (level.time == 0) /* level without time limit */
2752 if (!tape.playing && setup.sound_loops)
2753 PlaySoundExt(SND_GAME_LEVELTIME_BONUS, SOUND_MAX_VOLUME, SOUND_MIDDLE,
2754 SND_CTRL_PLAY_LOOP);
2756 while (TimePlayed < 999)
2758 if (!tape.playing && !setup.sound_loops)
2759 PlaySoundStereo(SND_GAME_LEVELTIME_BONUS, SOUND_MIDDLE);
2761 if (TimePlayed < 900 && TimePlayed % 10 == 0)
2764 RaiseScore(level.score[SC_TIME_BONUS] * 10);
2769 RaiseScore(level.score[SC_TIME_BONUS]);
2772 DrawGameValue_Time(TimePlayed);
2780 if (!tape.playing && setup.sound_loops)
2781 StopSound(SND_GAME_LEVELTIME_BONUS);
2784 /* close exit door after last player */
2785 if (AllPlayersGone && ExitX >= 0 && ExitY >= 0 &&
2786 (Feld[ExitX][ExitY] == EL_EXIT_OPEN ||
2787 Feld[ExitX][ExitY] == EL_SP_EXIT_OPEN))
2789 int element = Feld[ExitX][ExitY];
2791 Feld[ExitX][ExitY] = (element == EL_EXIT_OPEN ? EL_EXIT_CLOSING :
2792 EL_SP_EXIT_CLOSING);
2794 PlayLevelSoundElementAction(ExitX, ExitY, element, ACTION_CLOSING);
2797 /* player disappears */
2798 if (ExitX >= 0 && ExitY >= 0)
2799 DrawLevelField(ExitX, ExitY);
2805 printf("::: TAPE PLAYING -> DO NOT SAVE SCORE\n");
2807 printf("::: NO TAPE PLAYING -> SAVING SCORE\n");
2813 CloseDoor(DOOR_CLOSE_1);
2818 SaveTape(tape.level_nr); /* Ask to save tape */
2821 if (level_nr == leveldir_current->handicap_level)
2823 leveldir_current->handicap_level++;
2824 SaveLevelSetup_SeriesInfo();
2827 if (level_editor_test_game)
2828 local_player->score = -1; /* no highscore when playing from editor */
2829 else if (level_nr < leveldir_current->last_level)
2830 raise_level = TRUE; /* advance to next level */
2832 if ((hi_pos = NewHiScore()) >= 0)
2834 game_status = GAME_MODE_SCORES;
2835 DrawHallOfFame(hi_pos);
2844 game_status = GAME_MODE_MAIN;
2861 LoadScore(level_nr);
2863 if (strcmp(setup.player_name, EMPTY_PLAYER_NAME) == 0 ||
2864 local_player->score < highscore[MAX_SCORE_ENTRIES - 1].Score)
2867 for (k = 0; k < MAX_SCORE_ENTRIES; k++)
2869 if (local_player->score > highscore[k].Score)
2871 /* player has made it to the hall of fame */
2873 if (k < MAX_SCORE_ENTRIES - 1)
2875 int m = MAX_SCORE_ENTRIES - 1;
2878 for (l = k; l < MAX_SCORE_ENTRIES; l++)
2879 if (!strcmp(setup.player_name, highscore[l].Name))
2881 if (m == k) /* player's new highscore overwrites his old one */
2885 for (l = m; l > k; l--)
2887 strcpy(highscore[l].Name, highscore[l - 1].Name);
2888 highscore[l].Score = highscore[l - 1].Score;
2895 strncpy(highscore[k].Name, setup.player_name, MAX_PLAYER_NAME_LEN);
2896 highscore[k].Name[MAX_PLAYER_NAME_LEN] = '\0';
2897 highscore[k].Score = local_player->score;
2903 else if (!strncmp(setup.player_name, highscore[k].Name,
2904 MAX_PLAYER_NAME_LEN))
2905 break; /* player already there with a higher score */
2911 SaveScore(level_nr);
2916 inline static int getElementMoveStepsize(int x, int y)
2918 int element = Feld[x][y];
2919 int direction = MovDir[x][y];
2920 int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
2921 int dy = (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
2922 int horiz_move = (dx != 0);
2923 int sign = (horiz_move ? dx : dy);
2924 int step = sign * element_info[element].move_stepsize;
2926 /* special values for move stepsize for spring and things on conveyor belt */
2930 if (element == EL_SPRING)
2931 step = sign * MOVE_STEPSIZE_NORMAL * 2;
2932 else if (CAN_FALL(element) && !CAN_MOVE(element) &&
2933 y < lev_fieldy - 1 && IS_BELT_ACTIVE(Feld[x][y + 1]))
2934 step = sign * MOVE_STEPSIZE_NORMAL / 2;
2936 if (CAN_FALL(element) &&
2937 y < lev_fieldy - 1 && IS_BELT_ACTIVE(Feld[x][y + 1]))
2938 step = sign * MOVE_STEPSIZE_NORMAL / 2;
2939 else if (element == EL_SPRING)
2940 step = sign * MOVE_STEPSIZE_NORMAL * 2;
2947 void InitPlayerGfxAnimation(struct PlayerInfo *player, int action, int dir)
2949 if (player->GfxAction != action || player->GfxDir != dir)
2952 printf("Player frame reset! (%d => %d, %d => %d)\n",
2953 player->GfxAction, action, player->GfxDir, dir);
2956 player->GfxAction = action;
2957 player->GfxDir = dir;
2959 player->StepFrame = 0;
2963 static void ResetRandomAnimationValue(int x, int y)
2965 GfxRandom[x][y] = INIT_GFX_RANDOM();
2968 static void ResetGfxAnimation(int x, int y)
2971 int element, graphic;
2975 GfxAction[x][y] = ACTION_DEFAULT;
2976 GfxDir[x][y] = MovDir[x][y];
2979 element = Feld[x][y];
2980 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
2982 if (graphic_info[graphic].anim_global_sync)
2983 GfxFrame[x][y] = FrameCounter;
2984 else if (ANIM_MODE(graphic) == ANIM_CE_VALUE)
2985 GfxFrame[x][y] = CustomValue[x][y];
2986 else if (ANIM_MODE(graphic) == ANIM_CE_SCORE)
2987 GfxFrame[x][y] = element_info[element].collect_score;
2991 void InitMovingField(int x, int y, int direction)
2993 int element = Feld[x][y];
2997 int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
2998 int dy = (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
3002 if (!WasJustMoving[x][y] || direction != MovDir[x][y])
3003 ResetGfxAnimation(x, y);
3005 MovDir[x][y] = direction;
3006 GfxDir[x][y] = direction;
3007 GfxAction[x][y] = (direction == MV_DOWN && CAN_FALL(element) ?
3008 ACTION_FALLING : ACTION_MOVING);
3011 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
3013 if (graphic_info[graphic].anim_global_sync)
3014 GfxFrame[x][y] = FrameCounter;
3015 else if (ANIM_MODE(graphic) == ANIM_CE_VALUE)
3016 GfxFrame[x][y] = CustomValue[x][y];
3017 else if (ANIM_MODE(graphic) == ANIM_CE_SCORE)
3018 GfxFrame[x][y] = element_info[element].collect_score;
3021 /* this is needed for CEs with property "can move" / "not moving" */
3023 if (getElementMoveStepsize(x, y) != 0) /* moving or being moved */
3025 if (Feld[newx][newy] == EL_EMPTY)
3026 Feld[newx][newy] = EL_BLOCKED;
3028 MovDir[newx][newy] = MovDir[x][y];
3030 #if USE_NEW_CUSTOM_VALUE
3031 CustomValue[newx][newy] = CustomValue[x][y];
3034 GfxFrame[newx][newy] = GfxFrame[x][y];
3035 GfxRandom[newx][newy] = GfxRandom[x][y];
3036 GfxAction[newx][newy] = GfxAction[x][y];
3037 GfxDir[newx][newy] = GfxDir[x][y];
3041 void Moving2Blocked(int x, int y, int *goes_to_x, int *goes_to_y)
3043 int direction = MovDir[x][y];
3045 int newx = x + (direction & MV_LEFT ? -1 : direction & MV_RIGHT ? +1 : 0);
3046 int newy = y + (direction & MV_UP ? -1 : direction & MV_DOWN ? +1 : 0);
3048 int newx = x + (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
3049 int newy = y + (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
3056 void Blocked2Moving(int x, int y, int *comes_from_x, int *comes_from_y)
3058 int oldx = x, oldy = y;
3059 int direction = MovDir[x][y];
3061 if (direction == MV_LEFT)
3063 else if (direction == MV_RIGHT)
3065 else if (direction == MV_UP)
3067 else if (direction == MV_DOWN)
3070 *comes_from_x = oldx;
3071 *comes_from_y = oldy;
3074 int MovingOrBlocked2Element(int x, int y)
3076 int element = Feld[x][y];
3078 if (element == EL_BLOCKED)
3082 Blocked2Moving(x, y, &oldx, &oldy);
3083 return Feld[oldx][oldy];
3089 static int MovingOrBlocked2ElementIfNotLeaving(int x, int y)
3091 /* like MovingOrBlocked2Element(), but if element is moving
3092 and (x,y) is the field the moving element is just leaving,
3093 return EL_BLOCKED instead of the element value */
3094 int element = Feld[x][y];
3096 if (IS_MOVING(x, y))
3098 if (element == EL_BLOCKED)
3102 Blocked2Moving(x, y, &oldx, &oldy);
3103 return Feld[oldx][oldy];
3112 static void RemoveField(int x, int y)
3114 Feld[x][y] = EL_EMPTY;
3120 #if USE_NEW_CUSTOM_VALUE
3121 CustomValue[x][y] = 0;
3125 ChangeDelay[x][y] = 0;
3126 ChangePage[x][y] = -1;
3127 Pushed[x][y] = FALSE;
3130 ExplodeField[x][y] = EX_TYPE_NONE;
3133 GfxElement[x][y] = EL_UNDEFINED;
3134 GfxAction[x][y] = ACTION_DEFAULT;
3135 GfxDir[x][y] = MV_NONE;
3138 void RemoveMovingField(int x, int y)
3140 int oldx = x, oldy = y, newx = x, newy = y;
3141 int element = Feld[x][y];
3142 int next_element = EL_UNDEFINED;
3144 if (element != EL_BLOCKED && !IS_MOVING(x, y))
3147 if (IS_MOVING(x, y))
3149 Moving2Blocked(x, y, &newx, &newy);
3151 if (Feld[newx][newy] != EL_BLOCKED)
3153 /* element is moving, but target field is not free (blocked), but
3154 already occupied by something different (example: acid pool);
3155 in this case, only remove the moving field, but not the target */
3157 RemoveField(oldx, oldy);
3159 Store[oldx][oldy] = Store2[oldx][oldy] = 0;
3161 DrawLevelField(oldx, oldy);
3166 else if (element == EL_BLOCKED)
3168 Blocked2Moving(x, y, &oldx, &oldy);
3169 if (!IS_MOVING(oldx, oldy))
3173 if (element == EL_BLOCKED &&
3174 (Feld[oldx][oldy] == EL_QUICKSAND_EMPTYING ||
3175 Feld[oldx][oldy] == EL_MAGIC_WALL_EMPTYING ||
3176 Feld[oldx][oldy] == EL_BD_MAGIC_WALL_EMPTYING ||
3177 Feld[oldx][oldy] == EL_AMOEBA_DROPPING))
3178 next_element = get_next_element(Feld[oldx][oldy]);
3180 RemoveField(oldx, oldy);
3181 RemoveField(newx, newy);
3183 Store[oldx][oldy] = Store2[oldx][oldy] = 0;
3185 if (next_element != EL_UNDEFINED)
3186 Feld[oldx][oldy] = next_element;
3188 DrawLevelField(oldx, oldy);
3189 DrawLevelField(newx, newy);
3192 void DrawDynamite(int x, int y)
3194 int sx = SCREENX(x), sy = SCREENY(y);
3195 int graphic = el2img(Feld[x][y]);
3198 if (!IN_SCR_FIELD(sx, sy) || IS_PLAYER(x, y))
3201 if (IS_WALKABLE_INSIDE(Back[x][y]))
3205 DrawGraphic(sx, sy, el2img(Back[x][y]), 0);
3206 else if (Store[x][y])
3207 DrawGraphic(sx, sy, el2img(Store[x][y]), 0);
3209 frame = getGraphicAnimationFrame(graphic, GfxFrame[x][y]);
3211 if (Back[x][y] || Store[x][y])
3212 DrawGraphicThruMask(sx, sy, graphic, frame);
3214 DrawGraphic(sx, sy, graphic, frame);
3217 void CheckDynamite(int x, int y)
3219 if (MovDelay[x][y] != 0) /* dynamite is still waiting to explode */
3223 if (MovDelay[x][y] != 0)
3226 PlayLevelSoundActionIfLoop(x, y, ACTION_ACTIVE);
3232 StopLevelSoundActionIfLoop(x, y, ACTION_ACTIVE);
3239 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
3241 boolean num_checked_players = 0;
3244 for (i = 0; i < MAX_PLAYERS; i++)
3246 if (stored_player[i].active)
3248 int sx = stored_player[i].jx;
3249 int sy = stored_player[i].jy;
3251 if (num_checked_players == 0)
3258 *sx1 = MIN(*sx1, sx);
3259 *sy1 = MIN(*sy1, sy);
3260 *sx2 = MAX(*sx2, sx);
3261 *sy2 = MAX(*sy2, sy);
3264 num_checked_players++;
3269 static boolean checkIfAllPlayersFitToScreen_RND()
3271 int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
3273 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
3275 return (sx2 - sx1 < SCR_FIELDX &&
3276 sy2 - sy1 < SCR_FIELDY);
3279 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
3281 int sx1 = scroll_x, sy1 = scroll_y, sx2 = scroll_x, sy2 = scroll_y;
3283 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
3285 *sx = (sx1 + sx2) / 2;
3286 *sy = (sy1 + sy2) / 2;
3290 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
3291 int center_x, int center_y)
3293 int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
3295 setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
3297 *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
3298 *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
3301 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
3305 setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
3307 return (max_dx <= SCR_FIELDX / 2 &&
3308 max_dy <= SCR_FIELDY / 2);
3316 void DrawRelocateScreen(int x, int y, int move_dir, boolean center_screen,
3317 boolean quick_relocation)
3319 boolean ffwd_delay = (tape.playing && tape.fast_forward);
3320 boolean no_delay = (tape.warp_forward);
3321 int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
3322 int wait_delay_value = (no_delay ? 0 : frame_delay_value);
3324 if (quick_relocation)
3326 int offset = (setup.scroll_delay ? 3 : 0);
3333 if (!IN_VIS_FIELD(SCREENX(x), SCREENY(y)) || center_screen)
3335 scroll_x = (x < SBX_Left + MIDPOSX ? SBX_Left :
3336 x > SBX_Right + MIDPOSX ? SBX_Right :
3339 scroll_y = (y < SBY_Upper + MIDPOSY ? SBY_Upper :
3340 y > SBY_Lower + MIDPOSY ? SBY_Lower :
3345 if ((move_dir == MV_LEFT && scroll_x > x - MIDPOSX + offset) ||
3346 (move_dir == MV_RIGHT && scroll_x < x - MIDPOSX - offset))
3347 scroll_x = x - MIDPOSX + (scroll_x < x - MIDPOSX ? -offset : +offset);
3349 if ((move_dir == MV_UP && scroll_y > y - MIDPOSY + offset) ||
3350 (move_dir == MV_DOWN && scroll_y < y - MIDPOSY - offset))
3351 scroll_y = y - MIDPOSY + (scroll_y < y - MIDPOSY ? -offset : +offset);
3353 /* don't scroll over playfield boundaries */
3354 if (scroll_x < SBX_Left || scroll_x > SBX_Right)
3355 scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
3357 /* don't scroll over playfield boundaries */
3358 if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
3359 scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
3362 RedrawPlayfield(TRUE, 0,0,0,0);
3366 int scroll_xx = (x < SBX_Left + MIDPOSX ? SBX_Left :
3367 x > SBX_Right + MIDPOSX ? SBX_Right :
3370 int scroll_yy = (y < SBY_Upper + MIDPOSY ? SBY_Upper :
3371 y > SBY_Lower + MIDPOSY ? SBY_Lower :
3374 ScrollScreen(NULL, SCROLL_GO_ON); /* scroll last frame to full tile */
3376 while (scroll_x != scroll_xx || scroll_y != scroll_yy)
3379 int fx = FX, fy = FY;
3381 dx = (scroll_xx < scroll_x ? +1 : scroll_xx > scroll_x ? -1 : 0);
3382 dy = (scroll_yy < scroll_y ? +1 : scroll_yy > scroll_y ? -1 : 0);
3384 if (dx == 0 && dy == 0) /* no scrolling needed at all */
3390 fx += dx * TILEX / 2;
3391 fy += dy * TILEY / 2;
3393 ScrollLevel(dx, dy);
3396 /* scroll in two steps of half tile size to make things smoother */
3397 BlitBitmap(drawto_field, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
3399 Delay(wait_delay_value);
3401 /* scroll second step to align at full tile size */
3403 Delay(wait_delay_value);
3408 Delay(wait_delay_value);
3414 void DrawRelocatePlayer(struct PlayerInfo *player, boolean quick_relocation)
3416 boolean ffwd_delay = (tape.playing && tape.fast_forward);
3417 boolean no_delay = (tape.warp_forward);
3418 int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
3419 int wait_delay_value = (no_delay ? 0 : frame_delay_value);
3420 int jx = player->jx;
3421 int jy = player->jy;
3423 if (quick_relocation)
3425 int offset = (setup.scroll_delay ? 3 : 0);
3427 if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
3429 scroll_x = (player->jx < SBX_Left + MIDPOSX ? SBX_Left :
3430 player->jx > SBX_Right + MIDPOSX ? SBX_Right :
3431 player->jx - MIDPOSX);
3433 scroll_y = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
3434 player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
3435 player->jy - MIDPOSY);
3439 if ((player->MovDir == MV_LEFT && scroll_x > jx - MIDPOSX + offset) ||
3440 (player->MovDir == MV_RIGHT && scroll_x < jx - MIDPOSX - offset))
3441 scroll_x = jx - MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
3443 if ((player->MovDir == MV_UP && scroll_y > jy - MIDPOSY + offset) ||
3444 (player->MovDir == MV_DOWN && scroll_y < jy - MIDPOSY - offset))
3445 scroll_y = jy - MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
3447 /* don't scroll over playfield boundaries */
3448 if (scroll_x < SBX_Left || scroll_x > SBX_Right)
3449 scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
3451 /* don't scroll over playfield boundaries */
3452 if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
3453 scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
3456 RedrawPlayfield(TRUE, 0,0,0,0);
3460 int scroll_xx = (player->jx < SBX_Left + MIDPOSX ? SBX_Left :
3461 player->jx > SBX_Right + MIDPOSX ? SBX_Right :
3462 player->jx - MIDPOSX);
3464 int scroll_yy = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
3465 player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
3466 player->jy - MIDPOSY);
3468 ScrollScreen(NULL, SCROLL_GO_ON); /* scroll last frame to full tile */
3470 while (scroll_x != scroll_xx || scroll_y != scroll_yy)
3473 int fx = FX, fy = FY;
3475 dx = (scroll_xx < scroll_x ? +1 : scroll_xx > scroll_x ? -1 : 0);
3476 dy = (scroll_yy < scroll_y ? +1 : scroll_yy > scroll_y ? -1 : 0);
3478 if (dx == 0 && dy == 0) /* no scrolling needed at all */
3484 fx += dx * TILEX / 2;
3485 fy += dy * TILEY / 2;
3487 ScrollLevel(dx, dy);
3490 /* scroll in two steps of half tile size to make things smoother */
3491 BlitBitmap(drawto_field, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
3493 Delay(wait_delay_value);
3495 /* scroll second step to align at full tile size */
3497 Delay(wait_delay_value);
3502 Delay(wait_delay_value);
3508 void RelocatePlayer(int jx, int jy, int el_player_raw)
3510 int el_player = GET_PLAYER_ELEMENT(el_player_raw);
3511 int player_nr = GET_PLAYER_NR(el_player);
3512 struct PlayerInfo *player = &stored_player[player_nr];
3513 boolean ffwd_delay = (tape.playing && tape.fast_forward);
3514 boolean no_delay = (tape.warp_forward);
3515 int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
3516 int wait_delay_value = (no_delay ? 0 : frame_delay_value);
3517 int old_jx = player->jx;
3518 int old_jy = player->jy;
3519 int old_element = Feld[old_jx][old_jy];
3520 int element = Feld[jx][jy];
3521 boolean player_relocated = (old_jx != jx || old_jy != jy);
3523 int move_dir_horiz = (jx < old_jx ? MV_LEFT : jx > old_jx ? MV_RIGHT : 0);
3524 int move_dir_vert = (jy < old_jy ? MV_UP : jy > old_jy ? MV_DOWN : 0);
3525 int enter_side_horiz = MV_DIR_OPPOSITE(move_dir_horiz);
3526 int enter_side_vert = MV_DIR_OPPOSITE(move_dir_vert);
3527 int leave_side_horiz = move_dir_horiz;
3528 int leave_side_vert = move_dir_vert;
3529 int enter_side = enter_side_horiz | enter_side_vert;
3530 int leave_side = leave_side_horiz | leave_side_vert;
3532 if (player->GameOver) /* do not reanimate dead player */
3535 if (!player_relocated) /* no need to relocate the player */
3538 if (IS_PLAYER(jx, jy)) /* player already placed at new position */
3540 RemoveField(jx, jy); /* temporarily remove newly placed player */
3541 DrawLevelField(jx, jy);
3544 if (player->present)
3546 while (player->MovPos)
3548 ScrollPlayer(player, SCROLL_GO_ON);
3549 ScrollScreen(NULL, SCROLL_GO_ON);
3551 AdvanceFrameAndPlayerCounters(player->index_nr);
3556 Delay(wait_delay_value);
3559 DrawPlayer(player); /* needed here only to cleanup last field */
3560 DrawLevelField(player->jx, player->jy); /* remove player graphic */
3562 player->is_moving = FALSE;
3565 if (IS_CUSTOM_ELEMENT(old_element))
3566 CheckElementChangeByPlayer(old_jx, old_jy, old_element,
3568 player->index_bit, leave_side);
3570 CheckTriggeredElementChangeByPlayer(old_jx, old_jy, old_element,
3572 player->index_bit, leave_side);
3574 Feld[jx][jy] = el_player;
3575 InitPlayerField(jx, jy, el_player, TRUE);
3577 if (!ELEM_IS_PLAYER(element)) /* player may be set on walkable element */
3579 Feld[jx][jy] = element;
3580 InitField(jx, jy, FALSE);
3584 /* only visually relocate centered player */
3586 DrawRelocateScreen(player->jx, player->jy, player->MovDir, FALSE,
3587 level.instant_relocation);
3589 if (player->index_nr == game.centered_player_nr)
3590 DrawRelocatePlayer(player, level.instant_relocation);
3593 if (player == local_player) /* only visually relocate local player */
3594 DrawRelocatePlayer(player, level.instant_relocation);
3597 TestIfPlayerTouchesBadThing(jx, jy);
3598 TestIfPlayerTouchesCustomElement(jx, jy);
3600 if (IS_CUSTOM_ELEMENT(element))
3601 CheckElementChangeByPlayer(jx, jy, element, CE_ENTERED_BY_PLAYER,
3602 player->index_bit, enter_side);
3604 CheckTriggeredElementChangeByPlayer(jx, jy, element, CE_PLAYER_ENTERS_X,
3605 player->index_bit, enter_side);
3608 void Explode(int ex, int ey, int phase, int mode)
3614 /* !!! eliminate this variable !!! */
3615 int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
3617 if (game.explosions_delayed)
3619 ExplodeField[ex][ey] = mode;
3623 if (phase == EX_PHASE_START) /* initialize 'Store[][]' field */
3625 int center_element = Feld[ex][ey];
3626 int artwork_element, explosion_element; /* set these values later */
3629 /* --- This is only really needed (and now handled) in "Impact()". --- */
3630 /* do not explode moving elements that left the explode field in time */
3631 if (game.engine_version >= VERSION_IDENT(2,2,0,7) &&
3632 center_element == EL_EMPTY &&
3633 (mode == EX_TYPE_NORMAL || mode == EX_TYPE_CENTER))
3638 /* !!! at this place, the center element may be EL_BLOCKED !!! */
3639 if (mode == EX_TYPE_NORMAL ||
3640 mode == EX_TYPE_CENTER ||
3641 mode == EX_TYPE_CROSS)
3642 PlayLevelSoundElementAction(ex, ey, artwork_element, ACTION_EXPLODING);
3645 /* remove things displayed in background while burning dynamite */
3646 if (Back[ex][ey] != EL_EMPTY && !IS_INDESTRUCTIBLE(Back[ex][ey]))
3649 if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
3651 /* put moving element to center field (and let it explode there) */
3652 center_element = MovingOrBlocked2Element(ex, ey);
3653 RemoveMovingField(ex, ey);
3654 Feld[ex][ey] = center_element;
3657 /* now "center_element" is finally determined -- set related values now */
3658 artwork_element = center_element; /* for custom player artwork */
3659 explosion_element = center_element; /* for custom player artwork */
3661 if (IS_PLAYER(ex, ey))
3663 int player_nr = GET_PLAYER_NR(StorePlayer[ex][ey]);
3665 artwork_element = stored_player[player_nr].artwork_element;
3667 if (level.use_explosion_element[player_nr])
3669 explosion_element = level.explosion_element[player_nr];
3670 artwork_element = explosion_element;
3675 if (mode == EX_TYPE_NORMAL ||
3676 mode == EX_TYPE_CENTER ||
3677 mode == EX_TYPE_CROSS)
3678 PlayLevelSoundElementAction(ex, ey, artwork_element, ACTION_EXPLODING);
3682 last_phase = element_info[explosion_element].explosion_delay + 1;
3684 last_phase = element_info[center_element].explosion_delay + 1;
3687 for (y = ey - 1; y <= ey + 1; y++) for (x = ex - 1; x <= ex + 1; x++)
3689 int xx = x - ex + 1;
3690 int yy = y - ey + 1;
3693 if (!IN_LEV_FIELD(x, y) ||
3694 (mode & EX_TYPE_SINGLE_TILE && (x != ex || y != ey)) ||
3695 (mode == EX_TYPE_CROSS && (x != ex && y != ey)))
3698 element = Feld[x][y];
3700 if (IS_MOVING(x, y) || IS_BLOCKED(x, y))
3702 element = MovingOrBlocked2Element(x, y);
3704 if (!IS_EXPLOSION_PROOF(element))
3705 RemoveMovingField(x, y);
3708 /* indestructible elements can only explode in center (but not flames) */
3709 if ((IS_EXPLOSION_PROOF(element) && (x != ex || y != ey ||
3710 mode == EX_TYPE_BORDER)) ||
3711 element == EL_FLAMES)
3714 /* no idea why this was changed from 3.0.8 to 3.1.0 -- this causes buggy
3715 behaviour, for example when touching a yamyam that explodes to rocks
3716 with active deadly shield, a rock is created under the player !!! */
3717 /* (case 1 (surely buggy): >= 3.1.0, case 2 (maybe buggy): <= 3.0.8) */
3719 if (IS_PLAYER(x, y) && SHIELD_ON(PLAYERINFO(x, y)) &&
3720 (game.engine_version < VERSION_IDENT(3,1,0,0) ||
3721 (x == ex && y == ey && mode != EX_TYPE_BORDER)))
3723 if (IS_PLAYER(x, y) && SHIELD_ON(PLAYERINFO(x, y)))
3726 if (IS_ACTIVE_BOMB(element))
3728 /* re-activate things under the bomb like gate or penguin */
3729 Feld[x][y] = (Back[x][y] ? Back[x][y] : EL_EMPTY);
3736 /* save walkable background elements while explosion on same tile */
3737 if (IS_WALKABLE(element) && IS_INDESTRUCTIBLE(element) &&
3738 (x != ex || y != ey || mode == EX_TYPE_BORDER))
3739 Back[x][y] = element;
3741 /* ignite explodable elements reached by other explosion */
3742 if (element == EL_EXPLOSION)
3743 element = Store2[x][y];
3745 if (AmoebaNr[x][y] &&
3746 (element == EL_AMOEBA_FULL ||
3747 element == EL_BD_AMOEBA ||
3748 element == EL_AMOEBA_GROWING))
3750 AmoebaCnt[AmoebaNr[x][y]]--;
3751 AmoebaCnt2[AmoebaNr[x][y]]--;
3756 if (IS_PLAYER(ex, ey) && !PLAYER_EXPLOSION_PROTECTED(ex, ey))
3759 int player_nr = StorePlayer[ex][ey] - EL_PLAYER_1;
3761 Store[x][y] = EL_PLAYER_IS_EXPLODING_1 + player_nr;
3763 switch(StorePlayer[ex][ey])
3766 Store[x][y] = EL_PLAYER_IS_EXPLODING_2;
3769 Store[x][y] = EL_PLAYER_IS_EXPLODING_3;
3772 Store[x][y] = EL_PLAYER_IS_EXPLODING_4;
3776 Store[x][y] = EL_PLAYER_IS_EXPLODING_1;
3781 if (PLAYERINFO(ex, ey)->use_murphy)
3782 Store[x][y] = EL_EMPTY;
3785 /* !!! check this case -- currently needed for rnd_rado_negundo_v,
3786 !!! levels 015 018 019 020 021 022 023 026 027 028 !!! */
3787 else if (ELEM_IS_PLAYER(center_element))
3788 Store[x][y] = EL_EMPTY;
3789 else if (center_element == EL_YAMYAM)
3790 Store[x][y] = level.yamyam_content[game.yamyam_content_nr].e[xx][yy];
3791 else if (element_info[center_element].content.e[xx][yy] != EL_EMPTY)
3792 Store[x][y] = element_info[center_element].content.e[xx][yy];
3794 /* needed because EL_BD_BUTTERFLY is not defined as "CAN_EXPLODE"
3795 (killing EL_BD_BUTTERFLY with dynamite would result in BD diamond
3796 otherwise) -- FIX THIS !!! */
3797 else if (!CAN_EXPLODE(element) && element != EL_BD_BUTTERFLY)
3798 Store[x][y] = element_info[element].content.e[1][1];
3800 else if (!CAN_EXPLODE(element))
3801 Store[x][y] = element_info[element].content.e[1][1];
3804 Store[x][y] = EL_EMPTY;
3806 else if (center_element == EL_MOLE)
3807 Store[x][y] = EL_EMERALD_RED;
3808 else if (center_element == EL_PENGUIN)
3809 Store[x][y] = EL_EMERALD_PURPLE;
3810 else if (center_element == EL_BUG)
3811 Store[x][y] = ((x == ex && y == ey) ? EL_DIAMOND : EL_EMERALD);
3812 else if (center_element == EL_BD_BUTTERFLY)
3813 Store[x][y] = EL_BD_DIAMOND;
3814 else if (center_element == EL_SP_ELECTRON)
3815 Store[x][y] = EL_SP_INFOTRON;
3816 else if (center_element == EL_AMOEBA_TO_DIAMOND)
3817 Store[x][y] = level.amoeba_content;
3818 else if (center_element == EL_YAMYAM)
3819 Store[x][y] = level.yamyam_content[game.yamyam_content_nr].e[xx][yy];
3820 else if (IS_CUSTOM_ELEMENT(center_element) &&
3821 element_info[center_element].content.e[xx][yy] != EL_EMPTY)
3822 Store[x][y] = element_info[center_element].content.e[xx][yy];
3823 else if (element == EL_WALL_EMERALD)
3824 Store[x][y] = EL_EMERALD;
3825 else if (element == EL_WALL_DIAMOND)
3826 Store[x][y] = EL_DIAMOND;
3827 else if (element == EL_WALL_BD_DIAMOND)
3828 Store[x][y] = EL_BD_DIAMOND;
3829 else if (element == EL_WALL_EMERALD_YELLOW)
3830 Store[x][y] = EL_EMERALD_YELLOW;
3831 else if (element == EL_WALL_EMERALD_RED)
3832 Store[x][y] = EL_EMERALD_RED;
3833 else if (element == EL_WALL_EMERALD_PURPLE)
3834 Store[x][y] = EL_EMERALD_PURPLE;
3835 else if (element == EL_WALL_PEARL)
3836 Store[x][y] = EL_PEARL;
3837 else if (element == EL_WALL_CRYSTAL)
3838 Store[x][y] = EL_CRYSTAL;
3839 else if (IS_CUSTOM_ELEMENT(element) && !CAN_EXPLODE(element))
3840 Store[x][y] = element_info[element].content.e[1][1];
3842 Store[x][y] = EL_EMPTY;
3845 if (x != ex || y != ey || mode == EX_TYPE_BORDER ||
3846 center_element == EL_AMOEBA_TO_DIAMOND)
3847 Store2[x][y] = element;
3849 Feld[x][y] = EL_EXPLOSION;
3850 GfxElement[x][y] = artwork_element;
3853 printf(":: setting gfx(%d,%d) to %d ['%s']\n",
3854 x, y, artwork_element, EL_NAME(artwork_element));
3857 ExplodePhase[x][y] = 1;
3858 ExplodeDelay[x][y] = last_phase;
3863 if (center_element == EL_YAMYAM)
3864 game.yamyam_content_nr =
3865 (game.yamyam_content_nr + 1) % level.num_yamyam_contents;
3877 GfxFrame[x][y] = 0; /* restart explosion animation */
3879 last_phase = ExplodeDelay[x][y];
3881 ExplodePhase[x][y] = (phase < last_phase ? phase + 1 : 0);
3885 /* activate this even in non-DEBUG version until cause for crash in
3886 getGraphicAnimationFrame() (see below) is found and eliminated */
3892 /* this can happen if the player leaves an explosion just in time */
3893 if (GfxElement[x][y] == EL_UNDEFINED)
3894 GfxElement[x][y] = EL_EMPTY;
3896 if (GfxElement[x][y] == EL_UNDEFINED)
3899 printf("Explode(): x = %d, y = %d: GfxElement == EL_UNDEFINED\n", x, y);
3900 printf("Explode(): This should never happen!\n");
3903 GfxElement[x][y] = EL_EMPTY;
3909 border_element = Store2[x][y];
3910 if (IS_PLAYER(x, y) && !PLAYER_EXPLOSION_PROTECTED(x, y))
3911 border_element = StorePlayer[x][y];
3913 if (phase == element_info[border_element].ignition_delay ||
3914 phase == last_phase)
3916 boolean border_explosion = FALSE;
3918 if (IS_PLAYER(x, y) && PLAYERINFO(x, y)->present &&
3919 !PLAYER_EXPLOSION_PROTECTED(x, y))
3921 KillPlayerUnlessExplosionProtected(x, y);
3922 border_explosion = TRUE;
3924 else if (CAN_EXPLODE_BY_EXPLOSION(border_element))
3926 Feld[x][y] = Store2[x][y];
3929 border_explosion = TRUE;
3931 else if (border_element == EL_AMOEBA_TO_DIAMOND)
3933 AmoebeUmwandeln(x, y);
3935 border_explosion = TRUE;
3938 /* if an element just explodes due to another explosion (chain-reaction),
3939 do not immediately end the new explosion when it was the last frame of
3940 the explosion (as it would be done in the following "if"-statement!) */
3941 if (border_explosion && phase == last_phase)
3945 if (phase == last_phase)
3949 element = Feld[x][y] = Store[x][y];
3950 Store[x][y] = Store2[x][y] = 0;
3951 GfxElement[x][y] = EL_UNDEFINED;
3953 /* player can escape from explosions and might therefore be still alive */
3954 if (element >= EL_PLAYER_IS_EXPLODING_1 &&
3955 element <= EL_PLAYER_IS_EXPLODING_4)
3957 int player_nr = element - EL_PLAYER_IS_EXPLODING_1;
3958 int explosion_element = EL_PLAYER_1 + player_nr;
3959 int xx = MIN(MAX(0, x - stored_player[player_nr].jx + 1), 2);
3960 int yy = MIN(MAX(0, y - stored_player[player_nr].jy + 1), 2);
3962 if (level.use_explosion_element[player_nr])
3963 explosion_element = level.explosion_element[player_nr];
3965 Feld[x][y] = (stored_player[player_nr].active ? EL_EMPTY :
3966 element_info[explosion_element].content.e[xx][yy]);
3969 /* restore probably existing indestructible background element */
3970 if (Back[x][y] && IS_INDESTRUCTIBLE(Back[x][y]))
3971 element = Feld[x][y] = Back[x][y];
3974 MovDir[x][y] = MovPos[x][y] = MovDelay[x][y] = 0;
3975 GfxDir[x][y] = MV_NONE;
3976 ChangeDelay[x][y] = 0;
3977 ChangePage[x][y] = -1;
3979 #if USE_NEW_CUSTOM_VALUE
3980 CustomValue[x][y] = 0;
3983 InitField_WithBug2(x, y, FALSE);
3985 DrawLevelField(x, y);
3987 TestIfElementTouchesCustomElement(x, y);
3989 if (GFX_CRUMBLED(element))
3990 DrawLevelFieldCrumbledSandNeighbours(x, y);
3992 if (IS_PLAYER(x, y) && !PLAYERINFO(x, y)->present)
3993 StorePlayer[x][y] = 0;
3995 if (ELEM_IS_PLAYER(element))
3996 RelocatePlayer(x, y, element);
3998 else if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
4000 int graphic = el_act2img(GfxElement[x][y], ACTION_EXPLODING);
4001 int frame = getGraphicAnimationFrame(graphic, GfxFrame[x][y]);
4004 DrawLevelFieldCrumbledSand(x, y);
4006 if (IS_WALKABLE_OVER(Back[x][y]) && Back[x][y] != EL_EMPTY)
4008 DrawLevelElement(x, y, Back[x][y]);
4009 DrawGraphicThruMask(SCREENX(x), SCREENY(y), graphic, frame);
4011 else if (IS_WALKABLE_UNDER(Back[x][y]))
4013 DrawGraphic(SCREENX(x), SCREENY(y), graphic, frame);
4014 DrawLevelElementThruMask(x, y, Back[x][y]);
4016 else if (!IS_WALKABLE_INSIDE(Back[x][y]))
4017 DrawGraphic(SCREENX(x), SCREENY(y), graphic, frame);
4021 void DynaExplode(int ex, int ey)
4024 int dynabomb_element = Feld[ex][ey];
4025 int dynabomb_size = 1;
4026 boolean dynabomb_xl = FALSE;
4027 struct PlayerInfo *player;
4028 static int xy[4][2] =
4036 if (IS_ACTIVE_BOMB(dynabomb_element))
4038 player = &stored_player[dynabomb_element - EL_DYNABOMB_PLAYER_1_ACTIVE];
4039 dynabomb_size = player->dynabomb_size;
4040 dynabomb_xl = player->dynabomb_xl;
4041 player->dynabombs_left++;
4044 Explode(ex, ey, EX_PHASE_START, EX_TYPE_CENTER);
4046 for (i = 0; i < NUM_DIRECTIONS; i++)
4048 for (j = 1; j <= dynabomb_size; j++)
4050 int x = ex + j * xy[i][0];
4051 int y = ey + j * xy[i][1];
4054 if (!IN_LEV_FIELD(x, y) || IS_INDESTRUCTIBLE(Feld[x][y]))
4057 element = Feld[x][y];
4059 /* do not restart explosions of fields with active bombs */
4060 if (element == EL_EXPLOSION && IS_ACTIVE_BOMB(Store2[x][y]))
4063 Explode(x, y, EX_PHASE_START, EX_TYPE_BORDER);
4065 if (element != EL_EMPTY && element != EL_EXPLOSION &&
4066 !IS_DIGGABLE(element) && !dynabomb_xl)
4072 void Bang(int x, int y)
4074 int element = MovingOrBlocked2Element(x, y);
4075 int explosion_type = EX_TYPE_NORMAL;
4077 if (IS_PLAYER(x, y) && !PLAYER_EXPLOSION_PROTECTED(x, y))
4079 struct PlayerInfo *player = PLAYERINFO(x, y);
4081 element = Feld[x][y] = (player->use_murphy ? EL_SP_MURPHY :
4082 player->element_nr);
4084 if (level.use_explosion_element[player->index_nr])
4086 int explosion_element = level.explosion_element[player->index_nr];
4088 if (element_info[explosion_element].explosion_type == EXPLODES_CROSS)
4089 explosion_type = EX_TYPE_CROSS;
4090 else if (element_info[explosion_element].explosion_type == EXPLODES_1X1)
4091 explosion_type = EX_TYPE_CENTER;
4099 case EL_BD_BUTTERFLY:
4102 case EL_DARK_YAMYAM:
4106 RaiseScoreElement(element);
4109 case EL_DYNABOMB_PLAYER_1_ACTIVE:
4110 case EL_DYNABOMB_PLAYER_2_ACTIVE:
4111 case EL_DYNABOMB_PLAYER_3_ACTIVE:
4112 case EL_DYNABOMB_PLAYER_4_ACTIVE:
4113 case EL_DYNABOMB_INCREASE_NUMBER:
4114 case EL_DYNABOMB_INCREASE_SIZE:
4115 case EL_DYNABOMB_INCREASE_POWER:
4116 explosion_type = EX_TYPE_DYNA;
4121 case EL_LAMP_ACTIVE:
4122 case EL_AMOEBA_TO_DIAMOND:
4123 if (!IS_PLAYER(x, y)) /* penguin and player may be at same field */
4124 explosion_type = EX_TYPE_CENTER;
4128 if (element_info[element].explosion_type == EXPLODES_CROSS)
4129 explosion_type = EX_TYPE_CROSS;
4130 else if (element_info[element].explosion_type == EXPLODES_1X1)
4131 explosion_type = EX_TYPE_CENTER;
4135 if (explosion_type == EX_TYPE_DYNA)
4138 Explode(x, y, EX_PHASE_START, explosion_type);
4140 CheckTriggeredElementChange(x, y, element, CE_EXPLOSION_OF_X);
4143 void SplashAcid(int x, int y)
4145 if (IN_LEV_FIELD(x - 1, y - 1) && IS_FREE(x - 1, y - 1) &&
4146 (!IN_LEV_FIELD(x - 1, y - 2) ||
4147 !CAN_FALL(MovingOrBlocked2Element(x - 1, y - 2))))
4148 Feld[x - 1][y - 1] = EL_ACID_SPLASH_LEFT;
4150 if (IN_LEV_FIELD(x + 1, y - 1) && IS_FREE(x + 1, y - 1) &&
4151 (!IN_LEV_FIELD(x + 1, y - 2) ||
4152 !CAN_FALL(MovingOrBlocked2Element(x + 1, y - 2))))
4153 Feld[x + 1][y - 1] = EL_ACID_SPLASH_RIGHT;
4155 PlayLevelSound(x, y, SND_ACID_SPLASHING);
4158 static void InitBeltMovement()
4160 static int belt_base_element[4] =
4162 EL_CONVEYOR_BELT_1_LEFT,
4163 EL_CONVEYOR_BELT_2_LEFT,
4164 EL_CONVEYOR_BELT_3_LEFT,
4165 EL_CONVEYOR_BELT_4_LEFT
4167 static int belt_base_active_element[4] =
4169 EL_CONVEYOR_BELT_1_LEFT_ACTIVE,
4170 EL_CONVEYOR_BELT_2_LEFT_ACTIVE,
4171 EL_CONVEYOR_BELT_3_LEFT_ACTIVE,
4172 EL_CONVEYOR_BELT_4_LEFT_ACTIVE
4177 /* set frame order for belt animation graphic according to belt direction */
4178 for (i = 0; i < NUM_BELTS; i++)
4182 for (j = 0; j < NUM_BELT_PARTS; j++)
4184 int element = belt_base_active_element[belt_nr] + j;
4185 int graphic = el2img(element);
4187 if (game.belt_dir[i] == MV_LEFT)
4188 graphic_info[graphic].anim_mode &= ~ANIM_REVERSE;
4190 graphic_info[graphic].anim_mode |= ANIM_REVERSE;
4195 SCAN_PLAYFIELD(x, y)
4197 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
4200 int element = Feld[x][y];
4202 for (i = 0; i < NUM_BELTS; i++)
4204 if (IS_BELT(element) && game.belt_dir[i] != MV_NONE)
4206 int e_belt_nr = getBeltNrFromBeltElement(element);
4209 if (e_belt_nr == belt_nr)
4211 int belt_part = Feld[x][y] - belt_base_element[belt_nr];
4213 Feld[x][y] = belt_base_active_element[belt_nr] + belt_part;
4220 static void ToggleBeltSwitch(int x, int y)
4222 static int belt_base_element[4] =
4224 EL_CONVEYOR_BELT_1_LEFT,
4225 EL_CONVEYOR_BELT_2_LEFT,
4226 EL_CONVEYOR_BELT_3_LEFT,
4227 EL_CONVEYOR_BELT_4_LEFT
4229 static int belt_base_active_element[4] =
4231 EL_CONVEYOR_BELT_1_LEFT_ACTIVE,
4232 EL_CONVEYOR_BELT_2_LEFT_ACTIVE,
4233 EL_CONVEYOR_BELT_3_LEFT_ACTIVE,
4234 EL_CONVEYOR_BELT_4_LEFT_ACTIVE
4236 static int belt_base_switch_element[4] =
4238 EL_CONVEYOR_BELT_1_SWITCH_LEFT,
4239 EL_CONVEYOR_BELT_2_SWITCH_LEFT,
4240 EL_CONVEYOR_BELT_3_SWITCH_LEFT,
4241 EL_CONVEYOR_BELT_4_SWITCH_LEFT
4243 static int belt_move_dir[4] =
4251 int element = Feld[x][y];
4252 int belt_nr = getBeltNrFromBeltSwitchElement(element);
4253 int belt_dir_nr = (game.belt_dir_nr[belt_nr] + 1) % 4;
4254 int belt_dir = belt_move_dir[belt_dir_nr];
4257 if (!IS_BELT_SWITCH(element))
4260 game.belt_dir_nr[belt_nr] = belt_dir_nr;
4261 game.belt_dir[belt_nr] = belt_dir;
4263 if (belt_dir_nr == 3)
4266 /* set frame order for belt animation graphic according to belt direction */
4267 for (i = 0; i < NUM_BELT_PARTS; i++)
4269 int element = belt_base_active_element[belt_nr] + i;
4270 int graphic = el2img(element);
4272 if (belt_dir == MV_LEFT)
4273 graphic_info[graphic].anim_mode &= ~ANIM_REVERSE;
4275 graphic_info[graphic].anim_mode |= ANIM_REVERSE;
4279 SCAN_PLAYFIELD(xx, yy)
4281 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
4284 int element = Feld[xx][yy];
4286 if (IS_BELT_SWITCH(element))
4288 int e_belt_nr = getBeltNrFromBeltSwitchElement(element);
4290 if (e_belt_nr == belt_nr)
4292 Feld[xx][yy] = belt_base_switch_element[belt_nr] + belt_dir_nr;
4293 DrawLevelField(xx, yy);
4296 else if (IS_BELT(element) && belt_dir != MV_NONE)
4298 int e_belt_nr = getBeltNrFromBeltElement(element);
4300 if (e_belt_nr == belt_nr)
4302 int belt_part = Feld[xx][yy] - belt_base_element[belt_nr];
4304 Feld[xx][yy] = belt_base_active_element[belt_nr] + belt_part;
4305 DrawLevelField(xx, yy);
4308 else if (IS_BELT_ACTIVE(element) && belt_dir == MV_NONE)
4310 int e_belt_nr = getBeltNrFromBeltActiveElement(element);
4312 if (e_belt_nr == belt_nr)
4314 int belt_part = Feld[xx][yy] - belt_base_active_element[belt_nr];
4316 Feld[xx][yy] = belt_base_element[belt_nr] + belt_part;
4317 DrawLevelField(xx, yy);
4323 static void ToggleSwitchgateSwitch(int x, int y)
4327 game.switchgate_pos = !game.switchgate_pos;
4330 SCAN_PLAYFIELD(xx, yy)
4332 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
4335 int element = Feld[xx][yy];
4337 if (element == EL_SWITCHGATE_SWITCH_UP ||
4338 element == EL_SWITCHGATE_SWITCH_DOWN)
4340 Feld[xx][yy] = EL_SWITCHGATE_SWITCH_UP + game.switchgate_pos;
4341 DrawLevelField(xx, yy);
4343 else if (element == EL_SWITCHGATE_OPEN ||
4344 element == EL_SWITCHGATE_OPENING)
4346 Feld[xx][yy] = EL_SWITCHGATE_CLOSING;
4348 PlayLevelSoundAction(xx, yy, ACTION_CLOSING);
4350 else if (element == EL_SWITCHGATE_CLOSED ||
4351 element == EL_SWITCHGATE_CLOSING)
4353 Feld[xx][yy] = EL_SWITCHGATE_OPENING;
4355 PlayLevelSoundAction(xx, yy, ACTION_OPENING);
4360 static int getInvisibleActiveFromInvisibleElement(int element)
4362 return (element == EL_INVISIBLE_STEELWALL ? EL_INVISIBLE_STEELWALL_ACTIVE :
4363 element == EL_INVISIBLE_WALL ? EL_INVISIBLE_WALL_ACTIVE :
4364 element == EL_INVISIBLE_SAND ? EL_INVISIBLE_SAND_ACTIVE :
4368 static int getInvisibleFromInvisibleActiveElement(int element)
4370 return (element == EL_INVISIBLE_STEELWALL_ACTIVE ? EL_INVISIBLE_STEELWALL :
4371 element == EL_INVISIBLE_WALL_ACTIVE ? EL_INVISIBLE_WALL :
4372 element == EL_INVISIBLE_SAND_ACTIVE ? EL_INVISIBLE_SAND :
4376 static void RedrawAllLightSwitchesAndInvisibleElements()
4381 SCAN_PLAYFIELD(x, y)
4383 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
4386 int element = Feld[x][y];
4388 if (element == EL_LIGHT_SWITCH &&
4389 game.light_time_left > 0)
4391 Feld[x][y] = EL_LIGHT_SWITCH_ACTIVE;
4392 DrawLevelField(x, y);
4394 else if (element == EL_LIGHT_SWITCH_ACTIVE &&
4395 game.light_time_left == 0)
4397 Feld[x][y] = EL_LIGHT_SWITCH;
4398 DrawLevelField(x, y);
4400 else if (element == EL_EMC_DRIPPER &&
4401 game.light_time_left > 0)
4403 Feld[x][y] = EL_EMC_DRIPPER_ACTIVE;
4404 DrawLevelField(x, y);
4406 else if (element == EL_EMC_DRIPPER_ACTIVE &&
4407 game.light_time_left == 0)
4409 Feld[x][y] = EL_EMC_DRIPPER;
4410 DrawLevelField(x, y);
4412 else if (element == EL_INVISIBLE_STEELWALL ||
4413 element == EL_INVISIBLE_WALL ||
4414 element == EL_INVISIBLE_SAND)
4416 if (game.light_time_left > 0)
4417 Feld[x][y] = getInvisibleActiveFromInvisibleElement(element);
4419 DrawLevelField(x, y);
4421 /* uncrumble neighbour fields, if needed */
4422 if (element == EL_INVISIBLE_SAND)
4423 DrawLevelFieldCrumbledSandNeighbours(x, y);
4425 else if (element == EL_INVISIBLE_STEELWALL_ACTIVE ||
4426 element == EL_INVISIBLE_WALL_ACTIVE ||
4427 element == EL_INVISIBLE_SAND_ACTIVE)
4429 if (game.light_time_left == 0)
4430 Feld[x][y] = getInvisibleFromInvisibleActiveElement(element);
4432 DrawLevelField(x, y);
4434 /* re-crumble neighbour fields, if needed */
4435 if (element == EL_INVISIBLE_SAND)
4436 DrawLevelFieldCrumbledSandNeighbours(x, y);
4441 static void RedrawAllInvisibleElementsForLenses()
4446 SCAN_PLAYFIELD(x, y)
4448 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
4451 int element = Feld[x][y];
4453 if (element == EL_EMC_DRIPPER &&
4454 game.lenses_time_left > 0)
4456 Feld[x][y] = EL_EMC_DRIPPER_ACTIVE;
4457 DrawLevelField(x, y);
4459 else if (element == EL_EMC_DRIPPER_ACTIVE &&
4460 game.lenses_time_left == 0)
4462 Feld[x][y] = EL_EMC_DRIPPER;
4463 DrawLevelField(x, y);
4465 else if (element == EL_INVISIBLE_STEELWALL ||
4466 element == EL_INVISIBLE_WALL ||
4467 element == EL_INVISIBLE_SAND)
4469 if (game.lenses_time_left > 0)
4470 Feld[x][y] = getInvisibleActiveFromInvisibleElement(element);
4472 DrawLevelField(x, y);
4474 /* uncrumble neighbour fields, if needed */
4475 if (element == EL_INVISIBLE_SAND)
4476 DrawLevelFieldCrumbledSandNeighbours(x, y);
4478 else if (element == EL_INVISIBLE_STEELWALL_ACTIVE ||
4479 element == EL_INVISIBLE_WALL_ACTIVE ||
4480 element == EL_INVISIBLE_SAND_ACTIVE)
4482 if (game.lenses_time_left == 0)
4483 Feld[x][y] = getInvisibleFromInvisibleActiveElement(element);
4485 DrawLevelField(x, y);
4487 /* re-crumble neighbour fields, if needed */
4488 if (element == EL_INVISIBLE_SAND)
4489 DrawLevelFieldCrumbledSandNeighbours(x, y);
4494 static void RedrawAllInvisibleElementsForMagnifier()
4499 SCAN_PLAYFIELD(x, y)
4501 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
4504 int element = Feld[x][y];
4506 if (element == EL_EMC_FAKE_GRASS &&
4507 game.magnify_time_left > 0)
4509 Feld[x][y] = EL_EMC_FAKE_GRASS_ACTIVE;
4510 DrawLevelField(x, y);
4512 else if (element == EL_EMC_FAKE_GRASS_ACTIVE &&
4513 game.magnify_time_left == 0)
4515 Feld[x][y] = EL_EMC_FAKE_GRASS;
4516 DrawLevelField(x, y);
4518 else if (IS_GATE_GRAY(element) &&
4519 game.magnify_time_left > 0)
4521 Feld[x][y] = (IS_RND_GATE_GRAY(element) ?
4522 element - EL_GATE_1_GRAY + EL_GATE_1_GRAY_ACTIVE :
4523 IS_EM_GATE_GRAY(element) ?
4524 element - EL_EM_GATE_1_GRAY + EL_EM_GATE_1_GRAY_ACTIVE :
4525 IS_EMC_GATE_GRAY(element) ?
4526 element - EL_EMC_GATE_5_GRAY + EL_EMC_GATE_5_GRAY_ACTIVE :
4528 DrawLevelField(x, y);
4530 else if (IS_GATE_GRAY_ACTIVE(element) &&
4531 game.magnify_time_left == 0)
4533 Feld[x][y] = (IS_RND_GATE_GRAY_ACTIVE(element) ?
4534 element - EL_GATE_1_GRAY_ACTIVE + EL_GATE_1_GRAY :
4535 IS_EM_GATE_GRAY_ACTIVE(element) ?
4536 element - EL_EM_GATE_1_GRAY_ACTIVE + EL_EM_GATE_1_GRAY :
4537 IS_EMC_GATE_GRAY_ACTIVE(element) ?
4538 element - EL_EMC_GATE_5_GRAY_ACTIVE + EL_EMC_GATE_5_GRAY :
4540 DrawLevelField(x, y);
4545 static void ToggleLightSwitch(int x, int y)
4547 int element = Feld[x][y];
4549 game.light_time_left =
4550 (element == EL_LIGHT_SWITCH ?
4551 level.time_light * FRAMES_PER_SECOND : 0);
4553 RedrawAllLightSwitchesAndInvisibleElements();
4556 static void ActivateTimegateSwitch(int x, int y)
4560 game.timegate_time_left = level.time_timegate * FRAMES_PER_SECOND;
4563 SCAN_PLAYFIELD(xx, yy)
4565 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
4568 int element = Feld[xx][yy];
4570 if (element == EL_TIMEGATE_CLOSED ||
4571 element == EL_TIMEGATE_CLOSING)
4573 Feld[xx][yy] = EL_TIMEGATE_OPENING;
4574 PlayLevelSound(xx, yy, SND_TIMEGATE_OPENING);
4578 else if (element == EL_TIMEGATE_SWITCH_ACTIVE)
4580 Feld[xx][yy] = EL_TIMEGATE_SWITCH;
4581 DrawLevelField(xx, yy);
4587 Feld[x][y] = EL_TIMEGATE_SWITCH_ACTIVE;
4590 void Impact(int x, int y)
4592 boolean last_line = (y == lev_fieldy - 1);
4593 boolean object_hit = FALSE;
4594 boolean impact = (last_line || object_hit);
4595 int element = Feld[x][y];
4596 int smashed = EL_STEELWALL;
4598 if (!last_line) /* check if element below was hit */
4600 if (Feld[x][y + 1] == EL_PLAYER_IS_LEAVING)
4603 object_hit = (!IS_FREE(x, y + 1) && (!IS_MOVING(x, y + 1) ||
4604 MovDir[x][y + 1] != MV_DOWN ||
4605 MovPos[x][y + 1] <= TILEY / 2));
4607 /* do not smash moving elements that left the smashed field in time */
4608 if (game.engine_version >= VERSION_IDENT(2,2,0,7) && IS_MOVING(x, y + 1) &&
4609 ABS(MovPos[x][y + 1] + getElementMoveStepsize(x, y + 1)) >= TILEX)
4612 #if USE_QUICKSAND_IMPACT_BUGFIX
4613 if (Feld[x][y + 1] == EL_QUICKSAND_EMPTYING && object_hit == FALSE)
4615 RemoveMovingField(x, y + 1);
4616 Feld[x][y + 1] = EL_QUICKSAND_EMPTY;
4617 Feld[x][y + 2] = EL_ROCK;
4618 DrawLevelField(x, y + 2);
4625 smashed = MovingOrBlocked2Element(x, y + 1);
4627 impact = (last_line || object_hit);
4630 if (!last_line && smashed == EL_ACID) /* element falls into acid */
4632 SplashAcid(x, y + 1);
4636 /* !!! not sufficient for all cases -- see EL_PEARL below !!! */
4637 /* only reset graphic animation if graphic really changes after impact */
4639 el_act_dir2img(element, GfxAction[x][y], MV_DOWN) != el2img(element))
4641 ResetGfxAnimation(x, y);
4642 DrawLevelField(x, y);
4645 if (impact && CAN_EXPLODE_IMPACT(element))
4650 else if (impact && element == EL_PEARL)
4652 ResetGfxAnimation(x, y);
4654 Feld[x][y] = EL_PEARL_BREAKING;
4655 PlayLevelSound(x, y, SND_PEARL_BREAKING);
4658 else if (impact && CheckElementChange(x, y, element, smashed, CE_IMPACT))
4660 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
4665 if (impact && element == EL_AMOEBA_DROP)
4667 if (object_hit && IS_PLAYER(x, y + 1))
4668 KillPlayerUnlessEnemyProtected(x, y + 1);
4669 else if (object_hit && smashed == EL_PENGUIN)
4673 Feld[x][y] = EL_AMOEBA_GROWING;
4674 Store[x][y] = EL_AMOEBA_WET;
4676 ResetRandomAnimationValue(x, y);
4681 if (object_hit) /* check which object was hit */
4683 if (CAN_PASS_MAGIC_WALL(element) &&
4684 (smashed == EL_MAGIC_WALL ||
4685 smashed == EL_BD_MAGIC_WALL))
4688 int activated_magic_wall =
4689 (smashed == EL_MAGIC_WALL ? EL_MAGIC_WALL_ACTIVE :
4690 EL_BD_MAGIC_WALL_ACTIVE);
4692 /* activate magic wall / mill */
4694 SCAN_PLAYFIELD(xx, yy)
4696 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
4698 if (Feld[xx][yy] == smashed)
4699 Feld[xx][yy] = activated_magic_wall;
4701 game.magic_wall_time_left = level.time_magic_wall * FRAMES_PER_SECOND;
4702 game.magic_wall_active = TRUE;
4704 PlayLevelSound(x, y, (smashed == EL_MAGIC_WALL ?
4705 SND_MAGIC_WALL_ACTIVATING :
4706 SND_BD_MAGIC_WALL_ACTIVATING));
4709 if (IS_PLAYER(x, y + 1))
4711 if (CAN_SMASH_PLAYER(element))
4713 KillPlayerUnlessEnemyProtected(x, y + 1);
4717 else if (smashed == EL_PENGUIN)
4719 if (CAN_SMASH_PLAYER(element))
4725 else if (element == EL_BD_DIAMOND)
4727 if (IS_CLASSIC_ENEMY(smashed) && IS_BD_ELEMENT(smashed))
4733 else if (((element == EL_SP_INFOTRON ||
4734 element == EL_SP_ZONK) &&
4735 (smashed == EL_SP_SNIKSNAK ||
4736 smashed == EL_SP_ELECTRON ||
4737 smashed == EL_SP_DISK_ORANGE)) ||
4738 (element == EL_SP_INFOTRON &&
4739 smashed == EL_SP_DISK_YELLOW))
4744 else if (CAN_SMASH_EVERYTHING(element))
4746 if (IS_CLASSIC_ENEMY(smashed) ||
4747 CAN_EXPLODE_SMASHED(smashed))
4752 else if (!IS_MOVING(x, y + 1) && !IS_BLOCKED(x, y + 1))
4754 if (smashed == EL_LAMP ||
4755 smashed == EL_LAMP_ACTIVE)
4760 else if (smashed == EL_NUT)
4762 Feld[x][y + 1] = EL_NUT_BREAKING;
4763 PlayLevelSound(x, y, SND_NUT_BREAKING);
4764 RaiseScoreElement(EL_NUT);
4767 else if (smashed == EL_PEARL)
4769 ResetGfxAnimation(x, y);
4771 Feld[x][y + 1] = EL_PEARL_BREAKING;
4772 PlayLevelSound(x, y, SND_PEARL_BREAKING);
4775 else if (smashed == EL_DIAMOND)
4777 Feld[x][y + 1] = EL_DIAMOND_BREAKING;
4778 PlayLevelSound(x, y, SND_DIAMOND_BREAKING);
4781 else if (IS_BELT_SWITCH(smashed))
4783 ToggleBeltSwitch(x, y + 1);
4785 else if (smashed == EL_SWITCHGATE_SWITCH_UP ||
4786 smashed == EL_SWITCHGATE_SWITCH_DOWN)
4788 ToggleSwitchgateSwitch(x, y + 1);
4790 else if (smashed == EL_LIGHT_SWITCH ||
4791 smashed == EL_LIGHT_SWITCH_ACTIVE)
4793 ToggleLightSwitch(x, y + 1);
4798 TestIfElementSmashesCustomElement(x, y, MV_DOWN);
4801 CheckElementChange(x, y + 1, smashed, element, CE_SMASHED);
4803 CheckElementChangeBySide(x, y + 1, smashed, element,
4804 CE_SWITCHED, CH_SIDE_TOP);
4805 CheckTriggeredElementChangeBySide(x, y + 1, smashed, CE_SWITCH_OF_X,
4811 CheckElementChange(x, y + 1, smashed, element, CE_SMASHED);
4816 /* play sound of magic wall / mill */
4818 (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE ||
4819 Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE))
4821 if (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE)
4822 PlayLevelSound(x, y, SND_MAGIC_WALL_FILLING);
4823 else if (Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE)
4824 PlayLevelSound(x, y, SND_BD_MAGIC_WALL_FILLING);
4829 /* play sound of object that hits the ground */
4830 if (last_line || object_hit)
4831 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
4834 inline static void TurnRoundExt(int x, int y)
4846 { 0, 0 }, { 0, 0 }, { 0, 0 },
4851 int left, right, back;
4855 { MV_DOWN, MV_UP, MV_RIGHT },
4856 { MV_UP, MV_DOWN, MV_LEFT },
4858 { MV_LEFT, MV_RIGHT, MV_DOWN },
4862 { MV_RIGHT, MV_LEFT, MV_UP }
4865 int element = Feld[x][y];
4866 int move_pattern = element_info[element].move_pattern;
4868 int old_move_dir = MovDir[x][y];
4869 int left_dir = turn[old_move_dir].left;
4870 int right_dir = turn[old_move_dir].right;
4871 int back_dir = turn[old_move_dir].back;
4873 int left_dx = move_xy[left_dir].dx, left_dy = move_xy[left_dir].dy;
4874 int right_dx = move_xy[right_dir].dx, right_dy = move_xy[right_dir].dy;
4875 int move_dx = move_xy[old_move_dir].dx, move_dy = move_xy[old_move_dir].dy;
4876 int back_dx = move_xy[back_dir].dx, back_dy = move_xy[back_dir].dy;
4878 int left_x = x + left_dx, left_y = y + left_dy;
4879 int right_x = x + right_dx, right_y = y + right_dy;
4880 int move_x = x + move_dx, move_y = y + move_dy;
4884 if (element == EL_BUG || element == EL_BD_BUTTERFLY)
4886 TestIfBadThingTouchesOtherBadThing(x, y);
4888 if (ENEMY_CAN_ENTER_FIELD(element, right_x, right_y))
4889 MovDir[x][y] = right_dir;
4890 else if (!ENEMY_CAN_ENTER_FIELD(element, move_x, move_y))
4891 MovDir[x][y] = left_dir;
4893 if (element == EL_BUG && MovDir[x][y] != old_move_dir)
4895 else if (element == EL_BD_BUTTERFLY) /* && MovDir[x][y] == left_dir) */
4898 else if (element == EL_SPACESHIP || element == EL_BD_FIREFLY)
4900 TestIfBadThingTouchesOtherBadThing(x, y);
4902 if (ENEMY_CAN_ENTER_FIELD(element, left_x, left_y))
4903 MovDir[x][y] = left_dir;
4904 else if (!ENEMY_CAN_ENTER_FIELD(element, move_x, move_y))
4905 MovDir[x][y] = right_dir;
4907 if (element == EL_SPACESHIP && MovDir[x][y] != old_move_dir)
4909 else if (element == EL_BD_FIREFLY) /* && MovDir[x][y] == right_dir) */
4912 else if (element == EL_SP_SNIKSNAK || element == EL_SP_ELECTRON)
4914 TestIfBadThingTouchesOtherBadThing(x, y);
4916 if (ELEMENT_CAN_ENTER_FIELD_BASE_4(element, left_x, left_y, 0))
4917 MovDir[x][y] = left_dir;
4918 else if (!ELEMENT_CAN_ENTER_FIELD_BASE_4(element, move_x, move_y, 0))
4919 MovDir[x][y] = right_dir;
4921 if (MovDir[x][y] != old_move_dir)
4924 else if (element == EL_YAMYAM)
4926 boolean can_turn_left = YAMYAM_CAN_ENTER_FIELD(element, left_x, left_y);
4927 boolean can_turn_right = YAMYAM_CAN_ENTER_FIELD(element, right_x, right_y);
4929 if (can_turn_left && can_turn_right)
4930 MovDir[x][y] = (RND(3) ? (RND(2) ? left_dir : right_dir) : back_dir);
4931 else if (can_turn_left)
4932 MovDir[x][y] = (RND(2) ? left_dir : back_dir);
4933 else if (can_turn_right)
4934 MovDir[x][y] = (RND(2) ? right_dir : back_dir);
4936 MovDir[x][y] = back_dir;
4938 MovDelay[x][y] = 16 + 16 * RND(3);
4940 else if (element == EL_DARK_YAMYAM)
4942 boolean can_turn_left = DARK_YAMYAM_CAN_ENTER_FIELD(element,
4944 boolean can_turn_right = DARK_YAMYAM_CAN_ENTER_FIELD(element,
4947 if (can_turn_left && can_turn_right)
4948 MovDir[x][y] = (RND(3) ? (RND(2) ? left_dir : right_dir) : back_dir);
4949 else if (can_turn_left)
4950 MovDir[x][y] = (RND(2) ? left_dir : back_dir);
4951 else if (can_turn_right)
4952 MovDir[x][y] = (RND(2) ? right_dir : back_dir);
4954 MovDir[x][y] = back_dir;
4956 MovDelay[x][y] = 16 + 16 * RND(3);
4958 else if (element == EL_PACMAN)
4960 boolean can_turn_left = PACMAN_CAN_ENTER_FIELD(element, left_x, left_y);
4961 boolean can_turn_right = PACMAN_CAN_ENTER_FIELD(element, right_x, right_y);
4963 if (can_turn_left && can_turn_right)
4964 MovDir[x][y] = (RND(3) ? (RND(2) ? left_dir : right_dir) : back_dir);
4965 else if (can_turn_left)
4966 MovDir[x][y] = (RND(2) ? left_dir : back_dir);
4967 else if (can_turn_right)
4968 MovDir[x][y] = (RND(2) ? right_dir : back_dir);
4970 MovDir[x][y] = back_dir;
4972 MovDelay[x][y] = 6 + RND(40);
4974 else if (element == EL_PIG)
4976 boolean can_turn_left = PIG_CAN_ENTER_FIELD(element, left_x, left_y);
4977 boolean can_turn_right = PIG_CAN_ENTER_FIELD(element, right_x, right_y);
4978 boolean can_move_on = PIG_CAN_ENTER_FIELD(element, move_x, move_y);
4979 boolean should_turn_left, should_turn_right, should_move_on;
4981 int rnd = RND(rnd_value);
4983 should_turn_left = (can_turn_left &&
4985 IN_LEV_FIELD_AND_NOT_FREE(x + back_dx + left_dx,
4986 y + back_dy + left_dy)));
4987 should_turn_right = (can_turn_right &&
4989 IN_LEV_FIELD_AND_NOT_FREE(x + back_dx + right_dx,
4990 y + back_dy + right_dy)));
4991 should_move_on = (can_move_on &&
4994 IN_LEV_FIELD_AND_NOT_FREE(x + move_dx + left_dx,
4995 y + move_dy + left_dy) ||
4996 IN_LEV_FIELD_AND_NOT_FREE(x + move_dx + right_dx,
4997 y + move_dy + right_dy)));
4999 if (should_turn_left || should_turn_right || should_move_on)
5001 if (should_turn_left && should_turn_right && should_move_on)
5002 MovDir[x][y] = (rnd < rnd_value / 3 ? left_dir :
5003 rnd < 2 * rnd_value / 3 ? right_dir :
5005 else if (should_turn_left && should_turn_right)
5006 MovDir[x][y] = (rnd < rnd_value / 2 ? left_dir : right_dir);
5007 else if (should_turn_left && should_move_on)
5008 MovDir[x][y] = (rnd < rnd_value / 2 ? left_dir : old_move_dir);
5009 else if (should_turn_right && should_move_on)
5010 MovDir[x][y] = (rnd < rnd_value / 2 ? right_dir : old_move_dir);
5011 else if (should_turn_left)
5012 MovDir[x][y] = left_dir;
5013 else if (should_turn_right)
5014 MovDir[x][y] = right_dir;
5015 else if (should_move_on)
5016 MovDir[x][y] = old_move_dir;
5018 else if (can_move_on && rnd > rnd_value / 8)
5019 MovDir[x][y] = old_move_dir;
5020 else if (can_turn_left && can_turn_right)
5021 MovDir[x][y] = (rnd < rnd_value / 2 ? left_dir : right_dir);
5022 else if (can_turn_left && rnd > rnd_value / 8)
5023 MovDir[x][y] = left_dir;
5024 else if (can_turn_right && rnd > rnd_value/8)
5025 MovDir[x][y] = right_dir;
5027 MovDir[x][y] = back_dir;
5029 xx = x + move_xy[MovDir[x][y]].dx;
5030 yy = y + move_xy[MovDir[x][y]].dy;
5032 if (!IN_LEV_FIELD(xx, yy) ||
5033 (!IS_FREE(xx, yy) && !IS_FOOD_PIG(Feld[xx][yy])))
5034 MovDir[x][y] = old_move_dir;
5038 else if (element == EL_DRAGON)
5040 boolean can_turn_left = DRAGON_CAN_ENTER_FIELD(element, left_x, left_y);
5041 boolean can_turn_right = DRAGON_CAN_ENTER_FIELD(element, right_x, right_y);
5042 boolean can_move_on = DRAGON_CAN_ENTER_FIELD(element, move_x, move_y);
5044 int rnd = RND(rnd_value);
5046 if (can_move_on && rnd > rnd_value / 8)
5047 MovDir[x][y] = old_move_dir;
5048 else if (can_turn_left && can_turn_right)
5049 MovDir[x][y] = (rnd < rnd_value / 2 ? left_dir : right_dir);
5050 else if (can_turn_left && rnd > rnd_value / 8)
5051 MovDir[x][y] = left_dir;
5052 else if (can_turn_right && rnd > rnd_value / 8)
5053 MovDir[x][y] = right_dir;
5055 MovDir[x][y] = back_dir;
5057 xx = x + move_xy[MovDir[x][y]].dx;
5058 yy = y + move_xy[MovDir[x][y]].dy;
5060 if (!IN_LEV_FIELD_AND_IS_FREE(xx, yy))
5061 MovDir[x][y] = old_move_dir;
5065 else if (element == EL_MOLE)
5067 boolean can_move_on =
5068 (MOLE_CAN_ENTER_FIELD(element, move_x, move_y,
5069 IS_AMOEBOID(Feld[move_x][move_y]) ||
5070 Feld[move_x][move_y] == EL_AMOEBA_SHRINKING));
5073 boolean can_turn_left =
5074 (MOLE_CAN_ENTER_FIELD(element, left_x, left_y,
5075 IS_AMOEBOID(Feld[left_x][left_y])));
5077 boolean can_turn_right =
5078 (MOLE_CAN_ENTER_FIELD(element, right_x, right_y,
5079 IS_AMOEBOID(Feld[right_x][right_y])));
5081 if (can_turn_left && can_turn_right)
5082 MovDir[x][y] = (RND(2) ? left_dir : right_dir);
5083 else if (can_turn_left)
5084 MovDir[x][y] = left_dir;
5086 MovDir[x][y] = right_dir;
5089 if (MovDir[x][y] != old_move_dir)
5092 else if (element == EL_BALLOON)
5094 MovDir[x][y] = game.wind_direction;
5097 else if (element == EL_SPRING)
5099 #if USE_NEW_SPRING_BUMPER
5100 if (MovDir[x][y] & MV_HORIZONTAL)
5102 if (SPRING_CAN_BUMP_FROM_FIELD(move_x, move_y) &&
5103 !SPRING_CAN_ENTER_FIELD(element, x, y + 1))
5105 Feld[move_x][move_y] = EL_EMC_SPRING_BUMPER_ACTIVE;
5106 ResetGfxAnimation(move_x, move_y);
5107 DrawLevelField(move_x, move_y);
5109 MovDir[x][y] = back_dir;
5111 else if (!SPRING_CAN_ENTER_FIELD(element, move_x, move_y) ||
5112 SPRING_CAN_ENTER_FIELD(element, x, y + 1))
5113 MovDir[x][y] = MV_NONE;
5116 if (MovDir[x][y] & MV_HORIZONTAL &&
5117 (!SPRING_CAN_ENTER_FIELD(element, move_x, move_y) ||
5118 SPRING_CAN_ENTER_FIELD(element, x, y + 1)))
5119 MovDir[x][y] = MV_NONE;
5124 else if (element == EL_ROBOT ||
5125 element == EL_SATELLITE ||
5126 element == EL_PENGUIN ||
5127 element == EL_EMC_ANDROID)
5129 int attr_x = -1, attr_y = -1;
5140 for (i = 0; i < MAX_PLAYERS; i++)
5142 struct PlayerInfo *player = &stored_player[i];
5143 int jx = player->jx, jy = player->jy;
5145 if (!player->active)
5149 ABS(jx - x) + ABS(jy - y) < ABS(attr_x - x) + ABS(attr_y - y))
5157 if (element == EL_ROBOT && ZX >= 0 && ZY >= 0 &&
5158 (Feld[ZX][ZY] == EL_ROBOT_WHEEL_ACTIVE ||
5159 game.engine_version < VERSION_IDENT(3,1,0,0)))
5165 if (element == EL_PENGUIN)
5168 static int xy[4][2] =
5176 for (i = 0; i < NUM_DIRECTIONS; i++)
5178 int ex = x + xy[i][0];
5179 int ey = y + xy[i][1];
5181 if (IN_LEV_FIELD(ex, ey) && Feld[ex][ey] == EL_EXIT_OPEN)
5190 MovDir[x][y] = MV_NONE;
5192 MovDir[x][y] |= (AllPlayersGone ? MV_RIGHT : MV_LEFT);
5193 else if (attr_x > x)
5194 MovDir[x][y] |= (AllPlayersGone ? MV_LEFT : MV_RIGHT);
5196 MovDir[x][y] |= (AllPlayersGone ? MV_DOWN : MV_UP);
5197 else if (attr_y > y)
5198 MovDir[x][y] |= (AllPlayersGone ? MV_UP : MV_DOWN);
5200 if (element == EL_ROBOT)
5204 if (MovDir[x][y] & MV_HORIZONTAL && MovDir[x][y] & MV_VERTICAL)
5205 MovDir[x][y] &= (RND(2) ? MV_HORIZONTAL : MV_VERTICAL);
5206 Moving2Blocked(x, y, &newx, &newy);
5208 if (IN_LEV_FIELD(newx, newy) && IS_FREE_OR_PLAYER(newx, newy))
5209 MovDelay[x][y] = 8 + 8 * !RND(3);
5211 MovDelay[x][y] = 16;
5213 else if (element == EL_PENGUIN)
5219 if (MovDir[x][y] & MV_HORIZONTAL && MovDir[x][y] & MV_VERTICAL)
5221 boolean first_horiz = RND(2);
5222 int new_move_dir = MovDir[x][y];
5225 new_move_dir & (first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5226 Moving2Blocked(x, y, &newx, &newy);
5228 if (PENGUIN_CAN_ENTER_FIELD(element, newx, newy))
5232 new_move_dir & (!first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5233 Moving2Blocked(x, y, &newx, &newy);
5235 if (PENGUIN_CAN_ENTER_FIELD(element, newx, newy))
5238 MovDir[x][y] = old_move_dir;
5242 else if (element == EL_SATELLITE)
5248 if (MovDir[x][y] & MV_HORIZONTAL && MovDir[x][y] & MV_VERTICAL)
5250 boolean first_horiz = RND(2);
5251 int new_move_dir = MovDir[x][y];
5254 new_move_dir & (first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5255 Moving2Blocked(x, y, &newx, &newy);
5257 if (SATELLITE_CAN_ENTER_FIELD(newx, newy))
5261 new_move_dir & (!first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5262 Moving2Blocked(x, y, &newx, &newy);
5264 if (SATELLITE_CAN_ENTER_FIELD(newx, newy))
5267 MovDir[x][y] = old_move_dir;
5271 else if (element == EL_EMC_ANDROID)
5273 static int check_pos[16] =
5275 -1, /* 0 => (invalid) */
5276 7, /* 1 => MV_LEFT */
5277 3, /* 2 => MV_RIGHT */
5278 -1, /* 3 => (invalid) */
5280 0, /* 5 => MV_LEFT | MV_UP */
5281 2, /* 6 => MV_RIGHT | MV_UP */
5282 -1, /* 7 => (invalid) */
5283 5, /* 8 => MV_DOWN */
5284 6, /* 9 => MV_LEFT | MV_DOWN */
5285 4, /* 10 => MV_RIGHT | MV_DOWN */
5286 -1, /* 11 => (invalid) */
5287 -1, /* 12 => (invalid) */
5288 -1, /* 13 => (invalid) */
5289 -1, /* 14 => (invalid) */
5290 -1, /* 15 => (invalid) */
5298 { -1, -1, MV_LEFT | MV_UP },
5300 { +1, -1, MV_RIGHT | MV_UP },
5301 { +1, 0, MV_RIGHT },
5302 { +1, +1, MV_RIGHT | MV_DOWN },
5304 { -1, +1, MV_LEFT | MV_DOWN },
5307 int start_pos, check_order;
5308 boolean can_clone = FALSE;
5311 /* check if there is any free field around current position */
5312 for (i = 0; i < 8; i++)
5314 int newx = x + check_xy[i].dx;
5315 int newy = y + check_xy[i].dy;
5317 if (IN_LEV_FIELD_AND_IS_FREE(newx, newy))
5325 if (can_clone) /* randomly find an element to clone */
5329 start_pos = check_pos[RND(8)];
5330 check_order = (RND(2) ? -1 : +1);
5332 for (i = 0; i < 8; i++)
5334 int pos_raw = start_pos + i * check_order;
5335 int pos = (pos_raw + 8) % 8;
5336 int newx = x + check_xy[pos].dx;
5337 int newy = y + check_xy[pos].dy;
5339 if (ANDROID_CAN_CLONE_FIELD(newx, newy))
5341 element_info[element].move_leave_type = LEAVE_TYPE_LIMITED;
5342 element_info[element].move_leave_element = EL_TRIGGER_ELEMENT;
5344 Store[x][y] = Feld[newx][newy];
5353 if (can_clone) /* randomly find a direction to move */
5357 start_pos = check_pos[RND(8)];
5358 check_order = (RND(2) ? -1 : +1);
5360 for (i = 0; i < 8; i++)
5362 int pos_raw = start_pos + i * check_order;
5363 int pos = (pos_raw + 8) % 8;
5364 int newx = x + check_xy[pos].dx;
5365 int newy = y + check_xy[pos].dy;
5366 int new_move_dir = check_xy[pos].dir;
5368 if (IN_LEV_FIELD_AND_IS_FREE(newx, newy))
5370 MovDir[x][y] = new_move_dir;
5371 MovDelay[x][y] = level.android_clone_time * 8 + 1;
5380 if (can_clone) /* cloning and moving successful */
5383 /* cannot clone -- try to move towards player */
5385 start_pos = check_pos[MovDir[x][y] & 0x0f];
5386 check_order = (RND(2) ? -1 : +1);
5388 for (i = 0; i < 3; i++)
5390 /* first check start_pos, then previous/next or (next/previous) pos */
5391 int pos_raw = start_pos + (i < 2 ? i : -1) * check_order;
5392 int pos = (pos_raw + 8) % 8;
5393 int newx = x + check_xy[pos].dx;
5394 int newy = y + check_xy[pos].dy;
5395 int new_move_dir = check_xy[pos].dir;
5397 if (IS_PLAYER(newx, newy))
5400 if (ANDROID_CAN_ENTER_FIELD(element, newx, newy))
5402 MovDir[x][y] = new_move_dir;
5403 MovDelay[x][y] = level.android_move_time * 8 + 1;
5410 else if (move_pattern == MV_TURNING_LEFT ||
5411 move_pattern == MV_TURNING_RIGHT ||
5412 move_pattern == MV_TURNING_LEFT_RIGHT ||
5413 move_pattern == MV_TURNING_RIGHT_LEFT ||
5414 move_pattern == MV_TURNING_RANDOM ||
5415 move_pattern == MV_ALL_DIRECTIONS)
5417 boolean can_turn_left =
5418 CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, left_x, left_y);
5419 boolean can_turn_right =
5420 CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, right_x,right_y);
5422 if (element_info[element].move_stepsize == 0) /* "not moving" */
5425 if (move_pattern == MV_TURNING_LEFT)
5426 MovDir[x][y] = left_dir;
5427 else if (move_pattern == MV_TURNING_RIGHT)
5428 MovDir[x][y] = right_dir;
5429 else if (move_pattern == MV_TURNING_LEFT_RIGHT)
5430 MovDir[x][y] = (can_turn_left || !can_turn_right ? left_dir : right_dir);
5431 else if (move_pattern == MV_TURNING_RIGHT_LEFT)
5432 MovDir[x][y] = (can_turn_right || !can_turn_left ? right_dir : left_dir);
5433 else if (move_pattern == MV_TURNING_RANDOM)
5434 MovDir[x][y] = (can_turn_left && !can_turn_right ? left_dir :
5435 can_turn_right && !can_turn_left ? right_dir :
5436 RND(2) ? left_dir : right_dir);
5437 else if (can_turn_left && can_turn_right)
5438 MovDir[x][y] = (RND(3) ? (RND(2) ? left_dir : right_dir) : back_dir);
5439 else if (can_turn_left)
5440 MovDir[x][y] = (RND(2) ? left_dir : back_dir);
5441 else if (can_turn_right)
5442 MovDir[x][y] = (RND(2) ? right_dir : back_dir);
5444 MovDir[x][y] = back_dir;
5446 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5448 else if (move_pattern == MV_HORIZONTAL ||
5449 move_pattern == MV_VERTICAL)
5451 if (move_pattern & old_move_dir)
5452 MovDir[x][y] = back_dir;
5453 else if (move_pattern == MV_HORIZONTAL)
5454 MovDir[x][y] = (RND(2) ? MV_LEFT : MV_RIGHT);
5455 else if (move_pattern == MV_VERTICAL)
5456 MovDir[x][y] = (RND(2) ? MV_UP : MV_DOWN);
5458 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5460 else if (move_pattern & MV_ANY_DIRECTION)
5462 MovDir[x][y] = move_pattern;
5463 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5465 else if (move_pattern & MV_WIND_DIRECTION)
5467 MovDir[x][y] = game.wind_direction;
5468 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5470 else if (move_pattern == MV_ALONG_LEFT_SIDE)
5472 if (CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, left_x, left_y))
5473 MovDir[x][y] = left_dir;
5474 else if (!CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, move_x, move_y))
5475 MovDir[x][y] = right_dir;
5477 if (MovDir[x][y] != old_move_dir)
5478 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5480 else if (move_pattern == MV_ALONG_RIGHT_SIDE)
5482 if (CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, right_x, right_y))
5483 MovDir[x][y] = right_dir;
5484 else if (!CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, move_x, move_y))
5485 MovDir[x][y] = left_dir;
5487 if (MovDir[x][y] != old_move_dir)
5488 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5490 else if (move_pattern == MV_TOWARDS_PLAYER ||
5491 move_pattern == MV_AWAY_FROM_PLAYER)
5493 int attr_x = -1, attr_y = -1;
5495 boolean move_away = (move_pattern == MV_AWAY_FROM_PLAYER);
5506 for (i = 0; i < MAX_PLAYERS; i++)
5508 struct PlayerInfo *player = &stored_player[i];
5509 int jx = player->jx, jy = player->jy;
5511 if (!player->active)
5515 ABS(jx - x) + ABS(jy - y) < ABS(attr_x - x) + ABS(attr_y - y))
5523 MovDir[x][y] = MV_NONE;
5525 MovDir[x][y] |= (move_away ? MV_RIGHT : MV_LEFT);
5526 else if (attr_x > x)
5527 MovDir[x][y] |= (move_away ? MV_LEFT : MV_RIGHT);
5529 MovDir[x][y] |= (move_away ? MV_DOWN : MV_UP);
5530 else if (attr_y > y)
5531 MovDir[x][y] |= (move_away ? MV_UP : MV_DOWN);
5533 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5535 if (MovDir[x][y] & MV_HORIZONTAL && MovDir[x][y] & MV_VERTICAL)
5537 boolean first_horiz = RND(2);
5538 int new_move_dir = MovDir[x][y];
5540 if (element_info[element].move_stepsize == 0) /* "not moving" */
5542 first_horiz = (ABS(attr_x - x) >= ABS(attr_y - y));
5543 MovDir[x][y] &= (first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5549 new_move_dir & (first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5550 Moving2Blocked(x, y, &newx, &newy);
5552 if (CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, newx, newy))
5556 new_move_dir & (!first_horiz ? MV_HORIZONTAL : MV_VERTICAL);
5557 Moving2Blocked(x, y, &newx, &newy);
5559 if (CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, newx, newy))
5562 MovDir[x][y] = old_move_dir;
5565 else if (move_pattern == MV_WHEN_PUSHED ||
5566 move_pattern == MV_WHEN_DROPPED)
5568 if (!CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, move_x, move_y))
5569 MovDir[x][y] = MV_NONE;
5573 else if (move_pattern & MV_MAZE_RUNNER_STYLE)
5575 static int test_xy[7][2] =
5585 static int test_dir[7] =
5595 boolean hunter_mode = (move_pattern == MV_MAZE_HUNTER);
5596 int move_preference = -1000000; /* start with very low preference */
5597 int new_move_dir = MV_NONE;
5598 int start_test = RND(4);
5601 for (i = 0; i < NUM_DIRECTIONS; i++)
5603 int move_dir = test_dir[start_test + i];
5604 int move_dir_preference;
5606 xx = x + test_xy[start_test + i][0];
5607 yy = y + test_xy[start_test + i][1];
5609 if (hunter_mode && IN_LEV_FIELD(xx, yy) &&
5610 (IS_PLAYER(xx, yy) || Feld[xx][yy] == EL_PLAYER_IS_LEAVING))
5612 new_move_dir = move_dir;
5617 if (!CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, xx, yy))
5620 move_dir_preference = -1 * RunnerVisit[xx][yy];
5621 if (hunter_mode && PlayerVisit[xx][yy] > 0)
5622 move_dir_preference = PlayerVisit[xx][yy];
5624 if (move_dir_preference > move_preference)
5626 /* prefer field that has not been visited for the longest time */
5627 move_preference = move_dir_preference;
5628 new_move_dir = move_dir;
5630 else if (move_dir_preference == move_preference &&
5631 move_dir == old_move_dir)
5633 /* prefer last direction when all directions are preferred equally */
5634 move_preference = move_dir_preference;
5635 new_move_dir = move_dir;
5639 MovDir[x][y] = new_move_dir;
5640 if (old_move_dir != new_move_dir)
5641 MovDelay[x][y] = GET_NEW_MOVE_DELAY(element);
5645 static void TurnRound(int x, int y)
5647 int direction = MovDir[x][y];
5649 int element, graphic;
5654 GfxDir[x][y] = MovDir[x][y];
5656 if (direction != MovDir[x][y])
5660 GfxAction[x][y] = ACTION_TURNING_FROM_LEFT + MV_DIR_TO_BIT(direction);
5663 element = Feld[x][y];
5664 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
5666 if (graphic_info[graphic].anim_global_sync)
5667 GfxFrame[x][y] = FrameCounter;
5668 else if (ANIM_MODE(graphic) == ANIM_CE_VALUE)
5669 GfxFrame[x][y] = CustomValue[x][y];
5670 else if (ANIM_MODE(graphic) == ANIM_CE_SCORE)
5671 GfxFrame[x][y] = element_info[element].collect_score;
5675 static boolean JustBeingPushed(int x, int y)
5679 for (i = 0; i < MAX_PLAYERS; i++)
5681 struct PlayerInfo *player = &stored_player[i];
5683 if (player->active && player->is_pushing && player->MovPos)
5685 int next_jx = player->jx + (player->jx - player->last_jx);
5686 int next_jy = player->jy + (player->jy - player->last_jy);
5688 if (x == next_jx && y == next_jy)
5696 void StartMoving(int x, int y)
5698 boolean started_moving = FALSE; /* some elements can fall _and_ move */
5699 int element = Feld[x][y];
5704 if (MovDelay[x][y] == 0)
5705 GfxAction[x][y] = ACTION_DEFAULT;
5707 if (CAN_FALL(element) && y < lev_fieldy - 1)
5709 if ((x > 0 && IS_PLAYER(x - 1, y)) ||
5710 (x < lev_fieldx - 1 && IS_PLAYER(x + 1, y)))
5711 if (JustBeingPushed(x, y))
5714 if (element == EL_QUICKSAND_FULL)
5716 if (IS_FREE(x, y + 1))
5718 InitMovingField(x, y, MV_DOWN);
5719 started_moving = TRUE;
5721 Feld[x][y] = EL_QUICKSAND_EMPTYING;
5722 Store[x][y] = EL_ROCK;
5724 PlayLevelSoundAction(x, y, ACTION_EMPTYING);
5726 else if (Feld[x][y + 1] == EL_QUICKSAND_EMPTY)
5728 if (!MovDelay[x][y])
5729 MovDelay[x][y] = TILEY + 1;
5738 Feld[x][y] = EL_QUICKSAND_EMPTY;
5739 Feld[x][y + 1] = EL_QUICKSAND_FULL;
5740 Store[x][y + 1] = Store[x][y];
5743 PlayLevelSoundAction(x, y, ACTION_FILLING);
5746 else if ((element == EL_ROCK || element == EL_BD_ROCK) &&
5747 Feld[x][y + 1] == EL_QUICKSAND_EMPTY)
5749 InitMovingField(x, y, MV_DOWN);
5750 started_moving = TRUE;
5752 Feld[x][y] = EL_QUICKSAND_FILLING;
5753 Store[x][y] = element;
5755 PlayLevelSoundAction(x, y, ACTION_FILLING);
5757 else if (element == EL_MAGIC_WALL_FULL)
5759 if (IS_FREE(x, y + 1))
5761 InitMovingField(x, y, MV_DOWN);
5762 started_moving = TRUE;
5764 Feld[x][y] = EL_MAGIC_WALL_EMPTYING;
5765 Store[x][y] = EL_CHANGED(Store[x][y]);
5767 else if (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE)
5769 if (!MovDelay[x][y])
5770 MovDelay[x][y] = TILEY/4 + 1;
5779 Feld[x][y] = EL_MAGIC_WALL_ACTIVE;
5780 Feld[x][y + 1] = EL_MAGIC_WALL_FULL;
5781 Store[x][y + 1] = EL_CHANGED(Store[x][y]);
5785 else if (element == EL_BD_MAGIC_WALL_FULL)
5787 if (IS_FREE(x, y + 1))
5789 InitMovingField(x, y, MV_DOWN);
5790 started_moving = TRUE;
5792 Feld[x][y] = EL_BD_MAGIC_WALL_EMPTYING;
5793 Store[x][y] = EL_CHANGED2(Store[x][y]);
5795 else if (Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE)
5797 if (!MovDelay[x][y])
5798 MovDelay[x][y] = TILEY/4 + 1;
5807 Feld[x][y] = EL_BD_MAGIC_WALL_ACTIVE;
5808 Feld[x][y + 1] = EL_BD_MAGIC_WALL_FULL;
5809 Store[x][y + 1] = EL_CHANGED2(Store[x][y]);
5813 else if (CAN_PASS_MAGIC_WALL(element) &&
5814 (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE ||
5815 Feld[x][y + 1] == EL_BD_MAGIC_WALL_ACTIVE))
5817 InitMovingField(x, y, MV_DOWN);
5818 started_moving = TRUE;
5821 (Feld[x][y + 1] == EL_MAGIC_WALL_ACTIVE ? EL_MAGIC_WALL_FILLING :
5822 EL_BD_MAGIC_WALL_FILLING);
5823 Store[x][y] = element;
5825 else if (CAN_FALL(element) && Feld[x][y + 1] == EL_ACID)
5827 SplashAcid(x, y + 1);
5829 InitMovingField(x, y, MV_DOWN);
5830 started_moving = TRUE;
5832 Store[x][y] = EL_ACID;
5834 else if ((game.engine_version >= VERSION_IDENT(3,1,0,0) &&
5835 CheckCollision[x][y] && !IS_FREE(x, y + 1)) ||
5837 (game.engine_version >= VERSION_IDENT(3,0,7,0) &&
5838 CAN_FALL(element) && WasJustFalling[x][y] &&
5839 (Feld[x][y + 1] == EL_BLOCKED || IS_PLAYER(x, y + 1))) ||
5841 (game.engine_version < VERSION_IDENT(2,2,0,7) &&
5842 CAN_FALL(element) && WasJustMoving[x][y] && !Pushed[x][y + 1] &&
5843 (Feld[x][y + 1] == EL_BLOCKED)))
5845 /* this is needed for a special case not covered by calling "Impact()"
5846 from "ContinueMoving()": if an element moves to a tile directly below
5847 another element which was just falling on that tile (which was empty
5848 in the previous frame), the falling element above would just stop
5849 instead of smashing the element below (in previous version, the above
5850 element was just checked for "moving" instead of "falling", resulting
5851 in incorrect smashes caused by horizontal movement of the above
5852 element; also, the case of the player being the element to smash was
5853 simply not covered here... :-/ ) */
5855 CheckCollision[x][y] = 0;
5859 else if (IS_FREE(x, y + 1) && element == EL_SPRING && level.use_spring_bug)
5861 if (MovDir[x][y] == MV_NONE)
5863 InitMovingField(x, y, MV_DOWN);
5864 started_moving = TRUE;
5867 else if (IS_FREE(x, y + 1) || Feld[x][y + 1] == EL_DIAMOND_BREAKING)
5869 if (WasJustFalling[x][y]) /* prevent animation from being restarted */
5870 MovDir[x][y] = MV_DOWN;
5872 InitMovingField(x, y, MV_DOWN);
5873 started_moving = TRUE;
5875 else if (element == EL_AMOEBA_DROP)
5877 Feld[x][y] = EL_AMOEBA_GROWING;
5878 Store[x][y] = EL_AMOEBA_WET;
5880 else if (((IS_SLIPPERY(Feld[x][y + 1]) && !IS_PLAYER(x, y + 1)) ||
5881 (IS_EM_SLIPPERY_WALL(Feld[x][y + 1]) && IS_GEM(element))) &&
5882 !IS_FALLING(x, y + 1) && !WasJustMoving[x][y + 1] &&
5883 element != EL_DX_SUPABOMB && element != EL_SP_DISK_ORANGE)
5885 boolean can_fall_left = (x > 0 && IS_FREE(x - 1, y) &&
5886 (IS_FREE(x - 1, y + 1) ||
5887 Feld[x - 1][y + 1] == EL_ACID));
5888 boolean can_fall_right = (x < lev_fieldx - 1 && IS_FREE(x + 1, y) &&
5889 (IS_FREE(x + 1, y + 1) ||
5890 Feld[x + 1][y + 1] == EL_ACID));
5891 boolean can_fall_any = (can_fall_left || can_fall_right);
5892 boolean can_fall_both = (can_fall_left && can_fall_right);
5893 int slippery_type = element_info[Feld[x][y + 1]].slippery_type;
5895 #if USE_NEW_ALL_SLIPPERY
5896 if (can_fall_any && slippery_type != SLIPPERY_ANY_RANDOM)
5898 if (slippery_type == SLIPPERY_ANY_LEFT_RIGHT && can_fall_both)
5899 can_fall_right = FALSE;
5900 else if (slippery_type == SLIPPERY_ANY_RIGHT_LEFT && can_fall_both)
5901 can_fall_left = FALSE;
5902 else if (slippery_type == SLIPPERY_ONLY_LEFT)
5903 can_fall_right = FALSE;
5904 else if (slippery_type == SLIPPERY_ONLY_RIGHT)
5905 can_fall_left = FALSE;
5907 can_fall_any = (can_fall_left || can_fall_right);
5908 can_fall_both = FALSE;
5911 if (can_fall_any && IS_CUSTOM_ELEMENT(Feld[x][y + 1]))
5913 if (slippery_type == SLIPPERY_ONLY_LEFT)
5914 can_fall_right = FALSE;
5915 else if (slippery_type == SLIPPERY_ONLY_RIGHT)
5916 can_fall_left = FALSE;
5917 else if (slippery_type == SLIPPERY_ANY_LEFT_RIGHT && can_fall_both)
5918 can_fall_right = FALSE;
5919 else if (slippery_type == SLIPPERY_ANY_RIGHT_LEFT && can_fall_both)
5920 can_fall_left = FALSE;
5922 can_fall_any = (can_fall_left || can_fall_right);
5923 can_fall_both = (can_fall_left && can_fall_right);
5927 #if USE_NEW_ALL_SLIPPERY
5929 #if USE_NEW_SP_SLIPPERY
5930 /* !!! better use the same properties as for custom elements here !!! */
5931 else if (game.engine_version >= VERSION_IDENT(3,1,1,0) &&
5932 can_fall_both && IS_SP_ELEMENT(Feld[x][y + 1]))
5934 can_fall_right = FALSE; /* slip down on left side */
5935 can_fall_both = FALSE;
5940 #if USE_NEW_ALL_SLIPPERY
5943 if (element == EL_BD_ROCK || element == EL_BD_DIAMOND)
5944 can_fall_right = FALSE; /* slip down on left side */
5946 can_fall_left = !(can_fall_right = RND(2));
5948 can_fall_both = FALSE;
5953 if (game.emulation == EMU_BOULDERDASH ||
5954 element == EL_BD_ROCK || element == EL_BD_DIAMOND)
5955 can_fall_right = FALSE; /* slip down on left side */
5957 can_fall_left = !(can_fall_right = RND(2));
5959 can_fall_both = FALSE;
5965 /* if not determined otherwise, prefer left side for slipping down */
5966 InitMovingField(x, y, can_fall_left ? MV_LEFT : MV_RIGHT);
5967 started_moving = TRUE;
5971 else if (IS_BELT_ACTIVE(Feld[x][y + 1]) && !CAN_MOVE(element))
5973 else if (IS_BELT_ACTIVE(Feld[x][y + 1]))
5976 boolean left_is_free = (x > 0 && IS_FREE(x - 1, y));
5977 boolean right_is_free = (x < lev_fieldx - 1 && IS_FREE(x + 1, y));
5978 int belt_nr = getBeltNrFromBeltActiveElement(Feld[x][y + 1]);
5979 int belt_dir = game.belt_dir[belt_nr];
5981 if ((belt_dir == MV_LEFT && left_is_free) ||
5982 (belt_dir == MV_RIGHT && right_is_free))
5984 int nextx = (belt_dir == MV_LEFT ? x - 1 : x + 1);
5986 InitMovingField(x, y, belt_dir);
5987 started_moving = TRUE;
5989 Pushed[x][y] = TRUE;
5990 Pushed[nextx][y] = TRUE;
5992 GfxAction[x][y] = ACTION_DEFAULT;
5996 MovDir[x][y] = 0; /* if element was moving, stop it */
6001 /* not "else if" because of elements that can fall and move (EL_SPRING) */
6003 if (CAN_MOVE(element) && !started_moving && MovDir[x][y] != MV_NONE)
6005 if (CAN_MOVE(element) && !started_moving)
6008 int move_pattern = element_info[element].move_pattern;
6013 if (MovDir[x][y] == MV_NONE)
6015 printf("StartMoving(): %d,%d: element %d ['%s'] not moving\n",
6016 x, y, element, element_info[element].token_name);
6017 printf("StartMoving(): This should never happen!\n");
6022 Moving2Blocked(x, y, &newx, &newy);
6024 if (IS_PUSHABLE(element) && JustBeingPushed(x, y))
6027 if (game.engine_version >= VERSION_IDENT(3,1,0,0) &&
6028 CheckCollision[x][y] && !IN_LEV_FIELD_AND_IS_FREE(newx, newy))
6030 WasJustMoving[x][y] = 0;
6031 CheckCollision[x][y] = 0;
6033 TestIfElementHitsCustomElement(x, y, MovDir[x][y]);
6035 if (Feld[x][y] != element) /* element has changed */
6039 if (!MovDelay[x][y]) /* start new movement phase */
6041 /* all objects that can change their move direction after each step
6042 (YAMYAM, DARK_YAMYAM and PACMAN go straight until they hit a wall */
6044 if (element != EL_YAMYAM &&
6045 element != EL_DARK_YAMYAM &&
6046 element != EL_PACMAN &&
6047 !(move_pattern & MV_ANY_DIRECTION) &&
6048 move_pattern != MV_TURNING_LEFT &&
6049 move_pattern != MV_TURNING_RIGHT &&
6050 move_pattern != MV_TURNING_LEFT_RIGHT &&
6051 move_pattern != MV_TURNING_RIGHT_LEFT &&
6052 move_pattern != MV_TURNING_RANDOM)
6056 if (MovDelay[x][y] && (element == EL_BUG ||
6057 element == EL_SPACESHIP ||
6058 element == EL_SP_SNIKSNAK ||
6059 element == EL_SP_ELECTRON ||
6060 element == EL_MOLE))
6061 DrawLevelField(x, y);
6065 if (MovDelay[x][y]) /* wait some time before next movement */
6069 if (element == EL_ROBOT ||
6070 element == EL_YAMYAM ||
6071 element == EL_DARK_YAMYAM)
6073 DrawLevelElementAnimationIfNeeded(x, y, element);
6074 PlayLevelSoundAction(x, y, ACTION_WAITING);
6076 else if (element == EL_SP_ELECTRON)
6077 DrawLevelElementAnimationIfNeeded(x, y, element);
6078 else if (element == EL_DRAGON)
6081 int dir = MovDir[x][y];
6082 int dx = (dir == MV_LEFT ? -1 : dir == MV_RIGHT ? +1 : 0);
6083 int dy = (dir == MV_UP ? -1 : dir == MV_DOWN ? +1 : 0);
6084 int graphic = (dir == MV_LEFT ? IMG_FLAMES_1_LEFT :
6085 dir == MV_RIGHT ? IMG_FLAMES_1_RIGHT :
6086 dir == MV_UP ? IMG_FLAMES_1_UP :
6087 dir == MV_DOWN ? IMG_FLAMES_1_DOWN : IMG_EMPTY);
6088 int frame = getGraphicAnimationFrame(graphic, GfxFrame[x][y]);
6090 GfxAction[x][y] = ACTION_ATTACKING;
6092 if (IS_PLAYER(x, y))
6093 DrawPlayerField(x, y);
6095 DrawLevelField(x, y);
6097 PlayLevelSoundActionIfLoop(x, y, ACTION_ATTACKING);
6099 for (i = 1; i <= 3; i++)
6101 int xx = x + i * dx;
6102 int yy = y + i * dy;
6103 int sx = SCREENX(xx);
6104 int sy = SCREENY(yy);
6105 int flame_graphic = graphic + (i - 1);
6107 if (!IN_LEV_FIELD(xx, yy) || IS_DRAGONFIRE_PROOF(Feld[xx][yy]))
6112 int flamed = MovingOrBlocked2Element(xx, yy);
6116 if (IS_CLASSIC_ENEMY(flamed) || CAN_EXPLODE_BY_DRAGONFIRE(flamed))
6118 else if (IS_MOVING(xx, yy) || IS_BLOCKED(xx, yy))
6119 RemoveMovingField(xx, yy);
6121 RemoveField(xx, yy);
6123 if (IS_CLASSIC_ENEMY(flamed) || CAN_EXPLODE_BY_DRAGONFIRE(flamed))
6126 RemoveMovingField(xx, yy);
6129 ChangeDelay[xx][yy] = 0;
6131 Feld[xx][yy] = EL_FLAMES;
6133 if (IN_SCR_FIELD(sx, sy))
6135 DrawLevelFieldCrumbledSand(xx, yy);
6136 DrawGraphic(sx, sy, flame_graphic, frame);
6141 if (Feld[xx][yy] == EL_FLAMES)
6142 Feld[xx][yy] = EL_EMPTY;
6143 DrawLevelField(xx, yy);
6148 if (MovDelay[x][y]) /* element still has to wait some time */
6150 PlayLevelSoundAction(x, y, ACTION_WAITING);
6156 /* now make next step */
6158 Moving2Blocked(x, y, &newx, &newy); /* get next screen position */
6160 if (DONT_COLLIDE_WITH(element) &&
6161 IN_LEV_FIELD(newx, newy) && IS_PLAYER(newx, newy) &&
6162 !PLAYER_ENEMY_PROTECTED(newx, newy))
6164 TestIfBadThingRunsIntoPlayer(x, y, MovDir[x][y]);
6169 else if (CAN_MOVE_INTO_ACID(element) &&
6170 IN_LEV_FIELD(newx, newy) && Feld[newx][newy] == EL_ACID &&
6171 !IS_MV_DIAGONAL(MovDir[x][y]) &&
6172 (MovDir[x][y] == MV_DOWN ||
6173 game.engine_version >= VERSION_IDENT(3,1,0,0)))
6175 SplashAcid(newx, newy);
6176 Store[x][y] = EL_ACID;
6178 else if (element == EL_PENGUIN && IN_LEV_FIELD(newx, newy))
6180 if (Feld[newx][newy] == EL_EXIT_OPEN)
6183 DrawLevelField(x, y);
6185 PlayLevelSound(newx, newy, SND_PENGUIN_PASSING);
6186 if (IN_SCR_FIELD(SCREENX(newx), SCREENY(newy)))
6187 DrawGraphicThruMask(SCREENX(newx),SCREENY(newy), el2img(element), 0);
6189 local_player->friends_still_needed--;
6190 if (!local_player->friends_still_needed &&
6191 !local_player->GameOver && AllPlayersGone)
6192 local_player->LevelSolved = local_player->GameOver = TRUE;
6196 else if (IS_FOOD_PENGUIN(Feld[newx][newy]))
6198 if (DigField(local_player, x, y, newx, newy, 0,0, DF_DIG) == MP_MOVING)
6199 DrawLevelField(newx, newy);
6201 GfxDir[x][y] = MovDir[x][y] = MV_NONE;
6203 else if (!IS_FREE(newx, newy))
6205 GfxAction[x][y] = ACTION_WAITING;
6207 if (IS_PLAYER(x, y))
6208 DrawPlayerField(x, y);
6210 DrawLevelField(x, y);
6215 else if (element == EL_PIG && IN_LEV_FIELD(newx, newy))
6217 if (IS_FOOD_PIG(Feld[newx][newy]))
6219 if (IS_MOVING(newx, newy))
6220 RemoveMovingField(newx, newy);
6223 Feld[newx][newy] = EL_EMPTY;
6224 DrawLevelField(newx, newy);
6227 PlayLevelSound(x, y, SND_PIG_DIGGING);
6229 else if (!IS_FREE(newx, newy))
6231 if (IS_PLAYER(x, y))
6232 DrawPlayerField(x, y);
6234 DrawLevelField(x, y);
6239 else if (element == EL_EMC_ANDROID && IN_LEV_FIELD(newx, newy))
6241 if (Store[x][y] != EL_EMPTY)
6243 boolean can_clone = FALSE;
6246 /* check if element to clone is still there */
6247 for (yy = y - 1; yy <= y + 1; yy++) for (xx = x - 1; xx <= x + 1; xx++)
6249 if (IN_LEV_FIELD(xx, yy) && Feld[xx][yy] == Store[x][y])
6257 /* cannot clone or target field not free anymore -- do not clone */
6258 if (!can_clone || !ANDROID_CAN_ENTER_FIELD(element, newx, newy))
6259 Store[x][y] = EL_EMPTY;
6262 if (ANDROID_CAN_ENTER_FIELD(element, newx, newy))
6264 if (IS_MV_DIAGONAL(MovDir[x][y]))
6266 int diagonal_move_dir = MovDir[x][y];
6267 int stored = Store[x][y];
6268 int change_delay = 8;
6271 /* android is moving diagonally */
6273 CreateField(x, y, EL_DIAGONAL_SHRINKING);
6275 Store[x][y] = (stored == EL_ACID ? EL_EMPTY : stored);
6276 GfxElement[x][y] = EL_EMC_ANDROID;
6277 GfxAction[x][y] = ACTION_SHRINKING;
6278 GfxDir[x][y] = diagonal_move_dir;
6279 ChangeDelay[x][y] = change_delay;
6281 graphic = el_act_dir2img(GfxElement[x][y], GfxAction[x][y],
6284 DrawLevelGraphicAnimation(x, y, graphic);
6285 PlayLevelSoundAction(x, y, ACTION_SHRINKING);
6287 if (Feld[newx][newy] == EL_ACID)
6289 SplashAcid(newx, newy);
6294 CreateField(newx, newy, EL_DIAGONAL_GROWING);
6296 Store[newx][newy] = EL_EMC_ANDROID;
6297 GfxElement[newx][newy] = EL_EMC_ANDROID;
6298 GfxAction[newx][newy] = ACTION_GROWING;
6299 GfxDir[newx][newy] = diagonal_move_dir;
6300 ChangeDelay[newx][newy] = change_delay;
6302 graphic = el_act_dir2img(GfxElement[newx][newy],
6303 GfxAction[newx][newy], GfxDir[newx][newy]);
6305 DrawLevelGraphicAnimation(newx, newy, graphic);
6306 PlayLevelSoundAction(newx, newy, ACTION_GROWING);
6312 Feld[newx][newy] = EL_EMPTY;
6313 DrawLevelField(newx, newy);
6315 PlayLevelSoundAction(x, y, ACTION_DIGGING);
6318 else if (!IS_FREE(newx, newy))
6321 if (IS_PLAYER(x, y))
6322 DrawPlayerField(x, y);
6324 DrawLevelField(x, y);
6330 else if (IS_CUSTOM_ELEMENT(element) &&
6331 CUSTOM_ELEMENT_CAN_ENTER_FIELD(element, newx, newy))
6333 int new_element = Feld[newx][newy];
6335 if (!IS_FREE(newx, newy))
6337 int action = (IS_DIGGABLE(new_element) ? ACTION_DIGGING :
6338 IS_COLLECTIBLE(new_element) ? ACTION_COLLECTING :
6341 /* no element can dig solid indestructible elements */
6342 if (IS_INDESTRUCTIBLE(new_element) &&
6343 !IS_DIGGABLE(new_element) &&
6344 !IS_COLLECTIBLE(new_element))
6347 if (AmoebaNr[newx][newy] &&
6348 (new_element == EL_AMOEBA_FULL ||
6349 new_element == EL_BD_AMOEBA ||
6350 new_element == EL_AMOEBA_GROWING))
6352 AmoebaCnt[AmoebaNr[newx][newy]]--;
6353 AmoebaCnt2[AmoebaNr[newx][newy]]--;
6356 if (IS_MOVING(newx, newy))
6357 RemoveMovingField(newx, newy);
6360 RemoveField(newx, newy);
6361 DrawLevelField(newx, newy);
6364 /* if digged element was about to explode, prevent the explosion */
6365 ExplodeField[newx][newy] = EX_TYPE_NONE;
6367 PlayLevelSoundAction(x, y, action);
6370 Store[newx][newy] = EL_EMPTY;
6372 /* this makes it possible to leave the removed element again */
6373 if (IS_EQUAL_OR_IN_GROUP(new_element, MOVE_ENTER_EL(element)))
6374 Store[newx][newy] = new_element;
6376 if (IS_EQUAL_OR_IN_GROUP(new_element, MOVE_ENTER_EL(element)))
6378 int move_leave_element = element_info[element].move_leave_element;
6380 /* this makes it possible to leave the removed element again */
6381 Store[newx][newy] = (move_leave_element == EL_TRIGGER_ELEMENT ?
6382 new_element : move_leave_element);
6386 if (move_pattern & MV_MAZE_RUNNER_STYLE)
6388 RunnerVisit[x][y] = FrameCounter;
6389 PlayerVisit[x][y] /= 8; /* expire player visit path */
6392 else if (element == EL_DRAGON && IN_LEV_FIELD(newx, newy))
6394 if (!IS_FREE(newx, newy))
6396 if (IS_PLAYER(x, y))
6397 DrawPlayerField(x, y);
6399 DrawLevelField(x, y);
6405 boolean wanna_flame = !RND(10);
6406 int dx = newx - x, dy = newy - y;
6407 int newx1 = newx + 1 * dx, newy1 = newy + 1 * dy;
6408 int newx2 = newx + 2 * dx, newy2 = newy + 2 * dy;
6409 int element1 = (IN_LEV_FIELD(newx1, newy1) ?
6410 MovingOrBlocked2Element(newx1, newy1) : EL_STEELWALL);
6411 int element2 = (IN_LEV_FIELD(newx2, newy2) ?
6412 MovingOrBlocked2Element(newx2, newy2) : EL_STEELWALL);
6415 IS_CLASSIC_ENEMY(element1) ||
6416 IS_CLASSIC_ENEMY(element2)) &&
6417 element1 != EL_DRAGON && element2 != EL_DRAGON &&
6418 element1 != EL_FLAMES && element2 != EL_FLAMES)
6420 ResetGfxAnimation(x, y);
6421 GfxAction[x][y] = ACTION_ATTACKING;
6423 if (IS_PLAYER(x, y))
6424 DrawPlayerField(x, y);
6426 DrawLevelField(x, y);
6428 PlayLevelSound(x, y, SND_DRAGON_ATTACKING);
6430 MovDelay[x][y] = 50;
6434 RemoveField(newx, newy);
6436 Feld[newx][newy] = EL_FLAMES;
6437 if (IN_LEV_FIELD(newx1, newy1) && Feld[newx1][newy1] == EL_EMPTY)
6440 RemoveField(newx1, newy1);
6442 Feld[newx1][newy1] = EL_FLAMES;
6444 if (IN_LEV_FIELD(newx2, newy2) && Feld[newx2][newy2] == EL_EMPTY)
6447 RemoveField(newx2, newy2);
6449 Feld[newx2][newy2] = EL_FLAMES;
6456 else if (element == EL_YAMYAM && IN_LEV_FIELD(newx, newy) &&
6457 Feld[newx][newy] == EL_DIAMOND)
6459 if (IS_MOVING(newx, newy))
6460 RemoveMovingField(newx, newy);
6463 Feld[newx][newy] = EL_EMPTY;
6464 DrawLevelField(newx, newy);
6467 PlayLevelSound(x, y, SND_YAMYAM_DIGGING);
6469 else if (element == EL_DARK_YAMYAM && IN_LEV_FIELD(newx, newy) &&
6470 IS_FOOD_DARK_YAMYAM(Feld[newx][newy]))
6472 if (AmoebaNr[newx][newy])
6474 AmoebaCnt2[AmoebaNr[newx][newy]]--;
6475 if (Feld[newx][newy] == EL_AMOEBA_FULL ||
6476 Feld[newx][newy] == EL_BD_AMOEBA)
6477 AmoebaCnt[AmoebaNr[newx][newy]]--;
6482 if (IS_MOVING(newx, newy) || IS_BLOCKED(newx, newy))
6484 RemoveMovingField(newx, newy);
6487 if (IS_MOVING(newx, newy))
6489 RemoveMovingField(newx, newy);
6494 Feld[newx][newy] = EL_EMPTY;
6495 DrawLevelField(newx, newy);
6498 PlayLevelSound(x, y, SND_DARK_YAMYAM_DIGGING);
6500 else if ((element == EL_PACMAN || element == EL_MOLE)
6501 && IN_LEV_FIELD(newx, newy) && IS_AMOEBOID(Feld[newx][newy]))
6503 if (AmoebaNr[newx][newy])
6505 AmoebaCnt2[AmoebaNr[newx][newy]]--;
6506 if (Feld[newx][newy] == EL_AMOEBA_FULL ||
6507 Feld[newx][newy] == EL_BD_AMOEBA)
6508 AmoebaCnt[AmoebaNr[newx][newy]]--;
6511 if (element == EL_MOLE)
6513 Feld[newx][newy] = EL_AMOEBA_SHRINKING;
6514 PlayLevelSound(x, y, SND_MOLE_DIGGING);
6516 ResetGfxAnimation(x, y);
6517 GfxAction[x][y] = ACTION_DIGGING;
6518 DrawLevelField(x, y);
6520 MovDelay[newx][newy] = 0; /* start amoeba shrinking delay */
6522 return; /* wait for shrinking amoeba */
6524 else /* element == EL_PACMAN */
6526 Feld[newx][newy] = EL_EMPTY;
6527 DrawLevelField(newx, newy);
6528 PlayLevelSound(x, y, SND_PACMAN_DIGGING);
6531 else if (element == EL_MOLE && IN_LEV_FIELD(newx, newy) &&
6532 (Feld[newx][newy] == EL_AMOEBA_SHRINKING ||
6533 (Feld[newx][newy] == EL_EMPTY && Stop[newx][newy])))
6535 /* wait for shrinking amoeba to completely disappear */
6538 else if (!IN_LEV_FIELD(newx, newy) || !IS_FREE(newx, newy))
6540 /* object was running against a wall */
6545 /* !!! NEW "CE_BLOCKED" STUFF !!! -- DOES NOT WORK YET... !!! */
6546 if (move_pattern & MV_ANY_DIRECTION &&
6547 move_pattern == MovDir[x][y])
6549 int blocking_element =
6550 (IN_LEV_FIELD(newx, newy) ? Feld[newx][newy] : BorderElement);
6552 CheckElementChangeBySide(x, y, element, blocking_element, CE_BLOCKED,
6555 element = Feld[x][y]; /* element might have changed */
6559 if (GFX_ELEMENT(element) != EL_SAND) /* !!! FIX THIS (crumble) !!! */
6560 DrawLevelElementAnimation(x, y, element);
6562 if (DONT_TOUCH(element))
6563 TestIfBadThingTouchesPlayer(x, y);
6568 InitMovingField(x, y, MovDir[x][y]);
6570 PlayLevelSoundAction(x, y, ACTION_MOVING);
6574 ContinueMoving(x, y);
6577 void ContinueMoving(int x, int y)
6579 int element = Feld[x][y];
6580 struct ElementInfo *ei = &element_info[element];
6581 int direction = MovDir[x][y];
6582 int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
6583 int dy = (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
6584 int newx = x + dx, newy = y + dy;
6585 int stored = Store[x][y];
6586 int stored_new = Store[newx][newy];
6587 boolean pushed_by_player = (Pushed[x][y] && IS_PLAYER(x, y));
6588 boolean pushed_by_conveyor = (Pushed[x][y] && !IS_PLAYER(x, y));
6589 boolean last_line = (newy == lev_fieldy - 1);
6591 MovPos[x][y] += getElementMoveStepsize(x, y);
6593 if (pushed_by_player) /* special case: moving object pushed by player */
6594 MovPos[x][y] = SIGN(MovPos[x][y]) * (TILEX - ABS(PLAYERINFO(x,y)->MovPos));
6596 if (ABS(MovPos[x][y]) < TILEX)
6598 DrawLevelField(x, y);
6600 return; /* element is still moving */
6603 /* element reached destination field */
6605 Feld[x][y] = EL_EMPTY;
6606 Feld[newx][newy] = element;
6607 MovPos[x][y] = 0; /* force "not moving" for "crumbled sand" */
6609 if (Store[x][y] == EL_ACID) /* element is moving into acid pool */
6611 element = Feld[newx][newy] = EL_ACID;
6613 else if (element == EL_MOLE)
6615 Feld[x][y] = EL_SAND;
6617 DrawLevelFieldCrumbledSandNeighbours(x, y);
6619 else if (element == EL_QUICKSAND_FILLING)
6621 element = Feld[newx][newy] = get_next_element(element);
6622 Store[newx][newy] = Store[x][y];
6624 else if (element == EL_QUICKSAND_EMPTYING)
6626 Feld[x][y] = get_next_element(element);
6627 element = Feld[newx][newy] = Store[x][y];
6629 else if (element == EL_MAGIC_WALL_FILLING)
6631 element = Feld[newx][newy] = get_next_element(element);
6632 if (!game.magic_wall_active)
6633 element = Feld[newx][newy] = EL_MAGIC_WALL_DEAD;
6634 Store[newx][newy] = Store[x][y];
6636 else if (element == EL_MAGIC_WALL_EMPTYING)
6638 Feld[x][y] = get_next_element(element);
6639 if (!game.magic_wall_active)
6640 Feld[x][y] = EL_MAGIC_WALL_DEAD;
6641 element = Feld[newx][newy] = Store[x][y];
6643 #if USE_NEW_CUSTOM_VALUE
6644 InitField(newx, newy, FALSE);
6647 else if (element == EL_BD_MAGIC_WALL_FILLING)
6649 element = Feld[newx][newy] = get_next_element(element);
6650 if (!game.magic_wall_active)
6651 element = Feld[newx][newy] = EL_BD_MAGIC_WALL_DEAD;
6652 Store[newx][newy] = Store[x][y];
6654 else if (element == EL_BD_MAGIC_WALL_EMPTYING)
6656 Feld[x][y] = get_next_element(element);
6657 if (!game.magic_wall_active)
6658 Feld[x][y] = EL_BD_MAGIC_WALL_DEAD;
6659 element = Feld[newx][newy] = Store[x][y];
6661 #if USE_NEW_CUSTOM_VALUE
6662 InitField(newx, newy, FALSE);
6665 else if (element == EL_AMOEBA_DROPPING)
6667 Feld[x][y] = get_next_element(element);
6668 element = Feld[newx][newy] = Store[x][y];
6670 else if (element == EL_SOKOBAN_OBJECT)
6673 Feld[x][y] = Back[x][y];
6675 if (Back[newx][newy])
6676 Feld[newx][newy] = EL_SOKOBAN_FIELD_FULL;
6678 Back[x][y] = Back[newx][newy] = 0;
6681 Store[x][y] = EL_EMPTY;
6686 MovDelay[newx][newy] = 0;
6689 if (CAN_CHANGE_OR_HAS_ACTION(element))
6691 if (CAN_CHANGE(element))
6694 /* copy element change control values to new field */
6695 ChangeDelay[newx][newy] = ChangeDelay[x][y];
6696 ChangePage[newx][newy] = ChangePage[x][y];
6697 ChangeCount[newx][newy] = ChangeCount[x][y];
6698 ChangeEvent[newx][newy] = ChangeEvent[x][y];
6701 #if USE_NEW_CUSTOM_VALUE
6702 CustomValue[newx][newy] = CustomValue[x][y];
6708 #if USE_NEW_CUSTOM_VALUE
6709 CustomValue[newx][newy] = CustomValue[x][y];
6713 ChangeDelay[x][y] = 0;
6714 ChangePage[x][y] = -1;
6715 ChangeCount[x][y] = 0;
6716 ChangeEvent[x][y] = -1;
6718 #if USE_NEW_CUSTOM_VALUE
6719 CustomValue[x][y] = 0;
6722 /* copy animation control values to new field */
6723 GfxFrame[newx][newy] = GfxFrame[x][y];
6724 GfxRandom[newx][newy] = GfxRandom[x][y]; /* keep same random value */
6725 GfxAction[newx][newy] = GfxAction[x][y]; /* keep action one frame */
6726 GfxDir[newx][newy] = GfxDir[x][y]; /* keep element direction */
6728 Pushed[x][y] = Pushed[newx][newy] = FALSE;
6730 /* some elements can leave other elements behind after moving */
6732 if (ei->move_leave_element != EL_EMPTY &&
6733 (ei->move_leave_type == LEAVE_TYPE_UNLIMITED || stored != EL_EMPTY) &&
6734 (!IS_PLAYER(x, y) || IS_WALKABLE(ei->move_leave_element)))
6736 if (IS_CUSTOM_ELEMENT(element) && ei->move_leave_element != EL_EMPTY &&
6737 (ei->move_leave_type == LEAVE_TYPE_UNLIMITED || stored != EL_EMPTY) &&
6738 (!IS_PLAYER(x, y) || IS_WALKABLE(ei->move_leave_element)))
6741 int move_leave_element = ei->move_leave_element;
6745 /* this makes it possible to leave the removed element again */
6746 if (ei->move_leave_element == EL_TRIGGER_ELEMENT)
6747 move_leave_element = (stored == EL_ACID ? EL_EMPTY : stored);
6749 /* this makes it possible to leave the removed element again */
6750 if (ei->move_leave_element == EL_TRIGGER_ELEMENT)
6751 move_leave_element = stored;
6754 /* this makes it possible to leave the removed element again */
6755 if (ei->move_leave_type == LEAVE_TYPE_LIMITED &&
6756 ei->move_leave_element == EL_TRIGGER_ELEMENT)
6757 move_leave_element = stored;
6760 Feld[x][y] = move_leave_element;
6762 if (element_info[Feld[x][y]].move_direction_initial == MV_START_PREVIOUS)
6763 MovDir[x][y] = direction;
6765 InitField(x, y, FALSE);
6767 if (GFX_CRUMBLED(Feld[x][y]))
6768 DrawLevelFieldCrumbledSandNeighbours(x, y);
6770 if (ELEM_IS_PLAYER(move_leave_element))
6771 RelocatePlayer(x, y, move_leave_element);
6774 /* do this after checking for left-behind element */
6775 ResetGfxAnimation(x, y); /* reset animation values for old field */
6777 if (!CAN_MOVE(element) ||
6778 (CAN_FALL(element) && direction == MV_DOWN &&
6779 (element == EL_SPRING ||
6780 element_info[element].move_pattern == MV_WHEN_PUSHED ||
6781 element_info[element].move_pattern == MV_WHEN_DROPPED)))
6782 GfxDir[x][y] = MovDir[newx][newy] = 0;
6784 DrawLevelField(x, y);
6785 DrawLevelField(newx, newy);
6787 Stop[newx][newy] = TRUE; /* ignore this element until the next frame */
6789 /* prevent pushed element from moving on in pushed direction */
6790 if (pushed_by_player && CAN_MOVE(element) &&
6791 element_info[element].move_pattern & MV_ANY_DIRECTION &&
6792 !(element_info[element].move_pattern & direction))
6793 TurnRound(newx, newy);
6795 /* prevent elements on conveyor belt from moving on in last direction */
6796 if (pushed_by_conveyor && CAN_FALL(element) &&
6797 direction & MV_HORIZONTAL)
6798 MovDir[newx][newy] = 0;
6800 if (!pushed_by_player)
6802 int nextx = newx + dx, nexty = newy + dy;
6803 boolean check_collision_again = IN_LEV_FIELD_AND_IS_FREE(nextx, nexty);
6805 WasJustMoving[newx][newy] = CHECK_DELAY_MOVING;
6807 if (CAN_FALL(element) && direction == MV_DOWN)
6808 WasJustFalling[newx][newy] = CHECK_DELAY_FALLING;
6810 if ((!CAN_FALL(element) || direction == MV_DOWN) && check_collision_again)
6811 CheckCollision[newx][newy] = CHECK_DELAY_COLLISION;
6814 if (DONT_TOUCH(element)) /* object may be nasty to player or others */
6816 TestIfBadThingTouchesPlayer(newx, newy);
6817 TestIfBadThingTouchesFriend(newx, newy);
6819 if (!IS_CUSTOM_ELEMENT(element))
6820 TestIfBadThingTouchesOtherBadThing(newx, newy);
6822 else if (element == EL_PENGUIN)
6823 TestIfFriendTouchesBadThing(newx, newy);
6825 /* give the player one last chance (one more frame) to move away */
6826 if (CAN_FALL(element) && direction == MV_DOWN &&
6827 (last_line || (!IS_FREE(x, newy + 1) &&
6828 (!IS_PLAYER(x, newy + 1) ||
6829 game.engine_version < VERSION_IDENT(3,1,1,0)))))
6832 if (pushed_by_player && !game.use_change_when_pushing_bug)
6834 int push_side = MV_DIR_OPPOSITE(direction);
6835 struct PlayerInfo *player = PLAYERINFO(x, y);
6837 CheckElementChangeByPlayer(newx, newy, element, CE_PUSHED_BY_PLAYER,
6838 player->index_bit, push_side);
6839 CheckTriggeredElementChangeByPlayer(newx,newy, element, CE_PLAYER_PUSHES_X,
6840 player->index_bit, push_side);
6843 if (element == EL_EMC_ANDROID && pushed_by_player) /* make another move */
6844 MovDelay[newx][newy] = 1;
6846 CheckTriggeredElementChangeBySide(x, y, element, CE_MOVE_OF_X, direction);
6848 TestIfElementTouchesCustomElement(x, y); /* empty or new element */
6851 if (ChangePage[newx][newy] != -1) /* delayed change */
6853 int page = ChangePage[newx][newy];
6854 struct ElementChangeInfo *change = &ei->change_page[page];
6856 ChangePage[newx][newy] = -1;
6858 if (change->can_change)
6860 if (ChangeElement(newx, newy, element, page))
6862 if (change->post_change_function)
6863 change->post_change_function(newx, newy);
6867 if (change->has_action)
6868 ExecuteCustomElementAction(newx, newy, element, page);
6872 TestIfElementHitsCustomElement(newx, newy, direction);
6873 TestIfPlayerTouchesCustomElement(newx, newy);
6874 TestIfElementTouchesCustomElement(newx, newy);
6877 if (IS_CUSTOM_ELEMENT(element) && ei->move_enter_element != EL_EMPTY &&
6878 IS_EQUAL_OR_IN_GROUP(stored_new, ei->move_enter_element))
6879 CheckElementChangeBySide(newx, newy, element, stored_new, CE_DIGGING_X,
6880 MV_DIR_OPPOSITE(direction));
6884 int AmoebeNachbarNr(int ax, int ay)
6887 int element = Feld[ax][ay];
6889 static int xy[4][2] =
6897 for (i = 0; i < NUM_DIRECTIONS; i++)
6899 int x = ax + xy[i][0];
6900 int y = ay + xy[i][1];
6902 if (!IN_LEV_FIELD(x, y))
6905 if (Feld[x][y] == element && AmoebaNr[x][y] > 0)
6906 group_nr = AmoebaNr[x][y];
6912 void AmoebenVereinigen(int ax, int ay)
6914 int i, x, y, xx, yy;
6915 int new_group_nr = AmoebaNr[ax][ay];
6916 static int xy[4][2] =
6924 if (new_group_nr == 0)
6927 for (i = 0; i < NUM_DIRECTIONS; i++)
6932 if (!IN_LEV_FIELD(x, y))
6935 if ((Feld[x][y] == EL_AMOEBA_FULL ||
6936 Feld[x][y] == EL_BD_AMOEBA ||
6937 Feld[x][y] == EL_AMOEBA_DEAD) &&
6938 AmoebaNr[x][y] != new_group_nr)
6940 int old_group_nr = AmoebaNr[x][y];
6942 if (old_group_nr == 0)
6945 AmoebaCnt[new_group_nr] += AmoebaCnt[old_group_nr];
6946 AmoebaCnt[old_group_nr] = 0;
6947 AmoebaCnt2[new_group_nr] += AmoebaCnt2[old_group_nr];
6948 AmoebaCnt2[old_group_nr] = 0;
6951 SCAN_PLAYFIELD(xx, yy)
6953 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
6956 if (AmoebaNr[xx][yy] == old_group_nr)
6957 AmoebaNr[xx][yy] = new_group_nr;
6963 void AmoebeUmwandeln(int ax, int ay)
6967 if (Feld[ax][ay] == EL_AMOEBA_DEAD)
6969 int group_nr = AmoebaNr[ax][ay];
6974 printf("AmoebeUmwandeln(): ax = %d, ay = %d\n", ax, ay);
6975 printf("AmoebeUmwandeln(): This should never happen!\n");
6981 SCAN_PLAYFIELD(x, y)
6983 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
6986 if (Feld[x][y] == EL_AMOEBA_DEAD && AmoebaNr[x][y] == group_nr)
6989 Feld[x][y] = EL_AMOEBA_TO_DIAMOND;
6993 PlayLevelSound(ax, ay, (IS_GEM(level.amoeba_content) ?
6994 SND_AMOEBA_TURNING_TO_GEM :
6995 SND_AMOEBA_TURNING_TO_ROCK));
7000 static int xy[4][2] =
7008 for (i = 0; i < NUM_DIRECTIONS; i++)
7013 if (!IN_LEV_FIELD(x, y))
7016 if (Feld[x][y] == EL_AMOEBA_TO_DIAMOND)
7018 PlayLevelSound(x, y, (IS_GEM(level.amoeba_content) ?
7019 SND_AMOEBA_TURNING_TO_GEM :
7020 SND_AMOEBA_TURNING_TO_ROCK));
7027 void AmoebeUmwandelnBD(int ax, int ay, int new_element)
7030 int group_nr = AmoebaNr[ax][ay];
7031 boolean done = FALSE;
7036 printf("AmoebeUmwandelnBD(): ax = %d, ay = %d\n", ax, ay);
7037 printf("AmoebeUmwandelnBD(): This should never happen!\n");
7043 SCAN_PLAYFIELD(x, y)
7045 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
7048 if (AmoebaNr[x][y] == group_nr &&
7049 (Feld[x][y] == EL_AMOEBA_DEAD ||
7050 Feld[x][y] == EL_BD_AMOEBA ||
7051 Feld[x][y] == EL_AMOEBA_GROWING))
7054 Feld[x][y] = new_element;
7055 InitField(x, y, FALSE);
7056 DrawLevelField(x, y);
7062 PlayLevelSound(ax, ay, (new_element == EL_BD_ROCK ?
7063 SND_BD_AMOEBA_TURNING_TO_ROCK :
7064 SND_BD_AMOEBA_TURNING_TO_GEM));
7067 void AmoebeWaechst(int x, int y)
7069 static unsigned long sound_delay = 0;
7070 static unsigned long sound_delay_value = 0;
7072 if (!MovDelay[x][y]) /* start new growing cycle */
7076 if (DelayReached(&sound_delay, sound_delay_value))
7078 PlayLevelSoundElementAction(x, y, Store[x][y], ACTION_GROWING);
7079 sound_delay_value = 30;
7083 if (MovDelay[x][y]) /* wait some time before growing bigger */
7086 if (MovDelay[x][y]/2 && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
7088 int frame = getGraphicAnimationFrame(IMG_AMOEBA_GROWING,
7089 6 - MovDelay[x][y]);
7091 DrawGraphic(SCREENX(x), SCREENY(y), IMG_AMOEBA_GROWING, frame);
7094 if (!MovDelay[x][y])
7096 Feld[x][y] = Store[x][y];
7098 DrawLevelField(x, y);
7103 void AmoebaDisappearing(int x, int y)
7105 static unsigned long sound_delay = 0;
7106 static unsigned long sound_delay_value = 0;
7108 if (!MovDelay[x][y]) /* start new shrinking cycle */
7112 if (DelayReached(&sound_delay, sound_delay_value))
7113 sound_delay_value = 30;
7116 if (MovDelay[x][y]) /* wait some time before shrinking */
7119 if (MovDelay[x][y]/2 && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
7121 int frame = getGraphicAnimationFrame(IMG_AMOEBA_SHRINKING,
7122 6 - MovDelay[x][y]);
7124 DrawGraphic(SCREENX(x), SCREENY(y), IMG_AMOEBA_SHRINKING, frame);
7127 if (!MovDelay[x][y])
7129 Feld[x][y] = EL_EMPTY;
7130 DrawLevelField(x, y);
7132 /* don't let mole enter this field in this cycle;
7133 (give priority to objects falling to this field from above) */
7139 void AmoebeAbleger(int ax, int ay)
7142 int element = Feld[ax][ay];
7143 int graphic = el2img(element);
7144 int newax = ax, neway = ay;
7145 boolean can_drop = (element == EL_AMOEBA_WET || element == EL_EMC_DRIPPER);
7146 static int xy[4][2] =
7154 if (!level.amoeba_speed && element != EL_EMC_DRIPPER)
7156 Feld[ax][ay] = EL_AMOEBA_DEAD;
7157 DrawLevelField(ax, ay);
7161 if (IS_ANIMATED(graphic))
7162 DrawLevelGraphicAnimationIfNeeded(ax, ay, graphic);
7164 if (!MovDelay[ax][ay]) /* start making new amoeba field */
7165 MovDelay[ax][ay] = RND(FRAMES_PER_SECOND * 25 / (1 + level.amoeba_speed));
7167 if (MovDelay[ax][ay]) /* wait some time before making new amoeba */
7170 if (MovDelay[ax][ay])
7174 if (can_drop) /* EL_AMOEBA_WET or EL_EMC_DRIPPER */
7177 int x = ax + xy[start][0];
7178 int y = ay + xy[start][1];
7180 if (!IN_LEV_FIELD(x, y))
7183 if (IS_FREE(x, y) ||
7184 CAN_GROW_INTO(Feld[x][y]) ||
7185 Feld[x][y] == EL_QUICKSAND_EMPTY)
7191 if (newax == ax && neway == ay)
7194 else /* normal or "filled" (BD style) amoeba */
7197 boolean waiting_for_player = FALSE;
7199 for (i = 0; i < NUM_DIRECTIONS; i++)
7201 int j = (start + i) % 4;
7202 int x = ax + xy[j][0];
7203 int y = ay + xy[j][1];
7205 if (!IN_LEV_FIELD(x, y))
7208 if (IS_FREE(x, y) ||
7209 CAN_GROW_INTO(Feld[x][y]) ||
7210 Feld[x][y] == EL_QUICKSAND_EMPTY)
7216 else if (IS_PLAYER(x, y))
7217 waiting_for_player = TRUE;
7220 if (newax == ax && neway == ay) /* amoeba cannot grow */
7222 if (i == 4 && (!waiting_for_player || element == EL_BD_AMOEBA))
7224 Feld[ax][ay] = EL_AMOEBA_DEAD;
7225 DrawLevelField(ax, ay);
7226 AmoebaCnt[AmoebaNr[ax][ay]]--;
7228 if (AmoebaCnt[AmoebaNr[ax][ay]] <= 0) /* amoeba is completely dead */
7230 if (element == EL_AMOEBA_FULL)
7231 AmoebeUmwandeln(ax, ay);
7232 else if (element == EL_BD_AMOEBA)
7233 AmoebeUmwandelnBD(ax, ay, level.amoeba_content);
7238 else if (element == EL_AMOEBA_FULL || element == EL_BD_AMOEBA)
7240 /* amoeba gets larger by growing in some direction */
7242 int new_group_nr = AmoebaNr[ax][ay];
7245 if (new_group_nr == 0)
7247 printf("AmoebeAbleger(): newax = %d, neway = %d\n", newax, neway);
7248 printf("AmoebeAbleger(): This should never happen!\n");
7253 AmoebaNr[newax][neway] = new_group_nr;
7254 AmoebaCnt[new_group_nr]++;
7255 AmoebaCnt2[new_group_nr]++;
7257 /* if amoeba touches other amoeba(s) after growing, unify them */
7258 AmoebenVereinigen(newax, neway);
7260 if (element == EL_BD_AMOEBA && AmoebaCnt2[new_group_nr] >= 200)
7262 AmoebeUmwandelnBD(newax, neway, EL_BD_ROCK);
7268 if (!can_drop || neway < ay || !IS_FREE(newax, neway) ||
7269 (neway == lev_fieldy - 1 && newax != ax))
7271 Feld[newax][neway] = EL_AMOEBA_GROWING; /* creation of new amoeba */
7272 Store[newax][neway] = element;
7274 else if (neway == ay || element == EL_EMC_DRIPPER)
7276 Feld[newax][neway] = EL_AMOEBA_DROP; /* drop left/right of amoeba */
7278 PlayLevelSoundAction(newax, neway, ACTION_GROWING);
7282 InitMovingField(ax, ay, MV_DOWN); /* drop dripping from amoeba */
7283 Feld[ax][ay] = EL_AMOEBA_DROPPING;
7284 Store[ax][ay] = EL_AMOEBA_DROP;
7285 ContinueMoving(ax, ay);
7289 DrawLevelField(newax, neway);
7292 void Life(int ax, int ay)
7296 static int life[4] = { 2, 3, 3, 3 }; /* parameters for "game of life" */
7299 int element = Feld[ax][ay];
7300 int graphic = el2img(element);
7301 int *life_parameter = (element == EL_GAME_OF_LIFE ? level.game_of_life :
7303 boolean changed = FALSE;
7305 if (IS_ANIMATED(graphic))
7306 DrawLevelGraphicAnimationIfNeeded(ax, ay, graphic);
7311 if (!MovDelay[ax][ay]) /* start new "game of life" cycle */
7312 MovDelay[ax][ay] = life_time;
7314 if (MovDelay[ax][ay]) /* wait some time before next cycle */
7317 if (MovDelay[ax][ay])
7321 for (y1 = -1; y1 < 2; y1++) for (x1 = -1; x1 < 2; x1++)
7323 int xx = ax+x1, yy = ay+y1;
7326 if (!IN_LEV_FIELD(xx, yy))
7329 for (y2 = -1; y2 < 2; y2++) for (x2 = -1; x2 < 2; x2++)
7331 int x = xx+x2, y = yy+y2;
7333 if (!IN_LEV_FIELD(x, y) || (x == xx && y == yy))
7336 if (((Feld[x][y] == element ||
7337 (element == EL_GAME_OF_LIFE && IS_PLAYER(x, y))) &&
7339 (IS_FREE(x, y) && Stop[x][y]))
7343 if (xx == ax && yy == ay) /* field in the middle */
7345 if (nachbarn < life_parameter[0] ||
7346 nachbarn > life_parameter[1])
7348 Feld[xx][yy] = EL_EMPTY;
7350 DrawLevelField(xx, yy);
7351 Stop[xx][yy] = TRUE;
7355 else if (IS_FREE(xx, yy) || CAN_GROW_INTO(Feld[xx][yy]))
7356 { /* free border field */
7357 if (nachbarn >= life_parameter[2] &&
7358 nachbarn <= life_parameter[3])
7360 Feld[xx][yy] = element;
7361 MovDelay[xx][yy] = (element == EL_GAME_OF_LIFE ? 0 : life_time-1);
7363 DrawLevelField(xx, yy);
7364 Stop[xx][yy] = TRUE;
7371 PlayLevelSound(ax, ay, element == EL_BIOMAZE ? SND_BIOMAZE_GROWING :
7372 SND_GAME_OF_LIFE_GROWING);
7375 static void InitRobotWheel(int x, int y)
7377 ChangeDelay[x][y] = level.time_wheel * FRAMES_PER_SECOND;
7380 static void RunRobotWheel(int x, int y)
7382 PlayLevelSound(x, y, SND_ROBOT_WHEEL_ACTIVE);
7385 static void StopRobotWheel(int x, int y)
7387 if (ZX == x && ZY == y)
7391 static void InitTimegateWheel(int x, int y)
7393 ChangeDelay[x][y] = level.time_timegate * FRAMES_PER_SECOND;
7396 static void RunTimegateWheel(int x, int y)
7398 PlayLevelSound(x, y, SND_TIMEGATE_SWITCH_ACTIVE);
7401 static void InitMagicBallDelay(int x, int y)
7404 ChangeDelay[x][y] = (level.ball_time + 1) * 8 + 1;
7406 ChangeDelay[x][y] = level.ball_time * FRAMES_PER_SECOND + 1;
7410 static void ActivateMagicBall(int bx, int by)
7414 if (level.ball_random)
7416 int pos_border = RND(8); /* select one of the eight border elements */
7417 int pos_content = (pos_border > 3 ? pos_border + 1 : pos_border);
7418 int xx = pos_content % 3;
7419 int yy = pos_content / 3;
7424 if (IN_LEV_FIELD(x, y) && Feld[x][y] == EL_EMPTY)
7425 CreateField(x, y, level.ball_content[game.ball_content_nr].e[xx][yy]);
7429 for (y = by - 1; y <= by + 1; y++) for (x = bx - 1; x <= bx + 1; x++)
7431 int xx = x - bx + 1;
7432 int yy = y - by + 1;
7434 if (IN_LEV_FIELD(x, y) && Feld[x][y] == EL_EMPTY)
7435 CreateField(x, y, level.ball_content[game.ball_content_nr].e[xx][yy]);
7439 game.ball_content_nr = (game.ball_content_nr + 1) % level.num_ball_contents;
7442 static void InitDiagonalMovingElement(int x, int y)
7445 MovDelay[x][y] = level.android_move_time;
7449 void CheckExit(int x, int y)
7451 if (local_player->gems_still_needed > 0 ||
7452 local_player->sokobanfields_still_needed > 0 ||
7453 local_player->lights_still_needed > 0)
7455 int element = Feld[x][y];
7456 int graphic = el2img(element);
7458 if (IS_ANIMATED(graphic))
7459 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
7464 if (AllPlayersGone) /* do not re-open exit door closed after last player */
7467 Feld[x][y] = EL_EXIT_OPENING;
7469 PlayLevelSoundNearest(x, y, SND_CLASS_EXIT_OPENING);
7472 void CheckExitSP(int x, int y)
7474 if (local_player->gems_still_needed > 0)
7476 int element = Feld[x][y];
7477 int graphic = el2img(element);
7479 if (IS_ANIMATED(graphic))
7480 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
7485 if (AllPlayersGone) /* do not re-open exit door closed after last player */
7488 Feld[x][y] = EL_SP_EXIT_OPENING;
7490 PlayLevelSoundNearest(x, y, SND_CLASS_SP_EXIT_OPENING);
7493 static void CloseAllOpenTimegates()
7498 SCAN_PLAYFIELD(x, y)
7500 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
7503 int element = Feld[x][y];
7505 if (element == EL_TIMEGATE_OPEN || element == EL_TIMEGATE_OPENING)
7507 Feld[x][y] = EL_TIMEGATE_CLOSING;
7509 PlayLevelSoundAction(x, y, ACTION_CLOSING);
7514 void EdelsteinFunkeln(int x, int y)
7516 if (!IN_SCR_FIELD(SCREENX(x), SCREENY(y)) || IS_MOVING(x, y))
7519 if (Feld[x][y] == EL_BD_DIAMOND)
7522 if (MovDelay[x][y] == 0) /* next animation frame */
7523 MovDelay[x][y] = 11 * !SimpleRND(500);
7525 if (MovDelay[x][y] != 0) /* wait some time before next frame */
7529 if (setup.direct_draw && MovDelay[x][y])
7530 SetDrawtoField(DRAW_BUFFERED);
7532 DrawLevelElementAnimation(x, y, Feld[x][y]);
7534 if (MovDelay[x][y] != 0)
7536 int frame = getGraphicAnimationFrame(IMG_TWINKLE_WHITE,
7537 10 - MovDelay[x][y]);
7539 DrawGraphicThruMask(SCREENX(x), SCREENY(y), IMG_TWINKLE_WHITE, frame);
7541 if (setup.direct_draw)
7545 dest_x = FX + SCREENX(x) * TILEX;
7546 dest_y = FY + SCREENY(y) * TILEY;
7548 BlitBitmap(drawto_field, window,
7549 dest_x, dest_y, TILEX, TILEY, dest_x, dest_y);
7550 SetDrawtoField(DRAW_DIRECT);
7556 void MauerWaechst(int x, int y)
7560 if (!MovDelay[x][y]) /* next animation frame */
7561 MovDelay[x][y] = 3 * delay;
7563 if (MovDelay[x][y]) /* wait some time before next frame */
7567 if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
7569 int graphic = el_dir2img(Feld[x][y], GfxDir[x][y]);
7570 int frame = getGraphicAnimationFrame(graphic, 17 - MovDelay[x][y]);
7572 DrawGraphic(SCREENX(x), SCREENY(y), graphic, frame);
7575 if (!MovDelay[x][y])
7577 if (MovDir[x][y] == MV_LEFT)
7579 if (IN_LEV_FIELD(x - 1, y) && IS_WALL(Feld[x - 1][y]))
7580 DrawLevelField(x - 1, y);
7582 else if (MovDir[x][y] == MV_RIGHT)
7584 if (IN_LEV_FIELD(x + 1, y) && IS_WALL(Feld[x + 1][y]))
7585 DrawLevelField(x + 1, y);
7587 else if (MovDir[x][y] == MV_UP)
7589 if (IN_LEV_FIELD(x, y - 1) && IS_WALL(Feld[x][y - 1]))
7590 DrawLevelField(x, y - 1);
7594 if (IN_LEV_FIELD(x, y + 1) && IS_WALL(Feld[x][y + 1]))
7595 DrawLevelField(x, y + 1);
7598 Feld[x][y] = Store[x][y];
7600 GfxDir[x][y] = MovDir[x][y] = MV_NONE;
7601 DrawLevelField(x, y);
7606 void MauerAbleger(int ax, int ay)
7608 int element = Feld[ax][ay];
7609 int graphic = el2img(element);
7610 boolean oben_frei = FALSE, unten_frei = FALSE;
7611 boolean links_frei = FALSE, rechts_frei = FALSE;
7612 boolean oben_massiv = FALSE, unten_massiv = FALSE;
7613 boolean links_massiv = FALSE, rechts_massiv = FALSE;
7614 boolean new_wall = FALSE;
7616 if (IS_ANIMATED(graphic))
7617 DrawLevelGraphicAnimationIfNeeded(ax, ay, graphic);
7619 if (!MovDelay[ax][ay]) /* start building new wall */
7620 MovDelay[ax][ay] = 6;
7622 if (MovDelay[ax][ay]) /* wait some time before building new wall */
7625 if (MovDelay[ax][ay])
7629 if (IN_LEV_FIELD(ax, ay-1) && IS_FREE(ax, ay-1))
7631 if (IN_LEV_FIELD(ax, ay+1) && IS_FREE(ax, ay+1))
7633 if (IN_LEV_FIELD(ax-1, ay) && IS_FREE(ax-1, ay))
7635 if (IN_LEV_FIELD(ax+1, ay) && IS_FREE(ax+1, ay))
7638 if (element == EL_EXPANDABLE_WALL_VERTICAL ||
7639 element == EL_EXPANDABLE_WALL_ANY)
7643 Feld[ax][ay-1] = EL_EXPANDABLE_WALL_GROWING;
7644 Store[ax][ay-1] = element;
7645 GfxDir[ax][ay-1] = MovDir[ax][ay-1] = MV_UP;
7646 if (IN_SCR_FIELD(SCREENX(ax), SCREENY(ay-1)))
7647 DrawGraphic(SCREENX(ax), SCREENY(ay - 1),
7648 IMG_EXPANDABLE_WALL_GROWING_UP, 0);
7653 Feld[ax][ay+1] = EL_EXPANDABLE_WALL_GROWING;
7654 Store[ax][ay+1] = element;
7655 GfxDir[ax][ay+1] = MovDir[ax][ay+1] = MV_DOWN;
7656 if (IN_SCR_FIELD(SCREENX(ax), SCREENY(ay+1)))
7657 DrawGraphic(SCREENX(ax), SCREENY(ay + 1),
7658 IMG_EXPANDABLE_WALL_GROWING_DOWN, 0);
7663 if (element == EL_EXPANDABLE_WALL_HORIZONTAL ||
7664 element == EL_EXPANDABLE_WALL_ANY ||
7665 element == EL_EXPANDABLE_WALL)
7669 Feld[ax-1][ay] = EL_EXPANDABLE_WALL_GROWING;
7670 Store[ax-1][ay] = element;
7671 GfxDir[ax-1][ay] = MovDir[ax-1][ay] = MV_LEFT;
7672 if (IN_SCR_FIELD(SCREENX(ax-1), SCREENY(ay)))
7673 DrawGraphic(SCREENX(ax - 1), SCREENY(ay),
7674 IMG_EXPANDABLE_WALL_GROWING_LEFT, 0);
7680 Feld[ax+1][ay] = EL_EXPANDABLE_WALL_GROWING;
7681 Store[ax+1][ay] = element;
7682 GfxDir[ax+1][ay] = MovDir[ax+1][ay] = MV_RIGHT;
7683 if (IN_SCR_FIELD(SCREENX(ax+1), SCREENY(ay)))
7684 DrawGraphic(SCREENX(ax + 1), SCREENY(ay),
7685 IMG_EXPANDABLE_WALL_GROWING_RIGHT, 0);
7690 if (element == EL_EXPANDABLE_WALL && (links_frei || rechts_frei))
7691 DrawLevelField(ax, ay);
7693 if (!IN_LEV_FIELD(ax, ay-1) || IS_WALL(Feld[ax][ay-1]))
7695 if (!IN_LEV_FIELD(ax, ay+1) || IS_WALL(Feld[ax][ay+1]))
7696 unten_massiv = TRUE;
7697 if (!IN_LEV_FIELD(ax-1, ay) || IS_WALL(Feld[ax-1][ay]))
7698 links_massiv = TRUE;
7699 if (!IN_LEV_FIELD(ax+1, ay) || IS_WALL(Feld[ax+1][ay]))
7700 rechts_massiv = TRUE;
7702 if (((oben_massiv && unten_massiv) ||
7703 element == EL_EXPANDABLE_WALL_HORIZONTAL ||
7704 element == EL_EXPANDABLE_WALL) &&
7705 ((links_massiv && rechts_massiv) ||
7706 element == EL_EXPANDABLE_WALL_VERTICAL))
7707 Feld[ax][ay] = EL_WALL;
7710 PlayLevelSoundAction(ax, ay, ACTION_GROWING);
7713 void CheckForDragon(int x, int y)
7716 boolean dragon_found = FALSE;
7717 static int xy[4][2] =
7725 for (i = 0; i < NUM_DIRECTIONS; i++)
7727 for (j = 0; j < 4; j++)
7729 int xx = x + j * xy[i][0], yy = y + j * xy[i][1];
7731 if (IN_LEV_FIELD(xx, yy) &&
7732 (Feld[xx][yy] == EL_FLAMES || Feld[xx][yy] == EL_DRAGON))
7734 if (Feld[xx][yy] == EL_DRAGON)
7735 dragon_found = TRUE;
7744 for (i = 0; i < NUM_DIRECTIONS; i++)
7746 for (j = 0; j < 3; j++)
7748 int xx = x + j * xy[i][0], yy = y + j * xy[i][1];
7750 if (IN_LEV_FIELD(xx, yy) && Feld[xx][yy] == EL_FLAMES)
7752 Feld[xx][yy] = EL_EMPTY;
7753 DrawLevelField(xx, yy);
7762 static void InitBuggyBase(int x, int y)
7764 int element = Feld[x][y];
7765 int activating_delay = FRAMES_PER_SECOND / 4;
7768 (element == EL_SP_BUGGY_BASE ?
7769 2 * FRAMES_PER_SECOND + RND(5 * FRAMES_PER_SECOND) - activating_delay :
7770 element == EL_SP_BUGGY_BASE_ACTIVATING ?
7772 element == EL_SP_BUGGY_BASE_ACTIVE ?
7773 1 * FRAMES_PER_SECOND + RND(1 * FRAMES_PER_SECOND) : 1);
7776 static void WarnBuggyBase(int x, int y)
7779 static int xy[4][2] =
7787 for (i = 0; i < NUM_DIRECTIONS; i++)
7789 int xx = x + xy[i][0];
7790 int yy = y + xy[i][1];
7792 if (IN_LEV_FIELD(xx, yy) && IS_PLAYER(xx, yy))
7794 PlayLevelSound(x, y, SND_SP_BUGGY_BASE_ACTIVE);
7801 static void InitTrap(int x, int y)
7803 ChangeDelay[x][y] = 2 * FRAMES_PER_SECOND + RND(5 * FRAMES_PER_SECOND);
7806 static void ActivateTrap(int x, int y)
7808 PlayLevelSound(x, y, SND_TRAP_ACTIVATING);
7811 static void ChangeActiveTrap(int x, int y)
7813 int graphic = IMG_TRAP_ACTIVE;
7815 /* if new animation frame was drawn, correct crumbled sand border */
7816 if (IS_NEW_FRAME(GfxFrame[x][y], graphic))
7817 DrawLevelFieldCrumbledSand(x, y);
7820 static int getSpecialActionElement(int element, int number, int base_element)
7822 return (element != EL_EMPTY ? element :
7823 number != -1 ? base_element + number - 1 :
7827 static int getModifiedActionNumber(int value_old, int operator, int operand,
7828 int value_min, int value_max)
7830 int value_new = (operator == CA_MODE_SET ? operand :
7831 operator == CA_MODE_ADD ? value_old + operand :
7832 operator == CA_MODE_SUBTRACT ? value_old - operand :
7833 operator == CA_MODE_MULTIPLY ? value_old * operand :
7834 operator == CA_MODE_DIVIDE ? value_old / MAX(1, operand) :
7835 operator == CA_MODE_MODULO ? value_old % MAX(1, operand) :
7838 return (value_new < value_min ? value_min :
7839 value_new > value_max ? value_max :
7843 static void ExecuteCustomElementAction(int x, int y, int element, int page)
7845 struct ElementInfo *ei = &element_info[element];
7846 struct ElementChangeInfo *change = &ei->change_page[page];
7847 int action_type = change->action_type;
7848 int action_mode = change->action_mode;
7849 int action_arg = change->action_arg;
7852 if (!change->has_action)
7855 /* ---------- determine action paramater values -------------------------- */
7857 int level_time_value =
7858 (level.time > 0 ? TimeLeft :
7861 int action_arg_element =
7862 (action_arg == CA_ARG_PLAYER_TRIGGER ? change->actual_trigger_player :
7863 action_arg == CA_ARG_ELEMENT_TRIGGER ? change->actual_trigger_element :
7864 action_arg == CA_ARG_ELEMENT_TARGET ? change->target_element :
7867 int action_arg_direction =
7868 (action_arg >= CA_ARG_DIRECTION_LEFT &&
7869 action_arg <= CA_ARG_DIRECTION_DOWN ? action_arg - CA_ARG_DIRECTION :
7870 action_arg == CA_ARG_DIRECTION_TRIGGER ?
7871 change->actual_trigger_side :
7872 action_arg == CA_ARG_DIRECTION_TRIGGER_BACK ?
7873 MV_DIR_OPPOSITE(change->actual_trigger_side) :
7876 int action_arg_number_min =
7877 (action_type == CA_SET_PLAYER_SPEED ? STEPSIZE_NOT_MOVING :
7880 int action_arg_number_max =
7881 (action_type == CA_SET_PLAYER_SPEED ? STEPSIZE_EVEN_FASTER :
7882 action_type == CA_SET_LEVEL_GEMS ? 999 :
7883 action_type == CA_SET_LEVEL_TIME ? 9999 :
7884 action_type == CA_SET_LEVEL_SCORE ? 99999 :
7885 action_type == CA_SET_CE_SCORE ? 9999 :
7886 action_type == CA_SET_CE_VALUE ? 9999 :
7889 int action_arg_number_reset =
7890 (action_type == CA_SET_PLAYER_SPEED ? level.initial_player_stepsize :
7891 action_type == CA_SET_LEVEL_GEMS ? level.gems_needed :
7892 action_type == CA_SET_LEVEL_TIME ? level.time :
7893 action_type == CA_SET_LEVEL_SCORE ? 0 :
7894 action_type == CA_SET_CE_SCORE ? 0 :
7896 action_type == CA_SET_CE_VALUE ? GET_NEW_CUSTOM_VALUE(element) :
7898 action_type == CA_SET_CE_VALUE ? ei->custom_value_initial :
7902 int action_arg_number =
7903 (action_arg <= CA_ARG_MAX ? action_arg :
7904 action_arg >= CA_ARG_SPEED_NOT_MOVING &&
7905 action_arg <= CA_ARG_SPEED_EVEN_FASTER ? (action_arg - CA_ARG_SPEED) :
7906 action_arg == CA_ARG_SPEED_RESET ? action_arg_number_reset :
7907 action_arg == CA_ARG_NUMBER_MIN ? action_arg_number_min :
7908 action_arg == CA_ARG_NUMBER_MAX ? action_arg_number_max :
7909 action_arg == CA_ARG_NUMBER_RESET ? action_arg_number_reset :
7910 action_arg == CA_ARG_NUMBER_CE_SCORE ? ei->collect_score :
7911 #if USE_NEW_CUSTOM_VALUE
7912 action_arg == CA_ARG_NUMBER_CE_VALUE ? CustomValue[x][y] :
7914 action_arg == CA_ARG_NUMBER_CE_VALUE ? ei->custom_value_initial :
7916 action_arg == CA_ARG_NUMBER_CE_DELAY ? GET_CE_DELAY_VALUE(change) :
7917 action_arg == CA_ARG_NUMBER_LEVEL_TIME ? level_time_value :
7918 action_arg == CA_ARG_NUMBER_LEVEL_GEMS ? local_player->gems_still_needed :
7919 action_arg == CA_ARG_NUMBER_LEVEL_SCORE ? local_player->score :
7920 action_arg == CA_ARG_ELEMENT_CV_TARGET ? GET_NEW_CUSTOM_VALUE(change->target_element) :
7921 action_arg == CA_ARG_ELEMENT_CV_TRIGGER ? change->actual_trigger_ce_value:
7922 action_arg == CA_ARG_ELEMENT_NR_TARGET ? change->target_element :
7923 action_arg == CA_ARG_ELEMENT_NR_TRIGGER ? change->actual_trigger_element :
7926 int action_arg_number_old =
7927 (action_type == CA_SET_LEVEL_GEMS ? local_player->gems_still_needed :
7928 action_type == CA_SET_LEVEL_TIME ? TimeLeft :
7929 action_type == CA_SET_LEVEL_SCORE ? local_player->score :
7930 action_type == CA_SET_CE_SCORE ? ei->collect_score :
7931 action_type == CA_SET_CE_VALUE ? CustomValue[x][y] :
7934 int action_arg_number_new =
7935 getModifiedActionNumber(action_arg_number_old,
7936 action_mode, action_arg_number,
7937 action_arg_number_min, action_arg_number_max);
7939 int trigger_player_bits =
7940 (change->actual_trigger_player >= EL_PLAYER_1 &&
7941 change->actual_trigger_player <= EL_PLAYER_4 ?
7942 (1 << (change->actual_trigger_player - EL_PLAYER_1)) :
7945 int action_arg_player_bits =
7946 (action_arg >= CA_ARG_PLAYER_1 &&
7947 action_arg <= CA_ARG_PLAYER_4 ? action_arg - CA_ARG_PLAYER :
7948 action_arg == CA_ARG_PLAYER_TRIGGER ? trigger_player_bits :
7951 /* ---------- execute action -------------------------------------------- */
7960 /* ---------- level actions ------------------------------------------- */
7962 case CA_RESTART_LEVEL:
7964 game.restart_level = TRUE;
7969 case CA_SHOW_ENVELOPE:
7971 int element = getSpecialActionElement(action_arg_element,
7972 action_arg_number, EL_ENVELOPE_1);
7974 if (IS_ENVELOPE(element))
7975 local_player->show_envelope = element;
7980 case CA_SET_LEVEL_TIME:
7982 if (level.time > 0) /* only modify limited time value */
7984 TimeLeft = action_arg_number_new;
7986 DrawGameValue_Time(TimeLeft);
7988 if (!TimeLeft && setup.time_limit)
7989 for (i = 0; i < MAX_PLAYERS; i++)
7990 KillPlayer(&stored_player[i]);
7996 case CA_SET_LEVEL_SCORE:
7998 local_player->score = action_arg_number_new;
8000 DrawGameValue_Score(local_player->score);
8005 case CA_SET_LEVEL_GEMS:
8007 local_player->gems_still_needed = action_arg_number_new;
8009 DrawGameValue_Emeralds(local_player->gems_still_needed);
8014 case CA_SET_LEVEL_GRAVITY:
8016 game.gravity = (action_arg == CA_ARG_GRAVITY_OFF ? FALSE :
8017 action_arg == CA_ARG_GRAVITY_ON ? TRUE :
8018 action_arg == CA_ARG_GRAVITY_TOGGLE ? !game.gravity :
8023 case CA_SET_LEVEL_WIND:
8025 game.wind_direction = action_arg_direction;
8030 /* ---------- player actions ------------------------------------------ */
8032 case CA_MOVE_PLAYER:
8034 /* automatically move to the next field in specified direction */
8035 for (i = 0; i < MAX_PLAYERS; i++)
8036 if (trigger_player_bits & (1 << i))
8037 stored_player[i].programmed_action = action_arg_direction;
8042 case CA_EXIT_PLAYER:
8044 for (i = 0; i < MAX_PLAYERS; i++)
8045 if (action_arg_player_bits & (1 << i))
8046 stored_player[i].LevelSolved = stored_player[i].GameOver = TRUE;
8051 case CA_KILL_PLAYER:
8053 for (i = 0; i < MAX_PLAYERS; i++)
8054 if (action_arg_player_bits & (1 << i))
8055 KillPlayer(&stored_player[i]);
8060 case CA_SET_PLAYER_KEYS:
8062 int key_state = (action_mode == CA_MODE_ADD ? TRUE : FALSE);
8063 int element = getSpecialActionElement(action_arg_element,
8064 action_arg_number, EL_KEY_1);
8066 if (IS_KEY(element))
8068 for (i = 0; i < MAX_PLAYERS; i++)
8070 if (trigger_player_bits & (1 << i))
8072 stored_player[i].key[KEY_NR(element)] = key_state;
8075 DrawGameDoorValues();
8077 DrawGameValue_Keys(stored_player[i].key);
8080 redraw_mask |= REDRAW_DOOR_1;
8088 case CA_SET_PLAYER_SPEED:
8090 for (i = 0; i < MAX_PLAYERS; i++)
8092 if (trigger_player_bits & (1 << i))
8094 int move_stepsize = TILEX / stored_player[i].move_delay_value;
8096 if (action_arg == CA_ARG_SPEED_FASTER &&
8097 stored_player[i].cannot_move)
8099 action_arg_number = STEPSIZE_VERY_SLOW;
8101 else if (action_arg == CA_ARG_SPEED_SLOWER ||
8102 action_arg == CA_ARG_SPEED_FASTER)
8104 action_arg_number = 2;
8105 action_mode = (action_arg == CA_ARG_SPEED_SLOWER ? CA_MODE_DIVIDE :
8110 getModifiedActionNumber(move_stepsize,
8113 action_arg_number_min,
8114 action_arg_number_max);
8117 SetPlayerMoveSpeed(&stored_player[i], move_stepsize, FALSE);
8119 /* make sure that value is power of 2 */
8120 move_stepsize = (1 << log_2(move_stepsize));
8122 /* do no immediately change -- the player might just be moving */
8123 stored_player[i].move_delay_value_next = TILEX / move_stepsize;
8125 stored_player[i].cannot_move =
8126 (action_arg == CA_ARG_SPEED_NOT_MOVING ? TRUE : FALSE);
8134 case CA_SET_PLAYER_SHIELD:
8136 for (i = 0; i < MAX_PLAYERS; i++)
8138 if (trigger_player_bits & (1 << i))
8140 if (action_arg == CA_ARG_SHIELD_OFF)
8142 stored_player[i].shield_normal_time_left = 0;
8143 stored_player[i].shield_deadly_time_left = 0;
8145 else if (action_arg == CA_ARG_SHIELD_NORMAL)
8147 stored_player[i].shield_normal_time_left = 999999;
8149 else if (action_arg == CA_ARG_SHIELD_DEADLY)
8151 stored_player[i].shield_normal_time_left = 999999;
8152 stored_player[i].shield_deadly_time_left = 999999;
8160 case CA_SET_PLAYER_ARTWORK:
8162 for (i = 0; i < MAX_PLAYERS; i++)
8164 if (trigger_player_bits & (1 << i))
8166 int artwork_element = action_arg_element;
8168 if (action_arg == CA_ARG_ELEMENT_RESET)
8170 (level.use_artwork_element[i] ? level.artwork_element[i] :
8171 stored_player[i].element_nr);
8173 stored_player[i].artwork_element = artwork_element;
8175 SetPlayerWaiting(&stored_player[i], FALSE);
8177 /* set number of special actions for bored and sleeping animation */
8178 stored_player[i].num_special_action_bored =
8179 get_num_special_action(artwork_element,
8180 ACTION_BORING_1, ACTION_BORING_LAST);
8181 stored_player[i].num_special_action_sleeping =
8182 get_num_special_action(artwork_element,
8183 ACTION_SLEEPING_1, ACTION_SLEEPING_LAST);
8190 /* ---------- CE actions ---------------------------------------------- */
8192 case CA_SET_CE_SCORE:
8194 ei->collect_score = action_arg_number_new;
8199 case CA_SET_CE_VALUE:
8201 #if USE_NEW_CUSTOM_VALUE
8202 int last_custom_value = CustomValue[x][y];
8204 CustomValue[x][y] = action_arg_number_new;
8207 printf("::: Count == %d\n", CustomValue[x][y]);
8210 if (CustomValue[x][y] == 0 && last_custom_value > 0)
8213 printf("::: CE_VALUE_GETS_ZERO\n");
8216 CheckElementChange(x, y, element, EL_UNDEFINED, CE_VALUE_GETS_ZERO);
8217 CheckTriggeredElementChange(x, y, element, CE_VALUE_GETS_ZERO_OF_X);
8220 printf("::: RESULT: %d, %d\n", Feld[x][y], ChangePage[x][y]);
8228 /* ---------- engine actions ------------------------------------------ */
8230 case CA_SET_ENGINE_SCAN_MODE:
8232 InitPlayfieldScanMode(action_arg);
8242 static void CreateFieldExt(int x, int y, int element, boolean is_change)
8244 int previous_move_direction = MovDir[x][y];
8245 #if USE_NEW_CUSTOM_VALUE
8246 int last_ce_value = CustomValue[x][y];
8248 boolean add_player = (ELEM_IS_PLAYER(element) &&
8249 IS_WALKABLE(Feld[x][y]));
8251 /* check if element under player changes from accessible to unaccessible
8252 (needed for special case of dropping element which then changes) */
8253 if (IS_PLAYER(x, y) && !PLAYER_EXPLOSION_PROTECTED(x, y) &&
8254 IS_ACCESSIBLE(Feld[x][y]) && !IS_ACCESSIBLE(element))
8263 if (IS_MOVING(x, y) || IS_BLOCKED(x, y))
8264 RemoveMovingField(x, y);
8268 Feld[x][y] = element;
8270 ResetGfxAnimation(x, y);
8271 ResetRandomAnimationValue(x, y);
8273 if (element_info[Feld[x][y]].move_direction_initial == MV_START_PREVIOUS)
8274 MovDir[x][y] = previous_move_direction;
8276 #if USE_NEW_CUSTOM_VALUE
8277 if (element_info[Feld[x][y]].use_last_ce_value)
8278 CustomValue[x][y] = last_ce_value;
8281 InitField_WithBug1(x, y, FALSE);
8283 DrawLevelField(x, y);
8285 if (GFX_CRUMBLED(Feld[x][y]))
8286 DrawLevelFieldCrumbledSandNeighbours(x, y);
8289 /* "ChangeCount" not set yet to allow "entered by player" change one time */
8290 if (ELEM_IS_PLAYER(element))
8291 RelocatePlayer(x, y, element);
8294 ChangeCount[x][y]++; /* count number of changes in the same frame */
8296 TestIfBadThingTouchesPlayer(x, y);
8297 TestIfPlayerTouchesCustomElement(x, y);
8298 TestIfElementTouchesCustomElement(x, y);
8301 static void CreateField(int x, int y, int element)
8303 CreateFieldExt(x, y, element, FALSE);
8306 static void CreateElementFromChange(int x, int y, int element)
8308 element = GET_VALID_RUNTIME_ELEMENT(element);
8310 #if USE_STOP_CHANGED_ELEMENTS
8311 if (game.engine_version >= VERSION_IDENT(3,2,0,7))
8313 int old_element = Feld[x][y];
8315 /* prevent changed element from moving in same engine frame
8316 unless both old and new element can either fall or move */
8317 if ((!CAN_FALL(old_element) || !CAN_FALL(element)) &&
8318 (!CAN_MOVE(old_element) || !CAN_MOVE(element)))
8323 CreateFieldExt(x, y, element, TRUE);
8326 static boolean ChangeElement(int x, int y, int element, int page)
8328 struct ElementChangeInfo *change = &element_info[element].change_page[page];
8330 int old_element = Feld[x][y];
8332 /* always use default change event to prevent running into a loop */
8333 if (ChangeEvent[x][y] == -1)
8334 ChangeEvent[x][y] = CE_DELAY;
8336 if (ChangeEvent[x][y] == CE_DELAY)
8338 /* reset actual trigger element, trigger player and action element */
8339 change->actual_trigger_element = EL_EMPTY;
8340 change->actual_trigger_player = EL_PLAYER_1;
8341 change->actual_trigger_side = CH_SIDE_NONE;
8342 change->actual_trigger_ce_value = 0;
8345 /* do not change elements more than a specified maximum number of changes */
8346 if (ChangeCount[x][y] >= game.max_num_changes_per_frame)
8349 ChangeCount[x][y]++; /* count number of changes in the same frame */
8351 if (change->explode)
8358 if (change->use_target_content)
8360 boolean complete_replace = TRUE;
8361 boolean can_replace[3][3];
8364 for (yy = 0; yy < 3; yy++) for (xx = 0; xx < 3 ; xx++)
8367 boolean is_walkable;
8368 boolean is_diggable;
8369 boolean is_collectible;
8370 boolean is_removable;
8371 boolean is_destructible;
8372 int ex = x + xx - 1;
8373 int ey = y + yy - 1;
8374 int content_element = change->target_content.e[xx][yy];
8377 can_replace[xx][yy] = TRUE;
8379 if (ex == x && ey == y) /* do not check changing element itself */
8382 if (content_element == EL_EMPTY_SPACE)
8384 can_replace[xx][yy] = FALSE; /* do not replace border with space */
8389 if (!IN_LEV_FIELD(ex, ey))
8391 can_replace[xx][yy] = FALSE;
8392 complete_replace = FALSE;
8399 if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
8400 e = MovingOrBlocked2Element(ex, ey);
8402 is_empty = (IS_FREE(ex, ey) ||
8403 (IS_FREE_OR_PLAYER(ex, ey) && IS_WALKABLE(content_element)));
8405 is_walkable = (is_empty || IS_WALKABLE(e));
8406 is_diggable = (is_empty || IS_DIGGABLE(e));
8407 is_collectible = (is_empty || IS_COLLECTIBLE(e));
8408 is_destructible = (is_empty || !IS_INDESTRUCTIBLE(e));
8409 is_removable = (is_diggable || is_collectible);
8411 can_replace[xx][yy] =
8412 (((change->replace_when == CP_WHEN_EMPTY && is_empty) ||
8413 (change->replace_when == CP_WHEN_WALKABLE && is_walkable) ||
8414 (change->replace_when == CP_WHEN_DIGGABLE && is_diggable) ||
8415 (change->replace_when == CP_WHEN_COLLECTIBLE && is_collectible) ||
8416 (change->replace_when == CP_WHEN_REMOVABLE && is_removable) ||
8417 (change->replace_when == CP_WHEN_DESTRUCTIBLE && is_destructible)) &&
8418 !(IS_PLAYER(ex, ey) && ELEM_IS_PLAYER(content_element)));
8420 if (!can_replace[xx][yy])
8421 complete_replace = FALSE;
8424 if (!change->only_if_complete || complete_replace)
8426 boolean something_has_changed = FALSE;
8428 if (change->only_if_complete && change->use_random_replace &&
8429 RND(100) < change->random_percentage)
8432 for (yy = 0; yy < 3; yy++) for (xx = 0; xx < 3 ; xx++)
8434 int ex = x + xx - 1;
8435 int ey = y + yy - 1;
8436 int content_element;
8438 if (can_replace[xx][yy] && (!change->use_random_replace ||
8439 RND(100) < change->random_percentage))
8441 if (IS_MOVING(ex, ey) || IS_BLOCKED(ex, ey))
8442 RemoveMovingField(ex, ey);
8444 ChangeEvent[ex][ey] = ChangeEvent[x][y];
8446 content_element = change->target_content.e[xx][yy];
8447 target_element = GET_TARGET_ELEMENT(content_element, change);
8449 CreateElementFromChange(ex, ey, target_element);
8451 something_has_changed = TRUE;
8453 /* for symmetry reasons, freeze newly created border elements */
8454 if (ex != x || ey != y)
8455 Stop[ex][ey] = TRUE; /* no more moving in this frame */
8459 if (something_has_changed)
8461 PlayLevelSoundElementAction(x, y, element, ACTION_CHANGING);
8462 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + page);
8468 target_element = GET_TARGET_ELEMENT(change->target_element, change);
8470 if (element == EL_DIAGONAL_GROWING ||
8471 element == EL_DIAGONAL_SHRINKING)
8473 target_element = Store[x][y];
8475 Store[x][y] = EL_EMPTY;
8478 CreateElementFromChange(x, y, target_element);
8480 PlayLevelSoundElementAction(x, y, element, ACTION_CHANGING);
8481 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + page);
8484 /* this uses direct change before indirect change */
8485 CheckTriggeredElementChangeByPage(x, y, old_element, CE_CHANGE_OF_X, page);
8490 #if USE_NEW_DELAYED_ACTION
8492 static void HandleElementChange(int x, int y, int page)
8494 int element = MovingOrBlocked2Element(x, y);
8495 struct ElementInfo *ei = &element_info[element];
8496 struct ElementChangeInfo *change = &ei->change_page[page];
8499 if (!CAN_CHANGE_OR_HAS_ACTION(element) &&
8500 !CAN_CHANGE_OR_HAS_ACTION(Back[x][y]))
8503 printf("HandleElementChange(): %d,%d: element = %d ('%s')\n",
8504 x, y, element, element_info[element].token_name);
8505 printf("HandleElementChange(): This should never happen!\n");
8510 /* this can happen with classic bombs on walkable, changing elements */
8511 if (!CAN_CHANGE_OR_HAS_ACTION(element))
8514 if (!CAN_CHANGE(Back[x][y])) /* prevent permanent repetition */
8515 ChangeDelay[x][y] = 0;
8521 if (ChangeDelay[x][y] == 0) /* initialize element change */
8523 ChangeDelay[x][y] = GET_CHANGE_DELAY(change) + 1;
8525 if (change->can_change)
8527 ResetGfxAnimation(x, y);
8528 ResetRandomAnimationValue(x, y);
8530 if (change->pre_change_function)
8531 change->pre_change_function(x, y);
8535 ChangeDelay[x][y]--;
8537 if (ChangeDelay[x][y] != 0) /* continue element change */
8539 if (change->can_change)
8541 int graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
8543 if (IS_ANIMATED(graphic))
8544 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
8546 if (change->change_function)
8547 change->change_function(x, y);
8550 else /* finish element change */
8552 if (ChangePage[x][y] != -1) /* remember page from delayed change */
8554 page = ChangePage[x][y];
8555 ChangePage[x][y] = -1;
8557 change = &ei->change_page[page];
8560 if (IS_MOVING(x, y)) /* never change a running system ;-) */
8562 ChangeDelay[x][y] = 1; /* try change after next move step */
8563 ChangePage[x][y] = page; /* remember page to use for change */
8568 if (change->can_change)
8570 if (ChangeElement(x, y, element, page))
8572 if (change->post_change_function)
8573 change->post_change_function(x, y);
8577 if (change->has_action)
8578 ExecuteCustomElementAction(x, y, element, page);
8584 static void HandleElementChange(int x, int y, int page)
8586 int element = MovingOrBlocked2Element(x, y);
8587 struct ElementInfo *ei = &element_info[element];
8588 struct ElementChangeInfo *change = &ei->change_page[page];
8591 if (!CAN_CHANGE(element) && !CAN_CHANGE(Back[x][y]))
8594 printf("HandleElementChange(): %d,%d: element = %d ('%s')\n",
8595 x, y, element, element_info[element].token_name);
8596 printf("HandleElementChange(): This should never happen!\n");
8601 /* this can happen with classic bombs on walkable, changing elements */
8602 if (!CAN_CHANGE(element))
8605 if (!CAN_CHANGE(Back[x][y])) /* prevent permanent repetition */
8606 ChangeDelay[x][y] = 0;
8612 if (ChangeDelay[x][y] == 0) /* initialize element change */
8614 ChangeDelay[x][y] = GET_CHANGE_DELAY(change) + 1;
8616 ResetGfxAnimation(x, y);
8617 ResetRandomAnimationValue(x, y);
8619 if (change->pre_change_function)
8620 change->pre_change_function(x, y);
8623 ChangeDelay[x][y]--;
8625 if (ChangeDelay[x][y] != 0) /* continue element change */
8627 int graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
8629 if (IS_ANIMATED(graphic))
8630 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
8632 if (change->change_function)
8633 change->change_function(x, y);
8635 else /* finish element change */
8637 if (ChangePage[x][y] != -1) /* remember page from delayed change */
8639 page = ChangePage[x][y];
8640 ChangePage[x][y] = -1;
8642 change = &ei->change_page[page];
8645 if (IS_MOVING(x, y)) /* never change a running system ;-) */
8647 ChangeDelay[x][y] = 1; /* try change after next move step */
8648 ChangePage[x][y] = page; /* remember page to use for change */
8653 if (ChangeElement(x, y, element, page))
8655 if (change->post_change_function)
8656 change->post_change_function(x, y);
8663 static boolean CheckTriggeredElementChangeExt(int trigger_x, int trigger_y,
8664 int trigger_element,
8670 boolean change_done_any = FALSE;
8671 int trigger_page_bits = (trigger_page < 0 ? CH_PAGE_ANY : 1 << trigger_page);
8674 if (!(trigger_events[trigger_element][trigger_event]))
8677 for (i = 0; i < NUM_CUSTOM_ELEMENTS; i++)
8679 int element = EL_CUSTOM_START + i;
8680 boolean change_done = FALSE;
8683 if (!CAN_CHANGE_OR_HAS_ACTION(element) ||
8684 !HAS_ANY_CHANGE_EVENT(element, trigger_event))
8687 for (p = 0; p < element_info[element].num_change_pages; p++)
8689 struct ElementChangeInfo *change = &element_info[element].change_page[p];
8691 if (change->can_change_or_has_action &&
8692 change->has_event[trigger_event] &&
8693 change->trigger_side & trigger_side &&
8694 change->trigger_player & trigger_player &&
8695 change->trigger_page & trigger_page_bits &&
8696 IS_EQUAL_OR_IN_GROUP(trigger_element, change->trigger_element))
8698 change->actual_trigger_element = trigger_element;
8699 change->actual_trigger_player = EL_PLAYER_1 + log_2(trigger_player);
8700 change->actual_trigger_side = trigger_side;
8701 change->actual_trigger_ce_value = CustomValue[trigger_x][trigger_y];
8703 if ((change->can_change && !change_done) || change->has_action)
8708 SCAN_PLAYFIELD(x, y)
8710 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
8713 if (Feld[x][y] == element)
8715 if (change->can_change && !change_done)
8717 ChangeDelay[x][y] = 1;
8718 ChangeEvent[x][y] = trigger_event;
8720 HandleElementChange(x, y, p);
8722 #if USE_NEW_DELAYED_ACTION
8723 else if (change->has_action)
8725 ExecuteCustomElementAction(x, y, element, p);
8726 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + p);
8729 if (change->has_action)
8731 ExecuteCustomElementAction(x, y, element, p);
8732 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + p);
8738 if (change->can_change)
8741 change_done_any = TRUE;
8748 return change_done_any;
8751 static boolean CheckElementChangeExt(int x, int y,
8753 int trigger_element,
8758 boolean change_done = FALSE;
8761 if (!CAN_CHANGE_OR_HAS_ACTION(element) ||
8762 !HAS_ANY_CHANGE_EVENT(element, trigger_event))
8765 if (Feld[x][y] == EL_BLOCKED)
8767 Blocked2Moving(x, y, &x, &y);
8768 element = Feld[x][y];
8772 /* check if element has already changed */
8773 if (Feld[x][y] != element)
8776 /* check if element has already changed or is about to change after moving */
8777 if ((game.engine_version < VERSION_IDENT(3,2,0,7) &&
8778 Feld[x][y] != element) ||
8780 (game.engine_version >= VERSION_IDENT(3,2,0,7) &&
8781 (ChangeCount[x][y] >= game.max_num_changes_per_frame ||
8782 ChangePage[x][y] != -1)))
8786 for (p = 0; p < element_info[element].num_change_pages; p++)
8788 struct ElementChangeInfo *change = &element_info[element].change_page[p];
8790 boolean check_trigger_element =
8791 (trigger_event == CE_TOUCHING_X ||
8792 trigger_event == CE_HITTING_X ||
8793 trigger_event == CE_HIT_BY_X);
8795 if (change->can_change_or_has_action &&
8796 change->has_event[trigger_event] &&
8797 change->trigger_side & trigger_side &&
8798 change->trigger_player & trigger_player &&
8799 (!check_trigger_element ||
8800 IS_EQUAL_OR_IN_GROUP(trigger_element, change->trigger_element)))
8802 change->actual_trigger_element = trigger_element;
8803 change->actual_trigger_player = EL_PLAYER_1 + log_2(trigger_player);
8804 change->actual_trigger_side = trigger_side;
8805 change->actual_trigger_ce_value = CustomValue[x][y];
8807 /* special case: trigger element not at (x,y) position for some events */
8808 if (check_trigger_element)
8820 { 0, 0 }, { 0, 0 }, { 0, 0 },
8824 int xx = x + move_xy[MV_DIR_OPPOSITE(trigger_side)].dx;
8825 int yy = y + move_xy[MV_DIR_OPPOSITE(trigger_side)].dy;
8827 change->actual_trigger_ce_value = CustomValue[xx][yy];
8830 if (change->can_change && !change_done)
8832 ChangeDelay[x][y] = 1;
8833 ChangeEvent[x][y] = trigger_event;
8835 HandleElementChange(x, y, p);
8839 #if USE_NEW_DELAYED_ACTION
8840 else if (change->has_action)
8842 ExecuteCustomElementAction(x, y, element, p);
8843 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + p);
8846 if (change->has_action)
8848 ExecuteCustomElementAction(x, y, element, p);
8849 PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + p);
8858 static void PlayPlayerSound(struct PlayerInfo *player)
8860 int jx = player->jx, jy = player->jy;
8861 int sound_element = player->artwork_element;
8862 int last_action = player->last_action_waiting;
8863 int action = player->action_waiting;
8865 if (player->is_waiting)
8867 if (action != last_action)
8868 PlayLevelSoundElementAction(jx, jy, sound_element, action);
8870 PlayLevelSoundElementActionIfLoop(jx, jy, sound_element, action);
8874 if (action != last_action)
8875 StopSound(element_info[sound_element].sound[last_action]);
8877 if (last_action == ACTION_SLEEPING)
8878 PlayLevelSoundElementAction(jx, jy, sound_element, ACTION_AWAKENING);
8882 static void PlayAllPlayersSound()
8886 for (i = 0; i < MAX_PLAYERS; i++)
8887 if (stored_player[i].active)
8888 PlayPlayerSound(&stored_player[i]);
8891 static void SetPlayerWaiting(struct PlayerInfo *player, boolean is_waiting)
8893 boolean last_waiting = player->is_waiting;
8894 int move_dir = player->MovDir;
8896 player->dir_waiting = move_dir;
8897 player->last_action_waiting = player->action_waiting;
8901 if (!last_waiting) /* not waiting -> waiting */
8903 player->is_waiting = TRUE;
8905 player->frame_counter_bored =
8907 game.player_boring_delay_fixed +
8908 SimpleRND(game.player_boring_delay_random);
8909 player->frame_counter_sleeping =
8911 game.player_sleeping_delay_fixed +
8912 SimpleRND(game.player_sleeping_delay_random);
8915 InitPlayerGfxAnimation(player, ACTION_WAITING, move_dir);
8917 InitPlayerGfxAnimation(player, ACTION_WAITING, player->MovDir);
8921 if (game.player_sleeping_delay_fixed +
8922 game.player_sleeping_delay_random > 0 &&
8923 player->anim_delay_counter == 0 &&
8924 player->post_delay_counter == 0 &&
8925 FrameCounter >= player->frame_counter_sleeping)
8926 player->is_sleeping = TRUE;
8927 else if (game.player_boring_delay_fixed +
8928 game.player_boring_delay_random > 0 &&
8929 FrameCounter >= player->frame_counter_bored)
8930 player->is_bored = TRUE;
8932 player->action_waiting = (player->is_sleeping ? ACTION_SLEEPING :
8933 player->is_bored ? ACTION_BORING :
8937 if (player->is_sleeping && player->use_murphy)
8939 /* special case for sleeping Murphy when leaning against non-free tile */
8941 if (!IN_LEV_FIELD(player->jx - 1, player->jy) ||
8942 (Feld[player->jx - 1][player->jy] != EL_EMPTY &&
8943 !IS_MOVING(player->jx - 1, player->jy)))
8945 else if (!IN_LEV_FIELD(player->jx + 1, player->jy) ||
8946 (Feld[player->jx + 1][player->jy] != EL_EMPTY &&
8947 !IS_MOVING(player->jx + 1, player->jy)))
8948 move_dir = MV_RIGHT;
8950 player->is_sleeping = FALSE;
8952 player->dir_waiting = move_dir;
8956 if (player->is_sleeping)
8958 if (player->num_special_action_sleeping > 0)
8960 if (player->anim_delay_counter == 0 && player->post_delay_counter == 0)
8962 int last_special_action = player->special_action_sleeping;
8963 int num_special_action = player->num_special_action_sleeping;
8964 int special_action =
8965 (last_special_action == ACTION_DEFAULT ? ACTION_SLEEPING_1 :
8966 last_special_action == ACTION_SLEEPING ? ACTION_SLEEPING :
8967 last_special_action < ACTION_SLEEPING_1 + num_special_action - 1 ?
8968 last_special_action + 1 : ACTION_SLEEPING);
8969 int special_graphic =
8970 el_act_dir2img(player->artwork_element, special_action, move_dir);
8972 player->anim_delay_counter =
8973 graphic_info[special_graphic].anim_delay_fixed +
8974 SimpleRND(graphic_info[special_graphic].anim_delay_random);
8975 player->post_delay_counter =
8976 graphic_info[special_graphic].post_delay_fixed +
8977 SimpleRND(graphic_info[special_graphic].post_delay_random);
8979 player->special_action_sleeping = special_action;
8982 if (player->anim_delay_counter > 0)
8984 player->action_waiting = player->special_action_sleeping;
8985 player->anim_delay_counter--;
8987 else if (player->post_delay_counter > 0)
8989 player->post_delay_counter--;
8993 else if (player->is_bored)
8995 if (player->num_special_action_bored > 0)
8997 if (player->anim_delay_counter == 0 && player->post_delay_counter == 0)
8999 int special_action =
9000 ACTION_BORING_1 + SimpleRND(player->num_special_action_bored);
9001 int special_graphic =
9002 el_act_dir2img(player->artwork_element, special_action, move_dir);
9004 player->anim_delay_counter =
9005 graphic_info[special_graphic].anim_delay_fixed +
9006 SimpleRND(graphic_info[special_graphic].anim_delay_random);
9007 player->post_delay_counter =
9008 graphic_info[special_graphic].post_delay_fixed +
9009 SimpleRND(graphic_info[special_graphic].post_delay_random);
9011 player->special_action_bored = special_action;
9014 if (player->anim_delay_counter > 0)
9016 player->action_waiting = player->special_action_bored;
9017 player->anim_delay_counter--;
9019 else if (player->post_delay_counter > 0)
9021 player->post_delay_counter--;
9026 else if (last_waiting) /* waiting -> not waiting */
9028 player->is_waiting = FALSE;
9029 player->is_bored = FALSE;
9030 player->is_sleeping = FALSE;
9032 player->frame_counter_bored = -1;
9033 player->frame_counter_sleeping = -1;
9035 player->anim_delay_counter = 0;
9036 player->post_delay_counter = 0;
9038 player->dir_waiting = player->MovDir;
9039 player->action_waiting = ACTION_DEFAULT;
9041 player->special_action_bored = ACTION_DEFAULT;
9042 player->special_action_sleeping = ACTION_DEFAULT;
9046 static byte PlayerActions(struct PlayerInfo *player, byte player_action)
9048 boolean moved = FALSE, snapped = FALSE, dropped = FALSE;
9049 int left = player_action & JOY_LEFT;
9050 int right = player_action & JOY_RIGHT;
9051 int up = player_action & JOY_UP;
9052 int down = player_action & JOY_DOWN;
9053 int button1 = player_action & JOY_BUTTON_1;
9054 int button2 = player_action & JOY_BUTTON_2;
9055 int dx = (left ? -1 : right ? 1 : 0);
9056 int dy = (up ? -1 : down ? 1 : 0);
9058 if (!player->active || tape.pausing)
9064 snapped = SnapField(player, dx, dy);
9068 dropped = DropElement(player);
9070 moved = MovePlayer(player, dx, dy);
9073 if (tape.single_step && tape.recording && !tape.pausing)
9075 if (button1 || (dropped && !moved))
9077 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
9078 SnapField(player, 0, 0); /* stop snapping */
9082 SetPlayerWaiting(player, FALSE);
9084 return player_action;
9088 /* no actions for this player (no input at player's configured device) */
9090 DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
9091 SnapField(player, 0, 0);
9092 CheckGravityMovementWhenNotMoving(player);
9094 if (player->MovPos == 0)
9095 SetPlayerWaiting(player, TRUE);
9097 if (player->MovPos == 0) /* needed for tape.playing */
9098 player->is_moving = FALSE;
9100 player->is_dropping = FALSE;
9101 player->is_dropping_pressed = FALSE;
9102 player->drop_pressed_delay = 0;
9108 static void CheckLevelTime()
9112 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
9114 if (level.native_em_level->lev->home == 0) /* all players at home */
9116 local_player->LevelSolved = TRUE;
9117 AllPlayersGone = TRUE;
9119 level.native_em_level->lev->home = -1;
9122 if (level.native_em_level->ply[0]->alive == 0 &&
9123 level.native_em_level->ply[1]->alive == 0 &&
9124 level.native_em_level->ply[2]->alive == 0 &&
9125 level.native_em_level->ply[3]->alive == 0) /* all dead */
9126 AllPlayersGone = TRUE;
9129 if (TimeFrames >= FRAMES_PER_SECOND)
9134 for (i = 0; i < MAX_PLAYERS; i++)
9136 struct PlayerInfo *player = &stored_player[i];
9138 if (SHIELD_ON(player))
9140 player->shield_normal_time_left--;
9142 if (player->shield_deadly_time_left > 0)
9143 player->shield_deadly_time_left--;
9147 if (!level.use_step_counter)
9155 if (TimeLeft <= 10 && setup.time_limit)
9156 PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
9158 DrawGameValue_Time(TimeLeft);
9160 if (!TimeLeft && setup.time_limit)
9162 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
9163 level.native_em_level->lev->killed_out_of_time = TRUE;
9165 for (i = 0; i < MAX_PLAYERS; i++)
9166 KillPlayer(&stored_player[i]);
9169 else if (level.time == 0 && !AllPlayersGone) /* level w/o time limit */
9170 DrawGameValue_Time(TimePlayed);
9172 level.native_em_level->lev->time =
9173 (level.time == 0 ? TimePlayed : TimeLeft);
9176 if (tape.recording || tape.playing)
9177 DrawVideoDisplay(VIDEO_STATE_TIME_ON, TapeTime);
9181 void AdvanceFrameAndPlayerCounters(int player_nr)
9186 Error(ERR_NETWORK_CLIENT, "advancing frame counter from %d to %d",
9187 FrameCounter, FrameCounter + 1);
9190 /* advance frame counters (global frame counter and time frame counter) */
9194 /* advance player counters (counters for move delay, move animation etc.) */
9195 for (i = 0; i < MAX_PLAYERS; i++)
9197 boolean advance_player_counters = (player_nr == -1 || player_nr == i);
9198 int move_delay_value = stored_player[i].move_delay_value;
9199 int move_frames = MOVE_DELAY_NORMAL_SPEED / move_delay_value;
9201 if (!advance_player_counters) /* not all players may be affected */
9204 #if USE_NEW_PLAYER_ANIM
9205 if (move_frames == 0) /* less than one move per game frame */
9207 int stepsize = TILEX / move_delay_value;
9208 int delay = move_delay_value / MOVE_DELAY_NORMAL_SPEED;
9209 int count = (stored_player[i].is_moving ?
9210 ABS(stored_player[i].MovPos) / stepsize : FrameCounter);
9212 if (count % delay == 0)
9217 stored_player[i].Frame += move_frames;
9219 if (stored_player[i].MovPos != 0)
9220 stored_player[i].StepFrame += move_frames;
9222 if (stored_player[i].move_delay > 0)
9223 stored_player[i].move_delay--;
9225 /* due to bugs in previous versions, counter must count up, not down */
9226 if (stored_player[i].push_delay != -1)
9227 stored_player[i].push_delay++;
9229 if (stored_player[i].drop_delay > 0)
9230 stored_player[i].drop_delay--;
9232 if (stored_player[i].is_dropping_pressed)
9233 stored_player[i].drop_pressed_delay++;
9237 void StartGameActions(boolean init_network_game, boolean record_tape,
9240 unsigned long new_random_seed = InitRND(random_seed);
9243 TapeStartRecording(new_random_seed);
9245 #if defined(NETWORK_AVALIABLE)
9246 if (init_network_game)
9248 SendToServer_StartPlaying();
9256 game_status = GAME_MODE_PLAYING;
9263 static unsigned long game_frame_delay = 0;
9264 unsigned long game_frame_delay_value;
9265 byte *recorded_player_action;
9266 byte summarized_player_action = 0;
9267 byte tape_action[MAX_PLAYERS];
9270 if (game.restart_level)
9271 StartGameActions(options.network, setup.autorecord, NEW_RANDOMIZE);
9273 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
9275 if (level.native_em_level->lev->home == 0) /* all players at home */
9277 local_player->LevelSolved = TRUE;
9278 AllPlayersGone = TRUE;
9280 level.native_em_level->lev->home = -1;
9283 if (level.native_em_level->ply[0]->alive == 0 &&
9284 level.native_em_level->ply[1]->alive == 0 &&
9285 level.native_em_level->ply[2]->alive == 0 &&
9286 level.native_em_level->ply[3]->alive == 0) /* all dead */
9287 AllPlayersGone = TRUE;
9290 if (local_player->LevelSolved)
9293 if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
9296 if (game_status != GAME_MODE_PLAYING) /* status might have changed */
9299 game_frame_delay_value =
9300 (tape.playing && tape.fast_forward ? FfwdFrameDelay : GameFrameDelay);
9302 if (tape.playing && tape.warp_forward && !tape.pausing)
9303 game_frame_delay_value = 0;
9305 /* ---------- main game synchronization point ---------- */
9307 WaitUntilDelayReached(&game_frame_delay, game_frame_delay_value);
9309 if (network_playing && !network_player_action_received)
9311 /* try to get network player actions in time */
9313 #if defined(NETWORK_AVALIABLE)
9314 /* last chance to get network player actions without main loop delay */
9318 /* game was quit by network peer */
9319 if (game_status != GAME_MODE_PLAYING)
9322 if (!network_player_action_received)
9323 return; /* failed to get network player actions in time */
9325 /* do not yet reset "network_player_action_received" (for tape.pausing) */
9331 /* at this point we know that we really continue executing the game */
9334 network_player_action_received = FALSE;
9337 recorded_player_action = (tape.playing ? TapePlayAction() : NULL);
9339 if (tape.set_centered_player)
9341 game.centered_player_nr_next = tape.centered_player_nr_next;
9342 game.set_centered_player = TRUE;
9345 for (i = 0; i < MAX_PLAYERS; i++)
9347 summarized_player_action |= stored_player[i].action;
9349 if (!network_playing)
9350 stored_player[i].effective_action = stored_player[i].action;
9353 #if defined(NETWORK_AVALIABLE)
9354 if (network_playing)
9355 SendToServer_MovePlayer(summarized_player_action);
9358 if (!options.network && !setup.team_mode)
9359 local_player->effective_action = summarized_player_action;
9361 if (setup.team_mode && setup.input_on_focus && game.centered_player_nr != -1)
9363 for (i = 0; i < MAX_PLAYERS; i++)
9364 stored_player[i].effective_action =
9365 (i == game.centered_player_nr ? summarized_player_action : 0);
9368 if (recorded_player_action != NULL)
9369 for (i = 0; i < MAX_PLAYERS; i++)
9370 stored_player[i].effective_action = recorded_player_action[i];
9372 for (i = 0; i < MAX_PLAYERS; i++)
9374 tape_action[i] = stored_player[i].effective_action;
9376 /* (this can only happen in the R'n'D game engine) */
9377 if (tape.recording && tape_action[i] && !tape.player_participates[i])
9378 tape.player_participates[i] = TRUE; /* player just appeared from CE */
9381 /* only record actions from input devices, but not programmed actions */
9383 TapeRecordAction(tape_action);
9385 if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
9387 GameActions_EM_Main();
9395 void GameActions_EM_Main()
9397 byte effective_action[MAX_PLAYERS];
9398 boolean warp_mode = (tape.playing && tape.warp_forward && !tape.pausing);
9401 for (i = 0; i < MAX_PLAYERS; i++)
9402 effective_action[i] = stored_player[i].effective_action;
9404 GameActions_EM(effective_action, warp_mode);
9408 AdvanceFrameAndPlayerCounters(-1); /* advance counters for all players */
9411 void GameActions_RND()
9413 int magic_wall_x = 0, magic_wall_y = 0;
9414 int i, x, y, element, graphic;
9416 InitPlayfieldScanModeVars();
9418 #if USE_ONE_MORE_CHANGE_PER_FRAME
9419 if (game.engine_version >= VERSION_IDENT(3,2,0,7))
9421 SCAN_PLAYFIELD(x, y)
9423 ChangeCount[x][y] = 0;
9424 ChangeEvent[x][y] = -1;
9430 if (game.set_centered_player)
9432 boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen_RND();
9434 /* switching to "all players" only possible if all players fit to screen */
9435 if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
9437 game.centered_player_nr_next = game.centered_player_nr;
9438 game.set_centered_player = FALSE;
9441 /* do not switch focus to non-existing (or non-active) player */
9442 if (game.centered_player_nr_next >= 0 &&
9443 !stored_player[game.centered_player_nr_next].active)
9445 game.centered_player_nr_next = game.centered_player_nr;
9446 game.set_centered_player = FALSE;
9450 if (game.set_centered_player &&
9451 ScreenMovPos == 0) /* screen currently aligned at tile position */
9455 if (game.centered_player_nr_next == -1)
9457 setScreenCenteredToAllPlayers(&sx, &sy);
9461 sx = stored_player[game.centered_player_nr_next].jx;
9462 sy = stored_player[game.centered_player_nr_next].jy;
9465 game.centered_player_nr = game.centered_player_nr_next;
9466 game.set_centered_player = FALSE;
9468 DrawRelocateScreen(sx, sy, MV_NONE, TRUE, setup.quick_switch);
9469 DrawGameDoorValues();
9473 for (i = 0; i < MAX_PLAYERS; i++)
9475 int actual_player_action = stored_player[i].effective_action;
9478 /* !!! THIS BREAKS THE FOLLOWING TAPES: !!!
9479 - rnd_equinox_tetrachloride 048
9480 - rnd_equinox_tetrachloride_ii 096
9481 - rnd_emanuel_schmieg 002
9482 - doctor_sloan_ww 001, 020
9484 if (stored_player[i].MovPos == 0)
9485 CheckGravityMovement(&stored_player[i]);
9488 /* overwrite programmed action with tape action */
9489 if (stored_player[i].programmed_action)
9490 actual_player_action = stored_player[i].programmed_action;
9493 PlayerActions(&stored_player[i], actual_player_action);
9495 tape_action[i] = PlayerActions(&stored_player[i], actual_player_action);
9497 if (tape.recording && tape_action[i] && !tape.player_participates[i])
9498 tape.player_participates[i] = TRUE; /* player just appeared from CE */
9501 ScrollPlayer(&stored_player[i], SCROLL_GO_ON);
9505 network_player_action_received = FALSE;
9508 ScrollScreen(NULL, SCROLL_GO_ON);
9510 /* for backwards compatibility, the following code emulates a fixed bug that
9511 occured when pushing elements (causing elements that just made their last
9512 pushing step to already (if possible) make their first falling step in the
9513 same game frame, which is bad); this code is also needed to use the famous
9514 "spring push bug" which is used in older levels and might be wanted to be
9515 used also in newer levels, but in this case the buggy pushing code is only
9516 affecting the "spring" element and no other elements */
9518 if (game.engine_version < VERSION_IDENT(2,2,0,7) || level.use_spring_bug)
9520 for (i = 0; i < MAX_PLAYERS; i++)
9522 struct PlayerInfo *player = &stored_player[i];
9526 if (player->active && player->is_pushing && player->is_moving &&
9528 (game.engine_version < VERSION_IDENT(2,2,0,7) ||
9529 Feld[x][y] == EL_SPRING))
9531 ContinueMoving(x, y);
9533 /* continue moving after pushing (this is actually a bug) */
9534 if (!IS_MOVING(x, y))
9543 SCAN_PLAYFIELD(x, y)
9545 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
9548 ChangeCount[x][y] = 0;
9549 ChangeEvent[x][y] = -1;
9551 /* this must be handled before main playfield loop */
9552 if (Feld[x][y] == EL_PLAYER_IS_LEAVING)
9555 if (MovDelay[x][y] <= 0)
9559 #if USE_NEW_SNAP_DELAY
9560 if (Feld[x][y] == EL_ELEMENT_SNAPPING)
9563 if (MovDelay[x][y] <= 0)
9566 DrawLevelField(x, y);
9568 TestIfElementTouchesCustomElement(x, y); /* for empty space */
9574 if (ChangePage[x][y] != -1 && ChangeDelay[x][y] != 1)
9576 printf("GameActions(): x = %d, y = %d: ChangePage != -1\n", x, y);
9577 printf("GameActions(): This should never happen!\n");
9579 ChangePage[x][y] = -1;
9584 if (WasJustMoving[x][y] > 0)
9585 WasJustMoving[x][y]--;
9586 if (WasJustFalling[x][y] > 0)
9587 WasJustFalling[x][y]--;
9588 if (CheckCollision[x][y] > 0)
9589 CheckCollision[x][y]--;
9593 /* reset finished pushing action (not done in ContinueMoving() to allow
9594 continuous pushing animation for elements with zero push delay) */
9595 if (GfxAction[x][y] == ACTION_PUSHING && !IS_MOVING(x, y))
9597 ResetGfxAnimation(x, y);
9598 DrawLevelField(x, y);
9602 if (IS_BLOCKED(x, y))
9606 Blocked2Moving(x, y, &oldx, &oldy);
9607 if (!IS_MOVING(oldx, oldy))
9609 printf("GameActions(): (BLOCKED => MOVING) context corrupted!\n");
9610 printf("GameActions(): BLOCKED: x = %d, y = %d\n", x, y);
9611 printf("GameActions(): !MOVING: oldx = %d, oldy = %d\n", oldx, oldy);
9612 printf("GameActions(): This should never happen!\n");
9619 SCAN_PLAYFIELD(x, y)
9621 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
9624 element = Feld[x][y];
9625 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
9628 printf("::: %d,%d\n", x, y);
9630 if (element == EL_ROCK)
9631 printf("::: Yo man! Rocks can fall!\n");
9634 if (graphic_info[graphic].anim_global_sync)
9635 GfxFrame[x][y] = FrameCounter;
9636 else if (ANIM_MODE(graphic) == ANIM_CE_VALUE)
9638 int old_gfx_frame = GfxFrame[x][y];
9640 GfxFrame[x][y] = CustomValue[x][y];
9643 if (GfxFrame[x][y] != old_gfx_frame)
9645 DrawLevelGraphicAnimation(x, y, graphic);
9647 else if (ANIM_MODE(graphic) == ANIM_CE_SCORE)
9649 int old_gfx_frame = GfxFrame[x][y];
9651 GfxFrame[x][y] = element_info[element].collect_score;
9654 if (GfxFrame[x][y] != old_gfx_frame)
9656 DrawLevelGraphicAnimation(x, y, graphic);
9659 if (ANIM_MODE(graphic) == ANIM_RANDOM &&
9660 IS_NEXT_FRAME(GfxFrame[x][y], graphic))
9661 ResetRandomAnimationValue(x, y);
9663 SetRandomAnimationValue(x, y);
9665 PlayLevelSoundActionIfLoop(x, y, GfxAction[x][y]);
9667 if (IS_INACTIVE(element))
9669 if (IS_ANIMATED(graphic))
9670 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
9675 /* this may take place after moving, so 'element' may have changed */
9676 if (IS_CHANGING(x, y) &&
9677 (game.engine_version < VERSION_IDENT(3,0,7,1) || !Stop[x][y]))
9679 int page = element_info[element].event_page_nr[CE_DELAY];
9681 HandleElementChange(x, y, ChangePage[x][y] != -1 ? ChangePage[x][y] : page);
9685 printf("::: ChangeDelay == %d\n", ChangeDelay[x][y]);
9689 if (element == EL_CUSTOM_255)
9690 printf("::: ChangeDelay == %d\n", ChangeDelay[x][y]);
9694 HandleElementChange(x, y, page);
9696 if (CAN_CHANGE(element))
9697 HandleElementChange(x, y, page);
9699 if (HAS_ACTION(element))
9700 ExecuteCustomElementAction(x, y, element, page);
9705 element = Feld[x][y];
9706 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
9709 if (!IS_MOVING(x, y) && (CAN_FALL(element) || CAN_MOVE(element)))
9713 element = Feld[x][y];
9714 graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
9716 if (IS_ANIMATED(graphic) &&
9719 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
9721 if (IS_GEM(element) || element == EL_SP_INFOTRON)
9722 EdelsteinFunkeln(x, y);
9724 else if ((element == EL_ACID ||
9725 element == EL_EXIT_OPEN ||
9726 element == EL_SP_EXIT_OPEN ||
9727 element == EL_SP_TERMINAL ||
9728 element == EL_SP_TERMINAL_ACTIVE ||
9729 element == EL_EXTRA_TIME ||
9730 element == EL_SHIELD_NORMAL ||
9731 element == EL_SHIELD_DEADLY) &&
9732 IS_ANIMATED(graphic))
9733 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
9734 else if (IS_MOVING(x, y))
9735 ContinueMoving(x, y);
9736 else if (IS_ACTIVE_BOMB(element))
9737 CheckDynamite(x, y);
9738 else if (element == EL_AMOEBA_GROWING)
9739 AmoebeWaechst(x, y);
9740 else if (element == EL_AMOEBA_SHRINKING)
9741 AmoebaDisappearing(x, y);
9743 #if !USE_NEW_AMOEBA_CODE
9744 else if (IS_AMOEBALIVE(element))
9745 AmoebeAbleger(x, y);
9748 else if (element == EL_GAME_OF_LIFE || element == EL_BIOMAZE)
9750 else if (element == EL_EXIT_CLOSED)
9752 else if (element == EL_SP_EXIT_CLOSED)
9754 else if (element == EL_EXPANDABLE_WALL_GROWING)
9756 else if (element == EL_EXPANDABLE_WALL ||
9757 element == EL_EXPANDABLE_WALL_HORIZONTAL ||
9758 element == EL_EXPANDABLE_WALL_VERTICAL ||
9759 element == EL_EXPANDABLE_WALL_ANY)
9761 else if (element == EL_FLAMES)
9762 CheckForDragon(x, y);
9763 else if (element == EL_EXPLOSION)
9764 ; /* drawing of correct explosion animation is handled separately */
9765 else if (element == EL_ELEMENT_SNAPPING ||
9766 element == EL_DIAGONAL_SHRINKING ||
9767 element == EL_DIAGONAL_GROWING)
9770 graphic = el_act_dir2img(GfxElement[x][y], GfxAction[x][y],GfxDir[x][y]);
9772 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
9775 else if (IS_ANIMATED(graphic) && !IS_CHANGING(x, y))
9776 DrawLevelGraphicAnimationIfNeeded(x, y, graphic);
9779 if (element == EL_CUSTOM_255 ||
9780 element == EL_CUSTOM_256)
9781 DrawLevelGraphicAnimation(x, y, graphic);
9784 if (IS_BELT_ACTIVE(element))
9785 PlayLevelSoundAction(x, y, ACTION_ACTIVE);
9787 if (game.magic_wall_active)
9789 int jx = local_player->jx, jy = local_player->jy;
9791 /* play the element sound at the position nearest to the player */
9792 if ((element == EL_MAGIC_WALL_FULL ||
9793 element == EL_MAGIC_WALL_ACTIVE ||
9794 element == EL_MAGIC_WALL_EMPTYING ||
9795 element == EL_BD_MAGIC_WALL_FULL ||
9796 element == EL_BD_MAGIC_WALL_ACTIVE ||
9797 element == EL_BD_MAGIC_WALL_EMPTYING) &&
9798 ABS(x-jx) + ABS(y-jy) < ABS(magic_wall_x-jx) + ABS(magic_wall_y-jy))
9806 #if USE_NEW_AMOEBA_CODE
9807 /* new experimental amoeba growth stuff */
9808 if (!(FrameCounter % 8))
9810 static unsigned long random = 1684108901;
9812 for (i = 0; i < level.amoeba_speed * 28 / 8; i++)
9814 x = RND(lev_fieldx);
9815 y = RND(lev_fieldy);
9816 element = Feld[x][y];
9818 if (!IS_PLAYER(x,y) &&
9819 (element == EL_EMPTY ||
9820 CAN_GROW_INTO(element) ||
9821 element == EL_QUICKSAND_EMPTY ||
9822 element == EL_ACID_SPLASH_LEFT ||
9823 element == EL_ACID_SPLASH_RIGHT))
9825 if ((IN_LEV_FIELD(x, y-1) && Feld[x][y-1] == EL_AMOEBA_WET) ||
9826 (IN_LEV_FIELD(x-1, y) && Feld[x-1][y] == EL_AMOEBA_WET) ||
9827 (IN_LEV_FIELD(x+1, y) && Feld[x+1][y] == EL_AMOEBA_WET) ||
9828 (IN_LEV_FIELD(x, y+1) && Feld[x][y+1] == EL_AMOEBA_WET))
9829 Feld[x][y] = EL_AMOEBA_DROP;
9832 random = random * 129 + 1;
9838 if (game.explosions_delayed)
9841 game.explosions_delayed = FALSE;
9844 SCAN_PLAYFIELD(x, y)
9846 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
9849 element = Feld[x][y];
9851 if (ExplodeField[x][y])
9852 Explode(x, y, EX_PHASE_START, ExplodeField[x][y]);
9853 else if (element == EL_EXPLOSION)
9854 Explode(x, y, ExplodePhase[x][y], EX_TYPE_NORMAL);
9856 ExplodeField[x][y] = EX_TYPE_NONE;
9859 game.explosions_delayed = TRUE;
9862 if (game.magic_wall_active)
9864 if (!(game.magic_wall_time_left % 4))
9866 int element = Feld[magic_wall_x][magic_wall_y];
9868 if (element == EL_BD_MAGIC_WALL_FULL ||
9869 element == EL_BD_MAGIC_WALL_ACTIVE ||
9870 element == EL_BD_MAGIC_WALL_EMPTYING)
9871 PlayLevelSound(magic_wall_x, magic_wall_y, SND_BD_MAGIC_WALL_ACTIVE);
9873 PlayLevelSound(magic_wall_x, magic_wall_y, SND_MAGIC_WALL_ACTIVE);
9876 if (game.magic_wall_time_left > 0)
9878 game.magic_wall_time_left--;
9879 if (!game.magic_wall_time_left)
9882 SCAN_PLAYFIELD(x, y)
9884 for (y = 0; y < lev_fieldy; y++) for (x = 0; x < lev_fieldx; x++)
9887 element = Feld[x][y];
9889 if (element == EL_MAGIC_WALL_ACTIVE ||
9890 element == EL_MAGIC_WALL_FULL)
9892 Feld[x][y] = EL_MAGIC_WALL_DEAD;
9893 DrawLevelField(x, y);
9895 else if (element == EL_BD_MAGIC_WALL_ACTIVE ||
9896 element == EL_BD_MAGIC_WALL_FULL)
9898 Feld[x][y] = EL_BD_MAGIC_WALL_DEAD;
9899 DrawLevelField(x, y);
9903 game.magic_wall_active = FALSE;
9908 if (game.light_time_left > 0)
9910 game.light_time_left--;
9912 if (game.light_time_left == 0)
9913 RedrawAllLightSwitchesAndInvisibleElements();
9916 if (game.timegate_time_left > 0)
9918 game.timegate_time_left--;
9920 if (game.timegate_time_left == 0)
9921 CloseAllOpenTimegates();
9924 if (game.lenses_time_left > 0)
9926 game.lenses_time_left--;
9928 if (game.lenses_time_left == 0)
9929 RedrawAllInvisibleElementsForLenses();
9932 if (game.magnify_time_left > 0)
9934 game.magnify_time_left--;
9936 if (game.magnify_time_left == 0)
9937 RedrawAllInvisibleElementsForMagnifier();
9940 for (i = 0; i < MAX_PLAYERS; i++)
9942 struct PlayerInfo *player = &stored_player[i];
9944 if (SHIELD_ON(player))
9946 if (player->shield_deadly_time_left)
9947 PlayLevelSound(player->jx, player->jy, SND_SHIELD_DEADLY_ACTIVE);
9948 else if (player->shield_normal_time_left)
9949 PlayLevelSound(player->jx, player->jy, SND_SHIELD_NORMAL_ACTIVE);
9956 PlayAllPlayersSound();
9958 if (options.debug) /* calculate frames per second */
9960 static unsigned long fps_counter = 0;
9961 static int fps_frames = 0;
9962 unsigned long fps_delay_ms = Counter() - fps_counter;
9966 if (fps_delay_ms >= 500) /* calculate fps every 0.5 seconds */
9968 global.frames_per_second = 1000 * (float)fps_frames / fps_delay_ms;
9971 fps_counter = Counter();
9974 redraw_mask |= REDRAW_FPS;
9977 AdvanceFrameAndPlayerCounters(-1); /* advance counters for all players */
9979 if (local_player->show_envelope != 0 && local_player->MovPos == 0)
9981 ShowEnvelope(local_player->show_envelope - EL_ENVELOPE_1);
9983 local_player->show_envelope = 0;
9986 /* use random number generator in every frame to make it less predictable */
9987 if (game.engine_version >= VERSION_IDENT(3,1,1,0))
9991 static boolean AllPlayersInSight(struct PlayerInfo *player, int x, int y)
9993 int min_x = x, min_y = y, max_x = x, max_y = y;
9996 for (i = 0; i < MAX_PLAYERS; i++)
9998 int jx = stored_player[i].jx, jy = stored_player[i].jy;
10000 if (!stored_player[i].active || &stored_player[i] == player)
10003 min_x = MIN(min_x, jx);
10004 min_y = MIN(min_y, jy);
10005 max_x = MAX(max_x, jx);
10006 max_y = MAX(max_y, jy);
10009 return (max_x - min_x < SCR_FIELDX && max_y - min_y < SCR_FIELDY);
10012 static boolean AllPlayersInVisibleScreen()
10016 for (i = 0; i < MAX_PLAYERS; i++)
10018 int jx = stored_player[i].jx, jy = stored_player[i].jy;
10020 if (!stored_player[i].active)
10023 if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
10030 void ScrollLevel(int dx, int dy)
10032 int softscroll_offset = (setup.soft_scrolling ? TILEX : 0);
10035 BlitBitmap(drawto_field, drawto_field,
10036 FX + TILEX * (dx == -1) - softscroll_offset,
10037 FY + TILEY * (dy == -1) - softscroll_offset,
10038 SXSIZE - TILEX * (dx!=0) + 2 * softscroll_offset,
10039 SYSIZE - TILEY * (dy!=0) + 2 * softscroll_offset,
10040 FX + TILEX * (dx == 1) - softscroll_offset,
10041 FY + TILEY * (dy == 1) - softscroll_offset);
10045 x = (dx == 1 ? BX1 : BX2);
10046 for (y = BY1; y <= BY2; y++)
10047 DrawScreenField(x, y);
10052 y = (dy == 1 ? BY1 : BY2);
10053 for (x = BX1; x <= BX2; x++)
10054 DrawScreenField(x, y);
10057 redraw_mask |= REDRAW_FIELD;
10060 static boolean canFallDown(struct PlayerInfo *player)
10062 int jx = player->jx, jy = player->jy;
10064 return (IN_LEV_FIELD(jx, jy + 1) &&
10065 (IS_FREE(jx, jy + 1) ||
10066 (Feld[jx][jy + 1] == EL_ACID && player->can_fall_into_acid)) &&
10067 IS_WALKABLE_FROM(Feld[jx][jy], MV_DOWN) &&
10068 !IS_WALKABLE_INSIDE(Feld[jx][jy]));
10071 static boolean canPassField(int x, int y, int move_dir)
10073 int opposite_dir = MV_DIR_OPPOSITE(move_dir);
10074 int dx = (move_dir & MV_LEFT ? -1 : move_dir & MV_RIGHT ? +1 : 0);
10075 int dy = (move_dir & MV_UP ? -1 : move_dir & MV_DOWN ? +1 : 0);
10076 int nextx = x + dx;
10077 int nexty = y + dy;
10078 int element = Feld[x][y];
10080 return (IS_PASSABLE_FROM(element, opposite_dir) &&
10081 !CAN_MOVE(element) &&
10082 IN_LEV_FIELD(nextx, nexty) && !IS_PLAYER(nextx, nexty) &&
10083 IS_WALKABLE_FROM(Feld[nextx][nexty], move_dir) &&
10084 (level.can_pass_to_walkable || IS_FREE(nextx, nexty)));
10087 static boolean canMoveToValidFieldWithGravity(int x, int y, int move_dir)
10089 int opposite_dir = MV_DIR_OPPOSITE(move_dir);
10090 int dx = (move_dir & MV_LEFT ? -1 : move_dir & MV_RIGHT ? +1 : 0);
10091 int dy = (move_dir & MV_UP ? -1 : move_dir & MV_DOWN ? +1 : 0);
10095 return (IN_LEV_FIELD(newx, newy) && !IS_FREE_OR_PLAYER(newx, newy) &&
10096 IS_GRAVITY_REACHABLE(Feld[newx][newy]) &&
10097 (IS_DIGGABLE(Feld[newx][newy]) ||
10098 IS_WALKABLE_FROM(Feld[newx][newy], opposite_dir) ||
10099 canPassField(newx, newy, move_dir)));
10102 static void CheckGravityMovement(struct PlayerInfo *player)
10104 if (game.gravity && !player->programmed_action)
10106 int move_dir_horizontal = player->effective_action & MV_HORIZONTAL;
10107 int move_dir_vertical = player->effective_action & MV_VERTICAL;
10108 boolean player_is_snapping = player->effective_action & JOY_BUTTON_1;
10109 int jx = player->jx, jy = player->jy;
10110 boolean player_is_moving_to_valid_field =
10111 (!player_is_snapping &&
10112 (canMoveToValidFieldWithGravity(jx, jy, move_dir_horizontal) ||
10113 canMoveToValidFieldWithGravity(jx, jy, move_dir_vertical)));
10114 boolean player_can_fall_down = canFallDown(player);
10116 if (player_can_fall_down &&
10117 !player_is_moving_to_valid_field)
10118 player->programmed_action = MV_DOWN;
10122 static void CheckGravityMovementWhenNotMoving(struct PlayerInfo *player)
10124 return CheckGravityMovement(player);
10126 if (game.gravity && !player->programmed_action)
10128 int jx = player->jx, jy = player->jy;
10129 boolean field_under_player_is_free =
10130 (IN_LEV_FIELD(jx, jy + 1) && IS_FREE(jx, jy + 1));
10131 boolean player_is_standing_on_valid_field =
10132 (IS_WALKABLE_INSIDE(Feld[jx][jy]) ||
10133 (IS_WALKABLE(Feld[jx][jy]) &&
10134 !(element_info[Feld[jx][jy]].access_direction & MV_DOWN)));
10136 if (field_under_player_is_free && !player_is_standing_on_valid_field)
10137 player->programmed_action = MV_DOWN;
10142 MovePlayerOneStep()
10143 -----------------------------------------------------------------------------
10144 dx, dy: direction (non-diagonal) to try to move the player to
10145 real_dx, real_dy: direction as read from input device (can be diagonal)
10148 boolean MovePlayerOneStep(struct PlayerInfo *player,
10149 int dx, int dy, int real_dx, int real_dy)
10151 int jx = player->jx, jy = player->jy;
10152 int new_jx = jx + dx, new_jy = jy + dy;
10153 #if !USE_FIXED_DONT_RUN_INTO
10157 boolean player_can_move = !player->cannot_move;
10159 if (!player->active || (!dx && !dy))
10160 return MP_NO_ACTION;
10162 player->MovDir = (dx < 0 ? MV_LEFT :
10163 dx > 0 ? MV_RIGHT :
10165 dy > 0 ? MV_DOWN : MV_NONE);
10167 if (!IN_LEV_FIELD(new_jx, new_jy))
10168 return MP_NO_ACTION;
10170 if (!player_can_move)
10173 if (player->MovPos == 0)
10175 player->is_moving = FALSE;
10176 player->is_digging = FALSE;
10177 player->is_collecting = FALSE;
10178 player->is_snapping = FALSE;
10179 player->is_pushing = FALSE;
10182 DigField(player, 0, 0, 0, 0, 0, 0, DF_NO_PUSH);
10183 SnapField(player, 0, 0);
10187 return MP_NO_ACTION;
10192 if (!options.network && game.centered_player_nr == -1 &&
10193 !AllPlayersInSight(player, new_jx, new_jy))
10194 return MP_NO_ACTION;
10196 if (!options.network && !AllPlayersInSight(player, new_jx, new_jy))
10197 return MP_NO_ACTION;
10200 #if !USE_FIXED_DONT_RUN_INTO
10201 element = MovingOrBlocked2ElementIfNotLeaving(new_jx, new_jy);
10203 /* (moved to DigField()) */
10204 if (player_can_move && DONT_RUN_INTO(element))
10206 if (element == EL_ACID && dx == 0 && dy == 1)
10208 SplashAcid(new_jx, new_jy);
10209 Feld[jx][jy] = EL_PLAYER_1;
10210 InitMovingField(jx, jy, MV_DOWN);
10211 Store[jx][jy] = EL_ACID;
10212 ContinueMoving(jx, jy);
10213 BuryPlayer(player);
10216 TestIfPlayerRunsIntoBadThing(jx, jy, player->MovDir);
10222 can_move = DigField(player, jx, jy, new_jx, new_jy, real_dx,real_dy, DF_DIG);
10224 #if USE_FIXED_DONT_RUN_INTO
10225 if (can_move == MP_DONT_RUN_INTO)
10229 if (can_move != MP_MOVING)
10232 #if USE_FIXED_DONT_RUN_INTO
10235 /* check if DigField() has caused relocation of the player */
10236 if (player->jx != jx || player->jy != jy)
10237 return MP_NO_ACTION; /* <-- !!! CHECK THIS [-> MP_ACTION ?] !!! */
10239 StorePlayer[jx][jy] = 0;
10240 player->last_jx = jx;
10241 player->last_jy = jy;
10242 player->jx = new_jx;
10243 player->jy = new_jy;
10244 StorePlayer[new_jx][new_jy] = player->element_nr;
10246 if (player->move_delay_value_next != -1)
10248 player->move_delay_value = player->move_delay_value_next;
10249 player->move_delay_value_next = -1;
10253 (dx > 0 || dy > 0 ? -1 : 1) * (TILEX - TILEX / player->move_delay_value);
10255 player->step_counter++;
10257 PlayerVisit[jx][jy] = FrameCounter;
10259 ScrollPlayer(player, SCROLL_INIT);
10264 boolean MovePlayer(struct PlayerInfo *player, int dx, int dy)
10266 int jx = player->jx, jy = player->jy;
10267 int old_jx = jx, old_jy = jy;
10268 int moved = MP_NO_ACTION;
10270 if (!player->active)
10275 if (player->MovPos == 0)
10277 player->is_moving = FALSE;
10278 player->is_digging = FALSE;
10279 player->is_collecting = FALSE;
10280 player->is_snapping = FALSE;
10281 player->is_pushing = FALSE;
10287 if (player->move_delay > 0)
10290 player->move_delay = -1; /* set to "uninitialized" value */
10292 /* store if player is automatically moved to next field */
10293 player->is_auto_moving = (player->programmed_action != MV_NONE);
10295 /* remove the last programmed player action */
10296 player->programmed_action = 0;
10298 if (player->MovPos)
10300 /* should only happen if pre-1.2 tape recordings are played */
10301 /* this is only for backward compatibility */
10303 int original_move_delay_value = player->move_delay_value;
10306 printf("THIS SHOULD ONLY HAPPEN WITH PRE-1.2 LEVEL TAPES. [%ld]\n",
10310 /* scroll remaining steps with finest movement resolution */
10311 player->move_delay_value = MOVE_DELAY_NORMAL_SPEED;
10313 while (player->MovPos)
10315 ScrollPlayer(player, SCROLL_GO_ON);
10316 ScrollScreen(NULL, SCROLL_GO_ON);
10318 AdvanceFrameAndPlayerCounters(player->index_nr);
10324 player->move_delay_value = original_move_delay_value;
10327 if (player->last_move_dir & MV_HORIZONTAL)
10329 if (!(moved |= MovePlayerOneStep(player, 0, dy, dx, dy)))
10330 moved |= MovePlayerOneStep(player, dx, 0, dx, dy);
10334 if (!(moved |= MovePlayerOneStep(player, dx, 0, dx, dy)))
10335 moved |= MovePlayerOneStep(player, 0, dy, dx, dy);
10342 if (moved & MP_MOVING && !ScreenMovPos &&
10343 (player->index_nr == game.centered_player_nr ||
10344 game.centered_player_nr == -1))
10346 if (moved & MP_MOVING && !ScreenMovPos &&
10347 (player == local_player || !options.network))
10350 int old_scroll_x = scroll_x, old_scroll_y = scroll_y;
10351 int offset = (setup.scroll_delay ? 3 : 0);
10353 if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
10355 /* actual player has left the screen -- scroll in that direction */
10356 if (jx != old_jx) /* player has moved horizontally */
10357 scroll_x += (jx - old_jx);
10358 else /* player has moved vertically */
10359 scroll_y += (jy - old_jy);
10363 if (jx != old_jx) /* player has moved horizontally */
10365 if ((player->MovDir == MV_LEFT && scroll_x > jx - MIDPOSX + offset) ||
10366 (player->MovDir == MV_RIGHT && scroll_x < jx - MIDPOSX - offset))
10367 scroll_x = jx-MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
10369 /* don't scroll over playfield boundaries */
10370 if (scroll_x < SBX_Left || scroll_x > SBX_Right)
10371 scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
10373 /* don't scroll more than one field at a time */
10374 scroll_x = old_scroll_x + SIGN(scroll_x - old_scroll_x);
10376 /* don't scroll against the player's moving direction */
10377 if ((player->MovDir == MV_LEFT && scroll_x > old_scroll_x) ||
10378 (player->MovDir == MV_RIGHT && scroll_x < old_scroll_x))
10379 scroll_x = old_scroll_x;
10381 else /* player has moved vertically */
10383 if ((player->MovDir == MV_UP && scroll_y > jy - MIDPOSY + offset) ||
10384 (player->MovDir == MV_DOWN && scroll_y < jy - MIDPOSY - offset))
10385 scroll_y = jy-MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
10387 /* don't scroll over playfield boundaries */
10388 if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
10389 scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
10391 /* don't scroll more than one field at a time */
10392 scroll_y = old_scroll_y + SIGN(scroll_y - old_scroll_y);
10394 /* don't scroll against the player's moving direction */
10395 if ((player->MovDir == MV_UP && scroll_y > old_scroll_y) ||
10396 (player->MovDir == MV_DOWN && scroll_y < old_scroll_y))
10397 scroll_y = old_scroll_y;
10401 if (scroll_x != old_scroll_x || scroll_y != old_scroll_y)
10404 if (!options.network && game.centered_player_nr == -1 &&
10405 !AllPlayersInVisibleScreen())
10407 scroll_x = old_scroll_x;
10408 scroll_y = old_scroll_y;
10412 if (!options.network && !AllPlayersInVisibleScreen())
10414 scroll_x = old_scroll_x;
10415 scroll_y = old_scroll_y;
10420 ScrollScreen(player, SCROLL_INIT);
10421 ScrollLevel(old_scroll_x - scroll_x, old_scroll_y - scroll_y);
10426 player->StepFrame = 0;
10428 if (moved & MP_MOVING)
10430 if (old_jx != jx && old_jy == jy)
10431 player->MovDir = (old_jx < jx ? MV_RIGHT : MV_LEFT);
10432 else if (old_jx == jx && old_jy != jy)
10433 player->MovDir = (old_jy < jy ? MV_DOWN : MV_UP);
10435 DrawLevelField(jx, jy); /* for "crumbled sand" */
10437 player->last_move_dir = player->MovDir;
10438 player->is_moving = TRUE;
10439 player->is_snapping = FALSE;
10440 player->is_switching = FALSE;
10441 player->is_dropping = FALSE;
10442 player->is_dropping_pressed = FALSE;
10443 player->drop_pressed_delay = 0;
10447 CheckGravityMovementWhenNotMoving(player);
10449 player->is_moving = FALSE;
10451 /* at this point, the player is allowed to move, but cannot move right now
10452 (e.g. because of something blocking the way) -- ensure that the player
10453 is also allowed to move in the next frame (in old versions before 3.1.1,
10454 the player was forced to wait again for eight frames before next try) */
10456 if (game.engine_version >= VERSION_IDENT(3,1,1,0))
10457 player->move_delay = 0; /* allow direct movement in the next frame */
10460 if (player->move_delay == -1) /* not yet initialized by DigField() */
10461 player->move_delay = player->move_delay_value;
10463 if (game.engine_version < VERSION_IDENT(3,0,7,0))
10465 TestIfPlayerTouchesBadThing(jx, jy);
10466 TestIfPlayerTouchesCustomElement(jx, jy);
10469 if (!player->active)
10470 RemovePlayer(player);
10475 void ScrollPlayer(struct PlayerInfo *player, int mode)
10477 int jx = player->jx, jy = player->jy;
10478 int last_jx = player->last_jx, last_jy = player->last_jy;
10479 int move_stepsize = TILEX / player->move_delay_value;
10481 #if USE_NEW_PLAYER_SPEED
10482 if (!player->active)
10485 if (player->MovPos == 0 && mode == SCROLL_GO_ON) /* player not moving */
10488 if (!player->active || player->MovPos == 0)
10492 if (mode == SCROLL_INIT)
10494 player->actual_frame_counter = FrameCounter;
10495 player->GfxPos = move_stepsize * (player->MovPos / move_stepsize);
10497 if ((player->block_last_field || player->block_delay_adjustment > 0) &&
10498 Feld[last_jx][last_jy] == EL_EMPTY)
10500 int last_field_block_delay = 0; /* start with no blocking at all */
10501 int block_delay_adjustment = player->block_delay_adjustment;
10503 /* if player blocks last field, add delay for exactly one move */
10504 if (player->block_last_field)
10506 last_field_block_delay += player->move_delay_value;
10508 /* when blocking enabled, prevent moving up despite gravity */
10509 if (game.gravity && player->MovDir == MV_UP)
10510 block_delay_adjustment = -1;
10513 /* add block delay adjustment (also possible when not blocking) */
10514 last_field_block_delay += block_delay_adjustment;
10516 Feld[last_jx][last_jy] = EL_PLAYER_IS_LEAVING;
10517 MovDelay[last_jx][last_jy] = last_field_block_delay + 1;
10520 #if USE_NEW_PLAYER_SPEED
10521 if (player->MovPos != 0) /* player has not yet reached destination */
10527 else if (!FrameReached(&player->actual_frame_counter, 1))
10531 printf("::: player->MovPos: %d -> %d\n",
10533 player->MovPos + (player->MovPos > 0 ? -1 : 1) * move_stepsize);
10536 #if USE_NEW_PLAYER_SPEED
10537 if (player->MovPos != 0)
10539 player->MovPos += (player->MovPos > 0 ? -1 : 1) * move_stepsize;
10540 player->GfxPos = move_stepsize * (player->MovPos / move_stepsize);
10542 /* before DrawPlayer() to draw correct player graphic for this case */
10543 if (player->MovPos == 0)
10544 CheckGravityMovement(player);
10547 player->MovPos += (player->MovPos > 0 ? -1 : 1) * move_stepsize;
10548 player->GfxPos = move_stepsize * (player->MovPos / move_stepsize);
10550 /* before DrawPlayer() to draw correct player graphic for this case */
10551 if (player->MovPos == 0)
10552 CheckGravityMovement(player);
10555 if (player->MovPos == 0) /* player reached destination field */
10558 printf("::: player reached destination field\n");
10561 if (player->move_delay_reset_counter > 0)
10563 player->move_delay_reset_counter--;
10565 if (player->move_delay_reset_counter == 0)
10567 /* continue with normal speed after quickly moving through gate */
10568 HALVE_PLAYER_SPEED(player);
10570 /* be able to make the next move without delay */
10571 player->move_delay = 0;
10575 player->last_jx = jx;
10576 player->last_jy = jy;
10578 if (Feld[jx][jy] == EL_EXIT_OPEN ||
10579 Feld[jx][jy] == EL_SP_EXIT_OPEN ||
10580 Feld[jx][jy] == EL_SP_EXIT_OPENING) /* <-- special case */
10582 DrawPlayer(player); /* needed here only to cleanup last field */
10583 RemovePlayer(player);
10585 if (local_player->friends_still_needed == 0 ||
10586 IS_SP_ELEMENT(Feld[jx][jy]))
10587 player->LevelSolved = player->GameOver = TRUE;
10590 /* this breaks one level: "machine", level 000 */
10592 int move_direction = player->MovDir;
10593 int enter_side = MV_DIR_OPPOSITE(move_direction);
10594 int leave_side = move_direction;
10595 int old_jx = last_jx;
10596 int old_jy = last_jy;
10597 int old_element = Feld[old_jx][old_jy];
10598 int new_element = Feld[jx][jy];
10600 if (IS_CUSTOM_ELEMENT(old_element))
10601 CheckElementChangeByPlayer(old_jx, old_jy, old_element,
10603 player->index_bit, leave_side);
10605 CheckTriggeredElementChangeByPlayer(old_jx, old_jy, old_element,
10606 CE_PLAYER_LEAVES_X,
10607 player->index_bit, leave_side);
10609 if (IS_CUSTOM_ELEMENT(new_element))
10610 CheckElementChangeByPlayer(jx, jy, new_element, CE_ENTERED_BY_PLAYER,
10611 player->index_bit, enter_side);
10613 CheckTriggeredElementChangeByPlayer(jx, jy, new_element,
10614 CE_PLAYER_ENTERS_X,
10615 player->index_bit, enter_side);
10617 CheckTriggeredElementChangeBySide(jx, jy, player->element_nr,
10618 CE_MOVE_OF_X, move_direction);
10621 if (game.engine_version >= VERSION_IDENT(3,0,7,0))
10623 TestIfPlayerTouchesBadThing(jx, jy);
10624 TestIfPlayerTouchesCustomElement(jx, jy);
10626 /* needed because pushed element has not yet reached its destination,
10627 so it would trigger a change event at its previous field location */
10628 if (!player->is_pushing)
10629 TestIfElementTouchesCustomElement(jx, jy); /* for empty space */
10631 if (!player->active)
10632 RemovePlayer(player);
10635 if (level.use_step_counter)
10645 if (TimeLeft <= 10 && setup.time_limit)
10646 PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
10648 DrawGameValue_Time(TimeLeft);
10650 if (!TimeLeft && setup.time_limit)
10651 for (i = 0; i < MAX_PLAYERS; i++)
10652 KillPlayer(&stored_player[i]);
10654 else if (level.time == 0 && !AllPlayersGone) /* level w/o time limit */
10655 DrawGameValue_Time(TimePlayed);
10658 if (tape.single_step && tape.recording && !tape.pausing &&
10659 !player->programmed_action)
10660 TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
10664 void ScrollScreen(struct PlayerInfo *player, int mode)
10666 static unsigned long screen_frame_counter = 0;
10668 if (mode == SCROLL_INIT)
10670 /* set scrolling step size according to actual player's moving speed */
10671 ScrollStepSize = TILEX / player->move_delay_value;
10673 screen_frame_counter = FrameCounter;
10674 ScreenMovDir = player->MovDir;
10675 ScreenMovPos = player->MovPos;
10676 ScreenGfxPos = ScrollStepSize * (ScreenMovPos / ScrollStepSize);
10679 else if (!FrameReached(&screen_frame_counter, 1))
10684 ScreenMovPos += (ScreenMovPos > 0 ? -1 : 1) * ScrollStepSize;
10685 ScreenGfxPos = ScrollStepSize * (ScreenMovPos / ScrollStepSize);
10686 redraw_mask |= REDRAW_FIELD;
10689 ScreenMovDir = MV_NONE;
10692 void TestIfPlayerTouchesCustomElement(int x, int y)
10694 static int xy[4][2] =
10701 static int trigger_sides[4][2] =
10703 /* center side border side */
10704 { CH_SIDE_TOP, CH_SIDE_BOTTOM }, /* check top */
10705 { CH_SIDE_LEFT, CH_SIDE_RIGHT }, /* check left */
10706 { CH_SIDE_RIGHT, CH_SIDE_LEFT }, /* check right */
10707 { CH_SIDE_BOTTOM, CH_SIDE_TOP } /* check bottom */
10709 static int touch_dir[4] =
10711 MV_LEFT | MV_RIGHT,
10716 int center_element = Feld[x][y]; /* should always be non-moving! */
10719 for (i = 0; i < NUM_DIRECTIONS; i++)
10721 int xx = x + xy[i][0];
10722 int yy = y + xy[i][1];
10723 int center_side = trigger_sides[i][0];
10724 int border_side = trigger_sides[i][1];
10725 int border_element;
10727 if (!IN_LEV_FIELD(xx, yy))
10730 if (IS_PLAYER(x, y))
10732 struct PlayerInfo *player = PLAYERINFO(x, y);
10734 if (game.engine_version < VERSION_IDENT(3,0,7,0))
10735 border_element = Feld[xx][yy]; /* may be moving! */
10736 else if (!IS_MOVING(xx, yy) && !IS_BLOCKED(xx, yy))
10737 border_element = Feld[xx][yy];
10738 else if (MovDir[xx][yy] & touch_dir[i]) /* elements are touching */
10739 border_element = MovingOrBlocked2Element(xx, yy);
10741 continue; /* center and border element do not touch */
10743 CheckElementChangeByPlayer(xx, yy, border_element, CE_TOUCHED_BY_PLAYER,
10744 player->index_bit, border_side);
10745 CheckTriggeredElementChangeByPlayer(xx, yy, border_element,
10746 CE_PLAYER_TOUCHES_X,
10747 player->index_bit, border_side);
10749 else if (IS_PLAYER(xx, yy))
10751 struct PlayerInfo *player = PLAYERINFO(xx, yy);
10753 if (game.engine_version >= VERSION_IDENT(3,0,7,0))
10755 if (player->MovPos != 0 && !(player->MovDir & touch_dir[i]))
10756 continue; /* center and border element do not touch */
10759 CheckElementChangeByPlayer(x, y, center_element, CE_TOUCHED_BY_PLAYER,
10760 player->index_bit, center_side);
10761 CheckTriggeredElementChangeByPlayer(x, y, center_element,
10762 CE_PLAYER_TOUCHES_X,
10763 player->index_bit, center_side);
10769 #if USE_ELEMENT_TOUCHING_BUGFIX
10771 void TestIfElementTouchesCustomElement(int x, int y)
10773 static int xy[4][2] =
10780 static int trigger_sides[4][2] =
10782 /* center side border side */
10783 { CH_SIDE_TOP, CH_SIDE_BOTTOM }, /* check top */
10784 { CH_SIDE_LEFT, CH_SIDE_RIGHT }, /* check left */
10785 { CH_SIDE_RIGHT, CH_SIDE_LEFT }, /* check right */
10786 { CH_SIDE_BOTTOM, CH_SIDE_TOP } /* check bottom */
10788 static int touch_dir[4] =
10790 MV_LEFT | MV_RIGHT,
10795 boolean change_center_element = FALSE;
10796 int center_element = Feld[x][y]; /* should always be non-moving! */
10797 int border_element_old[NUM_DIRECTIONS];
10800 for (i = 0; i < NUM_DIRECTIONS; i++)
10802 int xx = x + xy[i][0];
10803 int yy = y + xy[i][1];
10804 int border_element;
10806 border_element_old[i] = -1;
10808 if (!IN_LEV_FIELD(xx, yy))
10811 if (game.engine_version < VERSION_IDENT(3,0,7,0))
10812 border_element = Feld[xx][yy]; /* may be moving! */
10813 else if (!IS_MOVING(xx, yy) && !IS_BLOCKED(xx, yy))
10814 border_element = Feld[xx][yy];
10815 else if (MovDir[xx][yy] & touch_dir[i]) /* elements are touching */
10816 border_element = MovingOrBlocked2Element(xx, yy);
10818 continue; /* center and border element do not touch */
10820 border_element_old[i] = border_element;
10823 for (i = 0; i < NUM_DIRECTIONS; i++)
10825 int xx = x + xy[i][0];
10826 int yy = y + xy[i][1];
10827 int center_side = trigger_sides[i][0];
10828 int border_element = border_element_old[i];
10830 if (border_element == -1)
10833 /* check for change of border element */
10834 CheckElementChangeBySide(xx, yy, border_element, center_element,
10835 CE_TOUCHING_X, center_side);
10838 for (i = 0; i < NUM_DIRECTIONS; i++)
10840 int border_side = trigger_sides[i][1];
10841 int border_element = border_element_old[i];
10843 if (border_element == -1)
10846 /* check for change of center element (but change it only once) */
10847 if (!change_center_element)
10848 change_center_element =
10849 CheckElementChangeBySide(x, y, center_element, border_element,
10850 CE_TOUCHING_X, border_side);
10856 void TestIfElementTouchesCustomElement_OLD(int x, int y)
10858 static int xy[4][2] =
10865 static int trigger_sides[4][2] =
10867 /* center side border side */
10868 { CH_SIDE_TOP, CH_SIDE_BOTTOM }, /* check top */
10869 { CH_SIDE_LEFT, CH_SIDE_RIGHT }, /* check left */
10870 { CH_SIDE_RIGHT, CH_SIDE_LEFT }, /* check right */
10871 { CH_SIDE_BOTTOM, CH_SIDE_TOP } /* check bottom */
10873 static int touch_dir[4] =
10875 MV_LEFT | MV_RIGHT,
10880 boolean change_center_element = FALSE;
10881 int center_element = Feld[x][y]; /* should always be non-moving! */
10884 for (i = 0; i < NUM_DIRECTIONS; i++)
10886 int xx = x + xy[i][0];
10887 int yy = y + xy[i][1];
10888 int center_side = trigger_sides[i][0];
10889 int border_side = trigger_sides[i][1];
10890 int border_element;
10892 if (!IN_LEV_FIELD(xx, yy))
10895 if (game.engine_version < VERSION_IDENT(3,0,7,0))
10896 border_element = Feld[xx][yy]; /* may be moving! */
10897 else if (!IS_MOVING(xx, yy) && !IS_BLOCKED(xx, yy))
10898 border_element = Feld[xx][yy];
10899 else if (MovDir[xx][yy] & touch_dir[i]) /* elements are touching */
10900 border_element = MovingOrBlocked2Element(xx, yy);
10902 continue; /* center and border element do not touch */
10904 /* check for change of center element (but change it only once) */
10905 if (!change_center_element)
10906 change_center_element =
10907 CheckElementChangeBySide(x, y, center_element, border_element,
10908 CE_TOUCHING_X, border_side);
10910 /* check for change of border element */
10911 CheckElementChangeBySide(xx, yy, border_element, center_element,
10912 CE_TOUCHING_X, center_side);
10918 void TestIfElementHitsCustomElement(int x, int y, int direction)
10920 int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
10921 int dy = (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
10922 int hitx = x + dx, hity = y + dy;
10923 int hitting_element = Feld[x][y];
10924 int touched_element;
10926 if (IN_LEV_FIELD(hitx, hity) && IS_FREE(hitx, hity))
10929 touched_element = (IN_LEV_FIELD(hitx, hity) ?
10930 MovingOrBlocked2Element(hitx, hity) : EL_STEELWALL);
10932 if (IN_LEV_FIELD(hitx, hity))
10934 int opposite_direction = MV_DIR_OPPOSITE(direction);
10935 int hitting_side = direction;
10936 int touched_side = opposite_direction;
10937 boolean object_hit = (!IS_MOVING(hitx, hity) ||
10938 MovDir[hitx][hity] != direction ||
10939 ABS(MovPos[hitx][hity]) <= TILEY / 2);
10945 CheckElementChangeBySide(x, y, hitting_element, touched_element,
10946 CE_HITTING_X, touched_side);
10948 CheckElementChangeBySide(hitx, hity, touched_element,
10949 hitting_element, CE_HIT_BY_X, hitting_side);
10951 CheckElementChangeBySide(hitx, hity, touched_element, hitting_element,
10952 CE_HIT_BY_SOMETHING, opposite_direction);
10956 /* "hitting something" is also true when hitting the playfield border */
10957 CheckElementChangeBySide(x, y, hitting_element, touched_element,
10958 CE_HITTING_SOMETHING, direction);
10962 void TestIfElementSmashesCustomElement(int x, int y, int direction)
10964 int dx = (direction == MV_LEFT ? -1 : direction == MV_RIGHT ? +1 : 0);
10965 int dy = (direction == MV_UP ? -1 : direction == MV_DOWN ? +1 : 0);
10966 int hitx = x + dx, hity = y + dy;
10967 int hitting_element = Feld[x][y];
10968 int touched_element;
10970 boolean object_hit = (IN_LEV_FIELD(hitx, hity) &&
10971 !IS_FREE(hitx, hity) &&
10972 (!IS_MOVING(hitx, hity) ||
10973 MovDir[hitx][hity] != direction ||
10974 ABS(MovPos[hitx][hity]) <= TILEY / 2));
10977 if (IN_LEV_FIELD(hitx, hity) && IS_FREE(hitx, hity))
10981 if (IN_LEV_FIELD(hitx, hity) && !object_hit)
10985 touched_element = (IN_LEV_FIELD(hitx, hity) ?
10986 MovingOrBlocked2Element(hitx, hity) : EL_STEELWALL);
10988 CheckElementChangeBySide(x, y, hitting_element, touched_element,
10989 EP_CAN_SMASH_EVERYTHING, direction);
10991 if (IN_LEV_FIELD(hitx, hity))
10993 int opposite_direction = MV_DIR_OPPOSITE(direction);
10994 int hitting_side = direction;
10995 int touched_side = opposite_direction;
10997 int touched_element = MovingOrBlocked2Element(hitx, hity);
11000 boolean object_hit = (!IS_MOVING(hitx, hity) ||
11001 MovDir[hitx][hity] != direction ||
11002 ABS(MovPos[hitx][hity]) <= TILEY / 2);
11011 CheckElementChangeBySide(hitx, hity, touched_element, hitting_element,
11012 CE_SMASHED_BY_SOMETHING, opposite_direction);
11014 CheckElementChangeBySide(x, y, hitting_element, touched_element,
11015 CE_OTHER_IS_SMASHING, touched_side);
11017 CheckElementChangeBySide(hitx, hity, touched_element, hitting_element,
11018 CE_OTHER_GETS_SMASHED, hitting_side);
11024 void TestIfGoodThingHitsBadThing(int good_x, int good_y, int good_move_dir)
11026 int i, kill_x = -1, kill_y = -1;
11028 int bad_element = -1;
11029 static int test_xy[4][2] =
11036 static int test_dir[4] =
11044 for (i = 0; i < NUM_DIRECTIONS; i++)
11046 int test_x, test_y, test_move_dir, test_element;
11048 test_x = good_x + test_xy[i][0];
11049 test_y = good_y + test_xy[i][1];
11051 if (!IN_LEV_FIELD(test_x, test_y))
11055 (IS_MOVING(test_x, test_y) ? MovDir[test_x][test_y] : MV_NONE);
11057 test_element = MovingOrBlocked2ElementIfNotLeaving(test_x, test_y);
11059 /* 1st case: good thing is moving towards DONT_RUN_INTO style bad thing;
11060 2nd case: DONT_TOUCH style bad thing does not move away from good thing
11062 if ((DONT_RUN_INTO(test_element) && good_move_dir == test_dir[i]) ||
11063 (DONT_TOUCH(test_element) && test_move_dir != test_dir[i]))
11067 bad_element = test_element;
11073 if (kill_x != -1 || kill_y != -1)
11075 if (IS_PLAYER(good_x, good_y))
11077 struct PlayerInfo *player = PLAYERINFO(good_x, good_y);
11079 if (player->shield_deadly_time_left > 0 &&
11080 !IS_INDESTRUCTIBLE(bad_element))
11081 Bang(kill_x, kill_y);
11082 else if (!PLAYER_ENEMY_PROTECTED(good_x, good_y))
11083 KillPlayer(player);
11086 Bang(good_x, good_y);
11090 void TestIfBadThingHitsGoodThing(int bad_x, int bad_y, int bad_move_dir)
11092 int i, kill_x = -1, kill_y = -1;
11093 int bad_element = Feld[bad_x][bad_y];
11094 static int test_xy[4][2] =
11101 static int touch_dir[4] =
11103 MV_LEFT | MV_RIGHT,
11108 static int test_dir[4] =
11116 if (bad_element == EL_EXPLOSION) /* skip just exploding bad things */
11119 for (i = 0; i < NUM_DIRECTIONS; i++)
11121 int test_x, test_y, test_move_dir, test_element;
11123 test_x = bad_x + test_xy[i][0];
11124 test_y = bad_y + test_xy[i][1];
11125 if (!IN_LEV_FIELD(test_x, test_y))
11129 (IS_MOVING(test_x, test_y) ? MovDir[test_x][test_y] : MV_NONE);
11131 test_element = Feld[test_x][test_y];
11133 /* 1st case: good thing is moving towards DONT_RUN_INTO style bad thing;
11134 2nd case: DONT_TOUCH style bad thing does not move away from good thing
11136 if ((DONT_RUN_INTO(bad_element) && bad_move_dir == test_dir[i]) ||
11137 (DONT_TOUCH(bad_element) && test_move_dir != test_dir[i]))
11139 /* good thing is player or penguin that does not move away */
11140 if (IS_PLAYER(test_x, test_y))
11142 struct PlayerInfo *player = PLAYERINFO(test_x, test_y);
11144 if (bad_element == EL_ROBOT && player->is_moving)
11145 continue; /* robot does not kill player if he is moving */
11147 if (game.engine_version >= VERSION_IDENT(3,0,7,0))
11149 if (player->MovPos != 0 && !(player->MovDir & touch_dir[i]))
11150 continue; /* center and border element do not touch */
11157 else if (test_element == EL_PENGUIN)
11166 if (kill_x != -1 || kill_y != -1)
11168 if (IS_PLAYER(kill_x, kill_y))
11170 struct PlayerInfo *player = PLAYERINFO(kill_x, kill_y);
11172 if (player->shield_deadly_time_left > 0 &&
11173 !IS_INDESTRUCTIBLE(bad_element))
11174 Bang(bad_x, bad_y);
11175 else if (!PLAYER_ENEMY_PROTECTED(kill_x, kill_y))
11176 KillPlayer(player);
11179 Bang(kill_x, kill_y);
11183 void TestIfPlayerTouchesBadThing(int x, int y)
11185 TestIfGoodThingHitsBadThing(x, y, MV_NONE);
11188 void TestIfPlayerRunsIntoBadThing(int x, int y, int move_dir)
11190 TestIfGoodThingHitsBadThing(x, y, move_dir);
11193 void TestIfBadThingTouchesPlayer(int x, int y)
11195 TestIfBadThingHitsGoodThing(x, y, MV_NONE);
11198 void TestIfBadThingRunsIntoPlayer(int x, int y, int move_dir)
11200 TestIfBadThingHitsGoodThing(x, y, move_dir);
11203 void TestIfFriendTouchesBadThing(int x, int y)
11205 TestIfGoodThingHitsBadThing(x, y, MV_NONE);
11208 void TestIfBadThingTouchesFriend(int x, int y)
11210 TestIfBadThingHitsGoodThing(x, y, MV_NONE);
11213 void TestIfBadThingTouchesOtherBadThing(int bad_x, int bad_y)
11215 int i, kill_x = bad_x, kill_y = bad_y;
11216 static int xy[4][2] =
11224 for (i = 0; i < NUM_DIRECTIONS; i++)
11228 x = bad_x + xy[i][0];
11229 y = bad_y + xy[i][1];
11230 if (!IN_LEV_FIELD(x, y))
11233 element = Feld[x][y];
11234 if (IS_AMOEBOID(element) || element == EL_GAME_OF_LIFE ||
11235 element == EL_AMOEBA_GROWING || element == EL_AMOEBA_DROP)
11243 if (kill_x != bad_x || kill_y != bad_y)
11244 Bang(bad_x, bad_y);
11247 void KillPlayer(struct PlayerInfo *player)
11249 int jx = player->jx, jy = player->jy;
11251 if (!player->active)
11254 /* remove accessible field at the player's position */
11255 Feld[jx][jy] = EL_EMPTY;
11257 /* deactivate shield (else Bang()/Explode() would not work right) */
11258 player->shield_normal_time_left = 0;
11259 player->shield_deadly_time_left = 0;
11262 BuryPlayer(player);
11265 static void KillPlayerUnlessEnemyProtected(int x, int y)
11267 if (!PLAYER_ENEMY_PROTECTED(x, y))
11268 KillPlayer(PLAYERINFO(x, y));
11271 static void KillPlayerUnlessExplosionProtected(int x, int y)
11273 if (!PLAYER_EXPLOSION_PROTECTED(x, y))
11274 KillPlayer(PLAYERINFO(x, y));
11277 void BuryPlayer(struct PlayerInfo *player)
11279 int jx = player->jx, jy = player->jy;
11281 if (!player->active)
11284 PlayLevelSoundElementAction(jx, jy, player->artwork_element, ACTION_DYING);
11285 PlayLevelSound(jx, jy, SND_GAME_LOSING);
11287 player->GameOver = TRUE;
11288 RemovePlayer(player);
11291 void RemovePlayer(struct PlayerInfo *player)
11293 int jx = player->jx, jy = player->jy;
11294 int i, found = FALSE;
11296 player->present = FALSE;
11297 player->active = FALSE;
11299 if (!ExplodeField[jx][jy])
11300 StorePlayer[jx][jy] = 0;
11302 if (player->is_moving)
11303 DrawLevelField(player->last_jx, player->last_jy);
11305 for (i = 0; i < MAX_PLAYERS; i++)
11306 if (stored_player[i].active)
11310 AllPlayersGone = TRUE;
11316 #if USE_NEW_SNAP_DELAY
11317 static void setFieldForSnapping(int x, int y, int element, int direction)
11319 struct ElementInfo *ei = &element_info[element];
11320 int direction_bit = MV_DIR_TO_BIT(direction);
11321 int graphic_snapping = ei->direction_graphic[ACTION_SNAPPING][direction_bit];
11322 int action = (graphic_snapping != IMG_EMPTY_SPACE ? ACTION_SNAPPING :
11323 IS_DIGGABLE(element) ? ACTION_DIGGING : ACTION_COLLECTING);
11325 Feld[x][y] = EL_ELEMENT_SNAPPING;
11326 MovDelay[x][y] = MOVE_DELAY_NORMAL_SPEED + 1 - 1;
11328 ResetGfxAnimation(x, y);
11330 GfxElement[x][y] = element;
11331 GfxAction[x][y] = action;
11332 GfxDir[x][y] = direction;
11333 GfxFrame[x][y] = -1;
11338 =============================================================================
11339 checkDiagonalPushing()
11340 -----------------------------------------------------------------------------
11341 check if diagonal input device direction results in pushing of object
11342 (by checking if the alternative direction is walkable, diggable, ...)
11343 =============================================================================
11346 static boolean checkDiagonalPushing(struct PlayerInfo *player,
11347 int x, int y, int real_dx, int real_dy)
11349 int jx, jy, dx, dy, xx, yy;
11351 if (real_dx == 0 || real_dy == 0) /* no diagonal direction => push */
11354 /* diagonal direction: check alternative direction */
11359 xx = jx + (dx == 0 ? real_dx : 0);
11360 yy = jy + (dy == 0 ? real_dy : 0);
11362 return (!IN_LEV_FIELD(xx, yy) || IS_SOLID_FOR_PUSHING(Feld[xx][yy]));
11366 =============================================================================
11368 -----------------------------------------------------------------------------
11369 x, y: field next to player (non-diagonal) to try to dig to
11370 real_dx, real_dy: direction as read from input device (can be diagonal)
11371 =============================================================================
11374 int DigField(struct PlayerInfo *player,
11375 int oldx, int oldy, int x, int y,
11376 int real_dx, int real_dy, int mode)
11378 boolean is_player = (IS_PLAYER(oldx, oldy) || mode != DF_DIG);
11379 boolean player_was_pushing = player->is_pushing;
11380 boolean player_can_move = (!player->cannot_move && mode != DF_SNAP);
11381 boolean player_can_move_or_snap = (!player->cannot_move || mode == DF_SNAP);
11382 int jx = oldx, jy = oldy;
11383 int dx = x - jx, dy = y - jy;
11384 int nextx = x + dx, nexty = y + dy;
11385 int move_direction = (dx == -1 ? MV_LEFT :
11386 dx == +1 ? MV_RIGHT :
11388 dy == +1 ? MV_DOWN : MV_NONE);
11389 int opposite_direction = MV_DIR_OPPOSITE(move_direction);
11390 int dig_side = MV_DIR_OPPOSITE(move_direction);
11391 int old_element = Feld[jx][jy];
11392 #if USE_FIXED_DONT_RUN_INTO
11393 int element = MovingOrBlocked2ElementIfNotLeaving(x, y);
11399 if (is_player) /* function can also be called by EL_PENGUIN */
11401 if (player->MovPos == 0)
11403 player->is_digging = FALSE;
11404 player->is_collecting = FALSE;
11407 if (player->MovPos == 0) /* last pushing move finished */
11408 player->is_pushing = FALSE;
11410 if (mode == DF_NO_PUSH) /* player just stopped pushing */
11412 player->is_switching = FALSE;
11413 player->push_delay = -1;
11415 return MP_NO_ACTION;
11419 #if !USE_FIXED_DONT_RUN_INTO
11420 if (IS_MOVING(x, y) || IS_PLAYER(x, y))
11421 return MP_NO_ACTION;
11424 if (IS_TUBE(Back[jx][jy]) && game.engine_version >= VERSION_IDENT(2,2,0,0))
11425 old_element = Back[jx][jy];
11427 /* in case of element dropped at player position, check background */
11428 else if (Back[jx][jy] != EL_EMPTY &&
11429 game.engine_version >= VERSION_IDENT(2,2,0,0))
11430 old_element = Back[jx][jy];
11433 #if USE_FIXED_DONT_RUN_INTO
11434 if (player_can_move && DONT_RUN_INTO(element))
11436 if (element == EL_ACID && dx == 0 && dy == 1)
11439 Feld[jx][jy] = EL_PLAYER_1;
11440 InitMovingField(jx, jy, MV_DOWN);
11441 Store[jx][jy] = EL_ACID;
11442 ContinueMoving(jx, jy);
11443 BuryPlayer(player);
11446 TestIfPlayerRunsIntoBadThing(jx, jy, player->MovDir);
11448 return MP_DONT_RUN_INTO;
11454 #if USE_FIXED_DONT_RUN_INTO
11455 if (player_can_move && DONT_RUN_INTO(element))
11457 TestIfPlayerRunsIntoBadThing(jx, jy, player->MovDir);
11459 return MP_DONT_RUN_INTO;
11464 if (IS_WALKABLE(old_element) && !ACCESS_FROM(old_element, move_direction))
11465 return MP_NO_ACTION; /* field has no opening in this direction */
11467 if (IS_PASSABLE(old_element) && !ACCESS_FROM(old_element,opposite_direction))
11468 return MP_NO_ACTION; /* field has no opening in this direction */
11471 #if USE_FIXED_DONT_RUN_INTO
11472 if (player_can_move && element == EL_ACID && move_direction == MV_DOWN)
11475 Feld[jx][jy] = EL_PLAYER_1;
11476 InitMovingField(jx, jy, MV_DOWN);
11477 Store[jx][jy] = EL_ACID;
11478 ContinueMoving(jx, jy);
11479 BuryPlayer(player);
11481 return MP_DONT_RUN_INTO;
11487 #if USE_FIXED_DONT_RUN_INTO
11488 if (player_can_move && DONT_RUN_INTO(element))
11490 if (element == EL_ACID && dx == 0 && dy == 1)
11493 Feld[jx][jy] = EL_PLAYER_1;
11494 InitMovingField(jx, jy, MV_DOWN);
11495 Store[jx][jy] = EL_ACID;
11496 ContinueMoving(jx, jy);
11497 BuryPlayer(player);
11500 TestIfPlayerRunsIntoBadThing(jx, jy, player->MovDir);
11502 return MP_DONT_RUN_INTO;
11507 #if USE_FIXED_DONT_RUN_INTO
11508 if (IS_MOVING(x, y) || IS_PLAYER(x, y))
11509 return MP_NO_ACTION;
11512 #if !USE_FIXED_DONT_RUN_INTO
11513 element = Feld[x][y];
11516 collect_count = element_info[element].collect_count_initial;
11518 if (!is_player && !IS_COLLECTIBLE(element)) /* penguin cannot collect it */
11519 return MP_NO_ACTION;
11521 if (game.engine_version < VERSION_IDENT(2,2,0,0))
11522 player_can_move = player_can_move_or_snap;
11524 if (mode == DF_SNAP && !IS_SNAPPABLE(element) &&
11525 game.engine_version >= VERSION_IDENT(2,2,0,0))
11527 CheckElementChangeByPlayer(x, y, element, CE_SNAPPED_BY_PLAYER,
11528 player->index_bit, dig_side);
11529 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SNAPS_X,
11530 player->index_bit, dig_side);
11532 if (Feld[x][y] != element) /* field changed by snapping */
11535 return MP_NO_ACTION;
11538 if (game.gravity && is_player && !player->is_auto_moving &&
11539 canFallDown(player) && move_direction != MV_DOWN &&
11540 !canMoveToValidFieldWithGravity(jx, jy, move_direction))
11541 return MP_NO_ACTION; /* player cannot walk here due to gravity */
11543 if (player_can_move &&
11544 IS_WALKABLE(element) && ACCESS_FROM(element, opposite_direction))
11546 int sound_element = SND_ELEMENT(element);
11547 int sound_action = ACTION_WALKING;
11549 if (IS_RND_GATE(element))
11551 if (!player->key[RND_GATE_NR(element)])
11552 return MP_NO_ACTION;
11554 else if (IS_RND_GATE_GRAY(element))
11556 if (!player->key[RND_GATE_GRAY_NR(element)])
11557 return MP_NO_ACTION;
11559 else if (IS_RND_GATE_GRAY_ACTIVE(element))
11561 if (!player->key[RND_GATE_GRAY_ACTIVE_NR(element)])
11562 return MP_NO_ACTION;
11564 else if (element == EL_EXIT_OPEN ||
11565 element == EL_SP_EXIT_OPEN ||
11566 element == EL_SP_EXIT_OPENING)
11568 sound_action = ACTION_PASSING; /* player is passing exit */
11570 else if (element == EL_EMPTY)
11572 sound_action = ACTION_MOVING; /* nothing to walk on */
11575 /* play sound from background or player, whatever is available */
11576 if (element_info[sound_element].sound[sound_action] != SND_UNDEFINED)
11577 PlayLevelSoundElementAction(x, y, sound_element, sound_action);
11579 PlayLevelSoundElementAction(x, y, player->artwork_element, sound_action);
11581 else if (player_can_move &&
11582 IS_PASSABLE(element) && canPassField(x, y, move_direction))
11584 if (!ACCESS_FROM(element, opposite_direction))
11585 return MP_NO_ACTION; /* field not accessible from this direction */
11587 if (CAN_MOVE(element)) /* only fixed elements can be passed! */
11588 return MP_NO_ACTION;
11590 if (IS_EM_GATE(element))
11592 if (!player->key[EM_GATE_NR(element)])
11593 return MP_NO_ACTION;
11595 else if (IS_EM_GATE_GRAY(element))
11597 if (!player->key[EM_GATE_GRAY_NR(element)])
11598 return MP_NO_ACTION;
11600 else if (IS_EM_GATE_GRAY_ACTIVE(element))
11602 if (!player->key[EM_GATE_GRAY_ACTIVE_NR(element)])
11603 return MP_NO_ACTION;
11605 else if (IS_SP_PORT(element))
11607 if (element == EL_SP_GRAVITY_PORT_LEFT ||
11608 element == EL_SP_GRAVITY_PORT_RIGHT ||
11609 element == EL_SP_GRAVITY_PORT_UP ||
11610 element == EL_SP_GRAVITY_PORT_DOWN)
11611 game.gravity = !game.gravity;
11612 else if (element == EL_SP_GRAVITY_ON_PORT_LEFT ||
11613 element == EL_SP_GRAVITY_ON_PORT_RIGHT ||
11614 element == EL_SP_GRAVITY_ON_PORT_UP ||
11615 element == EL_SP_GRAVITY_ON_PORT_DOWN)
11616 game.gravity = TRUE;
11617 else if (element == EL_SP_GRAVITY_OFF_PORT_LEFT ||
11618 element == EL_SP_GRAVITY_OFF_PORT_RIGHT ||
11619 element == EL_SP_GRAVITY_OFF_PORT_UP ||
11620 element == EL_SP_GRAVITY_OFF_PORT_DOWN)
11621 game.gravity = FALSE;
11624 /* automatically move to the next field with double speed */
11625 player->programmed_action = move_direction;
11627 if (player->move_delay_reset_counter == 0)
11629 player->move_delay_reset_counter = 2; /* two double speed steps */
11631 DOUBLE_PLAYER_SPEED(player);
11634 PlayLevelSoundAction(x, y, ACTION_PASSING);
11636 else if (player_can_move_or_snap && IS_DIGGABLE(element))
11640 if (mode != DF_SNAP)
11642 GfxElement[x][y] = GFX_ELEMENT(element);
11643 player->is_digging = TRUE;
11646 PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
11648 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_DIGS_X,
11649 player->index_bit, dig_side);
11651 if (mode == DF_SNAP)
11653 #if USE_NEW_SNAP_DELAY
11654 if (level.block_snap_field)
11655 setFieldForSnapping(x, y, element, move_direction);
11657 TestIfElementTouchesCustomElement(x, y); /* for empty space */
11659 TestIfElementTouchesCustomElement(x, y); /* for empty space */
11662 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SNAPS_X,
11663 player->index_bit, dig_side);
11666 else if (player_can_move_or_snap && IS_COLLECTIBLE(element))
11670 if (is_player && mode != DF_SNAP)
11672 GfxElement[x][y] = element;
11673 player->is_collecting = TRUE;
11676 if (element == EL_SPEED_PILL)
11678 player->move_delay_value = MOVE_DELAY_HIGH_SPEED;
11680 else if (element == EL_EXTRA_TIME && level.time > 0)
11682 TimeLeft += level.extra_time;
11683 DrawGameValue_Time(TimeLeft);
11685 else if (element == EL_SHIELD_NORMAL || element == EL_SHIELD_DEADLY)
11687 player->shield_normal_time_left += level.shield_normal_time;
11688 if (element == EL_SHIELD_DEADLY)
11689 player->shield_deadly_time_left += level.shield_deadly_time;
11691 else if (element == EL_DYNAMITE ||
11692 element == EL_EM_DYNAMITE ||
11693 element == EL_SP_DISK_RED)
11695 if (player->inventory_size < MAX_INVENTORY_SIZE)
11696 player->inventory_element[player->inventory_size++] = element;
11699 DrawGameDoorValues();
11701 DrawGameValue_Dynamite(local_player->inventory_size);
11704 else if (element == EL_DYNABOMB_INCREASE_NUMBER)
11706 player->dynabomb_count++;
11707 player->dynabombs_left++;
11709 else if (element == EL_DYNABOMB_INCREASE_SIZE)
11711 player->dynabomb_size++;
11713 else if (element == EL_DYNABOMB_INCREASE_POWER)
11715 player->dynabomb_xl = TRUE;
11717 else if (IS_KEY(element))
11719 player->key[KEY_NR(element)] = TRUE;
11722 DrawGameDoorValues();
11724 DrawGameValue_Keys(player->key);
11727 redraw_mask |= REDRAW_DOOR_1;
11729 else if (IS_ENVELOPE(element))
11731 player->show_envelope = element;
11733 else if (element == EL_EMC_LENSES)
11735 game.lenses_time_left = level.lenses_time * FRAMES_PER_SECOND;
11737 RedrawAllInvisibleElementsForLenses();
11739 else if (element == EL_EMC_MAGNIFIER)
11741 game.magnify_time_left = level.magnify_time * FRAMES_PER_SECOND;
11743 RedrawAllInvisibleElementsForMagnifier();
11745 else if (IS_DROPPABLE(element) ||
11746 IS_THROWABLE(element)) /* can be collected and dropped */
11750 if (collect_count == 0)
11751 player->inventory_infinite_element = element;
11753 for (i = 0; i < collect_count; i++)
11754 if (player->inventory_size < MAX_INVENTORY_SIZE)
11755 player->inventory_element[player->inventory_size++] = element;
11758 DrawGameDoorValues();
11760 DrawGameValue_Dynamite(local_player->inventory_size);
11763 else if (collect_count > 0)
11765 local_player->gems_still_needed -= collect_count;
11766 if (local_player->gems_still_needed < 0)
11767 local_player->gems_still_needed = 0;
11769 DrawGameValue_Emeralds(local_player->gems_still_needed);
11772 RaiseScoreElement(element);
11773 PlayLevelSoundElementAction(x, y, element, ACTION_COLLECTING);
11776 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_COLLECTS_X,
11777 player->index_bit, dig_side);
11779 if (mode == DF_SNAP)
11781 #if USE_NEW_SNAP_DELAY
11782 if (level.block_snap_field)
11783 setFieldForSnapping(x, y, element, move_direction);
11785 TestIfElementTouchesCustomElement(x, y); /* for empty space */
11787 TestIfElementTouchesCustomElement(x, y); /* for empty space */
11790 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SNAPS_X,
11791 player->index_bit, dig_side);
11794 else if (player_can_move_or_snap && IS_PUSHABLE(element))
11796 if (mode == DF_SNAP && element != EL_BD_ROCK)
11797 return MP_NO_ACTION;
11799 if (CAN_FALL(element) && dy)
11800 return MP_NO_ACTION;
11802 if (CAN_FALL(element) && IN_LEV_FIELD(x, y + 1) && IS_FREE(x, y + 1) &&
11803 !(element == EL_SPRING && level.use_spring_bug))
11804 return MP_NO_ACTION;
11806 if (CAN_MOVE(element) && GET_MAX_MOVE_DELAY(element) == 0 &&
11807 ((move_direction & MV_VERTICAL &&
11808 ((element_info[element].move_pattern & MV_LEFT &&
11809 IN_LEV_FIELD(x - 1, y) && IS_FREE(x - 1, y)) ||
11810 (element_info[element].move_pattern & MV_RIGHT &&
11811 IN_LEV_FIELD(x + 1, y) && IS_FREE(x + 1, y)))) ||
11812 (move_direction & MV_HORIZONTAL &&
11813 ((element_info[element].move_pattern & MV_UP &&
11814 IN_LEV_FIELD(x, y - 1) && IS_FREE(x, y - 1)) ||
11815 (element_info[element].move_pattern & MV_DOWN &&
11816 IN_LEV_FIELD(x, y + 1) && IS_FREE(x, y + 1))))))
11817 return MP_NO_ACTION;
11819 /* do not push elements already moving away faster than player */
11820 if (CAN_MOVE(element) && MovDir[x][y] == move_direction &&
11821 ABS(getElementMoveStepsize(x, y)) > MOVE_STEPSIZE_NORMAL)
11822 return MP_NO_ACTION;
11824 if (game.engine_version >= VERSION_IDENT(3,1,0,0))
11826 if (player->push_delay_value == -1 || !player_was_pushing)
11827 player->push_delay_value = GET_NEW_PUSH_DELAY(element);
11829 else if (game.engine_version >= VERSION_IDENT(3,0,7,1))
11831 if (player->push_delay_value == -1)
11832 player->push_delay_value = GET_NEW_PUSH_DELAY(element);
11834 else if (game.engine_version >= VERSION_IDENT(2,2,0,7))
11836 if (!player->is_pushing)
11837 player->push_delay_value = GET_NEW_PUSH_DELAY(element);
11840 player->is_pushing = TRUE;
11842 if (!(IN_LEV_FIELD(nextx, nexty) &&
11843 (IS_FREE(nextx, nexty) ||
11844 (Feld[nextx][nexty] == EL_SOKOBAN_FIELD_EMPTY &&
11845 IS_SB_ELEMENT(element)))))
11846 return MP_NO_ACTION;
11848 if (!checkDiagonalPushing(player, x, y, real_dx, real_dy))
11849 return MP_NO_ACTION;
11851 if (player->push_delay == -1) /* new pushing; restart delay */
11852 player->push_delay = 0;
11854 if (player->push_delay < player->push_delay_value &&
11855 !(tape.playing && tape.file_version < FILE_VERSION_2_0) &&
11856 element != EL_SPRING && element != EL_BALLOON)
11858 /* make sure that there is no move delay before next try to push */
11859 if (game.engine_version >= VERSION_IDENT(3,0,7,1))
11860 player->move_delay = 0;
11862 return MP_NO_ACTION;
11865 if (IS_SB_ELEMENT(element))
11867 if (element == EL_SOKOBAN_FIELD_FULL)
11869 Back[x][y] = EL_SOKOBAN_FIELD_EMPTY;
11870 local_player->sokobanfields_still_needed++;
11873 if (Feld[nextx][nexty] == EL_SOKOBAN_FIELD_EMPTY)
11875 Back[nextx][nexty] = EL_SOKOBAN_FIELD_EMPTY;
11876 local_player->sokobanfields_still_needed--;
11879 Feld[x][y] = EL_SOKOBAN_OBJECT;
11881 if (Back[x][y] == Back[nextx][nexty])
11882 PlayLevelSoundAction(x, y, ACTION_PUSHING);
11883 else if (Back[x][y] != 0)
11884 PlayLevelSoundElementAction(x, y, EL_SOKOBAN_FIELD_FULL,
11887 PlayLevelSoundElementAction(nextx, nexty, EL_SOKOBAN_FIELD_EMPTY,
11890 if (local_player->sokobanfields_still_needed == 0 &&
11891 game.emulation == EMU_SOKOBAN)
11893 player->LevelSolved = player->GameOver = TRUE;
11894 PlayLevelSound(x, y, SND_GAME_SOKOBAN_SOLVING);
11898 PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
11900 InitMovingField(x, y, move_direction);
11901 GfxAction[x][y] = ACTION_PUSHING;
11903 if (mode == DF_SNAP)
11904 ContinueMoving(x, y);
11906 MovPos[x][y] = (dx != 0 ? dx : dy);
11908 Pushed[x][y] = TRUE;
11909 Pushed[nextx][nexty] = TRUE;
11911 if (game.engine_version < VERSION_IDENT(2,2,0,7))
11912 player->push_delay_value = GET_NEW_PUSH_DELAY(element);
11914 player->push_delay_value = -1; /* get new value later */
11916 /* check for element change _after_ element has been pushed */
11917 if (game.use_change_when_pushing_bug)
11919 CheckElementChangeByPlayer(x, y, element, CE_PUSHED_BY_PLAYER,
11920 player->index_bit, dig_side);
11921 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_PUSHES_X,
11922 player->index_bit, dig_side);
11925 else if (IS_SWITCHABLE(element))
11927 if (PLAYER_SWITCHING(player, x, y))
11929 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_PRESSES_X,
11930 player->index_bit, dig_side);
11935 player->is_switching = TRUE;
11936 player->switch_x = x;
11937 player->switch_y = y;
11939 PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVATING);
11941 if (element == EL_ROBOT_WHEEL)
11943 Feld[x][y] = EL_ROBOT_WHEEL_ACTIVE;
11947 DrawLevelField(x, y);
11949 else if (element == EL_SP_TERMINAL)
11954 SCAN_PLAYFIELD(xx, yy)
11956 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
11959 if (Feld[xx][yy] == EL_SP_DISK_YELLOW)
11961 else if (Feld[xx][yy] == EL_SP_TERMINAL)
11962 Feld[xx][yy] = EL_SP_TERMINAL_ACTIVE;
11965 else if (IS_BELT_SWITCH(element))
11967 ToggleBeltSwitch(x, y);
11969 else if (element == EL_SWITCHGATE_SWITCH_UP ||
11970 element == EL_SWITCHGATE_SWITCH_DOWN)
11972 ToggleSwitchgateSwitch(x, y);
11974 else if (element == EL_LIGHT_SWITCH ||
11975 element == EL_LIGHT_SWITCH_ACTIVE)
11977 ToggleLightSwitch(x, y);
11979 else if (element == EL_TIMEGATE_SWITCH)
11981 ActivateTimegateSwitch(x, y);
11983 else if (element == EL_BALLOON_SWITCH_LEFT ||
11984 element == EL_BALLOON_SWITCH_RIGHT ||
11985 element == EL_BALLOON_SWITCH_UP ||
11986 element == EL_BALLOON_SWITCH_DOWN ||
11987 element == EL_BALLOON_SWITCH_NONE ||
11988 element == EL_BALLOON_SWITCH_ANY)
11990 game.wind_direction = (element == EL_BALLOON_SWITCH_LEFT ? MV_LEFT :
11991 element == EL_BALLOON_SWITCH_RIGHT ? MV_RIGHT :
11992 element == EL_BALLOON_SWITCH_UP ? MV_UP :
11993 element == EL_BALLOON_SWITCH_DOWN ? MV_DOWN :
11994 element == EL_BALLOON_SWITCH_NONE ? MV_NONE :
11997 else if (element == EL_LAMP)
11999 Feld[x][y] = EL_LAMP_ACTIVE;
12000 local_player->lights_still_needed--;
12002 ResetGfxAnimation(x, y);
12003 DrawLevelField(x, y);
12005 else if (element == EL_TIME_ORB_FULL)
12007 Feld[x][y] = EL_TIME_ORB_EMPTY;
12009 if (level.time > 0 || level.use_time_orb_bug)
12011 TimeLeft += level.time_orb_time;
12012 DrawGameValue_Time(TimeLeft);
12015 ResetGfxAnimation(x, y);
12016 DrawLevelField(x, y);
12018 else if (element == EL_EMC_MAGIC_BALL_SWITCH ||
12019 element == EL_EMC_MAGIC_BALL_SWITCH_ACTIVE)
12023 game.ball_state = !game.ball_state;
12026 SCAN_PLAYFIELD(xx, yy)
12028 for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
12031 int e = Feld[xx][yy];
12033 if (game.ball_state)
12035 if (e == EL_EMC_MAGIC_BALL)
12036 CreateField(xx, yy, EL_EMC_MAGIC_BALL_ACTIVE);
12037 else if (e == EL_EMC_MAGIC_BALL_SWITCH)
12038 CreateField(xx, yy, EL_EMC_MAGIC_BALL_SWITCH_ACTIVE);
12042 if (e == EL_EMC_MAGIC_BALL_ACTIVE)
12043 CreateField(xx, yy, EL_EMC_MAGIC_BALL);
12044 else if (e == EL_EMC_MAGIC_BALL_SWITCH_ACTIVE)
12045 CreateField(xx, yy, EL_EMC_MAGIC_BALL_SWITCH);
12050 CheckTriggeredElementChangeByPlayer(x, y, element, CE_SWITCH_OF_X,
12051 player->index_bit, dig_side);
12053 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SWITCHES_X,
12054 player->index_bit, dig_side);
12056 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_PRESSES_X,
12057 player->index_bit, dig_side);
12063 if (!PLAYER_SWITCHING(player, x, y))
12065 player->is_switching = TRUE;
12066 player->switch_x = x;
12067 player->switch_y = y;
12069 CheckElementChangeByPlayer(x, y, element, CE_SWITCHED,
12070 player->index_bit, dig_side);
12071 CheckTriggeredElementChangeByPlayer(x, y, element, CE_SWITCH_OF_X,
12072 player->index_bit, dig_side);
12074 CheckElementChangeByPlayer(x, y, element, CE_SWITCHED_BY_PLAYER,
12075 player->index_bit, dig_side);
12076 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_SWITCHES_X,
12077 player->index_bit, dig_side);
12080 CheckElementChangeByPlayer(x, y, element, CE_PRESSED_BY_PLAYER,
12081 player->index_bit, dig_side);
12082 CheckTriggeredElementChangeByPlayer(x, y, element, CE_PLAYER_PRESSES_X,
12083 player->index_bit, dig_side);
12085 return MP_NO_ACTION;
12088 player->push_delay = -1;
12090 if (is_player) /* function can also be called by EL_PENGUIN */
12092 if (Feld[x][y] != element) /* really digged/collected something */
12093 player->is_collecting = !player->is_digging;
12099 boolean SnapField(struct PlayerInfo *player, int dx, int dy)
12101 int jx = player->jx, jy = player->jy;
12102 int x = jx + dx, y = jy + dy;
12103 int snap_direction = (dx == -1 ? MV_LEFT :
12104 dx == +1 ? MV_RIGHT :
12106 dy == +1 ? MV_DOWN : MV_NONE);
12107 boolean can_continue_snapping = (level.continuous_snapping &&
12108 WasJustFalling[x][y] < CHECK_DELAY_FALLING);
12110 if (player->MovPos != 0 && game.engine_version >= VERSION_IDENT(2,2,0,0))
12113 if (!player->active || !IN_LEV_FIELD(x, y))
12121 if (player->MovPos == 0)
12122 player->is_pushing = FALSE;
12124 player->is_snapping = FALSE;
12126 if (player->MovPos == 0)
12128 player->is_moving = FALSE;
12129 player->is_digging = FALSE;
12130 player->is_collecting = FALSE;
12136 #if USE_NEW_CONTINUOUS_SNAPPING
12137 /* prevent snapping with already pressed snap key when not allowed */
12138 if (player->is_snapping && !can_continue_snapping)
12141 if (player->is_snapping)
12145 player->MovDir = snap_direction;
12147 if (player->MovPos == 0)
12149 player->is_moving = FALSE;
12150 player->is_digging = FALSE;
12151 player->is_collecting = FALSE;
12154 player->is_dropping = FALSE;
12155 player->is_dropping_pressed = FALSE;
12156 player->drop_pressed_delay = 0;
12158 if (DigField(player, jx, jy, x, y, 0, 0, DF_SNAP) == MP_NO_ACTION)
12161 player->is_snapping = TRUE;
12163 if (player->MovPos == 0)
12165 player->is_moving = FALSE;
12166 player->is_digging = FALSE;
12167 player->is_collecting = FALSE;
12170 if (player->MovPos != 0) /* prevent graphic bugs in versions < 2.2.0 */
12171 DrawLevelField(player->last_jx, player->last_jy);
12173 DrawLevelField(x, y);
12178 boolean DropElement(struct PlayerInfo *player)
12180 int old_element, new_element;
12181 int dropx = player->jx, dropy = player->jy;
12182 int drop_direction = player->MovDir;
12183 int drop_side = drop_direction;
12184 int drop_element = (player->inventory_size > 0 ?
12185 player->inventory_element[player->inventory_size - 1] :
12186 player->inventory_infinite_element != EL_UNDEFINED ?
12187 player->inventory_infinite_element :
12188 player->dynabombs_left > 0 ?
12189 EL_DYNABOMB_PLAYER_1_ACTIVE + player->index_nr :
12192 player->is_dropping_pressed = TRUE;
12194 /* do not drop an element on top of another element; when holding drop key
12195 pressed without moving, dropped element must move away before the next
12196 element can be dropped (this is especially important if the next element
12197 is dynamite, which can be placed on background for historical reasons) */
12198 if (PLAYER_DROPPING(player, dropx, dropy) && Feld[dropx][dropy] != EL_EMPTY)
12201 if (IS_THROWABLE(drop_element))
12203 dropx += GET_DX_FROM_DIR(drop_direction);
12204 dropy += GET_DY_FROM_DIR(drop_direction);
12206 if (!IN_LEV_FIELD(dropx, dropy))
12210 old_element = Feld[dropx][dropy]; /* old element at dropping position */
12211 new_element = drop_element; /* default: no change when dropping */
12213 /* check if player is active, not moving and ready to drop */
12214 if (!player->active || player->MovPos || player->drop_delay > 0)
12217 /* check if player has anything that can be dropped */
12218 if (new_element == EL_UNDEFINED)
12221 /* check if drop key was pressed long enough for EM style dynamite */
12222 if (new_element == EL_EM_DYNAMITE && player->drop_pressed_delay < 40)
12225 /* check if anything can be dropped at the current position */
12226 if (IS_ACTIVE_BOMB(old_element) || old_element == EL_EXPLOSION)
12229 /* collected custom elements can only be dropped on empty fields */
12230 if (IS_CUSTOM_ELEMENT(new_element) && old_element != EL_EMPTY)
12233 if (old_element != EL_EMPTY)
12234 Back[dropx][dropy] = old_element; /* store old element on this field */
12236 ResetGfxAnimation(dropx, dropy);
12237 ResetRandomAnimationValue(dropx, dropy);
12239 if (player->inventory_size > 0 ||
12240 player->inventory_infinite_element != EL_UNDEFINED)
12242 if (player->inventory_size > 0)
12244 player->inventory_size--;
12247 DrawGameDoorValues();
12249 DrawGameValue_Dynamite(local_player->inventory_size);
12252 if (new_element == EL_DYNAMITE)
12253 new_element = EL_DYNAMITE_ACTIVE;
12254 else if (new_element == EL_EM_DYNAMITE)
12255 new_element = EL_EM_DYNAMITE_ACTIVE;
12256 else if (new_element == EL_SP_DISK_RED)
12257 new_element = EL_SP_DISK_RED_ACTIVE;
12260 Feld[dropx][dropy] = new_element;
12262 if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
12263 DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
12264 el2img(Feld[dropx][dropy]), 0);
12266 PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
12268 /* needed if previous element just changed to "empty" in the last frame */
12269 ChangeCount[dropx][dropy] = 0; /* allow at least one more change */
12271 CheckElementChangeByPlayer(dropx, dropy, new_element, CE_DROPPED_BY_PLAYER,
12272 player->index_bit, drop_side);
12273 CheckTriggeredElementChangeByPlayer(dropx, dropy, new_element,
12275 player->index_bit, drop_side);
12277 TestIfElementTouchesCustomElement(dropx, dropy);
12279 else /* player is dropping a dyna bomb */
12281 player->dynabombs_left--;
12283 Feld[dropx][dropy] = new_element;
12285 if (IN_SCR_FIELD(SCREENX(dropx), SCREENY(dropy)))
12286 DrawGraphicThruMask(SCREENX(dropx), SCREENY(dropy),
12287 el2img(Feld[dropx][dropy]), 0);
12289 PlayLevelSoundAction(dropx, dropy, ACTION_DROPPING);
12292 if (Feld[dropx][dropy] == new_element) /* uninitialized unless CE change */
12293 InitField_WithBug1(dropx, dropy, FALSE);
12295 new_element = Feld[dropx][dropy]; /* element might have changed */
12297 if (IS_CUSTOM_ELEMENT(new_element) && CAN_MOVE(new_element) &&
12298 element_info[new_element].move_pattern == MV_WHEN_DROPPED)
12300 int move_direction, nextx, nexty;
12302 if (element_info[new_element].move_direction_initial == MV_START_AUTOMATIC)
12303 MovDir[dropx][dropy] = drop_direction;
12305 move_direction = MovDir[dropx][dropy];
12306 nextx = dropx + GET_DX_FROM_DIR(move_direction);
12307 nexty = dropy + GET_DY_FROM_DIR(move_direction);
12309 ChangeCount[dropx][dropy] = 0; /* allow at least one more change */
12310 CheckCollision[dropx][dropy] = CHECK_DELAY_COLLISION;
12313 player->drop_delay = GET_NEW_DROP_DELAY(drop_element);
12314 player->is_dropping = TRUE;
12316 player->drop_pressed_delay = 0;
12317 player->is_dropping_pressed = FALSE;
12319 player->drop_x = dropx;
12320 player->drop_y = dropy;
12325 /* ------------------------------------------------------------------------- */
12326 /* game sound playing functions */
12327 /* ------------------------------------------------------------------------- */
12329 static int *loop_sound_frame = NULL;
12330 static int *loop_sound_volume = NULL;
12332 void InitPlayLevelSound()
12334 int num_sounds = getSoundListSize();
12336 checked_free(loop_sound_frame);
12337 checked_free(loop_sound_volume);
12339 loop_sound_frame = checked_calloc(num_sounds * sizeof(int));
12340 loop_sound_volume = checked_calloc(num_sounds * sizeof(int));
12343 static void PlayLevelSound(int x, int y, int nr)
12345 int sx = SCREENX(x), sy = SCREENY(y);
12346 int volume, stereo_position;
12347 int max_distance = 8;
12348 int type = (IS_LOOP_SOUND(nr) ? SND_CTRL_PLAY_LOOP : SND_CTRL_PLAY_SOUND);
12350 if ((!setup.sound_simple && !IS_LOOP_SOUND(nr)) ||
12351 (!setup.sound_loops && IS_LOOP_SOUND(nr)))
12354 if (!IN_LEV_FIELD(x, y) ||
12355 sx < -max_distance || sx >= SCR_FIELDX + max_distance ||
12356 sy < -max_distance || sy >= SCR_FIELDY + max_distance)
12359 volume = SOUND_MAX_VOLUME;
12361 if (!IN_SCR_FIELD(sx, sy))
12363 int dx = ABS(sx - SCR_FIELDX / 2) - SCR_FIELDX / 2;
12364 int dy = ABS(sy - SCR_FIELDY / 2) - SCR_FIELDY / 2;
12366 volume -= volume * (dx > dy ? dx : dy) / max_distance;
12369 stereo_position = (SOUND_MAX_LEFT +
12370 (sx + max_distance) * SOUND_MAX_LEFT2RIGHT /
12371 (SCR_FIELDX + 2 * max_distance));
12373 if (IS_LOOP_SOUND(nr))
12375 /* This assures that quieter loop sounds do not overwrite louder ones,
12376 while restarting sound volume comparison with each new game frame. */
12378 if (loop_sound_volume[nr] > volume && loop_sound_frame[nr] == FrameCounter)
12381 loop_sound_volume[nr] = volume;
12382 loop_sound_frame[nr] = FrameCounter;
12385 PlaySoundExt(nr, volume, stereo_position, type);
12388 static void PlayLevelSoundNearest(int x, int y, int sound_action)
12390 PlayLevelSound(x < LEVELX(BX1) ? LEVELX(BX1) :
12391 x > LEVELX(BX2) ? LEVELX(BX2) : x,
12392 y < LEVELY(BY1) ? LEVELY(BY1) :
12393 y > LEVELY(BY2) ? LEVELY(BY2) : y,
12397 static void PlayLevelSoundAction(int x, int y, int action)
12399 PlayLevelSoundElementAction(x, y, Feld[x][y], action);
12402 static void PlayLevelSoundElementAction(int x, int y, int element, int action)
12404 int sound_effect = element_info[SND_ELEMENT(element)].sound[action];
12406 if (sound_effect != SND_UNDEFINED)
12407 PlayLevelSound(x, y, sound_effect);
12410 static void PlayLevelSoundElementActionIfLoop(int x, int y, int element,
12413 int sound_effect = element_info[SND_ELEMENT(element)].sound[action];
12415 if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
12416 PlayLevelSound(x, y, sound_effect);
12419 static void PlayLevelSoundActionIfLoop(int x, int y, int action)
12421 int sound_effect = element_info[SND_ELEMENT(Feld[x][y])].sound[action];
12423 if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
12424 PlayLevelSound(x, y, sound_effect);
12427 static void StopLevelSoundActionIfLoop(int x, int y, int action)
12429 int sound_effect = element_info[SND_ELEMENT(Feld[x][y])].sound[action];
12431 if (sound_effect != SND_UNDEFINED && IS_LOOP_SOUND(sound_effect))
12432 StopSound(sound_effect);
12435 static void PlayLevelMusic()
12437 if (levelset.music[level_nr] != MUS_UNDEFINED)
12438 PlayMusic(levelset.music[level_nr]); /* from config file */
12440 PlayMusic(MAP_NOCONF_MUSIC(level_nr)); /* from music dir */
12443 void PlayLevelSound_EM(int x, int y, int element_em, int sample)
12445 int element = (element_em > -1 ? map_element_EM_to_RND(element_em) : 0);
12450 PlayLevelSoundElementAction(x, y, element, ACTION_WALKING);
12454 PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
12458 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
12462 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
12466 PlayLevelSoundElementAction(x, y, element, ACTION_BREAKING);
12470 PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
12474 PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
12477 case SAMPLE_android_clone:
12478 PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
12481 case SAMPLE_android_move:
12482 PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
12485 case SAMPLE_spring:
12486 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
12490 PlayLevelSoundElementAction(x, y, element, ACTION_EATING);
12494 PlayLevelSoundElementAction(x, y, element, ACTION_WAITING);
12497 case SAMPLE_eater_eat:
12498 PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
12502 PlayLevelSoundElementAction(x, y, element, ACTION_MOVING);
12505 case SAMPLE_collect:
12506 PlayLevelSoundElementAction(x, y, element, ACTION_COLLECTING);
12509 case SAMPLE_diamond:
12510 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
12513 case SAMPLE_squash:
12514 /* !!! CHECK THIS !!! */
12516 PlayLevelSoundElementAction(x, y, element, ACTION_BREAKING);
12518 PlayLevelSoundElementAction(x, y, element, ACTION_SMASHED_BY_ROCK);
12522 case SAMPLE_wonderfall:
12523 PlayLevelSoundElementAction(x, y, element, ACTION_FILLING);
12527 PlayLevelSoundElementAction(x, y, element, ACTION_IMPACT);
12531 PlayLevelSoundElementAction(x, y, element, ACTION_PUSHING);
12535 PlayLevelSoundElementAction(x, y, element, ACTION_DIGGING);
12539 PlayLevelSoundElementAction(x, y, element, ACTION_SPLASHING);
12543 PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
12547 PlayLevelSoundElementAction(x, y, element, ACTION_GROWING);
12550 case SAMPLE_wonder:
12551 PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
12555 PlayLevelSoundElementAction(x, y, element, ACTION_PASSING);
12558 case SAMPLE_exit_open:
12559 PlayLevelSoundElementAction(x, y, element, ACTION_OPENING);
12562 case SAMPLE_exit_leave:
12563 PlayLevelSoundElementAction(x, y, element, ACTION_PASSING);
12566 case SAMPLE_dynamite:
12567 PlayLevelSoundElementAction(x, y, element, ACTION_DROPPING);
12571 PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
12575 PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVATING);
12579 PlayLevelSoundElementAction(x, y, element, ACTION_ACTIVE);
12583 PlayLevelSoundElementAction(x, y, element, ACTION_EXPLODING);
12587 PlayLevelSoundElementAction(x, y, element, ACTION_DYING);
12591 PlaySoundStereo(SND_GAME_RUNNING_OUT_OF_TIME, SOUND_MIDDLE);
12595 PlayLevelSoundElementAction(x, y, element, ACTION_DEFAULT);
12600 void RaiseScore(int value)
12602 local_player->score += value;
12604 DrawGameValue_Score(local_player->score);
12607 void RaiseScoreElement(int element)
12612 case EL_BD_DIAMOND:
12613 case EL_EMERALD_YELLOW:
12614 case EL_EMERALD_RED:
12615 case EL_EMERALD_PURPLE:
12616 case EL_SP_INFOTRON:
12617 RaiseScore(level.score[SC_EMERALD]);
12620 RaiseScore(level.score[SC_DIAMOND]);
12623 RaiseScore(level.score[SC_CRYSTAL]);
12626 RaiseScore(level.score[SC_PEARL]);
12629 case EL_BD_BUTTERFLY:
12630 case EL_SP_ELECTRON:
12631 RaiseScore(level.score[SC_BUG]);
12634 case EL_BD_FIREFLY:
12635 case EL_SP_SNIKSNAK:
12636 RaiseScore(level.score[SC_SPACESHIP]);
12639 case EL_DARK_YAMYAM:
12640 RaiseScore(level.score[SC_YAMYAM]);
12643 RaiseScore(level.score[SC_ROBOT]);
12646 RaiseScore(level.score[SC_PACMAN]);
12649 RaiseScore(level.score[SC_NUT]);
12652 case EL_EM_DYNAMITE:
12653 case EL_SP_DISK_RED:
12654 case EL_DYNABOMB_INCREASE_NUMBER:
12655 case EL_DYNABOMB_INCREASE_SIZE:
12656 case EL_DYNABOMB_INCREASE_POWER:
12657 RaiseScore(level.score[SC_DYNAMITE]);
12659 case EL_SHIELD_NORMAL:
12660 case EL_SHIELD_DEADLY:
12661 RaiseScore(level.score[SC_SHIELD]);
12663 case EL_EXTRA_TIME:
12664 RaiseScore(level.extra_time_score);
12678 RaiseScore(level.score[SC_KEY]);
12681 RaiseScore(element_info[element].collect_score);
12686 void RequestQuitGame(boolean ask_if_really_quit)
12688 if (AllPlayersGone ||
12689 !ask_if_really_quit ||
12690 level_editor_test_game ||
12691 Request("Do you really want to quit the game ?",
12692 REQ_ASK | REQ_STAY_CLOSED))
12694 #if defined(NETWORK_AVALIABLE)
12695 if (options.network)
12696 SendToServer_StopPlaying(NETWORK_STOP_BY_PLAYER);
12700 game_status = GAME_MODE_MAIN;
12706 if (tape.playing && tape.deactivate_display)
12707 TapeDeactivateDisplayOff(TRUE);
12709 OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
12711 if (tape.playing && tape.deactivate_display)
12712 TapeDeactivateDisplayOn();
12717 /* ---------- new game button stuff ---------------------------------------- */
12719 /* graphic position values for game buttons */
12720 #define GAME_BUTTON_XSIZE 30
12721 #define GAME_BUTTON_YSIZE 30
12722 #define GAME_BUTTON_XPOS 5
12723 #define GAME_BUTTON_YPOS 215
12724 #define SOUND_BUTTON_XPOS 5
12725 #define SOUND_BUTTON_YPOS (GAME_BUTTON_YPOS + GAME_BUTTON_YSIZE)
12727 #define GAME_BUTTON_STOP_XPOS (GAME_BUTTON_XPOS + 0 * GAME_BUTTON_XSIZE)
12728 #define GAME_BUTTON_PAUSE_XPOS (GAME_BUTTON_XPOS + 1 * GAME_BUTTON_XSIZE)
12729 #define GAME_BUTTON_PLAY_XPOS (GAME_BUTTON_XPOS + 2 * GAME_BUTTON_XSIZE)
12730 #define SOUND_BUTTON_MUSIC_XPOS (SOUND_BUTTON_XPOS + 0 * GAME_BUTTON_XSIZE)
12731 #define SOUND_BUTTON_LOOPS_XPOS (SOUND_BUTTON_XPOS + 1 * GAME_BUTTON_XSIZE)
12732 #define SOUND_BUTTON_SIMPLE_XPOS (SOUND_BUTTON_XPOS + 2 * GAME_BUTTON_XSIZE)
12739 } gamebutton_info[NUM_GAME_BUTTONS] =
12742 GAME_BUTTON_STOP_XPOS, GAME_BUTTON_YPOS,
12747 GAME_BUTTON_PAUSE_XPOS, GAME_BUTTON_YPOS,
12748 GAME_CTRL_ID_PAUSE,
12752 GAME_BUTTON_PLAY_XPOS, GAME_BUTTON_YPOS,
12757 SOUND_BUTTON_MUSIC_XPOS, SOUND_BUTTON_YPOS,
12758 SOUND_CTRL_ID_MUSIC,
12759 "background music on/off"
12762 SOUND_BUTTON_LOOPS_XPOS, SOUND_BUTTON_YPOS,
12763 SOUND_CTRL_ID_LOOPS,
12764 "sound loops on/off"
12767 SOUND_BUTTON_SIMPLE_XPOS, SOUND_BUTTON_YPOS,
12768 SOUND_CTRL_ID_SIMPLE,
12769 "normal sounds on/off"
12773 void CreateGameButtons()
12777 for (i = 0; i < NUM_GAME_BUTTONS; i++)
12779 Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
12780 struct GadgetInfo *gi;
12783 unsigned long event_mask;
12784 int gd_xoffset, gd_yoffset;
12785 int gd_x1, gd_x2, gd_y1, gd_y2;
12788 gd_xoffset = gamebutton_info[i].x;
12789 gd_yoffset = gamebutton_info[i].y;
12790 gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
12791 gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
12793 if (id == GAME_CTRL_ID_STOP ||
12794 id == GAME_CTRL_ID_PAUSE ||
12795 id == GAME_CTRL_ID_PLAY)
12797 button_type = GD_TYPE_NORMAL_BUTTON;
12799 event_mask = GD_EVENT_RELEASED;
12800 gd_y1 = DOOR_GFX_PAGEY1 + gd_yoffset - GAME_BUTTON_YSIZE;
12801 gd_y2 = DOOR_GFX_PAGEY1 + gd_yoffset - GAME_BUTTON_YSIZE;
12805 button_type = GD_TYPE_CHECK_BUTTON;
12807 ((id == SOUND_CTRL_ID_MUSIC && setup.sound_music) ||
12808 (id == SOUND_CTRL_ID_LOOPS && setup.sound_loops) ||
12809 (id == SOUND_CTRL_ID_SIMPLE && setup.sound_simple) ? TRUE : FALSE);
12810 event_mask = GD_EVENT_PRESSED;
12811 gd_y1 = DOOR_GFX_PAGEY1 + gd_yoffset;
12812 gd_y2 = DOOR_GFX_PAGEY1 + gd_yoffset - GAME_BUTTON_YSIZE;
12815 gi = CreateGadget(GDI_CUSTOM_ID, id,
12816 GDI_INFO_TEXT, gamebutton_info[i].infotext,
12817 GDI_X, DX + gd_xoffset,
12818 GDI_Y, DY + gd_yoffset,
12819 GDI_WIDTH, GAME_BUTTON_XSIZE,
12820 GDI_HEIGHT, GAME_BUTTON_YSIZE,
12821 GDI_TYPE, button_type,
12822 GDI_STATE, GD_BUTTON_UNPRESSED,
12823 GDI_CHECKED, checked,
12824 GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
12825 GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y1,
12826 GDI_ALT_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y2,
12827 GDI_ALT_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
12828 GDI_EVENT_MASK, event_mask,
12829 GDI_CALLBACK_ACTION, HandleGameButtons,
12833 Error(ERR_EXIT, "cannot create gadget");
12835 game_gadget[id] = gi;
12839 void FreeGameButtons()
12843 for (i = 0; i < NUM_GAME_BUTTONS; i++)
12844 FreeGadget(game_gadget[i]);
12847 static void MapGameButtons()
12851 for (i = 0; i < NUM_GAME_BUTTONS; i++)
12852 MapGadget(game_gadget[i]);
12855 void UnmapGameButtons()
12859 for (i = 0; i < NUM_GAME_BUTTONS; i++)
12860 UnmapGadget(game_gadget[i]);
12863 static void HandleGameButtons(struct GadgetInfo *gi)
12865 int id = gi->custom_id;
12867 if (game_status != GAME_MODE_PLAYING)
12872 case GAME_CTRL_ID_STOP:
12876 RequestQuitGame(TRUE);
12879 case GAME_CTRL_ID_PAUSE:
12880 if (options.network)
12882 #if defined(NETWORK_AVALIABLE)
12884 SendToServer_ContinuePlaying();
12886 SendToServer_PausePlaying();
12890 TapeTogglePause(TAPE_TOGGLE_MANUAL);
12893 case GAME_CTRL_ID_PLAY:
12896 #if defined(NETWORK_AVALIABLE)
12897 if (options.network)
12898 SendToServer_ContinuePlaying();
12902 tape.pausing = FALSE;
12903 DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF, 0);
12908 case SOUND_CTRL_ID_MUSIC:
12909 if (setup.sound_music)
12911 setup.sound_music = FALSE;
12914 else if (audio.music_available)
12916 setup.sound = setup.sound_music = TRUE;
12918 SetAudioMode(setup.sound);
12924 case SOUND_CTRL_ID_LOOPS:
12925 if (setup.sound_loops)
12926 setup.sound_loops = FALSE;
12927 else if (audio.loops_available)
12929 setup.sound = setup.sound_loops = TRUE;
12930 SetAudioMode(setup.sound);
12934 case SOUND_CTRL_ID_SIMPLE:
12935 if (setup.sound_simple)
12936 setup.sound_simple = FALSE;
12937 else if (audio.sound_available)
12939 setup.sound = setup.sound_simple = TRUE;
12940 SetAudioMode(setup.sound);