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