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