changed order of cave tile changes in EM engine
[rocksndiamonds.git] / src / game_em / logic.c
1 /* 2008-09-24 23:20:29
2  *
3  * David Tritscher
4  *
5  * my own version of the emerald mine engine
6  */
7
8 #include "main_em.h"
9
10
11 #define SPRING_ROLL     /* spring rolling off round things continues to roll */
12 #define USE_CHANGED_ACID_STUFF
13
14 #define RANDOM_RAW      (seed = seed << 31 | seed >> 1)
15 #define RANDOM(x)       (RANDOM_RAW & (x - 1))
16
17 static unsigned int seed;
18 static int score;
19
20 static void Lboom_generic(int x, int y, int element, int element_middle)
21 {
22   Boom[x-1][y-1] = element;
23   Boom[x][y-1]   = element;
24   Boom[x+1][y-1] = element;
25   Boom[x-1][y]   = element;
26   Boom[x][y]     = element_middle;
27   Boom[x+1][y]   = element;
28   Boom[x-1][y+1] = element;
29   Boom[x][y+1]   = element;
30   Boom[x+1][y+1] = element;
31 }
32
33 static void Lboom_bug(int x, int y, int by_element)
34 {
35   Next[x][y] = Znormal;
36
37   Lboom_generic(x, y, Xemerald, Xdiamond);
38
39 #if PLAY_ELEMENT_SOUND
40   play_element_sound(x, y, SOUND_boom, by_element);
41 #endif
42 }
43
44 static void Lboom_tank(int x, int y, int by_element)
45 {
46   Next[x][y] = Znormal;
47
48   Lboom_generic(x, y, Xblank, Xblank);
49
50 #if PLAY_ELEMENT_SOUND
51   play_element_sound(x, y, SOUND_boom, by_element);
52 #endif
53 }
54
55 static void Lboom_eater(int x, int y, int by_element)
56 {
57   Next[x][y] = Znormal;
58
59   Boom[x-1][y-1] = lev.eater_array[lev.eater_pos][0];
60   Boom[x][y-1]   = lev.eater_array[lev.eater_pos][1];
61   Boom[x+1][y-1] = lev.eater_array[lev.eater_pos][2];
62   Boom[x-1][y]   = lev.eater_array[lev.eater_pos][3];
63   Boom[x][y]     = lev.eater_array[lev.eater_pos][4];
64   Boom[x+1][y]   = lev.eater_array[lev.eater_pos][5];
65   Boom[x-1][y+1] = lev.eater_array[lev.eater_pos][6];
66   Boom[x][y+1]   = lev.eater_array[lev.eater_pos][7];
67   Boom[x+1][y+1] = lev.eater_array[lev.eater_pos][8];
68
69   lev.eater_pos = (lev.eater_pos + 1) & 7;
70
71 #if PLAY_ELEMENT_SOUND
72   play_element_sound(x, y, SOUND_boom, by_element);
73 #endif
74 }
75
76 static boolean player_killed(struct PLAYER *ply)
77 {
78   int x = ply->x;
79   int y = ply->y;
80
81   if (!ply->alive)
82     return FALSE;
83
84   if (lev.killed_out_of_time && setup.time_limit)
85     return TRUE;
86
87   switch(Cave[x][y-1])
88   {
89     case Xbug_1_n:
90     case Xbug_1_e:
91     case Xbug_1_s:
92     case Xbug_1_w:
93     case Xbug_2_n:
94     case Xbug_2_e:
95     case Xbug_2_s:
96     case Xbug_2_w:
97     case Xtank_1_n:
98     case Xtank_1_e:
99     case Xtank_1_s:
100     case Xtank_1_w:
101     case Xtank_2_n:
102     case Xtank_2_e:
103     case Xtank_2_s:
104     case Xtank_2_w:
105       return TRUE;
106   }
107
108   switch(Cave[x+1][y])
109   {
110     case Xbug_1_n:
111     case Xbug_1_e:
112     case Xbug_1_s:
113     case Xbug_1_w:
114     case Xbug_2_n:
115     case Xbug_2_e:
116     case Xbug_2_s:
117     case Xbug_2_w:
118     case Xtank_1_n:
119     case Xtank_1_e:
120     case Xtank_1_s:
121     case Xtank_1_w:
122     case Xtank_2_n:
123     case Xtank_2_e:
124     case Xtank_2_s:
125     case Xtank_2_w:
126       return TRUE;
127   }
128
129   switch(Cave[x][y+1])
130   {
131     case Xbug_1_n:
132     case Xbug_1_e:
133     case Xbug_1_s:
134     case Xbug_1_w:
135     case Xbug_2_n:
136     case Xbug_2_e:
137     case Xbug_2_s:
138     case Xbug_2_w:
139     case Xtank_1_n:
140     case Xtank_1_e:
141     case Xtank_1_s:
142     case Xtank_1_w:
143     case Xtank_2_n:
144     case Xtank_2_e:
145     case Xtank_2_s:
146     case Xtank_2_w:
147       return TRUE;
148   }
149
150   switch(Cave[x-1][y])
151   {
152     case Xbug_1_n:
153     case Xbug_1_e:
154     case Xbug_1_s:
155     case Xbug_1_w:
156     case Xbug_2_n:
157     case Xbug_2_e:
158     case Xbug_2_s:
159     case Xbug_2_w:
160     case Xtank_1_n:
161     case Xtank_1_e:
162     case Xtank_1_s:
163     case Xtank_1_w:
164     case Xtank_2_n:
165     case Xtank_2_e:
166     case Xtank_2_s:
167     case Xtank_2_w:
168       return TRUE;
169   }
170
171   switch(Cave[x][y])
172   {
173     case Xblank:
174     case Xacid_splash_e:
175     case Xacid_splash_w:
176     case Zplayer:
177     case Xdynamite_1:
178     case Xdynamite_2:
179     case Xdynamite_3:
180     case Xdynamite_4:
181 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
182     case Xfake_acid_1:
183     case Xfake_acid_2:
184     case Xfake_acid_3:
185     case Xfake_acid_4:
186     case Xfake_acid_5:
187     case Xfake_acid_6:
188     case Xfake_acid_7:
189     case Xfake_acid_8:
190 #endif
191       return FALSE;
192   }
193
194   return TRUE;
195 }
196
197 static void kill_player(struct PLAYER *ply)
198 {
199   int x = ply->x;
200   int y = ply->y;
201
202   ply->alive = 0;
203
204   switch(Cave[x][y-1])
205   {
206     case Xbug_1_n:
207     case Xbug_1_e:
208     case Xbug_1_s:
209     case Xbug_1_w:
210     case Xbug_2_n:
211     case Xbug_2_e:
212     case Xbug_2_s:
213     case Xbug_2_w:
214       Cave[x][y-1] = Xboom_bug;
215       break;
216
217     case Xtank_1_n:
218     case Xtank_1_e:
219     case Xtank_1_s:
220     case Xtank_1_w:
221     case Xtank_2_n:
222     case Xtank_2_e:
223     case Xtank_2_s:
224     case Xtank_2_w:
225       Cave[x][y-1] = Xboom_bomb;
226       break;
227   }
228
229   switch(Cave[x+1][y])
230   {
231     case Xbug_1_n:
232     case Xbug_1_e:
233     case Xbug_1_s:
234     case Xbug_1_w:
235     case Xbug_2_n:
236     case Xbug_2_e:
237     case Xbug_2_s:
238     case Xbug_2_w:
239       Cave[x+1][y] = Xboom_bug;
240       break;
241
242     case Xtank_1_n:
243     case Xtank_1_e:
244     case Xtank_1_s:
245     case Xtank_1_w:
246     case Xtank_2_n:
247     case Xtank_2_e:
248     case Xtank_2_s:
249     case Xtank_2_w:
250       Cave[x+1][y] = Xboom_bomb;
251       break;
252   }
253
254   switch(Cave[x][y+1])
255   {
256     case Xbug_1_n:
257     case Xbug_1_e:
258     case Xbug_1_s:
259     case Xbug_1_w:
260     case Xbug_2_n:
261     case Xbug_2_e:
262     case Xbug_2_s:
263     case Xbug_2_w:
264       Cave[x][y+1] = Xboom_bug;
265       break;
266
267     case Xtank_1_n:
268     case Xtank_1_e:
269     case Xtank_1_s:
270     case Xtank_1_w:
271     case Xtank_2_n:
272     case Xtank_2_e:
273     case Xtank_2_s:
274     case Xtank_2_w:
275       Cave[x][y+1] = Xboom_bomb;
276       break;
277   }
278
279   switch(Cave[x-1][y])
280   {
281     case Xbug_1_n:
282     case Xbug_1_e:
283     case Xbug_1_s:
284     case Xbug_1_w:
285     case Xbug_2_n:
286     case Xbug_2_e:
287     case Xbug_2_s:
288     case Xbug_2_w:
289       Cave[x-1][y] = Xboom_bug;
290       break;
291
292     case Xtank_1_n:
293     case Xtank_1_e:
294     case Xtank_1_s:
295     case Xtank_1_w:
296     case Xtank_2_n:
297     case Xtank_2_e:
298     case Xtank_2_s:
299     case Xtank_2_w:
300       Cave[x-1][y] = Xboom_bomb;
301       break;
302   }
303
304   switch(Cave[x][y])
305   {
306     case Xexit_1:
307     case Xexit_2:
308     case Xexit_3:
309       lev.exit_x = x;
310       lev.exit_y = y;
311       play_element_sound(x, y, SOUND_exit_leave, Xexit_1);
312       break;
313
314     default:
315       play_element_sound(x, y, SOUND_die, Zplayer);
316       break;
317   }
318
319   switch(Cave[x][y])
320   {
321 #ifdef USE_CHANGED_ACID_STUFF
322     case Xacid_1:
323     case Xacid_2:
324     case Xacid_3:
325     case Xacid_4:
326     case Xacid_5:
327     case Xacid_6:
328     case Xacid_7:
329     case Xacid_8:
330       break;
331 #endif
332
333     default:
334       Cave[x][y] = Xboom_1;
335       Boom[x][y] = Xblank;
336       break;
337   }
338 }
339
340 static boolean player_digfield(struct PLAYER *ply, int dx, int dy)
341 {
342   int anim = (dx < 0 ? 3 : dx > 0 ? 1 : dy < 0 ? 0 : dy > 0 ? 2 : 2);
343   int oldx = ply->x;
344   int oldy = ply->y;
345   int x = oldx + dx;
346   int y = oldy + dy;
347   boolean result = TRUE;
348
349   if (!dx && !dy)                       /* no direction specified */
350     return FALSE;
351
352   if (dx && dy && ply->joy_snap)        /* more than one direction specified */
353     return FALSE;
354
355   if (ply->joy_snap == 0)               /* player wants to move */
356   {
357     int element = Cave[x][y];
358
359     switch(Cave[x][y])
360     {
361       /* fire is released */
362       case Xblank:
363       case Xacid_splash_e:
364       case Xacid_splash_w:
365         Cave[x][y] = Zplayer;
366         Next[x][y] = Zplayer;
367 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
368       case Xfake_acid_1:
369       case Xfake_acid_2:
370       case Xfake_acid_3:
371       case Xfake_acid_4:
372       case Xfake_acid_5:
373       case Xfake_acid_6:
374       case Xfake_acid_7:
375       case Xfake_acid_8:
376 #endif
377         play_element_sound(x, y, SOUND_blank, Xblank);
378         ply->anim = PLY_walk_n + anim;
379         ply->x = x;
380         ply->y = y;
381         break;
382
383 #ifdef USE_CHANGED_ACID_STUFF
384       case Xacid_1:
385       case Xacid_2:
386       case Xacid_3:
387       case Xacid_4:
388       case Xacid_5:
389       case Xacid_6:
390       case Xacid_7:
391       case Xacid_8:
392         if (Cave[x+1][y-1] == Xblank)
393           Cave[x+1][y-1] = Xacid_splash_e;
394         if (Cave[x-1][y-1] == Xblank)
395           Cave[x-1][y-1] = Xacid_splash_w;
396         play_element_sound(x, y, SOUND_acid, Xacid_1);
397 #endif
398
399       case Xboom_android:
400       case Xboom_1:
401       case Xbug_1_n:
402       case Xbug_1_e:
403       case Xbug_1_s:
404       case Xbug_1_w:
405       case Xbug_2_n:
406       case Xbug_2_e:
407       case Xbug_2_s:
408       case Xbug_2_w:
409       case Xtank_1_n:
410       case Xtank_1_e:
411       case Xtank_1_s:
412       case Xtank_1_w:
413       case Xtank_2_n:
414       case Xtank_2_e:
415       case Xtank_2_s:
416       case Xtank_2_w:
417
418 #ifndef USE_CHANGED_ACID_STUFF
419       case Xacid_1:
420       case Xacid_2:
421       case Xacid_3:
422       case Xacid_4:
423       case Xacid_5:
424       case Xacid_6:
425       case Xacid_7:
426       case Xacid_8:
427 #endif
428         ply->anim = PLY_walk_n + anim;
429         ply->x = x;
430         ply->y = y;
431         break;
432
433       case Xgrass:
434         Cave[x][y] = (dy ? (dy < 0 ? Ygrass_nB : Ygrass_sB) :
435                       (dx > 0 ? Ygrass_eB : Ygrass_wB));
436         Next[x][y] = Zplayer;
437         play_element_sound(x, y, SOUND_dirt, Xgrass);
438         ply->anim = PLY_walk_n + anim;
439         ply->x = x;
440         ply->y = y;
441         break;
442
443       case Xdirt:
444         Cave[x][y] = (dy ? (dy < 0 ? Ydirt_nB : Ydirt_sB) :
445                       (dx > 0 ? Ydirt_eB : Ydirt_wB));
446         Next[x][y] = Zplayer;
447         play_element_sound(x, y, SOUND_dirt, Xdirt);
448         ply->anim = PLY_walk_n + anim;
449         ply->x = x;
450         ply->y = y;
451         break;
452
453       case Xdiamond:
454       case Xdiamond_pause:
455         Cave[x][y] = Ydiamond_blank;
456         Next[x][y] = Zplayer;
457         play_element_sound(x, y, SOUND_collect, element);
458         lev.score += lev.diamond_score;
459         lev.required = lev.required < 3 ? 0 : lev.required - 3;
460         game.snapshot.collected_item = TRUE;
461         ply->anim = PLY_walk_n + anim;
462         ply->x = x;
463         ply->y = y;
464         break;
465
466       case Xemerald:
467       case Xemerald_pause:
468         Cave[x][y] = Yemerald_blank;
469         Next[x][y] = Zplayer;
470         play_element_sound(x, y, SOUND_collect, element);
471         lev.score += lev.emerald_score;
472         lev.required = lev.required < 1 ? 0 : lev.required - 1;
473         game.snapshot.collected_item = TRUE;
474         ply->anim = PLY_walk_n + anim;
475         ply->x = x;
476         ply->y = y;
477         break;
478
479       case Xdynamite:
480         Cave[x][y] = Ydynamite_blank;
481         Next[x][y] = Zplayer;
482         play_element_sound(x, y, SOUND_collect, element);
483         lev.score += lev.dynamite_score;
484         ply->dynamite = ply->dynamite > 9998 ? 9999 : ply->dynamite + 1;
485         ply->anim = PLY_walk_n + anim;
486         ply->x = x;
487         ply->y = y;
488         break;
489
490       case Xkey_1:
491         ply->keys |= 0x01;
492         Cave[x][y] = Ykey_1_blank;
493         goto key_walk;
494
495       case Xkey_2:
496         ply->keys |= 0x02;
497         Cave[x][y] = Ykey_2_blank;
498         goto key_walk;
499
500       case Xkey_3:
501         ply->keys |= 0x04;
502         Cave[x][y] = Ykey_3_blank;
503         goto key_walk;
504
505       case Xkey_4:
506         ply->keys |= 0x08;
507         Cave[x][y] = Ykey_4_blank;
508         goto key_walk;
509
510       case Xkey_5:
511         ply->keys |= 0x10;
512         Cave[x][y] = Ykey_5_blank;
513         goto key_walk;
514
515       case Xkey_6:
516         ply->keys |= 0x20;
517         Cave[x][y] = Ykey_6_blank;
518         goto key_walk;
519
520       case Xkey_7:
521         ply->keys |= 0x40;
522         Cave[x][y] = Ykey_7_blank;
523         goto key_walk;
524
525       case Xkey_8:
526         ply->keys |= 0x80;
527         Cave[x][y] = Ykey_8_blank;
528         goto key_walk;
529
530       key_walk:
531
532         Next[x][y] = Zplayer;
533         play_element_sound(x, y, SOUND_collect, element);
534         lev.score += lev.key_score;
535         ply->anim = PLY_walk_n + anim;
536         ply->x = x;
537         ply->y = y;
538         break;
539
540       case Xlenses:
541         Cave[x][y] = Ylenses_blank;
542         Next[x][y] = Zplayer;
543         play_element_sound(x, y, SOUND_collect, element);
544         lev.score += lev.lenses_score;
545         lev.lenses_cnt = lev.lenses_time;
546         ply->anim = PLY_walk_n + anim;
547         ply->x = x;
548         ply->y = y;
549         break;
550
551       case Xmagnify:
552         Cave[x][y] = Ymagnify_blank;
553         Next[x][y] = Zplayer;
554         play_element_sound(x, y, SOUND_collect, element);
555         lev.score += lev.magnify_score;
556         lev.magnify_cnt = lev.magnify_time;
557         ply->anim = PLY_walk_n + anim;
558         ply->x = x;
559         ply->y = y;
560         break;
561
562       case Xstone:
563         if (dy)
564           break;
565
566         switch(Cave[x+dx][y])
567         {
568           case Xblank:
569           case Xacid_splash_e:
570           case Xacid_splash_w:
571             Cave[x+dx][y] = dx > 0 ? Ystone_e : Ystone_w;
572             Next[x+dx][y] = Xstone_pause;
573             goto stone_walk;
574
575           case Xacid_1:
576           case Xacid_2:
577           case Xacid_3:
578           case Xacid_4:
579           case Xacid_5:
580           case Xacid_6:
581           case Xacid_7:
582           case Xacid_8:
583             if (Cave[x+dx+1][y-1] == Xblank)
584               Cave[x+dx+1][y-1] = Xacid_splash_e;
585             if (Cave[x+dx-1][y-1] == Xblank)
586               Cave[x+dx-1][y-1] = Xacid_splash_w;
587             play_element_sound(x, y, SOUND_acid, Xacid_1);
588
589           stone_walk:
590
591             Cave[x][y] = dx > 0 ? Ystone_eB : Ystone_wB;
592             Next[x][y] = Zplayer;
593             play_element_sound(x, y, SOUND_roll, Xstone);
594             ply->x = x;
595             break;
596         }
597
598         ply->anim = PLY_push_n + anim;
599         break;
600
601       case Xbomb:
602         if (dy)
603           break;
604
605         switch(Cave[x+dx][y])
606         {
607           case Xblank:
608           case Xacid_splash_e:
609           case Xacid_splash_w:
610             Cave[x+dx][y] = dx > 0 ? Ybomb_e : Ybomb_w;
611             Next[x+dx][y] = Xbomb_pause;
612             goto bomb_walk;
613
614           case Xacid_1:
615           case Xacid_2:
616           case Xacid_3:
617           case Xacid_4:
618           case Xacid_5:
619           case Xacid_6:
620           case Xacid_7:
621           case Xacid_8:
622             if (Cave[x+dx+1][y-1] == Xblank)
623               Cave[x+dx+1][y-1] = Xacid_splash_e;
624             if (Cave[x+dx-1][y-1] == Xblank)
625               Cave[x+dx-1][y-1] = Xacid_splash_w;
626             play_element_sound(x, y, SOUND_acid, Xacid_1);
627
628           bomb_walk:
629
630             Cave[x][y] = dx > 0 ? Ybomb_eB : Ybomb_wB;
631             Next[x][y] = Zplayer;
632             play_element_sound(x, y, SOUND_roll, Xbomb);
633             ply->x = x;
634             break;
635         }
636
637         ply->anim = PLY_push_n + anim;
638         break;
639
640       case Xnut:
641         if (dy)
642           break;
643
644         switch(Cave[x+dx][y])
645         {
646           case Xblank:
647           case Xacid_splash_e:
648           case Xacid_splash_w:
649             Cave[x+dx][y] = dx > 0 ? Ynut_e : Ynut_w;
650             Next[x+dx][y] = Xnut_pause;
651             goto nut_walk;
652
653           case Xacid_1:
654           case Xacid_2:
655           case Xacid_3:
656           case Xacid_4:
657           case Xacid_5:
658           case Xacid_6:
659           case Xacid_7:
660           case Xacid_8:
661             if (Cave[x+dx+1][y-1] == Xblank)
662               Cave[x+dx+1][y-1] = Xacid_splash_e;
663             if (Cave[x+dx-1][y-1] == Xblank)
664               Cave[x+dx-1][y-1] = Xacid_splash_w;
665             play_element_sound(x, y, SOUND_acid, Xacid_1);
666
667           nut_walk:
668
669             Cave[x][y] = dx > 0 ? Ynut_eB : Ynut_wB;
670             Next[x][y] = Zplayer;
671             play_element_sound(x, y, SOUND_roll, Xnut);
672             ply->x = x;
673             break;
674         }
675
676         ply->anim = PLY_push_n + anim;
677         break;
678
679       case Xspring:
680         if (dy)
681           break;
682
683         switch(Cave[x+dx][y])
684         {
685           case Xblank:
686           case Xacid_splash_e:
687           case Xacid_splash_w:
688             Cave[x+dx][y] = dx > 0 ? Yspring_e : Yspring_w;
689             Next[x+dx][y] = dx > 0 ? Xspring_e : Xspring_w;
690             goto spring_walk;
691
692           case Xacid_1:
693           case Xacid_2:
694           case Xacid_3:
695           case Xacid_4:
696           case Xacid_5:
697           case Xacid_6:
698           case Xacid_7:
699           case Xacid_8:
700             if (Cave[x+dx+1][y-1] == Xblank)
701               Cave[x+dx+1][y-1] = Xacid_splash_e;
702             if (Cave[x+dx-1][y-1] == Xblank)
703               Cave[x+dx-1][y-1] = Xacid_splash_w;
704             play_element_sound(x, y, SOUND_acid, Xacid_1);
705
706           spring_walk:
707
708             Cave[x][y] = dx > 0 ? Yspring_eB : Yspring_wB;
709             Next[x][y] = Zplayer;
710             play_element_sound(x, y, SOUND_roll, Xspring);
711             ply->x = x;
712             break;
713
714           case Xalien:
715           case Xalien_pause:
716             Cave[x][y] = dx > 0 ? Yspring_alien_eB : Yspring_alien_wB;
717             Cave[x+dx][y] = dx > 0 ? Yspring_alien_e : Yspring_alien_w;
718             Next[x][y] = Zplayer;
719             Next[x+dx][y] = dx > 0 ? Xspring_e : Xspring_w;
720             play_element_sound(x, y, SOUND_slurp, Xalien);
721             lev.score += lev.slurp_score;
722             ply->x = x;
723             break;
724         }
725
726         ply->anim = PLY_push_n + anim;
727         break;
728
729       case Xspring_pause:
730       case Xstone_pause:
731       case Xbomb_pause:
732       case Xnut_pause:
733       case Xsand_stonein_1:
734       case Xsand_stonein_2:
735       case Xsand_stonein_3:
736       case Xsand_stonein_4:
737         if (dy)
738           break;
739
740         ply->anim = PLY_push_n + anim;
741         break;
742
743       case Xballoon:
744         switch(Cave[x+dx][y+dy])
745         {
746           case Xblank:
747           case Xacid_splash_e:
748           case Xacid_splash_w:
749             Cave[x+dx][y+dy] = (dy ? (dy < 0 ? Yballoon_n : Yballoon_s) :
750                                 (dx > 0 ? Yballoon_e : Yballoon_w));
751             Next[x+dx][y+dy] = Xballoon;
752             goto balloon_walk;
753
754           case Xacid_1:
755           case Xacid_2:
756           case Xacid_3:
757           case Xacid_4:
758           case Xacid_5:
759           case Xacid_6:
760           case Xacid_7:
761           case Xacid_8:
762             if (Cave[x+dx+1][y+dy-1] == Xblank)
763               Cave[x+dx+1][y+dy-1] = Xacid_splash_e;
764             if (Cave[x+dx-1][y+dy-1] == Xblank)
765               Cave[x+dx-1][y+dy-1] = Xacid_splash_w;
766             play_element_sound(x, y, SOUND_acid, Xacid_1);
767
768           balloon_walk:
769
770             Cave[x][y] = (dy ? (dy < 0 ? Yballoon_nB : Yballoon_sB) :
771                           (dx > 0 ? Yballoon_eB : Yballoon_wB));
772             Next[x][y] = Zplayer;
773             play_element_sound(x, y, SOUND_push, Xballoon);
774             ply->x = x;
775             ply->y = y;
776             break;
777         }
778
779         ply->anim = PLY_push_n + anim;
780         break;
781
782       case Xandroid:
783       case Xandroid_1_n:
784       case Xandroid_2_n:
785       case Xandroid_1_e:
786       case Xandroid_2_e:
787       case Xandroid_1_s:
788       case Xandroid_2_s:
789       case Xandroid_1_w:
790       case Xandroid_2_w:
791         switch(Cave[x+dx][y+dy])
792         {
793           case Xblank:
794           case Xacid_splash_e:
795           case Xacid_splash_w:
796             Cave[x+dx][y+dy] = (dy ? (dy < 0 ? Yandroid_n : Yandroid_s) :
797                                 (dx > 0 ? Yandroid_e : Yandroid_w));
798             Next[x+dx][y+dy] = (dy ? (dy < 0 ? Xandroid_2_n : Xandroid_2_s) :
799                                 (dx > 0 ? Xandroid_2_e : Xandroid_2_w));
800             goto android_walk;
801
802           case Xacid_1:
803           case Xacid_2:
804           case Xacid_3:
805           case Xacid_4:
806           case Xacid_5:
807           case Xacid_6:
808           case Xacid_7:
809           case Xacid_8:
810             if (Cave[x+dx+1][y+dy-1] == Xblank)
811               Cave[x+dx+1][y+dy-1] = Xacid_splash_e;
812             if (Cave[x+dx-1][y+dy-1] == Xblank)
813               Cave[x+dx-1][y+dy-1] = Xacid_splash_w;
814             play_element_sound(x, y, SOUND_acid, Xacid_1);
815
816           android_walk:
817
818             Cave[x][y] = (dy ? (dy < 0 ? Yandroid_nB : Yandroid_sB) :
819                           (dx > 0 ? Yandroid_eB : Yandroid_wB));
820             Next[x][y] = Zplayer;
821             play_element_sound(x, y, SOUND_push, Xandroid);
822             ply->x = x;
823             ply->y = y;
824             break;
825         }
826
827         ply->anim = PLY_push_n + anim;
828         break;
829
830       case Xdoor_1:
831       case Xfake_door_1:
832         if (ply->keys & 0x01)
833           goto door_walk;
834         else
835           break;
836
837       case Xdoor_2:
838       case Xfake_door_2:
839         if (ply->keys & 0x02)
840           goto door_walk;
841         else
842           break;
843
844       case Xdoor_3:
845       case Xfake_door_3:
846         if (ply->keys & 0x04)
847           goto door_walk;
848         else
849           break;
850
851       case Xdoor_4:
852       case Xfake_door_4:
853         if (ply->keys & 0x08)
854           goto door_walk;
855         else
856           break;
857
858       case Xdoor_5:
859       case Xfake_door_5:
860         if (ply->keys & 0x10)
861           goto door_walk;
862         else
863           break;
864
865       case Xdoor_6:
866       case Xfake_door_6:
867         if (ply->keys & 0x20)
868           goto door_walk;
869         else
870           break;
871
872       case Xdoor_7:
873       case Xfake_door_7:
874         if (ply->keys & 0x40)
875           goto door_walk;
876         else
877           break;
878
879       case Xdoor_8:
880       case Xfake_door_8:
881         if (ply->keys & 0x80)
882           goto door_walk;
883         else
884           break;
885
886       door_walk:
887
888         if (!tab_blank[Cave[x+dx][y+dy]])
889           break;
890
891         Cave[x+dx][y+dy] = Zplayer;
892         Next[x+dx][y+dy] = Zplayer;
893         play_element_sound(x, y, SOUND_door, element);
894         ply->anim = PLY_walk_n + anim;
895         ply->x = x + dx;
896         ply->y = y + dy;
897         break;
898
899       case Xwheel:
900         play_element_sound(x, y, SOUND_press, element);
901         lev.wheel_cnt = lev.wheel_time;
902         lev.wheel_x = x;
903         lev.wheel_y = y;
904         break;
905
906       case Xwind_n:
907         lev.wind_direction = 0;
908         goto wind_walk;
909
910       case Xwind_e:
911         lev.wind_direction = 1;
912         goto wind_walk;
913
914       case Xwind_s:
915         lev.wind_direction = 2;
916         goto wind_walk;
917
918       case Xwind_w:
919         lev.wind_direction = 3;
920         goto wind_walk;
921
922       case Xwind_any:
923         lev.wind_direction = dy ? (dy < 0 ? 0 : 2) : (dx > 0 ? 1 : 3);
924         goto wind_walk;
925
926       wind_walk:
927
928         play_element_sound(x, y, SOUND_press, element);
929         lev.wind_cnt = lev.wind_time;
930         break;
931
932       case Xwind_stop:
933         play_element_sound(x, y, SOUND_press, element);
934         lev.wind_cnt = 0;
935         break;
936
937       case Xswitch:
938         play_element_sound(x, y, SOUND_press, element);
939         lev.ball_cnt = lev.ball_time;
940         lev.ball_state = !lev.ball_state;
941         break;
942
943       case Xplant:
944         Cave[x][y] = Yplant;
945         Next[x][y] = Xplant;
946         play_element_sound(x, y, SOUND_blank, Xplant);
947         ply->anim = PLY_walk_n + anim;
948         ply->x = x;
949         ply->y = y;
950         break;
951
952       case Xexit_1:
953       case Xexit_2:
954       case Xexit_3:
955         lev.home--;
956
957         if (lev.home == 0)
958           game_em.level_solved = TRUE;
959
960         ply->anim = PLY_walk_n + anim;
961         ply->x = x;
962         ply->y = y;
963
964         break;
965     }
966
967     if (ply->x == oldx && ply->y == oldy)       /* no movement */
968       result = FALSE;
969   }
970   else                                  /* player wants to snap */
971   {
972     int element = Cave[x][y];
973
974     switch(Cave[x][y])
975     {
976       /* fire is pressed */
977
978       case Xgrass:
979         Cave[x][y] = Ygrass_blank;
980         Next[x][y] = Xblank;
981         play_element_sound(x, y, SOUND_dirt, element);
982         ply->anim = PLY_shoot_n + anim;
983         break;
984
985       case Xdirt:
986         Cave[x][y] = Ydirt_blank;
987         Next[x][y] = Xblank;
988         play_element_sound(x, y, SOUND_dirt, element);
989         ply->anim = PLY_shoot_n + anim;
990         break;
991
992       case Xdiamond:
993       case Xdiamond_pause:
994         Cave[x][y] = Ydiamond_blank;
995         Next[x][y] = Xblank;
996         play_element_sound(x, y, SOUND_collect, element);
997         lev.score += lev.diamond_score;
998         lev.required = lev.required < 3 ? 0 : lev.required - 3;
999         game.snapshot.collected_item = TRUE;
1000         ply->anim = PLY_walk_n + anim;
1001         break;
1002
1003       case Xemerald:
1004       case Xemerald_pause:
1005         Cave[x][y] = Yemerald_blank;
1006         Next[x][y] = Xblank;
1007         play_element_sound(x, y, SOUND_collect, element);
1008         lev.score += lev.emerald_score;
1009         lev.required = lev.required < 1 ? 0 : lev.required - 1;
1010         game.snapshot.collected_item = TRUE;
1011         ply->anim = PLY_walk_n + anim;
1012         break;
1013
1014       case Xdynamite:
1015         Cave[x][y] = Ydynamite_blank;
1016         Next[x][y] = Xblank;
1017         play_element_sound(x, y, SOUND_collect, element);
1018         lev.score += lev.dynamite_score;
1019         ply->dynamite = ply->dynamite > 9998 ? 9999 : ply->dynamite + 1;
1020         ply->anim = PLY_walk_n + anim;
1021         break;
1022
1023       case Xkey_1:
1024         ply->keys |= 0x01;
1025         Cave[x][y] = Ykey_1_blank;
1026         goto key_shoot;
1027
1028       case Xkey_2:
1029         ply->keys |= 0x02;
1030         Cave[x][y] = Ykey_2_blank;
1031         goto key_shoot;
1032
1033       case Xkey_3:
1034         ply->keys |= 0x04;
1035         Cave[x][y] = Ykey_3_blank;
1036         goto key_shoot;
1037
1038       case Xkey_4:
1039         ply->keys |= 0x08;
1040         Cave[x][y] = Ykey_4_blank;
1041         goto key_shoot;
1042
1043       case Xkey_5:
1044         ply->keys |= 0x10;
1045         Cave[x][y] = Ykey_5_blank;
1046         goto key_shoot;
1047
1048       case Xkey_6:
1049         ply->keys |= 0x20;
1050         Cave[x][y] = Ykey_6_blank;
1051         goto key_shoot;
1052
1053       case Xkey_7:
1054         ply->keys |= 0x40;
1055         Cave[x][y] = Ykey_7_blank;
1056         goto key_shoot;
1057
1058       case Xkey_8:
1059         ply->keys |= 0x80;
1060         Cave[x][y] = Ykey_8_blank;
1061         goto key_shoot;
1062
1063       key_shoot:
1064
1065         Next[x][y] = Xblank;
1066         play_element_sound(x, y, SOUND_collect, element);
1067         lev.score += lev.key_score;
1068         ply->anim = PLY_walk_n + anim;
1069         break;
1070
1071       case Xlenses:
1072         Cave[x][y] = Ylenses_blank;
1073         Next[x][y] = Xblank;
1074         play_element_sound(x, y, SOUND_collect, element);
1075         lev.score += lev.lenses_score;
1076         lev.lenses_cnt = lev.lenses_time;
1077         ply->anim = PLY_walk_n + anim;
1078         break;
1079
1080       case Xmagnify:
1081         Cave[x][y] = Ymagnify_blank;
1082         Next[x][y] = Xblank;
1083         play_element_sound(x, y, SOUND_collect, element);
1084         lev.score += lev.magnify_score;
1085         lev.magnify_cnt = lev.magnify_time;
1086         ply->anim = PLY_walk_n + anim;
1087         break;
1088
1089       default:
1090         result = FALSE;
1091     }
1092   }
1093
1094   return result;
1095 }
1096
1097 static void check_player(struct PLAYER *ply)
1098 {
1099   int oldx = ply->x;
1100   int oldy = ply->y;
1101   int x = oldx;
1102   int y = oldy;
1103   int dx = 0, dy = 0;
1104
1105   game_em.last_player_direction[ply->num] = MV_NONE;
1106
1107   if (ply->joy_w)               /* west */
1108   {
1109     x--;
1110     dx = -1;
1111   }
1112   else if (ply->joy_e)          /* east */
1113   {
1114     x++;
1115     dx = 1;
1116   }
1117
1118   if (ply->joy_n)               /* north */
1119   {
1120     y--;
1121     dy = -1;
1122   }
1123   else if (ply->joy_s)          /* south */
1124   {
1125     y++;
1126     dy = 1;
1127   }
1128
1129   if (dx || dy)
1130   {
1131     int oldx = ply->x;
1132     int oldy = ply->y;
1133     int x = oldx + dx;
1134     int y = oldy + dy;
1135     boolean players_visible_before_move;
1136     boolean players_visible_after_move;
1137     boolean can_move;
1138
1139     players_visible_before_move = checkIfAllPlayersFitToScreen();
1140
1141     ply->x = x;
1142     ply->y = y;
1143
1144     players_visible_after_move = checkIfAllPlayersFitToScreen();
1145
1146     /*
1147       player is allowed to move only in the following cases:
1148       - it is not needed to display all players (not focussed to all players)
1149       - all players are (still or again) visible after the move
1150       - some players were already outside visible screen area before the move
1151     */
1152     can_move = (game.centered_player_nr != -1 ||
1153                 players_visible_after_move ||
1154                 !players_visible_before_move);
1155
1156     ply->x = oldx;
1157     ply->y = oldy;
1158
1159     if (!can_move)
1160     {
1161       ply->joy_n = ply->joy_e = ply->joy_s = ply->joy_w = 0;
1162
1163       return;
1164     }
1165   }
1166
1167   if (dx == 0 && dy == 0)
1168   {
1169     ply->joy_stick = 0;
1170
1171     if (ply->joy_drop)
1172     {
1173       if (++ply->dynamite_cnt == 5 && ply->dynamite)
1174       {
1175         Cave[x][y] = Xdynamite_1;
1176         play_element_sound(x, y, SOUND_dynamite, Xdynamite_1);
1177         ply->dynamite--;
1178       }
1179     }
1180     else
1181     {
1182       ply->dynamite_cnt = 0;
1183     }
1184
1185     RandomEM += 7;      /* be a bit more random if the player doesn't move */
1186
1187     return;
1188   }
1189
1190   ply->joy_stick = 1;
1191   ply->joy_n = ply->joy_e = ply->joy_s = ply->joy_w = 0;
1192   ply->dynamite_cnt = 0;        /* reset dynamite timer if we move */
1193   ply->joy_spin = !ply->joy_spin;
1194
1195   if (ply->joy_snap == 0)               /* player wants to move */
1196   {
1197     boolean moved = FALSE;
1198
1199     if (ply->last_move_dir & MV_HORIZONTAL)
1200     {
1201       if (!(moved = player_digfield(ply, 0, dy)))
1202         moved = player_digfield(ply, dx, 0);
1203     }
1204     else
1205     {
1206       if (!(moved = player_digfield(ply, dx, 0)))
1207         moved = player_digfield(ply, 0, dy);
1208     }
1209
1210     if (moved)
1211     {
1212       if (oldx != ply->x)
1213         ply->last_move_dir = (dx < 0 ? MV_LEFT : MV_RIGHT);
1214       else if (oldy != ply->y)
1215         ply->last_move_dir = (dy < 0 ? MV_UP : MV_DOWN);
1216
1217       game_em.any_player_moving = TRUE;
1218       game_em.last_moving_player = ply->num;
1219       game_em.last_player_direction[ply->num] = ply->last_move_dir;
1220     }
1221   }
1222   else                                  /* player wants to snap */
1223   {
1224     game_em.any_player_snapping = player_digfield(ply, dx, dy);
1225   }
1226 }
1227
1228 static void set_nearest_player_xy(int x, int y, int *dx, int *dy)
1229 {
1230   int distance, distance_shortest = EM_MAX_CAVE_WIDTH + EM_MAX_CAVE_HEIGHT;
1231   int i;
1232
1233   /* default values if no players are alive anymore */
1234   *dx = 0;
1235   *dy = 0;
1236
1237   for (i = 0; i < MAX_PLAYERS; i++)
1238   {
1239     if (!ply[i].alive)
1240       continue;
1241
1242     distance = ABS(ply[i].x - x) + ABS(ply[i].y - y);
1243
1244     if (distance < distance_shortest)
1245     {
1246       *dx = ply[i].x;
1247       *dy = ply[i].y;
1248
1249       distance_shortest = distance;
1250     }
1251   }
1252 }
1253
1254 static void Lacid_1(int x, int y)
1255 {
1256   Next[x][y] = Xacid_2;
1257 }
1258
1259 static void Lacid_2(int x, int y)
1260 {
1261   Next[x][y] = Xacid_3;
1262 }
1263
1264 static void Lacid_3(int x, int y)
1265 {
1266   Next[x][y] = Xacid_4;
1267 }
1268
1269 static void Lacid_4(int x, int y)
1270 {
1271   Next[x][y] = Xacid_5;
1272 }
1273
1274 static void Lacid_5(int x, int y)
1275 {
1276   Next[x][y] = Xacid_6;
1277 }
1278
1279 static void Lacid_6(int x, int y)
1280 {
1281   Next[x][y] = Xacid_7;
1282 }
1283
1284 static void Lacid_7(int x, int y)
1285 {
1286   Next[x][y] = Xacid_8;
1287 }
1288
1289 static void Lacid_8(int x, int y)
1290 {
1291   Next[x][y] = Xacid_1;
1292 }
1293
1294 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
1295 static void Lfake_acid_1(int x, int y)
1296 {
1297   Next[x][y] = Xfake_acid_2;
1298 }
1299
1300 static void Lfake_acid_2(int x, int y)
1301 {
1302   Next[x][y] = Xfake_acid_3;
1303 }
1304
1305 static void Lfake_acid_3(int x, int y)
1306 {
1307   Next[x][y] = Xfake_acid_4;
1308 }
1309
1310 static void Lfake_acid_4(int x, int y)
1311 {
1312   Next[x][y] = Xfake_acid_5;
1313 }
1314
1315 static void Lfake_acid_5(int x, int y)
1316 {
1317   Next[x][y] = Xfake_acid_6;
1318 }
1319
1320 static void Lfake_acid_6(int x, int y)
1321 {
1322   Next[x][y] = Xfake_acid_7;
1323 }
1324
1325 static void Lfake_acid_7(int x, int y)
1326 {
1327   Next[x][y] = Xfake_acid_8;
1328 }
1329
1330 static void Lfake_acid_8(int x, int y)
1331 {
1332   Next[x][y] = Xfake_acid_1;
1333 }
1334 #endif
1335
1336 static void Landroid(int x, int y)
1337 {
1338   int dx, dy, temp;
1339
1340   if (lev.android_clone_cnt == 0)
1341   {
1342     if (Cave[x-1][y-1] != Xblank &&
1343         Cave[x][y-1]   != Xblank &&
1344         Cave[x+1][y-1] != Xblank &&
1345         Cave[x-1][y]   != Xblank &&
1346         Cave[x+1][y]   != Xblank &&
1347         Cave[x-1][y+1] != Xblank &&
1348         Cave[x][y+1]   != Xblank &&
1349         Cave[x+1][y+1] != Xblank)
1350       goto android_move;
1351
1352     switch (RANDOM(8))
1353     {
1354       /* randomly find an object to clone */
1355
1356       case 0: /* S,NE,W,NW,SE,E,SW,N */
1357         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1358         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1359         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1360         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1361         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1362         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1363         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1364         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1365         goto android_move;
1366
1367       case 1: /* NW,SE,N,S,NE,SW,E,W */
1368         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1369         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1370         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1371         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1372         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1373         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1374         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1375         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1376         goto android_move;
1377
1378       case 2: /* SW,E,S,W,N,NW,SE,NE */
1379         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1380         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1381         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1382         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1383         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1384         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1385         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1386         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1387         goto android_move;
1388
1389       case 3: /* N,SE,NE,E,W,S,NW,SW */
1390         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1391         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1392         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1393         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1394         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1395         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1396         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1397         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1398         goto android_move;
1399
1400       case 4: /* SE,NW,E,NE,SW,W,N,S */
1401         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1402         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1403         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1404         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1405         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1406         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1407         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1408         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1409         goto android_move;
1410
1411       case 5: /* NE,W,SE,SW,S,N,E,NW */
1412         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1413         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1414         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1415         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1416         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1417         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1418         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1419         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1420         goto android_move;
1421
1422       case 6: /* E,N,SW,S,NW,NE,SE,W */
1423         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1424         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1425         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1426         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1427         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1428         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1429         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1430         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1431         goto android_move;
1432
1433       case 7: /* W,SW,NW,N,E,SE,NE,S */
1434         temp = lev.android_array[Cave[x-1][y]];   if (temp != Xblank) break;
1435         temp = lev.android_array[Cave[x-1][y+1]]; if (temp != Xblank) break;
1436         temp = lev.android_array[Cave[x-1][y-1]]; if (temp != Xblank) break;
1437         temp = lev.android_array[Cave[x][y-1]];   if (temp != Xblank) break;
1438         temp = lev.android_array[Cave[x+1][y]];   if (temp != Xblank) break;
1439         temp = lev.android_array[Cave[x+1][y+1]]; if (temp != Xblank) break;
1440         temp = lev.android_array[Cave[x+1][y-1]]; if (temp != Xblank) break;
1441         temp = lev.android_array[Cave[x][y+1]];   if (temp != Xblank) break;
1442         goto android_move;
1443     }
1444
1445     Next[x][y] = temp;  /* the item we chose to clone */
1446     play_element_sound(x, y, SOUND_android_clone, temp);
1447
1448     switch (RANDOM(8))
1449     {
1450       /* randomly find a direction to move */
1451
1452       case 0: /* S,NE,W,NW,SE,E,SW,N */
1453         if (Cave[x][y+1] == Xblank)   goto android_s;
1454         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1455         if (Cave[x-1][y] == Xblank)   goto android_w;
1456         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1457         if (Cave[x+1][y+1] == Xblank) goto android_se;
1458         if (Cave[x+1][y] == Xblank)   goto android_e;
1459         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1460         if (Cave[x][y-1] == Xblank)   goto android_n;
1461         goto android_move;
1462
1463       case 1: /* NW,SE,N,S,NE,SW,E,W */
1464         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1465         if (Cave[x+1][y+1] == Xblank) goto android_se;
1466         if (Cave[x][y-1] == Xblank)   goto android_n;
1467         if (Cave[x][y+1] == Xblank)   goto android_s;
1468         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1469         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1470         if (Cave[x+1][y] == Xblank)   goto android_e;
1471         if (Cave[x-1][y] == Xblank)   goto android_w;
1472         goto android_move;
1473
1474       case 2: /* SW,E,S,W,N,NW,SE,NE */
1475         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1476         if (Cave[x+1][y] == Xblank)   goto android_e;
1477         if (Cave[x][y+1] == Xblank)   goto android_s;
1478         if (Cave[x-1][y] == Xblank)   goto android_w;
1479         if (Cave[x][y-1] == Xblank)   goto android_n;
1480         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1481         if (Cave[x+1][y+1] == Xblank) goto android_se;
1482         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1483         goto android_move;
1484
1485       case 3: /* N,SE,NE,E,W,S,NW,SW */
1486         if (Cave[x][y-1] == Xblank)   goto android_n;
1487         if (Cave[x+1][y+1] == Xblank) goto android_se;
1488         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1489         if (Cave[x+1][y] == Xblank)   goto android_e;
1490         if (Cave[x-1][y] == Xblank)   goto android_w;
1491         if (Cave[x][y+1] == Xblank)   goto android_s;
1492         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1493         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1494         goto android_move;
1495
1496       case 4: /* SE,NW,E,NE,SW,W,N,S */
1497         if (Cave[x+1][y+1] == Xblank) goto android_se;
1498         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1499         if (Cave[x+1][y] == Xblank)   goto android_e;
1500         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1501         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1502         if (Cave[x-1][y] == Xblank)   goto android_w;
1503         if (Cave[x][y-1] == Xblank)   goto android_n;
1504         if (Cave[x][y+1] == Xblank)   goto android_s;
1505         goto android_move;
1506
1507       case 5: /* NE,W,SE,SW,S,N,E,NW */
1508         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1509         if (Cave[x-1][y] == Xblank)   goto android_w;
1510         if (Cave[x+1][y+1] == Xblank) goto android_se;
1511         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1512         if (Cave[x][y+1] == Xblank)   goto android_s;
1513         if (Cave[x][y-1] == Xblank)   goto android_n;
1514         if (Cave[x+1][y] == Xblank)   goto android_e;
1515         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1516         goto android_move;
1517
1518       case 6: /* E,N,SW,S,NW,NE,SE,W */
1519         if (Cave[x+1][y] == Xblank)   goto android_e;
1520         if (Cave[x][y-1] == Xblank)   goto android_n;
1521         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1522         if (Cave[x][y+1] == Xblank)   goto android_s;
1523         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1524         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1525         if (Cave[x+1][y+1] == Xblank) goto android_se;
1526         if (Cave[x-1][y] == Xblank)   goto android_w;
1527         goto android_move;
1528
1529       case 7: /* W,SW,NW,N,E,SE,NE,S */
1530         if (Cave[x-1][y] == Xblank)   goto android_w;
1531         if (Cave[x-1][y+1] == Xblank) goto android_sw;
1532         if (Cave[x-1][y-1] == Xblank) goto android_nw;
1533         if (Cave[x][y-1] == Xblank)   goto android_n;
1534         if (Cave[x+1][y] == Xblank)   goto android_e;
1535         if (Cave[x+1][y+1] == Xblank) goto android_se;
1536         if (Cave[x+1][y-1] == Xblank) goto android_ne;
1537         if (Cave[x][y+1] == Xblank)   goto android_s;
1538         goto android_move;
1539     }
1540   }
1541
1542  android_move:
1543   if (lev.android_move_cnt == 0)
1544   {
1545     if (Cave[x-1][y-1] == Zplayer ||
1546         Cave[x][y-1]   == Zplayer ||
1547         Cave[x+1][y-1] == Zplayer ||
1548         Cave[x-1][y]   == Zplayer ||
1549         Cave[x+1][y]   == Zplayer ||
1550         Cave[x-1][y+1] == Zplayer ||
1551         Cave[x][y+1]   == Zplayer ||
1552         Cave[x+1][y+1] == Zplayer)
1553       goto android_still;
1554
1555     set_nearest_player_xy(x, y, &dx, &dy);
1556
1557     Next[x][y] = Xblank;        /* assume we will move */
1558     temp = ((x < dx) + 1 - (x > dx)) + ((y < dy) + 1 - (y > dy)) * 3;
1559
1560     if (RANDOM(2))
1561     {
1562       switch (temp)
1563       {
1564         /* attempt clockwise move first if direct path is blocked */
1565
1566         case 0: /* north west */
1567           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1568           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1569           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1570           break;
1571
1572         case 1: /* north */
1573           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1574           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1575           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1576           break;
1577
1578         case 2: /* north east */
1579           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1580           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1581           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1582           break;
1583
1584         case 3: /* west */
1585           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1586           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1587           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1588           break;
1589
1590         case 4: /* nowhere */
1591           break;
1592
1593         case 5: /* east */
1594           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1595           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1596           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1597           break;
1598
1599         case 6: /* south west */
1600           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1601           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1602           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1603           break;
1604
1605         case 7: /* south */
1606           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1607           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1608           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1609           break;
1610
1611         case 8: /* south east */
1612           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1613           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1614           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1615           break;
1616       }
1617     }
1618     else
1619     {
1620       switch (temp)
1621       {
1622         /* attempt counterclockwise move first if direct path is blocked */
1623
1624         case 0: /* north west */
1625           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1626           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1627           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1628           break;
1629
1630         case 1: /* north */
1631           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1632           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1633           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1634           break;
1635
1636         case 2: /* north east */
1637           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1638           if (tab_android_move[Cave[x][y-1]])   goto android_n;
1639           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1640           break;
1641
1642         case 3: /* west */
1643           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1644           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1645           if (tab_android_move[Cave[x-1][y-1]]) goto android_nw;
1646           break;
1647
1648         case 4: /* nowhere */
1649           break;
1650
1651         case 5: /* east */
1652           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1653           if (tab_android_move[Cave[x+1][y-1]]) goto android_ne;
1654           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1655           break;
1656
1657         case 6: /* south west */
1658           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1659           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1660           if (tab_android_move[Cave[x-1][y]])   goto android_w;
1661           break;
1662
1663         case 7: /* south */
1664           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1665           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1666           if (tab_android_move[Cave[x-1][y+1]]) goto android_sw;
1667           break;
1668
1669         case 8: /* south east */
1670           if (tab_android_move[Cave[x+1][y+1]]) goto android_se;
1671           if (tab_android_move[Cave[x+1][y]])   goto android_e;
1672           if (tab_android_move[Cave[x][y+1]])   goto android_s;
1673           break;
1674       }
1675     }
1676   }
1677
1678  android_still:
1679   Next[x][y] = Xandroid;
1680   return;
1681
1682  android_n:
1683   Cave[x][y] = Yandroid_nB;
1684   Cave[x][y-1] = Yandroid_n;
1685   Next[x][y-1] = Xandroid;
1686   play_element_sound(x, y, SOUND_android_move, Xandroid);
1687   return;
1688
1689  android_ne:
1690   Cave[x][y] = Yandroid_neB;
1691   Cave[x+1][y-1] = Yandroid_ne;
1692   Next[x+1][y-1] = Xandroid;
1693   play_element_sound(x, y, SOUND_android_move, Xandroid);
1694   return;
1695
1696  android_e:
1697   Cave[x][y] = Yandroid_eB;
1698   Cave[x+1][y] = Yandroid_e;
1699   Next[x+1][y] = Xandroid;
1700   play_element_sound(x, y, SOUND_android_move, Xandroid);
1701   return;
1702
1703  android_se:
1704   Cave[x][y] = Yandroid_seB;
1705   Cave[x+1][y+1] = Yandroid_se;
1706   Next[x+1][y+1] = Xandroid;
1707   play_element_sound(x, y, SOUND_android_move, Xandroid);
1708   return;
1709
1710  android_s:
1711   Cave[x][y] = Yandroid_sB;
1712   Cave[x][y+1] = Yandroid_s;
1713   Next[x][y+1] = Xandroid;
1714   play_element_sound(x, y, SOUND_android_move, Xandroid);
1715   return;
1716
1717  android_sw:
1718   Cave[x][y] = Yandroid_swB;
1719   Cave[x-1][y+1] = Yandroid_sw;
1720   Next[x-1][y+1] = Xandroid;
1721   play_element_sound(x, y, SOUND_android_move, Xandroid);
1722   return;
1723
1724  android_w:
1725   Cave[x][y] = Yandroid_wB;
1726   Cave[x-1][y] = Yandroid_w;
1727   Next[x-1][y] = Xandroid;
1728   play_element_sound(x, y, SOUND_android_move, Xandroid);
1729   return;
1730
1731  android_nw:
1732   Cave[x][y] = Yandroid_nwB;
1733   Cave[x-1][y-1] = Yandroid_nw;
1734   Next[x-1][y-1] = Xandroid;
1735   play_element_sound(x, y, SOUND_android_move, Xandroid);
1736   return;
1737 }
1738
1739 static void Landroid_1_n(int x, int y)
1740 {
1741   switch (Cave[x][y-1])
1742   {
1743     case Xblank:
1744     case Xacid_splash_e:
1745     case Xacid_splash_w:
1746       Cave[x][y] = Yandroid_nB;
1747       Next[x][y] = Xblank;
1748       Cave[x][y-1] = Yandroid_n;
1749       Next[x][y-1] = Xandroid;
1750       play_element_sound(x, y, SOUND_android_move, Xandroid_1_n);
1751       return;
1752
1753     case Xacid_1:
1754     case Xacid_2:
1755     case Xacid_3:
1756     case Xacid_4:
1757     case Xacid_5:
1758     case Xacid_6:
1759     case Xacid_7:
1760     case Xacid_8:
1761       Cave[x][y] = Yandroid_nB;
1762       Next[x][y] = Xblank;
1763       if (Cave[x+1][y-2] == Xblank)
1764         Cave[x+1][y-2] = Xacid_splash_e;
1765       if (Cave[x-1][y-2] == Xblank)
1766         Cave[x-1][y-2] = Xacid_splash_w;
1767       play_element_sound(x, y, SOUND_acid, Xacid_1);
1768       return;
1769
1770     default:
1771       Landroid(x, y);
1772       return;
1773   }
1774 }
1775
1776 static void Landroid_2_n(int x, int y)
1777 {
1778   switch (Cave[x][y-1])
1779   {
1780     case Xblank:
1781     case Xacid_splash_e:
1782     case Xacid_splash_w:
1783       Cave[x][y] = Yandroid_nB;
1784       Next[x][y] = Xblank;
1785       Cave[x][y-1] = Yandroid_n;
1786       Next[x][y-1] = Xandroid_1_n;
1787       play_element_sound(x, y, SOUND_android_move, Xandroid_2_n);
1788       return;
1789
1790     case Xacid_1:
1791     case Xacid_2:
1792     case Xacid_3:
1793     case Xacid_4:
1794     case Xacid_5:
1795     case Xacid_6:
1796     case Xacid_7:
1797     case Xacid_8:
1798       Cave[x][y] = Yandroid_nB;
1799       Next[x][y] = Xblank;
1800       if (Cave[x+1][y-2] == Xblank)
1801         Cave[x+1][y-2] = Xacid_splash_e;
1802       if (Cave[x-1][y-2] == Xblank)
1803         Cave[x-1][y-2] = Xacid_splash_w;
1804       play_element_sound(x, y, SOUND_acid, Xacid_1);
1805       return;
1806
1807     default:
1808       Landroid(x, y);
1809       return;
1810   }
1811 }
1812
1813 static void Landroid_1_e(int x, int y)
1814 {
1815   switch (Cave[x+1][y])
1816   {
1817     case Xblank:
1818     case Xacid_splash_e:
1819     case Xacid_splash_w:
1820       Cave[x][y] = Yandroid_eB;
1821       Next[x][y] = Xblank;
1822       Cave[x+1][y] = Yandroid_e;
1823       Next[x+1][y] = Xandroid;
1824       play_element_sound(x, y, SOUND_android_move, Xandroid_1_e);
1825       return;
1826
1827     case Xacid_1:
1828     case Xacid_2:
1829     case Xacid_3:
1830     case Xacid_4:
1831     case Xacid_5:
1832     case Xacid_6:
1833     case Xacid_7:
1834     case Xacid_8:
1835       Cave[x][y] = Yandroid_eB;
1836       Next[x][y] = Xblank;
1837       if (Cave[x+2][y-1] == Xblank)
1838         Cave[x+2][y-1] = Xacid_splash_e;
1839       if (Cave[x][y-1] == Xblank)
1840         Cave[x][y-1] = Xacid_splash_w;
1841       play_element_sound(x, y, SOUND_acid, Xacid_1);
1842       return;
1843
1844     default:
1845       Landroid(x, y);
1846       return;
1847   }
1848 }
1849
1850 static void Landroid_2_e(int x, int y)
1851 {
1852   switch (Cave[x+1][y])
1853   {
1854     case Xblank:
1855     case Xacid_splash_e:
1856     case Xacid_splash_w:
1857       Cave[x][y] = Yandroid_eB;
1858       Next[x][y] = Xblank;
1859       Cave[x+1][y] = Yandroid_e;
1860       Next[x+1][y] = Xandroid_1_e;
1861       play_element_sound(x, y, SOUND_android_move, Xandroid_2_e);
1862       return;
1863
1864     case Xacid_1:
1865     case Xacid_2:
1866     case Xacid_3:
1867     case Xacid_4:
1868     case Xacid_5:
1869     case Xacid_6:
1870     case Xacid_7:
1871     case Xacid_8:
1872       Cave[x][y] = Yandroid_eB;
1873       Next[x][y] = Xblank;
1874       if (Cave[x+2][y-1] == Xblank)
1875         Cave[x+2][y-1] = Xacid_splash_e;
1876       if (Cave[x][y-1] == Xblank)
1877         Cave[x][y-1] = Xacid_splash_w;
1878       play_element_sound(x, y, SOUND_acid, Xacid_1);
1879       return;
1880
1881     default:
1882       Landroid(x, y);
1883       return;
1884   }
1885 }
1886
1887 static void Landroid_1_s(int x, int y)
1888 {
1889   switch (Cave[x][y+1])
1890   {
1891     case Xblank:
1892     case Xacid_splash_e:
1893     case Xacid_splash_w:
1894       Cave[x][y] = Yandroid_sB;
1895       Next[x][y] = Xblank;
1896       Cave[x][y+1] = Yandroid_s;
1897       Next[x][y+1] = Xandroid;
1898       play_element_sound(x, y, SOUND_android_move, Xandroid_1_s);
1899       return;
1900
1901     case Xacid_1:
1902     case Xacid_2:
1903     case Xacid_3:
1904     case Xacid_4:
1905     case Xacid_5:
1906     case Xacid_6:
1907     case Xacid_7:
1908     case Xacid_8:
1909       Cave[x][y] = Yandroid_sB;
1910       Next[x][y] = Xblank;
1911       if (Cave[x+1][y] == Xblank)
1912         Cave[x+1][y] = Xacid_splash_e;
1913       if (Cave[x-1][y] == Xblank)
1914         Cave[x-1][y] = Xacid_splash_w;
1915       play_element_sound(x, y, SOUND_acid, Xacid_1);
1916       return;
1917
1918     default:
1919       Landroid(x, y);
1920       return;
1921   }
1922 }
1923
1924 static void Landroid_2_s(int x, int y)
1925 {
1926   switch (Cave[x][y+1])
1927   {
1928     case Xblank:
1929     case Xacid_splash_e:
1930     case Xacid_splash_w:
1931       Cave[x][y] = Yandroid_sB;
1932       Next[x][y] = Xblank;
1933       Cave[x][y+1] = Yandroid_s;
1934       Next[x][y+1] = Xandroid_1_s;
1935       play_element_sound(x, y, SOUND_android_move, Xandroid_2_s);
1936       return;
1937
1938     case Xacid_1:
1939     case Xacid_2:
1940     case Xacid_3:
1941     case Xacid_4:
1942     case Xacid_5:
1943     case Xacid_6:
1944     case Xacid_7:
1945     case Xacid_8:
1946       Cave[x][y] = Yandroid_sB;
1947       Next[x][y] = Xblank;
1948       if (Cave[x+1][y] == Xblank)
1949         Cave[x+1][y] = Xacid_splash_e;
1950       if (Cave[x-1][y] == Xblank)
1951         Cave[x-1][y] = Xacid_splash_w;
1952       play_element_sound(x, y, SOUND_acid, Xacid_1);
1953       return;
1954
1955     default:
1956       Landroid(x, y);
1957       return;
1958   }
1959 }
1960
1961 static void Landroid_1_w(int x, int y)
1962 {
1963   switch (Cave[x-1][y])
1964   {
1965     case Xblank:
1966     case Xacid_splash_e:
1967     case Xacid_splash_w:
1968       Cave[x][y] = Yandroid_wB;
1969       Next[x][y] = Xblank;
1970       Cave[x-1][y] = Yandroid_w;
1971       Next[x-1][y] = Xandroid;
1972       play_element_sound(x, y, SOUND_android_move, Xandroid_1_w);
1973       return;
1974
1975     case Xacid_1:
1976     case Xacid_2:
1977     case Xacid_3:
1978     case Xacid_4:
1979     case Xacid_5:
1980     case Xacid_6:
1981     case Xacid_7:
1982     case Xacid_8:
1983       Cave[x][y] = Yandroid_wB;
1984       Next[x][y] = Xblank;
1985       if (Cave[x][y-1] == Xblank)
1986         Cave[x][y-1] = Xacid_splash_e;
1987       if (Cave[x-2][y-1] == Xblank)
1988         Cave[x-2][y-1] = Xacid_splash_w;
1989       play_element_sound(x, y, SOUND_acid, Xacid_1);
1990       return;
1991
1992     default:
1993       Landroid(x, y);
1994       return;
1995   }
1996 }
1997
1998 static void Landroid_2_w(int x, int y)
1999 {
2000   switch (Cave[x-1][y])
2001   {
2002     case Xblank:
2003     case Xacid_splash_e:
2004     case Xacid_splash_w:
2005       Cave[x][y] = Yandroid_wB;
2006       Next[x][y] = Xblank;
2007       Cave[x-1][y] = Yandroid_w;
2008       Next[x-1][y] = Xandroid_1_w;
2009       play_element_sound(x, y, SOUND_android_move, Xandroid_1_w);
2010       return;
2011
2012     case Xacid_1:
2013     case Xacid_2:
2014     case Xacid_3:
2015     case Xacid_4:
2016     case Xacid_5:
2017     case Xacid_6:
2018     case Xacid_7:
2019     case Xacid_8:
2020       Cave[x][y] = Yandroid_wB;
2021       Next[x][y] = Xblank;
2022       if (Cave[x][y-1] == Xblank)
2023         Cave[x][y-1] = Xacid_splash_e;
2024       if (Cave[x-2][y-1] == Xblank)
2025         Cave[x-2][y-1] = Xacid_splash_w;
2026       play_element_sound(x, y, SOUND_acid, Xacid_1);
2027       return;
2028
2029     default:
2030       Landroid(x, y);
2031       return;
2032   }
2033 }
2034
2035 static void Leater_n(int x, int y)
2036 {
2037   if (Cave[x+1][y] == Xdiamond)
2038   {
2039     Cave[x+1][y] = Ydiamond_blank;
2040     Next[x+1][y] = Xblank;
2041     play_element_sound(x, y, SOUND_eater_eat, Xeater_n);
2042     return;
2043   }
2044
2045   if (Cave[x][y+1] == Xdiamond)
2046   {
2047     Cave[x][y+1] = Ydiamond_blank;
2048     Next[x][y+1] = Xblank;
2049     play_element_sound(x, y, SOUND_eater_eat, Xeater_n);
2050     return;
2051   }
2052
2053   if (Cave[x-1][y] == Xdiamond)
2054   {
2055     Cave[x-1][y] = Ydiamond_blank;
2056     Next[x-1][y] = Xblank;
2057     play_element_sound(x, y, SOUND_eater_eat, Xeater_n);
2058     return;
2059   }
2060
2061   if (Cave[x][y-1] == Xdiamond)
2062   {
2063     Cave[x][y-1] = Ydiamond_blank;
2064     Next[x][y-1] = Xblank;
2065     play_element_sound(x, y, SOUND_eater_eat, Xeater_n);
2066     return;
2067   }
2068
2069   switch (Cave[x][y-1])
2070   {
2071     case Xblank:
2072     case Xacid_splash_e:
2073     case Xacid_splash_w:
2074     case Xplant:
2075     case Yplant:
2076     case Zplayer:
2077       Cave[x][y] = Yeater_nB;
2078       Next[x][y] = Xblank;
2079       Cave[x][y-1] = Yeater_n;
2080       Next[x][y-1] = Xeater_n;
2081       return;
2082
2083     case Xacid_1:
2084     case Xacid_2:
2085     case Xacid_3:
2086     case Xacid_4:
2087     case Xacid_5:
2088     case Xacid_6:
2089     case Xacid_7:
2090     case Xacid_8:
2091       Cave[x][y] = Yeater_nB;
2092       Next[x][y] = Xblank;
2093       if (Cave[x+1][y-2] == Xblank)
2094         Cave[x+1][y-2] = Xacid_splash_e;
2095       if (Cave[x-1][y-2] == Xblank)
2096         Cave[x-1][y-2] = Xacid_splash_w;
2097       play_element_sound(x, y, SOUND_acid, Xacid_1);
2098       return;
2099
2100     default:
2101       Next[x][y] = RANDOM(2) ? Xeater_e : Xeater_w;
2102       play_element_sound(x, y, SOUND_eater, Xeater_n);
2103       return;
2104   }
2105 }
2106
2107 static void Leater_e(int x, int y)
2108 {
2109   if (Cave[x][y+1] == Xdiamond)
2110   {
2111     Cave[x][y+1] = Ydiamond_blank;
2112     Next[x][y+1] = Xblank;
2113     play_element_sound(x, y, SOUND_eater_eat, Xeater_e);
2114     return;
2115   }
2116
2117   if (Cave[x-1][y] == Xdiamond)
2118   {
2119     Cave[x-1][y] = Ydiamond_blank;
2120     Next[x-1][y] = Xblank;
2121     play_element_sound(x, y, SOUND_eater_eat, Xeater_e);
2122     return;
2123   }
2124
2125   if (Cave[x][y-1] == Xdiamond)
2126   {
2127     Cave[x][y-1] = Ydiamond_blank;
2128     Next[x][y-1] = Xblank;
2129     play_element_sound(x, y, SOUND_eater_eat, Xeater_e);
2130     return;
2131   }
2132
2133   if (Cave[x+1][y] == Xdiamond)
2134   {
2135     Cave[x+1][y] = Ydiamond_blank;
2136     Next[x+1][y] = Xblank;
2137     play_element_sound(x, y, SOUND_eater_eat, Xeater_e);
2138     return;
2139   }
2140
2141   switch (Cave[x+1][y])
2142   {
2143     case Xblank:
2144     case Xacid_splash_e:
2145     case Xacid_splash_w:
2146     case Xplant:
2147     case Yplant:
2148     case Zplayer:
2149       Cave[x][y] = Yeater_eB;
2150       Next[x][y] = Xblank;
2151       Cave[x+1][y] = Yeater_e;
2152       Next[x+1][y] = Xeater_e;
2153       return;
2154
2155     case Xacid_1:
2156     case Xacid_2:
2157     case Xacid_3:
2158     case Xacid_4:
2159     case Xacid_5:
2160     case Xacid_6:
2161     case Xacid_7:
2162     case Xacid_8:
2163       Cave[x][y] = Yeater_eB;
2164       Next[x][y] = Xblank;
2165       if (Cave[x+2][y-1] == Xblank)
2166         Cave[x+2][y-1] = Xacid_splash_e;
2167       if (Cave[x][y-1] == Xblank)
2168         Cave[x][y-1] = Xacid_splash_w;
2169       play_element_sound(x, y, SOUND_acid, Xacid_1);
2170       return;
2171
2172     default:
2173       Next[x][y] = RANDOM(2) ? Xeater_n : Xeater_s;
2174       play_element_sound(x, y, SOUND_eater, Xeater_e);
2175       return;
2176   }
2177 }
2178
2179 static void Leater_s(int x, int y)
2180 {
2181   if (Cave[x-1][y] == Xdiamond)
2182   {
2183     Cave[x-1][y] = Ydiamond_blank;
2184     Next[x-1][y] = Xblank;
2185     play_element_sound(x, y, SOUND_eater_eat, Xeater_s);
2186     return;
2187   }
2188
2189   if (Cave[x][y-1] == Xdiamond)
2190   {
2191     Cave[x][y-1] = Ydiamond_blank;
2192     Next[x][y-1] = Xblank;
2193     play_element_sound(x, y, SOUND_eater_eat, Xeater_s);
2194     return;
2195   }
2196
2197   if (Cave[x+1][y] == Xdiamond)
2198   {
2199     Cave[x+1][y] = Ydiamond_blank;
2200     Next[x+1][y] = Xblank;
2201     play_element_sound(x, y, SOUND_eater_eat, Xeater_s);
2202     return;
2203   }
2204
2205   if (Cave[x][y+1] == Xdiamond)
2206   {
2207     Cave[x][y+1] = Ydiamond_blank;
2208     Next[x][y+1] = Xblank;
2209     play_element_sound(x, y, SOUND_eater_eat, Xeater_s);
2210     return;
2211   }
2212
2213   switch (Cave[x][y+1])
2214   {
2215     case Xblank:
2216     case Xacid_splash_e:
2217     case Xacid_splash_w:
2218     case Xplant:
2219     case Yplant:
2220     case Zplayer:
2221       Cave[x][y] = Yeater_sB;
2222       Next[x][y] = Xblank;
2223       Cave[x][y+1] = Yeater_s;
2224       Next[x][y+1] = Xeater_s;
2225       return;
2226
2227     case Xacid_1:
2228     case Xacid_2:
2229     case Xacid_3:
2230     case Xacid_4:
2231     case Xacid_5:
2232     case Xacid_6:
2233     case Xacid_7:
2234     case Xacid_8:
2235       Cave[x][y] = Yeater_sB;
2236       Next[x][y] = Xblank;
2237       if (Cave[x+1][y] == Xblank)
2238         Cave[x+1][y] = Xacid_splash_e;
2239       if (Cave[x-1][y] == Xblank)
2240         Cave[x-1][y] = Xacid_splash_w;
2241       play_element_sound(x, y, SOUND_acid, Xacid_1);
2242       return;
2243
2244     default:
2245       Next[x][y] = RANDOM(2) ? Xeater_e : Xeater_w;
2246       play_element_sound(x, y, SOUND_eater, Xeater_s);
2247       return;
2248   }
2249 }
2250
2251 static void Leater_w(int x, int y)
2252 {
2253   if (Cave[x][y-1] == Xdiamond)
2254   {
2255     Cave[x][y-1] = Ydiamond_blank;
2256     Next[x][y-1] = Xblank;
2257     play_element_sound(x, y, SOUND_eater_eat, Xeater_w);
2258     return;
2259   }
2260
2261   if (Cave[x+1][y] == Xdiamond)
2262   {
2263     Cave[x+1][y] = Ydiamond_blank;
2264     Next[x+1][y] = Xblank;
2265     play_element_sound(x, y, SOUND_eater_eat, Xeater_w);
2266     return;
2267   }
2268
2269   if (Cave[x][y+1] == Xdiamond)
2270   {
2271     Cave[x][y+1] = Ydiamond_blank;
2272     Next[x][y+1] = Xblank;
2273     play_element_sound(x, y, SOUND_eater_eat, Xeater_w);
2274     return;
2275   }
2276
2277   if (Cave[x-1][y] == Xdiamond)
2278   {
2279     Cave[x-1][y] = Ydiamond_blank;
2280     Next[x-1][y] = Xblank;
2281     play_element_sound(x, y, SOUND_eater_eat, Xeater_w);
2282     return;
2283   }
2284
2285   switch (Cave[x-1][y])
2286   {
2287     case Xblank:
2288     case Xacid_splash_e:
2289     case Xacid_splash_w:
2290     case Xplant:
2291     case Yplant:
2292     case Zplayer:
2293       Cave[x][y] = Yeater_wB;
2294       Next[x][y] = Xblank;
2295       Cave[x-1][y] = Yeater_w;
2296       Next[x-1][y] = Xeater_w;
2297       return;
2298
2299     case Xacid_1:
2300     case Xacid_2:
2301     case Xacid_3:
2302     case Xacid_4:
2303     case Xacid_5:
2304     case Xacid_6:
2305     case Xacid_7:
2306     case Xacid_8:
2307       Cave[x][y] = Yeater_wB;
2308       Next[x][y] = Xblank;
2309       if (Cave[x][y-1] == Xblank)
2310         Cave[x][y-1] = Xacid_splash_e;
2311       if (Cave[x-2][y-1] == Xblank)
2312         Cave[x-2][y-1] = Xacid_splash_w;
2313       play_element_sound(x, y, SOUND_acid, Xacid_1);
2314       return;
2315
2316     default:
2317       Next[x][y] = RANDOM(2) ? Xeater_n : Xeater_s;
2318       play_element_sound(x, y, SOUND_eater, Xeater_w);
2319       return;
2320   }
2321 }
2322
2323 static void Lalien(int x, int y)
2324 {
2325   int dx, dy;
2326
2327   if (lev.wheel_cnt)
2328   {
2329     dx = lev.wheel_x;
2330     dy = lev.wheel_y;
2331   }
2332   else
2333   {
2334     set_nearest_player_xy(x, y, &dx, &dy);
2335   }
2336
2337   if (RANDOM(2))
2338   {
2339     if (y > dy)
2340     {
2341       switch (Cave[x][y-1])
2342       {
2343         case Xblank:
2344         case Xacid_splash_e:
2345         case Xacid_splash_w:
2346         case Xplant:
2347         case Yplant:
2348         case Zplayer:
2349           Cave[x][y] = Yalien_nB;
2350           Next[x][y] = Xblank;
2351           Cave[x][y-1] = Yalien_n;
2352           Next[x][y-1] = Xalien_pause;
2353           play_element_sound(x, y, SOUND_alien, Xalien);
2354           return;
2355
2356         case Xacid_1:
2357         case Xacid_2:
2358         case Xacid_3:
2359         case Xacid_4:
2360         case Xacid_5:
2361         case Xacid_6:
2362         case Xacid_7:
2363         case Xacid_8:
2364           Cave[x][y] = Yalien_nB;
2365           Next[x][y] = Xblank;
2366           if (Cave[x+1][y-2] == Xblank)
2367             Cave[x+1][y-2] = Xacid_splash_e;
2368           if (Cave[x-1][y-2] == Xblank)
2369             Cave[x-1][y-2] = Xacid_splash_w;
2370           play_element_sound(x, y, SOUND_acid, Xacid_1);
2371           return;
2372       }
2373     }
2374     else if (y < dy)
2375     {
2376       switch (Cave[x][y+1])
2377       {
2378         case Xblank:
2379         case Xacid_splash_e:
2380         case Xacid_splash_w:
2381         case Xplant:
2382         case Yplant:
2383         case Zplayer:
2384           Cave[x][y] = Yalien_sB;
2385           Next[x][y] = Xblank;
2386           Cave[x][y+1] = Yalien_s;
2387           Next[x][y+1] = Xalien_pause;
2388           play_element_sound(x, y, SOUND_alien, Xalien);
2389           return;
2390
2391         case Xacid_1:
2392         case Xacid_2:
2393         case Xacid_3:
2394         case Xacid_4:
2395         case Xacid_5:
2396         case Xacid_6:
2397         case Xacid_7:
2398         case Xacid_8:
2399           Cave[x][y] = Yalien_sB;
2400           Next[x][y] = Xblank;
2401           if (Cave[x+1][y] == Xblank)
2402             Cave[x+1][y] = Xacid_splash_e;
2403           if (Cave[x-1][y] == Xblank)
2404             Cave[x-1][y] = Xacid_splash_w;
2405           play_element_sound(x, y, SOUND_acid, Xacid_1);
2406           return;
2407       }
2408     }
2409   }
2410   else
2411   {
2412     if (x < dx)
2413     {
2414       switch (Cave[x+1][y])
2415       {
2416         case Xblank:
2417         case Xacid_splash_e:
2418         case Xacid_splash_w:
2419         case Xplant:
2420         case Yplant:
2421         case Zplayer:
2422           Cave[x][y] = Yalien_eB;
2423           Next[x][y] = Xblank;
2424           Cave[x+1][y] = Yalien_e;
2425           Next[x+1][y] = Xalien_pause;
2426           play_element_sound(x, y, SOUND_alien, Xalien);
2427           return;
2428
2429         case Xacid_1:
2430         case Xacid_2:
2431         case Xacid_3:
2432         case Xacid_4:
2433         case Xacid_5:
2434         case Xacid_6:
2435         case Xacid_7:
2436         case Xacid_8:
2437           Cave[x][y] = Yalien_eB;
2438           Next[x][y] = Xblank;
2439           if (Cave[x+2][y-1] == Xblank)
2440             Cave[x+2][y-1] = Xacid_splash_e;
2441           if (Cave[x][y-1] == Xblank)
2442             Cave[x][y-1] = Xacid_splash_w;
2443           play_element_sound(x, y, SOUND_acid, Xacid_1);
2444           return;
2445       }
2446     }
2447     else if (x > dx)
2448     {
2449       switch (Cave[x-1][y])
2450       {
2451         case Xblank:
2452         case Xacid_splash_e:
2453         case Xacid_splash_w:
2454         case Xplant:
2455         case Yplant:
2456         case Zplayer:
2457           Cave[x][y] = Yalien_wB;
2458           Next[x][y] = Xblank;
2459           Cave[x-1][y] = Yalien_w;
2460           Next[x-1][y] = Xalien_pause;
2461           play_element_sound(x, y, SOUND_alien, Xalien);
2462           return;
2463
2464         case Xacid_1:
2465         case Xacid_2:
2466         case Xacid_3:
2467         case Xacid_4:
2468         case Xacid_5:
2469         case Xacid_6:
2470         case Xacid_7:
2471         case Xacid_8:
2472           Cave[x][y] = Yalien_wB;
2473           Next[x][y] = Xblank;
2474           if (Cave[x][y-1] == Xblank)
2475             Cave[x][y-1] = Xacid_splash_e;
2476           if (Cave[x-2][y-1] == Xblank)
2477             Cave[x-2][y-1] = Xacid_splash_w;
2478           play_element_sound(x, y, SOUND_acid, Xacid_1);
2479           return;
2480       }
2481     }
2482   }
2483 }
2484
2485 static void Lalien_pause(int x, int y)
2486 {
2487   Next[x][y] = Xalien;
2488 }
2489
2490 static void Lbug_n(int x, int y)
2491 {
2492   switch (Cave[x][y-1])
2493   {
2494     case Xblank:
2495     case Xacid_splash_e:
2496     case Xacid_splash_w:
2497     case Xplant:
2498     case Yplant:
2499     case Zplayer:
2500       Cave[x][y] = Ybug_nB;
2501       Next[x][y] = Xblank;
2502       Cave[x][y-1] = Ybug_n;
2503       Next[x][y-1] = Xbug_1_n;
2504       play_element_sound(x, y, SOUND_bug, Xbug_1_n);
2505       return;
2506
2507     case Xacid_1:
2508     case Xacid_2:
2509     case Xacid_3:
2510     case Xacid_4:
2511     case Xacid_5:
2512     case Xacid_6:
2513     case Xacid_7:
2514     case Xacid_8:
2515       Cave[x][y] = Ybug_nB;
2516       Next[x][y] = Xblank;
2517       if (Cave[x+1][y-2] == Xblank)
2518         Cave[x+1][y-2] = Xacid_splash_e;
2519       if (Cave[x-1][y-2] == Xblank)
2520         Cave[x-1][y-2] = Xacid_splash_w;
2521       play_element_sound(x, y, SOUND_acid, Xacid_1);
2522       return;
2523
2524     default:
2525       Cave[x][y] = Ybug_n_w;
2526       Next[x][y] = Xbug_2_w;
2527       play_element_sound(x, y, SOUND_bug, Xbug_1_n);
2528       return;
2529   }
2530 }
2531
2532 static void Lbug_1_n(int x, int y)
2533 {
2534   if (tab_amoeba[Cave[x][y-1]] ||
2535       tab_amoeba[Cave[x+1][y]] ||
2536       tab_amoeba[Cave[x][y+1]] ||
2537       tab_amoeba[Cave[x-1][y]])
2538   {
2539     Lboom_bug(x, y, Xbug_1_n);
2540
2541     return;
2542   }
2543
2544   switch (Cave[x+1][y])
2545   {
2546     case Xblank:
2547     case Xacid_splash_e:
2548     case Xacid_splash_w:
2549     case Xplant:
2550     case Yplant:
2551     case Xacid_1:
2552     case Xacid_2:
2553     case Xacid_3:
2554     case Xacid_4:
2555     case Xacid_5:
2556     case Xacid_6:
2557     case Xacid_7:
2558     case Xacid_8:
2559     case Zplayer:
2560       Cave[x][y] = Ybug_n_e;
2561       Next[x][y] = Xbug_2_e;
2562       play_element_sound(x, y, SOUND_bug, Xbug_1_n);
2563       return;
2564
2565     default:
2566       Lbug_n(x, y);
2567       return;
2568   }
2569 }
2570
2571 static void Lbug_2_n(int x, int y)
2572 {
2573   if (tab_amoeba[Cave[x][y-1]] ||
2574       tab_amoeba[Cave[x+1][y]] ||
2575       tab_amoeba[Cave[x][y+1]] ||
2576       tab_amoeba[Cave[x-1][y]])
2577   {
2578     Lboom_bug(x, y, Xbug_2_n);
2579
2580     return;
2581   }
2582
2583   Lbug_n(x, y);
2584 }
2585
2586 static void Lbug_e(int x, int y)
2587 {
2588   switch (Cave[x+1][y])
2589   {
2590     case Xblank:
2591     case Xacid_splash_e:
2592     case Xacid_splash_w:
2593     case Xplant:
2594     case Yplant:
2595     case Zplayer:
2596       Cave[x][y] = Ybug_eB;
2597       Next[x][y] = Xblank;
2598       Cave[x+1][y] = Ybug_e;
2599       Next[x+1][y] = Xbug_1_e;
2600       play_element_sound(x, y, SOUND_bug, Xbug_1_e);
2601       return;
2602
2603     case Xacid_1:
2604     case Xacid_2:
2605     case Xacid_3:
2606     case Xacid_4:
2607     case Xacid_5:
2608     case Xacid_6:
2609     case Xacid_7:
2610     case Xacid_8:
2611       Cave[x][y] = Ybug_eB;
2612       Next[x][y] = Xblank;
2613       if (Cave[x+2][y-1] == Xblank)
2614         Cave[x+2][y-1] = Xacid_splash_e;
2615       if (Cave[x][y-1] == Xblank)
2616         Cave[x][y-1] = Xacid_splash_w;
2617       play_element_sound(x, y, SOUND_acid, Xacid_1);
2618       return;
2619
2620     default:
2621       Cave[x][y] = Ybug_e_n;
2622       Next[x][y] = Xbug_2_n;
2623       play_element_sound(x, y, SOUND_bug, Xbug_1_e);
2624       return;
2625   }
2626 }
2627
2628 static void Lbug_1_e(int x, int y)
2629 {
2630   if (tab_amoeba[Cave[x][y-1]] ||
2631       tab_amoeba[Cave[x+1][y]] ||
2632       tab_amoeba[Cave[x][y+1]] ||
2633       tab_amoeba[Cave[x-1][y]])
2634   {
2635     Lboom_bug(x, y, Xbug_1_e);
2636
2637     return;
2638   }
2639
2640   switch (Cave[x][y+1])
2641   {
2642     case Xblank:
2643     case Xacid_splash_e:
2644     case Xacid_splash_w:
2645     case Xplant:
2646     case Yplant:
2647     case Xacid_1:
2648     case Xacid_2:
2649     case Xacid_3:
2650     case Xacid_4:
2651     case Xacid_5:
2652     case Xacid_6:
2653     case Xacid_7:
2654     case Xacid_8:
2655     case Zplayer:
2656       Cave[x][y] = Ybug_e_s;
2657       Next[x][y] = Xbug_2_s;
2658       play_element_sound(x, y, SOUND_bug, Xbug_1_e);
2659       return;
2660
2661     default:
2662       Lbug_e(x, y);
2663       return;
2664   }
2665 }
2666
2667 static void Lbug_2_e(int x, int y)
2668 {
2669   if (tab_amoeba[Cave[x][y-1]] ||
2670       tab_amoeba[Cave[x+1][y]] ||
2671       tab_amoeba[Cave[x][y+1]] ||
2672       tab_amoeba[Cave[x-1][y]])
2673   {
2674     Lboom_bug(x, y, Xbug_2_e);
2675
2676     return;
2677   }
2678
2679   Lbug_e(x, y);
2680 }
2681
2682 static void Lbug_s(int x, int y)
2683 {
2684   switch (Cave[x][y+1])
2685   {
2686     case Xblank:
2687     case Xacid_splash_e:
2688     case Xacid_splash_w:
2689     case Xplant:
2690     case Yplant:
2691     case Zplayer:
2692       Cave[x][y] = Ybug_sB;
2693       Next[x][y] = Xblank;
2694       Cave[x][y+1] = Ybug_s;
2695       Next[x][y+1] = Xbug_1_s;
2696       play_element_sound(x, y, SOUND_bug, Xbug_1_s);
2697       return;
2698
2699     case Xacid_1:
2700     case Xacid_2:
2701     case Xacid_3:
2702     case Xacid_4:
2703     case Xacid_5:
2704     case Xacid_6:
2705     case Xacid_7:
2706     case Xacid_8:
2707       Cave[x][y] = Ybug_sB;
2708       Next[x][y] = Xblank;
2709       if (Cave[x+1][y] == Xblank)
2710         Cave[x+1][y] = Xacid_splash_e;
2711       if (Cave[x-1][y] == Xblank)
2712         Cave[x-1][y] = Xacid_splash_w;
2713       play_element_sound(x, y, SOUND_acid, Xacid_1);
2714       return;
2715
2716     default:
2717       Cave[x][y] = Ybug_s_e;
2718       Next[x][y] = Xbug_2_e;
2719       play_element_sound(x, y, SOUND_bug, Xbug_1_s);
2720       return;
2721   }
2722 }
2723
2724 static void Lbug_1_s(int x, int y)
2725 {
2726   if (tab_amoeba[Cave[x][y-1]] ||
2727       tab_amoeba[Cave[x+1][y]] ||
2728       tab_amoeba[Cave[x][y+1]] ||
2729       tab_amoeba[Cave[x-1][y]])
2730   {
2731     Lboom_bug(x, y, Xbug_1_s);
2732
2733     return;
2734   }
2735
2736   switch (Cave[x-1][y])
2737   {
2738     case Xblank:
2739     case Xacid_splash_e:
2740     case Xacid_splash_w:
2741     case Xplant:
2742     case Yplant:
2743     case Xacid_1:
2744     case Xacid_2:
2745     case Xacid_3:
2746     case Xacid_4:
2747     case Xacid_5:
2748     case Xacid_6:
2749     case Xacid_7:
2750     case Xacid_8:
2751     case Zplayer:
2752       Cave[x][y] = Ybug_s_w;
2753       Next[x][y] = Xbug_2_w;
2754       play_element_sound(x, y, SOUND_bug, Xbug_1_s);
2755       return;
2756
2757     default:
2758       Lbug_s(x, y);
2759       return;
2760   }
2761 }
2762
2763 static void Lbug_2_s(int x, int y)
2764 {
2765   if (tab_amoeba[Cave[x][y-1]] ||
2766       tab_amoeba[Cave[x+1][y]] ||
2767       tab_amoeba[Cave[x][y+1]] ||
2768       tab_amoeba[Cave[x-1][y]])
2769   {
2770     Lboom_bug(x, y, Xbug_2_s);
2771
2772     return;
2773   }
2774
2775   Lbug_s(x, y);
2776 }
2777
2778 static void Lbug_w(int x, int y)
2779 {
2780   switch (Cave[x-1][y])
2781   {
2782     case Xblank:
2783     case Xacid_splash_e:
2784     case Xacid_splash_w:
2785     case Xplant:
2786     case Yplant:
2787     case Zplayer:
2788       Cave[x][y] = Ybug_wB;
2789       Next[x][y] = Xblank;
2790       Cave[x-1][y] = Ybug_w;
2791       Next[x-1][y] = Xbug_1_w;
2792       play_element_sound(x, y, SOUND_bug, Xbug_1_w);
2793       return;
2794
2795     case Xacid_1:
2796     case Xacid_2:
2797     case Xacid_3:
2798     case Xacid_4:
2799     case Xacid_5:
2800     case Xacid_6:
2801     case Xacid_7:
2802     case Xacid_8:
2803       Cave[x][y] = Ybug_wB;
2804       Next[x][y] = Xblank;
2805       if (Cave[x][y-1] == Xblank)
2806         Cave[x][y-1] = Xacid_splash_e;
2807       if (Cave[x-2][y-1] == Xblank)
2808         Cave[x-2][y-1] = Xacid_splash_w;
2809       play_element_sound(x, y, SOUND_acid, Xacid_1);
2810       return;
2811
2812     default:
2813       Cave[x][y] = Ybug_w_s;
2814       Next[x][y] = Xbug_2_s;
2815       play_element_sound(x, y, SOUND_bug, Xbug_1_w);
2816       return;
2817   }
2818 }
2819
2820 static void Lbug_1_w(int x, int y)
2821 {
2822   if (tab_amoeba[Cave[x][y-1]] ||
2823       tab_amoeba[Cave[x+1][y]] ||
2824       tab_amoeba[Cave[x][y+1]] ||
2825       tab_amoeba[Cave[x-1][y]])
2826   {
2827     Lboom_bug(x, y, Xbug_1_w);
2828
2829     return;
2830   }
2831
2832   switch (Cave[x][y-1])
2833   {
2834     case Xblank:
2835     case Xacid_splash_e:
2836     case Xacid_splash_w:
2837     case Xplant:
2838     case Yplant:
2839     case Xacid_1:
2840     case Xacid_2:
2841     case Xacid_3:
2842     case Xacid_4:
2843     case Xacid_5:
2844     case Xacid_6:
2845     case Xacid_7:
2846     case Xacid_8:
2847     case Zplayer:
2848       Cave[x][y] = Ybug_w_n;
2849       Next[x][y] = Xbug_2_n;
2850       play_element_sound(x, y, SOUND_bug, Xbug_1_w);
2851       return;
2852
2853     default:
2854       Lbug_w(x, y);
2855       return;
2856   }
2857 }
2858
2859 static void Lbug_2_w(int x, int y)
2860 {
2861   if (tab_amoeba[Cave[x][y-1]] ||
2862       tab_amoeba[Cave[x+1][y]] ||
2863       tab_amoeba[Cave[x][y+1]] ||
2864       tab_amoeba[Cave[x-1][y]])
2865   {
2866     Lboom_bug(x, y, Xbug_2_w);
2867
2868     return;
2869   }
2870
2871   Lbug_w(x, y);
2872 }
2873
2874 static void Ltank_n(int x, int y)
2875 {
2876   switch (Cave[x][y-1])
2877   {
2878     case Xblank:
2879     case Xacid_splash_e:
2880     case Xacid_splash_w:
2881     case Xplant:
2882     case Yplant:
2883     case Zplayer:
2884       Cave[x][y] = Ytank_nB;
2885       Next[x][y] = Xblank;
2886       Cave[x][y-1] = Ytank_n;
2887       Next[x][y-1] = Xtank_1_n;
2888       play_element_sound(x, y, SOUND_tank, Xtank_1_n);
2889       return;
2890
2891     case Xacid_1:
2892     case Xacid_2:
2893     case Xacid_3:
2894     case Xacid_4:
2895     case Xacid_5:
2896     case Xacid_6:
2897     case Xacid_7:
2898     case Xacid_8:
2899       Cave[x][y] = Ytank_nB;
2900       Next[x][y] = Xblank;
2901       if (Cave[x+1][y-2] == Xblank)
2902         Cave[x+1][y-2] = Xacid_splash_e;
2903       if (Cave[x-1][y-2] == Xblank)
2904         Cave[x-1][y-2] = Xacid_splash_w;
2905       play_element_sound(x, y, SOUND_acid, Xacid_1);
2906       return;
2907
2908     default:
2909       Cave[x][y] = Ytank_n_e;
2910       Next[x][y] = Xtank_2_e;
2911       play_element_sound(x, y, SOUND_tank, Xtank_1_n);
2912       return;
2913   }
2914 }
2915
2916 static void Ltank_1_n(int x, int y)
2917 {
2918   if (tab_amoeba[Cave[x][y-1]] ||
2919       tab_amoeba[Cave[x+1][y]] ||
2920       tab_amoeba[Cave[x][y+1]] ||
2921       tab_amoeba[Cave[x-1][y]])
2922   {
2923     Lboom_tank(x, y, Xtank_1_n);
2924
2925     return;
2926   }
2927
2928   switch (Cave[x-1][y])
2929   {
2930     case Xblank:
2931     case Xacid_splash_e:
2932     case Xacid_splash_w:
2933     case Xplant:
2934     case Yplant:
2935     case Xacid_1:
2936     case Xacid_2:
2937     case Xacid_3:
2938     case Xacid_4:
2939     case Xacid_5:
2940     case Xacid_6:
2941     case Xacid_7:
2942     case Xacid_8:
2943     case Zplayer:
2944       Cave[x][y] = Ytank_n_w;
2945       Next[x][y] = Xtank_2_w;
2946       play_element_sound(x, y, SOUND_tank, Xtank_1_n);
2947       return;
2948
2949     default:
2950       Ltank_n(x, y);
2951       return;
2952   }
2953 }
2954
2955 static void Ltank_2_n(int x, int y)
2956 {
2957   if (tab_amoeba[Cave[x][y-1]] ||
2958       tab_amoeba[Cave[x+1][y]] ||
2959       tab_amoeba[Cave[x][y+1]] ||
2960       tab_amoeba[Cave[x-1][y]])
2961   {
2962     Lboom_tank(x, y, Xtank_2_n);
2963
2964     return;
2965   }
2966
2967   Ltank_n(x, y);
2968 }
2969
2970 static void Ltank_e(int x, int y)
2971 {
2972   switch (Cave[x+1][y])
2973   {
2974     case Xblank:
2975     case Xacid_splash_e:
2976     case Xacid_splash_w:
2977     case Xplant:
2978     case Yplant:
2979     case Zplayer:
2980       Cave[x][y] = Ytank_eB;
2981       Next[x][y] = Xblank;
2982       Cave[x+1][y] = Ytank_e;
2983       Next[x+1][y] = Xtank_1_e;
2984       play_element_sound(x, y, SOUND_tank, Xtank_1_e);
2985       return;
2986
2987     case Xacid_1:
2988     case Xacid_2:
2989     case Xacid_3:
2990     case Xacid_4:
2991     case Xacid_5:
2992     case Xacid_6:
2993     case Xacid_7:
2994     case Xacid_8:
2995       Cave[x][y] = Ytank_eB;
2996       Next[x][y] = Xblank;
2997       if (Cave[x+2][y-1] == Xblank)
2998         Cave[x+2][y-1] = Xacid_splash_e;
2999       if (Cave[x][y-1] == Xblank)
3000         Cave[x][y-1] = Xacid_splash_w;
3001       play_element_sound(x, y, SOUND_acid, Xacid_1);
3002       return;
3003
3004     default:
3005       Cave[x][y] = Ytank_e_s;
3006       Next[x][y] = Xtank_2_s;
3007       play_element_sound(x, y, SOUND_tank, Xtank_1_e);
3008       return;
3009   }
3010 }
3011
3012 static void Ltank_1_e(int x, int y)
3013 {
3014   if (tab_amoeba[Cave[x][y-1]] ||
3015       tab_amoeba[Cave[x+1][y]] ||
3016       tab_amoeba[Cave[x][y+1]] ||
3017       tab_amoeba[Cave[x-1][y]])
3018   {
3019     Lboom_tank(x, y, Xtank_1_e);
3020
3021     return;
3022   }
3023
3024   switch (Cave[x][y-1])
3025   {
3026     case Xblank:
3027     case Xacid_splash_e:
3028     case Xacid_splash_w:
3029     case Xplant:
3030     case Yplant:
3031     case Xacid_1:
3032     case Xacid_2:
3033     case Xacid_3:
3034     case Xacid_4:
3035     case Xacid_5:
3036     case Xacid_6:
3037     case Xacid_7:
3038     case Xacid_8:
3039     case Zplayer:
3040       Cave[x][y] = Ytank_e_n;
3041       Next[x][y] = Xtank_2_n;
3042       play_element_sound(x, y, SOUND_tank, Xtank_1_e);
3043       return;
3044
3045     default:
3046       Ltank_e(x, y);
3047       return;
3048   }
3049 }
3050
3051 static void Ltank_2_e(int x, int y)
3052 {
3053   if (tab_amoeba[Cave[x][y-1]] ||
3054       tab_amoeba[Cave[x+1][y]] ||
3055       tab_amoeba[Cave[x][y+1]] ||
3056       tab_amoeba[Cave[x-1][y]])
3057   {
3058     Lboom_tank(x, y, Xtank_2_e);
3059
3060     return;
3061   }
3062
3063   Ltank_e(x, y);
3064 }
3065
3066 static void Ltank_s(int x, int y)
3067 {
3068   switch (Cave[x][y+1])
3069   {
3070     case Xblank:
3071     case Xacid_splash_e:
3072     case Xacid_splash_w:
3073     case Xplant:
3074     case Yplant:
3075     case Zplayer:
3076       Cave[x][y] = Ytank_sB;
3077       Next[x][y] = Xblank;
3078       Cave[x][y+1] = Ytank_s;
3079       Next[x][y+1] = Xtank_1_s;
3080       play_element_sound(x, y, SOUND_tank, Xtank_1_s);
3081       return;
3082
3083     case Xacid_1:
3084     case Xacid_2:
3085     case Xacid_3:
3086     case Xacid_4:
3087     case Xacid_5:
3088     case Xacid_6:
3089     case Xacid_7:
3090     case Xacid_8:
3091       Cave[x][y] = Ytank_sB;
3092       Next[x][y] = Xblank;
3093       if (Cave[x+1][y] == Xblank)
3094         Cave[x+1][y] = Xacid_splash_e;
3095       if (Cave[x-1][y] == Xblank)
3096         Cave[x-1][y] = Xacid_splash_w;
3097       play_element_sound(x, y, SOUND_acid, Xacid_1);
3098       return;
3099
3100     default:
3101       Cave[x][y] = Ytank_s_w;
3102       Next[x][y] = Xtank_2_w;
3103       play_element_sound(x, y, SOUND_tank, Xtank_1_s);
3104       return;
3105   }
3106 }
3107
3108 static void Ltank_1_s(int x, int y)
3109 {
3110   if (tab_amoeba[Cave[x][y-1]] ||
3111       tab_amoeba[Cave[x+1][y]] ||
3112       tab_amoeba[Cave[x][y+1]] ||
3113       tab_amoeba[Cave[x-1][y]])
3114   {
3115     Lboom_tank(x, y, Xtank_1_s);
3116
3117     return;
3118   }
3119
3120   switch (Cave[x+1][y])
3121   {
3122     case Xblank:
3123     case Xacid_splash_e:
3124     case Xacid_splash_w:
3125     case Xplant:
3126     case Yplant:
3127     case Xacid_1:
3128     case Xacid_2:
3129     case Xacid_3:
3130     case Xacid_4:
3131     case Xacid_5:
3132     case Xacid_6:
3133     case Xacid_7:
3134     case Xacid_8:
3135     case Zplayer:
3136       Cave[x][y] = Ytank_s_e;
3137       Next[x][y] = Xtank_2_e;
3138       play_element_sound(x, y, SOUND_tank, Xtank_1_s);
3139       return;
3140
3141     default:
3142       Ltank_s(x, y);
3143       return;
3144   }
3145 }
3146
3147 static void Ltank_2_s(int x, int y)
3148 {
3149   if (tab_amoeba[Cave[x][y-1]] ||
3150       tab_amoeba[Cave[x+1][y]] ||
3151       tab_amoeba[Cave[x][y+1]] ||
3152       tab_amoeba[Cave[x-1][y]])
3153   {
3154     Lboom_tank(x, y, Xtank_2_s);
3155
3156     return;
3157   }
3158
3159   Ltank_s(x, y);
3160 }
3161
3162 static void Ltank_w(int x, int y)
3163 {
3164   switch (Cave[x-1][y])
3165   {
3166     case Xblank:
3167     case Xacid_splash_e:
3168     case Xacid_splash_w:
3169     case Xplant:
3170     case Yplant:
3171     case Zplayer:
3172       Cave[x][y] = Ytank_wB;
3173       Next[x][y] = Xblank;
3174       Cave[x-1][y] = Ytank_w;
3175       Next[x-1][y] = Xtank_1_w;
3176       play_element_sound(x, y, SOUND_tank, Xtank_1_w);
3177       return;
3178
3179     case Xacid_1:
3180     case Xacid_2:
3181     case Xacid_3:
3182     case Xacid_4:
3183     case Xacid_5:
3184     case Xacid_6:
3185     case Xacid_7:
3186     case Xacid_8:
3187       Cave[x][y] = Ytank_wB;
3188       Next[x][y] = Xblank;
3189       if (Cave[x][y-1] == Xblank)
3190         Cave[x][y-1] = Xacid_splash_e;
3191       if (Cave[x-2][y-1] == Xblank)
3192         Cave[x-2][y-1] = Xacid_splash_w;
3193       play_element_sound(x, y, SOUND_acid, Xacid_1);
3194       return;
3195
3196     default:
3197       Cave[x][y] = Ytank_w_n;
3198       Next[x][y] = Xtank_2_n;
3199       play_element_sound(x, y, SOUND_tank, Xtank_1_w);
3200       return;
3201   }
3202 }
3203
3204 static void Ltank_1_w(int x, int y)
3205 {
3206   if (tab_amoeba[Cave[x][y-1]] ||
3207       tab_amoeba[Cave[x+1][y]] ||
3208       tab_amoeba[Cave[x][y+1]] ||
3209       tab_amoeba[Cave[x-1][y]])
3210   {
3211     Lboom_tank(x, y, Xtank_1_w);
3212
3213     return;
3214   }
3215
3216   switch (Cave[x][y+1])
3217   {
3218     case Xblank:
3219     case Xacid_splash_e:
3220     case Xacid_splash_w:
3221     case Xplant:
3222     case Yplant:
3223     case Xacid_1:
3224     case Xacid_2:
3225     case Xacid_3:
3226     case Xacid_4:
3227     case Xacid_5:
3228     case Xacid_6:
3229     case Xacid_7:
3230     case Xacid_8:
3231     case Zplayer:
3232       Cave[x][y] = Ytank_w_s;
3233       Next[x][y] = Xtank_2_s;
3234       play_element_sound(x, y, SOUND_tank, Xtank_1_w);
3235       return;
3236
3237     default:
3238       Ltank_w(x, y);
3239       return;
3240   }
3241 }
3242
3243 static void Ltank_2_w(int x, int y)
3244 {
3245   if (tab_amoeba[Cave[x][y-1]] ||
3246       tab_amoeba[Cave[x+1][y]] ||
3247       tab_amoeba[Cave[x][y+1]] ||
3248       tab_amoeba[Cave[x-1][y]])
3249   {
3250     Lboom_tank(x, y, Xtank_2_w);
3251
3252     return;
3253   }
3254
3255   Ltank_w(x, y);
3256 }
3257
3258 static void Lemerald(int x, int y)
3259 {
3260   switch (Cave[x][y+1])
3261   {
3262     case Xblank:
3263     case Xacid_splash_e:
3264     case Xacid_splash_w:
3265       Cave[x][y] = Yemerald_sB;
3266       Next[x][y] = Xblank;
3267       Cave[x][y+1] = Yemerald_s;
3268       Next[x][y+1] = Xemerald_fall;
3269       return;
3270
3271     case Xacid_1:
3272     case Xacid_2:
3273     case Xacid_3:
3274     case Xacid_4:
3275     case Xacid_5:
3276     case Xacid_6:
3277     case Xacid_7:
3278     case Xacid_8:
3279       Cave[x][y] = Yemerald_sB;
3280       Next[x][y] = Xblank;
3281       if (Cave[x+1][y] == Xblank)
3282         Cave[x+1][y] = Xacid_splash_e;
3283       if (Cave[x-1][y] == Xblank)
3284         Cave[x-1][y] = Xacid_splash_w;
3285       play_element_sound(x, y, SOUND_acid, Xacid_1);
3286       return;
3287
3288     case Xspring:
3289     case Xspring_pause:
3290     case Xspring_e:
3291     case Xspring_w:
3292     case Xandroid:
3293     case Xandroid_1_n:
3294     case Xandroid_2_n:
3295     case Xandroid_1_e:
3296     case Xandroid_2_e:
3297     case Xandroid_1_s:
3298     case Xandroid_2_s:
3299     case Xandroid_1_w:
3300     case Xandroid_2_w:
3301     case Xstone:
3302     case Xstone_pause:
3303     case Xemerald:
3304     case Xemerald_pause:
3305     case Xdiamond:
3306     case Xdiamond_pause:
3307     case Xbomb:
3308     case Xbomb_pause:
3309     case Xballoon:
3310     case Xacid_ne:
3311     case Xacid_nw:
3312     case Xball_1:
3313     case Xball_2:
3314     case Xnut:
3315     case Xnut_pause:
3316     case Xslidewall_ns:
3317     case Xslidewall_ew:
3318     case Xwonderwall:
3319     case Xkey_1:
3320     case Xkey_2:
3321     case Xkey_3:
3322     case Xkey_4:
3323     case Xkey_5:
3324     case Xkey_6:
3325     case Xkey_7:
3326     case Xkey_8:
3327     case Xbumper:
3328     case Xswitch:
3329     case Xsteel_1:
3330     case Xsteel_2:
3331     case Xsteel_3:
3332     case Xsteel_4:
3333     case Xwall_1:
3334     case Xwall_2:
3335     case Xwall_3:
3336     case Xwall_4:
3337     case Xroundwall_1:
3338     case Xroundwall_2:
3339     case Xroundwall_3:
3340     case Xroundwall_4:
3341       if (RANDOM(2))
3342       {
3343         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3344         {
3345           Cave[x][y] = Yemerald_eB;
3346           Next[x][y] = Xblank;
3347           Cave[x+1][y] = Yemerald_e;
3348           Next[x+1][y] = Xemerald_pause;
3349           return;
3350         }
3351
3352         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3353         {
3354           Cave[x][y] = Yemerald_wB;
3355           Next[x][y] = Xblank;
3356           Cave[x-1][y] = Yemerald_w;
3357           Next[x-1][y] = Xemerald_pause;
3358           return;
3359         }
3360       }
3361       else
3362       {
3363         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3364         {
3365           Cave[x][y] = Yemerald_wB;
3366           Next[x][y] = Xblank;
3367           Cave[x-1][y] = Yemerald_w;
3368           Next[x-1][y] = Xemerald_pause;
3369           return;
3370         }
3371
3372         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3373         {
3374           Cave[x][y] = Yemerald_eB;
3375           Next[x][y] = Xblank;
3376           Cave[x+1][y] = Yemerald_e;
3377           Next[x+1][y] = Xemerald_pause;
3378           return;
3379         }
3380       }
3381
3382     default:
3383       if (++lev.shine_cnt > 50)
3384       {
3385         lev.shine_cnt = RANDOM(8);
3386         Cave[x][y] = Xemerald_shine;
3387       }
3388
3389       return;
3390   }
3391 }
3392
3393 static void Lemerald_pause(int x, int y)
3394 {
3395   switch (Cave[x][y+1])
3396   {
3397     case Xblank:
3398     case Xacid_splash_e:
3399     case Xacid_splash_w:
3400       Cave[x][y] = Yemerald_sB;
3401       Next[x][y] = Xblank;
3402       Cave[x][y+1] = Yemerald_s;
3403       Next[x][y+1] = Xemerald_fall;
3404       return;
3405
3406     case Xacid_1:
3407     case Xacid_2:
3408     case Xacid_3:
3409     case Xacid_4:
3410     case Xacid_5:
3411     case Xacid_6:
3412     case Xacid_7:
3413     case Xacid_8:
3414       Cave[x][y] = Yemerald_sB;
3415       Next[x][y] = Xblank;
3416       if (Cave[x+1][y] == Xblank)
3417         Cave[x+1][y] = Xacid_splash_e;
3418       if (Cave[x-1][y] == Xblank)
3419         Cave[x-1][y] = Xacid_splash_w;
3420       play_element_sound(x, y, SOUND_acid, Xacid_1);
3421       return;
3422
3423     default:
3424       Cave[x][y] = Xemerald;
3425       Next[x][y] = Xemerald;
3426       return;
3427   }
3428 }
3429
3430 static void Lemerald_fall(int x, int y)
3431 {
3432   switch (Cave[x][y+1])
3433   {
3434     case Xblank:
3435     case Xacid_splash_e:
3436     case Xacid_splash_w:
3437     case Zplayer:
3438       Cave[x][y] = Yemerald_sB;
3439       Next[x][y] = Xblank;
3440       Cave[x][y+1] = Yemerald_s;
3441       Next[x][y+1] = Xemerald_fall;
3442       return;
3443
3444     case Xacid_1:
3445     case Xacid_2:
3446     case Xacid_3:
3447     case Xacid_4:
3448     case Xacid_5:
3449     case Xacid_6:
3450     case Xacid_7:
3451     case Xacid_8:
3452       Cave[x][y] = Yemerald_sB;
3453       Next[x][y] = Xblank;
3454       if (Cave[x+1][y] == Xblank)
3455         Cave[x+1][y] = Xacid_splash_e;
3456       if (Cave[x-1][y] == Xblank)
3457         Cave[x-1][y] = Xacid_splash_w;
3458       play_element_sound(x, y, SOUND_acid, Xacid_1);
3459       return;
3460
3461     case Xwonderwall:
3462       if (lev.wonderwall_time)
3463       {
3464         lev.wonderwall_state = 1;
3465         Cave[x][y] = Yemerald_sB;
3466         if (tab_blank[Cave[x][y+2]])
3467         {
3468           Cave[x][y+2] = Ydiamond_s;
3469           Next[x][y+2] = Xdiamond_fall;
3470         }
3471
3472         Next[x][y] = Xblank;
3473         play_element_sound(x, y, SOUND_wonderfall, Xwonderwall);
3474         return;
3475       }
3476
3477     default:
3478       Cave[x][y] = Xemerald;
3479       Next[x][y] = Xemerald;
3480       play_element_sound(x, y, SOUND_diamond, Xemerald);
3481       return;
3482   }
3483 }
3484
3485 static void Ldiamond(int x, int y)
3486 {
3487   switch (Cave[x][y+1])
3488   {
3489     case Xblank:
3490     case Xacid_splash_e:
3491     case Xacid_splash_w:
3492       Cave[x][y] = Ydiamond_sB;
3493       Next[x][y] = Xblank;
3494       Cave[x][y+1] = Ydiamond_s;
3495       Next[x][y+1] = Xdiamond_fall;
3496       return;
3497
3498     case Xacid_1:
3499     case Xacid_2:
3500     case Xacid_3:
3501     case Xacid_4:
3502     case Xacid_5:
3503     case Xacid_6:
3504     case Xacid_7:
3505     case Xacid_8:
3506       Cave[x][y] = Ydiamond_sB;
3507       Next[x][y] = Xblank;
3508       if (Cave[x+1][y] == Xblank)
3509         Cave[x+1][y] = Xacid_splash_e;
3510       if (Cave[x-1][y] == Xblank)
3511         Cave[x-1][y] = Xacid_splash_w;
3512       play_element_sound(x, y, SOUND_acid, Xacid_1);
3513       return;
3514
3515     case Xspring:
3516     case Xspring_pause:
3517     case Xspring_e:
3518     case Xspring_w:
3519     case Xandroid:
3520     case Xandroid_1_n:
3521     case Xandroid_2_n:
3522     case Xandroid_1_e:
3523     case Xandroid_2_e:
3524     case Xandroid_1_s:
3525     case Xandroid_2_s:
3526     case Xandroid_1_w:
3527     case Xandroid_2_w:
3528     case Xstone:
3529     case Xstone_pause:
3530     case Xemerald:
3531     case Xemerald_pause:
3532     case Xdiamond:
3533     case Xdiamond_pause:
3534     case Xbomb:
3535     case Xbomb_pause:
3536     case Xballoon:
3537     case Xacid_ne:
3538     case Xacid_nw:
3539     case Xball_1:
3540     case Xball_2:
3541     case Xnut:
3542     case Xnut_pause:
3543     case Xslidewall_ns:
3544     case Xslidewall_ew:
3545     case Xwonderwall:
3546     case Xkey_1:
3547     case Xkey_2:
3548     case Xkey_3:
3549     case Xkey_4:
3550     case Xkey_5:
3551     case Xkey_6:
3552     case Xkey_7:
3553     case Xkey_8:
3554     case Xbumper:
3555     case Xswitch:
3556     case Xsteel_1:
3557     case Xsteel_2:
3558     case Xsteel_3:
3559     case Xsteel_4:
3560     case Xwall_1:
3561     case Xwall_2:
3562     case Xwall_3:
3563     case Xwall_4:
3564     case Xroundwall_1:
3565     case Xroundwall_2:
3566     case Xroundwall_3:
3567     case Xroundwall_4:
3568       if (RANDOM(2))
3569       {
3570         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3571         {
3572           Cave[x][y] = Ydiamond_eB;
3573           Next[x][y] = Xblank;
3574           Cave[x+1][y] = Ydiamond_e;
3575           Next[x+1][y] = Xdiamond_pause;
3576           return;
3577         }
3578
3579         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3580         {
3581           Cave[x][y] = Ydiamond_wB;
3582           Next[x][y] = Xblank;
3583           Cave[x-1][y] = Ydiamond_w;
3584           Next[x-1][y] = Xdiamond_pause;
3585           return;
3586         }
3587       }
3588       else
3589       {
3590         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3591         {
3592           Cave[x][y] = Ydiamond_wB;
3593           Next[x][y] = Xblank;
3594           Cave[x-1][y] = Ydiamond_w;
3595           Next[x-1][y] = Xdiamond_pause;
3596           return;
3597         }
3598
3599         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3600         {
3601           Cave[x][y] = Ydiamond_eB;
3602           Next[x][y] = Xblank;
3603           Cave[x+1][y] = Ydiamond_e;
3604           Next[x+1][y] = Xdiamond_pause;
3605           return;
3606         }
3607       }
3608
3609     default:
3610       if (++lev.shine_cnt > 50)
3611       {
3612         lev.shine_cnt = RANDOM(8);
3613         Cave[x][y] = Xdiamond_shine;
3614       }
3615
3616       return;
3617   }
3618 }
3619
3620 static void Ldiamond_pause(int x, int y)
3621 {
3622   switch (Cave[x][y+1])
3623   {
3624     case Xblank:
3625     case Xacid_splash_e:
3626     case Xacid_splash_w:
3627       Cave[x][y] = Ydiamond_sB;
3628       Next[x][y] = Xblank;
3629       Cave[x][y+1] = Ydiamond_s;
3630       Next[x][y+1] = Xdiamond_fall;
3631       return;
3632
3633     case Xacid_1:
3634     case Xacid_2:
3635     case Xacid_3:
3636     case Xacid_4:
3637     case Xacid_5:
3638     case Xacid_6:
3639     case Xacid_7:
3640     case Xacid_8:
3641       Cave[x][y] = Ydiamond_sB;
3642       Next[x][y] = Xblank;
3643       if (Cave[x+1][y] == Xblank)
3644         Cave[x+1][y] = Xacid_splash_e;
3645       if (Cave[x-1][y] == Xblank)
3646         Cave[x-1][y] = Xacid_splash_w;
3647       play_element_sound(x, y, SOUND_acid, Xacid_1);
3648       return;
3649
3650     default:
3651       Cave[x][y] = Xdiamond;
3652       Next[x][y] = Xdiamond;
3653       return;
3654   }
3655 }
3656
3657 static void Ldiamond_fall(int x, int y)
3658 {
3659   switch (Cave[x][y+1])
3660   {
3661     case Xblank:
3662     case Xacid_splash_e:
3663     case Xacid_splash_w:
3664     case Zplayer:
3665       Cave[x][y] = Ydiamond_sB;
3666       Next[x][y] = Xblank;
3667       Cave[x][y+1] = Ydiamond_s;
3668       Next[x][y+1] = Xdiamond_fall;
3669       return;
3670
3671     case Xacid_1:
3672     case Xacid_2:
3673     case Xacid_3:
3674     case Xacid_4:
3675     case Xacid_5:
3676     case Xacid_6:
3677     case Xacid_7:
3678     case Xacid_8:
3679       Cave[x][y] = Ydiamond_sB;
3680       Next[x][y] = Xblank;
3681       if (Cave[x+1][y] == Xblank)
3682         Cave[x+1][y] = Xacid_splash_e;
3683       if (Cave[x-1][y] == Xblank)
3684         Cave[x-1][y] = Xacid_splash_w;
3685       play_element_sound(x, y, SOUND_acid, Xacid_1);
3686       return;
3687
3688     case Xwonderwall:
3689       if (lev.wonderwall_time)
3690       {
3691         lev.wonderwall_state = 1;
3692         Cave[x][y] = Ydiamond_sB;
3693         if (tab_blank[Cave[x][y+2]])
3694         {
3695           Cave[x][y+2] = Ystone_s;
3696           Next[x][y+2] = Xstone_fall;
3697         }
3698
3699         Next[x][y] = Xblank;
3700         play_element_sound(x, y, SOUND_wonderfall, Xwonderwall);
3701         return;
3702       }
3703
3704     default:
3705       Cave[x][y] = Xdiamond;
3706       Next[x][y] = Xdiamond;
3707       play_element_sound(x, y, SOUND_diamond, Xdiamond);
3708       return;
3709   }
3710 }
3711
3712 static void Lstone(int x, int y)
3713 {
3714   switch (Cave[x][y+1])
3715   {
3716     case Xblank:
3717     case Xacid_splash_e:
3718     case Xacid_splash_w:
3719     case Xplant:
3720     case Yplant:
3721 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
3722     case Xfake_acid_1:
3723     case Xfake_acid_2:
3724     case Xfake_acid_3:
3725     case Xfake_acid_4:
3726     case Xfake_acid_5:
3727     case Xfake_acid_6:
3728     case Xfake_acid_7:
3729     case Xfake_acid_8:
3730 #endif
3731       Cave[x][y] = Ystone_sB;
3732       Next[x][y] = Xblank;
3733       Cave[x][y+1] = Ystone_s;
3734       Next[x][y+1] = Xstone_fall;
3735       return;
3736
3737     case Xacid_1:
3738     case Xacid_2:
3739     case Xacid_3:
3740     case Xacid_4:
3741     case Xacid_5:
3742     case Xacid_6:
3743     case Xacid_7:
3744     case Xacid_8:
3745       Cave[x][y] = Ystone_sB;
3746       Next[x][y] = Xblank;
3747       if (Cave[x+1][y] == Xblank)
3748         Cave[x+1][y] = Xacid_splash_e;
3749       if (Cave[x-1][y] == Xblank)
3750         Cave[x-1][y] = Xacid_splash_w;
3751       play_element_sound(x, y, SOUND_acid, Xacid_1);
3752       return;
3753
3754     case Xsand:
3755       Cave[x][y] = Xsand_stonein_1;
3756       Next[x][y] = Xsand_stonein_2;
3757       Cave[x][y+1] = Xsand_sandstone_1;
3758       Next[x][y+1] = Xsand_sandstone_2;
3759       return;
3760
3761     case Xspring:
3762     case Xspring_pause:
3763     case Xspring_e:
3764     case Xspring_w:
3765     case Xandroid:
3766     case Xandroid_1_n:
3767     case Xandroid_2_n:
3768     case Xandroid_1_e:
3769     case Xandroid_2_e:
3770     case Xandroid_1_s:
3771     case Xandroid_2_s:
3772     case Xandroid_1_w:
3773     case Xandroid_2_w:
3774     case Xstone:
3775     case Xstone_pause:
3776     case Xemerald:
3777     case Xemerald_pause:
3778     case Xdiamond:
3779     case Xdiamond_pause:
3780     case Xbomb:
3781     case Xbomb_pause:
3782     case Xballoon:
3783     case Xacid_ne:
3784     case Xacid_nw:
3785     case Xball_1:
3786     case Xball_2:
3787     case Xnut:
3788     case Xnut_pause:
3789     case Xslidewall_ns:
3790     case Xslidewall_ew:
3791     case Xkey_1:
3792     case Xkey_2:
3793     case Xkey_3:
3794     case Xkey_4:
3795     case Xkey_5:
3796     case Xkey_6:
3797     case Xkey_7:
3798     case Xkey_8:
3799     case Xbumper:
3800     case Xswitch:
3801     case Xlenses:
3802     case Xmagnify:
3803     case Xroundwall_1:
3804     case Xroundwall_2:
3805     case Xroundwall_3:
3806     case Xroundwall_4:
3807       if (RANDOM(2))
3808       {
3809         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3810         {
3811           Cave[x][y] = Ystone_eB;
3812           Next[x][y] = Xblank;
3813           Cave[x+1][y] = Ystone_e;
3814           Next[x+1][y] = Xstone_pause;
3815           return;
3816         }
3817
3818         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3819         {
3820           Cave[x][y] = Ystone_wB;
3821           Next[x][y] = Xblank;
3822           Cave[x-1][y] = Ystone_w;
3823           Next[x-1][y] = Xstone_pause;
3824           return;
3825         }
3826       }
3827       else
3828       {
3829         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
3830         {
3831           Cave[x][y] = Ystone_wB;
3832           Next[x][y] = Xblank;
3833           Cave[x-1][y] = Ystone_w;
3834           Next[x-1][y] = Xstone_pause;
3835           return;
3836         }
3837
3838         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
3839         {
3840           Cave[x][y] = Ystone_eB;
3841           Next[x][y] = Xblank;
3842           Cave[x+1][y] = Ystone_e;
3843           Next[x+1][y] = Xstone_pause;
3844           return;
3845         }
3846       }
3847   }
3848 }
3849
3850 static void Lstone_pause(int x, int y)
3851 {
3852   switch (Cave[x][y+1])
3853   {
3854     case Xblank:
3855     case Xacid_splash_e:
3856     case Xacid_splash_w:
3857 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
3858     case Xfake_acid_1:
3859     case Xfake_acid_2:
3860     case Xfake_acid_3:
3861     case Xfake_acid_4:
3862     case Xfake_acid_5:
3863     case Xfake_acid_6:
3864     case Xfake_acid_7:
3865     case Xfake_acid_8:
3866 #endif
3867       Cave[x][y] = Ystone_sB;
3868       Next[x][y] = Xblank;
3869       Cave[x][y+1] = Ystone_s;
3870       Next[x][y+1] = Xstone_fall;
3871       return;
3872
3873     case Xacid_1:
3874     case Xacid_2:
3875     case Xacid_3:
3876     case Xacid_4:
3877     case Xacid_5:
3878     case Xacid_6:
3879     case Xacid_7:
3880     case Xacid_8:
3881       Cave[x][y] = Ystone_sB;
3882       Next[x][y] = Xblank;
3883       if (Cave[x+1][y] == Xblank)
3884         Cave[x+1][y] = Xacid_splash_e;
3885       if (Cave[x-1][y] == Xblank)
3886         Cave[x-1][y] = Xacid_splash_w;
3887       play_element_sound(x, y, SOUND_acid, Xacid_1);
3888       return;
3889
3890     default:
3891       Cave[x][y] = Xstone;
3892       Next[x][y] = Xstone;
3893       return;
3894   }
3895 }
3896
3897 static void Lstone_fall(int x, int y)
3898 {
3899   switch (Cave[x][y+1])
3900   {
3901     case Xblank:
3902     case Xacid_splash_e:
3903     case Xacid_splash_w:
3904     case Zplayer:
3905 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
3906     case Xfake_acid_1:
3907     case Xfake_acid_2:
3908     case Xfake_acid_3:
3909     case Xfake_acid_4:
3910     case Xfake_acid_5:
3911     case Xfake_acid_6:
3912     case Xfake_acid_7:
3913     case Xfake_acid_8:
3914 #endif
3915       Cave[x][y] = Ystone_sB;
3916       Next[x][y] = Xblank;
3917       Cave[x][y+1] = Ystone_s;
3918       Next[x][y+1] = Xstone_fall;
3919       return;
3920
3921     case Xacid_1:
3922     case Xacid_2:
3923     case Xacid_3:
3924     case Xacid_4:
3925     case Xacid_5:
3926     case Xacid_6:
3927     case Xacid_7:
3928     case Xacid_8:
3929       Cave[x][y] = Ystone_sB;
3930       Next[x][y] = Xblank;
3931       if (Cave[x+1][y] == Xblank)
3932         Cave[x+1][y] = Xacid_splash_e;
3933       if (Cave[x-1][y] == Xblank)
3934         Cave[x-1][y] = Xacid_splash_w;
3935       play_element_sound(x, y, SOUND_acid, Xacid_1);
3936       return;
3937
3938     case Xnut:
3939     case Xnut_pause:
3940       Next[x][y] = Xstone;
3941       Cave[x][y+1] = Ynut_stone;
3942       Next[x][y+1] = Xemerald;
3943       play_element_sound(x, y, SOUND_crack, Xnut);
3944       score += lev.nut_score;
3945       return;
3946
3947     case Xbug_1_n:
3948     case Xbug_1_e:
3949     case Xbug_1_s:
3950     case Xbug_1_w:
3951     case Xbug_2_n:
3952     case Xbug_2_e:
3953     case Xbug_2_s:
3954     case Xbug_2_w:
3955       Cave[x][y] = Ystone_sB;
3956       Cave[x][y+1] = Ybug_stone;
3957       Lboom_bug(x, y+1, Xstone_fall);
3958       score += lev.bug_score;
3959       return;
3960
3961     case Xtank_1_n:
3962     case Xtank_1_e:
3963     case Xtank_1_s:
3964     case Xtank_1_w:
3965     case Xtank_2_n:
3966     case Xtank_2_e:
3967     case Xtank_2_s:
3968     case Xtank_2_w:
3969       Cave[x][y] = Ystone_sB;
3970       Cave[x][y+1] = Ytank_stone;
3971       Lboom_tank(x, y+1, Xstone_fall);
3972       score += lev.tank_score;
3973       return;
3974
3975     case Xspring:
3976       if (RANDOM(2))
3977       {
3978         switch (Cave[x+1][y+1])
3979         {
3980           case Xblank:
3981           case Xacid_splash_e:
3982           case Xacid_splash_w:
3983           case Xalien:
3984           case Xalien_pause:
3985             Cave[x][y+1] = Xspring_e;
3986             break;
3987
3988           default:
3989             Cave[x][y+1] = Xspring_w;
3990             break;
3991         }
3992       }
3993       else
3994       {
3995         switch (Cave[x-1][y+1])
3996         {
3997           case Xblank:
3998           case Xacid_splash_e:
3999           case Xacid_splash_w:
4000           case Xalien:
4001           case Xalien_pause:
4002             Cave[x][y+1] = Xspring_w;
4003             break;
4004           default:
4005             Cave[x][y+1] = Xspring_e;
4006             break;
4007         }
4008       }
4009
4010       Next[x][y] = Xstone;
4011       return;
4012
4013     case Xeater_n:
4014     case Xeater_e:
4015     case Xeater_s:
4016     case Xeater_w:
4017       Cave[x][y] = Ystone_sB;
4018       Cave[x][y+1] = Yeater_stone;
4019       Lboom_eater(x, y+1, Xstone_fall);
4020       score += lev.eater_score;
4021       return;
4022
4023     case Xalien:
4024     case Xalien_pause:
4025       Cave[x][y] = Ystone_sB;
4026       Cave[x][y+1] = Yalien_stone;
4027       Lboom_tank(x, y+1, Xstone_fall);
4028       score += lev.alien_score;
4029       return;
4030
4031     case Xdiamond:
4032     case Xdiamond_pause:
4033       switch (Cave[x][y+2])
4034       {
4035         case Xblank:
4036         case Xacid_splash_e:
4037         case Xacid_splash_w:
4038         case Zplayer:
4039         case Xbug_1_n:
4040         case Xbug_1_e:
4041         case Xbug_1_s:
4042         case Xbug_1_w:
4043         case Xbug_2_n:
4044         case Xbug_2_e:
4045         case Xbug_2_s:
4046         case Xbug_2_w:
4047         case Xtank_1_n:
4048         case Xtank_1_e:
4049         case Xtank_1_s:
4050         case Xtank_1_w:
4051         case Xtank_2_n:
4052         case Xtank_2_e:
4053         case Xtank_2_s:
4054         case Xtank_2_w:
4055         case Xspring_fall:
4056         case Xandroid:
4057         case Xandroid_1_n:
4058         case Xandroid_2_n:
4059         case Xandroid_1_e:
4060         case Xandroid_2_e:
4061         case Xandroid_1_s:
4062         case Xandroid_2_s:
4063         case Xandroid_1_w:
4064         case Xandroid_2_w:
4065         case Xstone_fall:
4066         case Xemerald_fall:
4067         case Xdiamond_fall:
4068         case Xbomb_fall:
4069         case Xacid_s:
4070         case Xacid_1:
4071         case Xacid_2:
4072         case Xacid_3:
4073         case Xacid_4:
4074         case Xacid_5:
4075         case Xacid_6:
4076         case Xacid_7:
4077         case Xacid_8:
4078         case Xnut_fall:
4079         case Xplant:
4080         case Yplant:
4081           Next[x][y] = Xstone;
4082           play_element_sound(x, y, SOUND_stone, Xstone);
4083           return;
4084       }
4085
4086       Cave[x][y] = Ystone_sB;
4087       Next[x][y] = Xblank;
4088       Cave[x][y+1] = Ydiamond_stone;
4089       Next[x][y+1] = Xstone_pause;
4090       play_element_sound(x, y, SOUND_squash, Xdiamond);
4091       return;
4092
4093     case Xbomb:
4094     case Xbomb_pause:
4095       Cave[x][y+1] = Ybomb_blank;
4096       Lboom_tank(x, y+1, Xstone_fall);
4097       return;
4098
4099     case Xwonderwall:
4100       if (lev.wonderwall_time)
4101       {
4102         lev.wonderwall_state = 1;
4103         Cave[x][y] = Ystone_sB;
4104
4105         if (tab_blank[Cave[x][y+2]])
4106         {
4107           Cave[x][y+2] = Yemerald_s;
4108           Next[x][y+2] = Xemerald_fall;
4109         }
4110
4111         Next[x][y] = Xblank;
4112         play_element_sound(x, y, SOUND_wonderfall, Xwonderwall);
4113         return;
4114       }
4115
4116     default:
4117       Cave[x][y] = Xstone;
4118       Next[x][y] = Xstone;
4119       play_element_sound(x, y, SOUND_stone, Xstone);
4120       return;
4121   }
4122 }
4123
4124 static void Lbomb(int x, int y)
4125 {
4126   switch (Cave[x][y+1])
4127   {
4128     case Xblank:
4129     case Xacid_splash_e:
4130     case Xacid_splash_w:
4131       Cave[x][y] = Ybomb_sB;
4132       Next[x][y] = Xblank;
4133       Cave[x][y+1] = Ybomb_s;
4134       Next[x][y+1] = Xbomb_fall;
4135       return;
4136
4137     case Xacid_1:
4138     case Xacid_2:
4139     case Xacid_3:
4140     case Xacid_4:
4141     case Xacid_5:
4142     case Xacid_6:
4143     case Xacid_7:
4144     case Xacid_8:
4145       Cave[x][y] = Ybomb_sB;
4146       Next[x][y] = Xblank;
4147       if (Cave[x+1][y] == Xblank)
4148         Cave[x+1][y] = Xacid_splash_e;
4149       if (Cave[x-1][y] == Xblank)
4150         Cave[x-1][y] = Xacid_splash_w;
4151       play_element_sound(x, y, SOUND_acid, Xacid_1);
4152       return;
4153
4154     case Xspring:
4155     case Xspring_pause:
4156     case Xspring_e:
4157     case Xspring_w:
4158     case Xandroid:
4159     case Xandroid_1_n:
4160     case Xandroid_2_n:
4161     case Xandroid_1_e:
4162     case Xandroid_2_e:
4163     case Xandroid_1_s:
4164     case Xandroid_2_s:
4165     case Xandroid_1_w:
4166     case Xandroid_2_w:
4167     case Xstone:
4168     case Xstone_pause:
4169     case Xemerald:
4170     case Xemerald_pause:
4171     case Xdiamond:
4172     case Xdiamond_pause:
4173     case Xbomb:
4174     case Xbomb_pause:
4175     case Xballoon:
4176     case Xacid_ne:
4177     case Xacid_nw:
4178     case Xball_1:
4179     case Xball_2:
4180     case Xnut:
4181     case Xnut_pause:
4182     case Xslidewall_ns:
4183     case Xslidewall_ew:
4184     case Xkey_1:
4185     case Xkey_2:
4186     case Xkey_3:
4187     case Xkey_4:
4188     case Xkey_5:
4189     case Xkey_6:
4190     case Xkey_7:
4191     case Xkey_8:
4192     case Xbumper:
4193     case Xswitch:
4194     case Xroundwall_1:
4195     case Xroundwall_2:
4196     case Xroundwall_3:
4197     case Xroundwall_4:
4198       if (RANDOM(2))
4199       {
4200         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4201         {
4202           Cave[x][y] = Ybomb_eB;
4203           Next[x][y] = Xblank;
4204           Cave[x+1][y] = Ybomb_e;
4205           Next[x+1][y] = Xbomb_pause;
4206           return;
4207         }
4208
4209         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4210         {
4211           Cave[x][y] = Ybomb_wB;
4212           Next[x][y] = Xblank;
4213           Cave[x-1][y] = Ybomb_w;
4214           Next[x-1][y] = Xbomb_pause;
4215           return;
4216         }
4217       }
4218       else
4219       {
4220         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4221         {
4222           Cave[x][y] = Ybomb_wB;
4223           Next[x][y] = Xblank;
4224           Cave[x-1][y] = Ybomb_w;
4225           Next[x-1][y] = Xbomb_pause;
4226           return;
4227         }
4228
4229         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4230         {
4231           Cave[x][y] = Ybomb_eB;
4232           Next[x][y] = Xblank;
4233           Cave[x+1][y] = Ybomb_e;
4234           Next[x+1][y] = Xbomb_pause;
4235           return;
4236         }
4237       }
4238   }
4239 }
4240
4241 static void Lbomb_pause(int x, int y)
4242 {
4243   switch (Cave[x][y+1])
4244   {
4245     case Xblank:
4246     case Xacid_splash_e:
4247     case Xacid_splash_w:
4248       Cave[x][y] = Ybomb_sB;
4249       Next[x][y] = Xblank;
4250       Cave[x][y+1] = Ybomb_s;
4251       Next[x][y+1] = Xbomb_fall;
4252       return;
4253
4254     case Xacid_1:
4255     case Xacid_2:
4256     case Xacid_3:
4257     case Xacid_4:
4258     case Xacid_5:
4259     case Xacid_6:
4260     case Xacid_7:
4261     case Xacid_8:
4262       Cave[x][y] = Ybomb_sB;
4263       Next[x][y] = Xblank;
4264       if (Cave[x+1][y] == Xblank)
4265         Cave[x+1][y] = Xacid_splash_e;
4266       if (Cave[x-1][y] == Xblank)
4267         Cave[x-1][y] = Xacid_splash_w;
4268       play_element_sound(x, y, SOUND_acid, Xacid_1);
4269       return;
4270
4271     default:
4272       Cave[x][y] = Xbomb;
4273       Next[x][y] = Xbomb;
4274       return;
4275   }
4276 }
4277
4278 static void Lbomb_fall(int x, int y)
4279 {
4280   switch (Cave[x][y+1])
4281   {
4282     case Xblank:
4283     case Xacid_splash_e:
4284     case Xacid_splash_w:
4285       Cave[x][y] = Ybomb_sB;
4286       Next[x][y] = Xblank;
4287       Cave[x][y+1] = Ybomb_s;
4288       Next[x][y+1] = Xbomb_fall;
4289       return;
4290
4291     case Xacid_1:
4292     case Xacid_2:
4293     case Xacid_3:
4294     case Xacid_4:
4295     case Xacid_5:
4296     case Xacid_6:
4297     case Xacid_7:
4298     case Xacid_8:
4299       Cave[x][y] = Ybomb_sB;
4300       Next[x][y] = Xblank;
4301       if (Cave[x+1][y] == Xblank)
4302         Cave[x+1][y] = Xacid_splash_e;
4303       if (Cave[x-1][y] == Xblank)
4304         Cave[x-1][y] = Xacid_splash_w;
4305       play_element_sound(x, y, SOUND_acid, Xacid_1);
4306       return;
4307
4308     default:
4309       Cave[x][y] = Ybomb_blank;
4310       Lboom_tank(x, y, Xbomb_fall);
4311       return;
4312   }
4313 }
4314
4315 static void Lnut(int x, int y)
4316 {
4317   switch (Cave[x][y+1])
4318   {
4319     case Xblank:
4320     case Xacid_splash_e:
4321     case Xacid_splash_w:
4322       Cave[x][y] = Ynut_sB;
4323       Next[x][y] = Xblank;
4324       Cave[x][y+1] = Ynut_s;
4325       Next[x][y+1] = Xnut_fall;
4326       return;
4327
4328     case Xacid_1:
4329     case Xacid_2:
4330     case Xacid_3:
4331     case Xacid_4:
4332     case Xacid_5:
4333     case Xacid_6:
4334     case Xacid_7:
4335     case Xacid_8:
4336       Cave[x][y] = Ynut_sB;
4337       Next[x][y] = Xblank;
4338       if (Cave[x+1][y] == Xblank)
4339         Cave[x+1][y] = Xacid_splash_e;
4340       if (Cave[x-1][y] == Xblank)
4341         Cave[x-1][y] = Xacid_splash_w;
4342       play_element_sound(x, y, SOUND_acid, Xacid_1);
4343       return;
4344
4345     case Xspring:
4346     case Xspring_pause:
4347     case Xspring_e:
4348     case Xspring_w:
4349     case Xandroid:
4350     case Xandroid_1_n:
4351     case Xandroid_2_n:
4352     case Xandroid_1_e:
4353     case Xandroid_2_e:
4354     case Xandroid_1_s:
4355     case Xandroid_2_s:
4356     case Xandroid_1_w:
4357     case Xandroid_2_w:
4358     case Xstone:
4359     case Xstone_pause:
4360     case Xemerald:
4361     case Xemerald_pause:
4362     case Xdiamond:
4363     case Xdiamond_pause:
4364     case Xbomb:
4365     case Xbomb_pause:
4366     case Xballoon:
4367     case Xacid_ne:
4368     case Xacid_nw:
4369     case Xball_1:
4370     case Xball_2:
4371     case Xnut:
4372     case Xnut_pause:
4373     case Xslidewall_ns:
4374     case Xslidewall_ew:
4375     case Xkey_1:
4376     case Xkey_2:
4377     case Xkey_3:
4378     case Xkey_4:
4379     case Xkey_5:
4380     case Xkey_6:
4381     case Xkey_7:
4382     case Xkey_8:
4383     case Xbumper:
4384     case Xswitch:
4385     case Xroundwall_1:
4386     case Xroundwall_2:
4387     case Xroundwall_3:
4388     case Xroundwall_4:
4389       if (RANDOM(2))
4390       {
4391         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4392         {
4393           Cave[x][y] = Ynut_eB;
4394           Next[x][y] = Xblank;
4395           Cave[x+1][y] = Ynut_e;
4396           Next[x+1][y] = Xnut_pause;
4397           return;
4398         }
4399
4400         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4401         {
4402           Cave[x][y] = Ynut_wB;
4403           Next[x][y] = Xblank;
4404           Cave[x-1][y] = Ynut_w;
4405           Next[x-1][y] = Xnut_pause;
4406           return;
4407         }
4408       }
4409       else
4410       {
4411         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4412         {
4413           Cave[x][y] = Ynut_wB;
4414           Next[x][y] = Xblank;
4415           Cave[x-1][y] = Ynut_w;
4416           Next[x-1][y] = Xnut_pause;
4417           return;
4418         }
4419
4420         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4421         {
4422           Cave[x][y] = Ynut_eB;
4423           Next[x][y] = Xblank;
4424           Cave[x+1][y] = Ynut_e;
4425           Next[x+1][y] = Xnut_pause;
4426           return;
4427         }
4428       }
4429   }
4430 }
4431
4432 static void Lnut_pause(int x, int y)
4433 {
4434   switch (Cave[x][y+1])
4435   {
4436     case Xblank:
4437     case Xacid_splash_e:
4438     case Xacid_splash_w:
4439       Cave[x][y] = Ynut_sB;
4440       Next[x][y] = Xblank;
4441       Cave[x][y+1] = Ynut_s;
4442       Next[x][y+1] = Xnut_fall;
4443       return;
4444
4445     case Xacid_1:
4446     case Xacid_2:
4447     case Xacid_3:
4448     case Xacid_4:
4449     case Xacid_5:
4450     case Xacid_6:
4451     case Xacid_7:
4452     case Xacid_8:
4453       Cave[x][y] = Ynut_sB;
4454       Next[x][y] = Xblank;
4455       if (Cave[x+1][y] == Xblank)
4456         Cave[x+1][y] = Xacid_splash_e;
4457       if (Cave[x-1][y] == Xblank)
4458         Cave[x-1][y] = Xacid_splash_w;
4459       play_element_sound(x, y, SOUND_acid, Xacid_1);
4460       return;
4461
4462     default:
4463       Cave[x][y] = Xnut;
4464       Next[x][y] = Xnut;
4465       return;
4466   }
4467 }
4468
4469 static void Lnut_fall(int x, int y)
4470 {
4471   switch (Cave[x][y+1])
4472   {
4473     case Xblank:
4474     case Xacid_splash_e:
4475     case Xacid_splash_w:
4476     case Zplayer:
4477       Cave[x][y] = Ynut_sB;
4478       Next[x][y] = Xblank;
4479       Cave[x][y+1] = Ynut_s;
4480       Next[x][y+1] = Xnut_fall;
4481       return;
4482
4483     case Xacid_1:
4484     case Xacid_2:
4485     case Xacid_3:
4486     case Xacid_4:
4487     case Xacid_5:
4488     case Xacid_6:
4489     case Xacid_7:
4490     case Xacid_8:
4491       Cave[x][y] = Ynut_sB;
4492       Next[x][y] = Xblank;
4493       if (Cave[x+1][y] == Xblank)
4494         Cave[x+1][y] = Xacid_splash_e;
4495       if (Cave[x-1][y] == Xblank)
4496         Cave[x-1][y] = Xacid_splash_w;
4497       play_element_sound(x, y, SOUND_acid, Xacid_1);
4498       return;
4499
4500     default:
4501       Cave[x][y] = Xnut;
4502       Next[x][y] = Xnut;
4503       play_element_sound(x, y, SOUND_nut, Xnut);
4504       return;
4505   }
4506 }
4507
4508 static void Lspring(int x, int y)
4509 {
4510   switch (Cave[x][y+1])
4511   {
4512     case Xblank:
4513     case Xacid_splash_e:
4514     case Xacid_splash_w:
4515     case Xplant:
4516     case Yplant:
4517       Cave[x][y] = Yspring_sB;
4518       Next[x][y] = Xblank;
4519       Cave[x][y+1] = Yspring_s;
4520       Next[x][y+1] = Xspring_fall;
4521       return;
4522
4523     case Xacid_1:
4524     case Xacid_2:
4525     case Xacid_3:
4526     case Xacid_4:
4527     case Xacid_5:
4528     case Xacid_6:
4529     case Xacid_7:
4530     case Xacid_8:
4531       Cave[x][y] = Yspring_sB;
4532       Next[x][y] = Xblank;
4533       if (Cave[x+1][y] == Xblank)
4534         Cave[x+1][y] = Xacid_splash_e;
4535       if (Cave[x-1][y] == Xblank)
4536         Cave[x-1][y] = Xacid_splash_w;
4537       play_element_sound(x, y, SOUND_acid, Xacid_1);
4538       return;
4539
4540     case Xspring:
4541     case Xspring_pause:
4542     case Xspring_e:
4543     case Xspring_w:
4544     case Xandroid:
4545     case Xandroid_1_n:
4546     case Xandroid_2_n:
4547     case Xandroid_1_e:
4548     case Xandroid_2_e:
4549     case Xandroid_1_s:
4550     case Xandroid_2_s:
4551     case Xandroid_1_w:
4552     case Xandroid_2_w:
4553     case Xstone:
4554     case Xstone_pause:
4555     case Xemerald:
4556     case Xemerald_pause:
4557     case Xdiamond:
4558     case Xdiamond_pause:
4559     case Xbomb:
4560     case Xbomb_pause:
4561     case Xballoon:
4562     case Xacid_ne:
4563     case Xacid_nw:
4564     case Xball_1:
4565     case Xball_2:
4566     case Xnut:
4567     case Xnut_pause:
4568     case Xslidewall_ns:
4569     case Xslidewall_ew:
4570     case Xkey_1:
4571     case Xkey_2:
4572     case Xkey_3:
4573     case Xkey_4:
4574     case Xkey_5:
4575     case Xkey_6:
4576     case Xkey_7:
4577     case Xkey_8:
4578     case Xbumper:
4579     case Xswitch:
4580     case Xroundwall_1:
4581     case Xroundwall_2:
4582     case Xroundwall_3:
4583     case Xroundwall_4:
4584       if (RANDOM(2))
4585       {
4586         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4587         {
4588           Cave[x][y] = Yspring_eB;
4589           Next[x][y] = Xblank;
4590           Cave[x+1][y] = Yspring_e;
4591           if (Cave[x][y+1] == Xbumper)
4592             Cave[x][y+1] = XbumperB;
4593
4594 #ifdef SPRING_ROLL
4595           Next[x+1][y] = Xspring_e;
4596 #else
4597           Next[x+1][y] = Xspring_pause;
4598 #endif
4599           return;
4600         }
4601
4602         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4603         {
4604           Cave[x][y] = Yspring_wB;
4605           Next[x][y] = Xblank;
4606           Cave[x-1][y] = Yspring_w;
4607           if (Cave[x][y+1] == Xbumper)
4608             Cave[x][y+1] = XbumperB;
4609
4610 #ifdef SPRING_ROLL
4611           Next[x-1][y] = Xspring_w;
4612 #else
4613           Next[x-1][y] = Xspring_pause;
4614 #endif
4615           return;
4616         }
4617       }
4618       else
4619       {
4620         if (tab_blank[Cave[x-1][y]] && tab_acid[Cave[x-1][y+1]])
4621         {
4622           Cave[x][y] = Yspring_wB;
4623           Next[x][y] = Xblank;
4624           Cave[x-1][y] = Yspring_w;
4625           if (Cave[x][y+1] == Xbumper)
4626             Cave[x][y+1] = XbumperB;
4627
4628 #ifdef SPRING_ROLL
4629           Next[x-1][y] = Xspring_w;
4630 #else
4631           Next[x-1][y] = Xspring_pause;
4632 #endif
4633           return;
4634         }
4635
4636         if (tab_blank[Cave[x+1][y]] && tab_acid[Cave[x+1][y+1]])
4637         {
4638           Cave[x][y] = Yspring_eB;
4639           Next[x][y] = Xblank;
4640           Cave[x+1][y] = Yspring_e;
4641           if (Cave[x][y+1] == Xbumper)
4642             Cave[x][y+1] = XbumperB;
4643
4644 #ifdef SPRING_ROLL
4645           Next[x+1][y] = Xspring_e;
4646 #else
4647           Next[x+1][y] = Xspring_pause;
4648 #endif
4649           return;
4650         }
4651       }
4652   }
4653 }
4654
4655 static void Lspring_pause(int x, int y)
4656 {
4657   switch (Cave[x][y+1])
4658   {
4659     case Xblank:
4660     case Xacid_splash_e:
4661     case Xacid_splash_w:
4662       Cave[x][y] = Yspring_sB;
4663       Next[x][y] = Xblank;
4664       Cave[x][y+1] = Yspring_s;
4665       Next[x][y+1] = Xspring_fall;
4666       return;
4667
4668     case Xacid_1:
4669     case Xacid_2:
4670     case Xacid_3:
4671     case Xacid_4:
4672     case Xacid_5:
4673     case Xacid_6:
4674     case Xacid_7:
4675     case Xacid_8:
4676       Cave[x][y] = Yspring_sB;
4677       Next[x][y] = Xblank;
4678       if (Cave[x+1][y] == Xblank)
4679         Cave[x+1][y] = Xacid_splash_e;
4680       if (Cave[x-1][y] == Xblank)
4681         Cave[x-1][y] = Xacid_splash_w;
4682       play_element_sound(x, y, SOUND_acid, Xacid_1);
4683       return;
4684
4685     default:
4686       Cave[x][y] = Xspring;
4687       Next[x][y] = Xspring;
4688       return;
4689   }
4690 }
4691
4692 static void Lspring_e(int x, int y)
4693 {
4694   switch (Cave[x][y+1])
4695   {
4696     case Xblank:
4697     case Xacid_splash_e:
4698     case Xacid_splash_w:
4699       Cave[x][y] = Yspring_sB;
4700       Next[x][y] = Xblank;
4701       Cave[x][y+1] = Yspring_s;
4702       Next[x][y+1] = Xspring_fall;
4703       return;
4704
4705     case Xacid_1:
4706     case Xacid_2:
4707     case Xacid_3:
4708     case Xacid_4:
4709     case Xacid_5:
4710     case Xacid_6:
4711     case Xacid_7:
4712     case Xacid_8:
4713       Cave[x][y] = Yspring_sB;
4714       Next[x][y] = Xblank;
4715       if (Cave[x+1][y] == Xblank)
4716         Cave[x+1][y] = Xacid_splash_e;
4717       if (Cave[x-1][y] == Xblank)
4718         Cave[x-1][y] = Xacid_splash_w;
4719       play_element_sound(x, y, SOUND_acid, Xacid_1);
4720       return;
4721
4722     case Xbumper:
4723       Cave[x][y+1] = XbumperB;
4724   }
4725
4726   switch (Cave[x+1][y])
4727   {
4728     case Xblank:
4729     case Xacid_splash_e:
4730     case Xacid_splash_w:
4731     case Yalien_nB:
4732     case Yalien_eB:
4733     case Yalien_sB:
4734     case Yalien_wB:
4735       Cave[x][y] = Yspring_eB;
4736       Next[x][y] = Xblank;
4737       Cave[x+1][y] = Yspring_e;
4738       Next[x+1][y] = Xspring_e;
4739       return;
4740
4741     case Xacid_1:
4742     case Xacid_2:
4743     case Xacid_3:
4744     case Xacid_4:
4745     case Xacid_5:
4746     case Xacid_6:
4747     case Xacid_7:
4748     case Xacid_8:
4749       Cave[x][y] = Yspring_eB;
4750       Next[x][y] = Xblank;
4751       if (Cave[x+2][y-1] == Xblank)
4752         Cave[x+2][y-1] = Xacid_splash_e;
4753       if (Cave[x][y-1] == Xblank)
4754         Cave[x][y-1] = Xacid_splash_w;
4755       play_element_sound(x, y, SOUND_acid, Xacid_1);
4756       return;
4757
4758     case Xalien:
4759     case Xalien_pause:
4760     case Yalien_n:
4761     case Yalien_e:
4762     case Yalien_s:
4763     case Yalien_w:
4764       Cave[x][y] = Yspring_alien_eB;
4765       Next[x][y] = Xblank;
4766       Cave[x+1][y] = Yspring_alien_e;
4767       Next[x+1][y] = Xspring_e;
4768       play_element_sound(x, y, SOUND_slurp, Xalien);
4769       score += lev.slurp_score;
4770       return;
4771
4772     case Xbumper:
4773     case XbumperB:
4774       Cave[x+1][y] = XbumperB;
4775       Next[x][y] = Xspring_w;
4776       play_element_sound(x, y, SOUND_spring, Xspring);
4777       return;
4778
4779     default:
4780       Cave[x][y] = Xspring;
4781       Next[x][y] = Xspring;
4782       play_element_sound(x, y, SOUND_spring, Xspring);
4783       return;
4784   }
4785 }
4786
4787 static void Lspring_w(int x, int y)
4788 {
4789   switch (Cave[x][y+1])
4790   {
4791     case Xblank:
4792     case Xacid_splash_e:
4793     case Xacid_splash_w:
4794       Cave[x][y] = Yspring_sB;
4795       Next[x][y] = Xblank;
4796       Cave[x][y+1] = Yspring_s;
4797       Next[x][y+1] = Xspring_fall;
4798       return;
4799
4800     case Xacid_1:
4801     case Xacid_2:
4802     case Xacid_3:
4803     case Xacid_4:
4804     case Xacid_5:
4805     case Xacid_6:
4806     case Xacid_7:
4807     case Xacid_8:
4808       Cave[x][y] = Yspring_sB;
4809       Next[x][y] = Xblank;
4810       if (Cave[x+1][y] == Xblank)
4811         Cave[x+1][y] = Xacid_splash_e;
4812       if (Cave[x-1][y] == Xblank)
4813         Cave[x-1][y] = Xacid_splash_w;
4814       play_element_sound(x, y, SOUND_acid, Xacid_1);
4815       return;
4816
4817     case Xbumper:
4818       Cave[x][y+1] = XbumperB;
4819   }
4820
4821   switch (Cave[x-1][y])
4822   {
4823     case Xblank:
4824     case Xacid_splash_e:
4825     case Xacid_splash_w:
4826     case Yalien_nB:
4827     case Yalien_eB:
4828     case Yalien_sB:
4829     case Yalien_wB:
4830       Cave[x][y] = Yspring_wB;
4831       Next[x][y] = Xblank;
4832       Cave[x-1][y] = Yspring_w;
4833       Next[x-1][y] = Xspring_w;
4834       return;
4835
4836     case Xacid_1:
4837     case Xacid_2:
4838     case Xacid_3:
4839     case Xacid_4:
4840     case Xacid_5:
4841     case Xacid_6:
4842     case Xacid_7:
4843     case Xacid_8:
4844       Cave[x][y] = Yspring_wB;
4845       Next[x][y] = Xblank;
4846       if (Cave[x][y-1] == Xblank)
4847         Cave[x][y-1] = Xacid_splash_e;
4848       if (Cave[x-2][y-1] == Xblank)
4849         Cave[x-2][y-1] = Xacid_splash_w;
4850       play_element_sound(x, y, SOUND_acid, Xacid_1);
4851       return;
4852
4853     case Xalien:
4854     case Xalien_pause:
4855     case Yalien_n:
4856     case Yalien_e:
4857     case Yalien_s:
4858     case Yalien_w:
4859       Cave[x][y] = Yspring_alien_wB;
4860       Next[x][y] = Xblank;
4861       Cave[x-1][y] = Yspring_alien_w;
4862       Next[x-1][y] = Xspring_w;
4863       play_element_sound(x, y, SOUND_slurp, Xalien);
4864       score += lev.slurp_score;
4865       return;
4866
4867     case Xbumper:
4868     case XbumperB:
4869       Cave[x-1][y] = XbumperB;
4870       Next[x][y] = Xspring_e;
4871       play_element_sound(x, y, SOUND_spring, Xspring);
4872       return;
4873
4874     default:
4875       Cave[x][y] = Xspring;
4876       Next[x][y] = Xspring;
4877       play_element_sound(x, y, SOUND_spring, Xspring);
4878       return;
4879   }
4880 }
4881
4882 static void Lspring_fall(int x, int y)
4883 {
4884   switch (Cave[x][y+1])
4885   {
4886     case Xblank:
4887     case Xacid_splash_e:
4888     case Xacid_splash_w:
4889     case Zplayer:
4890       Cave[x][y] = Yspring_sB;
4891       Next[x][y] = Xblank;
4892       Cave[x][y+1] = Yspring_s;
4893       Next[x][y+1] = Xspring_fall;
4894       return;
4895
4896     case Xacid_1:
4897     case Xacid_2:
4898     case Xacid_3:
4899     case Xacid_4:
4900     case Xacid_5:
4901     case Xacid_6:
4902     case Xacid_7:
4903     case Xacid_8:
4904       Cave[x][y] = Yspring_sB;
4905       Next[x][y] = Xblank;
4906       if (Cave[x+1][y] == Xblank)
4907         Cave[x+1][y] = Xacid_splash_e;
4908       if (Cave[x-1][y] == Xblank)
4909         Cave[x-1][y] = Xacid_splash_w;
4910       play_element_sound(x, y, SOUND_acid, Xacid_1);
4911       return;
4912
4913     case Xbomb:
4914     case Xbomb_pause:
4915       Cave[x][y+1] = Ybomb_blank;
4916       Lboom_tank(x, y+1, Xspring_fall);
4917       return;
4918
4919     case Xbug_1_n:
4920     case Xbug_1_e:
4921     case Xbug_1_s:
4922     case Xbug_1_w:
4923     case Xbug_2_n:
4924     case Xbug_2_e:
4925     case Xbug_2_s:
4926     case Xbug_2_w:
4927       Cave[x][y] = Yspring_sB;
4928       Cave[x][y+1] = Ybug_spring;
4929       Lboom_bug(x, y+1, Xspring_fall);
4930       score += lev.bug_score;
4931       return;
4932
4933     case Xtank_1_n:
4934     case Xtank_1_e:
4935     case Xtank_1_s:
4936     case Xtank_1_w:
4937     case Xtank_2_n:
4938     case Xtank_2_e:
4939     case Xtank_2_s:
4940     case Xtank_2_w:
4941       Cave[x][y] = Yspring_sB;
4942       Cave[x][y+1] = Ytank_spring;
4943       Lboom_tank(x, y+1, Xspring_fall);
4944       score += lev.tank_score;
4945       return;
4946
4947     case Xeater_n:
4948     case Xeater_e:
4949     case Xeater_s:
4950     case Xeater_w:
4951       Cave[x][y] = Yspring_sB;
4952       Cave[x][y+1] = Yeater_spring;
4953       Lboom_eater(x, y+1, Xspring_fall);
4954       score += lev.eater_score;
4955       return;
4956
4957     case Xalien:
4958     case Xalien_pause:
4959       Cave[x][y] = Yspring_sB;
4960       Cave[x][y+1] = Yalien_spring;
4961       Lboom_tank(x, y+1, Xspring_fall);
4962       score += lev.alien_score;
4963       return;
4964
4965     default:
4966       Cave[x][y] = Xspring;
4967       Next[x][y] = Xspring;
4968       play_element_sound(x, y, SOUND_spring, Xspring);
4969       return;
4970   }
4971 }
4972
4973 static void Lpush_emerald_e(int x, int y)
4974 {
4975   switch (Cave[x+1][y])
4976   {
4977     case Zborder:
4978     case Znormal:
4979     case Zdynamite:
4980     case Xboom_bug:
4981     case Xboom_bomb:
4982     case Xboom_android:
4983     case Xboom_1:
4984     case Zplayer:
4985       Cave[x][y] = Xemerald;
4986       Next[x][y] = Xemerald;
4987       return;
4988
4989     default:
4990       Cave[x][y] = Yemerald_eB;
4991       Next[x][y] = Xblank;
4992       Cave[x+1][y] = Yemerald_e;
4993       Next[x+1][y] = Xemerald_pause;
4994       return;
4995   }
4996 }
4997
4998 static void Lpush_emerald_w(int x, int y)
4999 {
5000   switch (Cave[x-1][y])
5001   {
5002     case Zborder:
5003     case Znormal:
5004     case Zdynamite:
5005     case Xboom_bug:
5006     case Xboom_bomb:
5007     case Xboom_android:
5008     case Xboom_1:
5009     case Zplayer:
5010       Cave[x][y] = Xemerald;
5011       Next[x][y] = Xemerald;
5012       return;
5013
5014     default:
5015       Cave[x][y] = Yemerald_wB;
5016       Next[x][y] = Xblank;
5017       Cave[x-1][y] = Yemerald_w;
5018       Next[x-1][y] = Xemerald_pause;
5019       return;
5020   }
5021 }
5022
5023 static void Lpush_diamond_e(int x, int y)
5024 {
5025   switch (Cave[x+1][y])
5026   {
5027     case Zborder:
5028     case Znormal:
5029     case Zdynamite:
5030     case Xboom_bug:
5031     case Xboom_bomb:
5032     case Xboom_android:
5033     case Xboom_1:
5034     case Zplayer:
5035       Cave[x][y] = Xdiamond;
5036       Next[x][y] = Xdiamond;
5037       return;
5038
5039     default:
5040       Cave[x][y] = Ydiamond_eB;
5041       Next[x][y] = Xblank;
5042       Cave[x+1][y] = Ydiamond_e;
5043       Next[x+1][y] = Xdiamond_pause;
5044       return;
5045   }
5046 }
5047
5048 static void Lpush_diamond_w(int x, int y)
5049 {
5050   switch (Cave[x-1][y])
5051   {
5052     case Zborder:
5053     case Znormal:
5054     case Zdynamite:
5055     case Xboom_bug:
5056     case Xboom_bomb:
5057     case Xboom_android:
5058     case Xboom_1:
5059     case Zplayer:
5060       Cave[x][y] = Xdiamond;
5061       Next[x][y] = Xdiamond;
5062       return;
5063
5064     default:
5065       Cave[x][y] = Ydiamond_wB;
5066       Next[x][y] = Xblank;
5067       Cave[x-1][y] = Ydiamond_w;
5068       Next[x-1][y] = Xdiamond_pause;
5069       return;
5070   }
5071 }
5072
5073 static void Lpush_stone_e(int x, int y)
5074 {
5075   switch (Cave[x+1][y])
5076   {
5077     case Zborder:
5078     case Znormal:
5079     case Zdynamite:
5080     case Xboom_bug:
5081     case Xboom_bomb:
5082     case Xboom_android:
5083     case Xboom_1:
5084     case Zplayer:
5085       Cave[x][y] = Xstone;
5086       Next[x][y] = Xstone;
5087       return;
5088
5089     default:
5090       Cave[x][y] = Ystone_eB;
5091       Next[x][y] = Xblank;
5092       Cave[x+1][y] = Ystone_e;
5093       Next[x+1][y] = Xstone_pause;
5094       return;
5095   }
5096 }
5097
5098 static void Lpush_stone_w(int x, int y)
5099 {
5100   switch (Cave[x-1][y])
5101   {
5102     case Zborder:
5103     case Znormal:
5104     case Zdynamite:
5105     case Xboom_bug:
5106     case Xboom_bomb:
5107     case Xboom_android:
5108     case Xboom_1:
5109     case Zplayer:
5110       Cave[x][y] = Xstone;
5111       Next[x][y] = Xstone;
5112       return;
5113
5114     default:
5115       Cave[x][y] = Ystone_wB;
5116       Next[x][y] = Xblank;
5117       Cave[x-1][y] = Ystone_w;
5118       Next[x-1][y] = Xstone_pause;
5119       return;
5120   }
5121 }
5122
5123 static void Lpush_bomb_e(int x, int y)
5124 {
5125   switch (Cave[x+1][y])
5126   {
5127     case Zborder:
5128     case Znormal:
5129     case Zdynamite:
5130     case Xboom_bug:
5131     case Xboom_bomb:
5132     case Xboom_android:
5133     case Xboom_1:
5134     case Zplayer:
5135       Cave[x][y] = Xbomb;
5136       Next[x][y] = Xbomb;
5137       return;
5138
5139     default:
5140       Cave[x][y] = Ybomb_eB;
5141       Next[x][y] = Xblank;
5142       Cave[x+1][y] = Ybomb_e;
5143       Next[x+1][y] = Xbomb_pause;
5144       return;
5145   }
5146 }
5147
5148 static void Lpush_bomb_w(int x, int y)
5149 {
5150   switch (Cave[x-1][y])
5151   {
5152     case Zborder:
5153     case Znormal:
5154     case Zdynamite:
5155     case Xboom_bug:
5156     case Xboom_bomb:
5157     case Xboom_android:
5158     case Xboom_1:
5159     case Zplayer:
5160       Cave[x][y] = Xbomb;
5161       Next[x][y] = Xbomb;
5162       return;
5163
5164     default:
5165       Cave[x][y] = Ybomb_wB;
5166       Next[x][y] = Xblank;
5167       Cave[x-1][y] = Ybomb_w;
5168       Next[x-1][y] = Xbomb_pause;
5169       return;
5170   }
5171 }
5172
5173 static void Lpush_nut_e(int x, int y)
5174 {
5175   switch (Cave[x+1][y])
5176   {
5177     case Zborder:
5178     case Znormal:
5179     case Zdynamite:
5180     case Xboom_bug:
5181     case Xboom_bomb:
5182     case Xboom_android:
5183     case Xboom_1:
5184     case Zplayer:
5185       Cave[x][y] = Xnut;
5186       Next[x][y] = Xnut;
5187       return;
5188
5189     default:
5190       Cave[x][y] = Ynut_eB;
5191       Next[x][y] = Xblank;
5192       Cave[x+1][y] = Ynut_e;
5193       Next[x+1][y] = Xnut_pause;
5194       return;
5195   }
5196 }
5197
5198 static void Lpush_nut_w(int x, int y)
5199 {
5200   switch (Cave[x-1][y])
5201   {
5202     case Zborder:
5203     case Znormal:
5204     case Zdynamite:
5205     case Xboom_bug:
5206     case Xboom_bomb:
5207     case Xboom_android:
5208     case Xboom_1:
5209     case Zplayer:
5210       Cave[x][y] = Xnut;
5211       Next[x][y] = Xnut;
5212       return;
5213
5214     default:
5215       Cave[x][y] = Ynut_wB;
5216       Next[x][y] = Xblank;
5217       Cave[x-1][y] = Ynut_w;
5218       Next[x-1][y] = Xnut_pause;
5219       return;
5220   }
5221 }
5222
5223 static void Lpush_spring_e(int x, int y)
5224 {
5225   switch (Cave[x+1][y])
5226   {
5227     case Zborder:
5228     case Znormal:
5229     case Zdynamite:
5230     case Xboom_bug:
5231     case Xboom_bomb:
5232     case Xboom_android:
5233     case Xboom_1:
5234     case Zplayer:
5235       Cave[x][y] = Xspring;
5236       Next[x][y] = Xspring;
5237       return;
5238
5239     default:
5240       Cave[x][y] = Yspring_eB;
5241       Next[x][y] = Xblank;
5242       Cave[x+1][y] = Yspring_e;
5243       Next[x+1][y] = Xspring_e;
5244       return;
5245   }
5246 }
5247
5248 static void Lpush_spring_w(int x, int y)
5249 {
5250   switch (Cave[x-1][y])
5251   {
5252     case Zborder:
5253     case Znormal:
5254     case Zdynamite:
5255     case Xboom_bug:
5256     case Xboom_bomb:
5257     case Xboom_android:
5258     case Xboom_1:
5259     case Zplayer:
5260       Cave[x][y] = Xspring;
5261       Next[x][y] = Xspring;
5262       return;
5263
5264     default:
5265       Cave[x][y] = Yspring_wB;
5266       Next[x][y] = Xblank;
5267       Cave[x-1][y] = Yspring_w;
5268       Next[x-1][y] = Xspring_w;
5269       return;
5270   }
5271 }
5272
5273 static void Ldynamite_1(int x, int y)
5274 {
5275   play_element_sound(x, y, SOUND_tick, Xdynamite_1);
5276   Next[x][y] = Xdynamite_2;
5277 }
5278
5279 static void Ldynamite_2(int x, int y)
5280 {
5281   play_element_sound(x, y, SOUND_tick, Xdynamite_2);
5282   Next[x][y] = Xdynamite_3;
5283 }
5284
5285 static void Ldynamite_3(int x, int y)
5286 {
5287   play_element_sound(x, y, SOUND_tick, Xdynamite_3);
5288   Next[x][y] = Xdynamite_4;
5289 }
5290
5291 static void Ldynamite_4(int x, int y)
5292 {
5293   play_element_sound(x, y, SOUND_tick, Xdynamite_4);
5294   Next[x][y] = Zdynamite;
5295
5296   Lboom_generic(x, y, Xblank, Xblank);
5297 }
5298
5299 static void Lfake_door_1(int x, int y)
5300 {
5301   if (lev.magnify_cnt)
5302     Cave[x][y] = Xdoor_1;
5303 }
5304
5305 static void Lfake_door_2(int x, int y)
5306 {
5307   if (lev.magnify_cnt)
5308     Cave[x][y] = Xdoor_2;
5309 }
5310
5311 static void Lfake_door_3(int x, int y)
5312 {
5313   if (lev.magnify_cnt)
5314     Cave[x][y] = Xdoor_3;
5315 }
5316
5317 static void Lfake_door_4(int x, int y)
5318 {
5319   if (lev.magnify_cnt)
5320     Cave[x][y] = Xdoor_4;
5321 }
5322
5323 static void Lfake_door_5(int x, int y)
5324 {
5325   if (lev.magnify_cnt)
5326     Cave[x][y] = Xdoor_5;
5327 }
5328
5329 static void Lfake_door_6(int x, int y)
5330 {
5331   if (lev.magnify_cnt)
5332     Cave[x][y] = Xdoor_6;
5333 }
5334
5335 static void Lfake_door_7(int x, int y)
5336 {
5337   if (lev.magnify_cnt)
5338     Cave[x][y] = Xdoor_7;
5339 }
5340
5341 static void Lfake_door_8(int x, int y)
5342 {
5343   if (lev.magnify_cnt)
5344     Cave[x][y] = Xdoor_8;
5345 }
5346
5347 static void Lballoon(int x, int y)
5348 {
5349   if (lev.wind_cnt == 0)
5350     return;
5351
5352   switch (lev.wind_direction)
5353   {
5354     case 0: /* north */
5355       switch (Cave[x][y-1])
5356       {
5357         case Xblank:
5358         case Xacid_splash_e:
5359         case Xacid_splash_w:
5360           Cave[x][y] = Yballoon_nB;
5361           Next[x][y] = Xblank;
5362           Cave[x][y-1] = Yballoon_n;
5363           Next[x][y-1] = Xballoon;
5364           return;
5365
5366         case Xacid_1:
5367         case Xacid_2:
5368         case Xacid_3:
5369         case Xacid_4:
5370         case Xacid_5:
5371         case Xacid_6:
5372         case Xacid_7:
5373         case Xacid_8:
5374           Cave[x][y] = Yballoon_nB;
5375           Next[x][y] = Xblank;
5376           if (Cave[x+1][y-2] == Xblank)
5377             Cave[x+1][y-2] = Xacid_splash_e;
5378           if (Cave[x-1][y-2] == Xblank)
5379             Cave[x-1][y-2] = Xacid_splash_w;
5380           play_element_sound(x, y, SOUND_acid, Xacid_1);
5381           return;
5382       }
5383       break;
5384
5385     case 1: /* east */
5386       switch (Cave[x+1][y])
5387       {
5388         case Xblank:
5389         case Xacid_splash_e:
5390         case Xacid_splash_w:
5391           Cave[x][y] = Yballoon_eB;
5392           Next[x][y] = Xblank;
5393           Cave[x+1][y] = Yballoon_e;
5394           Next[x+1][y] = Xballoon;
5395           return;
5396
5397         case Xacid_1:
5398         case Xacid_2:
5399         case Xacid_3:
5400         case Xacid_4:
5401         case Xacid_5:
5402         case Xacid_6:
5403         case Xacid_7:
5404         case Xacid_8:
5405           Cave[x][y] = Yballoon_eB;
5406           Next[x][y] = Xblank;
5407           if (Cave[x+2][y-1] == Xblank)
5408             Cave[x+2][y-1] = Xacid_splash_e;
5409           if (Cave[x][y-1] == Xblank)
5410             Cave[x][y-1] = Xacid_splash_w;
5411           play_element_sound(x, y, SOUND_acid, Xacid_1);
5412           return;
5413       }
5414       break;
5415
5416     case 2: /* south */
5417       switch (Cave[x][y+1])
5418       {
5419         case Xblank:
5420         case Xacid_splash_e:
5421         case Xacid_splash_w:
5422           Cave[x][y] = Yballoon_sB;
5423           Next[x][y] = Xblank;
5424           Cave[x][y+1] = Yballoon_s;
5425           Next[x][y+1] = Xballoon;
5426           return;
5427
5428         case Xacid_1:
5429         case Xacid_2:
5430         case Xacid_3:
5431         case Xacid_4:
5432         case Xacid_5:
5433         case Xacid_6:
5434         case Xacid_7:
5435         case Xacid_8:
5436           Cave[x][y] = Yballoon_sB;
5437           Next[x][y] = Xblank;
5438           if (Cave[x+1][y] == Xblank)
5439             Cave[x+1][y] = Xacid_splash_e;
5440           if (Cave[x-1][y] == Xblank)
5441             Cave[x-1][y] = Xacid_splash_w;
5442           play_element_sound(x, y, SOUND_acid, Xacid_1);
5443           return;
5444       }
5445       break;
5446
5447     case 3: /* west */
5448       switch (Cave[x-1][y])
5449       {
5450         case Xblank:
5451         case Xacid_splash_e:
5452         case Xacid_splash_w:
5453           Cave[x][y] = Yballoon_wB;
5454           Next[x][y] = Xblank;
5455           Cave[x-1][y] = Yballoon_w;
5456           Next[x-1][y] = Xballoon;
5457           return;
5458
5459         case Xacid_1:
5460         case Xacid_2:
5461         case Xacid_3:
5462         case Xacid_4:
5463         case Xacid_5:
5464         case Xacid_6:
5465         case Xacid_7:
5466         case Xacid_8:
5467           Cave[x][y] = Yballoon_wB;
5468           Next[x][y] = Xblank;
5469           if (Cave[x][y-1] == Xblank)
5470             Cave[x][y-1] = Xacid_splash_e;
5471           if (Cave[x-2][y-1] == Xblank)
5472             Cave[x-2][y-1] = Xacid_splash_w;
5473           play_element_sound(x, y, SOUND_acid, Xacid_1);
5474           return;
5475       }
5476       break;
5477   }
5478 }
5479
5480 static void Lball_common(int x, int y)
5481 {
5482   play_element_sound(x, y, SOUND_ball, Xball_1);
5483
5484   if (lev.ball_random)
5485   {
5486     switch (RANDOM(8))
5487     {
5488       case 0:
5489         if (lev.ball_array[lev.ball_pos][0] != Xblank &&
5490             tab_blank[Cave[x-1][y-1]])
5491         {
5492           Cave[x-1][y-1] = Yball_blank;
5493           Next[x-1][y-1] = lev.ball_array[lev.ball_pos][0];
5494         }
5495         break;
5496
5497       case 1:
5498         if (lev.ball_array[lev.ball_pos][1] != Xblank &&
5499             tab_blank[Cave[x][y-1]])
5500         {
5501           Cave[x][y-1] = Yball_blank;
5502           Next[x][y-1] = lev.ball_array[lev.ball_pos][1];
5503         }
5504         break;
5505
5506       case 2:
5507         if (lev.ball_array[lev.ball_pos][2] != Xblank &&
5508             tab_blank[Cave[x+1][y-1]])
5509         {
5510           Cave[x+1][y-1] = Yball_blank;
5511           Next[x+1][y-1] = lev.ball_array[lev.ball_pos][2];
5512         }
5513         break;
5514
5515       case 3:
5516         if (lev.ball_array[lev.ball_pos][3] != Xblank &&
5517             tab_blank[Cave[x-1][y]])
5518         {
5519           Cave[x-1][y] = Yball_blank;
5520           Next[x-1][y] = lev.ball_array[lev.ball_pos][3];
5521         }
5522         break;
5523
5524       case 4:
5525         if (lev.ball_array[lev.ball_pos][4] != Xblank &&
5526             tab_blank[Cave[x+1][y]])
5527         {
5528           Cave[x+1][y] = Yball_blank;
5529           Next[x+1][y] = lev.ball_array[lev.ball_pos][4];
5530         }
5531         break;
5532
5533       case 5:
5534         if (lev.ball_array[lev.ball_pos][5] != Xblank &&
5535             tab_blank[Cave[x-1][y+1]])
5536         {
5537           Cave[x-1][y+1] = Yball_blank;
5538           Next[x-1][y+1] = lev.ball_array[lev.ball_pos][5];
5539         }
5540         break;
5541
5542       case 6:
5543         if (lev.ball_array[lev.ball_pos][6] != Xblank &&
5544             tab_blank[Cave[x][y+1]])
5545         {
5546           Cave[x][y+1] = Yball_blank;
5547           Next[x][y+1] = lev.ball_array[lev.ball_pos][6];
5548         }
5549         break;
5550
5551       case 7:
5552         if (lev.ball_array[lev.ball_pos][7] != Xblank &&
5553             tab_blank[Cave[x+1][y+1]])
5554         {
5555           Cave[x+1][y+1] = Yball_blank;
5556           Next[x+1][y+1] = lev.ball_array[lev.ball_pos][7];
5557         }
5558         break;
5559     }
5560   }
5561   else
5562   {
5563     if (lev.ball_array[lev.ball_pos][0] != Xblank &&
5564         tab_blank[Cave[x-1][y-1]])
5565     {
5566       Cave[x-1][y-1] = Yball_blank;
5567       Next[x-1][y-1] = lev.ball_array[lev.ball_pos][0];
5568     }
5569
5570     if (lev.ball_array[lev.ball_pos][1] != Xblank &&
5571         tab_blank[Cave[x][y-1]])
5572     {
5573       Cave[x][y-1] = Yball_blank;
5574       Next[x][y-1] = lev.ball_array[lev.ball_pos][1];
5575     }
5576
5577     if (lev.ball_array[lev.ball_pos][2] != Xblank &&
5578         tab_blank[Cave[x+1][y-1]])
5579     {
5580       Cave[x+1][y-1] = Yball_blank;
5581       Next[x+1][y-1] = lev.ball_array[lev.ball_pos][2];
5582     }
5583
5584     if (lev.ball_array[lev.ball_pos][3] != Xblank &&
5585         tab_blank[Cave[x-1][y]])
5586     {
5587       Cave[x-1][y] = Yball_blank;
5588       Next[x-1][y] = lev.ball_array[lev.ball_pos][3];
5589     }
5590
5591     if (lev.ball_array[lev.ball_pos][4] != Xblank &&
5592         tab_blank[Cave[x+1][y]])
5593     {
5594       Cave[x+1][y] = Yball_blank;
5595       Next[x+1][y] = lev.ball_array[lev.ball_pos][4];
5596     }
5597
5598     if (lev.ball_array[lev.ball_pos][5] != Xblank &&
5599         tab_blank[Cave[x-1][y+1]])
5600     {
5601       Cave[x-1][y+1] = Yball_blank;
5602       Next[x-1][y+1] = lev.ball_array[lev.ball_pos][5];
5603     }
5604
5605     if (lev.ball_array[lev.ball_pos][6] != Xblank &&
5606         tab_blank[Cave[x][y+1]])
5607     {
5608       Cave[x][y+1] = Yball_blank;
5609       Next[x][y+1] = lev.ball_array[lev.ball_pos][6];
5610     }
5611
5612     if (lev.ball_array[lev.ball_pos][7] != Xblank &&
5613         tab_blank[Cave[x+1][y+1]])
5614     {
5615       Cave[x+1][y+1] = Yball_blank;
5616       Next[x+1][y+1] = lev.ball_array[lev.ball_pos][7];
5617     }
5618   }
5619
5620   lev.ball_pos = (lev.ball_pos + 1) % lev.num_ball_arrays;
5621 }
5622
5623 static void Lball_1(int x, int y)
5624 {
5625   if (lev.ball_state == 0)
5626     return;
5627
5628   Cave[x][y] = Yball_1;
5629   Next[x][y] = Xball_2;
5630   if (lev.ball_cnt)
5631     return;
5632
5633   Lball_common(x, y);
5634 }
5635
5636 static void Lball_2(int x, int y)
5637 {
5638   if (lev.ball_state == 0)
5639     return;
5640
5641   Cave[x][y] = Yball_2;
5642   Next[x][y] = Xball_1;
5643   if (lev.ball_cnt)
5644     return;
5645
5646   Lball_common(x, y);
5647 }
5648
5649 static void Ldrip(int x, int y)
5650 {
5651   Next[x][y] = Xdrip_fall;
5652 }
5653
5654 static void Ldrip_fall(int x, int y)
5655 {
5656   int temp;
5657
5658   switch (Cave[x][y+1])
5659   {
5660     case Xblank:
5661     case Xacid_splash_e:
5662     case Xacid_splash_w:
5663     case Xplant:
5664     case Yplant:
5665     case Zplayer:
5666       Cave[x][y] = Ydrip_1_sB;
5667       Next[x][y] = Xdrip_stretchB;
5668       Cave[x][y+1] = Ydrip_1_s;
5669       Next[x][y+1] = Xdrip_stretch;
5670       return;
5671
5672     case Xacid_1:
5673     case Xacid_2:
5674     case Xacid_3:
5675     case Xacid_4:
5676     case Xacid_5:
5677     case Xacid_6:
5678     case Xacid_7:
5679     case Xacid_8:
5680       Cave[x][y] = Ydrip_1_sB;
5681       Next[x][y] = Xdrip_stretchB;
5682       if (Cave[x+1][y] == Xblank)
5683         Cave[x+1][y] = Xacid_splash_e;
5684       if (Cave[x-1][y] == Xblank)
5685         Cave[x-1][y] = Xacid_splash_w;
5686       play_element_sound(x, y, SOUND_acid, Xacid_1);
5687       return;
5688
5689     default:
5690       switch (RANDOM(8))
5691       {
5692         case 0: temp = Xamoeba_1; break;
5693         case 1: temp = Xamoeba_2; break;
5694         case 2: temp = Xamoeba_3; break;
5695         case 3: temp = Xamoeba_4; break;
5696         case 4: temp = Xamoeba_5; break;
5697         case 5: temp = Xamoeba_6; break;
5698         case 6: temp = Xamoeba_7; break;
5699         case 7: temp = Xamoeba_8; break;
5700       }
5701
5702       Cave[x][y] = temp;
5703       Next[x][y] = temp;
5704       play_element_sound(x, y, SOUND_drip, Xdrip_fall);
5705       return;
5706   }
5707 }
5708
5709 static void Ldrip_stretch(int x, int y)
5710 {
5711   Cave[x][y] = Ydrip_2_s;
5712   Next[x][y] = Xdrip_fall;
5713 }
5714
5715 static void Ldrip_stretchB(int x, int y)
5716 {
5717   Cave[x][y] = Ydrip_2_sB;
5718   Next[x][y] = Xblank;
5719 }
5720
5721 static void Lwonderwall(int x, int y)
5722 {
5723   if (lev.wonderwall_time && lev.wonderwall_state)
5724   {
5725     Cave[x][y] = XwonderwallB;
5726     play_element_sound(x, y, SOUND_wonder, Xwonderwall);
5727   }
5728 }
5729
5730 static void Lwheel(int x, int y)
5731 {
5732   if (lev.wheel_cnt && x == lev.wheel_x && y == lev.wheel_y)
5733     Cave[x][y] = XwheelB;
5734 }
5735
5736 static void Lswitch(int x, int y)
5737 {
5738   if (lev.ball_state)
5739     Cave[x][y] = XswitchB;
5740 }
5741
5742 static void Lfake_blank(int x, int y)
5743 {
5744   if (lev.lenses_cnt)
5745     Cave[x][y] = Xfake_blankB;
5746 }
5747
5748 static void Lfake_grass(int x, int y)
5749 {
5750   if (lev.magnify_cnt)
5751     Cave[x][y] = Xfake_grassB;
5752 }
5753
5754 static void Lfake_amoeba(int x, int y)
5755 {
5756   if (lev.lenses_cnt)
5757     Cave[x][y] = Xfake_amoebaB;
5758 }
5759
5760 static void Lsand_stone(int x, int y)
5761 {
5762   switch (Cave[x][y+1])
5763   {
5764     case Xblank:
5765     case Xacid_splash_e:
5766     case Xacid_splash_w:
5767       Cave[x][y] = Xsand_stonesand_quickout_1;
5768       Next[x][y] = Xsand_stonesand_quickout_2;
5769       Cave[x][y+1] = Xsand_stoneout_1;
5770       Next[x][y+1] = Xsand_stoneout_2;
5771       return;
5772
5773     case Xacid_1:
5774     case Xacid_2:
5775     case Xacid_3:
5776     case Xacid_4:
5777     case Xacid_5:
5778     case Xacid_6:
5779     case Xacid_7:
5780     case Xacid_8:
5781       Cave[x][y] = Xsand_stonesand_quickout_1;
5782       Next[x][y] = Xsand_stonesand_quickout_2;
5783       if (Cave[x+1][y] == Xblank)
5784         Cave[x+1][y] = Xacid_splash_e;
5785       if (Cave[x-1][y] == Xblank)
5786         Cave[x-1][y] = Xacid_splash_w;
5787       play_element_sound(x, y, SOUND_acid, Xacid_1);
5788       return;
5789
5790     case Xsand:
5791       Cave[x][y] = Xsand_stonesand_1;
5792       Next[x][y] = Xsand_stonesand_2;
5793       Cave[x][y+1] = Xsand_sandstone_1;
5794       Next[x][y+1] = Xsand_sandstone_2;
5795       return;
5796   }
5797 }
5798
5799 static void Lsand_stonein_1(int x, int y)
5800 {
5801   Next[x][y] = Xsand_stonein_2;
5802 }
5803
5804 static void Lsand_stonein_2(int x, int y)
5805 {
5806   Next[x][y] = Xsand_stonein_3;
5807 }
5808
5809 static void Lsand_stonein_3(int x, int y)
5810 {
5811   Next[x][y] = Xsand_stonein_4;
5812 }
5813
5814 static void Lsand_stonein_4(int x, int y)
5815 {
5816   Next[x][y] = Xblank;
5817 }
5818
5819 static void Lsand_sandstone_1(int x, int y)
5820 {
5821   Next[x][y] = Xsand_sandstone_2;
5822 }
5823
5824 static void Lsand_sandstone_2(int x, int y)
5825 {
5826   Next[x][y] = Xsand_sandstone_3;
5827 }
5828
5829 static void Lsand_sandstone_3(int x, int y)
5830 {
5831   Next[x][y] = Xsand_sandstone_4;
5832 }
5833
5834 static void Lsand_sandstone_4(int x, int y)
5835 {
5836   Next[x][y] = Xsand_stone;
5837 }
5838
5839 static void Lsand_stonesand_1(int x, int y)
5840 {
5841   Next[x][y] = Xsand_stonesand_2;
5842 }
5843
5844 static void Lsand_stonesand_2(int x, int y)
5845 {
5846   Next[x][y] = Xsand_stonesand_3;
5847 }
5848
5849 static void Lsand_stonesand_3(int x, int y)
5850 {
5851   Next[x][y] = Xsand_stonesand_4;
5852 }
5853
5854 static void Lsand_stonesand_4(int x, int y)
5855 {
5856   Next[x][y] = Xsand;
5857 }
5858
5859 static void Lsand_stoneout_1(int x, int y)
5860 {
5861   Next[x][y] = Xsand_stoneout_2;
5862 }
5863
5864 static void Lsand_stoneout_2(int x, int y)
5865 {
5866   Next[x][y] = Xstone_fall;
5867 }
5868
5869 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
5870 static void Lsand_stonesand_quickout_1(int x, int y)
5871 {
5872   Next[x][y] = Xsand_stonesand_quickout_2;
5873 }
5874
5875 static void Lsand_stonesand_quickout_2(int x, int y)
5876 {
5877   Next[x][y] = Xsand;
5878 }
5879 #endif
5880
5881 static void Lslidewall_ns(int x, int y)
5882 {
5883   if (tab_blank[Cave[x][y-1]])
5884   {
5885     Cave[x][y-1] = Yslidewall_ns_blank;
5886     Next[x][y-1] = Xslidewall_ns;
5887     play_element_sound(x, y, SOUND_slidewall, Xslidewall_ns);
5888   }
5889
5890   if (tab_blank[Cave[x][y+1]])
5891   {
5892     Cave[x][y+1] = Yslidewall_ns_blank;
5893     Next[x][y+1] = Xslidewall_ns;
5894     play_element_sound(x, y, SOUND_slidewall, Xslidewall_ns);
5895   }
5896 }
5897
5898 static void Lslidewall_ew(int x, int y)
5899 {
5900   if (tab_blank[Cave[x+1][y]])
5901   {
5902     Cave[x+1][y] = Yslidewall_ew_blank;
5903     Next[x+1][y] = Xslidewall_ew;
5904     play_element_sound(x, y, SOUND_slidewall, Xslidewall_ew);
5905   }
5906
5907   if (tab_blank[Cave[x-1][y]])
5908   {
5909     Cave[x-1][y] = Yslidewall_ew_blank;
5910     Next[x-1][y] = Xslidewall_ew;
5911     play_element_sound(x, y, SOUND_slidewall, Xslidewall_ew);
5912   }
5913 }
5914
5915 static void Lexit(int x, int y)
5916 {
5917   if (lev.required > 0)
5918     return;
5919
5920   switch (RANDOM(64) / 21)
5921   {
5922     case 0:
5923       Cave[x][y] = Xexit_1;
5924       Next[x][y] = Xexit_2;
5925       break;
5926
5927     case 1:
5928       Cave[x][y] = Xexit_2;
5929       Next[x][y] = Xexit_3;
5930       break;
5931
5932     default:
5933       Cave[x][y] = Xexit_3;
5934       Next[x][y] = Xexit_1;
5935       break;
5936   }
5937
5938   play_element_sound(x, y, SOUND_exit_open, Xexit);
5939 }
5940
5941 static void Lexit_1(int x, int y)
5942 {
5943   Next[x][y] = Xexit_2;
5944 }
5945
5946 static void Lexit_2(int x, int y)
5947 {
5948   Next[x][y] = Xexit_3;
5949 }
5950
5951 static void Lexit_3(int x, int y)
5952 {
5953   Next[x][y] = Xexit_1;
5954 }
5955
5956 static void Lpause(int x, int y)
5957 {
5958   Next[x][y] = Xblank;
5959 }
5960
5961 static void Lamoeba(int x, int y)
5962 {
5963   switch (Cave[x][y])
5964   {
5965     case Xblank:
5966     case Xacid_splash_e:
5967     case Xacid_splash_w:
5968     case Xgrass:
5969     case Xdirt:
5970     case Xsand:
5971     case Xplant:
5972     case Yplant:
5973       if (tab_amoeba[Cave[x][y-1]] ||
5974           tab_amoeba[Cave[x+1][y]] ||
5975           tab_amoeba[Cave[x][y+1]] ||
5976           tab_amoeba[Cave[x-1][y]])
5977         Cave[x][y] = Xdrip;
5978   }
5979 }
5980
5981 static void Lexplode(int x, int y)
5982 {
5983   switch (Cave[x][y])
5984   {
5985     case Znormal:
5986       Cave[x][y]     = Xboom_1;
5987       Cave[x][y-1]   = tab_explode_normal[Cave[x][y-1]];
5988       Cave[x-1][y]   = tab_explode_normal[Cave[x-1][y]];
5989       Cave[x+1][y]   = tab_explode_normal[Cave[x+1][y]];
5990       Cave[x][y+1]   = tab_explode_normal[Cave[x][y+1]];
5991       Cave[x-1][y-1] = tab_explode_normal[Cave[x-1][y-1]];
5992       Cave[x+1][y-1] = tab_explode_normal[Cave[x+1][y-1]];
5993       Cave[x-1][y+1] = tab_explode_normal[Cave[x-1][y+1]];
5994       Cave[x+1][y+1] = tab_explode_normal[Cave[x+1][y+1]];
5995       break;
5996
5997     case Zdynamite:
5998       Cave[x][y]     = Xboom_1;
5999       Cave[x][y-1]   = tab_explode_dynamite[Cave[x][y-1]];
6000       Cave[x-1][y]   = tab_explode_dynamite[Cave[x-1][y]];
6001       Cave[x+1][y]   = tab_explode_dynamite[Cave[x+1][y]];
6002       Cave[x][y+1]   = tab_explode_dynamite[Cave[x][y+1]];
6003       Cave[x-1][y-1] = tab_explode_dynamite[Cave[x-1][y-1]];
6004       Cave[x+1][y-1] = tab_explode_dynamite[Cave[x+1][y-1]];
6005       Cave[x-1][y+1] = tab_explode_dynamite[Cave[x-1][y+1]];
6006       Cave[x+1][y+1] = tab_explode_dynamite[Cave[x+1][y+1]];
6007       break;
6008   }
6009 }
6010
6011 static void Lboom_1(int x, int y)
6012 {
6013   Next[x][y] = Xboom_2;
6014 #if !PLAY_ELEMENT_SOUND
6015   if (x != lev.exit_x && y != lev.exit_y)
6016     play_sound(x, y, SOUND_boom);
6017   else
6018     lev.exit_x = lev.exit_y = -1;
6019 #endif
6020 }
6021
6022 static void Lboom_2(int x, int y)
6023 {
6024   Next[x][y] = Boom[x][y];
6025 }
6026
6027 static void Lboom_android(int x, int y)
6028 {
6029 #if PLAY_ELEMENT_SOUND
6030   play_element_sound(x, y, SOUND_boom, Xandroid);
6031 #endif
6032
6033   Lboom_1(x, y);
6034 }
6035
6036 static void handle_tile(int x, int y)
6037 {
6038   switch (Cave[x][y])
6039   {
6040     case Xacid_1:               Lacid_1(x, y);                  break;
6041     case Xacid_2:               Lacid_2(x, y);                  break;
6042     case Xacid_3:               Lacid_3(x, y);                  break;
6043     case Xacid_4:               Lacid_4(x, y);                  break;
6044     case Xacid_5:               Lacid_5(x, y);                  break;
6045     case Xacid_6:               Lacid_6(x, y);                  break;
6046     case Xacid_7:               Lacid_7(x, y);                  break;
6047     case Xacid_8:               Lacid_8(x, y);                  break;
6048
6049 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
6050     case Xfake_acid_1:          Lfake_acid_1(x, y);             break;
6051     case Xfake_acid_2:          Lfake_acid_2(x, y);             break;
6052     case Xfake_acid_3:          Lfake_acid_3(x, y);             break;
6053     case Xfake_acid_4:          Lfake_acid_4(x, y);             break;
6054     case Xfake_acid_5:          Lfake_acid_5(x, y);             break;
6055     case Xfake_acid_6:          Lfake_acid_6(x, y);             break;
6056     case Xfake_acid_7:          Lfake_acid_7(x, y);             break;
6057     case Xfake_acid_8:          Lfake_acid_8(x, y);             break;
6058 #endif
6059
6060     case Xandroid:              Landroid(x, y);                 break;
6061     case Xandroid_1_n:          Landroid_1_n(x, y);             break;
6062     case Xandroid_2_n:          Landroid_2_n(x, y);             break;
6063     case Xandroid_1_e:          Landroid_1_e(x, y);             break;
6064     case Xandroid_2_e:          Landroid_2_e(x, y);             break;
6065     case Xandroid_1_s:          Landroid_1_s(x, y);             break;
6066     case Xandroid_2_s:          Landroid_2_s(x, y);             break;
6067     case Xandroid_1_w:          Landroid_1_w(x, y);             break;
6068     case Xandroid_2_w:          Landroid_2_w(x, y);             break;
6069
6070     case Xeater_n:              Leater_n(x, y);                 break;
6071     case Xeater_e:              Leater_e(x, y);                 break;
6072     case Xeater_s:              Leater_s(x, y);                 break;
6073     case Xeater_w:              Leater_w(x, y);                 break;
6074
6075     case Xalien:                Lalien(x, y);                   break;
6076     case Xalien_pause:          Lalien_pause(x, y);             break;
6077
6078     case Xbug_1_n:              Lbug_1_n(x, y);                 break;
6079     case Xbug_2_n:              Lbug_2_n(x, y);                 break;
6080     case Xbug_1_e:              Lbug_1_e(x, y);                 break;
6081     case Xbug_2_e:              Lbug_2_e(x, y);                 break;
6082     case Xbug_1_s:              Lbug_1_s(x, y);                 break;
6083     case Xbug_2_s:              Lbug_2_s(x, y);                 break;
6084     case Xbug_1_w:              Lbug_1_w(x, y);                 break;
6085     case Xbug_2_w:              Lbug_2_w(x, y);                 break;
6086
6087     case Xtank_1_n:             Ltank_1_n(x, y);                break;
6088     case Xtank_2_n:             Ltank_2_n(x, y);                break;
6089     case Xtank_1_e:             Ltank_1_e(x, y);                break;
6090     case Xtank_2_e:             Ltank_2_e(x, y);                break;
6091     case Xtank_1_s:             Ltank_1_s(x, y);                break;
6092     case Xtank_2_s:             Ltank_2_s(x, y);                break;
6093     case Xtank_1_w:             Ltank_1_w(x, y);                break;
6094     case Xtank_2_w:             Ltank_2_w(x, y);                break;
6095
6096     case Xemerald:              Lemerald(x, y);                 break;
6097     case Xemerald_pause:        Lemerald_pause(x, y);           break;
6098     case Xemerald_fall:         Lemerald_fall(x, y);            break;
6099
6100     case Xdiamond:              Ldiamond(x, y);                 break;
6101     case Xdiamond_pause:        Ldiamond_pause(x, y);           break;
6102     case Xdiamond_fall:         Ldiamond_fall(x, y);            break;
6103
6104     case Xstone:                Lstone(x, y);                   break;
6105     case Xstone_pause:          Lstone_pause(x, y);             break;
6106     case Xstone_fall:           Lstone_fall(x, y);              break;
6107
6108     case Xbomb:                 Lbomb(x, y);                    break;
6109     case Xbomb_pause:           Lbomb_pause(x, y);              break;
6110     case Xbomb_fall:            Lbomb_fall(x, y);               break;
6111
6112     case Xnut:                  Lnut(x, y);                     break;
6113     case Xnut_pause:            Lnut_pause(x, y);               break;
6114     case Xnut_fall:             Lnut_fall(x, y);                break;
6115
6116     case Xspring:               Lspring(x, y);                  break;
6117     case Xspring_pause:         Lspring_pause(x, y);            break;
6118     case Xspring_e:             Lspring_e(x, y);                break;
6119     case Xspring_w:             Lspring_w(x, y);                break;
6120     case Xspring_fall:          Lspring_fall(x, y);             break;
6121
6122     case Xpush_emerald_e:       Lpush_emerald_e(x, y);          break;
6123     case Xpush_emerald_w:       Lpush_emerald_w(x, y);          break;
6124     case Xpush_diamond_e:       Lpush_diamond_e(x, y);          break;
6125     case Xpush_diamond_w:       Lpush_diamond_w(x, y);          break;
6126     case Xpush_stone_e:         Lpush_stone_e(x, y);            break;
6127     case Xpush_stone_w:         Lpush_stone_w(x, y);            break;
6128     case Xpush_bomb_e:          Lpush_bomb_e(x, y);             break;
6129     case Xpush_bomb_w:          Lpush_bomb_w(x, y);             break;
6130     case Xpush_nut_e:           Lpush_nut_e(x, y);              break;
6131     case Xpush_nut_w:           Lpush_nut_w(x, y);              break;
6132     case Xpush_spring_e:        Lpush_spring_e(x, y);           break;
6133     case Xpush_spring_w:        Lpush_spring_w(x, y);           break;
6134
6135     case Xdynamite_1:           Ldynamite_1(x, y);              break;
6136     case Xdynamite_2:           Ldynamite_2(x, y);              break;
6137     case Xdynamite_3:           Ldynamite_3(x, y);              break;
6138     case Xdynamite_4:           Ldynamite_4(x, y);              break;
6139
6140     case Xfake_door_1:          Lfake_door_1(x, y);             break;
6141     case Xfake_door_2:          Lfake_door_2(x, y);             break;
6142     case Xfake_door_3:          Lfake_door_3(x, y);             break;
6143     case Xfake_door_4:          Lfake_door_4(x, y);             break;
6144     case Xfake_door_5:          Lfake_door_5(x, y);             break;
6145     case Xfake_door_6:          Lfake_door_6(x, y);             break;
6146     case Xfake_door_7:          Lfake_door_7(x, y);             break;
6147     case Xfake_door_8:          Lfake_door_8(x, y);             break;
6148
6149     case Xballoon:              Lballoon(x, y);                 break;
6150
6151     case Xball_1:               Lball_1(x, y);                  break;
6152     case Xball_2:               Lball_2(x, y);                  break;
6153
6154     case Xdrip:                 Ldrip(x, y);                    break;
6155     case Xdrip_fall:            Ldrip_fall(x, y);               break;
6156     case Xdrip_stretch:         Ldrip_stretch(x, y);            break;
6157     case Xdrip_stretchB:        Ldrip_stretchB(x, y);           break;
6158
6159     case Xwonderwall:           Lwonderwall(x, y);              break;
6160
6161     case Xwheel:                Lwheel(x, y);                   break;
6162
6163     case Xswitch:               Lswitch(x, y);                  break;
6164
6165     case Xfake_blank:           Lfake_blank(x, y);              break;
6166     case Xfake_grass:           Lfake_grass(x, y);              break;
6167     case Xfake_amoeba:          Lfake_amoeba(x, y);             break;
6168
6169     case Xsand_stone:           Lsand_stone(x, y);              break;
6170     case Xsand_stonein_1:       Lsand_stonein_1(x, y);          break;
6171     case Xsand_stonein_2:       Lsand_stonein_2(x, y);          break;
6172     case Xsand_stonein_3:       Lsand_stonein_3(x, y);          break;
6173     case Xsand_stonein_4:       Lsand_stonein_4(x, y);          break;
6174     case Xsand_sandstone_1:     Lsand_sandstone_1(x, y);        break;
6175     case Xsand_sandstone_2:     Lsand_sandstone_2(x, y);        break;
6176     case Xsand_sandstone_3:     Lsand_sandstone_3(x, y);        break;
6177     case Xsand_sandstone_4:     Lsand_sandstone_4(x, y);        break;
6178     case Xsand_stonesand_1:     Lsand_stonesand_1(x, y);        break;
6179     case Xsand_stonesand_2:     Lsand_stonesand_2(x, y);        break;
6180     case Xsand_stonesand_3:     Lsand_stonesand_3(x, y);        break;
6181     case Xsand_stonesand_4:     Lsand_stonesand_4(x, y);        break;
6182     case Xsand_stoneout_1:      Lsand_stoneout_1(x, y);         break;
6183     case Xsand_stoneout_2:      Lsand_stoneout_2(x, y);         break;
6184 #ifdef EM_ENGINE_USE_ADDITIONAL_ELEMENTS
6185     case Xsand_stonesand_quickout_1: Lsand_stonesand_quickout_1(x, y); break;
6186     case Xsand_stonesand_quickout_2: Lsand_stonesand_quickout_2(x, y); break;
6187 #endif
6188
6189     case Xslidewall_ns:         Lslidewall_ns(x, y);            break;
6190     case Xslidewall_ew:         Lslidewall_ew(x, y);            break;
6191
6192     case Xexit:                 Lexit(x, y);                    break;
6193     case Xexit_1:               Lexit_1(x, y);                  break;
6194     case Xexit_2:               Lexit_2(x, y);                  break;
6195     case Xexit_3:               Lexit_3(x, y);                  break;
6196
6197     case Xpause:                Lpause(x, y);                   break;
6198
6199     case Xboom_bug:             Lboom_bug(x, y, Xboom_bug);     break;
6200     case Xboom_bomb:            Lboom_tank(x, y, Xboom_bomb);   break;
6201     case Xboom_android:         Lboom_android(x, y);            break;
6202     case Xboom_1:               Lboom_1(x, y);                  break;
6203     case Xboom_2:               Lboom_2(x, y);                  break;
6204   }
6205 }
6206
6207 void logic_1(void)
6208 {
6209   int start_check_nr;
6210   int i;
6211
6212   game_em.any_player_moving = FALSE;
6213   game_em.any_player_snapping = FALSE;
6214
6215   /* must test for death and actually kill separately */
6216   for (i = 0; i < MAX_PLAYERS; i++)
6217   {
6218     boolean ply_kill = player_killed(&ply[i]);
6219
6220     if (ply[i].alive && ply_kill)
6221       kill_player(&ply[i]);
6222   }
6223
6224   for (i = 0; i < MAX_PLAYERS; i++)
6225   {
6226     ply[i].oldx = ply[i].x;
6227     ply[i].oldy = ply[i].y;
6228     ply[i].anim = PLY_still;
6229   }
6230
6231   start_check_nr = (RandomEM & 128 ? 0 : 1) * 2 + (RandomEM & 256 ? 0 : 1);
6232
6233   for (i = 0; i < MAX_PLAYERS; i++)
6234   {
6235     int check_nr = (start_check_nr + i) % MAX_PLAYERS;
6236
6237     if (ply[check_nr].alive)
6238       check_player(&ply[check_nr]);
6239   }
6240
6241   for (i = 0; i < MAX_PLAYERS; i++)
6242   {
6243     if (!ply[i].alive)
6244       continue;
6245
6246     if (Cave[ply[i].oldx][ply[i].oldy] == Zplayer)
6247     {
6248       Cave[ply[i].oldx][ply[i].oldy] = Xblank;
6249       Next[ply[i].oldx][ply[i].oldy] = Xblank;
6250     }
6251
6252     if (Cave[ply[i].x][ply[i].y] == Xblank)
6253     {
6254       Cave[ply[i].x][ply[i].y] = Zplayer;
6255       Next[ply[i].x][ply[i].y] = Zplayer;
6256     }
6257   }
6258 }
6259
6260 void logic_2(void)
6261 {
6262   int x, y;
6263
6264   seed = RandomEM;
6265   score = 0;
6266
6267   for (y = 1; y < HEIGHT - 1; y++)
6268     for (x = 0; x < WIDTH; x++)
6269       handle_tile(x, y);
6270
6271   if (ply[0].alive || ply[1].alive || ply[2].alive || ply[3].alive)
6272     lev.score += score;         /* only add a score if someone is alive */
6273   else
6274     game_em.game_over = TRUE;
6275
6276   RandomEM = seed;
6277
6278   /* triple buffering */
6279   void *temp = Cave;
6280   Cave = Next;
6281   Next = Draw;
6282   Draw = temp;
6283 }
6284
6285 void logic_3(void)
6286 {
6287   int x;
6288   int y;
6289   int count;
6290   unsigned int random;
6291
6292   /* update variables */
6293
6294   if (lev.score > 9999)
6295     lev.score = 9999;
6296
6297   if (lev.android_move_cnt-- == 0)
6298     lev.android_move_cnt = lev.android_move_time;
6299   if (lev.android_clone_cnt-- == 0)
6300     lev.android_clone_cnt = lev.android_clone_time;
6301   if (lev.ball_state)
6302     if (lev.ball_cnt-- == 0)
6303       lev.ball_cnt = lev.ball_time;
6304   if (lev.lenses_cnt)
6305     lev.lenses_cnt--;
6306   if (lev.magnify_cnt)
6307     lev.magnify_cnt--;
6308   if (lev.wheel_cnt)
6309     lev.wheel_cnt--;
6310   if (lev.wind_cnt)
6311     lev.wind_cnt--;
6312   if (lev.wonderwall_time && lev.wonderwall_state)
6313     lev.wonderwall_time--;
6314
6315   if (lev.wheel_cnt)
6316     play_element_sound(lev.wheel_x, lev.wheel_y, SOUND_wheel, Xwheel);
6317
6318   /* grow amoeba */
6319
6320   random = RandomEM;
6321
6322   for (count = lev.amoeba_time; count--;)
6323   {
6324     x = (random >> 10) % (WIDTH - 2);
6325     y = (random >> 20) % (HEIGHT - 2);
6326
6327     Lamoeba(x, y);
6328
6329     random = random * 129 + 1;
6330   }
6331
6332   RandomEM = random;
6333
6334   /* handle explosions */
6335
6336   for (y = 1; y < HEIGHT - 1; y++)
6337     for (x = 1; x < WIDTH - 1; x++)
6338       Lexplode(x, y);
6339
6340   /* triple buffering */
6341
6342   for (y = 0; y < HEIGHT; y++)
6343     for (x = 0; x < WIDTH; x++)
6344       Next[x][y] = Cave[x][y];
6345 }