Debian Codegraph @

terps/taylor/animations.c

raw symbols SCIP
1 //
2 // animations.c
3 // Part of TaylorMade, an interpreter for Adventure Soft UK games
4 //
5 // Created by Petter Sjölund on 2022-03-24.
6 //
7
8 // This is currently inefficient in two ways: 1) we redraw the entire
9 // image when only parts have changed, and 2) we sometimes draw the
10 // entire image, and then draw a second animation on top of this.
11 //
12 // There should be a second buffer array copy that represents what is
13 // currently on-screen, and every pixel should be compared against this
14 // to see if it is the same before being redrawn.
15 //
16 // The click shelf animation should be done by setting the bitmap in a
17 // buffer array first, before drawing the image. This will require a
18 // second copy of the image buffer.
19
20 #include "sagadraw.h"
21 #include "utility.h"
22
23 #include "animations.h"
24
25 #define UNFOLDING_SPACE 50
26 #define STARS_ANIMATION_RATE 15
27 #define STARS_ANIMATION_RATE_64 40
28 #define FIELD_ANIMATION_RATE 10
29 #define SERPENT_ANIMATION_RATE 100
30 #define ROBOT_ANIMATION_RATE 400
31 #define QUEEN_ANIMATION_RATE 120
32
33 int AnimationRunning = 0;
34 static int KaylethAnimationIndex = 0;
35 static int AnimationStage = 0;
36 static int ClickShelfStage = 0;
37
38 extern uint8_t screenbuf[768][9];
39 extern Image *images;
40 extern int draw_to_buffer;
41
42 static void AnimateStars(void)
43 {
44 int carry;
45 /* First fill area with black, erasing all stars */
46 RectFill(48, 16, 160, 32, 0, 0);
47 /* We go line by line and pixel row by pixel row */
48 for (int line = 3; line < 7; line++) {
49 for (int pixrow = 0; pixrow < 8; pixrow++) {
50 carry = 0;
51 /* The left half is rotated one pixel to the left, */
52 /* byte by byte, but we actually rotate to the right */
53 /* because the bytes are flipped in our implementation */
54 /* for some reason */
55 for (int col = 15; col > 5; col--) {
56 uint8_t attribute = screenbuf[col + line * 32][8];
57 glui32 ink = attribute & 7;
58 ink += 8 * ((attribute & 64) == 64);
59 ink = Remap(ink);
60 for (int bit = 0; bit < 8; bit++) {
61 if ((screenbuf[col + line * 32][pixrow] & (1 << bit)) != 0) {
62 PutPixel(col * 8 + bit, line * 8 + pixrow, ink);
63 }
64 }
65 carry = rotate_right_with_carry(&(screenbuf[col + line * 32][pixrow]), carry);
66 }
67 if (carry) {
68 screenbuf[line * 32 + 15][pixrow] = screenbuf[line * 32 + 15][pixrow] | 128;
69 }
70 carry = 0;
71 /* Then the right half */
72 for (int col = 16; col < 26; col++) {
73 uint8_t attribute = screenbuf[col + line * 32][8];
74 glui32 ink = attribute & 7;
75 ink += 8 * ((attribute & 64) == 64);
76 ink = Remap(ink);
77 for (int pix = 0; pix < 8; pix++) {
78 if ((screenbuf[col + line * 32][pixrow] & (1 << pix)) != 0) {
79 PutPixel(col * 8 + pix, line * 8 + pixrow, ink);
80 }
81 }
82 carry = rotate_left_with_carry(&(screenbuf[col + line * 32][pixrow]), carry);
83 }
84 screenbuf[line * 32 + 16][pixrow] = screenbuf[line * 32 + 16][pixrow] | carry;
85 }
86 }
87 }
88
89 static void AnimateForcefield(void)
90 {
91 int carry;
92 /* First fill door area with black, erasing field */
93 RectFill(104, 16, 48, 39, 0, 0);
94 /* We go line by line and pixel row by pixel row */
95
96 uint8_t colour = screenbuf[2 * 32 + 13][8];
97 glui32 ink = Remap(colour & 0x7);
98
99 for (int line = 2; line < 7; line++) {
100 for (int pixrow = 0; pixrow < 8; pixrow++) {
101 carry = 0;
102 for (int col = 13; col < 19; col++) {
103 for (int pix = 0; pix < 8; pix++) {
104 if ((screenbuf[col + line * 32][pixrow] & (1 << pix)) != 0) {
105 PutPixel(col * 8 + pix, line * 8 + pixrow, ink);
106 }
107 }
108 /* The force field is rotated one pixel to the right, */
109 /* byte by byte, but we actually rotate to the left */
110 /* because the bytes are flipped in our implementation */
111 /* for some reason */
112 carry = rotate_left_with_carry(&(screenbuf[col + line * 32][pixrow]), carry);
113 }
114 screenbuf[line * 32 + 13][pixrow] = screenbuf[line * 32 + 13][pixrow] | carry;
115 }
116 }
117 }
118
119 static void FillCell(int cell, glui32 ink)
120 {
121 int startx = (cell % 32) * 8;
122 int starty = (cell / 32) * 8;
123 for (int pixrow = 0; pixrow < 8; pixrow++) {
124 for (int pix = 0; pix < 8; pix++) {
125 if ((screenbuf[cell][pixrow] & (1 << pix)) == 0) {
126 PutPixel(startx + pix, starty + pixrow, ink);
127 }
128 }
129 }
130 }
131
132 static void AnimateQueenComputer(void)
133 {
134 int offset = 3;
135 int rotatingink = 7 - (AnimationStage - 7 * (AnimationStage > 7));
136 /* First fill areas with black */
137 for (int i = 0; i < 3; i++) {
138 for (int line = 3; line <= 6; line += 3) {
139 for (int cell = 0; cell < 2; cell++) {
140 FillCell(line * 32 + offset + cell, 8 + rotatingink);
141 rotatingink--;
142 if (rotatingink < 0)
143 rotatingink = 7;
144 }
145 }
146 if (offset == 3)
147 offset = 10;
148 else if (offset == 10)
149 offset = 27;
150 }
151 for (int cell = 0; cell < 3; cell++) {
152 FillCell(7 * 32 + 18 + cell, 8 + rotatingink--);
153 if (rotatingink < 0)
154 rotatingink = 7;
155 }
156 if (AnimationStage == 0) {
157 draw_to_buffer = 0;
158 /* Image block 18: Arcadian head */
159 DrawSagaPictureAtPos(18, 18, 1);
160 draw_to_buffer = 1;
161 }
162
163 if (AnimationStage == 7) {
164 RectFill(144, 8, 24, 40, 0, 0);
165 }
166
167 AnimationStage++;
168 if (AnimationStage > 15)
169 AnimationStage = 0;
170 }
171
172 static void AnimateKaylethClickShelves(int stage)
173 {
174 RectFill(100, 0, 56, 81, 0, 0);
175 for (int line = 0; line < 10; line++) {
176 for (int col = 12; col < 20; col++) {
177 for (int i = 0; i < 8; i++) {
178 int ypos = line * 8 + i + stage;
179 if (ypos > 79)
180 ypos = ypos - 80;
181 uint8_t attribute = screenbuf[col + (ypos / 8) * 32][8];
182 glui32 ink = attribute & 7;
183 ink += 8 * ((attribute & 64) == 64);
184 attribute = screenbuf[col + ((79 - ypos) / 8) * 32][8];
185 glui32 ink2 = attribute & 7;
186 ink2 += 8 * ((attribute & 64) == 64);
187 ink = Remap(ink);
188 ink2 = Remap(ink2);
189 for (int j = 0; j < 8; j++)
190 if (col > 15) {
191 if ((screenbuf[col + line * 32][i] & (1 << j)) != 0) {
192 PutPixel(col * 8 + j, ypos, ink);
193 }
194 } else {
195 if ((screenbuf[col + (9 - line) * 32][7 - i] & (1 << j)) != 0) {
196 PutPixel(col * 8 + j, 79 - ypos, ink2);
197 }
198 }
199 }
200 }
201 }
202 }
203
204 struct KaylethAnimationFrame {
205 int counter_to_draw_at;
206 int counter;
207 int image;
208 };
209
210 struct KaylethAnimation {
211 int start_frame;
212 int required_object;
213 int number_of_frames;
214 int current_frame;
215 struct KaylethAnimationFrame *frames;
216 };
217
218 struct KaylethAnimation **KaylethAnimations = NULL;
219
220 // This is really the number of rooms. We use NULL for rooms without animation.
221 #define NUMBER_OF_KAYLETH_ANIMATIONS 92
222
223 void LoadKaylethAnimationData(void)
224 {
225 KaylethAnimations = MemAlloc(sizeof(struct KaylethAnimation *) * NUMBER_OF_KAYLETH_ANIMATIONS);
226 KaylethAnimations[0] = NULL;
227 uint8_t *ptr = &FileImage[AnimationData];
228 int counter = 0;
229 while (counter < NUMBER_OF_KAYLETH_ANIMATIONS) {
230 while (*ptr == 0xff) {
231 // No animation for this room
232 KaylethAnimations[counter] = NULL;
233 counter++;
234 ptr++;
235 }
236 if (counter < NUMBER_OF_KAYLETH_ANIMATIONS) {
237 struct KaylethAnimation *anim = MemAlloc(sizeof(struct KaylethAnimation));
238 anim->start_frame = *ptr / 3;
239 ptr++;
240 anim->current_frame = 0;
241 anim->required_object = *ptr;
242 ptr += 2; // Skipping skip byte, always zero initially
243 anim->number_of_frames = 0;
244 for (int i = 0; ptr[i] != 0xff; i += 3) {
245 anim->number_of_frames++;
246 }
247 anim->frames = MemAlloc(anim->number_of_frames * sizeof(struct KaylethAnimationFrame));
248 for (int i = 0; i < anim->number_of_frames; i++) {
249 anim->frames[i].counter_to_draw_at = *ptr;
250 anim->frames[i].counter = 0;
251 ptr += 2; // Skipping counter byte, always zero initially
252 anim->frames[i].image = *ptr++;
253 }
254
255 // Hack Azap chamber animations to look more like the original
256 if (anim->frames[0].image == 118) {
257 anim->frames[1].counter_to_draw_at = 5;
258 anim->frames[3].counter_to_draw_at = 5;
259 anim->frames[4].counter_to_draw_at = 8;
260 }
261
262 KaylethAnimations[counter++] = anim;
263
264 ptr++;
265 }
266 }
267
268 // Hack assembly line animation 2 to look more like the original
269 KaylethAnimations[2]->frames[0].counter_to_draw_at = 10;
270 KaylethAnimations[2]->frames[1].counter_to_draw_at = 20;
271 KaylethAnimations[2]->frames[2].counter_to_draw_at = 20;
272 }
273
274 static int UpdateKaylethAnimationFrames(void) // Draw animation frame
275 {
276 if (KaylethAnimations[MyLoc] == NULL) {
277 return 0;
278 }
279
280 // Temporarily set object 0 to this location
281 int obj0loc = ObjectLoc[0];
282 ObjectLoc[0] = MyLoc;
283 // And object 122 if the destroyer droid has caught us
284 if (Flag[10] > 1)
285 ObjectLoc[122] = MyLoc;
286
287 struct KaylethAnimation *anim = KaylethAnimations[MyLoc];
288
289 // Reset animation if we are in a new room
290 if (KaylethAnimationIndex != MyLoc) {
291 KaylethAnimationIndex = MyLoc;
292 anim->current_frame = 0;
293 }
294
295 if (ObjectLoc[anim->required_object] == MyLoc) {
296
297 struct KaylethAnimationFrame *frame = &anim->frames[anim->current_frame];
298
299 if (frame->counter >= frame->counter_to_draw_at) {
300 DrawTaylor(frame->image);
301 DrawSagaPictureFromBuffer();
302 anim->current_frame++;
303 frame->counter = 0;
304 if (anim->current_frame >= anim->number_of_frames) {
305 anim->current_frame = anim->start_frame;
306 }
307 } else {
308 frame->counter++;
309 }
310 }
311
312 // Reset the temporarily moved objects
313 ObjectLoc[0] = obj0loc;
314 ObjectLoc[122] = DESTROYED;
315 return 1;
316 }
317
318 void UpdateKaylethAnimations(void)
319 {
320 // This is an attempt to make the animation jerky like the original.
321 ClickShelfStage++;
322 if (ClickShelfStage == 9)
323 ClickShelfStage = 0;
324 UpdateKaylethAnimationFrames();
325 if (MyLoc == 3) {
326 AnimateKaylethClickShelves(AnimationStage);
327 if (ClickShelfStage < 7)
328 AnimationStage++;
329 if (AnimationStage > 80)
330 AnimationStage = 0;
331 }
332 }
333
334 void UpdateRebelAnimations(void)
335 {
336 if (MyLoc == 1 && ObjectLoc[UNFOLDING_SPACE] == 1) {
337 AnimateStars();
338 } else if (MyLoc == 86 && ObjectLoc[42] == 86) {
339 AnimateForcefield();
340 } else if (MyLoc == 88 && ObjectLoc[107] == 88) {
341 AnimateQueenComputer();
342 } else if (MyLoc > 28 && MyLoc < 34) {
343 if (ObjectLoc[92] == MyLoc) {
344 AnimationStage++;
345 if (AnimationStage > 5) {
346 AnimationStage = 5;
347 glk_request_timer_events(0);
348 AnimationRunning = 0;
349 }
350 } else {
351 AnimationStage--;
352 if (AnimationStage < 0) {
353 AnimationStage = 0;
354 glk_request_timer_events(0);
355 AnimationRunning = 0;
356 } else if (AnimationStage == 4) {
357 glk_request_timer_events(50);
358 }
359 DrawTaylor(MyLoc);
360 }
361 if (AnimationStage) {
362 DrawSagaPictureAtPos(62 + AnimationStage, 14, 10 - AnimationStage - (AnimationStage > 2));
363 }
364 DrawSagaPictureFromBuffer();
365 } else if (MyLoc == 50 && ObjectLoc[58] == 50) {
366 draw_to_buffer = 0;
367 DrawSagaPictureAtPos(138 + AnimationStage, 13, 2);
368 AnimationStage = (AnimationStage == 0);
369 draw_to_buffer = 1;
370 } else if (MyLoc == 71 && ObjectLoc[36] == 71) {
371 draw_to_buffer = 0;
372 if (!AnimationStage)
373 DrawSagaPictureAtPos(133, 14, 4);
374 else
375 DrawSagaPictureAtPos(142, 17, 6);
376 AnimationStage = (AnimationStage == 0);
377 draw_to_buffer = 1;
378 } else {
379 glk_request_timer_events(0);
380 AnimationRunning = 0;
381 AnimationStage = 0;
382 }
383 }
384
385 void StartAnimations(void)
386 {
387 if (BaseGame == REBEL_PLANET) {
388 if (MyLoc == 1 && ObjectLoc[UNFOLDING_SPACE] == 1) {
389 int rate = STARS_ANIMATION_RATE;
390 if (CurrentGame == REBEL_PLANET_64)
391 rate = STARS_ANIMATION_RATE_64;
392 if (AnimationRunning != rate) {
393 glk_request_timer_events(rate);
394 AnimationRunning = rate;
395 }
396 } else if (MyLoc == 86 && ObjectLoc[42] == 86) {
397 if (AnimationRunning != FIELD_ANIMATION_RATE) {
398 glk_request_timer_events(FIELD_ANIMATION_RATE);
399 AnimationRunning = FIELD_ANIMATION_RATE;
400 }
401 } else if (MyLoc == 88 && ObjectLoc[107] == 88) {
402 if (AnimationRunning != QUEEN_ANIMATION_RATE) {
403 glk_request_timer_events(QUEEN_ANIMATION_RATE);
404 AnimationRunning = QUEEN_ANIMATION_RATE;
405 }
406 } else if (MyLoc > 28 && MyLoc < 34) {
407 glk_request_timer_events(SERPENT_ANIMATION_RATE);
408 UpdateRebelAnimations();
409 } else if (MyLoc == 50 && ObjectLoc[58] == 50) {
410 glk_request_timer_events(ROBOT_ANIMATION_RATE);
411 UpdateRebelAnimations();
412 } else if (MyLoc == 71 && ObjectLoc[36] == 71) {
413 glk_request_timer_events(ROBOT_ANIMATION_RATE);
414 UpdateRebelAnimations();
415 }
416 } else if (BaseGame == KAYLETH) {
417 int speed = 0;
418 if (UpdateKaylethAnimationFrames()) {
419 speed = 20;
420 KaylethAnimationIndex = 0;
421 }
422 if (ObjectLoc[0] == MyLoc && CurrentGame == KAYLETH_64) { // Azap chamber
423 speed = 30;
424 }
425 switch (MyLoc) {
426 case 1:
427 speed = 14;
428 break;
429 case 2: // Conveyor belt
430 speed = 10;
431 break;
432 case 3: // Click shelves
433 if (CurrentGame == KAYLETH_64)
434 speed = 80;
435 else
436 speed = 40;
437 break;
438 case 32: // Videodrome
439 speed = 50;
440 break;
441 case 36: // Guard dome
442 speed = 12;
443 break;
444 case 52:
445 case 53: // Twin peril forest
446 if (ObjectLoc[30] == MyLoc) {
447 speed = 150;
448 UpdateKaylethAnimationFrames();
449 }
450 break;
451 case 56: // Citadel of Zenron
452 speed = 40;
453 break;
454 default:
455 break;
456 }
457 if (AnimationRunning != speed) {
458 glk_request_timer_events(speed);
459 AnimationRunning = speed;
460 }
461 }
462 }