added optional button to restart game (door, panel and touch variants)
[rocksndiamonds.git] / src / anim.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // anim.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "anim.h"
15 #include "main.h"
16 #include "tools.h"
17 #include "files.h"
18 #include "events.h"
19 #include "screens.h"
20 #include "tape.h"
21
22
23 #define DEBUG_ANIM_DELAY                0
24 #define DEBUG_ANIM_EVENTS               0
25
26
27 // values for global toon animation definition
28 #define NUM_GLOBAL_TOON_ANIMS           1
29 #define NUM_GLOBAL_TOON_PARTS           MAX_NUM_TOONS
30
31 // values for global animation definition (including toons)
32 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS +             \
33                                          NUM_GLOBAL_TOON_ANIMS)
34 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
35                                             NUM_GLOBAL_TOON_PARTS)
36
37 #define MAX_GLOBAL_ANIM_LIST            (NUM_GAME_MODES *               \
38                                          NUM_GLOBAL_ANIMS_AND_TOONS *   \
39                                          NUM_GLOBAL_ANIM_PARTS_AND_TOONS)
40
41 #define ANIM_CLASS_BIT_TITLE_INITIAL    0
42 #define ANIM_CLASS_BIT_TITLE            1
43 #define ANIM_CLASS_BIT_MAIN             2
44 #define ANIM_CLASS_BIT_NAMES            3
45 #define ANIM_CLASS_BIT_SCORES           4
46 #define ANIM_CLASS_BIT_SCORESONLY       5
47 #define ANIM_CLASS_BIT_SUBMENU          6
48 #define ANIM_CLASS_BIT_MENU             7
49 #define ANIM_CLASS_BIT_TOONS            8
50 #define ANIM_CLASS_BIT_NO_TITLE         9
51
52 #define NUM_ANIM_CLASSES                10
53
54 #define ANIM_CLASS_NONE                 0
55 #define ANIM_CLASS_TITLE_INITIAL        (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
56 #define ANIM_CLASS_TITLE                (1 << ANIM_CLASS_BIT_TITLE)
57 #define ANIM_CLASS_MAIN                 (1 << ANIM_CLASS_BIT_MAIN)
58 #define ANIM_CLASS_NAMES                (1 << ANIM_CLASS_BIT_NAMES)
59 #define ANIM_CLASS_SCORES               (1 << ANIM_CLASS_BIT_SCORES)
60 #define ANIM_CLASS_SCORESONLY           (1 << ANIM_CLASS_BIT_SCORESONLY)
61 #define ANIM_CLASS_SUBMENU              (1 << ANIM_CLASS_BIT_SUBMENU)
62 #define ANIM_CLASS_MENU                 (1 << ANIM_CLASS_BIT_MENU)
63 #define ANIM_CLASS_TOONS                (1 << ANIM_CLASS_BIT_TOONS)
64 #define ANIM_CLASS_NO_TITLE             (1 << ANIM_CLASS_BIT_NO_TITLE)
65
66 #define ANIM_CLASS_TOONS_SCORES         (ANIM_CLASS_TOONS       |       \
67                                          ANIM_CLASS_SCORES      |       \
68                                          ANIM_CLASS_NO_TITLE)
69
70 #define ANIM_CLASS_TOONS_SCORESONLY     (ANIM_CLASS_TOONS       |       \
71                                          ANIM_CLASS_SCORES      |       \
72                                          ANIM_CLASS_SCORESONLY  |       \
73                                          ANIM_CLASS_NO_TITLE)
74
75 #define ANIM_CLASS_TOONS_MENU_MAIN      (ANIM_CLASS_TOONS       |       \
76                                          ANIM_CLASS_MENU        |       \
77                                          ANIM_CLASS_MAIN        |       \
78                                          ANIM_CLASS_NO_TITLE)
79
80 #define ANIM_CLASS_TOONS_MENU_SUBMENU   (ANIM_CLASS_TOONS       |       \
81                                          ANIM_CLASS_MENU        |       \
82                                          ANIM_CLASS_SUBMENU     |       \
83                                          ANIM_CLASS_NO_TITLE)
84
85 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS       |       \
86                                          ANIM_CLASS_MENU        |       \
87                                          ANIM_CLASS_SUBMENU     |       \
88                                          ANIM_CLASS_NAMES       |       \
89                                          ANIM_CLASS_NO_TITLE)
90
91 // values for global animation states
92 #define ANIM_STATE_INACTIVE             0
93 #define ANIM_STATE_RESTART              (1 << 0)
94 #define ANIM_STATE_WAITING              (1 << 1)
95 #define ANIM_STATE_RUNNING              (1 << 2)
96
97 // values for global animation control
98 #define ANIM_NO_ACTION                  0
99 #define ANIM_START                      1
100 #define ANIM_CONTINUE                   2
101 #define ANIM_STOP                       3
102
103
104 struct GlobalAnimPartControlInfo
105 {
106   int old_nr;           // position before mapping animation parts linearly
107   int old_anim_nr;      // position before mapping animations linearly
108
109   int nr;
110   int anim_nr;
111   int mode_nr;
112
113   boolean is_base;      // animation part is base/main/default animation part
114
115   int sound;
116   int music;
117   int graphic;
118
119   struct GraphicInfo graphic_info;
120   struct GraphicInfo control_info;
121
122   boolean class_playfield_or_door;
123
124   int viewport_x;
125   int viewport_y;
126   int viewport_width;
127   int viewport_height;
128
129   int x, y;
130   int step_xoffset, step_yoffset;
131
132   int tile_x, tile_y;
133   int tile_xoffset, tile_yoffset;
134
135   unsigned int initial_anim_sync_frame;
136   unsigned int anim_random_frame;
137
138   DelayCounter step_delay;
139
140   int init_delay_counter;
141   int anim_delay_counter;
142   int post_delay_counter;
143
144   int fade_delay_counter;
145   int fade_alpha;
146
147   boolean init_event_state;
148   boolean anim_event_state;
149
150   boolean triggered;
151   boolean clickable;
152   boolean clicked;
153
154   int drawing_stage;
155
156   int state;
157   int last_anim_status;
158 };
159
160 struct GlobalAnimMainControlInfo
161 {
162   struct GlobalAnimPartControlInfo base;
163   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
164
165   int nr;
166   int mode_nr;
167
168   struct GraphicInfo control_info;
169
170   int num_parts;        // number of animation parts, but without base part
171   int num_parts_all;    // number of animation parts, including base part
172   int part_counter;
173   int active_part_nr;
174
175   boolean has_base;     // animation has base/main/default animation part
176
177   int last_x, last_y;
178
179   int init_delay_counter;
180
181   int state;
182
183   int last_state, last_active_part_nr;
184 };
185
186 struct GlobalAnimControlInfo
187 {
188   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
189
190   int nr;
191   int num_anims;
192 };
193
194 struct GameModeAnimClass
195 {
196   int game_mode;
197   int class;
198 } game_mode_anim_classes_list[] =
199 {
200   { GAME_MODE_TITLE_INITIAL_1,          ANIM_CLASS_TITLE_INITIAL        },
201   { GAME_MODE_TITLE_INITIAL_2,          ANIM_CLASS_TITLE_INITIAL        },
202   { GAME_MODE_TITLE_INITIAL_3,          ANIM_CLASS_TITLE_INITIAL        },
203   { GAME_MODE_TITLE_INITIAL_4,          ANIM_CLASS_TITLE_INITIAL        },
204   { GAME_MODE_TITLE_INITIAL_5,          ANIM_CLASS_TITLE_INITIAL        },
205   { GAME_MODE_TITLE_1,                  ANIM_CLASS_TITLE                },
206   { GAME_MODE_TITLE_2,                  ANIM_CLASS_TITLE                },
207   { GAME_MODE_TITLE_3,                  ANIM_CLASS_TITLE                },
208   { GAME_MODE_TITLE_4,                  ANIM_CLASS_TITLE                },
209   { GAME_MODE_TITLE_5,                  ANIM_CLASS_TITLE                },
210   { GAME_MODE_NAMES,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
211   { GAME_MODE_LEVELS,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
212   { GAME_MODE_LEVELNR,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
213   { GAME_MODE_INFO,                     ANIM_CLASS_TOONS_MENU_SUBMENU   },
214   { GAME_MODE_SETUP,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
215   { GAME_MODE_PSEUDO_NAMESONLY,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
216   { GAME_MODE_PSEUDO_TYPENAMES,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
217   { GAME_MODE_PSEUDO_MAINONLY,          ANIM_CLASS_TOONS_MENU_MAIN      },
218   { GAME_MODE_PSEUDO_TYPENAME,          ANIM_CLASS_TOONS_MENU_MAIN      },
219   { GAME_MODE_PSEUDO_SCORESOLD,         ANIM_CLASS_TOONS_SCORESONLY     },
220   { GAME_MODE_PSEUDO_SCORESNEW,         ANIM_CLASS_TOONS_SCORESONLY     },
221   { GAME_MODE_SCOREINFO,                ANIM_CLASS_TOONS_SCORES         },
222   { GAME_MODE_EDITOR,                   ANIM_CLASS_NO_TITLE             },
223   { GAME_MODE_PLAYING,                  ANIM_CLASS_NO_TITLE             },
224
225   { -1,                                 -1                              }
226 };
227
228 struct AnimClassGameMode
229 {
230   int class_bit;
231   int game_mode;
232 } anim_class_game_modes_list[] =
233 {
234   { ANIM_CLASS_BIT_TITLE_INITIAL,       GAME_MODE_TITLE_INITIAL         },
235   { ANIM_CLASS_BIT_TITLE,               GAME_MODE_TITLE                 },
236   { ANIM_CLASS_BIT_MAIN,                GAME_MODE_MAIN                  },
237   { ANIM_CLASS_BIT_NAMES,               GAME_MODE_NAMES                 },
238   { ANIM_CLASS_BIT_SCORES,              GAME_MODE_SCORES                },
239   { ANIM_CLASS_BIT_SCORESONLY,          GAME_MODE_PSEUDO_SCORESONLY     },
240   { ANIM_CLASS_BIT_SUBMENU,             GAME_MODE_PSEUDO_SUBMENU        },
241   { ANIM_CLASS_BIT_MENU,                GAME_MODE_PSEUDO_MENU           },
242   { ANIM_CLASS_BIT_TOONS,               GAME_MODE_PSEUDO_TOONS          },
243   { ANIM_CLASS_BIT_NO_TITLE,            GAME_MODE_PSEUDO_NO_TITLE       },
244
245   { -1,                                 -1                              }
246 };
247
248 // forward declaration for internal use
249 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
250 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
251 static void HandleGlobalAnim(int, int);
252 static void DoAnimationExt(void);
253 static void ResetGlobalAnim_Clickable(void);
254 static void ResetGlobalAnim_Clicked(void);
255
256 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
257 static struct GlobalAnimPartControlInfo *global_anim_list[MAX_GLOBAL_ANIM_LIST];
258 static int num_global_anim_list = 0;
259
260 static unsigned int anim_sync_frame = 0;
261
262 static int game_mode_anim_classes[NUM_GAME_MODES];
263 static int anim_class_game_modes[NUM_ANIM_CLASSES];
264
265 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
266 static int anim_status_last = GAME_MODE_DEFAULT;
267 static int anim_classes_last = ANIM_CLASS_NONE;
268
269 static boolean drawing_to_fading_buffer = FALSE;
270
271 static boolean handle_click = FALSE;
272
273
274 // ============================================================================
275 // generic animation frame calculation
276 // ============================================================================
277
278 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
279                       int sync_frame)
280 {
281   int frame = 0;
282
283   if (delay < 1)                        // delay must be at least 1
284     delay = 1;
285
286   sync_frame += start_frame * delay;
287
288   if (mode & ANIM_LOOP)                 // looping animation
289   {
290     frame = (sync_frame % (delay * num_frames)) / delay;
291   }
292   else if (mode & ANIM_LINEAR)          // linear (non-looping) animation
293   {
294     frame = sync_frame / delay;
295
296     if (frame > num_frames - 1)
297       frame = num_frames - 1;
298   }
299   else if (mode & ANIM_PINGPONG)        // oscillate (border frames once)
300   {
301     int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
302
303     frame = (sync_frame % (delay * max_anim_frames)) / delay;
304     frame = (frame < num_frames ? frame : max_anim_frames - frame);
305   }
306   else if (mode & ANIM_PINGPONG2)       // oscillate (border frames twice)
307   {
308     int max_anim_frames = 2 * num_frames;
309
310     frame = (sync_frame % (delay * max_anim_frames)) / delay;
311     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
312   }
313   else if (mode & ANIM_RANDOM)          // play frames in random order
314   {
315     // note: expect different frames for the same delay cycle!
316
317     if (gfx.anim_random_frame < 0)
318       frame = GetSimpleRandom(num_frames);
319     else
320       frame = gfx.anim_random_frame % num_frames;
321   }
322   else if (mode & ANIM_LEVEL_NR)        // play frames by level number
323   {
324     int level_pos = level_nr - gfx.anim_first_level;
325
326     frame = level_pos % num_frames;
327   }
328   else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
329   {
330     frame = sync_frame % num_frames;
331   }
332
333   if (mode & ANIM_REVERSE)              // use reverse animation direction
334     frame = num_frames - frame - 1;
335
336   return frame;
337 }
338
339
340 // ============================================================================
341 // global animation functions
342 // ============================================================================
343
344 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
345 {
346   struct GraphicInfo *c = &anim->control_info;
347   int last_anim_random_frame = gfx.anim_random_frame;
348   int part_nr;
349
350   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
351
352   part_nr = getAnimationFrame(anim->num_parts, 1,
353                               c->anim_mode, c->anim_start_frame,
354                               anim->part_counter);
355
356   gfx.anim_random_frame = last_anim_random_frame;
357
358   return part_nr;
359 }
360
361 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
362 {
363   const struct GlobalAnimPartControlInfo *o1 =
364     *(struct GlobalAnimPartControlInfo **)obj1;
365   const struct GlobalAnimPartControlInfo *o2 =
366     *(struct GlobalAnimPartControlInfo **)obj2;
367   int compare_result;
368
369   if (o1->control_info.draw_order != o2->control_info.draw_order)
370     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
371   else if (o1->mode_nr != o2->mode_nr)
372     compare_result = o1->mode_nr - o2->mode_nr;
373   else if (o1->anim_nr != o2->anim_nr)
374     compare_result = o1->anim_nr - o2->anim_nr;
375   else
376     compare_result = o1->nr - o2->nr;
377
378   return compare_result;
379 }
380
381 static boolean isPausedOnPlayfieldOrDoor(struct GlobalAnimPartControlInfo *part)
382 {
383   // only pause playfield and door animations when playing
384   if (game_status != GAME_MODE_PLAYING)
385     return FALSE;
386
387   // do not pause animations when game ended (and engine is running)
388   if (checkGameEnded())
389     return FALSE;
390
391   // only pause animations on playfield and doors
392   if (!part->class_playfield_or_door)
393     return FALSE;
394
395   // only pause animations when engine is paused or request dialog is active
396   if (!tape.pausing && !game.request_active)
397     return FALSE;
398
399   return TRUE;
400 }
401
402 static void InitToonControls(void)
403 {
404   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
405   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
406   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
407   int mode_nr, anim_nr, part_nr;
408   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
409   int num_toons = MAX_NUM_TOONS;
410   int i;
411
412   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
413     num_toons = global.num_toons;
414
415   mode_nr = mode_nr_toons;
416   anim_nr = ctrl->num_anims;
417
418   anim->nr = anim_nr;
419   anim->mode_nr = mode_nr;
420   anim->control_info = graphic_info[control];
421
422   anim->num_parts = 0;
423   anim->num_parts_all = 0;
424   anim->part_counter = 0;
425   anim->active_part_nr = 0;
426
427   anim->has_base = FALSE;
428
429   anim->last_x = POS_OFFSCREEN;
430   anim->last_y = POS_OFFSCREEN;
431
432   anim->init_delay_counter = 0;
433
434   anim->state = ANIM_STATE_INACTIVE;
435
436   part_nr = 0;
437
438   for (i = 0; i < num_toons; i++)
439   {
440     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
441     int sound = SND_UNDEFINED;
442     int music = MUS_UNDEFINED;
443     int graphic = IMG_TOON_1 + i;
444
445     control = graphic;
446
447     part->nr = part_nr;
448     part->anim_nr = anim_nr;
449     part->mode_nr = mode_nr;
450
451     part->is_base = FALSE;
452
453     part->sound = sound;
454     part->music = music;
455     part->graphic = graphic;
456
457     part->graphic_info = graphic_info[graphic];
458     part->control_info = graphic_info[control];
459
460     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
461
462     part->control_info.init_delay_fixed = 0;
463     part->control_info.init_delay_random = 150;
464
465     part->control_info.x = ARG_UNDEFINED_VALUE;
466     part->control_info.y = ARG_UNDEFINED_VALUE;
467
468     part->initial_anim_sync_frame = 0;
469     part->anim_random_frame = -1;
470
471     part->step_delay.count = 0;
472     part->step_delay.value = graphic_info[control].step_delay;
473
474     part->state = ANIM_STATE_INACTIVE;
475     part->last_anim_status = -1;
476
477     anim->num_parts++;
478     anim->num_parts_all++;
479
480     part_nr++;
481   }
482
483   ctrl->num_anims++;
484 }
485
486 static void InitGlobalAnimControls(void)
487 {
488   int i, m, a, p;
489   int mode_nr, anim_nr, part_nr;
490   int sound, music, graphic, control;
491
492   anim_sync_frame = 0;
493
494   for (m = 0; m < NUM_GAME_MODES; m++)
495   {
496     mode_nr = m;
497
498     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
499
500     ctrl->nr = mode_nr;
501     ctrl->num_anims = 0;
502
503     anim_nr = 0;
504
505     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
506     {
507       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
508       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
509
510       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
511
512       // if no base animation parameters defined, use default values
513       if (control == IMG_UNDEFINED)
514         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
515
516       anim->nr = anim_nr;
517       anim->mode_nr = mode_nr;
518       anim->control_info = graphic_info[control];
519
520       anim->num_parts = 0;
521       anim->num_parts_all = 0;
522       anim->part_counter = 0;
523       anim->active_part_nr = 0;
524
525       anim->has_base = FALSE;
526
527       anim->last_x = POS_OFFSCREEN;
528       anim->last_y = POS_OFFSCREEN;
529
530       anim->init_delay_counter = 0;
531
532       anim->state = ANIM_STATE_INACTIVE;
533
534       part_nr = 0;
535
536       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
537       {
538         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
539
540         sound   = global_anim_info[a].sound[p][m];
541         music   = global_anim_info[a].music[p][m];
542         graphic = global_anim_info[a].graphic[p][m];
543         control = global_anim_info[ctrl_id].graphic[p][m];
544
545         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
546             control == IMG_UNDEFINED)
547           continue;
548
549 #if 0
550         Debug("anim:InitGlobalAnimControls",
551               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
552               m, a, p, mode_nr, anim_nr, part_nr, control);
553 #endif
554
555 #if 0
556         Debug("anim:InitGlobalAnimControls",
557               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
558               m, a, p, mode_nr, anim_nr, part_nr, sound);
559 #endif
560
561         part->old_nr = p;
562         part->old_anim_nr = a;
563
564         part->nr = part_nr;
565         part->anim_nr = anim_nr;
566         part->mode_nr = mode_nr;
567
568         part->sound = sound;
569         part->music = music;
570         part->graphic = graphic;
571
572         part->graphic_info = graphic_info[graphic];
573         part->control_info = graphic_info[control];
574
575         part->initial_anim_sync_frame = 0;
576         part->anim_random_frame = -1;
577
578         part->step_delay.count = 0;
579         part->step_delay.value = graphic_info[control].step_delay;
580
581         part->state = ANIM_STATE_INACTIVE;
582         part->last_anim_status = -1;
583
584         anim->num_parts_all++;
585
586         if (p < GLOBAL_ANIM_ID_PART_BASE)
587         {
588           part->is_base = FALSE;
589
590           anim->num_parts++;
591           part_nr++;
592         }
593         else
594         {
595           part->is_base = TRUE;
596
597           anim->base = *part;
598           anim->has_base = TRUE;
599         }
600
601         // apply special settings to pointer-style animations
602         if (part->control_info.class == get_hash_from_key("pointer"))
603         {
604           // force pointer-style animations to be checked for clicks first
605           part->control_info.draw_order = 1000000;
606
607           // force pointer-style animations to pass-through clicks
608           part->control_info.style |= STYLE_PASSTHROUGH;
609         }
610       }
611
612       if (anim->num_parts > 0 || anim->has_base)
613       {
614         ctrl->num_anims++;
615         anim_nr++;
616       }
617     }
618   }
619
620   InitToonControls();
621
622   // create list of all animation parts
623   num_global_anim_list = 0;
624   for (m = 0; m < NUM_GAME_MODES; m++)
625     for (a = 0; a < global_anim_ctrl[m].num_anims; a++)
626       for (p = 0; p < global_anim_ctrl[m].anim[a].num_parts_all; p++)
627         global_anim_list[num_global_anim_list++] =
628           &global_anim_ctrl[m].anim[a].part[p];
629
630   // sort list of all animation parts according to draw_order and number
631   qsort(global_anim_list, num_global_anim_list,
632         sizeof(struct GlobalAnimPartControlInfo *),
633         compareGlobalAnimPartControlInfo);
634
635   for (i = 0; i < NUM_GAME_MODES; i++)
636     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
637   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
638     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
639       game_mode_anim_classes_list[i].class;
640
641   for (i = 0; i < NUM_ANIM_CLASSES; i++)
642     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
643   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
644     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
645       anim_class_game_modes_list[i].game_mode;
646
647   anim_status_last_before_fading = GAME_MODE_LOADING;
648   anim_status_last = GAME_MODE_LOADING;
649   anim_classes_last = ANIM_CLASS_NONE;
650 }
651
652 static void SetGlobalAnimEventsForCustomElements(int list_pos)
653 {
654   int num_events = GetGlobalAnimEventValueCount(list_pos);
655   int i;
656
657   for (i = 0; i < num_events; i++)
658   {
659     int event = GetGlobalAnimEventValue(list_pos, i);
660
661     if (event & ANIM_EVENT_CE_CHANGE)
662     {
663       int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
664
665       if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
666         element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
667     }
668   }
669 }
670
671 void InitGlobalAnimEventsForCustomElements(void)
672 {
673   int m, a, p;
674   int control;
675
676   // custom element events for global animations only relevant while playing
677   m = GAME_MODE_PLAYING;
678
679   for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
680   {
681     int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
682
683     control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
684
685     // if no base animation parameters defined, use default values
686     if (control == IMG_UNDEFINED)
687       control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
688
689     SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
690     SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
691
692     for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
693     {
694       control = global_anim_info[ctrl_id].graphic[p][m];
695
696       if (control == IMG_UNDEFINED)
697         continue;
698
699       SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
700       SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
701     }
702   }
703 }
704
705 void InitGlobalAnimations(void)
706 {
707   InitGlobalAnimControls();
708 }
709
710 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
711                                 Bitmap *src_bitmap, int src_x0, int src_y0,
712                                 int drawing_target)
713 {
714   struct GraphicInfo *g = &part->graphic_info;
715   struct GraphicInfo *c = &part->control_info;
716   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
717     (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
718   void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
719     (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
720   Bitmap *fade_bitmap =
721     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
722      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
723   int alpha = (c->fade_mode & FADE_MODE_FADE ? part->fade_alpha : g->alpha);
724   int x, y;
725
726   for (y = 0; y < c->stacked_yfactor; y++)
727   {
728     for (x = 0; x < c->stacked_xfactor; x++)
729     {
730       int src_x = src_x0;
731       int src_y = src_y0;
732       int dst_x = part->x + x * (g->width  + c->stacked_xoffset);
733       int dst_y = part->y + y * (g->height + c->stacked_yoffset);
734       int cut_x = 0;
735       int cut_y = 0;
736       int width  = g->width;
737       int height = g->height;
738
739       if (dst_x < 0)
740       {
741         width += dst_x;
742         cut_x = -dst_x;
743         dst_x = 0;
744       }
745       else if (dst_x > part->viewport_width - g->width)
746       {
747         width -= (dst_x - (part->viewport_width - g->width));
748       }
749
750       if (dst_y < 0)
751       {
752         height += dst_y;
753         cut_y  = -dst_y;
754         dst_y = 0;
755       }
756       else if (dst_y > part->viewport_height - g->height)
757       {
758         height -= (dst_y - (part->viewport_height - g->height));
759       }
760
761       if (width <= 0 || height <= 0)
762         continue;
763
764       src_x += cut_x;
765       src_y += cut_y;
766
767       dst_x += part->viewport_x;
768       dst_y += part->viewport_y;
769
770       SetBitmapAlphaNextBlit(src_bitmap, alpha);
771
772       if (drawing_target == DRAW_TO_SCREEN)
773         blit_screen(src_bitmap, src_x, src_y, width, height,
774                     dst_x, dst_y);
775       else
776         blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
777                     dst_x, dst_y);
778     }
779   }
780 }
781
782 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
783 {
784   int game_mode_anim_action[NUM_GAME_MODES];
785   int mode_nr;
786   int i;
787
788   if (!setup.global_animations)
789     return;
790
791   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
792       drawing_target == DRAW_TO_SCREEN)
793     DoAnimationExt();
794
795   // always start with reliable default values (no animation actions)
796   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
797     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
798
799   if (global.anim_status != anim_status_last)
800   {
801     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
802     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
803     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
804     int i;
805
806     if (drawing_target == DRAW_TO_FADE_TARGET)
807       after_fading = TRUE;
808
809     // special case: changing from/to these screens is done without fading
810     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
811         global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
812         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
813         anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
814       after_fading = TRUE;
815
816     // ---------- part 1 ------------------------------------------------------
817     // start or stop global animations by change of game mode
818     // (special handling of animations for "current screen" and "all screens")
819
820     if (global.anim_status_next != anim_status_last_before_fading)
821     {
822       // stop animations for last screen before fading to new screen
823       game_mode_anim_action[anim_status_last] = ANIM_STOP;
824
825       // start animations for current screen after fading to new screen
826       game_mode_anim_action[global.anim_status] = ANIM_START;
827     }
828
829     // start animations for all screens after loading new artwork set
830     if (anim_status_last == GAME_MODE_LOADING)
831       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
832
833     // ---------- part 2 ------------------------------------------------------
834     // start or stop global animations by change of animation class
835     // (generic handling of animations for "class of screens")
836
837     for (i = 0; i < NUM_ANIM_CLASSES; i++)
838     {
839       int anim_class_check = (1 << i);
840       int anim_class_game_mode = anim_class_game_modes[i];
841       int anim_class_last = anim_classes_last & anim_class_check;
842       int anim_class_next = anim_classes_next & anim_class_check;
843
844       // stop animations for changed screen class before fading to new screen
845       if (before_fading && anim_class_last && !anim_class_next)
846         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
847
848       // start animations for changed screen class after fading to new screen
849       if (after_fading && !anim_class_last && anim_class_next)
850         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
851     }
852
853     if (drawing_target == DRAW_TO_SCREEN)
854     {
855       if (after_fading)
856       {
857         anim_classes_last = anim_classes_next;
858         anim_status_last_before_fading = global.anim_status;
859       }
860
861       anim_status_last = global.anim_status;
862
863       // start or stop animations determined to be started or stopped above
864       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
865         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
866           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
867     }
868     else if (drawing_target == DRAW_TO_FADE_TARGET)
869     {
870       drawing_to_fading_buffer = TRUE;
871
872       // start animations determined to be (temporary) started above
873       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
874         if (game_mode_anim_action[mode_nr] == ANIM_START)
875           HandleGlobalAnim(ANIM_START, mode_nr);
876     }
877   }
878
879   // when restarting global animations, do not redraw them, but stop here
880   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
881     return;
882
883   if (global.anim_status == GAME_MODE_LOADING)
884     return;
885
886   for (i = 0; i < num_global_anim_list; i++)
887   {
888     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
889     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
890     struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
891     struct GraphicInfo *g = &part->graphic_info;
892     Bitmap *src_bitmap;
893     int src_x, src_y;
894     int sync_frame;
895     int frame;
896     int last_anim_random_frame = gfx.anim_random_frame;
897
898     if (!setup.toons &&
899         part->graphic >= IMG_TOON_1 &&
900         part->graphic <= IMG_TOON_20)
901       continue;
902
903     // when preparing source fading buffer, only draw animations to be stopped
904     if (drawing_target == DRAW_TO_FADE_SOURCE &&
905         game_mode_anim_action[part->mode_nr] != ANIM_STOP)
906       continue;
907
908     // when preparing target fading buffer, only draw animations to be started
909     if (drawing_target == DRAW_TO_FADE_TARGET &&
910         game_mode_anim_action[part->mode_nr] != ANIM_START)
911       continue;
912
913     if (!(anim->state & ANIM_STATE_RUNNING))
914       continue;
915
916     if (!(part->state & ANIM_STATE_RUNNING))
917       continue;
918
919     if (part->drawing_stage != drawing_stage)
920       continue;
921
922     // if game is paused, also pause playfield and door animations
923     if (isPausedOnPlayfieldOrDoor(part))
924       part->initial_anim_sync_frame++;
925
926     sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
927
928     // re-initialize random animation frame after animation delay
929     if (g->anim_mode == ANIM_RANDOM &&
930         sync_frame % g->anim_delay == 0 &&
931         sync_frame > 0)
932       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
933
934     gfx.anim_random_frame = part->anim_random_frame;
935
936     frame = getAnimationFrame(g->anim_frames, g->anim_delay,
937                               g->anim_mode, g->anim_start_frame,
938                               sync_frame);
939
940     gfx.anim_random_frame = last_anim_random_frame;
941
942     getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
943                                &src_x, &src_y);
944
945     BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
946   }
947
948   if (drawing_target == DRAW_TO_FADE_TARGET)
949   {
950     // stop animations determined to be (temporary) started above
951     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
952       if (game_mode_anim_action[mode_nr] == ANIM_START)
953         HandleGlobalAnim(ANIM_STOP, mode_nr);
954
955     drawing_to_fading_buffer = FALSE;
956   }
957 }
958
959 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
960 {
961   int last_cursor_mode_override = gfx.cursor_mode_override;
962
963   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
964   {
965     ResetGlobalAnim_Clickable();
966
967     gfx.cursor_mode_override = CURSOR_UNDEFINED;
968   }
969
970   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
971
972   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
973   {
974     ResetGlobalAnim_Clicked();
975   }
976
977   if (gfx.cursor_mode_override != last_cursor_mode_override)
978     SetMouseCursor(gfx.cursor_mode);
979 }
980
981 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
982 {
983   int viewport_x;
984   int viewport_y;
985   int viewport_width;
986   int viewport_height;
987   boolean changed = FALSE;
988
989   if (part->last_anim_status == global.anim_status &&
990       part->control_info.class != get_hash_from_key("pointer"))
991     return FALSE;
992
993   part->last_anim_status = global.anim_status;
994
995   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
996
997   part->class_playfield_or_door = FALSE;
998
999   if (part->control_info.class == get_hash_from_key("window") ||
1000       part->control_info.class == get_hash_from_key("border"))
1001   {
1002     viewport_x = 0;
1003     viewport_y = 0;
1004     viewport_width  = WIN_XSIZE;
1005     viewport_height = WIN_YSIZE;
1006
1007     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1008   }
1009   else if (part->control_info.class == get_hash_from_key("pointer"))
1010   {
1011     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1012     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1013
1014     // prevent displaying off-screen custom mouse cursor in upper left corner
1015     if (gfx.mouse_x == POS_OFFSCREEN &&
1016         gfx.mouse_y == POS_OFFSCREEN)
1017       mx = my = POS_OFFSCREEN;
1018
1019     viewport_x = mx - part->control_info.x;
1020     viewport_y = my - part->control_info.y;
1021     viewport_width  = part->graphic_info.width;
1022     viewport_height = part->graphic_info.height;
1023
1024     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1025
1026     // do not use global animation mouse pointer when reloading artwork
1027     if (global.anim_status != GAME_MODE_LOADING)
1028       gfx.cursor_mode_override = CURSOR_NONE;
1029   }
1030   else if (part->control_info.class == get_hash_from_key("door_1"))
1031   {
1032     viewport_x = DX;
1033     viewport_y = DY;
1034     viewport_width  = DXSIZE;
1035     viewport_height = DYSIZE;
1036
1037     part->class_playfield_or_door = TRUE;
1038   }
1039   else if (part->control_info.class == get_hash_from_key("door_2"))
1040   {
1041     if (part->mode_nr == GAME_MODE_EDITOR)
1042     {
1043       viewport_x = EX;
1044       viewport_y = EY;
1045       viewport_width  = EXSIZE;
1046       viewport_height = EYSIZE;
1047     }
1048     else
1049     {
1050       viewport_x = VX;
1051       viewport_y = VY;
1052       viewport_width  = VXSIZE;
1053       viewport_height = VYSIZE;
1054     }
1055
1056     part->class_playfield_or_door = TRUE;
1057   }
1058   else          // default: "playfield"
1059   {
1060     viewport_x = REAL_SX;
1061     viewport_y = REAL_SY;
1062     viewport_width  = FULL_SXSIZE;
1063     viewport_height = FULL_SYSIZE;
1064
1065     part->class_playfield_or_door = TRUE;
1066   }
1067
1068   if (viewport_x != part->viewport_x ||
1069       viewport_y != part->viewport_y ||
1070       viewport_width  != part->viewport_width ||
1071       viewport_height != part->viewport_height)
1072   {
1073     part->viewport_x = viewport_x;
1074     part->viewport_y = viewport_y;
1075     part->viewport_width  = viewport_width;
1076     part->viewport_height = viewport_height;
1077
1078     if (part->control_info.class != get_hash_from_key("pointer"))
1079       changed = TRUE;
1080   }
1081
1082   return changed;
1083 }
1084
1085 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1086 {
1087   int sound = part->sound;
1088
1089   if (sound == SND_UNDEFINED)
1090     return;
1091
1092   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1093       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1094     return;
1095
1096   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1097   if (IS_LOOP_SOUND(sound))
1098     PlaySoundLoop(sound);
1099   else
1100     PlaySound(sound);
1101
1102 #if 0
1103   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1104          part->anim_nr, part->nr, part->mode_nr, sound);
1105 #endif
1106 }
1107
1108 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1109 {
1110   int sound = part->sound;
1111
1112   if (sound == SND_UNDEFINED)
1113     return;
1114
1115   StopSound(sound);
1116
1117 #if 0
1118   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1119         part->anim_nr, part->nr, part->mode_nr, sound);
1120 #endif
1121 }
1122
1123 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1124 {
1125   int music = part->music;
1126
1127   if (music == MUS_UNDEFINED)
1128     return;
1129
1130   if (!setup.sound_music)
1131     return;
1132
1133   if (IS_LOOP_MUSIC(music))
1134     PlayMusicLoop(music);
1135   else
1136     PlayMusic(music);
1137
1138 #if 0
1139   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1140         part->anim_nr, part->nr, part->mode_nr, music);
1141 #endif
1142 }
1143
1144 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1145 {
1146   int music = part->music;
1147
1148   if (music == MUS_UNDEFINED)
1149     return;
1150
1151   char *anim_music = getMusicInfoEntryFilename(music);
1152   char *curr_music = getCurrentlyPlayingMusicFilename();
1153
1154   // do not stop music if global anim music differs from current music
1155   if (!strEqual(curr_music, anim_music))
1156     return;
1157
1158   StopMusic();
1159
1160 #if 0
1161   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1162         part->anim_nr, part->nr, part->mode_nr, music);
1163 #endif
1164 }
1165
1166 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1167 {
1168   // when drawing animations to fading buffer, do not play sounds or music
1169   if (drawing_to_fading_buffer)
1170     return;
1171
1172   PlayGlobalAnimSound(part);
1173   PlayGlobalAnimMusic(part);
1174 }
1175
1176 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1177 {
1178   StopGlobalAnimSound(part);
1179   StopGlobalAnimMusic(part);
1180 }
1181
1182 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1183 {
1184   // when drawing animations to fading buffer, do not play sounds
1185   if (drawing_to_fading_buffer)
1186     return;
1187
1188   // loop sounds only expire when playing
1189   if (game_status != GAME_MODE_PLAYING)
1190     return;
1191
1192   // check if any sound is defined for this animation part
1193   if (part->sound == SND_UNDEFINED)
1194     return;
1195
1196   // normal (non-loop) sounds do not expire when playing
1197   if (!IS_LOOP_SOUND(part->sound))
1198     return;
1199
1200   // prevent expiring loop sounds when playing
1201   PlayGlobalAnimSound(part);
1202 }
1203
1204 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1205 {
1206   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1207   int mask_ce_only   = mask & ~ANIM_EVENT_PAGE_MASK;
1208
1209   if (mask & ANIM_EVENT_ANY)
1210     return (anim_event & ANIM_EVENT_ANY);
1211   else if (mask & ANIM_EVENT_SELF)
1212     return (anim_event & ANIM_EVENT_SELF);
1213   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1214     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1215   else if (mask & ANIM_EVENT_CE_CHANGE)
1216     return (anim_event == mask ||
1217             anim_event == mask_ce_only);
1218   else
1219     return (anim_event == mask ||
1220             anim_event == mask_anim_only);
1221 }
1222
1223 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1224 {
1225   struct GraphicInfo *c = &part->control_info;
1226   int i;
1227
1228   if (part->init_event_state)
1229   {
1230     int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1231
1232     for (i = 0; i < num_init_events; i++)
1233     {
1234       int init_event = GetGlobalAnimEventValue(c->init_event, i);
1235
1236       if (checkGlobalAnimEvent(init_event, mask))
1237         return TRUE;
1238     }
1239   }
1240   else if (part->anim_event_state)
1241   {
1242     int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1243
1244     for (i = 0; i < num_anim_events; i++)
1245     {
1246       int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1247
1248       if (checkGlobalAnimEvent(anim_event, mask))
1249         return TRUE;
1250     }
1251   }
1252
1253   return FALSE;
1254 }
1255
1256 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1257                                    int mx, int my)
1258 {
1259   struct GraphicInfo *g = &part->graphic_info;
1260   struct GraphicInfo *c = &part->control_info;
1261   int part_x = part->viewport_x + part->x;
1262   int part_y = part->viewport_y + part->y;
1263   int part_width  = g->width;
1264   int part_height = g->height;
1265   int x, y;
1266
1267   for (y = 0; y < c->stacked_yfactor; y++)
1268   {
1269     for (x = 0; x < c->stacked_xfactor; x++)
1270     {
1271       int part_stacked_x = part_x + x * (part_width  + c->stacked_xoffset);
1272       int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1273
1274       if (mx >= part_stacked_x &&
1275           mx <  part_stacked_x + part_width &&
1276           my >= part_stacked_y &&
1277           my <  part_stacked_y + part_height)
1278         return TRUE;
1279     }
1280   }
1281
1282   return FALSE;
1283 }
1284
1285 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1286                              int mx, int my, boolean clicked)
1287 {
1288   // check if mouse click was detected at all
1289   if (!clicked)
1290     return FALSE;
1291
1292   // check if mouse click is outside the animation part's viewport
1293   if (mx <  part->viewport_x ||
1294       mx >= part->viewport_x + part->viewport_width ||
1295       my <  part->viewport_y ||
1296       my >= part->viewport_y + part->viewport_height)
1297     return FALSE;
1298
1299   // check if mouse click is inside the animation part's (stacked) graphic
1300   if (isInsidePartStacked(part, mx, my))
1301     return TRUE;
1302
1303   return FALSE;
1304 }
1305
1306 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1307 {
1308   return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1309 }
1310
1311 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1312 {
1313   return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1314 }
1315
1316 static void SetGlobalAnimPartTileXY(struct GlobalAnimPartControlInfo *part)
1317 {
1318   // calculate playfield position (with scrolling) for related CE tile
1319   // (do not use FX/FY, which are incorrect during envelope requests)
1320   int FX0 = 2 * TILEX_VAR;      // same as FX during DRAW_TO_FIELDBUFFER
1321   int FY0 = 2 * TILEY_VAR;      // same as FY during DRAW_TO_FIELDBUFFER
1322   int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1323   int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1324   int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1325   int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1326   int cx = SX - REAL_SX;
1327   int cy = SY - REAL_SY;
1328   int x = sx - fx + cx;
1329   int y = sy - fy + cy;
1330
1331   part->tile_xoffset += part->step_xoffset;
1332   part->tile_yoffset += part->step_yoffset;
1333
1334   part->x = x + part->tile_xoffset;
1335   part->y = y + part->tile_yoffset;
1336 }
1337
1338 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1339                                      boolean *click_consumed,
1340                                      boolean *any_event_action,
1341                                      int event_value, char *info_text)
1342 {
1343   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1344
1345   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1346   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1347   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1348
1349   if (!part->is_base)
1350     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1351
1352   int anim2_nr;
1353
1354   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1355   {
1356     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1357     int part2_nr;
1358
1359     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1360     {
1361       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1362
1363       if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1364         continue;
1365
1366       if (isClickablePart(part2, mask))
1367       {
1368         part2->triggered = TRUE;
1369         *click_consumed |= clickConsumed(part);         // click was on "part"!
1370
1371 #if DEBUG_ANIM_EVENTS
1372         Debug("anim:InitGlobalAnim_Triggered",
1373               "%d.%d TRIGGERED BY %s OF %d.%d",
1374               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1375               part->old_anim_nr + 1, part->old_nr + 1);
1376 #endif
1377 #if 0
1378         Debug("anim:InitGlobalAnim_Triggered",
1379               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1380               part2->control_info.anim_event_action);
1381 #endif
1382
1383         // after executing event action, ignore any further actions
1384         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1385           *any_event_action = TRUE;
1386       }
1387
1388 #if 0
1389       struct GraphicInfo *c = &part2->control_info;
1390
1391       if (isClickablePart(part2, mask))
1392         Debug("anim:InitGlobalAnim_Triggered",
1393               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1394               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1395               anim_nr, part_nr);
1396       else
1397         Debug("anim:InitGlobalAnim_Triggered",
1398               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1399               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1400 #endif
1401     }
1402   }
1403 }
1404
1405 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1406                                                      int x, int y,
1407                                                      int trigger_x,
1408                                                      int trigger_y)
1409 {
1410   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1411
1412   int event_value = ANIM_EVENT_CE_CHANGE;
1413   int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1414   int mask = event_value | event_bits;
1415   int anim2_nr;
1416
1417   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1418   {
1419     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1420     int part2_nr;
1421
1422     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1423     {
1424       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1425
1426       if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1427         continue;
1428
1429       if (isClickablePart(part2, mask) && !part2->triggered)
1430       {
1431         struct GraphicInfo *c = &part2->control_info;
1432
1433         if (c->position == POS_CE ||
1434             c->position == POS_CE_TRIGGER)
1435         {
1436           // store CE tile and offset position to handle scrolling
1437           part2->tile_x = (c->position == POS_CE_TRIGGER ? trigger_x : x);
1438           part2->tile_y = (c->position == POS_CE_TRIGGER ? trigger_y : y);
1439           part2->tile_xoffset = c->x;
1440           part2->tile_yoffset = c->y;
1441
1442           // set resulting animation position relative to CE tile position
1443           // (but only for ".init_event", not ".anim_event" type events)
1444           if (part2->init_event_state)
1445             SetGlobalAnimPartTileXY(part2);
1446
1447           // restart animation (by using current sync frame)
1448           part2->initial_anim_sync_frame = anim_sync_frame;
1449         }
1450
1451         part2->triggered = TRUE;
1452
1453         // do not trigger any other animation if CE change event was consumed
1454         if (c->style == STYLE_CONSUME_CE_EVENT)
1455           return;
1456
1457 #if 0
1458         Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1459               "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1460 #endif
1461       }
1462     }
1463   }
1464 }
1465
1466 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1467                                   int delay_type, char *info_text)
1468 {
1469 #if DEBUG_ANIM_DELAY
1470   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1471         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1472 #endif
1473
1474   DoGlobalAnim_DelayAction(part, delay_type);
1475 }
1476
1477 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1478                                   int event_value, char *info_text)
1479 {
1480 #if DEBUG_ANIM_EVENTS
1481   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1482         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1483 #endif
1484
1485   boolean click_consumed = FALSE;
1486   boolean any_event_action = FALSE;
1487
1488   // check if this event is defined to trigger other animations
1489   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1490                            event_value, info_text);
1491 }
1492
1493 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1494                                  int state)
1495 {
1496   if (handle_click && !part->clicked)
1497     return state;
1498
1499   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1500   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1501   struct GraphicInfo *g = &part->graphic_info;
1502   struct GraphicInfo *c = &part->control_info;
1503   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1504   int alpha = (g->alpha != -1 ? g->alpha : SDL_ALPHA_OPAQUE);
1505
1506   // if game is paused, also pause playfield and door animations
1507   if (isPausedOnPlayfieldOrDoor(part))
1508     return state;
1509
1510   if (viewport_changed)
1511     state |= ANIM_STATE_RESTART;
1512
1513   if (state & ANIM_STATE_RESTART)
1514   {
1515     // when drawing animations to fading buffer, only start fixed animations
1516     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1517                                      c->y == ARG_UNDEFINED_VALUE))
1518       return ANIM_STATE_INACTIVE;
1519
1520     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1521
1522     part->init_delay_counter =
1523       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1524
1525     part->anim_delay_counter =
1526       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1527
1528     part->post_delay_counter = 0;
1529
1530     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1531     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1532
1533     part->initial_anim_sync_frame =
1534       (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1535        anim_sync_frame + part->init_delay_counter);
1536
1537     // do not re-initialize random animation frame after fade-in
1538     if (part->anim_random_frame == -1)
1539       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1540
1541     if (c->fade_mode & FADE_MODE_FADE)
1542     {
1543       // when fading in screen, first frame is 100 % transparent or opaque
1544       part->fade_delay_counter = c->fade_delay + 1;
1545       part->fade_alpha = (c->fade_mode == FADE_MODE_FADE_IN ? 0 : alpha);
1546     }
1547     else
1548     {
1549       part->fade_delay_counter = 0;
1550       part->fade_alpha = -1;
1551     }
1552
1553     if (c->direction & MV_HORIZONTAL)
1554     {
1555       int pos_bottom = part->viewport_height - g->height;
1556
1557       if (c->position == POS_TOP)
1558         part->y = 0;
1559       else if (c->position == POS_UPPER)
1560         part->y = GetSimpleRandom(pos_bottom / 2);
1561       else if (c->position == POS_MIDDLE)
1562         part->y = pos_bottom / 2;
1563       else if (c->position == POS_LOWER)
1564         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1565       else if (c->position == POS_BOTTOM)
1566         part->y = pos_bottom;
1567       else
1568         part->y = GetSimpleRandom(pos_bottom);
1569
1570       if (c->direction == MV_RIGHT)
1571       {
1572         part->step_xoffset = c->step_offset;
1573         part->x = -g->width + part->step_xoffset;
1574       }
1575       else
1576       {
1577         part->step_xoffset = -c->step_offset;
1578         part->x = part->viewport_width + part->step_xoffset;
1579       }
1580
1581       part->step_yoffset = 0;
1582     }
1583     else if (c->direction & MV_VERTICAL)
1584     {
1585       int pos_right = part->viewport_width - g->width;
1586
1587       if (c->position == POS_LEFT)
1588         part->x = 0;
1589       else if (c->position == POS_RIGHT)
1590         part->x = pos_right;
1591       else
1592         part->x = GetSimpleRandom(pos_right);
1593
1594       if (c->direction == MV_DOWN)
1595       {
1596         part->step_yoffset = c->step_offset;
1597         part->y = -g->height + part->step_yoffset;
1598       }
1599       else
1600       {
1601         part->step_yoffset = -c->step_offset;
1602         part->y = part->viewport_height + part->step_yoffset;
1603       }
1604
1605       part->step_xoffset = 0;
1606     }
1607     else
1608     {
1609       part->x = 0;
1610       part->y = 0;
1611
1612       part->step_xoffset = 0;
1613       part->step_yoffset = 0;
1614     }
1615
1616     if (part->control_info.class != get_hash_from_key("pointer"))
1617     {
1618       if (c->x != ARG_UNDEFINED_VALUE)
1619         part->x = c->x;
1620       if (c->y != ARG_UNDEFINED_VALUE)
1621         part->y = c->y;
1622     }
1623
1624     if (c->position == POS_LAST &&
1625         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1626         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1627     {
1628       part->x = anim->last_x;
1629       part->y = anim->last_y;
1630     }
1631
1632     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1633       part->step_xoffset = c->step_xoffset;
1634     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1635       part->step_yoffset = c->step_yoffset;
1636
1637     if (part->init_delay_counter == 0 &&
1638         !part->init_event_state)
1639     {
1640       PlayGlobalAnimSoundAndMusic(part);
1641
1642       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1643       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1644     }
1645     else
1646     {
1647       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1648     }
1649   }
1650
1651   if (part->clicked &&
1652       part->init_event_state)
1653   {
1654     if (part->initial_anim_sync_frame > 0)
1655       part->initial_anim_sync_frame = anim_sync_frame;
1656
1657     part->init_delay_counter = 1;
1658     part->init_event_state = FALSE;
1659
1660     part->clicked = FALSE;
1661   }
1662
1663   if (part->clicked &&
1664       part->anim_event_state)
1665   {
1666     part->anim_delay_counter = 1;
1667     part->anim_event_state = FALSE;
1668
1669     part->clicked = FALSE;
1670   }
1671
1672   if (part->init_delay_counter > 0)
1673   {
1674     part->init_delay_counter--;
1675
1676     if (part->init_delay_counter == 0)
1677     {
1678       part->init_event_state = FALSE;
1679
1680       PlayGlobalAnimSoundAndMusic(part);
1681
1682       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1683       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1684
1685       // continue with state ANIM_STATE_RUNNING (set below)
1686     }
1687     else
1688     {
1689       return ANIM_STATE_WAITING;
1690     }
1691   }
1692
1693   if (part->init_event_state)
1694     return ANIM_STATE_WAITING;
1695
1696   // animation part is now running/visible and therefore clickable
1697   part->clickable = TRUE;
1698
1699   // check if moving animation has left the visible screen area
1700   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1701       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1702       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1703       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1704   {
1705     // do not wait for "anim" events for off-screen animations
1706     part->anim_event_state = FALSE;
1707
1708     // do not stop animation before "anim" or "post" counter are finished
1709     if (part->anim_delay_counter == 0 &&
1710         part->post_delay_counter == 0)
1711     {
1712       StopGlobalAnimSoundAndMusic(part);
1713
1714       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1715
1716       part->post_delay_counter =
1717         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1718
1719       if (part->post_delay_counter > 0)
1720         return ANIM_STATE_RUNNING;
1721
1722       // drawing last frame not needed here -- animation not visible anymore
1723       return ANIM_STATE_RESTART;
1724     }
1725   }
1726
1727   if (part->anim_delay_counter > 0)
1728   {
1729     part->anim_delay_counter--;
1730
1731     if (part->anim_delay_counter == 0)
1732     {
1733       part->anim_event_state = FALSE;
1734
1735       StopGlobalAnimSoundAndMusic(part);
1736
1737       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1738       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1739
1740       part->post_delay_counter =
1741         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1742
1743       if (part->post_delay_counter > 0)
1744         return ANIM_STATE_RUNNING;
1745
1746       // additional state "RUNNING" required to not skip drawing last frame
1747       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1748     }
1749   }
1750
1751   if (part->post_delay_counter > 0)
1752   {
1753     part->post_delay_counter--;
1754
1755     if (part->post_delay_counter == 0)
1756     {
1757       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1758       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1759
1760       return ANIM_STATE_RESTART;
1761     }
1762
1763     return ANIM_STATE_WAITING;
1764   }
1765
1766   if (part->fade_delay_counter > 0)
1767   {
1768     part->fade_delay_counter--;
1769     part->fade_alpha = alpha * (c->fade_mode == FADE_MODE_FADE_IN ?
1770                                 c->fade_delay - part->fade_delay_counter :
1771                                 part->fade_delay_counter) / c->fade_delay;
1772   }
1773
1774   // special case to prevent expiring loop sounds when playing
1775   PlayGlobalAnimSoundIfLoop(part);
1776
1777   if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1778     return ANIM_STATE_RUNNING;
1779
1780 #if 0
1781   {
1782     static unsigned int last_counter = -1;
1783     unsigned int counter = Counter();
1784
1785     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1786           anim_sync_frame, counter - last_counter);
1787
1788     last_counter = counter;
1789   }
1790 #endif
1791
1792   if (c->position == POS_CE ||
1793       c->position == POS_CE_TRIGGER)
1794   {
1795     SetGlobalAnimPartTileXY(part);
1796   }
1797   else
1798   {
1799     part->x += part->step_xoffset;
1800     part->y += part->step_yoffset;
1801   }
1802
1803   anim->last_x = part->x;
1804   anim->last_y = part->y;
1805
1806   return ANIM_STATE_RUNNING;
1807 }
1808
1809 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1810                                   int action)
1811 {
1812   struct GlobalAnimPartControlInfo *part;
1813   struct GraphicInfo *c = &anim->control_info;
1814   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1815   int state, active_part_nr;
1816   int i;
1817
1818 #if 0
1819   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1820          anim->mode_nr, anim->nr, anim->num_parts);
1821   Debug("anim:HandleGlobalAnim_Main",
1822         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1823 #endif
1824
1825 #if 0
1826   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1827         (action == ANIM_START ? "ANIM_START" :
1828          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1829          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1830         anim->nr,
1831         anim->state & ANIM_STATE_RESTART,
1832         anim->state & ANIM_STATE_WAITING,
1833         anim->state & ANIM_STATE_RUNNING,
1834         anim->num_parts);
1835 #endif
1836
1837   switch (action)
1838   {
1839     case ANIM_START:
1840       anim->state = anim->last_state = ANIM_STATE_RESTART;
1841       anim->active_part_nr = anim->last_active_part_nr = 0;
1842       anim->part_counter = 0;
1843
1844       break;
1845
1846     case ANIM_CONTINUE:
1847       if (anim->state == ANIM_STATE_INACTIVE)
1848         return;
1849
1850       anim->state = anim->last_state;
1851       anim->active_part_nr = anim->last_active_part_nr;
1852
1853       break;
1854
1855     case ANIM_STOP:
1856       anim->state = ANIM_STATE_INACTIVE;
1857
1858       for (i = 0; i < num_parts; i++)
1859         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1860
1861       return;
1862
1863     default:
1864       break;
1865   }
1866
1867   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1868   {
1869 #if 0
1870     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1871           anim->mode_nr, anim->nr, num_parts);
1872 #endif
1873
1874     for (i = 0; i < num_parts; i++)
1875     {
1876       part = &anim->part[i];
1877
1878       switch (action)
1879       {
1880         case ANIM_START:
1881           anim->state = ANIM_STATE_RUNNING;
1882           part->state = ANIM_STATE_RESTART;
1883
1884           break;
1885
1886         case ANIM_CONTINUE:
1887           if (part->state == ANIM_STATE_INACTIVE)
1888             continue;
1889
1890           break;
1891
1892         case ANIM_STOP:
1893           part->state = ANIM_STATE_INACTIVE;
1894
1895           continue;
1896
1897         default:
1898           break;
1899       }
1900
1901       part->state = HandleGlobalAnim_Part(part, part->state);
1902
1903       // when animation mode is "once", stop after animation was played once
1904       if (c->anim_mode & ANIM_ONCE &&
1905           part->state & ANIM_STATE_RESTART)
1906         part->state = ANIM_STATE_INACTIVE;
1907     }
1908
1909     anim->last_state = anim->state;
1910     anim->last_active_part_nr = anim->active_part_nr;
1911
1912     return;
1913   }
1914
1915   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1916     anim->active_part_nr = getGlobalAnimationPart(anim);
1917
1918   part = &anim->part[anim->active_part_nr];
1919
1920   // first set all animation parts to "inactive", ...
1921   for (i = 0; i < num_parts; i++)
1922     anim->part[i].state = ANIM_STATE_INACTIVE;
1923
1924   // ... then set current animation part to "running" ...
1925   part->state = ANIM_STATE_RUNNING;
1926
1927   // ... unless it is waiting for an initial event
1928   if (part->init_event_state)
1929     part->state = ANIM_STATE_WAITING;
1930
1931   anim->state = HandleGlobalAnim_Part(part, anim->state);
1932
1933   if (anim->state & ANIM_STATE_RESTART)
1934     anim->part_counter++;
1935
1936   // when animation mode is "once", stop after all animations were played once
1937   if (c->anim_mode & ANIM_ONCE &&
1938       anim->part_counter == anim->num_parts)
1939     anim->state = ANIM_STATE_INACTIVE;
1940
1941   state = anim->state;
1942   active_part_nr = anim->active_part_nr;
1943
1944   // while the animation parts are pausing (waiting or inactive), play the base
1945   // (main) animation; this corresponds to the "boring player animation" logic
1946   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1947   if (anim->has_base)
1948   {
1949     if (anim->state == ANIM_STATE_WAITING ||
1950         anim->state == ANIM_STATE_INACTIVE)
1951     {
1952       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1953       part = &anim->part[anim->active_part_nr];
1954
1955       if (anim->state != anim->last_state)
1956         part->state = ANIM_STATE_RESTART;
1957
1958       anim->state = ANIM_STATE_RUNNING;
1959       part->state = HandleGlobalAnim_Part(part, part->state);
1960     }
1961   }
1962
1963   anim->last_state = state;
1964   anim->last_active_part_nr = active_part_nr;
1965 }
1966
1967 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1968 {
1969   int i;
1970
1971 #if 0
1972   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1973 #endif
1974
1975   for (i = 0; i < ctrl->num_anims; i++)
1976     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1977 }
1978
1979 static void HandleGlobalAnim(int action, int game_mode)
1980 {
1981 #if 0
1982   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1983 #endif
1984
1985   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1986 }
1987
1988 static void DoAnimationExt(void)
1989 {
1990   int i;
1991
1992 #if 0
1993   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1994 #endif
1995
1996   // global animations now synchronized with frame delay of screen update
1997   anim_sync_frame++;
1998
1999   for (i = 0; i < NUM_GAME_MODES; i++)
2000     HandleGlobalAnim(ANIM_CONTINUE, i);
2001
2002 #if 0
2003   // force screen redraw in next frame to continue drawing global animations
2004   redraw_mask = REDRAW_ALL;
2005 #endif
2006 }
2007
2008 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
2009                                      int delay_type)
2010 {
2011   int delay_action =
2012     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
2013      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
2014      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
2015      ANIM_DELAY_ACTION_NONE);
2016
2017   if (delay_action == ANIM_DELAY_ACTION_NONE)
2018     return;
2019
2020   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
2021 }
2022
2023 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
2024 {
2025   int event_action = (part->init_event_state ?
2026                       part->control_info.init_event_action :
2027                       part->control_info.anim_event_action);
2028
2029   if (event_action == ANIM_EVENT_ACTION_NONE)
2030     return FALSE;
2031
2032   if (event_action < MAX_IMAGE_FILES)
2033     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
2034   else
2035     OpenURLFromHash(anim_url_hash, event_action);
2036
2037   // check if further actions are allowed to be executed
2038   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
2039     return FALSE;
2040
2041   return TRUE;
2042 }
2043
2044 static void InitGlobalAnim_Clickable(void)
2045 {
2046   int i;
2047
2048   for (i = 0; i < num_global_anim_list; i++)
2049   {
2050     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2051
2052     if (part->triggered)
2053       part->clicked = TRUE;
2054
2055     part->triggered = FALSE;
2056     part->clickable = FALSE;
2057   }
2058 }
2059
2060 #define ANIM_CLICKED_RESET      0
2061 #define ANIM_CLICKED_PRESSED    1
2062 #define ANIM_CLICKED_RELEASED   2
2063
2064 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2065 {
2066   boolean click_consumed = FALSE;
2067   boolean anything_clicked = FALSE;
2068   boolean any_part_clicked = FALSE;
2069   boolean any_event_action = FALSE;
2070   int i;
2071
2072   // check animation parts in reverse draw order (to stop when clicked)
2073   for (i = num_global_anim_list - 1; i >= 0; i--)
2074   {
2075     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2076
2077     // if request dialog is active, only handle pointer-style animations
2078     if (game.request_active &&
2079         part->control_info.class != get_hash_from_key("pointer"))
2080       continue;
2081
2082     if (clicked_event == ANIM_CLICKED_RESET)
2083     {
2084       part->clicked = FALSE;
2085
2086       continue;
2087     }
2088
2089     if (!part->clickable)
2090       continue;
2091
2092     if (!(part->state & ANIM_STATE_RUNNING))
2093       continue;
2094
2095     // always handle "any" click events (clicking anywhere on screen) ...
2096     if (clicked_event == ANIM_CLICKED_PRESSED &&
2097         isClickablePart(part, ANIM_EVENT_ANY))
2098     {
2099 #if DEBUG_ANIM_EVENTS
2100       Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2101             part->old_anim_nr + 1, part->old_nr + 1);
2102 #endif
2103
2104       anything_clicked = part->clicked = TRUE;
2105       click_consumed |= clickConsumed(part);
2106     }
2107
2108     // always handle "unclick:any" events (releasing anywhere on screen) ...
2109     if (clicked_event == ANIM_CLICKED_RELEASED &&
2110         isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2111     {
2112 #if DEBUG_ANIM_EVENTS
2113       Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2114             part->old_anim_nr + 1, part->old_nr + 1);
2115 #endif
2116
2117       anything_clicked = part->clicked = TRUE;
2118       click_consumed |= clickConsumed(part);
2119     }
2120
2121     // ... but only handle the first (topmost) clickable animation
2122     if (any_part_clicked)
2123       continue;
2124
2125     if (clicked_event == ANIM_CLICKED_PRESSED &&
2126         isClickedPart(part, mx, my, TRUE))
2127     {
2128 #if 0
2129       Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2130             anim_nr, part_nr, part->control_info.anim_event_action);
2131 #endif
2132
2133       // after executing event action, ignore any further actions
2134       if (!any_event_action && DoGlobalAnim_EventAction(part))
2135         any_event_action = TRUE;
2136
2137       // determine if mouse clicks should be blocked from other animations
2138       any_part_clicked |= clickConsumed(part);
2139
2140       if (isClickablePart(part, ANIM_EVENT_SELF))
2141       {
2142 #if DEBUG_ANIM_EVENTS
2143         Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2144               part->old_anim_nr + 1, part->old_nr + 1);
2145 #endif
2146
2147         anything_clicked = part->clicked = TRUE;
2148         click_consumed |= clickConsumed(part);
2149       }
2150
2151       // determine if mouse clicks should be blocked by this animation
2152       click_consumed |= clickBlocked(part);
2153
2154       // check if this click is defined to trigger other animations
2155       InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2156                                ANIM_EVENT_CLICK, "CLICK");
2157     }
2158   }
2159
2160   if (anything_clicked)
2161   {
2162     handle_click = TRUE;
2163
2164     for (i = 0; i < NUM_GAME_MODES; i++)
2165       HandleGlobalAnim(ANIM_CONTINUE, i);
2166
2167     handle_click = FALSE;
2168
2169     // prevent ignoring release event if processed within same game frame
2170     StopProcessingEvents();
2171   }
2172
2173   return (click_consumed || any_event_action);
2174 }
2175
2176 static void ResetGlobalAnim_Clickable(void)
2177 {
2178   InitGlobalAnim_Clickable();
2179 }
2180
2181 static void ResetGlobalAnim_Clicked(void)
2182 {
2183   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2184 }
2185
2186 void RestartGlobalAnimsByStatus(int status)
2187 {
2188   int anim_status_last = global.anim_status;
2189
2190   global.anim_status = status;
2191
2192   // force restarting global animations by changed global animation status
2193   DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2194
2195   global.anim_status = anim_status_last;
2196 }
2197
2198 void SetAnimStatusBeforeFading(int status)
2199 {
2200   anim_status_last_before_fading = status;
2201 }
2202
2203 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2204 {
2205   static boolean click_consumed = FALSE;
2206   static int last_button = 0;
2207   boolean press_event;
2208   boolean release_event;
2209   boolean click_consumed_current = click_consumed;
2210
2211   if (button != 0 && force_click)
2212     last_button = 0;
2213
2214   // check if button state has changed since last invocation
2215   press_event   = (button != 0 && last_button == 0);
2216   release_event = (button == 0 && last_button != 0);
2217   last_button = button;
2218
2219   if (press_event)
2220   {
2221     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2222     click_consumed_current = click_consumed;
2223   }
2224
2225   if (release_event)
2226   {
2227     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2228     click_consumed = FALSE;
2229   }
2230
2231   return click_consumed_current;
2232 }
2233
2234 int getGlobalAnimSyncFrame(void)
2235 {
2236   return anim_sync_frame;
2237 }
2238
2239 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y,
2240                                           int trigger_x, int trigger_y)
2241 {
2242   if (!IS_CUSTOM_ELEMENT(element))
2243     return;
2244
2245   // custom element stored as 0 to 255, change page stored as 1 to 32
2246   InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2247                                            x, y, trigger_x, trigger_y);
2248 }