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