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