reverted setting (and maybe inheriting) draw order for global animations
[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         // force pointer-style animations to pass-through clicks
602         if (part->control_info.class == get_hash_from_key("pointer") &&
603             part->control_info.style == STYLE_DEFAULT)
604           part->control_info.style |= STYLE_PASSTHROUGH;
605       }
606
607       if (anim->num_parts > 0 || anim->has_base)
608       {
609         ctrl->num_anims++;
610         anim_nr++;
611       }
612     }
613   }
614
615   InitToonControls();
616
617   // create list of all animation parts
618   num_global_anim_list = 0;
619   for (m = 0; m < NUM_GAME_MODES; m++)
620     for (a = 0; a < global_anim_ctrl[m].num_anims; a++)
621       for (p = 0; p < global_anim_ctrl[m].anim[a].num_parts_all; p++)
622         global_anim_list[num_global_anim_list++] =
623           &global_anim_ctrl[m].anim[a].part[p];
624
625   // sort list of all animation parts according to draw_order and number
626   qsort(global_anim_list, num_global_anim_list,
627         sizeof(struct GlobalAnimPartControlInfo *),
628         compareGlobalAnimPartControlInfo);
629
630   for (i = 0; i < NUM_GAME_MODES; i++)
631     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
632   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
633     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
634       game_mode_anim_classes_list[i].class;
635
636   for (i = 0; i < NUM_ANIM_CLASSES; i++)
637     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
638   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
639     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
640       anim_class_game_modes_list[i].game_mode;
641
642   anim_status_last_before_fading = GAME_MODE_LOADING;
643   anim_status_last = GAME_MODE_LOADING;
644   anim_classes_last = ANIM_CLASS_NONE;
645 }
646
647 static void SetGlobalAnimEventsForCustomElements(int list_pos)
648 {
649   int num_events = GetGlobalAnimEventValueCount(list_pos);
650   int i;
651
652   for (i = 0; i < num_events; i++)
653   {
654     int event = GetGlobalAnimEventValue(list_pos, i);
655
656     if (event & ANIM_EVENT_CE_CHANGE)
657     {
658       int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
659
660       if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
661         element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
662     }
663   }
664 }
665
666 void InitGlobalAnimEventsForCustomElements(void)
667 {
668   int m, a, p;
669   int control;
670
671   // custom element events for global animations only relevant while playing
672   m = GAME_MODE_PLAYING;
673
674   for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
675   {
676     int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
677
678     control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
679
680     // if no base animation parameters defined, use default values
681     if (control == IMG_UNDEFINED)
682       control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
683
684     SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
685     SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
686
687     for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
688     {
689       control = global_anim_info[ctrl_id].graphic[p][m];
690
691       if (control == IMG_UNDEFINED)
692         continue;
693
694       SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
695       SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
696     }
697   }
698 }
699
700 void InitGlobalAnimations(void)
701 {
702   InitGlobalAnimControls();
703 }
704
705 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
706                                 Bitmap *src_bitmap, int src_x0, int src_y0,
707                                 int drawing_target)
708 {
709   struct GraphicInfo *g = &part->graphic_info;
710   struct GraphicInfo *c = &part->control_info;
711   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
712     (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
713   void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
714     (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
715   Bitmap *fade_bitmap =
716     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
717      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
718   int alpha = (c->fade_mode & FADE_MODE_FADE ? part->fade_alpha : g->alpha);
719   int x, y;
720
721   for (y = 0; y < c->stacked_yfactor; y++)
722   {
723     for (x = 0; x < c->stacked_xfactor; x++)
724     {
725       int src_x = src_x0;
726       int src_y = src_y0;
727       int dst_x = part->x + x * (g->width  + c->stacked_xoffset);
728       int dst_y = part->y + y * (g->height + c->stacked_yoffset);
729       int cut_x = 0;
730       int cut_y = 0;
731       int width  = g->width;
732       int height = g->height;
733
734       if (dst_x < 0)
735       {
736         width += dst_x;
737         cut_x = -dst_x;
738         dst_x = 0;
739       }
740       else if (dst_x > part->viewport_width - g->width)
741       {
742         width -= (dst_x - (part->viewport_width - g->width));
743       }
744
745       if (dst_y < 0)
746       {
747         height += dst_y;
748         cut_y  = -dst_y;
749         dst_y = 0;
750       }
751       else if (dst_y > part->viewport_height - g->height)
752       {
753         height -= (dst_y - (part->viewport_height - g->height));
754       }
755
756       if (width <= 0 || height <= 0)
757         continue;
758
759       src_x += cut_x;
760       src_y += cut_y;
761
762       dst_x += part->viewport_x;
763       dst_y += part->viewport_y;
764
765       SetBitmapAlphaNextBlit(src_bitmap, alpha);
766
767       if (drawing_target == DRAW_TO_SCREEN)
768         blit_screen(src_bitmap, src_x, src_y, width, height,
769                     dst_x, dst_y);
770       else
771         blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
772                     dst_x, dst_y);
773     }
774   }
775 }
776
777 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
778 {
779   int game_mode_anim_action[NUM_GAME_MODES];
780   int mode_nr;
781   int i;
782
783   if (!setup.toons)
784     return;
785
786   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
787       drawing_target == DRAW_TO_SCREEN)
788     DoAnimationExt();
789
790   // always start with reliable default values (no animation actions)
791   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
792     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
793
794   if (global.anim_status != anim_status_last)
795   {
796     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
797     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
798     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
799     int i;
800
801     if (drawing_target == DRAW_TO_FADE_TARGET)
802       after_fading = TRUE;
803
804     // special case: changing from/to these screens is done without fading
805     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
806         global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
807         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
808         anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
809       after_fading = TRUE;
810
811     // ---------- part 1 ------------------------------------------------------
812     // start or stop global animations by change of game mode
813     // (special handling of animations for "current screen" and "all screens")
814
815     if (global.anim_status_next != anim_status_last_before_fading)
816     {
817       // stop animations for last screen before fading to new screen
818       game_mode_anim_action[anim_status_last] = ANIM_STOP;
819
820       // start animations for current screen after fading to new screen
821       game_mode_anim_action[global.anim_status] = ANIM_START;
822     }
823
824     // start animations for all screens after loading new artwork set
825     if (anim_status_last == GAME_MODE_LOADING)
826       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
827
828     // ---------- part 2 ------------------------------------------------------
829     // start or stop global animations by change of animation class
830     // (generic handling of animations for "class of screens")
831
832     for (i = 0; i < NUM_ANIM_CLASSES; i++)
833     {
834       int anim_class_check = (1 << i);
835       int anim_class_game_mode = anim_class_game_modes[i];
836       int anim_class_last = anim_classes_last & anim_class_check;
837       int anim_class_next = anim_classes_next & anim_class_check;
838
839       // stop animations for changed screen class before fading to new screen
840       if (before_fading && anim_class_last && !anim_class_next)
841         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
842
843       // start animations for changed screen class after fading to new screen
844       if (after_fading && !anim_class_last && anim_class_next)
845         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
846     }
847
848     if (drawing_target == DRAW_TO_SCREEN)
849     {
850       if (after_fading)
851       {
852         anim_classes_last = anim_classes_next;
853         anim_status_last_before_fading = global.anim_status;
854       }
855
856       anim_status_last = global.anim_status;
857
858       // start or stop animations determined to be started or stopped above
859       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
860         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
861           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
862     }
863     else if (drawing_target == DRAW_TO_FADE_TARGET)
864     {
865       drawing_to_fading_buffer = TRUE;
866
867       // start animations determined to be (temporary) started above
868       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
869         if (game_mode_anim_action[mode_nr] == ANIM_START)
870           HandleGlobalAnim(ANIM_START, mode_nr);
871     }
872   }
873
874   // when restarting global animations, do not redraw them, but stop here
875   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
876     return;
877
878   if (global.anim_status == GAME_MODE_LOADING)
879     return;
880
881   for (i = 0; i < num_global_anim_list; i++)
882   {
883     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
884     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
885     struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
886     struct GraphicInfo *g = &part->graphic_info;
887     Bitmap *src_bitmap;
888     int src_x, src_y;
889     int sync_frame;
890     int frame;
891     int last_anim_random_frame = gfx.anim_random_frame;
892
893     // when preparing source fading buffer, only draw animations to be stopped
894     if (drawing_target == DRAW_TO_FADE_SOURCE &&
895         game_mode_anim_action[part->mode_nr] != ANIM_STOP)
896       continue;
897
898     // when preparing target fading buffer, only draw animations to be started
899     if (drawing_target == DRAW_TO_FADE_TARGET &&
900         game_mode_anim_action[part->mode_nr] != ANIM_START)
901       continue;
902
903     if (!(anim->state & ANIM_STATE_RUNNING))
904       continue;
905
906     if (!(part->state & ANIM_STATE_RUNNING))
907       continue;
908
909     if (part->drawing_stage != drawing_stage)
910       continue;
911
912     // if game is paused, also pause playfield and door animations
913     if (isPausedOnPlayfieldOrDoor(part))
914       part->initial_anim_sync_frame++;
915
916     sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
917
918     // re-initialize random animation frame after animation delay
919     if (g->anim_mode == ANIM_RANDOM &&
920         sync_frame % g->anim_delay == 0 &&
921         sync_frame > 0)
922       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
923
924     gfx.anim_random_frame = part->anim_random_frame;
925
926     frame = getAnimationFrame(g->anim_frames, g->anim_delay,
927                               g->anim_mode, g->anim_start_frame,
928                               sync_frame);
929
930     gfx.anim_random_frame = last_anim_random_frame;
931
932     getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
933                                &src_x, &src_y);
934
935     BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
936   }
937
938   if (drawing_target == DRAW_TO_FADE_TARGET)
939   {
940     // stop animations determined to be (temporary) started above
941     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
942       if (game_mode_anim_action[mode_nr] == ANIM_START)
943         HandleGlobalAnim(ANIM_STOP, mode_nr);
944
945     drawing_to_fading_buffer = FALSE;
946   }
947 }
948
949 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
950 {
951   int last_cursor_mode_override = gfx.cursor_mode_override;
952
953   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
954   {
955     ResetGlobalAnim_Clickable();
956
957     gfx.cursor_mode_override = CURSOR_UNDEFINED;
958   }
959
960   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
961
962   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
963   {
964     ResetGlobalAnim_Clicked();
965   }
966
967   if (gfx.cursor_mode_override != last_cursor_mode_override)
968     SetMouseCursor(gfx.cursor_mode);
969 }
970
971 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
972 {
973   int viewport_x;
974   int viewport_y;
975   int viewport_width;
976   int viewport_height;
977   boolean changed = FALSE;
978
979   if (part->last_anim_status == global.anim_status &&
980       part->control_info.class != get_hash_from_key("pointer"))
981     return FALSE;
982
983   part->last_anim_status = global.anim_status;
984
985   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
986
987   part->class_playfield_or_door = FALSE;
988
989   if (part->control_info.class == get_hash_from_key("window") ||
990       part->control_info.class == get_hash_from_key("border"))
991   {
992     viewport_x = 0;
993     viewport_y = 0;
994     viewport_width  = WIN_XSIZE;
995     viewport_height = WIN_YSIZE;
996
997     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
998   }
999   else if (part->control_info.class == get_hash_from_key("pointer"))
1000   {
1001     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1002     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1003
1004     // prevent displaying off-screen custom mouse cursor in upper left corner
1005     if (gfx.mouse_x == POS_OFFSCREEN &&
1006         gfx.mouse_y == POS_OFFSCREEN)
1007       mx = my = POS_OFFSCREEN;
1008
1009     viewport_x = mx - part->control_info.x;
1010     viewport_y = my - part->control_info.y;
1011     viewport_width  = part->graphic_info.width;
1012     viewport_height = part->graphic_info.height;
1013
1014     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1015
1016     // do not use global animation mouse pointer when reloading artwork
1017     if (global.anim_status != GAME_MODE_LOADING)
1018       gfx.cursor_mode_override = CURSOR_NONE;
1019   }
1020   else if (part->control_info.class == get_hash_from_key("door_1"))
1021   {
1022     viewport_x = DX;
1023     viewport_y = DY;
1024     viewport_width  = DXSIZE;
1025     viewport_height = DYSIZE;
1026
1027     part->class_playfield_or_door = TRUE;
1028   }
1029   else if (part->control_info.class == get_hash_from_key("door_2"))
1030   {
1031     if (part->mode_nr == GAME_MODE_EDITOR)
1032     {
1033       viewport_x = EX;
1034       viewport_y = EY;
1035       viewport_width  = EXSIZE;
1036       viewport_height = EYSIZE;
1037     }
1038     else
1039     {
1040       viewport_x = VX;
1041       viewport_y = VY;
1042       viewport_width  = VXSIZE;
1043       viewport_height = VYSIZE;
1044     }
1045
1046     part->class_playfield_or_door = TRUE;
1047   }
1048   else          // default: "playfield"
1049   {
1050     viewport_x = REAL_SX;
1051     viewport_y = REAL_SY;
1052     viewport_width  = FULL_SXSIZE;
1053     viewport_height = FULL_SYSIZE;
1054
1055     part->class_playfield_or_door = TRUE;
1056   }
1057
1058   if (viewport_x != part->viewport_x ||
1059       viewport_y != part->viewport_y ||
1060       viewport_width  != part->viewport_width ||
1061       viewport_height != part->viewport_height)
1062   {
1063     part->viewport_x = viewport_x;
1064     part->viewport_y = viewport_y;
1065     part->viewport_width  = viewport_width;
1066     part->viewport_height = viewport_height;
1067
1068     if (part->control_info.class != get_hash_from_key("pointer"))
1069       changed = TRUE;
1070   }
1071
1072   return changed;
1073 }
1074
1075 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1076 {
1077   int sound = part->sound;
1078
1079   if (sound == SND_UNDEFINED)
1080     return;
1081
1082   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1083       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1084     return;
1085
1086   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1087   if (IS_LOOP_SOUND(sound))
1088     PlaySoundLoop(sound);
1089   else
1090     PlaySound(sound);
1091
1092 #if 0
1093   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1094          part->anim_nr, part->nr, part->mode_nr, sound);
1095 #endif
1096 }
1097
1098 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1099 {
1100   int sound = part->sound;
1101
1102   if (sound == SND_UNDEFINED)
1103     return;
1104
1105   StopSound(sound);
1106
1107 #if 0
1108   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1109         part->anim_nr, part->nr, part->mode_nr, sound);
1110 #endif
1111 }
1112
1113 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1114 {
1115   int music = part->music;
1116
1117   if (music == MUS_UNDEFINED)
1118     return;
1119
1120   if (!setup.sound_music)
1121     return;
1122
1123   if (IS_LOOP_MUSIC(music))
1124     PlayMusicLoop(music);
1125   else
1126     PlayMusic(music);
1127
1128 #if 0
1129   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1130         part->anim_nr, part->nr, part->mode_nr, music);
1131 #endif
1132 }
1133
1134 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1135 {
1136   int music = part->music;
1137
1138   if (music == MUS_UNDEFINED)
1139     return;
1140
1141   char *anim_music = getMusicInfoEntryFilename(music);
1142   char *curr_music = getCurrentlyPlayingMusicFilename();
1143
1144   // do not stop music if global anim music differs from current music
1145   if (!strEqual(curr_music, anim_music))
1146     return;
1147
1148   StopMusic();
1149
1150 #if 0
1151   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1152         part->anim_nr, part->nr, part->mode_nr, music);
1153 #endif
1154 }
1155
1156 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1157 {
1158   // when drawing animations to fading buffer, do not play sounds or music
1159   if (drawing_to_fading_buffer)
1160     return;
1161
1162   PlayGlobalAnimSound(part);
1163   PlayGlobalAnimMusic(part);
1164 }
1165
1166 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1167 {
1168   StopGlobalAnimSound(part);
1169   StopGlobalAnimMusic(part);
1170 }
1171
1172 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1173 {
1174   // when drawing animations to fading buffer, do not play sounds
1175   if (drawing_to_fading_buffer)
1176     return;
1177
1178   // loop sounds only expire when playing
1179   if (game_status != GAME_MODE_PLAYING)
1180     return;
1181
1182   // check if any sound is defined for this animation part
1183   if (part->sound == SND_UNDEFINED)
1184     return;
1185
1186   // normal (non-loop) sounds do not expire when playing
1187   if (!IS_LOOP_SOUND(part->sound))
1188     return;
1189
1190   // prevent expiring loop sounds when playing
1191   PlayGlobalAnimSound(part);
1192 }
1193
1194 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1195 {
1196   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1197   int mask_ce_only   = mask & ~ANIM_EVENT_PAGE_MASK;
1198
1199   if (mask & ANIM_EVENT_ANY)
1200     return (anim_event & ANIM_EVENT_ANY);
1201   else if (mask & ANIM_EVENT_SELF)
1202     return (anim_event & ANIM_EVENT_SELF);
1203   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1204     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1205   else if (mask & ANIM_EVENT_CE_CHANGE)
1206     return (anim_event == mask ||
1207             anim_event == mask_ce_only);
1208   else
1209     return (anim_event == mask ||
1210             anim_event == mask_anim_only);
1211 }
1212
1213 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1214 {
1215   struct GraphicInfo *c = &part->control_info;
1216   int i;
1217
1218   if (part->init_event_state)
1219   {
1220     int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1221
1222     for (i = 0; i < num_init_events; i++)
1223     {
1224       int init_event = GetGlobalAnimEventValue(c->init_event, i);
1225
1226       if (checkGlobalAnimEvent(init_event, mask))
1227         return TRUE;
1228     }
1229   }
1230   else if (part->anim_event_state)
1231   {
1232     int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1233
1234     for (i = 0; i < num_anim_events; i++)
1235     {
1236       int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1237
1238       if (checkGlobalAnimEvent(anim_event, mask))
1239         return TRUE;
1240     }
1241   }
1242
1243   return FALSE;
1244 }
1245
1246 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1247                                    int mx, int my)
1248 {
1249   struct GraphicInfo *g = &part->graphic_info;
1250   struct GraphicInfo *c = &part->control_info;
1251   int part_x = part->viewport_x + part->x;
1252   int part_y = part->viewport_y + part->y;
1253   int part_width  = g->width;
1254   int part_height = g->height;
1255   int x, y;
1256
1257   for (y = 0; y < c->stacked_yfactor; y++)
1258   {
1259     for (x = 0; x < c->stacked_xfactor; x++)
1260     {
1261       int part_stacked_x = part_x + x * (part_width  + c->stacked_xoffset);
1262       int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1263
1264       if (mx >= part_stacked_x &&
1265           mx <  part_stacked_x + part_width &&
1266           my >= part_stacked_y &&
1267           my <  part_stacked_y + part_height)
1268         return TRUE;
1269     }
1270   }
1271
1272   return FALSE;
1273 }
1274
1275 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1276                              int mx, int my, boolean clicked)
1277 {
1278   // check if mouse click was detected at all
1279   if (!clicked)
1280     return FALSE;
1281
1282   // check if mouse click is outside the animation part's viewport
1283   if (mx <  part->viewport_x ||
1284       mx >= part->viewport_x + part->viewport_width ||
1285       my <  part->viewport_y ||
1286       my >= part->viewport_y + part->viewport_height)
1287     return FALSE;
1288
1289   // check if mouse click is inside the animation part's (stacked) graphic
1290   if (isInsidePartStacked(part, mx, my))
1291     return TRUE;
1292
1293   return FALSE;
1294 }
1295
1296 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1297 {
1298   return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1299 }
1300
1301 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1302 {
1303   return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1304 }
1305
1306 static void SetGlobalAnimPartTileXY(struct GlobalAnimPartControlInfo *part)
1307 {
1308   // calculate playfield position (with scrolling) for related CE tile
1309   // (do not use FX/FY, which are incorrect during envelope requests)
1310   int FX0 = 2 * TILEX_VAR;      // same as FX during DRAW_TO_FIELDBUFFER
1311   int FY0 = 2 * TILEY_VAR;      // same as FY during DRAW_TO_FIELDBUFFER
1312   int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1313   int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1314   int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1315   int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1316   int cx = SX - REAL_SX;
1317   int cy = SY - REAL_SY;
1318   int x = sx - fx + cx;
1319   int y = sy - fy + cy;
1320
1321   part->tile_xoffset += part->step_xoffset;
1322   part->tile_yoffset += part->step_yoffset;
1323
1324   part->x = x + part->tile_xoffset;
1325   part->y = y + part->tile_yoffset;
1326 }
1327
1328 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1329                                      boolean *click_consumed,
1330                                      boolean *any_event_action,
1331                                      int event_value, char *info_text)
1332 {
1333   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1334
1335   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1336   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1337   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1338
1339   if (!part->is_base)
1340     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1341
1342   int anim2_nr;
1343
1344   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1345   {
1346     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1347     int part2_nr;
1348
1349     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1350     {
1351       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1352
1353       if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1354         continue;
1355
1356       if (isClickablePart(part2, mask))
1357       {
1358         part2->triggered = TRUE;
1359         *click_consumed |= clickConsumed(part);         // click was on "part"!
1360
1361 #if DEBUG_ANIM_EVENTS
1362         Debug("anim:InitGlobalAnim_Triggered",
1363               "%d.%d TRIGGERED BY %s OF %d.%d",
1364               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1365               part->old_anim_nr + 1, part->old_nr + 1);
1366 #endif
1367 #if 0
1368         Debug("anim:InitGlobalAnim_Triggered",
1369               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1370               part2->control_info.anim_event_action);
1371 #endif
1372
1373         // after executing event action, ignore any further actions
1374         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1375           *any_event_action = TRUE;
1376       }
1377
1378 #if 0
1379       struct GraphicInfo *c = &part2->control_info;
1380
1381       if (isClickablePart(part2, mask))
1382         Debug("anim:InitGlobalAnim_Triggered",
1383               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1384               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1385               anim_nr, part_nr);
1386       else
1387         Debug("anim:InitGlobalAnim_Triggered",
1388               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1389               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1390 #endif
1391     }
1392   }
1393 }
1394
1395 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1396                                                      int x, int y,
1397                                                      int trigger_x,
1398                                                      int trigger_y)
1399 {
1400   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1401
1402   int event_value = ANIM_EVENT_CE_CHANGE;
1403   int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1404   int mask = event_value | event_bits;
1405   int anim2_nr;
1406
1407   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1408   {
1409     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1410     int part2_nr;
1411
1412     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1413     {
1414       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1415
1416       if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1417         continue;
1418
1419       if (isClickablePart(part2, mask) && !part2->triggered)
1420       {
1421         struct GraphicInfo *c = &part2->control_info;
1422
1423         if (c->position == POS_CE ||
1424             c->position == POS_CE_TRIGGER)
1425         {
1426           // store CE tile and offset position to handle scrolling
1427           part2->tile_x = (c->position == POS_CE_TRIGGER ? trigger_x : x);
1428           part2->tile_y = (c->position == POS_CE_TRIGGER ? trigger_y : y);
1429           part2->tile_xoffset = c->x;
1430           part2->tile_yoffset = c->y;
1431
1432           // set resulting animation position relative to CE tile position
1433           // (but only for ".init_event", not ".anim_event" type events)
1434           if (part2->init_event_state)
1435             SetGlobalAnimPartTileXY(part2);
1436
1437           // restart animation (by using current sync frame)
1438           part2->initial_anim_sync_frame = anim_sync_frame;
1439         }
1440
1441         part2->triggered = TRUE;
1442
1443         // do not trigger any other animation if CE change event was consumed
1444         if (c->style == STYLE_CONSUME_CE_EVENT)
1445           return;
1446
1447 #if 0
1448         Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1449               "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1450 #endif
1451       }
1452     }
1453   }
1454 }
1455
1456 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1457                                   int delay_type, char *info_text)
1458 {
1459 #if DEBUG_ANIM_DELAY
1460   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1461         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1462 #endif
1463
1464   DoGlobalAnim_DelayAction(part, delay_type);
1465 }
1466
1467 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1468                                   int event_value, char *info_text)
1469 {
1470 #if DEBUG_ANIM_EVENTS
1471   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1472         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1473 #endif
1474
1475   boolean click_consumed = FALSE;
1476   boolean any_event_action = FALSE;
1477
1478   // check if this event is defined to trigger other animations
1479   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1480                            event_value, info_text);
1481 }
1482
1483 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1484                                  int state)
1485 {
1486   if (handle_click && !part->clicked)
1487     return state;
1488
1489   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1490   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1491   struct GraphicInfo *g = &part->graphic_info;
1492   struct GraphicInfo *c = &part->control_info;
1493   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1494   int alpha = (g->alpha != -1 ? g->alpha : SDL_ALPHA_OPAQUE);
1495
1496   // if game is paused, also pause playfield and door animations
1497   if (isPausedOnPlayfieldOrDoor(part))
1498     return state;
1499
1500   if (viewport_changed)
1501     state |= ANIM_STATE_RESTART;
1502
1503   if (state & ANIM_STATE_RESTART)
1504   {
1505     // when drawing animations to fading buffer, only start fixed animations
1506     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1507                                      c->y == ARG_UNDEFINED_VALUE))
1508       return ANIM_STATE_INACTIVE;
1509
1510     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1511
1512     part->init_delay_counter =
1513       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1514
1515     part->anim_delay_counter =
1516       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1517
1518     part->post_delay_counter = 0;
1519
1520     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1521     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1522
1523     part->initial_anim_sync_frame =
1524       (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1525        anim_sync_frame + part->init_delay_counter);
1526
1527     // do not re-initialize random animation frame after fade-in
1528     if (part->anim_random_frame == -1)
1529       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1530
1531     if (c->fade_mode & FADE_MODE_FADE)
1532     {
1533       // when fading in screen, first frame is 100 % transparent or opaque
1534       part->fade_delay_counter = c->fade_delay + 1;
1535       part->fade_alpha = (c->fade_mode == FADE_MODE_FADE_IN ? 0 : alpha);
1536     }
1537     else
1538     {
1539       part->fade_delay_counter = 0;
1540       part->fade_alpha = -1;
1541     }
1542
1543     if (c->direction & MV_HORIZONTAL)
1544     {
1545       int pos_bottom = part->viewport_height - g->height;
1546
1547       if (c->position == POS_TOP)
1548         part->y = 0;
1549       else if (c->position == POS_UPPER)
1550         part->y = GetSimpleRandom(pos_bottom / 2);
1551       else if (c->position == POS_MIDDLE)
1552         part->y = pos_bottom / 2;
1553       else if (c->position == POS_LOWER)
1554         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1555       else if (c->position == POS_BOTTOM)
1556         part->y = pos_bottom;
1557       else
1558         part->y = GetSimpleRandom(pos_bottom);
1559
1560       if (c->direction == MV_RIGHT)
1561       {
1562         part->step_xoffset = c->step_offset;
1563         part->x = -g->width + part->step_xoffset;
1564       }
1565       else
1566       {
1567         part->step_xoffset = -c->step_offset;
1568         part->x = part->viewport_width + part->step_xoffset;
1569       }
1570
1571       part->step_yoffset = 0;
1572     }
1573     else if (c->direction & MV_VERTICAL)
1574     {
1575       int pos_right = part->viewport_width - g->width;
1576
1577       if (c->position == POS_LEFT)
1578         part->x = 0;
1579       else if (c->position == POS_RIGHT)
1580         part->x = pos_right;
1581       else
1582         part->x = GetSimpleRandom(pos_right);
1583
1584       if (c->direction == MV_DOWN)
1585       {
1586         part->step_yoffset = c->step_offset;
1587         part->y = -g->height + part->step_yoffset;
1588       }
1589       else
1590       {
1591         part->step_yoffset = -c->step_offset;
1592         part->y = part->viewport_height + part->step_yoffset;
1593       }
1594
1595       part->step_xoffset = 0;
1596     }
1597     else
1598     {
1599       part->x = 0;
1600       part->y = 0;
1601
1602       part->step_xoffset = 0;
1603       part->step_yoffset = 0;
1604     }
1605
1606     if (part->control_info.class != get_hash_from_key("pointer"))
1607     {
1608       if (c->x != ARG_UNDEFINED_VALUE)
1609         part->x = c->x;
1610       if (c->y != ARG_UNDEFINED_VALUE)
1611         part->y = c->y;
1612     }
1613
1614     if (c->position == POS_LAST &&
1615         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1616         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1617     {
1618       part->x = anim->last_x;
1619       part->y = anim->last_y;
1620     }
1621
1622     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1623       part->step_xoffset = c->step_xoffset;
1624     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1625       part->step_yoffset = c->step_yoffset;
1626
1627     if (part->init_delay_counter == 0 &&
1628         !part->init_event_state)
1629     {
1630       PlayGlobalAnimSoundAndMusic(part);
1631
1632       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1633       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1634     }
1635     else
1636     {
1637       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1638     }
1639   }
1640
1641   if (part->clicked &&
1642       part->init_event_state)
1643   {
1644     if (part->initial_anim_sync_frame > 0)
1645       part->initial_anim_sync_frame = anim_sync_frame;
1646
1647     part->init_delay_counter = 1;
1648     part->init_event_state = FALSE;
1649
1650     part->clicked = FALSE;
1651   }
1652
1653   if (part->clicked &&
1654       part->anim_event_state)
1655   {
1656     part->anim_delay_counter = 1;
1657     part->anim_event_state = FALSE;
1658
1659     part->clicked = FALSE;
1660   }
1661
1662   if (part->init_delay_counter > 0)
1663   {
1664     part->init_delay_counter--;
1665
1666     if (part->init_delay_counter == 0)
1667     {
1668       part->init_event_state = FALSE;
1669
1670       PlayGlobalAnimSoundAndMusic(part);
1671
1672       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1673       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1674
1675       // continue with state ANIM_STATE_RUNNING (set below)
1676     }
1677     else
1678     {
1679       return ANIM_STATE_WAITING;
1680     }
1681   }
1682
1683   if (part->init_event_state)
1684     return ANIM_STATE_WAITING;
1685
1686   // animation part is now running/visible and therefore clickable
1687   part->clickable = TRUE;
1688
1689   // check if moving animation has left the visible screen area
1690   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1691       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1692       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1693       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1694   {
1695     // do not wait for "anim" events for off-screen animations
1696     part->anim_event_state = FALSE;
1697
1698     // do not stop animation before "anim" or "post" counter are finished
1699     if (part->anim_delay_counter == 0 &&
1700         part->post_delay_counter == 0)
1701     {
1702       StopGlobalAnimSoundAndMusic(part);
1703
1704       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1705
1706       part->post_delay_counter =
1707         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1708
1709       if (part->post_delay_counter > 0)
1710         return ANIM_STATE_RUNNING;
1711
1712       // drawing last frame not needed here -- animation not visible anymore
1713       return ANIM_STATE_RESTART;
1714     }
1715   }
1716
1717   if (part->anim_delay_counter > 0)
1718   {
1719     part->anim_delay_counter--;
1720
1721     if (part->anim_delay_counter == 0)
1722     {
1723       part->anim_event_state = FALSE;
1724
1725       StopGlobalAnimSoundAndMusic(part);
1726
1727       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1728       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1729
1730       part->post_delay_counter =
1731         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1732
1733       if (part->post_delay_counter > 0)
1734         return ANIM_STATE_RUNNING;
1735
1736       // additional state "RUNNING" required to not skip drawing last frame
1737       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1738     }
1739   }
1740
1741   if (part->post_delay_counter > 0)
1742   {
1743     part->post_delay_counter--;
1744
1745     if (part->post_delay_counter == 0)
1746     {
1747       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1748       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1749
1750       return ANIM_STATE_RESTART;
1751     }
1752
1753     return ANIM_STATE_WAITING;
1754   }
1755
1756   if (part->fade_delay_counter > 0)
1757   {
1758     part->fade_delay_counter--;
1759     part->fade_alpha = alpha * (c->fade_mode == FADE_MODE_FADE_IN ?
1760                                 c->fade_delay - part->fade_delay_counter :
1761                                 part->fade_delay_counter) / c->fade_delay;
1762   }
1763
1764   // special case to prevent expiring loop sounds when playing
1765   PlayGlobalAnimSoundIfLoop(part);
1766
1767   if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1768     return ANIM_STATE_RUNNING;
1769
1770 #if 0
1771   {
1772     static unsigned int last_counter = -1;
1773     unsigned int counter = Counter();
1774
1775     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1776           anim_sync_frame, counter - last_counter);
1777
1778     last_counter = counter;
1779   }
1780 #endif
1781
1782   if (c->position == POS_CE ||
1783       c->position == POS_CE_TRIGGER)
1784   {
1785     SetGlobalAnimPartTileXY(part);
1786   }
1787   else
1788   {
1789     part->x += part->step_xoffset;
1790     part->y += part->step_yoffset;
1791   }
1792
1793   anim->last_x = part->x;
1794   anim->last_y = part->y;
1795
1796   return ANIM_STATE_RUNNING;
1797 }
1798
1799 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1800                                   int action)
1801 {
1802   struct GlobalAnimPartControlInfo *part;
1803   struct GraphicInfo *c = &anim->control_info;
1804   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1805   int state, active_part_nr;
1806   int i;
1807
1808 #if 0
1809   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1810          anim->mode_nr, anim->nr, anim->num_parts);
1811   Debug("anim:HandleGlobalAnim_Main",
1812         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1813 #endif
1814
1815 #if 0
1816   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1817         (action == ANIM_START ? "ANIM_START" :
1818          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1819          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1820         anim->nr,
1821         anim->state & ANIM_STATE_RESTART,
1822         anim->state & ANIM_STATE_WAITING,
1823         anim->state & ANIM_STATE_RUNNING,
1824         anim->num_parts);
1825 #endif
1826
1827   switch (action)
1828   {
1829     case ANIM_START:
1830       anim->state = anim->last_state = ANIM_STATE_RESTART;
1831       anim->active_part_nr = anim->last_active_part_nr = 0;
1832       anim->part_counter = 0;
1833
1834       break;
1835
1836     case ANIM_CONTINUE:
1837       if (anim->state == ANIM_STATE_INACTIVE)
1838         return;
1839
1840       anim->state = anim->last_state;
1841       anim->active_part_nr = anim->last_active_part_nr;
1842
1843       break;
1844
1845     case ANIM_STOP:
1846       anim->state = ANIM_STATE_INACTIVE;
1847
1848       for (i = 0; i < num_parts; i++)
1849         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1850
1851       return;
1852
1853     default:
1854       break;
1855   }
1856
1857   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1858   {
1859 #if 0
1860     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1861           anim->mode_nr, anim->nr, num_parts);
1862 #endif
1863
1864     for (i = 0; i < num_parts; i++)
1865     {
1866       part = &anim->part[i];
1867
1868       switch (action)
1869       {
1870         case ANIM_START:
1871           anim->state = ANIM_STATE_RUNNING;
1872           part->state = ANIM_STATE_RESTART;
1873
1874           break;
1875
1876         case ANIM_CONTINUE:
1877           if (part->state == ANIM_STATE_INACTIVE)
1878             continue;
1879
1880           break;
1881
1882         case ANIM_STOP:
1883           part->state = ANIM_STATE_INACTIVE;
1884
1885           continue;
1886
1887         default:
1888           break;
1889       }
1890
1891       part->state = HandleGlobalAnim_Part(part, part->state);
1892
1893       // when animation mode is "once", stop after animation was played once
1894       if (c->anim_mode & ANIM_ONCE &&
1895           part->state & ANIM_STATE_RESTART)
1896         part->state = ANIM_STATE_INACTIVE;
1897     }
1898
1899     anim->last_state = anim->state;
1900     anim->last_active_part_nr = anim->active_part_nr;
1901
1902     return;
1903   }
1904
1905   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1906     anim->active_part_nr = getGlobalAnimationPart(anim);
1907
1908   part = &anim->part[anim->active_part_nr];
1909
1910   // first set all animation parts to "inactive", ...
1911   for (i = 0; i < num_parts; i++)
1912     anim->part[i].state = ANIM_STATE_INACTIVE;
1913
1914   // ... then set current animation part to "running" ...
1915   part->state = ANIM_STATE_RUNNING;
1916
1917   // ... unless it is waiting for an initial event
1918   if (part->init_event_state)
1919     part->state = ANIM_STATE_WAITING;
1920
1921   anim->state = HandleGlobalAnim_Part(part, anim->state);
1922
1923   if (anim->state & ANIM_STATE_RESTART)
1924     anim->part_counter++;
1925
1926   // when animation mode is "once", stop after all animations were played once
1927   if (c->anim_mode & ANIM_ONCE &&
1928       anim->part_counter == anim->num_parts)
1929     anim->state = ANIM_STATE_INACTIVE;
1930
1931   state = anim->state;
1932   active_part_nr = anim->active_part_nr;
1933
1934   // while the animation parts are pausing (waiting or inactive), play the base
1935   // (main) animation; this corresponds to the "boring player animation" logic
1936   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1937   if (anim->has_base)
1938   {
1939     if (anim->state == ANIM_STATE_WAITING ||
1940         anim->state == ANIM_STATE_INACTIVE)
1941     {
1942       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1943       part = &anim->part[anim->active_part_nr];
1944
1945       if (anim->state != anim->last_state)
1946         part->state = ANIM_STATE_RESTART;
1947
1948       anim->state = ANIM_STATE_RUNNING;
1949       part->state = HandleGlobalAnim_Part(part, part->state);
1950     }
1951   }
1952
1953   anim->last_state = state;
1954   anim->last_active_part_nr = active_part_nr;
1955 }
1956
1957 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1958 {
1959   int i;
1960
1961 #if 0
1962   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1963 #endif
1964
1965   for (i = 0; i < ctrl->num_anims; i++)
1966     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1967 }
1968
1969 static void HandleGlobalAnim(int action, int game_mode)
1970 {
1971 #if 0
1972   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1973 #endif
1974
1975   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1976 }
1977
1978 static void DoAnimationExt(void)
1979 {
1980   int i;
1981
1982 #if 0
1983   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1984 #endif
1985
1986   // global animations now synchronized with frame delay of screen update
1987   anim_sync_frame++;
1988
1989   for (i = 0; i < NUM_GAME_MODES; i++)
1990     HandleGlobalAnim(ANIM_CONTINUE, i);
1991
1992 #if 0
1993   // force screen redraw in next frame to continue drawing global animations
1994   redraw_mask = REDRAW_ALL;
1995 #endif
1996 }
1997
1998 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1999                                      int delay_type)
2000 {
2001   int delay_action =
2002     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
2003      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
2004      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
2005      ANIM_DELAY_ACTION_NONE);
2006
2007   if (delay_action == ANIM_DELAY_ACTION_NONE)
2008     return;
2009
2010   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
2011 }
2012
2013 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
2014 {
2015   int event_action = (part->init_event_state ?
2016                       part->control_info.init_event_action :
2017                       part->control_info.anim_event_action);
2018
2019   if (event_action == ANIM_EVENT_ACTION_NONE)
2020     return FALSE;
2021
2022   if (event_action < MAX_IMAGE_FILES)
2023     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
2024   else
2025     OpenURLFromHash(anim_url_hash, event_action);
2026
2027   // check if further actions are allowed to be executed
2028   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
2029     return FALSE;
2030
2031   return TRUE;
2032 }
2033
2034 static void InitGlobalAnim_Clickable(void)
2035 {
2036   int i;
2037
2038   for (i = 0; i < num_global_anim_list; i++)
2039   {
2040     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2041
2042     if (part->triggered)
2043       part->clicked = TRUE;
2044
2045     part->triggered = FALSE;
2046     part->clickable = FALSE;
2047   }
2048 }
2049
2050 #define ANIM_CLICKED_RESET      0
2051 #define ANIM_CLICKED_PRESSED    1
2052 #define ANIM_CLICKED_RELEASED   2
2053
2054 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2055 {
2056   boolean click_consumed = FALSE;
2057   boolean anything_clicked = FALSE;
2058   boolean any_part_clicked = FALSE;
2059   boolean any_event_action = FALSE;
2060   int i;
2061
2062   // check animation parts in reverse draw order (to stop when clicked)
2063   for (i = num_global_anim_list - 1; i >= 0; i--)
2064   {
2065     struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2066
2067     // if request dialog is active, only handle pointer-style animations
2068     if (game.request_active &&
2069         part->control_info.class != get_hash_from_key("pointer"))
2070       continue;
2071
2072     if (clicked_event == ANIM_CLICKED_RESET)
2073     {
2074       part->clicked = FALSE;
2075
2076       continue;
2077     }
2078
2079     if (!part->clickable)
2080       continue;
2081
2082     if (!(part->state & ANIM_STATE_RUNNING))
2083       continue;
2084
2085     // always handle "any" click events (clicking anywhere on screen) ...
2086     if (clicked_event == ANIM_CLICKED_PRESSED &&
2087         isClickablePart(part, ANIM_EVENT_ANY))
2088     {
2089 #if DEBUG_ANIM_EVENTS
2090       Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2091             part->old_anim_nr + 1, part->old_nr + 1);
2092 #endif
2093
2094       anything_clicked = part->clicked = TRUE;
2095       click_consumed |= clickConsumed(part);
2096     }
2097
2098     // always handle "unclick:any" events (releasing anywhere on screen) ...
2099     if (clicked_event == ANIM_CLICKED_RELEASED &&
2100         isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2101     {
2102 #if DEBUG_ANIM_EVENTS
2103       Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2104             part->old_anim_nr + 1, part->old_nr + 1);
2105 #endif
2106
2107       anything_clicked = part->clicked = TRUE;
2108       click_consumed |= clickConsumed(part);
2109     }
2110
2111     // ... but only handle the first (topmost) clickable animation
2112     if (any_part_clicked)
2113       continue;
2114
2115     if (clicked_event == ANIM_CLICKED_PRESSED &&
2116         isClickedPart(part, mx, my, TRUE))
2117     {
2118 #if 0
2119       Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2120             anim_nr, part_nr, part->control_info.anim_event_action);
2121 #endif
2122
2123       // after executing event action, ignore any further actions
2124       if (!any_event_action && DoGlobalAnim_EventAction(part))
2125         any_event_action = TRUE;
2126
2127       // determine if mouse clicks should be blocked from other animations
2128       any_part_clicked |= clickConsumed(part);
2129
2130       if (isClickablePart(part, ANIM_EVENT_SELF))
2131       {
2132 #if DEBUG_ANIM_EVENTS
2133         Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2134               part->old_anim_nr + 1, part->old_nr + 1);
2135 #endif
2136
2137         anything_clicked = part->clicked = TRUE;
2138         click_consumed |= clickConsumed(part);
2139       }
2140
2141       // determine if mouse clicks should be blocked by this animation
2142       click_consumed |= clickBlocked(part);
2143
2144       // check if this click is defined to trigger other animations
2145       InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2146                                ANIM_EVENT_CLICK, "CLICK");
2147     }
2148   }
2149
2150   if (anything_clicked)
2151   {
2152     handle_click = TRUE;
2153
2154     for (i = 0; i < NUM_GAME_MODES; i++)
2155       HandleGlobalAnim(ANIM_CONTINUE, i);
2156
2157     handle_click = FALSE;
2158
2159     // prevent ignoring release event if processed within same game frame
2160     StopProcessingEvents();
2161   }
2162
2163   return (click_consumed || any_event_action);
2164 }
2165
2166 static void ResetGlobalAnim_Clickable(void)
2167 {
2168   InitGlobalAnim_Clickable();
2169 }
2170
2171 static void ResetGlobalAnim_Clicked(void)
2172 {
2173   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2174 }
2175
2176 void RestartGlobalAnimsByStatus(int status)
2177 {
2178   int anim_status_last = global.anim_status;
2179
2180   global.anim_status = status;
2181
2182   // force restarting global animations by changed global animation status
2183   DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2184
2185   global.anim_status = anim_status_last;
2186 }
2187
2188 void SetAnimStatusBeforeFading(int status)
2189 {
2190   anim_status_last_before_fading = status;
2191 }
2192
2193 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2194 {
2195   static boolean click_consumed = FALSE;
2196   static int last_button = 0;
2197   boolean press_event;
2198   boolean release_event;
2199   boolean click_consumed_current = click_consumed;
2200
2201   if (button != 0 && force_click)
2202     last_button = 0;
2203
2204   // check if button state has changed since last invocation
2205   press_event   = (button != 0 && last_button == 0);
2206   release_event = (button == 0 && last_button != 0);
2207   last_button = button;
2208
2209   if (press_event)
2210   {
2211     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2212     click_consumed_current = click_consumed;
2213   }
2214
2215   if (release_event)
2216   {
2217     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2218     click_consumed = FALSE;
2219   }
2220
2221   return click_consumed_current;
2222 }
2223
2224 int getGlobalAnimSyncFrame(void)
2225 {
2226   return anim_sync_frame;
2227 }
2228
2229 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y,
2230                                           int trigger_x, int trigger_y)
2231 {
2232   if (!IS_CUSTOM_ELEMENT(element))
2233     return;
2234
2235   // custom element stored as 0 to 255, change page stored as 1 to 32
2236   InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2237                                            x, y, trigger_x, trigger_y);
2238 }