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