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