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