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