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