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