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