added ignoring gadgets if click already caused animation event action
[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     // special case: changing from/to this screen is done without fading
604     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
605         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME)
606       after_fading = TRUE;
607
608     // ---------- part 1 ------------------------------------------------------
609     // start or stop global animations by change of game mode
610     // (special handling of animations for "current screen" and "all screens")
611
612     if (global.anim_status_next != anim_status_last_before_fading)
613     {
614       // stop animations for last screen before fading to new screen
615       game_mode_anim_action[anim_status_last] = ANIM_STOP;
616
617       // start animations for current screen after fading to new screen
618       game_mode_anim_action[global.anim_status] = ANIM_START;
619     }
620
621     // start animations for all screens after loading new artwork set
622     if (anim_status_last == GAME_MODE_LOADING)
623       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
624
625     // ---------- part 2 ------------------------------------------------------
626     // start or stop global animations by change of animation class
627     // (generic handling of animations for "class of screens")
628
629     for (i = 0; i < NUM_ANIM_CLASSES; i++)
630     {
631       int anim_class_check = (1 << i);
632       int anim_class_game_mode = anim_class_game_modes[i];
633       int anim_class_last = anim_classes_last & anim_class_check;
634       int anim_class_next = anim_classes_next & anim_class_check;
635
636       // stop animations for changed screen class before fading to new screen
637       if (before_fading && anim_class_last && !anim_class_next)
638         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
639
640       // start animations for changed screen class after fading to new screen
641       if (after_fading && !anim_class_last && anim_class_next)
642         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
643     }
644
645     if (drawing_target == DRAW_TO_SCREEN)
646     {
647       if (after_fading)
648       {
649         anim_classes_last = anim_classes_next;
650         anim_status_last_before_fading = global.anim_status;
651       }
652
653       anim_status_last = global.anim_status;
654
655       // start or stop animations determined to be started or stopped above
656       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
657         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
658           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
659     }
660     else if (drawing_target == DRAW_TO_FADE_TARGET)
661     {
662       drawing_to_fading_buffer = TRUE;
663
664       // start animations determined to be (temporary) started above
665       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
666         if (game_mode_anim_action[mode_nr] == ANIM_START)
667           HandleGlobalAnim(ANIM_START, mode_nr);
668     }
669   }
670
671   if (global.anim_status == GAME_MODE_LOADING)
672     return;
673
674   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
675   {
676     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
677     int anim_nr;
678
679     // when preparing source fading buffer, only draw animations to be stopped
680     if (drawing_target == DRAW_TO_FADE_SOURCE &&
681         game_mode_anim_action[mode_nr] != ANIM_STOP)
682       continue;
683
684     // when preparing target fading buffer, only draw animations to be started
685     if (drawing_target == DRAW_TO_FADE_TARGET &&
686         game_mode_anim_action[mode_nr] != ANIM_START)
687       continue;
688
689 #if 0
690     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
691         mode_nr != game_status)
692       continue;
693 #endif
694
695     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
696     {
697       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
698       struct GraphicInfo *c = &anim->control_info;
699       int part_first, part_last;
700       int part_nr;
701
702       if (!(anim->state & ANIM_STATE_RUNNING))
703         continue;
704
705       part_first = part_last = anim->active_part_nr;
706
707       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
708       {
709         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
710
711         part_first = 0;
712         part_last = num_parts - 1;
713       }
714
715       for (part_nr = part_first; part_nr <= part_last; part_nr++)
716       {
717         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
718         struct GraphicInfo *g = &part->graphic_info;
719         Bitmap *src_bitmap;
720         int src_x, src_y;
721         int width  = g->width;
722         int height = g->height;
723         int dst_x = part->x;
724         int dst_y = part->y;
725         int cut_x = 0;
726         int cut_y = 0;
727         int sync_frame;
728         int frame;
729         void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
730           (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
731         void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
732           (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
733
734         if (!(part->state & ANIM_STATE_RUNNING))
735           continue;
736
737         if (part->drawing_stage != drawing_stage)
738           continue;
739
740         if (part->x < 0)
741         {
742           dst_x = 0;
743           width += part->x;
744           cut_x = -part->x;
745         }
746         else if (part->x > part->viewport_width - g->width)
747           width -= (part->x - (part->viewport_width - g->width));
748
749         if (part->y < 0)
750         {
751           dst_y = 0;
752           height += part->y;
753           cut_y = -part->y;
754         }
755         else if (part->y > part->viewport_height - g->height)
756           height -= (part->y - (part->viewport_height - g->height));
757
758         if (width <= 0 || height <= 0)
759           continue;
760
761         dst_x += part->viewport_x;
762         dst_y += part->viewport_y;
763
764         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
765         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
766                                   g->anim_mode, g->anim_start_frame,
767                                   sync_frame);
768
769         getFixedGraphicSource(part->graphic, frame, &src_bitmap,
770                               &src_x, &src_y);
771
772         src_x += cut_x;
773         src_y += cut_y;
774
775         if (drawing_target == DRAW_TO_SCREEN)
776           blit_screen(src_bitmap, src_x, src_y, width, height,
777                       dst_x, dst_y);
778         else
779           blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
780                       dst_x, dst_y);
781       }
782     }
783   }
784
785   if (drawing_target == DRAW_TO_FADE_TARGET)
786   {
787     // stop animations determined to be (temporary) started above
788     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
789       if (game_mode_anim_action[mode_nr] == ANIM_START)
790         HandleGlobalAnim(ANIM_STOP, mode_nr);
791
792     drawing_to_fading_buffer = FALSE;
793   }
794 }
795
796 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
797 {
798   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
799     ResetGlobalAnim_Clickable();
800
801   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
802
803   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
804     ResetGlobalAnim_Clicked();
805 }
806
807 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
808 {
809   int viewport_x;
810   int viewport_y;
811   int viewport_width;
812   int viewport_height;
813   boolean changed = FALSE;
814
815   if (part->last_anim_status == global.anim_status)
816     return FALSE;
817
818   part->last_anim_status = global.anim_status;
819
820   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
821
822   if (part->control_info.class == get_hash_from_key("window") ||
823       part->control_info.class == get_hash_from_key("border"))
824   {
825     viewport_x = 0;
826     viewport_y = 0;
827     viewport_width  = WIN_XSIZE;
828     viewport_height = WIN_YSIZE;
829
830     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
831   }
832   else if (part->control_info.class == get_hash_from_key("door_1"))
833   {
834     viewport_x = DX;
835     viewport_y = DY;
836     viewport_width  = DXSIZE;
837     viewport_height = DYSIZE;
838   }
839   else if (part->control_info.class == get_hash_from_key("door_2"))
840   {
841     viewport_x = VX;
842     viewport_y = VY;
843     viewport_width  = VXSIZE;
844     viewport_height = VYSIZE;
845   }
846   else          // default: "playfield"
847   {
848     viewport_x = REAL_SX;
849     viewport_y = REAL_SY;
850     viewport_width  = FULL_SXSIZE;
851     viewport_height = FULL_SYSIZE;
852   }
853
854   if (viewport_x != part->viewport_x ||
855       viewport_y != part->viewport_y ||
856       viewport_width  != part->viewport_width ||
857       viewport_height != part->viewport_height)
858   {
859     part->viewport_x = viewport_x;
860     part->viewport_y = viewport_y;
861     part->viewport_width  = viewport_width;
862     part->viewport_height = viewport_height;
863
864     changed = TRUE;
865   }
866
867   return changed;
868 }
869
870 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
871 {
872   int sound = part->sound;
873
874   if (sound == SND_UNDEFINED)
875     return;
876
877   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
878       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
879     return;
880
881   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
882   if (IS_LOOP_SOUND(sound))
883     PlaySoundLoop(sound);
884   else
885     PlaySound(sound);
886
887 #if 0
888   printf("::: PLAY SOUND %d.%d.%d: %d\n",
889          part->anim_nr, part->nr, part->mode_nr, sound);
890 #endif
891 }
892
893 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
894 {
895   int sound = part->sound;
896
897   if (sound == SND_UNDEFINED)
898     return;
899
900   StopSound(sound);
901
902 #if 0
903   printf("::: STOP SOUND %d.%d.%d: %d\n",
904          part->anim_nr, part->nr, part->mode_nr, sound);
905 #endif
906 }
907
908 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
909 {
910   int music = part->music;
911
912   if (music == MUS_UNDEFINED)
913     return;
914
915   if (!setup.sound_music)
916     return;
917
918   PlayMusic(music);
919
920 #if 0
921   printf("::: PLAY MUSIC %d.%d.%d: %d\n",
922          part->anim_nr, part->nr, part->mode_nr, music);
923 #endif
924 }
925
926 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
927 {
928   int music = part->music;
929
930   if (music == MUS_UNDEFINED)
931     return;
932
933   StopMusic();
934
935 #if 0
936   printf("::: STOP MUSIC %d.%d.%d: %d\n",
937          part->anim_nr, part->nr, part->mode_nr, music);
938 #endif
939 }
940
941 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
942 {
943   // when drawing animations to fading buffer, do not play sounds or music
944   if (drawing_to_fading_buffer)
945     return;
946
947   PlayGlobalAnimSound(part);
948   PlayGlobalAnimMusic(part);
949 }
950
951 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
952 {
953   StopGlobalAnimSound(part);
954   StopGlobalAnimMusic(part);
955 }
956
957 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
958 {
959   struct GraphicInfo *c = &part->control_info;
960   int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
961   int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
962
963   if (mask & ANIM_EVENT_ANY)
964     return (c->init_event & ANIM_EVENT_ANY ||
965             c->anim_event & ANIM_EVENT_ANY);
966   else if (mask & ANIM_EVENT_SELF)
967     return (c->init_event & ANIM_EVENT_SELF ||
968             c->anim_event & ANIM_EVENT_SELF);
969   else
970     return ((c->init_event & trigger_mask) == mask ||
971             (c->anim_event & trigger_mask) == mask ||
972             (c->init_event & trigger_mask) == mask_anim_only ||
973             (c->anim_event & trigger_mask) == mask_anim_only);
974 }
975
976 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
977                              int mx, int my, boolean clicked)
978 {
979   struct GraphicInfo *g = &part->graphic_info;
980   int part_x = part->viewport_x + part->x;
981   int part_y = part->viewport_y + part->y;
982   int part_width  = g->width;
983   int part_height = g->height;
984
985   // check if mouse click was detected at all
986   if (!clicked)
987     return FALSE;
988
989   // check if mouse click is inside the animation part's viewport
990   if (mx <  part->viewport_x ||
991       mx >= part->viewport_x + part->viewport_width ||
992       my <  part->viewport_y ||
993       my >= part->viewport_y + part->viewport_height)
994     return FALSE;
995
996   // check if mouse click is inside the animation part's graphic
997   if (mx <  part_x ||
998       mx >= part_x + part_width ||
999       my <  part_y ||
1000       my >= part_y + part_height)
1001     return FALSE;
1002
1003   return TRUE;
1004 }
1005
1006 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1007 {
1008   return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1009 }
1010
1011 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
1012 {
1013   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1014   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1015   struct GraphicInfo *g = &part->graphic_info;
1016   struct GraphicInfo *c = &part->control_info;
1017   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1018
1019   if (viewport_changed)
1020     state |= ANIM_STATE_RESTART;
1021
1022   if (state & ANIM_STATE_RESTART)
1023   {
1024     // when drawing animations to fading buffer, only start fixed animations
1025     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1026                                      c->y == ARG_UNDEFINED_VALUE))
1027       return ANIM_STATE_INACTIVE;
1028
1029     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1030
1031     part->init_delay_counter =
1032       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1033
1034     part->anim_delay_counter =
1035       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1036
1037     part->init_event_state = c->init_event;
1038     part->anim_event_state = c->anim_event;
1039
1040     part->initial_anim_sync_frame =
1041       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1042
1043     if (c->direction & MV_HORIZONTAL)
1044     {
1045       int pos_bottom = part->viewport_height - g->height;
1046
1047       if (c->position == POS_TOP)
1048         part->y = 0;
1049       else if (c->position == POS_UPPER)
1050         part->y = GetSimpleRandom(pos_bottom / 2);
1051       else if (c->position == POS_MIDDLE)
1052         part->y = pos_bottom / 2;
1053       else if (c->position == POS_LOWER)
1054         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1055       else if (c->position == POS_BOTTOM)
1056         part->y = pos_bottom;
1057       else
1058         part->y = GetSimpleRandom(pos_bottom);
1059
1060       if (c->direction == MV_RIGHT)
1061       {
1062         part->step_xoffset = c->step_offset;
1063         part->x = -g->width + part->step_xoffset;
1064       }
1065       else
1066       {
1067         part->step_xoffset = -c->step_offset;
1068         part->x = part->viewport_width + part->step_xoffset;
1069       }
1070
1071       part->step_yoffset = 0;
1072     }
1073     else if (c->direction & MV_VERTICAL)
1074     {
1075       int pos_right = part->viewport_width - g->width;
1076
1077       if (c->position == POS_LEFT)
1078         part->x = 0;
1079       else if (c->position == POS_RIGHT)
1080         part->x = pos_right;
1081       else
1082         part->x = GetSimpleRandom(pos_right);
1083
1084       if (c->direction == MV_DOWN)
1085       {
1086         part->step_yoffset = c->step_offset;
1087         part->y = -g->height + part->step_yoffset;
1088       }
1089       else
1090       {
1091         part->step_yoffset = -c->step_offset;
1092         part->y = part->viewport_height + part->step_yoffset;
1093       }
1094
1095       part->step_xoffset = 0;
1096     }
1097     else
1098     {
1099       part->x = 0;
1100       part->y = 0;
1101
1102       part->step_xoffset = 0;
1103       part->step_yoffset = 0;
1104     }
1105
1106     if (c->x != ARG_UNDEFINED_VALUE)
1107       part->x = c->x;
1108     if (c->y != ARG_UNDEFINED_VALUE)
1109       part->y = c->y;
1110
1111     if (c->position == POS_LAST &&
1112         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1113         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1114     {
1115       part->x = anim->last_x;
1116       part->y = anim->last_y;
1117     }
1118
1119     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1120       part->step_xoffset = c->step_xoffset;
1121     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1122       part->step_yoffset = c->step_yoffset;
1123
1124     if (part->init_delay_counter == 0 &&
1125         part->init_event_state == ANIM_EVENT_NONE)
1126       PlayGlobalAnimSoundAndMusic(part);
1127   }
1128
1129   if (part->clicked &&
1130       part->init_event_state != ANIM_EVENT_NONE)
1131   {
1132     if (part->initial_anim_sync_frame > 0)
1133       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1134
1135     part->init_delay_counter = 1;
1136     part->init_event_state = ANIM_EVENT_NONE;
1137
1138     part->clicked = FALSE;
1139   }
1140
1141   if (part->clicked &&
1142       part->anim_event_state != ANIM_EVENT_NONE)
1143   {
1144     part->anim_delay_counter = 1;
1145     part->anim_event_state = ANIM_EVENT_NONE;
1146
1147     part->clicked = FALSE;
1148   }
1149
1150   if (part->init_delay_counter > 0)
1151   {
1152     part->init_delay_counter--;
1153
1154     if (part->init_delay_counter == 0)
1155     {
1156       part->init_event_state = ANIM_EVENT_NONE;
1157
1158       PlayGlobalAnimSoundAndMusic(part);
1159     }
1160
1161     return ANIM_STATE_WAITING;
1162   }
1163
1164   if (part->init_event_state != ANIM_EVENT_NONE)
1165     return ANIM_STATE_WAITING;
1166
1167   // animation part is now running/visible and therefore clickable
1168   part->clickable = TRUE;
1169
1170   // check if moving animation has left the visible screen area
1171   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1172       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1173       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1174       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1175   {
1176     // do not wait for "anim" events for off-screen animations
1177     part->anim_event_state = ANIM_EVENT_NONE;
1178
1179     // do not stop animation before "anim" or "post" counter are finished
1180     if (part->anim_delay_counter == 0 &&
1181         part->post_delay_counter == 0)
1182     {
1183       StopGlobalAnimSoundAndMusic(part);
1184
1185       part->post_delay_counter =
1186         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1187
1188       if (part->post_delay_counter > 0)
1189         return ANIM_STATE_RUNNING;
1190
1191       // drawing last frame not needed here -- animation not visible anymore
1192       return ANIM_STATE_RESTART;
1193     }
1194   }
1195
1196   if (part->anim_delay_counter > 0)
1197   {
1198     part->anim_delay_counter--;
1199
1200     if (part->anim_delay_counter == 0)
1201     {
1202       part->anim_event_state = ANIM_EVENT_NONE;
1203
1204       StopGlobalAnimSoundAndMusic(part);
1205
1206       part->post_delay_counter =
1207         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1208
1209       if (part->post_delay_counter > 0)
1210         return ANIM_STATE_RUNNING;
1211
1212       // additional state "RUNNING" required to not skip drawing last frame
1213       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1214     }
1215   }
1216
1217   if (part->post_delay_counter > 0)
1218   {
1219     part->post_delay_counter--;
1220
1221     if (part->post_delay_counter == 0)
1222       return ANIM_STATE_RESTART;
1223
1224     return ANIM_STATE_WAITING;
1225   }
1226
1227   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1228                        anim_sync_frame))
1229     return ANIM_STATE_RUNNING;
1230
1231 #if 0
1232   {
1233     static unsigned int last_counter = -1;
1234     unsigned int counter = Counter();
1235
1236     printf("::: NEXT ANIM PART [%d, %d]\n",
1237            anim_sync_frame, counter - last_counter);
1238
1239     last_counter = counter;
1240   }
1241 #endif
1242
1243   part->x += part->step_xoffset;
1244   part->y += part->step_yoffset;
1245
1246   anim->last_x = part->x;
1247   anim->last_y = part->y;
1248
1249   return ANIM_STATE_RUNNING;
1250 }
1251
1252 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1253 {
1254   struct GlobalAnimPartControlInfo *part;
1255   struct GraphicInfo *c = &anim->control_info;
1256   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1257   int state, active_part_nr;
1258   int i;
1259
1260 #if 0
1261   printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1262          anim->mode_nr, anim->nr, anim->num_parts);
1263   printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1264 #endif
1265
1266 #if 0
1267   printf("::: %s(%d): %d, %d, %d [%d]\n",
1268          (action == ANIM_START ? "ANIM_START" :
1269           action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1270           action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1271          anim->nr,
1272          anim->state & ANIM_STATE_RESTART,
1273          anim->state & ANIM_STATE_WAITING,
1274          anim->state & ANIM_STATE_RUNNING,
1275          anim->num_parts);
1276 #endif
1277
1278   switch (action)
1279   {
1280     case ANIM_START:
1281       anim->state = anim->last_state = ANIM_STATE_RESTART;
1282       anim->active_part_nr = anim->last_active_part_nr = 0;
1283       anim->part_counter = 0;
1284
1285       break;
1286
1287     case ANIM_CONTINUE:
1288       if (anim->state == ANIM_STATE_INACTIVE)
1289         return;
1290
1291       anim->state = anim->last_state;
1292       anim->active_part_nr = anim->last_active_part_nr;
1293
1294       break;
1295
1296     case ANIM_STOP:
1297       anim->state = ANIM_STATE_INACTIVE;
1298
1299       for (i = 0; i < num_parts; i++)
1300         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1301
1302       return;
1303
1304     default:
1305       break;
1306   }
1307
1308   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1309   {
1310 #if 0
1311     printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1312            anim->mode_nr, anim->nr, num_parts);
1313 #endif
1314
1315     for (i = 0; i < num_parts; i++)
1316     {
1317       part = &anim->part[i];
1318
1319       switch (action)
1320       {
1321         case ANIM_START:
1322           anim->state = ANIM_STATE_RUNNING;
1323           part->state = ANIM_STATE_RESTART;
1324
1325           break;
1326
1327         case ANIM_CONTINUE:
1328           if (part->state == ANIM_STATE_INACTIVE)
1329             continue;
1330
1331           break;
1332
1333         case ANIM_STOP:
1334           part->state = ANIM_STATE_INACTIVE;
1335
1336           continue;
1337
1338         default:
1339           break;
1340       }
1341
1342       part->state = HandleGlobalAnim_Part(part, part->state);
1343
1344       // when animation mode is "once", stop after animation was played once
1345       if (c->anim_mode & ANIM_ONCE &&
1346           part->state & ANIM_STATE_RESTART)
1347         part->state = ANIM_STATE_INACTIVE;
1348     }
1349
1350     anim->last_state = anim->state;
1351     anim->last_active_part_nr = anim->active_part_nr;
1352
1353     return;
1354   }
1355
1356   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1357     anim->active_part_nr = getGlobalAnimationPart(anim);
1358
1359   part = &anim->part[anim->active_part_nr];
1360
1361   // first set all animation parts to "inactive", ...
1362   for (i = 0; i < num_parts; i++)
1363     anim->part[i].state = ANIM_STATE_INACTIVE;
1364
1365   // ... then set current animation parts to "running"
1366   part->state = ANIM_STATE_RUNNING;
1367
1368   anim->state = HandleGlobalAnim_Part(part, anim->state);
1369
1370   if (anim->state & ANIM_STATE_RESTART)
1371     anim->part_counter++;
1372
1373   // when animation mode is "once", stop after all animations were played once
1374   if (c->anim_mode & ANIM_ONCE &&
1375       anim->part_counter == anim->num_parts)
1376     anim->state = ANIM_STATE_INACTIVE;
1377
1378   state = anim->state;
1379   active_part_nr = anim->active_part_nr;
1380
1381   // while the animation parts are pausing (waiting or inactive), play the base
1382   // (main) animation; this corresponds to the "boring player animation" logic
1383   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1384   if (anim->has_base)
1385   {
1386     if (anim->state == ANIM_STATE_WAITING ||
1387         anim->state == ANIM_STATE_INACTIVE)
1388     {
1389       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1390       part = &anim->part[anim->active_part_nr];
1391
1392       if (anim->state != anim->last_state)
1393         part->state = ANIM_STATE_RESTART;
1394
1395       anim->state = ANIM_STATE_RUNNING;
1396       part->state = HandleGlobalAnim_Part(part, part->state);
1397     }
1398   }
1399
1400   anim->last_state = state;
1401   anim->last_active_part_nr = active_part_nr;
1402 }
1403
1404 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1405 {
1406   int i;
1407
1408 #if 0
1409   printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1410          ctrl->nr, ctrl->num_anims);
1411 #endif
1412
1413   for (i = 0; i < ctrl->num_anims; i++)
1414     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1415 }
1416
1417 static void HandleGlobalAnim(int action, int game_mode)
1418 {
1419 #if 0
1420   printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1421 #endif
1422
1423   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1424 }
1425
1426 static void DoAnimationExt()
1427 {
1428   int i;
1429
1430 #if 0
1431   printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1432 #endif
1433
1434   // global animations now synchronized with frame delay of screen update
1435   anim_sync_frame++;
1436
1437   for (i = 0; i < NUM_GAME_MODES; i++)
1438     HandleGlobalAnim(ANIM_CONTINUE, i);
1439
1440 #if 0
1441   // force screen redraw in next frame to continue drawing global animations
1442   redraw_mask = REDRAW_ALL;
1443 #endif
1444 }
1445
1446 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1447 {
1448   int anim_event_action = part->control_info.anim_event_action;
1449
1450   if (anim_event_action == -1)
1451     return FALSE;
1452
1453   return (DoGadgetAction(anim_event_action) ||
1454           DoScreenAction(anim_event_action));
1455 }
1456
1457 static void InitGlobalAnim_Clickable()
1458 {
1459   int mode_nr;
1460
1461   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1462   {
1463     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1464     int anim_nr;
1465
1466     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1467     {
1468       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1469       int part_nr;
1470
1471       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1472       {
1473         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1474
1475         part->clickable = FALSE;
1476       }
1477     }
1478   }
1479 }
1480
1481 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1482 {
1483   boolean anything_clicked = FALSE;
1484   boolean any_part_clicked = FALSE;
1485   boolean any_event_action = FALSE;
1486   int mode_nr;
1487
1488   // check game modes in reverse draw order (to stop when clicked)
1489   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1490   {
1491     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1492     int anim_nr;
1493
1494     // check animations in reverse draw order (to stop when clicked)
1495     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1496     {
1497       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1498       int part_nr;
1499
1500       // check animation parts in reverse draw order (to stop when clicked)
1501       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1502       {
1503         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1504
1505         if (!clicked)
1506         {
1507           part->clicked = FALSE;
1508
1509           continue;
1510         }
1511
1512         if (!part->clickable)
1513           continue;
1514
1515         if (part->state != ANIM_STATE_RUNNING)
1516           continue;
1517
1518         // always handle "any" click events (clicking anywhere on screen) ...
1519         if (isClickablePart(part, ANIM_EVENT_ANY))
1520         {
1521           part->clicked = TRUE;
1522           anything_clicked = clickConsumed(part);
1523         }
1524
1525         // ... but only handle the first (topmost) clickable animation
1526         if (any_part_clicked)
1527           continue;
1528
1529         if (isClickedPart(part, mx, my, clicked))
1530         {
1531 #if 0
1532           printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1533                  part->control_info.anim_event_action);
1534 #endif
1535
1536           // after executing event action, ignore any further actions
1537           if (!any_event_action && DoGlobalAnim_EventAction(part))
1538             any_event_action = TRUE;
1539
1540           any_part_clicked = TRUE;
1541
1542           if (isClickablePart(part, ANIM_EVENT_SELF))
1543           {
1544             part->clicked = TRUE;
1545             anything_clicked = clickConsumed(part);
1546           }
1547
1548           // check if this click is defined to trigger other animations
1549           int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1550           int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1551           int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1552
1553           if (!part->is_base)
1554             mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1555
1556           int anim2_nr;
1557
1558           for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1559           {
1560             struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1561             int part2_nr;
1562
1563             for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1564             {
1565               struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1566
1567               if (part2->state != ANIM_STATE_RUNNING)
1568                 continue;
1569
1570               if (isClickablePart(part2, mask))
1571               {
1572                 part2->clicked = TRUE;
1573                 anything_clicked = clickConsumed(part); // click was on "part"!
1574
1575 #if 0
1576                 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1577                        part2->control_info.anim_event_action);
1578 #endif
1579
1580                 // after executing event action, ignore any further actions
1581                 if (!any_event_action && DoGlobalAnim_EventAction(part2))
1582                   any_event_action = TRUE;
1583               }
1584
1585 #if 0
1586               struct GraphicInfo *c = &part2->control_info;
1587
1588               printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1589                      anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1590
1591               if (isClickablePart(part2, mask))
1592                 printf(" <--- TRIGGERED BY %d.%d",
1593                        anim_nr, part_nr);
1594
1595               printf("\n");
1596 #endif
1597             }
1598           }
1599         }
1600       }
1601     }
1602   }
1603
1604   return (anything_clicked || any_event_action);
1605 }
1606
1607 static void ResetGlobalAnim_Clickable()
1608 {
1609   InitGlobalAnim_Clickable();
1610 }
1611
1612 static void ResetGlobalAnim_Clicked()
1613 {
1614   InitGlobalAnim_Clicked(-1, -1, FALSE);
1615 }
1616
1617 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1618 {
1619   static boolean click_consumed = FALSE;
1620   static int last_button = 0;
1621   boolean press_event;
1622   boolean release_event;
1623   boolean click_consumed_current = click_consumed;
1624
1625   /* check if button state has changed since last invocation */
1626   press_event   = (button != 0 && last_button == 0);
1627   release_event = (button == 0 && last_button != 0);
1628   last_button = button;
1629
1630   if (press_event)
1631   {
1632     click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1633     click_consumed_current = click_consumed;
1634   }
1635
1636   if (release_event)
1637     click_consumed = FALSE;
1638
1639   return click_consumed_current;
1640 }