added support for post delay for global animations
[rocksndiamonds.git] / src / cartoons.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 // cartoons.c
10 // ============================================================================
11
12 #include "cartoons.h"
13 #include "main.h"
14 #include "tools.h"
15
16
17 /* values for global animation definition */
18 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS + 1)
19 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
20                                             MAX_NUM_TOONS)
21
22 struct GlobalAnimPartControlInfo
23 {
24   int nr;
25   int anim_nr;
26   int mode_nr;
27
28   int graphic;
29   struct GraphicInfo graphic_info;
30   struct GraphicInfo control_info;
31
32   int x, y;
33   int step_xoffset, step_yoffset;
34
35   unsigned int initial_anim_sync_frame;
36   unsigned int step_delay, step_delay_value;
37
38   unsigned int init_delay, init_delay_value;
39   unsigned int anim_delay, anim_delay_value;
40   unsigned int post_delay, post_delay_value;
41
42   int state;
43 };
44
45 struct GlobalAnimMainControlInfo
46 {
47   struct GlobalAnimPartControlInfo base;
48   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
49
50   int nr;
51   int mode_nr;
52
53   struct GraphicInfo control_info;
54
55   int num_parts;
56   int part_counter;
57   int active_part_nr;
58
59   boolean has_base;
60
61   unsigned int init_delay, init_delay_value;
62
63   int state;
64 };
65
66 struct GlobalAnimControlInfo
67 {
68   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
69
70   int nr;
71   int num_anims;
72 };
73
74
75 /* forward declaration for internal use */
76 static void DoAnimationExt(void);
77
78 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_SPECIAL_GFX_ARGS];
79 static struct ToonInfo toons[MAX_NUM_TOONS];
80
81 static unsigned int anim_sync_frame = 0;
82 static unsigned int anim_sync_frame_delay = 0;
83 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
84
85 static boolean do_animations = FALSE;
86
87
88 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
89 {
90   struct GraphicInfo *c = &anim->control_info;
91   int last_anim_random_frame = gfx.anim_random_frame;
92   int part_nr;
93
94   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
95
96   part_nr = getAnimationFrame(anim->num_parts, 1,
97                               c->anim_mode, c->anim_start_frame,
98                               anim->part_counter);
99
100   gfx.anim_random_frame = last_anim_random_frame;
101
102   return part_nr;
103 }
104
105 static void PrepareBackbuffer()
106 {
107   if (game_status != GAME_MODE_PLAYING)
108     return;
109
110   BlitScreenToBitmap(backbuffer);
111 }
112
113 boolean ToonNeedsRedraw()
114 {
115   return TRUE;
116 }
117
118 void InitToons()
119 {
120   int num_toons = MAX_NUM_TOONS;
121   int i;
122
123   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
124     num_toons = global.num_toons;
125
126   for (i = 0; i < num_toons; i++)
127   {
128     int graphic = IMG_TOON_1 + i;
129     struct FileInfo *image = getImageListEntryFromImageID(graphic);
130
131     toons[i].bitmap = graphic_info[graphic].bitmap;
132
133     toons[i].src_x = graphic_info[graphic].src_x;
134     toons[i].src_y = graphic_info[graphic].src_y;
135
136     toons[i].width  = graphic_info[graphic].width;
137     toons[i].height = graphic_info[graphic].height;
138
139     toons[i].anim_frames      = graphic_info[graphic].anim_frames;
140     toons[i].anim_delay       = graphic_info[graphic].anim_delay;
141     toons[i].anim_mode        = graphic_info[graphic].anim_mode;
142     toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
143
144     toons[i].step_offset = graphic_info[graphic].step_offset;
145     toons[i].step_delay  = graphic_info[graphic].step_delay;
146
147     toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
148     toons[i].position = image->parameter[GFX_ARG_POSITION];
149   }
150
151   InitToonScreen(bitmap_db_toons,
152                  BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
153                  toons, num_toons,
154                  REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
155                  GAME_FRAME_DELAY);
156 }
157
158 static void InitToonControls()
159 {
160   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_DEFAULT];
161   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
162   int mode_nr, anim_nr, part_nr;
163   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
164   int num_toons = MAX_NUM_TOONS;
165   int i;
166
167   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
168     num_toons = global.num_toons;
169
170   mode_nr = GAME_MODE_DEFAULT;
171   anim_nr = ctrl->num_anims;
172
173   anim->nr = anim_nr;
174   anim->mode_nr = mode_nr;
175   anim->control_info = graphic_info[control];
176
177   anim->num_parts = 0;
178   anim->part_counter = 0;
179   anim->active_part_nr = 0;
180
181   anim->has_base = FALSE;
182
183   anim->init_delay = 0;
184   anim->init_delay_value = 0;
185
186   anim->state = ANIM_STATE_INACTIVE;
187
188   part_nr = 0;
189
190   for (i = 0; i < num_toons; i++)
191   {
192     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
193     int graphic = IMG_TOON_1 + i;
194     int control = graphic;
195
196     part->nr = part_nr;
197     part->anim_nr = anim_nr;
198     part->mode_nr = mode_nr;
199     part->graphic = graphic;
200     part->graphic_info = graphic_info[graphic];
201     part->control_info = graphic_info[control];
202
203     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
204
205     part->control_info.init_delay_fixed = 0;
206     part->control_info.init_delay_random = 150;
207
208     part->control_info.x = ARG_UNDEFINED_VALUE;
209     part->control_info.y = ARG_UNDEFINED_VALUE;
210
211     part->step_delay = 0;
212     part->step_delay_value = graphic_info[control].step_delay;
213
214     part->state = ANIM_STATE_INACTIVE;
215
216     anim->num_parts++;
217     part_nr++;
218   }
219
220   ctrl->num_anims++;
221 }
222
223 void InitGlobalAnimControls()
224 {
225   int m, a, p;
226   int mode_nr, anim_nr, part_nr;
227   int graphic, control;
228
229   anim_sync_frame = 0;
230
231   ResetDelayCounter(&anim_sync_frame_delay);
232
233   for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
234   {
235     mode_nr = m;
236
237     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
238
239     ctrl->nr = mode_nr;
240     ctrl->num_anims = 0;
241
242     anim_nr = 0;
243
244     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
245     {
246       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
247       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
248
249       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
250
251       // if no base animation parameters defined, use default values
252       if (control == IMG_UNDEFINED)
253         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
254
255       anim->nr = anim_nr;
256       anim->mode_nr = mode_nr;
257       anim->control_info = graphic_info[control];
258
259       anim->num_parts = 0;
260       anim->part_counter = 0;
261       anim->active_part_nr = 0;
262
263       anim->has_base = FALSE;
264
265       anim->init_delay = 0;
266       anim->init_delay_value = 0;
267
268       anim->state = ANIM_STATE_INACTIVE;
269
270       part_nr = 0;
271
272       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
273       {
274         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
275
276         graphic = global_anim_info[a].graphic[p][m];
277         control = global_anim_info[ctrl_id].graphic[p][m];
278
279         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
280             control == IMG_UNDEFINED)
281           continue;
282
283 #if 0
284         printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
285                m, a, p, mode_nr, anim_nr, part_nr, control);
286 #endif
287
288         part->nr = part_nr;
289         part->anim_nr = anim_nr;
290         part->mode_nr = mode_nr;
291         part->graphic = graphic;
292         part->graphic_info = graphic_info[graphic];
293         part->control_info = graphic_info[control];
294
295         part->step_delay = 0;
296         part->step_delay_value = graphic_info[control].step_delay;
297
298         part->state = ANIM_STATE_INACTIVE;
299
300         if (p < GLOBAL_ANIM_ID_PART_BASE)
301         {
302           anim->num_parts++;
303           part_nr++;
304         }
305         else
306         {
307           anim->base = *part;
308           anim->has_base = TRUE;
309         }
310       }
311
312       if (anim->num_parts > 0 || anim->has_base)
313       {
314         ctrl->num_anims++;
315         anim_nr++;
316       }
317     }
318   }
319
320   InitToonControls();
321 }
322
323 void DrawGlobalAnim()
324 {
325   int mode_nr;
326
327   if (game_status == GAME_MODE_LOADING)
328     do_animations = FALSE;
329
330   if (!do_animations || !setup.toons)
331     return;
332
333   DoAnimationExt();
334
335   for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
336   {
337     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
338     int anim_nr;
339
340     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
341         mode_nr != game_status)
342       continue;
343
344     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
345     {
346       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
347       struct GraphicInfo *c = &anim->control_info;
348       int part_first, part_last;
349       int part_nr;
350
351       if (!(anim->state & ANIM_STATE_RUNNING))
352         continue;
353
354       part_first = part_last = anim->active_part_nr;
355
356       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
357       {
358         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
359
360         part_first = 0;
361         part_last = num_parts - 1;
362       }
363
364       for (part_nr = part_first; part_nr <= part_last; part_nr++)
365       {
366         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
367         struct GraphicInfo *g = &part->graphic_info;
368         Bitmap *src_bitmap;
369         int src_x, src_y;
370         int width  = g->width;
371         int height = g->height;
372         int dst_x = part->x;
373         int dst_y = part->y;
374         int cut_x = 0;
375         int cut_y = 0;
376         int sync_frame;
377         int frame;
378
379         if (!(part->state & ANIM_STATE_RUNNING))
380           continue;
381
382         if (part->x < 0)
383         {
384           dst_x = 0;
385           width += part->x;
386           cut_x = -part->x;
387         }
388         else if (part->x > FULL_SXSIZE - g->width)
389           width -= (part->x - (FULL_SXSIZE - g->width));
390
391         if (part->y < 0)
392         {
393           dst_y = 0;
394           height += part->y;
395           cut_y = -part->y;
396         }
397         else if (part->y > FULL_SYSIZE - g->height)
398           height -= (part->y - (FULL_SYSIZE - g->height));
399
400         dst_x += REAL_SX;
401         dst_y += REAL_SY;
402
403         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
404         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
405                                   g->anim_mode, g->anim_start_frame,
406                                   sync_frame);
407
408         getFixedGraphicSource(part->graphic, frame, &src_bitmap,
409                               &src_x, &src_y);
410
411         src_x += cut_x;
412         src_y += cut_y;
413
414         BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
415                            dst_x, dst_y);
416       }
417     }
418   }
419 }
420
421 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
422 {
423   struct GraphicInfo *g = &part->graphic_info;
424   struct GraphicInfo *c = &part->control_info;
425
426   if (state & ANIM_STATE_RESTART)
427   {
428     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
429
430     part->init_delay_value =
431       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
432
433     part->anim_delay_value =
434       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
435
436     part->initial_anim_sync_frame =
437       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_value);
438
439     if (c->direction & MV_HORIZONTAL)
440     {
441       int pos_bottom = FULL_SYSIZE - g->height;
442
443       if (c->position == POS_TOP)
444         part->y = 0;
445       else if (c->position == POS_UPPER)
446         part->y = GetSimpleRandom(pos_bottom / 2);
447       else if (c->position == POS_MIDDLE)
448         part->y = pos_bottom / 2;
449       else if (c->position == POS_LOWER)
450         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
451       else if (c->position == POS_BOTTOM)
452         part->y = pos_bottom;
453       else
454         part->y = GetSimpleRandom(pos_bottom);
455
456       if (c->direction == MV_RIGHT)
457       {
458         part->step_xoffset = c->step_offset;
459         part->x = -g->width + part->step_xoffset;
460       }
461       else
462       {
463         part->step_xoffset = -c->step_offset;
464         part->x = FULL_SXSIZE + part->step_xoffset;
465       }
466
467       part->step_yoffset = 0;
468     }
469     else if (c->direction & MV_VERTICAL)
470     {
471       int pos_right = FULL_SXSIZE - g->width;
472
473       if (c->position == POS_LEFT)
474         part->x = 0;
475       else if (c->position == POS_RIGHT)
476         part->x = pos_right;
477       else
478         part->x = GetSimpleRandom(pos_right);
479
480       if (c->direction == MV_DOWN)
481       {
482         part->step_yoffset = c->step_offset;
483         part->y = -g->height + part->step_yoffset;
484       }
485       else
486       {
487         part->step_yoffset = -c->step_offset;
488         part->y = FULL_SYSIZE + part->step_yoffset;
489       }
490
491       part->step_xoffset = 0;
492     }
493     else
494     {
495       part->x = 0;
496       part->y = 0;
497
498       part->step_xoffset = 0;
499       part->step_yoffset = 0;
500     }
501
502     if (c->x != ARG_UNDEFINED_VALUE)
503       part->x = c->x;
504     if (c->y != ARG_UNDEFINED_VALUE)
505       part->y = c->y;
506
507     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
508       part->step_xoffset = c->step_xoffset;
509     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
510       part->step_yoffset = c->step_yoffset;
511   }
512
513   if (part->init_delay_value > 0)
514   {
515     part->init_delay_value--;
516
517     return ANIM_STATE_WAITING;
518   }
519
520   if ((part->x <= -g->width    && part->step_xoffset <= 0) ||
521       (part->x >=  FULL_SXSIZE && part->step_xoffset >= 0) ||
522       (part->y <= -g->height   && part->step_yoffset <= 0) ||
523       (part->y >=  FULL_SYSIZE && part->step_yoffset >= 0))
524     return ANIM_STATE_RESTART;
525
526   if (part->anim_delay_value > 0)
527   {
528     part->anim_delay_value--;
529
530     if (part->anim_delay_value == 0)
531     {
532       part->post_delay_value =
533         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
534
535       if (part->post_delay_value > 0)
536         return ANIM_STATE_RUNNING;
537
538       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
539     }
540   }
541
542   if (part->post_delay_value > 0)
543   {
544     part->post_delay_value--;
545
546     if (part->post_delay_value == 0)
547       return ANIM_STATE_RESTART;
548
549     return ANIM_STATE_WAITING;
550   }
551
552   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
553                        anim_sync_frame))
554     return ANIM_STATE_RUNNING;
555
556 #if 0
557   {
558     static unsigned int last_counter = -1;
559     unsigned int counter = Counter();
560
561     printf("::: NEXT ANIM PART [%d, %d]\n",
562            anim_sync_frame, counter - last_counter);
563
564     last_counter = counter;
565   }
566 #endif
567
568   part->x += part->step_xoffset;
569   part->y += part->step_yoffset;
570
571   return ANIM_STATE_RUNNING;
572 }
573
574 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
575 {
576   struct GlobalAnimPartControlInfo *part;
577   struct GraphicInfo *c = &anim->control_info;
578   boolean skip = FALSE;
579
580 #if 0
581   printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
582          anim->mode_nr, anim->nr, anim->num_parts);
583   printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
584 #endif
585
586 #if 0
587   printf("::: %s(%d): %d, %d, %d [%d]\n",
588          (action == ANIM_START ? "ANIM_START" :
589           action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
590           action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
591          anim->nr,
592          anim->state & ANIM_STATE_RESTART,
593          anim->state & ANIM_STATE_WAITING,
594          anim->state & ANIM_STATE_RUNNING,
595          anim->num_parts);
596 #endif
597
598   switch (action)
599   {
600     case ANIM_START:
601       anim->state = ANIM_STATE_RESTART;
602       anim->part_counter = 0;
603       anim->active_part_nr = 0;
604       skip = TRUE;
605
606       break;
607
608     case ANIM_CONTINUE:
609       if (anim->state == ANIM_STATE_INACTIVE)
610         skip = TRUE;
611
612       break;
613
614     case ANIM_STOP:
615       anim->state = ANIM_STATE_INACTIVE;
616       skip = TRUE;
617
618       break;
619
620     default:
621       break;
622   }
623
624   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
625   {
626     int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
627     int i;
628
629 #if 0
630     printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
631            anim->mode_nr, anim->nr, num_parts);
632 #endif
633
634     for (i = 0; i < num_parts; i++)
635     {
636       part = &anim->part[i];
637
638       switch (action)
639       {
640         case ANIM_START:
641           anim->state = ANIM_STATE_RUNNING;
642           part->state = ANIM_STATE_RESTART;
643           skip = TRUE;
644
645           break;
646
647         case ANIM_CONTINUE:
648           if (part->state == ANIM_STATE_INACTIVE)
649             skip = TRUE;
650
651           break;
652
653         case ANIM_STOP:
654           part->state = ANIM_STATE_INACTIVE;
655           skip = TRUE;
656
657           break;
658
659         default:
660           break;
661       }
662
663       if (skip)
664         continue;
665
666       part->state = HandleGlobalAnim_Part(part, part->state);
667     }
668
669     return;
670   }
671
672   if (skip)
673     return;
674
675   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
676     anim->active_part_nr = getGlobalAnimationPart(anim);
677
678   part = &anim->part[anim->active_part_nr];
679
680   part->state = ANIM_STATE_RUNNING;
681
682   anim->state = HandleGlobalAnim_Part(part, anim->state);
683
684   if (anim->state & ANIM_STATE_RESTART)
685     anim->part_counter++;
686 }
687
688 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
689 {
690   int i;
691
692 #if 0
693   printf("::: HandleGlobalAnim_Mode: %d => %d\n",
694          ctrl->nr, ctrl->num_anims);
695 #endif
696
697   for (i = 0; i < ctrl->num_anims; i++)
698     HandleGlobalAnim_Main(&ctrl->anim[i], action);
699 }
700
701 void HandleGlobalAnim(int action)
702 {
703 #if 0
704   printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
705 #endif
706
707   HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
708   HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
709 }
710
711 void InitAnimation()
712 {
713   // HandleAnimation(ANIM_START);
714
715 #if 0
716   printf("::: InitAnimation\n");
717 #endif
718
719   // InitCounter();
720
721   InitGlobalAnimControls();
722
723   HandleGlobalAnim(ANIM_START);
724
725   do_animations = TRUE;
726 }
727
728 void StopAnimation()
729 {
730   // HandleAnimation(ANIM_STOP);
731
732 #if 0
733   printf("::: StopAnimation\n");
734 #endif
735
736   HandleGlobalAnim(ANIM_STOP);
737
738   do_animations = FALSE;
739 }
740
741 static void DoAnimationExt()
742 {
743 #if 0
744   printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
745 #endif
746
747 #if 1
748   WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
749   anim_sync_frame++;
750 #else
751   if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
752     anim_sync_frame++;
753 #endif
754
755   HandleGlobalAnim(ANIM_CONTINUE);
756
757 #if 1
758   // force screen redraw in next frame to continue drawing global animations
759   redraw_mask = REDRAW_ALL;
760 #endif
761 }
762
763 void DoAnimation()
764 {
765   // HandleAnimation(ANIM_CONTINUE);
766
767 #if 1
768   // force screen redraw in next frame to continue drawing global animations
769   redraw_mask = REDRAW_ALL;
770 #endif
771 }