Mirai's Miscellaneous Misadventures
M28 / core / animations.c
1
2
3
4#include <mimimi/models.h>
5#include <mimimi/appearances.h>
6#include <mimimi/allocators.h>
7#include <mimimi/animations.h>
8#include <mimimi/assets.h>
9#include <mimimi/poses.h>
10
11#include "math.c"
12
13static unsigned char mimimi_layer_pixel(struct mimimi_layer *layer, unsigned char y_angle, int x, int y)
14{
15 int h = layer->count;
16
17 if (y < 0) return 0;
18 if (y >= h) return 0;
19
20 int w0 = layer->rows[y].width;
21 int w1 = layer->rows[y].height;
22
23 int w = mimimi_sine[y_angle] * w0 + (128 - mimimi_sine[y_angle]) * w1;
24 w /= 128;
25
26 if (x < -w / 2) return 0;
27 if (x >= w / 2) return 0;
28
29 x += w / 2;
30
31 int count = layer->rows[y].count;
32
33 int a = y_angle;
34 a *= count;
35 a += 128;
36 a /= 256;
37 a += x;
38 a %= count;
39
40 return layer->rows[y].colors[a];
41}
42
43static void mimimi_swap(int *x, int *y)
44{
45 int z = *x;
46 *x = *y;
47 *y = z;
48}
49
50static void mimimi_layer(struct mimimi_image *image, struct mimimi_layer *layer, int x0, int y0, struct mimimi_pose_layer *pose_layer, int behind)
51{
52 int width = image->width;
53 int height = image->height;
54
55 unsigned char y_angle = pose_layer->y_angle;
56 int z_angle = mimimi_mod(pose_layer->z_angle, 4);
57
58 if (behind != 0) y_angle += 128;
59
60 if (z_angle == 0)
61 {
62 for (int y = 0 ; y < height ; y++)
63 for (int x = 0 ; x < width ; x++)
64 {
65 int x1 = x + layer->x - x0;
66 int y1 = y + layer->y - y0;
67
68 x1 += pose_layer->slant * (y - y0) / 256;
69
70 if (behind != 0) x1 = -x1 - 1;
71
72 unsigned char color = mimimi_layer_pixel(layer, y_angle, x1, y1);
73 if (color != 0) image->colors[x + y * width] = color;
74 }
75 }
76
77 if (z_angle == 1)
78 {
79 for (int y = 0 ; y < height ; y++)
80 for (int x = 0 ; x < width ; x++)
81 {
82 int x1 = x - layer->y - x0;
83 int y1 = y + layer->x - y0;
84
85 y1 -= pose_layer->slant * (x - x0) / 256;
86
87 if (behind != 0) y1 = -y1 - 1;
88
89 unsigned char color = mimimi_layer_pixel(layer, y_angle, y1, -x1);
90 if (color != 0) image->colors[x + y * width] = color;
91 }
92
93 return;
94 }
95
96 if (z_angle == 2)
97 {
98 for (int y = 0 ; y < height ; y++)
99 for (int x = 0 ; x < width ; x++)
100 {
101 int x1 = x - layer->x - x0;
102 int y1 = y - layer->y - y0;
103
104 x1 += pose_layer->slant * (y - y0) / 256;
105
106 if (behind != 0) x1 = -x1 + 1;
107
108 unsigned char color = mimimi_layer_pixel(layer, y_angle, -x1, -y1);
109 if (color != 0) image->colors[x + y * width] = color;
110 }
111
112 return;
113 }
114
115 if (z_angle == 3)
116 {
117 for (int y = 0 ; y < height ; y++)
118 for (int x = 0 ; x < width ; x++)
119 {
120 int x1 = x + layer->y - x0;
121 int y1 = y - layer->x - y0;
122
123 y1 -= pose_layer->slant * (x - x0) / 256;
124
125 if (behind != 0) y1 = -y1 + 1;
126
127 unsigned char color = mimimi_layer_pixel(layer, y_angle, -y1, x1);
128 if (color != 0) image->colors[x + y * width] = color;
129 }
130
131 return;
132 }
133}
134
135static void mimimi_apply_y_angle(struct mimimi_model *model, struct mimimi_layer *layers, struct mimimi_pose_layer *pose_layers)
136{
137 for (int i = 0 ; i < model->count ; i++)
138 {
139 struct mimimi_layer *layer = layers + i;
140 struct mimimi_pose_layer *pose_layer = pose_layers + i;
141 int parent_index = model->layers[i].parent_index;
142
143 if (i == parent_index) continue;
144
145 int parent_y_angle = pose_layers[parent_index].y_angle;
146 pose_layer->y_angle += parent_y_angle;
147
148 unsigned char y_angle = parent_y_angle;
149
150 int sin = mimimi_sine[y_angle];
151 int cos = mimimi_cosine[y_angle];
152
153 int x = layer->x;
154 int z = layer->z;
155
156 layer->x = cos*x / 128 + sin*z / 128;
157 layer->z = cos*z / 128 - sin*x / 128;
158 }
159}
160
161static void mimimi_apply_z_angle(struct mimimi_model *model, struct mimimi_layer *layers, struct mimimi_pose_layer *pose_layers, struct mimimi_allocator *allocator)
162{
163 int *xs = (*allocator->allocate)(sizeof *xs * model->count);
164 int *ys = (*allocator->allocate)(sizeof *ys * model->count);
165
166 for (int i = 0 ; i < model->count ; i++)
167 {
168 struct mimimi_layer *layer = layers + i;
169 struct mimimi_pose_layer *pose_layer = pose_layers + i;
170 int parent_index = model->layers[i].parent_index;
171
172 xs[i] = layer->x;
173 ys[i] = layer->y;
174 if (i == parent_index) continue;
175 xs[i] += xs[parent_index];
176 ys[i] += ys[parent_index];
177
178 int z_angle = mimimi_mod(pose_layer->z_angle, 4);
179
180 int x = xs[i];
181 int y = ys[i];
182 if (z_angle == 1) x = -x, mimimi_swap(&x, &y);
183 if (z_angle == 2) x = -x, y = -y;
184 if (z_angle == 3) y = -y, mimimi_swap(&x, &y);
185 layer->x = x - xs[parent_index];
186 layer->y = y - ys[parent_index];
187
188 if (z_angle == 1) xs[i] = -xs[i], mimimi_swap(xs + i, ys + i);
189 if (z_angle == 2) xs[i] = -xs[i], ys[i] = -ys[i];
190 if (z_angle == 3) ys[i] = -ys[i], mimimi_swap(xs + i, ys + i);
191 }
192
193 (*allocator->deallocate)(xs);
194 (*allocator->deallocate)(ys);
195}
196
197static void mimimi_apply_slant(struct mimimi_model *model, struct mimimi_layer *layers, struct mimimi_pose_layer *pose_layers, struct mimimi_allocator *allocator)
198{
199 int *ys = (*allocator->allocate)(sizeof *ys * model->count);
200
201 for (int i = 0 ; i < model->count ; i++)
202 {
203 struct mimimi_layer *layer = layers + i;
204 struct mimimi_pose_layer *pose_layer = pose_layers + i;
205 int parent_index = model->layers[i].parent_index;
206
207 ys[i] = layer->y;
208 if (i == parent_index) continue;
209 ys[i] += ys[parent_index];
210
211 int y = ys[i];
212 layer->x += pose_layer->slant * ys[i] / 256;
213 }
214
215 (*allocator->deallocate)(ys);
216}
217
218static void mimimi_absolutize(struct mimimi_model *model, struct mimimi_layer *layers, struct mimimi_pose_layer *pose_layers)
219{
220 for (int i = 0 ; i < model->count ; i++)
221 {
222 struct mimimi_layer *layer = layers + i;
223 struct mimimi_pose_layer *pose_layer = pose_layers + i;
224 int parent_index = model->layers[i].parent_index;
225 if (i == parent_index) continue;
226
227 layer->x += layers[parent_index].x;
228 layer->y += layers[parent_index].y;
229 layer->z += layers[parent_index].z;
230
231 pose_layer->slant += pose_layers[parent_index].slant;
232 pose_layer->z_angle += pose_layers[parent_index].z_angle;
233 pose_layer->z_angle = mimimi_mod(pose_layer->z_angle, 4);
234 }
235}
236
237void mimimi_pose(struct mimimi_image *image, struct mimimi_model *model, struct mimimi_pose *pose, struct mimimi_allocator *allocator)
238{
239 struct mimimi_layer *layers = (*allocator->allocate)(sizeof *layers * model->count);
240 struct mimimi_pose_layer *pose_layers = (*allocator->allocate)(sizeof *pose_layers * model->count);
241
242 struct mimimi_half_layer { int behind; int z; struct mimimi_layer *layer; struct mimimi_pose_layer *pose_layer; };
243 struct mimimi_half_layer *half_layers = (*allocator->allocate)(sizeof *half_layers * model->count * 2);
244
245 for (int i = 0 ; i < model->count ; i++)
246 {
247 int parent_index = model->layers[i].parent_index;
248
249 struct mimimi_layer *layer = layers + i;
250 *layer = *model->layers[i].layer;
251
252 struct mimimi_pose_layer *pose_layer = pose_layers + i;
253
254 if (i < pose->count)
255 {
256 *pose_layer = pose->layers[i];
257 }
258 else
259 {
260 pose_layer->slant = 0;
261 pose_layer->y_angle = 0;
262 pose_layer->z_angle = 0;
263 }
264 }
265
266 mimimi_apply_y_angle(model, layers, pose_layers);
267 mimimi_apply_z_angle(model, layers, pose_layers, allocator);
268 mimimi_apply_slant(model, layers, pose_layers, allocator);
269 mimimi_absolutize(model, layers, pose_layers);
270
271 for (int i = 0 ; i < model->count ; i++)
272 {
273 struct mimimi_layer *layer = layers + i;
274 struct mimimi_pose_layer *pose_layer = pose_layers + i;
275
276 unsigned char y_angle = pose_layers[i].y_angle;
277
278 int width = layer->width;
279
280 half_layers[i * 2 + 0].behind = 0;
281 half_layers[i * 2 + 0].z = layer->z + width;
282 half_layers[i * 2 + 0].layer = layer;
283 half_layers[i * 2 + 0].pose_layer = pose_layer;
284
285 half_layers[i * 2 + 1].behind = 1;
286 half_layers[i * 2 + 1].z = layer->z - width;
287 half_layers[i * 2 + 1].layer = layer;
288 half_layers[i * 2 + 1].pose_layer = pose_layer;
289 }
290
291
292 for (int i = 0 ; i < model->count * 2 ; i++)
293 {
294 int a = half_layers[i].z;
295
296 int k = i;
297 for (int j = i - 1 ; j >= 0 ; j--)
298 {
299 int b = half_layers[j].z;
300 if (a >= b) break;
301
302 struct mimimi_half_layer half_layer = half_layers[k];
303 half_layers[k] = half_layers[j];
304 half_layers[j] = half_layer;
305
306 k = j;
307 }
308 }
309
310 for (int y = 0 ; y < image->height ; y++)
311 for (int x = 0 ; x < image->width ; x++)
312 image->colors[x + y * image->width] = 0;
313
314 for (int i = 0 ; i < model->count * 2 ; i++)
315 {
316 struct mimimi_half_layer *half_layer = half_layers + i;
317 mimimi_layer(image, half_layer->layer, pose->x, pose->y, half_layer->pose_layer, half_layer->behind);
318 }
319
320 (*allocator->deallocate)(layers);
321 (*allocator->deallocate)(pose_layers);
322 (*allocator->deallocate)(half_layers);
323}
324
325static void mimimi_interpolate_pose_layer(struct mimimi_pose_layer *a, struct mimimi_pose_layer *b, int i, int count)
326{
327 int slant = a->slant * (count - i) / count;
328 slant += mimimi_div(b->slant * i, count);
329
330 int y_angle = a->y_angle * (count - i) / count;
331 y_angle += mimimi_div(b->y_angle * i, count);
332
333 int z_angle = a->z_angle * (count - i) / count;
334 z_angle += mimimi_div(b->z_angle * i, count);
335
336 a->slant = slant;
337 a->y_angle = y_angle;
338 a->z_angle = z_angle;
339}
340
341static void mimimi_interpolate_pose(struct mimimi_pose *a, struct mimimi_pose *b, int i, int count)
342{
343 if (count == 0) return;
344
345 int x = a->x * (count - i) / count;
346 x += mimimi_div(b->x * i, count);
347
348 int y = a->y * (count - i) / count;
349 y += mimimi_div(b->y * i, count);
350
351 a->x = x;
352 a->y = y;
353
354 for (int j = 0 ; j < a->count ; j++)
355 mimimi_interpolate_pose_layer(a->layers + j, b->layers + j, i, count);
356}
357
358void mimimi_movement(struct mimimi_animation *animation, struct mimimi_model *model, struct mimimi_movement *movement, struct mimimi_allocator *allocator)
359{
360 if (movement->count == 1)
361 {
362 for (int i = 0 ; i < animation->count ; i++)
363 mimimi_pose(animation->images + i, model, movement->poses, allocator);
364 return;
365 }
366
367 for (int i = 0 ; i < animation->count ; i++)
368 {
369 int j = i * (movement->count - 1) / animation->count;
370 int k = i - j * animation->count / (movement->count - 1);
371
372 struct mimimi_pose pose = movement->poses[j];
373 mimimi_interpolate_pose(&pose, movement->poses + j + 1, k, animation->count / (movement->count - 1));
374 mimimi_pose(animation->images + i, model, &pose, allocator);
375 }
376}
377
378static void mimimi_appearance_animations(struct mimimi_animation_set *animations, struct mimimi_model *model, int x, int y, int coefficient, struct mimimi_allocator *allocator)
379{
380 int animation_count = animations->standing_animation_count;
381 for (int i = 0 ; i < animation_count ; i++)
382 {
383 struct mimimi_pose_layer layers[3][7] = {0};
384
385 int slant = -32 * i / animation_count * coefficient;
386 int y_angle = 64 + (20 + 20 * i / animation_count) * coefficient;
387 int arm_slant = 64 * i / animation_count;
388
389 layers[0][0].y_angle = y_angle;
390 layers[0][0].slant = +slant;
391 layers[0][1].slant = -slant;
392 layers[0][2].slant = -arm_slant;
393 layers[0][3].slant = +arm_slant;
394 layers[0][4].slant = +arm_slant;
395 layers[0][5].slant = -arm_slant;
396
397 layers[1][0].y_angle = y_angle;
398 layers[1][0].slant = +slant;
399 layers[1][1].slant = -slant;
400 layers[1][2].slant = +arm_slant;
401 layers[1][3].slant = -arm_slant;
402 layers[1][4].slant = -arm_slant;
403 layers[1][5].slant = +arm_slant;
404
405 layers[2][0].y_angle = y_angle;
406 layers[2][0].slant = +slant;
407 layers[2][1].slant = -slant;
408 layers[2][2].slant = -arm_slant;
409 layers[2][3].slant = +arm_slant;
410 layers[2][4].slant = +arm_slant;
411 layers[2][5].slant = -arm_slant;
412
413 struct mimimi_pose poses[] = {{x, y, 7, layers[0]}, {x, y, 7, layers[1]}, {x, y, 7, layers[2]}};
414 struct mimimi_movement movement = {3, poses};
415 mimimi_movement(animations->standing + i, model, &movement, allocator);
416 }
417
418 struct mimimi_pose_layer falling_layers[7] = {0};
419
420 falling_layers[0].z_angle = coefficient;
421 falling_layers[0].y_angle = 64 + 32 * coefficient;
422 falling_layers[2].slant = -64 * coefficient;
423 falling_layers[2].z_angle = coefficient;
424 falling_layers[3].slant = 64 * coefficient;
425 falling_layers[3].z_angle = coefficient;
426 falling_layers[4].slant = 64 * coefficient;
427 falling_layers[5].slant = 64 * coefficient;
428
429 struct mimimi_pose falling_poses[] = {{x - 12 * coefficient, y - 12, 7, falling_layers}};
430 struct mimimi_movement falling_movement = {1, falling_poses};
431 mimimi_movement(animations->falling, model, &falling_movement, allocator);
432
433 struct mimimi_pose_layer knocked_layers[1] = {0};
434
435 knocked_layers[0].z_angle = -coefficient;
436 knocked_layers[0].y_angle = 64 + 32 * coefficient;
437
438 struct mimimi_pose knocked_poses[] = {{x - 12 * coefficient, y - 20, 1, falling_layers}};
439 struct mimimi_movement knocked_movement = {1, knocked_poses};
440 mimimi_movement(animations->knocked, model, &knocked_movement, allocator);
441
442 int image_count = animations->jumping_image_count;
443 for (int i = 0 ; i < image_count ; i++)
444 {
445 struct mimimi_pose_layer layers[2] = {0};
446
447 int slant = -32 * i / image_count * coefficient;
448 int y_angle = 64 + (20 + 20 * i / image_count) * coefficient;
449
450 layers[0].y_angle = y_angle;
451 layers[0].slant = +slant;
452 layers[1].slant = -slant;
453
454 struct mimimi_pose pose = {x, y, 2, layers};
455 mimimi_pose(animations->jumping + i, model, &pose, allocator);
456 }
457}
458
459void mimimi_appearance(struct mimimi_appearance *appearance, struct mimimi_model *model, int x, int y, struct mimimi_allocator *allocator)
460{
461 mimimi_appearance_animations(&appearance->left, model, x, y, 1, allocator);
462 mimimi_appearance_animations(&appearance->right, model, x, y, -1, allocator);
463}