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