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