removed global animation configuration option '.step_frames' again
[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       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
532   }
533
534   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
535                        anim_sync_frame))
536     return ANIM_STATE_RUNNING;
537
538 #if 0
539   {
540     static unsigned int last_counter = -1;
541     unsigned int counter = Counter();
542
543     printf("::: NEXT ANIM PART [%d, %d]\n",
544            anim_sync_frame, counter - last_counter);
545
546     last_counter = counter;
547   }
548 #endif
549
550   part->x += part->step_xoffset;
551   part->y += part->step_yoffset;
552
553   return ANIM_STATE_RUNNING;
554 }
555
556 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
557 {
558   struct GlobalAnimPartControlInfo *part;
559   struct GraphicInfo *c = &anim->control_info;
560   boolean skip = FALSE;
561
562 #if 0
563   printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
564          anim->mode_nr, anim->nr, anim->num_parts);
565   printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
566 #endif
567
568 #if 0
569   printf("::: %s(%d): %d, %d, %d [%d]\n",
570          (action == ANIM_START ? "ANIM_START" :
571           action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
572           action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
573          anim->nr,
574          anim->state & ANIM_STATE_RESTART,
575          anim->state & ANIM_STATE_WAITING,
576          anim->state & ANIM_STATE_RUNNING,
577          anim->num_parts);
578 #endif
579
580   switch (action)
581   {
582     case ANIM_START:
583       anim->state = ANIM_STATE_RESTART;
584       anim->part_counter = 0;
585       anim->active_part_nr = 0;
586       skip = TRUE;
587
588       break;
589
590     case ANIM_CONTINUE:
591       if (anim->state == ANIM_STATE_INACTIVE)
592         skip = TRUE;
593
594       break;
595
596     case ANIM_STOP:
597       anim->state = ANIM_STATE_INACTIVE;
598       skip = TRUE;
599
600       break;
601
602     default:
603       break;
604   }
605
606   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
607   {
608     int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
609     int i;
610
611 #if 0
612     printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
613            anim->mode_nr, anim->nr, num_parts);
614 #endif
615
616     for (i = 0; i < num_parts; i++)
617     {
618       part = &anim->part[i];
619
620       switch (action)
621       {
622         case ANIM_START:
623           anim->state = ANIM_STATE_RUNNING;
624           part->state = ANIM_STATE_RESTART;
625           skip = TRUE;
626
627           break;
628
629         case ANIM_CONTINUE:
630           if (part->state == ANIM_STATE_INACTIVE)
631             skip = TRUE;
632
633           break;
634
635         case ANIM_STOP:
636           part->state = ANIM_STATE_INACTIVE;
637           skip = TRUE;
638
639           break;
640
641         default:
642           break;
643       }
644
645       if (skip)
646         continue;
647
648       part->state = HandleGlobalAnim_Part(part, part->state);
649     }
650
651     return;
652   }
653
654   if (skip)
655     return;
656
657   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
658     anim->active_part_nr = getGlobalAnimationPart(anim);
659
660   part = &anim->part[anim->active_part_nr];
661
662   part->state = ANIM_STATE_RUNNING;
663
664   anim->state = HandleGlobalAnim_Part(part, anim->state);
665
666   if (anim->state & ANIM_STATE_RESTART)
667     anim->part_counter++;
668 }
669
670 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
671 {
672   int i;
673
674 #if 0
675   printf("::: HandleGlobalAnim_Mode: %d => %d\n",
676          ctrl->nr, ctrl->num_anims);
677 #endif
678
679   for (i = 0; i < ctrl->num_anims; i++)
680     HandleGlobalAnim_Main(&ctrl->anim[i], action);
681 }
682
683 void HandleGlobalAnim(int action)
684 {
685 #if 0
686   printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
687 #endif
688
689   HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
690   HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
691 }
692
693 void InitAnimation()
694 {
695   // HandleAnimation(ANIM_START);
696
697 #if 0
698   printf("::: InitAnimation\n");
699 #endif
700
701   // InitCounter();
702
703   InitGlobalAnimControls();
704
705   HandleGlobalAnim(ANIM_START);
706
707   do_animations = TRUE;
708 }
709
710 void StopAnimation()
711 {
712   // HandleAnimation(ANIM_STOP);
713
714 #if 0
715   printf("::: StopAnimation\n");
716 #endif
717
718   HandleGlobalAnim(ANIM_STOP);
719
720   do_animations = FALSE;
721 }
722
723 static void DoAnimationExt()
724 {
725 #if 0
726   printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
727 #endif
728
729 #if 1
730   WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
731   anim_sync_frame++;
732 #else
733   if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
734     anim_sync_frame++;
735 #endif
736
737   HandleGlobalAnim(ANIM_CONTINUE);
738
739 #if 1
740   // force screen redraw in next frame to continue drawing global animations
741   redraw_mask = REDRAW_ALL;
742 #endif
743 }
744
745 void DoAnimation()
746 {
747   // HandleAnimation(ANIM_CONTINUE);
748
749 #if 1
750   // force screen redraw in next frame to continue drawing global animations
751   redraw_mask = REDRAW_ALL;
752 #endif
753 }