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