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