fixed nasty x/y typo bug
[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 //                  http://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 "events.h"
18 #include "screens.h"
19
20
21 // values for global toon animation definition
22 #define NUM_GLOBAL_TOON_ANIMS           1
23 #define NUM_GLOBAL_TOON_PARTS           MAX_NUM_TOONS
24
25 // values for global animation definition (including toons)
26 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS +             \
27                                          NUM_GLOBAL_TOON_ANIMS)
28 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
29                                             NUM_GLOBAL_TOON_PARTS)
30
31 #define ANIM_CLASS_BIT_TITLE_INITIAL    0
32 #define ANIM_CLASS_BIT_TITLE            1
33 #define ANIM_CLASS_BIT_MAIN             2
34 #define ANIM_CLASS_BIT_SCORES           3
35 #define ANIM_CLASS_BIT_SUBMENU          4
36 #define ANIM_CLASS_BIT_MENU             5
37 #define ANIM_CLASS_BIT_TOONS            6
38 #define ANIM_CLASS_BIT_NO_TITLE         7
39
40 #define NUM_ANIM_CLASSES                8
41
42 #define ANIM_CLASS_NONE                 0
43 #define ANIM_CLASS_TITLE_INITIAL        (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
44 #define ANIM_CLASS_TITLE                (1 << ANIM_CLASS_BIT_TITLE)
45 #define ANIM_CLASS_MAIN                 (1 << ANIM_CLASS_BIT_MAIN)
46 #define ANIM_CLASS_SCORES               (1 << ANIM_CLASS_BIT_SCORES)
47 #define ANIM_CLASS_SUBMENU              (1 << ANIM_CLASS_BIT_SUBMENU)
48 #define ANIM_CLASS_MENU                 (1 << ANIM_CLASS_BIT_MENU)
49 #define ANIM_CLASS_TOONS                (1 << ANIM_CLASS_BIT_TOONS)
50 #define ANIM_CLASS_NO_TITLE             (1 << ANIM_CLASS_BIT_NO_TITLE)
51
52 #define ANIM_CLASS_TOONS_SCORES         (ANIM_CLASS_TOONS       |       \
53                                          ANIM_CLASS_SCORES      |       \
54                                          ANIM_CLASS_NO_TITLE)
55
56 #define ANIM_CLASS_TOONS_MENU_MAIN      (ANIM_CLASS_TOONS       |       \
57                                          ANIM_CLASS_MENU        |       \
58                                          ANIM_CLASS_MAIN        |       \
59                                          ANIM_CLASS_NO_TITLE)
60
61 #define ANIM_CLASS_TOONS_MENU_SUBMENU   (ANIM_CLASS_TOONS       |       \
62                                          ANIM_CLASS_MENU        |       \
63                                          ANIM_CLASS_SUBMENU     |       \
64                                          ANIM_CLASS_NO_TITLE)
65
66 // values for global animation states
67 #define ANIM_STATE_INACTIVE             0
68 #define ANIM_STATE_RESTART              (1 << 0)
69 #define ANIM_STATE_WAITING              (1 << 1)
70 #define ANIM_STATE_RUNNING              (1 << 2)
71
72 // values for global animation control
73 #define ANIM_NO_ACTION                  0
74 #define ANIM_START                      1
75 #define ANIM_CONTINUE                   2
76 #define ANIM_STOP                       3
77
78
79 struct GlobalAnimPartControlInfo
80 {
81   int old_nr;           // position before mapping animation parts linearly
82   int old_anim_nr;      // position before mapping animations linearly
83
84   int nr;
85   int anim_nr;
86   int mode_nr;
87
88   boolean is_base;      // animation part is base/main/default animation part
89
90   int sound;
91   int music;
92   int graphic;
93
94   struct GraphicInfo graphic_info;
95   struct GraphicInfo control_info;
96
97   int viewport_x;
98   int viewport_y;
99   int viewport_width;
100   int viewport_height;
101
102   int x, y;
103   int step_xoffset, step_yoffset;
104
105   unsigned int initial_anim_sync_frame;
106   unsigned int step_delay, step_delay_value;
107
108   int init_delay_counter;
109   int anim_delay_counter;
110   int post_delay_counter;
111
112   boolean init_event_state;
113   boolean anim_event_state;
114
115   boolean clickable;
116   boolean clicked;
117
118   int drawing_stage;
119
120   int state;
121   int last_anim_status;
122 };
123
124 struct GlobalAnimMainControlInfo
125 {
126   struct GlobalAnimPartControlInfo base;
127   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
128
129   int nr;
130   int mode_nr;
131
132   struct GraphicInfo control_info;
133
134   int num_parts;        // number of animation parts, but without base part
135   int num_parts_all;    // number of animation parts, including base part
136   int part_counter;
137   int active_part_nr;
138
139   boolean has_base;     // animation has base/main/default animation part
140
141   int last_x, last_y;
142
143   int init_delay_counter;
144
145   int state;
146
147   int last_state, last_active_part_nr;
148 };
149
150 struct GlobalAnimControlInfo
151 {
152   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
153
154   int nr;
155   int num_anims;
156 };
157
158 struct GameModeAnimClass
159 {
160   int game_mode;
161   int class;
162 } game_mode_anim_classes_list[] =
163 {
164   { GAME_MODE_TITLE_INITIAL_1,          ANIM_CLASS_TITLE_INITIAL        },
165   { GAME_MODE_TITLE_INITIAL_2,          ANIM_CLASS_TITLE_INITIAL        },
166   { GAME_MODE_TITLE_INITIAL_3,          ANIM_CLASS_TITLE_INITIAL        },
167   { GAME_MODE_TITLE_INITIAL_4,          ANIM_CLASS_TITLE_INITIAL        },
168   { GAME_MODE_TITLE_INITIAL_5,          ANIM_CLASS_TITLE_INITIAL        },
169   { GAME_MODE_TITLE_1,                  ANIM_CLASS_TITLE                },
170   { GAME_MODE_TITLE_2,                  ANIM_CLASS_TITLE                },
171   { GAME_MODE_TITLE_3,                  ANIM_CLASS_TITLE                },
172   { GAME_MODE_TITLE_4,                  ANIM_CLASS_TITLE                },
173   { GAME_MODE_TITLE_5,                  ANIM_CLASS_TITLE                },
174   { GAME_MODE_LEVELS,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
175   { GAME_MODE_LEVELNR,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
176   { GAME_MODE_INFO,                     ANIM_CLASS_TOONS_MENU_SUBMENU   },
177   { GAME_MODE_SETUP,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
178   { GAME_MODE_PSEUDO_MAINONLY,          ANIM_CLASS_TOONS_MENU_MAIN      },
179   { GAME_MODE_PSEUDO_TYPENAME,          ANIM_CLASS_TOONS_MENU_MAIN      },
180   { GAME_MODE_PSEUDO_SCORESOLD,         ANIM_CLASS_TOONS_SCORES         },
181   { GAME_MODE_PSEUDO_SCORESNEW,         ANIM_CLASS_TOONS_SCORES         },
182   { GAME_MODE_EDITOR,                   ANIM_CLASS_NO_TITLE             },
183   { GAME_MODE_PLAYING,                  ANIM_CLASS_NO_TITLE             },
184
185   { -1,                                 -1                              }
186 };
187
188 struct AnimClassGameMode
189 {
190   int class_bit;
191   int game_mode;
192 } anim_class_game_modes_list[] =
193 {
194   { ANIM_CLASS_BIT_TITLE_INITIAL,       GAME_MODE_TITLE_INITIAL         },
195   { ANIM_CLASS_BIT_TITLE,               GAME_MODE_TITLE                 },
196   { ANIM_CLASS_BIT_MAIN,                GAME_MODE_MAIN                  },
197   { ANIM_CLASS_BIT_SCORES,              GAME_MODE_SCORES                },
198   { ANIM_CLASS_BIT_SUBMENU,             GAME_MODE_PSEUDO_SUBMENU        },
199   { ANIM_CLASS_BIT_MENU,                GAME_MODE_PSEUDO_MENU           },
200   { ANIM_CLASS_BIT_TOONS,               GAME_MODE_PSEUDO_TOONS          },
201   { ANIM_CLASS_BIT_NO_TITLE,            GAME_MODE_PSEUDO_NO_TITLE       },
202
203   { -1,                                 -1                              }
204 };
205
206 // forward declaration for internal use
207 static void HandleGlobalAnim(int, int);
208 static void DoAnimationExt(void);
209 static void ResetGlobalAnim_Clickable(void);
210 static void ResetGlobalAnim_Clicked(void);
211
212 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
213
214 static unsigned int anim_sync_frame = 0;
215
216 static int game_mode_anim_classes[NUM_GAME_MODES];
217 static int anim_class_game_modes[NUM_ANIM_CLASSES];
218
219 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
220 static int anim_status_last = GAME_MODE_DEFAULT;
221 static int anim_classes_last = ANIM_CLASS_NONE;
222
223 static boolean drawing_to_fading_buffer = FALSE;
224
225
226 // ============================================================================
227 // generic animation frame calculation
228 // ============================================================================
229
230 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
231                       int sync_frame)
232 {
233   int frame = 0;
234
235   sync_frame += start_frame * delay;
236
237   if (mode & ANIM_LOOP)                 // looping animation
238   {
239     frame = (sync_frame % (delay * num_frames)) / delay;
240   }
241   else if (mode & ANIM_LINEAR)          // linear (non-looping) animation
242   {
243     frame = sync_frame / delay;
244
245     if (frame > num_frames - 1)
246       frame = num_frames - 1;
247   }
248   else if (mode & ANIM_PINGPONG)        // oscillate (border frames once)
249   {
250     int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
251
252     frame = (sync_frame % (delay * max_anim_frames)) / delay;
253     frame = (frame < num_frames ? frame : max_anim_frames - frame);
254   }
255   else if (mode & ANIM_PINGPONG2)       // oscillate (border frames twice)
256   {
257     int max_anim_frames = 2 * num_frames;
258
259     frame = (sync_frame % (delay * max_anim_frames)) / delay;
260     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
261   }
262   else if (mode & ANIM_RANDOM)          // play frames in random order
263   {
264     // note: expect different frames for the same delay cycle!
265
266     if (gfx.anim_random_frame < 0)
267       frame = GetSimpleRandom(num_frames);
268     else
269       frame = gfx.anim_random_frame % num_frames;
270   }
271   else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
272   {
273     frame = sync_frame % num_frames;
274   }
275
276   if (mode & ANIM_REVERSE)              // use reverse animation direction
277     frame = num_frames - frame - 1;
278
279   return frame;
280 }
281
282
283 // ============================================================================
284 // global animation functions
285 // ============================================================================
286
287 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
288 {
289   struct GraphicInfo *c = &anim->control_info;
290   int last_anim_random_frame = gfx.anim_random_frame;
291   int part_nr;
292
293   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
294
295   part_nr = getAnimationFrame(anim->num_parts, 1,
296                               c->anim_mode, c->anim_start_frame,
297                               anim->part_counter);
298
299   gfx.anim_random_frame = last_anim_random_frame;
300
301   return part_nr;
302 }
303
304 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
305 {
306   const struct GlobalAnimPartControlInfo *o1 =
307     (struct GlobalAnimPartControlInfo *)obj1;
308   const struct GlobalAnimPartControlInfo *o2 =
309     (struct GlobalAnimPartControlInfo *)obj2;
310   int compare_result;
311
312   if (o1->control_info.draw_order != o2->control_info.draw_order)
313     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
314   else
315     compare_result = o1->nr - o2->nr;
316
317   return compare_result;
318 }
319
320 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
321 {
322   const struct GlobalAnimMainControlInfo *o1 =
323     (struct GlobalAnimMainControlInfo *)obj1;
324   const struct GlobalAnimMainControlInfo *o2 =
325     (struct GlobalAnimMainControlInfo *)obj2;
326   int compare_result;
327
328   if (o1->control_info.draw_order != o2->control_info.draw_order)
329     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
330   else
331     compare_result = o1->nr - o2->nr;
332
333   return compare_result;
334 }
335
336 static void InitToonControls(void)
337 {
338   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
339   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
340   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
341   int mode_nr, anim_nr, part_nr;
342   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
343   int num_toons = MAX_NUM_TOONS;
344   int i;
345
346   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
347     num_toons = global.num_toons;
348
349   mode_nr = mode_nr_toons;
350   anim_nr = ctrl->num_anims;
351
352   anim->nr = anim_nr;
353   anim->mode_nr = mode_nr;
354   anim->control_info = graphic_info[control];
355
356   anim->num_parts = 0;
357   anim->num_parts_all = 0;
358   anim->part_counter = 0;
359   anim->active_part_nr = 0;
360
361   anim->has_base = FALSE;
362
363   anim->last_x = POS_OFFSCREEN;
364   anim->last_y = POS_OFFSCREEN;
365
366   anim->init_delay_counter = 0;
367
368   anim->state = ANIM_STATE_INACTIVE;
369
370   part_nr = 0;
371
372   for (i = 0; i < num_toons; i++)
373   {
374     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
375     int sound = SND_UNDEFINED;
376     int music = MUS_UNDEFINED;
377     int graphic = IMG_TOON_1 + i;
378     int control = graphic;
379
380     part->nr = part_nr;
381     part->anim_nr = anim_nr;
382     part->mode_nr = mode_nr;
383
384     part->is_base = FALSE;
385
386     part->sound = sound;
387     part->music = music;
388     part->graphic = graphic;
389
390     part->graphic_info = graphic_info[graphic];
391     part->control_info = graphic_info[control];
392
393     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
394
395     part->control_info.init_delay_fixed = 0;
396     part->control_info.init_delay_random = 150;
397
398     part->control_info.x = ARG_UNDEFINED_VALUE;
399     part->control_info.y = ARG_UNDEFINED_VALUE;
400
401     part->initial_anim_sync_frame = 0;
402
403     part->step_delay = 0;
404     part->step_delay_value = graphic_info[control].step_delay;
405
406     part->state = ANIM_STATE_INACTIVE;
407     part->last_anim_status = -1;
408
409     anim->num_parts++;
410     anim->num_parts_all++;
411
412     part_nr++;
413   }
414
415   ctrl->num_anims++;
416 }
417
418 static void InitGlobalAnimControls(void)
419 {
420   int i, m, a, p;
421   int mode_nr, anim_nr, part_nr;
422   int sound, music, graphic, control;
423
424   anim_sync_frame = 0;
425
426   for (m = 0; m < NUM_GAME_MODES; m++)
427   {
428     mode_nr = m;
429
430     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
431
432     ctrl->nr = mode_nr;
433     ctrl->num_anims = 0;
434
435     anim_nr = 0;
436
437     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
438     {
439       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
440       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
441
442       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
443
444       // if no base animation parameters defined, use default values
445       if (control == IMG_UNDEFINED)
446         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
447
448       anim->nr = anim_nr;
449       anim->mode_nr = mode_nr;
450       anim->control_info = graphic_info[control];
451
452       anim->num_parts = 0;
453       anim->num_parts_all = 0;
454       anim->part_counter = 0;
455       anim->active_part_nr = 0;
456
457       anim->has_base = FALSE;
458
459       anim->last_x = POS_OFFSCREEN;
460       anim->last_y = POS_OFFSCREEN;
461
462       anim->init_delay_counter = 0;
463
464       anim->state = ANIM_STATE_INACTIVE;
465
466       part_nr = 0;
467
468       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
469       {
470         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
471
472         sound   = global_anim_info[a].sound[p][m];
473         music   = global_anim_info[a].music[p][m];
474         graphic = global_anim_info[a].graphic[p][m];
475         control = global_anim_info[ctrl_id].graphic[p][m];
476
477         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
478             control == IMG_UNDEFINED)
479           continue;
480
481 #if 0
482         printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
483                m, a, p, mode_nr, anim_nr, part_nr, control);
484 #endif
485
486 #if 0
487         printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
488                m, a, p, mode_nr, anim_nr, part_nr, sound);
489 #endif
490
491         part->old_nr = p;
492         part->old_anim_nr = a;
493
494         part->nr = part_nr;
495         part->anim_nr = anim_nr;
496         part->mode_nr = mode_nr;
497
498         part->sound = sound;
499         part->music = music;
500         part->graphic = graphic;
501
502         part->graphic_info = graphic_info[graphic];
503         part->control_info = graphic_info[control];
504
505         part->initial_anim_sync_frame = 0;
506
507         part->step_delay = 0;
508         part->step_delay_value = graphic_info[control].step_delay;
509
510         part->state = ANIM_STATE_INACTIVE;
511         part->last_anim_status = -1;
512
513         anim->num_parts_all++;
514
515         if (p < GLOBAL_ANIM_ID_PART_BASE)
516         {
517           part->is_base = FALSE;
518
519           anim->num_parts++;
520           part_nr++;
521         }
522         else
523         {
524           part->is_base = TRUE;
525
526           anim->base = *part;
527           anim->has_base = TRUE;
528         }
529       }
530
531       if (anim->num_parts > 0 || anim->has_base)
532       {
533         ctrl->num_anims++;
534         anim_nr++;
535       }
536     }
537   }
538
539   InitToonControls();
540
541   // sort all animations according to draw_order and animation number
542   for (m = 0; m < NUM_GAME_MODES; m++)
543   {
544     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
545
546     // sort all main animations for this game mode
547     qsort(ctrl->anim, ctrl->num_anims,
548           sizeof(struct GlobalAnimMainControlInfo),
549           compareGlobalAnimMainControlInfo);
550
551     for (a = 0; a < ctrl->num_anims; a++)
552     {
553       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
554
555       // sort all animation parts for this main animation
556       qsort(anim->part, anim->num_parts,
557             sizeof(struct GlobalAnimPartControlInfo),
558             compareGlobalAnimPartControlInfo);
559     }
560   }
561
562   for (i = 0; i < NUM_GAME_MODES; i++)
563     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
564   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
565     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
566       game_mode_anim_classes_list[i].class;
567
568   for (i = 0; i < NUM_ANIM_CLASSES; i++)
569     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
570   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
571     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
572       anim_class_game_modes_list[i].game_mode;
573
574   anim_status_last_before_fading = GAME_MODE_LOADING;
575   anim_status_last = GAME_MODE_LOADING;
576   anim_classes_last = ANIM_CLASS_NONE;
577 }
578
579 void InitGlobalAnimations(void)
580 {
581   InitGlobalAnimControls();
582 }
583
584 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
585 {
586   Bitmap *fade_bitmap =
587     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
588      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
589   int game_mode_anim_action[NUM_GAME_MODES];
590   int mode_nr;
591
592   if (!setup.toons)
593     return;
594
595   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
596       drawing_target == DRAW_TO_SCREEN)
597     DoAnimationExt();
598
599   // always start with reliable default values (no animation actions)
600   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
601     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
602
603   if (global.anim_status != anim_status_last)
604   {
605     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
606     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
607     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
608     int i;
609
610     if (drawing_target == DRAW_TO_FADE_TARGET)
611       after_fading = TRUE;
612
613     // special case: changing from/to this screen is done without fading
614     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
615         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME)
616       after_fading = TRUE;
617
618     // ---------- part 1 ------------------------------------------------------
619     // start or stop global animations by change of game mode
620     // (special handling of animations for "current screen" and "all screens")
621
622     if (global.anim_status_next != anim_status_last_before_fading)
623     {
624       // stop animations for last screen before fading to new screen
625       game_mode_anim_action[anim_status_last] = ANIM_STOP;
626
627       // start animations for current screen after fading to new screen
628       game_mode_anim_action[global.anim_status] = ANIM_START;
629     }
630
631     // start animations for all screens after loading new artwork set
632     if (anim_status_last == GAME_MODE_LOADING)
633       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
634
635     // ---------- part 2 ------------------------------------------------------
636     // start or stop global animations by change of animation class
637     // (generic handling of animations for "class of screens")
638
639     for (i = 0; i < NUM_ANIM_CLASSES; i++)
640     {
641       int anim_class_check = (1 << i);
642       int anim_class_game_mode = anim_class_game_modes[i];
643       int anim_class_last = anim_classes_last & anim_class_check;
644       int anim_class_next = anim_classes_next & anim_class_check;
645
646       // stop animations for changed screen class before fading to new screen
647       if (before_fading && anim_class_last && !anim_class_next)
648         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
649
650       // start animations for changed screen class after fading to new screen
651       if (after_fading && !anim_class_last && anim_class_next)
652         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
653     }
654
655     if (drawing_target == DRAW_TO_SCREEN)
656     {
657       if (after_fading)
658       {
659         anim_classes_last = anim_classes_next;
660         anim_status_last_before_fading = global.anim_status;
661       }
662
663       anim_status_last = global.anim_status;
664
665       // start or stop animations determined to be started or stopped above
666       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
667         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
668           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
669     }
670     else if (drawing_target == DRAW_TO_FADE_TARGET)
671     {
672       drawing_to_fading_buffer = TRUE;
673
674       // start animations determined to be (temporary) started above
675       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
676         if (game_mode_anim_action[mode_nr] == ANIM_START)
677           HandleGlobalAnim(ANIM_START, mode_nr);
678     }
679   }
680
681   if (global.anim_status == GAME_MODE_LOADING)
682     return;
683
684   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
685   {
686     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
687     int anim_nr;
688
689     // when preparing source fading buffer, only draw animations to be stopped
690     if (drawing_target == DRAW_TO_FADE_SOURCE &&
691         game_mode_anim_action[mode_nr] != ANIM_STOP)
692       continue;
693
694     // when preparing target fading buffer, only draw animations to be started
695     if (drawing_target == DRAW_TO_FADE_TARGET &&
696         game_mode_anim_action[mode_nr] != ANIM_START)
697       continue;
698
699 #if 0
700     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
701         mode_nr != game_status)
702       continue;
703 #endif
704
705     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
706     {
707       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
708       struct GraphicInfo *c = &anim->control_info;
709       int part_first, part_last;
710       int part_nr;
711
712       if (!(anim->state & ANIM_STATE_RUNNING))
713         continue;
714
715       part_first = part_last = anim->active_part_nr;
716
717       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
718       {
719         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
720
721         part_first = 0;
722         part_last = num_parts - 1;
723       }
724
725       for (part_nr = part_first; part_nr <= part_last; part_nr++)
726       {
727         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
728         struct GraphicInfo *g = &part->graphic_info;
729         Bitmap *src_bitmap;
730         int src_x, src_y;
731         int width  = g->width;
732         int height = g->height;
733         int dst_x = part->x;
734         int dst_y = part->y;
735         int cut_x = 0;
736         int cut_y = 0;
737         int sync_frame;
738         int frame;
739         void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
740           (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
741         void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
742           (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
743
744         if (!(part->state & ANIM_STATE_RUNNING))
745           continue;
746
747         if (part->drawing_stage != drawing_stage)
748           continue;
749
750         if (part->x < 0)
751         {
752           dst_x = 0;
753           width += part->x;
754           cut_x = -part->x;
755         }
756         else if (part->x > part->viewport_width - g->width)
757           width -= (part->x - (part->viewport_width - g->width));
758
759         if (part->y < 0)
760         {
761           dst_y = 0;
762           height += part->y;
763           cut_y = -part->y;
764         }
765         else if (part->y > part->viewport_height - g->height)
766           height -= (part->y - (part->viewport_height - g->height));
767
768         if (width <= 0 || height <= 0)
769           continue;
770
771         dst_x += part->viewport_x;
772         dst_y += part->viewport_y;
773
774         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
775         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
776                                   g->anim_mode, g->anim_start_frame,
777                                   sync_frame);
778
779         getFixedGraphicSource(part->graphic, frame, &src_bitmap,
780                               &src_x, &src_y);
781
782         src_x += cut_x;
783         src_y += cut_y;
784
785         if (drawing_target == DRAW_TO_SCREEN)
786           blit_screen(src_bitmap, src_x, src_y, width, height,
787                       dst_x, dst_y);
788         else
789           blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
790                       dst_x, dst_y);
791       }
792     }
793   }
794
795   if (drawing_target == DRAW_TO_FADE_TARGET)
796   {
797     // stop animations determined to be (temporary) started above
798     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
799       if (game_mode_anim_action[mode_nr] == ANIM_START)
800         HandleGlobalAnim(ANIM_STOP, mode_nr);
801
802     drawing_to_fading_buffer = FALSE;
803   }
804 }
805
806 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
807 {
808   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
809     ResetGlobalAnim_Clickable();
810
811   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
812
813   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
814     ResetGlobalAnim_Clicked();
815 }
816
817 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
818 {
819   int viewport_x;
820   int viewport_y;
821   int viewport_width;
822   int viewport_height;
823   boolean changed = FALSE;
824
825   if (part->last_anim_status == global.anim_status)
826     return FALSE;
827
828   part->last_anim_status = global.anim_status;
829
830   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
831
832   if (part->control_info.class == get_hash_from_key("window") ||
833       part->control_info.class == get_hash_from_key("border"))
834   {
835     viewport_x = 0;
836     viewport_y = 0;
837     viewport_width  = WIN_XSIZE;
838     viewport_height = WIN_YSIZE;
839
840     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
841   }
842   else if (part->control_info.class == get_hash_from_key("door_1"))
843   {
844     viewport_x = DX;
845     viewport_y = DY;
846     viewport_width  = DXSIZE;
847     viewport_height = DYSIZE;
848   }
849   else if (part->control_info.class == get_hash_from_key("door_2"))
850   {
851     viewport_x = VX;
852     viewport_y = VY;
853     viewport_width  = VXSIZE;
854     viewport_height = VYSIZE;
855   }
856   else          // default: "playfield"
857   {
858     viewport_x = REAL_SX;
859     viewport_y = REAL_SY;
860     viewport_width  = FULL_SXSIZE;
861     viewport_height = FULL_SYSIZE;
862   }
863
864   if (viewport_x != part->viewport_x ||
865       viewport_y != part->viewport_y ||
866       viewport_width  != part->viewport_width ||
867       viewport_height != part->viewport_height)
868   {
869     part->viewport_x = viewport_x;
870     part->viewport_y = viewport_y;
871     part->viewport_width  = viewport_width;
872     part->viewport_height = viewport_height;
873
874     changed = TRUE;
875   }
876
877   return changed;
878 }
879
880 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
881 {
882   int sound = part->sound;
883
884   if (sound == SND_UNDEFINED)
885     return;
886
887   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
888       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
889     return;
890
891   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
892   if (IS_LOOP_SOUND(sound))
893     PlaySoundLoop(sound);
894   else
895     PlaySound(sound);
896
897 #if 0
898   printf("::: PLAY SOUND %d.%d.%d: %d\n",
899          part->anim_nr, part->nr, part->mode_nr, sound);
900 #endif
901 }
902
903 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
904 {
905   int sound = part->sound;
906
907   if (sound == SND_UNDEFINED)
908     return;
909
910   StopSound(sound);
911
912 #if 0
913   printf("::: STOP SOUND %d.%d.%d: %d\n",
914          part->anim_nr, part->nr, part->mode_nr, sound);
915 #endif
916 }
917
918 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
919 {
920   int music = part->music;
921
922   if (music == MUS_UNDEFINED)
923     return;
924
925   if (!setup.sound_music)
926     return;
927
928   if (IS_LOOP_MUSIC(music))
929     PlayMusicLoop(music);
930   else
931     PlayMusic(music);
932
933 #if 0
934   printf("::: PLAY MUSIC %d.%d.%d: %d\n",
935          part->anim_nr, part->nr, part->mode_nr, music);
936 #endif
937 }
938
939 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
940 {
941   int music = part->music;
942
943   if (music == MUS_UNDEFINED)
944     return;
945
946   StopMusic();
947
948 #if 0
949   printf("::: STOP MUSIC %d.%d.%d: %d\n",
950          part->anim_nr, part->nr, part->mode_nr, music);
951 #endif
952 }
953
954 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
955 {
956   // when drawing animations to fading buffer, do not play sounds or music
957   if (drawing_to_fading_buffer)
958     return;
959
960   PlayGlobalAnimSound(part);
961   PlayGlobalAnimMusic(part);
962 }
963
964 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
965 {
966   StopGlobalAnimSound(part);
967   StopGlobalAnimMusic(part);
968 }
969
970 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
971 {
972   // when drawing animations to fading buffer, do not play sounds
973   if (drawing_to_fading_buffer)
974     return;
975
976   // loop sounds only expire when playing
977   if (game_status != GAME_MODE_PLAYING)
978     return;
979
980   // check if any sound is defined for this animation part
981   if (part->sound == SND_UNDEFINED)
982     return;
983
984   // normal (non-loop) sounds do not expire when playing
985   if (!IS_LOOP_SOUND(part->sound))
986     return;
987
988   // prevent expiring loop sounds when playing
989   PlayGlobalAnimSound(part);
990 }
991
992 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
993 {
994   struct GraphicInfo *c = &part->control_info;
995   int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
996   int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
997
998   if (mask & ANIM_EVENT_ANY)
999     return (c->init_event & ANIM_EVENT_ANY ||
1000             c->anim_event & ANIM_EVENT_ANY);
1001   else if (mask & ANIM_EVENT_SELF)
1002     return (c->init_event & ANIM_EVENT_SELF ||
1003             c->anim_event & ANIM_EVENT_SELF);
1004   else
1005     return ((c->init_event & trigger_mask) == mask ||
1006             (c->anim_event & trigger_mask) == mask ||
1007             (c->init_event & trigger_mask) == mask_anim_only ||
1008             (c->anim_event & trigger_mask) == mask_anim_only);
1009 }
1010
1011 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1012                              int mx, int my, boolean clicked)
1013 {
1014   struct GraphicInfo *g = &part->graphic_info;
1015   int part_x = part->viewport_x + part->x;
1016   int part_y = part->viewport_y + part->y;
1017   int part_width  = g->width;
1018   int part_height = g->height;
1019
1020   // check if mouse click was detected at all
1021   if (!clicked)
1022     return FALSE;
1023
1024   // check if mouse click is inside the animation part's viewport
1025   if (mx <  part->viewport_x ||
1026       mx >= part->viewport_x + part->viewport_width ||
1027       my <  part->viewport_y ||
1028       my >= part->viewport_y + part->viewport_height)
1029     return FALSE;
1030
1031   // check if mouse click is inside the animation part's graphic
1032   if (mx <  part_x ||
1033       mx >= part_x + part_width ||
1034       my <  part_y ||
1035       my >= part_y + part_height)
1036     return FALSE;
1037
1038   return TRUE;
1039 }
1040
1041 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1042 {
1043   return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1044 }
1045
1046 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1047                                  int state)
1048 {
1049   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1050   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1051   struct GraphicInfo *g = &part->graphic_info;
1052   struct GraphicInfo *c = &part->control_info;
1053   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1054
1055   if (viewport_changed)
1056     state |= ANIM_STATE_RESTART;
1057
1058   if (state & ANIM_STATE_RESTART)
1059   {
1060     // when drawing animations to fading buffer, only start fixed animations
1061     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1062                                      c->y == ARG_UNDEFINED_VALUE))
1063       return ANIM_STATE_INACTIVE;
1064
1065     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1066
1067     part->init_delay_counter =
1068       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1069
1070     part->anim_delay_counter =
1071       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1072
1073     part->init_event_state = c->init_event;
1074     part->anim_event_state = c->anim_event;
1075
1076     part->initial_anim_sync_frame =
1077       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1078
1079     if (c->direction & MV_HORIZONTAL)
1080     {
1081       int pos_bottom = part->viewport_height - g->height;
1082
1083       if (c->position == POS_TOP)
1084         part->y = 0;
1085       else if (c->position == POS_UPPER)
1086         part->y = GetSimpleRandom(pos_bottom / 2);
1087       else if (c->position == POS_MIDDLE)
1088         part->y = pos_bottom / 2;
1089       else if (c->position == POS_LOWER)
1090         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1091       else if (c->position == POS_BOTTOM)
1092         part->y = pos_bottom;
1093       else
1094         part->y = GetSimpleRandom(pos_bottom);
1095
1096       if (c->direction == MV_RIGHT)
1097       {
1098         part->step_xoffset = c->step_offset;
1099         part->x = -g->width + part->step_xoffset;
1100       }
1101       else
1102       {
1103         part->step_xoffset = -c->step_offset;
1104         part->x = part->viewport_width + part->step_xoffset;
1105       }
1106
1107       part->step_yoffset = 0;
1108     }
1109     else if (c->direction & MV_VERTICAL)
1110     {
1111       int pos_right = part->viewport_width - g->width;
1112
1113       if (c->position == POS_LEFT)
1114         part->x = 0;
1115       else if (c->position == POS_RIGHT)
1116         part->x = pos_right;
1117       else
1118         part->x = GetSimpleRandom(pos_right);
1119
1120       if (c->direction == MV_DOWN)
1121       {
1122         part->step_yoffset = c->step_offset;
1123         part->y = -g->height + part->step_yoffset;
1124       }
1125       else
1126       {
1127         part->step_yoffset = -c->step_offset;
1128         part->y = part->viewport_height + part->step_yoffset;
1129       }
1130
1131       part->step_xoffset = 0;
1132     }
1133     else
1134     {
1135       part->x = 0;
1136       part->y = 0;
1137
1138       part->step_xoffset = 0;
1139       part->step_yoffset = 0;
1140     }
1141
1142     if (c->x != ARG_UNDEFINED_VALUE)
1143       part->x = c->x;
1144     if (c->y != ARG_UNDEFINED_VALUE)
1145       part->y = c->y;
1146
1147     if (c->position == POS_LAST &&
1148         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1149         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1150     {
1151       part->x = anim->last_x;
1152       part->y = anim->last_y;
1153     }
1154
1155     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1156       part->step_xoffset = c->step_xoffset;
1157     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1158       part->step_yoffset = c->step_yoffset;
1159
1160     if (part->init_delay_counter == 0 &&
1161         part->init_event_state == ANIM_EVENT_NONE)
1162       PlayGlobalAnimSoundAndMusic(part);
1163   }
1164
1165   if (part->clicked &&
1166       part->init_event_state != ANIM_EVENT_NONE)
1167   {
1168     if (part->initial_anim_sync_frame > 0)
1169       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1170
1171     part->init_delay_counter = 1;
1172     part->init_event_state = ANIM_EVENT_NONE;
1173
1174     part->clicked = FALSE;
1175   }
1176
1177   if (part->clicked &&
1178       part->anim_event_state != ANIM_EVENT_NONE)
1179   {
1180     part->anim_delay_counter = 1;
1181     part->anim_event_state = ANIM_EVENT_NONE;
1182
1183     part->clicked = FALSE;
1184   }
1185
1186   if (part->init_delay_counter > 0)
1187   {
1188     part->init_delay_counter--;
1189
1190     if (part->init_delay_counter == 0)
1191     {
1192       part->init_event_state = ANIM_EVENT_NONE;
1193
1194       PlayGlobalAnimSoundAndMusic(part);
1195     }
1196
1197     return ANIM_STATE_WAITING;
1198   }
1199
1200   if (part->init_event_state != ANIM_EVENT_NONE)
1201     return ANIM_STATE_WAITING;
1202
1203   // animation part is now running/visible and therefore clickable
1204   part->clickable = TRUE;
1205
1206   // check if moving animation has left the visible screen area
1207   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1208       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1209       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1210       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1211   {
1212     // do not wait for "anim" events for off-screen animations
1213     part->anim_event_state = ANIM_EVENT_NONE;
1214
1215     // do not stop animation before "anim" or "post" counter are finished
1216     if (part->anim_delay_counter == 0 &&
1217         part->post_delay_counter == 0)
1218     {
1219       StopGlobalAnimSoundAndMusic(part);
1220
1221       part->post_delay_counter =
1222         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1223
1224       if (part->post_delay_counter > 0)
1225         return ANIM_STATE_RUNNING;
1226
1227       // drawing last frame not needed here -- animation not visible anymore
1228       return ANIM_STATE_RESTART;
1229     }
1230   }
1231
1232   if (part->anim_delay_counter > 0)
1233   {
1234     part->anim_delay_counter--;
1235
1236     if (part->anim_delay_counter == 0)
1237     {
1238       part->anim_event_state = ANIM_EVENT_NONE;
1239
1240       StopGlobalAnimSoundAndMusic(part);
1241
1242       part->post_delay_counter =
1243         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1244
1245       if (part->post_delay_counter > 0)
1246         return ANIM_STATE_RUNNING;
1247
1248       // additional state "RUNNING" required to not skip drawing last frame
1249       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1250     }
1251   }
1252
1253   if (part->post_delay_counter > 0)
1254   {
1255     part->post_delay_counter--;
1256
1257     if (part->post_delay_counter == 0)
1258       return ANIM_STATE_RESTART;
1259
1260     return ANIM_STATE_WAITING;
1261   }
1262
1263   // special case to prevent expiring loop sounds when playing
1264   PlayGlobalAnimSoundIfLoop(part);
1265
1266   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1267                        anim_sync_frame))
1268     return ANIM_STATE_RUNNING;
1269
1270 #if 0
1271   {
1272     static unsigned int last_counter = -1;
1273     unsigned int counter = Counter();
1274
1275     printf("::: NEXT ANIM PART [%d, %d]\n",
1276            anim_sync_frame, counter - last_counter);
1277
1278     last_counter = counter;
1279   }
1280 #endif
1281
1282   part->x += part->step_xoffset;
1283   part->y += part->step_yoffset;
1284
1285   anim->last_x = part->x;
1286   anim->last_y = part->y;
1287
1288   return ANIM_STATE_RUNNING;
1289 }
1290
1291 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1292                                   int action)
1293 {
1294   struct GlobalAnimPartControlInfo *part;
1295   struct GraphicInfo *c = &anim->control_info;
1296   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1297   int state, active_part_nr;
1298   int i;
1299
1300 #if 0
1301   printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1302          anim->mode_nr, anim->nr, anim->num_parts);
1303   printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1304 #endif
1305
1306 #if 0
1307   printf("::: %s(%d): %d, %d, %d [%d]\n",
1308          (action == ANIM_START ? "ANIM_START" :
1309           action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1310           action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1311          anim->nr,
1312          anim->state & ANIM_STATE_RESTART,
1313          anim->state & ANIM_STATE_WAITING,
1314          anim->state & ANIM_STATE_RUNNING,
1315          anim->num_parts);
1316 #endif
1317
1318   switch (action)
1319   {
1320     case ANIM_START:
1321       anim->state = anim->last_state = ANIM_STATE_RESTART;
1322       anim->active_part_nr = anim->last_active_part_nr = 0;
1323       anim->part_counter = 0;
1324
1325       break;
1326
1327     case ANIM_CONTINUE:
1328       if (anim->state == ANIM_STATE_INACTIVE)
1329         return;
1330
1331       anim->state = anim->last_state;
1332       anim->active_part_nr = anim->last_active_part_nr;
1333
1334       break;
1335
1336     case ANIM_STOP:
1337       anim->state = ANIM_STATE_INACTIVE;
1338
1339       for (i = 0; i < num_parts; i++)
1340         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1341
1342       return;
1343
1344     default:
1345       break;
1346   }
1347
1348   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1349   {
1350 #if 0
1351     printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1352            anim->mode_nr, anim->nr, num_parts);
1353 #endif
1354
1355     for (i = 0; i < num_parts; i++)
1356     {
1357       part = &anim->part[i];
1358
1359       switch (action)
1360       {
1361         case ANIM_START:
1362           anim->state = ANIM_STATE_RUNNING;
1363           part->state = ANIM_STATE_RESTART;
1364
1365           break;
1366
1367         case ANIM_CONTINUE:
1368           if (part->state == ANIM_STATE_INACTIVE)
1369             continue;
1370
1371           break;
1372
1373         case ANIM_STOP:
1374           part->state = ANIM_STATE_INACTIVE;
1375
1376           continue;
1377
1378         default:
1379           break;
1380       }
1381
1382       part->state = HandleGlobalAnim_Part(part, part->state);
1383
1384       // when animation mode is "once", stop after animation was played once
1385       if (c->anim_mode & ANIM_ONCE &&
1386           part->state & ANIM_STATE_RESTART)
1387         part->state = ANIM_STATE_INACTIVE;
1388     }
1389
1390     anim->last_state = anim->state;
1391     anim->last_active_part_nr = anim->active_part_nr;
1392
1393     return;
1394   }
1395
1396   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1397     anim->active_part_nr = getGlobalAnimationPart(anim);
1398
1399   part = &anim->part[anim->active_part_nr];
1400
1401   // first set all animation parts to "inactive", ...
1402   for (i = 0; i < num_parts; i++)
1403     anim->part[i].state = ANIM_STATE_INACTIVE;
1404
1405   // ... then set current animation parts to "running"
1406   part->state = ANIM_STATE_RUNNING;
1407
1408   anim->state = HandleGlobalAnim_Part(part, anim->state);
1409
1410   if (anim->state & ANIM_STATE_RESTART)
1411     anim->part_counter++;
1412
1413   // when animation mode is "once", stop after all animations were played once
1414   if (c->anim_mode & ANIM_ONCE &&
1415       anim->part_counter == anim->num_parts)
1416     anim->state = ANIM_STATE_INACTIVE;
1417
1418   state = anim->state;
1419   active_part_nr = anim->active_part_nr;
1420
1421   // while the animation parts are pausing (waiting or inactive), play the base
1422   // (main) animation; this corresponds to the "boring player animation" logic
1423   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1424   if (anim->has_base)
1425   {
1426     if (anim->state == ANIM_STATE_WAITING ||
1427         anim->state == ANIM_STATE_INACTIVE)
1428     {
1429       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1430       part = &anim->part[anim->active_part_nr];
1431
1432       if (anim->state != anim->last_state)
1433         part->state = ANIM_STATE_RESTART;
1434
1435       anim->state = ANIM_STATE_RUNNING;
1436       part->state = HandleGlobalAnim_Part(part, part->state);
1437     }
1438   }
1439
1440   anim->last_state = state;
1441   anim->last_active_part_nr = active_part_nr;
1442 }
1443
1444 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1445 {
1446   int i;
1447
1448 #if 0
1449   printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1450          ctrl->nr, ctrl->num_anims);
1451 #endif
1452
1453   for (i = 0; i < ctrl->num_anims; i++)
1454     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1455 }
1456
1457 static void HandleGlobalAnim(int action, int game_mode)
1458 {
1459 #if 0
1460   printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1461 #endif
1462
1463   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1464 }
1465
1466 static void DoAnimationExt(void)
1467 {
1468   int i;
1469
1470 #if 0
1471   printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1472 #endif
1473
1474   // global animations now synchronized with frame delay of screen update
1475   anim_sync_frame++;
1476
1477   for (i = 0; i < NUM_GAME_MODES; i++)
1478     HandleGlobalAnim(ANIM_CONTINUE, i);
1479
1480 #if 0
1481   // force screen redraw in next frame to continue drawing global animations
1482   redraw_mask = REDRAW_ALL;
1483 #endif
1484 }
1485
1486 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1487 {
1488   int anim_event_action = part->control_info.anim_event_action;
1489
1490   if (anim_event_action == -1)
1491     return FALSE;
1492
1493   boolean action_executed = (DoGadgetAction(anim_event_action) ||
1494                              DoScreenAction(anim_event_action) ||
1495                              DoKeysymAction(anim_event_action));
1496
1497   // check if further actions are allowed to be executed
1498   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1499     return FALSE;
1500
1501   return action_executed;
1502 }
1503
1504 static void InitGlobalAnim_Clickable(void)
1505 {
1506   int mode_nr;
1507
1508   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1509   {
1510     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1511     int anim_nr;
1512
1513     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1514     {
1515       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1516       int part_nr;
1517
1518       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1519       {
1520         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1521
1522         part->clickable = FALSE;
1523       }
1524     }
1525   }
1526 }
1527
1528 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1529 {
1530   boolean anything_clicked = FALSE;
1531   boolean any_part_clicked = FALSE;
1532   boolean any_event_action = FALSE;
1533   int mode_nr;
1534
1535   // check game modes in reverse draw order (to stop when clicked)
1536   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1537   {
1538     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1539     int anim_nr;
1540
1541     // check animations in reverse draw order (to stop when clicked)
1542     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1543     {
1544       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1545       int part_nr;
1546
1547       // check animation parts in reverse draw order (to stop when clicked)
1548       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1549       {
1550         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1551
1552         if (!clicked)
1553         {
1554           part->clicked = FALSE;
1555
1556           continue;
1557         }
1558
1559         if (!part->clickable)
1560           continue;
1561
1562         if (part->state != ANIM_STATE_RUNNING)
1563           continue;
1564
1565         // always handle "any" click events (clicking anywhere on screen) ...
1566         if (isClickablePart(part, ANIM_EVENT_ANY))
1567         {
1568           part->clicked = TRUE;
1569           anything_clicked = clickConsumed(part);
1570         }
1571
1572         // ... but only handle the first (topmost) clickable animation
1573         if (any_part_clicked)
1574           continue;
1575
1576         if (isClickedPart(part, mx, my, clicked))
1577         {
1578 #if 0
1579           printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1580                  part->control_info.anim_event_action);
1581 #endif
1582
1583           // after executing event action, ignore any further actions
1584           if (!any_event_action && DoGlobalAnim_EventAction(part))
1585             any_event_action = TRUE;
1586
1587           // determine if mouse clicks should be blocked from other animations
1588           any_part_clicked = clickConsumed(part);
1589
1590           if (isClickablePart(part, ANIM_EVENT_SELF))
1591           {
1592             part->clicked = TRUE;
1593             anything_clicked = clickConsumed(part);
1594           }
1595
1596           // check if this click is defined to trigger other animations
1597           int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1598           int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1599           int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1600
1601           if (!part->is_base)
1602             mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1603
1604           int anim2_nr;
1605
1606           for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1607           {
1608             struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1609             int part2_nr;
1610
1611             for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1612             {
1613               struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1614
1615               if (part2->state != ANIM_STATE_RUNNING)
1616                 continue;
1617
1618               if (isClickablePart(part2, mask))
1619               {
1620                 part2->clicked = TRUE;
1621                 anything_clicked = clickConsumed(part); // click was on "part"!
1622
1623 #if 0
1624                 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1625                        part2->control_info.anim_event_action);
1626 #endif
1627
1628                 // after executing event action, ignore any further actions
1629                 if (!any_event_action && DoGlobalAnim_EventAction(part2))
1630                   any_event_action = TRUE;
1631               }
1632
1633 #if 0
1634               struct GraphicInfo *c = &part2->control_info;
1635
1636               printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1637                      anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1638
1639               if (isClickablePart(part2, mask))
1640                 printf(" <--- TRIGGERED BY %d.%d",
1641                        anim_nr, part_nr);
1642
1643               printf("\n");
1644 #endif
1645             }
1646           }
1647         }
1648       }
1649     }
1650   }
1651
1652   return (anything_clicked || any_event_action);
1653 }
1654
1655 static void ResetGlobalAnim_Clickable(void)
1656 {
1657   InitGlobalAnim_Clickable();
1658 }
1659
1660 static void ResetGlobalAnim_Clicked(void)
1661 {
1662   InitGlobalAnim_Clicked(-1, -1, FALSE);
1663 }
1664
1665 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1666 {
1667   static boolean click_consumed = FALSE;
1668   static int last_button = 0;
1669   boolean press_event;
1670   boolean release_event;
1671   boolean click_consumed_current = click_consumed;
1672
1673   // check if button state has changed since last invocation
1674   press_event   = (button != 0 && last_button == 0);
1675   release_event = (button == 0 && last_button != 0);
1676   last_button = button;
1677
1678   if (press_event)
1679   {
1680     click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1681     click_consumed_current = click_consumed;
1682   }
1683
1684   if (release_event)
1685     click_consumed = FALSE;
1686
1687   return click_consumed_current;
1688 }