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