DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
gameutil.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 25
4
5#include "gameutil.hpp"
6
7#include <eekernel.h>
8#include <libgraph.h>
9#include <libpkt.h>
10#include <libvu0.h>
11#include <sifdma.h>
12#include <sifrpc.h>
13
14#include <cmath>
15#include <cstdio>
16#include <cstring>
17
18#include "camera.hpp"
19#include "character.hpp"
20#include "dataalloc.hpp"
21#include "frame.hpp"
22#include "framevu1.hpp"
23#include "mathutil.hpp"
24#include "mdt.hpp"
25#include "mglib.hpp"
26#include "rect.hpp"
27#include "texture.hpp"
28#include "visualvu1.hpp"
29
30/* Shared helpers: the IOP midi bridge, motion interpolation, collision and
31 * ground queries, and 2D sprite setup. */
32
33static s32 sbuff[16]; // RPC argument and response storage shared by MIDI calls.
34static sceSifClientData gCd; // Client state bound to the EZMIDI IOP server.
35static sceSifDmaData transData; // Descriptor reused for synchronous EE-to-IOP transfers.
36
37int ezMidiInit() {
38 s32 wait;
39
40 sceSifInitRpc(0);
41
42 while (1) {
43 if (sceSifBindRpc(&gCd, 0x12346, 0) < 0) {
44 printf("error: sceSifBindRpc \n");
45
46 for (;;) {
47 }
48 }
49
50 wait = 10000;
51
52 while (wait--) {
53 }
54
55 if (gCd.server != 0) {
56 break;
57 }
58 }
59
60 return 1;
61}
62
63int ezMidi(int command, int argument) {
64 s32 wait;
65 s32 receive_size;
66
67 receive_size = 0;
68
69 for (wait = 0; wait < 2000; wait++) {
70 }
71
72 if ((command & 0x8000) != 0) {
73 receive_size = 64;
74 }
75
76 if ((command & 0x1000) != 0) {
77 sceSifCallRpc(&gCd, command, 0, (void *) argument, 64, (void *) sbuff, receive_size, 0, 0);
78 } else {
79 sbuff[0] = argument;
80 sceSifCallRpc(&gCd, command, 0, (void *) sbuff, 16, (void *) sbuff, receive_size, 0, 0);
81 }
82
83 return sbuff[0];
84}
85
86int ezTransToIOP(void *iop_address, void *ee_address, int size) {
87 s32 id;
88
89 transData.data = ee_address;
90 transData.addr = iop_address;
91 transData.size = size;
92 transData.mode = 0;
93
94 FlushCache(0);
95 id = sceSifSetDma(&transData, 1);
96
97 if (id == 0) {
98 return -1;
99 }
100
101 while (sceSifDmaStat(id) >= 0) {
102 }
103
104 return 0;
105}
106
114static void QuatSlerp(float *from, float *to, float t, float *out) {
115 float to_x;
116 float to_y;
117 float to_z;
118 float to_w;
119 float cosine;
120 float angle;
121 float inv_sine;
122 float scale_from;
123 float scale_to;
124
125 from[0] = -from[0];
126 to[0] = -to[0];
127 cosine = from[1] * to[1] + from[2] * to[2] + from[3] * to[3] + from[0] * to[0];
128
129 if (cosine < 0.0f) {
130 cosine = -cosine;
131 to_x = -to[1];
132 to_y = -to[2];
133 to_z = -to[3];
134 to_w = -to[0];
135 } else {
136 to_x = to[1];
137 to_y = to[2];
138 to_z = to[3];
139 to_w = to[0];
140 }
141
142 if (1.0f - cosine > 0.001f) {
143 angle = acosf(cosine);
144 inv_sine = 1.0f / sinf(angle);
145 scale_from = inv_sine * sinf((1.0f - t) * angle);
146 scale_to = inv_sine * sinf(t * angle);
147 } else {
148 scale_from = 1.0f - t;
149 scale_to = t;
150 }
151
152 out[1] = scale_from * from[1] + scale_to * to_x;
153 out[2] = scale_from * from[2] + scale_to * to_y;
154 out[3] = scale_from * from[3] + scale_to * to_z;
155 out[0] = scale_from * from[0] + scale_to * to_w;
156}
157
161static inline int SearchMotKey(Mot_List *list, u_int frame) {
162 int middle;
163 int low = 0;
164 int high = list->key_count;
165
166 while (low < high) {
167 middle = (low + high) >> 1;
168
169 if (list->keys[middle].frame <= frame) {
170 low = middle + 1;
171 } else {
172 high = middle;
173 }
174 }
175
176 return low - 1;
177}
178
179Mot_List *MotionProc(CFrame *frame, MOTION_STATE *state, Mot_List *list) {
180 int key;
181 int next;
182 float t;
183
184 key = SearchMotKey(list, state->frame);
185
186 if (state->next_frame == state->frame + 1) {
187 u_int time;
188 u_int next_time;
189
190 next = key + 1;
191 next_time = list->keys[next].frame;
192 time = list->keys[key].frame;
193
194 if ((next_time != time + 1) != 0) {
195 t = (state->time - (float) time) / ((float) next_time - (float) time);
196 } else {
197 t = state->blend;
198 }
199 } else {
200 if (state->next_frame == list->keys[state->next_frame - 1].frame) {
201 next = state->next_frame - 1;
202 } else {
203 int found = SearchMotKey(list, state->next_frame);
204
205 next = found;
206 }
207
208 t = state->blend;
209 }
210
211 if (!(t < 0.0f) && t <= 1.0f && key < list->key_count && next < list->key_count) {
212 float one_minus_t;
213 sceVu0FVECTOR value;
214 sceVu0FVECTOR rotation;
215 sceVu0FVECTOR from;
216 sceVu0FVECTOR to;
217 CFrameVu1 *target = &((CFrameVu1 *) frame)[list->frame];
218
219 switch (list->type) {
221 sceVu0CopyVector(from, list->keys[key].value);
222 sceVu0CopyVector(to, list->keys[next].value);
223
224 if (!(t <= 0.0001f) && t < 0.9999f) {
225 QuatSlerp(from, to, t, rotation);
226 target->SetTransMatrix(rotation);
227 } else {
228 from[0] = -from[0];
229 to[0] = -to[0];
230
231 if (t <= 0.0001f) {
232 target->SetTransMatrix(from);
233 }
234
235 if (!(t < 0.9999f)) {
236 target->SetTransMatrix(to);
237 }
238 }
239
240 break;
241 case MOTION_KEY_SCALE:
242 if (!(t <= 0.0001f) && t < 0.9999f) {
243 sceVu0InterVectorXYZ(value, list->keys[next].value, list->keys[key].value, t);
244 } else {
245 if (t <= 0.0001f) {
246 sceVu0CopyVectorXYZ(value, list->keys[key].value);
247 }
248
249 if (!(t < 0.9999f)) {
250 sceVu0CopyVectorXYZ(value, list->keys[next].value);
251 }
252 }
253
254 target->SetScale(value[0], value[1], value[2]);
255 break;
257 if (!(t <= 0.0001f) && t < 0.9999f) {
258 sceVu0InterVectorXYZ(value, list->keys[next].value, list->keys[key].value, t);
259 } else {
260 if (t <= 0.0001f) {
261 sceVu0CopyVectorXYZ(value, list->keys[key].value);
262 }
263
264 if (!(t < 0.9999f)) {
265 sceVu0CopyVectorXYZ(value, list->keys[next].value);
266 }
267 }
268
269 target->local[3][0] = value[0];
270 target->local[3][1] = value[1];
271 target->local[3][2] = value[2];
272 target->world_valid = false;
273 break;
274 case MOTION_KEY_VERTEX: {
275 int vertex;
276 MDT_HEADER *model = (MDT_HEADER *) target->GetVisual()->GetMDTDataAddress();
277 sceVu0FVECTOR *vertices = (sceVu0FVECTOR *) ((u_char *) model + model->vertex_ofs);
278 u_int frame_no;
279
280 target->attr.remake_pending = 1;
281 frame_no = list->frame;
282
283 if (!(t <= 0.0001f) && t < 0.9999f) {
284 while (frame_no == list->frame) {
285 vertex = list->target - 1;
286 sceVu0InterVectorXYZ(value, list->keys[next].value, list->keys[key].value, t);
287 sceVu0CopyVectorXYZ(vertices[vertex], value);
288 list = list->next;
289
290 switch ((int) list) {
291 case 0:
292 return NULL;
293 }
294 }
295 } else {
296 if (t <= 0.0001f) {
297 while (frame_no == list->frame) {
298 vertex = list->target - 1;
299 sceVu0CopyVectorXYZ(vertices[vertex], list->keys[key].value);
300 list = list->next;
301
302 switch ((int) list) {
303 case 0:
304 return NULL;
305 }
306 }
307 }
308
309 if (!(t < 0.9999f)) {
310 while (frame_no == list->frame) {
311 vertex = list->target - 1;
312 sceVu0CopyVectorXYZ(vertices[vertex], list->keys[next].value);
313 list = list->next;
314
315 switch ((int) list) {
316 case 0:
317 return NULL;
318 }
319 }
320 }
321 }
322
323 return list;
324 }
326 if (state->camera != NULL) {
327 sceVu0InterVectorXYZ(value, list->keys[next].value, list->keys[key].value, t);
328 frame->GetWorldPosition(value, value);
329 state->camera->SetPos(NULL, value[0], value[1], value[2]);
330 }
331
332 break;
334 if (state->camera != NULL) {
335 sceVu0InterVectorXYZ(value, list->keys[next].value, list->keys[key].value, t);
336 frame->GetWorldPosition(value, value);
337 state->camera->SetRef(NULL, value[0], value[1], value[2]);
338 }
339
340 break;
342 MDT_HEADER *model;
343 MDT_MATERIAL *material;
344
345 one_minus_t = 1.0f - t;
346 model = (MDT_HEADER *) target->GetVisual()->GetMDTDataAddress();
347 material = (MDT_MATERIAL *) ((u_char *) model + model->info_ofs);
348 material[list->target].diffuse[3] = 1.0f - (one_minus_t * list->keys[key].value[0] + t * list->keys[next].value[0]);
349 target->attr.remake_pending = 2;
350 break;
351 }
353 MDT_HEADER *model = (MDT_HEADER *) target->GetVisual()->GetMDTDataAddress();
354 MDT_MATERIAL *material = (MDT_MATERIAL *) ((u_char *) model + model->info_ofs);
355
356 sceVu0InterVectorXYZ(material[list->target].diffuse, list->keys[next].value, list->keys[key].value, t);
357 target->attr.remake_pending = 2;
358 break;
359 }
361 if (state->camera != NULL) {
362 one_minus_t = 1.0f - t;
363 state->camera->SetRoll(-(PI * ((one_minus_t * list->keys[key].value[0] + t * list->keys[next].value[0]) / 180.0f)));
364 }
365
366 break;
368 if (state->camera != NULL) {
369 one_minus_t = 1.0f - t;
370 MGSetProjection(0.5f * (480.0f * (1.0f / tanf(PI * ((0.5f * (one_minus_t * list->keys[key].value[0] + t * list->keys[next].value[0])) / 180.0f)))));
371 }
372
373 break;
375 if (list->keys[key].value[0] < 1.0f) {
376 target->attr.draw_on = 0;
377 } else {
378 target->attr.draw_on = 3;
379 }
380
381 break;
383 if (list->keys[key].value[0] < 1.0f) {
384 target->attr.draw_on = 2;
385 } else {
386 target->attr.draw_on = 1;
387 }
388
389 break;
390 }
391 }
392
393 return list->next;
394}
395
399static sceVu0FVECTOR def_vrtx[3000];
400#ifndef PORT
401Mot_List *MotionProc2(CFrame *frame, tagMOTION_TYPE *motion, tagFRAME_INF *frame_info, Mot_List *list) {
402 static sceVu0FMATRIX Bone_Matrix;
403 static sceVu0FMATRIX Bone_Matrix_inv;
404 static sceVu0FMATRIX Bone_Matrix_Base;
405 static sceVu0FVECTOR *vert;
406 CFrameVu1 *target;
407 u_int i;
408
409 if (list->type == MOTION_KEY_SKIP) {
410 return list->next;
411 }
412
413 target = &((CFrameVu1 *) frame)[list->target];
414
415 if (list->target == frame_info[list->frame].parent_frame) {
416 CFrameVu1 *owner = &((CFrameVu1 *) frame)[list->frame];
417 MDT_HEADER *model = (MDT_HEADER *) owner->GetVisual()->GetMDTDataAddress();
418
419 vert = (sceVu0FVECTOR *) ((u_char *) model + model->vertex_ofs);
420
421 if (frame_info[list->frame].vertex_count > 3000) {
422 printf("###### MAX_VERTX OVER %d/%d######\n", frame_info[list->frame].vertex_count, 3000);
423 }
424
425 memcpy(def_vrtx, frame_info[list->frame].base_vertices, frame_info[list->frame].vertex_count * sizeof(sceVu0FVECTOR));
426 sceVu0UnitMatrix(Bone_Matrix);
427 sceVu0UnitMatrix(Bone_Matrix_Base);
428 sceVu0InversMatrix(Bone_Matrix_inv, frame_info[list->frame].matrix);
429 owner->attr.remake_pending = 1;
430 sceVu0UnitMatrix(frame_info[list->target].bone_base_matrix);
431 sceVu0UnitMatrix(frame_info[list->target].bone_matrix);
432 } else {
433 sceVu0FMATRIX local;
434 sceVu0FVECTOR translation;
435
436 sceVu0CopyMatrix(local, target->local);
437 MulMatrix(Bone_Matrix, frame_info[frame_info[list->target].parent_frame].bone_matrix, local);
438 MulMatrix(Bone_Matrix_Base, frame_info[frame_info[list->target].parent_frame].bone_base_matrix, motion->base_matrices[list->target]);
439 sceVu0CopyMatrix(frame_info[list->target].bone_base_matrix, Bone_Matrix_Base);
440 sceVu0CopyMatrix(frame_info[list->target].bone_matrix, Bone_Matrix);
441 sceVu0CopyVector(translation, Bone_Matrix_Base[3]);
442 sceVu0InversMatrix(Bone_Matrix_Base, Bone_Matrix_Base);
443 sceVu0CopyVector(Bone_Matrix_Base[3], translation);
444 }
445
446 for (i = 0; i < list->key_count; i++) {
447 sceVu0FVECTOR weight;
448 register float *weight_ptr;
449 register float *bone;
450 register float *base;
451 register float *inverse;
452 register float *source;
453 register float *deformed;
454 register float *out;
455
456 weight[0] = 0.01f * list->keys[i].value[0];
457 out = vert[list->keys[i].frame];
458 source = frame_info[list->frame].base_vertices[list->keys[i].frame];
459 base = (float *) Bone_Matrix_Base;
460 bone = (float *) Bone_Matrix;
461 deformed = def_vrtx[list->keys[i].frame];
462 weight_ptr = weight;
463 inverse = (float *) Bone_Matrix_inv;
464 // Move the vertex toward the bone's transform by its weight, then map it back into the
465 // skinned frame's space.
466 asm {
467 lqc2 vf4, 0x30(base)
468 lqc2 vf10, 0(source)
469 lqc2 vf1, 0(base)
470 lqc2 vf2, 0x10(base)
471 lqc2 vf3, 0x20(base)
472 vsub.xyz vf15, vf10, vf4
473 vaddx.w vf15, vf0, vf0x
474 lqc2 vf5, 0(bone)
475 lqc2 vf6, 0x10(bone)
476 lqc2 vf7, 0x20(bone)
477 lqc2 vf8, 0x30(bone)
478 vmulax.xyzw ACC, vf1, vf15x
479 vmadday.xyzw ACC, vf2, vf15y
480 vmaddaz.xyzw ACC, vf3, vf15z
481 vmaddaw.xyzw ACC, vf4, vf15w
482 vmsubw.xyz vf15, vf4, vf0w
483 vaddx.w vf15, vf0, vf0x
484 lqc2 vf9, 0(weight_ptr)
485 lqc2 vf11, 0(deformed)
486 vmulax.xyzw ACC, vf5, vf15x
487 vmadday.xyzw ACC, vf6, vf15y
488 vmaddaz.xyzw ACC, vf7, vf15z
489 vmaddaw.xyzw ACC, vf8, vf15w
490 vmsubw.xyz vf15, vf10, vf0w
491 vmulaw.xyzw ACC, vf11, vf0w
492 lqc2 vf20, 0(inverse)
493 lqc2 vf21, 0x10(inverse)
494 lqc2 vf22, 0x20(inverse)
495 lqc2 vf23, 0x30(inverse)
496 vmaddx.xyz vf11, vf15, vf9x
497 sqc2 vf11, 0(deformed)
498 vmulax.xyzw ACC, vf20, vf11x
499 vmadday.xyzw ACC, vf21, vf11y
500 vmaddaz.xyzw ACC, vf22, vf11z
501 vmaddw.xyzw vf16, vf23, vf11w
502 sqc2 vf16, 0(out)
503 }
504 }
505
506 return list->next;
507}
508#endif
509void SetMotionEX(CFrame *frame, tagMOTION_TYPE *motion, MOTION_INFO *info, MOTION_STATE *state, tagFRAME_INF *frame_info) {
510 Mot_List *list;
511 Mot_List *list2;
512
513 if (state->playing_no != state->motion_no || state->blending != 0) {
514 if (state->blending == 0) {
515 state->blend = 0.0f;
516 }
517
518 state->blending = true;
519 state->frame = state->time;
520 state->blend += state->blend_step;
521 state->playing_no = state->motion_no;
522
523 if (!(state->blend < 1.0f)) {
524 state->time = state->next_frame;
525 state->frame = state->next_frame;
526 state->blend = 0.0f;
527 state->blending = false;
528 }
529
530 for (list = motion->proc_list; list != NULL;) {
531 list = MotionProc(frame, state, list);
532 }
533
534 if (state->look_at != 0) {
535 if (state->look_target != NULL) {
536 LookAt(state->look_frame, state->look_target, state->look_constraint);
537 } else {
538 LookAt(state->look_frame, state->look_position, state->look_constraint);
539 }
540 }
541
542 for (list = motion->proc_list2; list != NULL;) {
543 list = MotionProc2(frame, motion, frame_info, list);
544 }
545
546 return;
547 }
548
549 state->playing_no = state->motion_no;
550 state->frame = state->time;
551 state->next_frame = state->frame + 1;
552 state->blend = state->time - state->frame;
553
554 for (list2 = motion->proc_list; list2 != NULL;) {
555 list2 = MotionProc(frame, state, list2);
556 }
557
558 if (state->look_at != 0) {
559 if (state->look_target != NULL) {
560 LookAt(state->look_frame, state->look_target, state->look_constraint);
561 } else {
562 LookAt(state->look_frame, state->look_position, state->look_constraint);
563 }
564 }
565
566 for (list2 = motion->proc_list2; list2 != NULL;) {
567 list2 = MotionProc2(frame, motion, frame_info, list2);
568 }
569
570 state->time += info[state->motion_no].speed;
571
572 if ((unsigned int) state->time >= info[state->motion_no].end) {
573 state->time -= (float) info[state->motion_no].end - (float) info[state->motion_no].start;
574 state->frame = info[state->motion_no].start;
575 state->next_frame = state->frame + 1;
576 state->blend = state->time - state->frame;
577 }
578}
579
587int CreateAnimeDataEX(tagMOTION_TYPE *motion, CDataAlloc2<1> *arena, MOTION_FILE_INFO *files) {
588 if (files[0].name != NULL) {
589 motion->base_matrices = (sceVu0FMATRIX *) arena->Alloc((files[0].size >> 4) + 1);
590 memcpy(motion->base_matrices, files[0].data, files[0].size);
591 }
592
593 if (files[1].name != NULL) {
594 Mot_List *list;
595 Mot_File_List *record;
596 u_char *data;
597 Mot_List *reversed;
598 Mot_List *previous;
599 Mot_List *node;
600 u_char *keys;
601
602 data = (u_char *) files[1].data;
603 motion->proc_list = NULL;
604
605 do {
606 record = (Mot_File_List *) data;
607 list = (Mot_List *) arena->Alloc(sizeof(Mot_List) / 16 + 1);
608 list->frame = record->frame;
609 list->target = record->target;
610 list->key_count = record->key_count;
611 list->type = record->type;
612 list->keys = (Mot_Key *) arena->Alloc(list->key_count * sizeof(Mot_Key) / 16 + 1);
613 data += sizeof(Mot_File_List);
614 keys = data;
615 data += list->key_count * sizeof(Mot_Key);
616 memcpy(list->keys, keys, list->key_count * sizeof(Mot_Key));
617
618 if (motion->proc_list == NULL) {
619 list->next = NULL;
620 } else {
621 list->next = motion->proc_list;
622 }
623
624 motion->proc_list = list;
625 } while (record->more != 0L);
626
627 reversed = NULL;
628
629 while ((node = motion->proc_list) != NULL) {
630 previous = reversed;
631 reversed = node;
632 motion->proc_list = node->next;
633 node->next = previous;
634 }
635
636 motion->proc_list = reversed;
637 }
638
639 if (files[2].name != NULL) {
640 Mot_List *list;
641 Mot_File_List *record;
642 u_char *data;
643 Mot_List *reversed;
644 Mot_List *previous;
645 Mot_List *node;
646 u_char *keys;
647
648 data = (u_char *) files[2].data;
649 motion->proc_list2 = NULL;
650
651 do {
652 record = (Mot_File_List *) data;
653 list = (Mot_List *) arena->Alloc(sizeof(Mot_List) / 16 + 1);
654 list->frame = record->frame;
655 list->target = record->target;
656 list->key_count = record->key_count;
657 list->type = record->type;
658 list->keys = (Mot_Key *) arena->Alloc(list->key_count * sizeof(Mot_Key) / 16 + 1);
659 data += sizeof(Mot_File_List);
660 keys = data;
661 data += list->key_count * sizeof(Mot_Key);
662 memcpy(list->keys, keys, list->key_count * sizeof(Mot_Key));
663
664 if (motion->proc_list2 == NULL) {
665 list->next = NULL;
666 } else {
667 list->next = motion->proc_list2;
668 }
669
670 motion->proc_list2 = list;
671 } while (record->more != 0L);
672
673 reversed = NULL;
674
675 while ((node = motion->proc_list2) != NULL) {
676 previous = reversed;
677 reversed = node;
678 motion->proc_list2 = node->next;
679 node->next = previous;
680 }
681
682 motion->proc_list2 = reversed;
683 }
684
685 return 1;
686}
687
695void AnimeDataInit(CFrame *frame, tagMOTION_TYPE *motion, CDataAlloc2<1> *arena, tagFRAME_INF **frame_info) {
696 *frame_info = (tagFRAME_INF *) arena->Alloc64((frame->GetFrameNum() + 10) * sizeof(tagFRAME_INF) / 16 + 1);
697 AnimeDataInit(frame, motion, arena, *frame_info);
698}
699
707int AnimeDataInit(CFrame *frame, tagMOTION_TYPE *motion, CDataAlloc2<1> *arena, tagFRAME_INF *frame_info) {
708 CFrameVu1 *current;
709 u32 target;
710 CFrame *parent;
711 Mot_List *list = motion->proc_list2;
712 u32 last = -1;
713 int i;
714 int count = frame->GetFrameNum();
715
716 for (i = 0; i < count; i++) {
717 current = &((CFrameVu1 *) frame)[i];
718 tagFRAME_INF *info = &frame_info[i];
719 sceVu0CopyMatrix(info->matrix, current->local);
720 parent = current->parent;
721 info->parent_frame = (CFrameVu1 *) parent - (CFrameVu1 *) frame;
722 }
723
724 for (; list != NULL; list = list->next) {
725 if (list->type == MOTION_KEY_SKIP) {
726 continue;
727 }
728
729 // Retail searches for each parent's index and never uses it.
730 target = list->target;
731 parent = ((CFrameVu1 *) frame)[target].parent;
732
733 for (i = 0; i < target; i++) {
734 if (parent == &((CFrameVu1 *) frame)[i]) {
735 break;
736 }
737 }
738
739 if (last == list->frame) {
740 continue;
741 }
742
743 current = &((CFrameVu1 *) frame)[list->frame];
744 parent = current->parent;
745
746 for (i = 0; i < list->frame; i++) {
747 if (parent == &((CFrameVu1 *) frame)[i]) {
748 break;
749 }
750 }
751
752 CVisualVu1 *visual = current->GetVisual();
753
754 if (visual != NULL) {
755 sceVu0FVECTOR *vertices;
756 MDT_HEADER *model = (MDT_HEADER *) visual->GetMDTDataAddress();
757 vertices = (sceVu0FVECTOR *) ((u_char *) model + model->vertex_ofs);
758 sceVu0FMATRIX matrix;
759
760 frame_info[list->frame].base_vertices = (sceVu0FVECTOR *) arena->Alloc(model->vertex_num * sizeof(sceVu0FVECTOR) / 16 + 1);
761 frame_info[list->frame].vertex_count = model->vertex_num;
762 sceVu0UnitMatrix(matrix);
763 sceVu0CopyMatrix(matrix, frame_info[list->frame].matrix);
764
765 for (i = 0; i < (u32) model->vertex_num; i++) {
766 sceVu0ApplyMatrix(frame_info[list->frame].base_vertices[i], matrix, vertices[i]);
767 }
768 }
769
770 last = list->frame;
771 }
772
773 return 1;
774}
775
776int NextMotionTime_GET_EX(MOTION_INFO *info, MOTION_STATE *state) {
777 int playing_start = info[state->playing_no].start;
778 float progress = (state->time - playing_start) / (info[state->playing_no].end - playing_start);
779 int start = info[state->motion_no].start;
780 int end = info[state->motion_no].end;
781 int time = start + progress * (end - start);
782
783 if (end < time) {
784 time = end;
785 }
786
787 if (time < start) {
788 time = start;
789 }
790
791 return time;
792}
793
797static void SetRotationMatrixFromDir(sceVu0FMATRIX matrix, float *direction, float angle) {
798 float cosine = cosf(angle);
799 float sine = sinf(angle);
800 float inverse_cosine = 1.0f - cosine;
801 sceVu0FVECTOR axis;
802
803 sceVu0UnitMatrix(matrix);
804 sceVu0Normalize(axis, direction);
805 matrix[0][0] = cosine + inverse_cosine * (axis[0] * axis[0]);
806 matrix[0][1] = inverse_cosine * (axis[0] * axis[1]) - axis[2] * sine;
807 matrix[0][2] = inverse_cosine * (axis[0] * axis[2]) + axis[1] * sine;
808 matrix[1][0] = inverse_cosine * (axis[1] * axis[0]) + axis[2] * sine;
809 matrix[1][1] = cosine + inverse_cosine * (axis[1] * axis[1]);
810 matrix[1][2] = inverse_cosine * (axis[1] * axis[2]) - axis[0] * sine;
811 matrix[2][0] = inverse_cosine * (axis[2] * axis[0]) - axis[1] * sine;
812 matrix[2][1] = inverse_cosine * (axis[2] * axis[1]) + axis[0] * sine;
813 matrix[2][2] = cosine + inverse_cosine * (axis[2] * axis[2]);
814}
815
816int LookAt(CFrameVu1 *frame, float *target, _FRAMECONSTRAINT constraint) {
817 sceVu0FVECTOR up;
818 sceVu0FVECTOR local;
819 sceVu0FVECTOR axis;
820 sceVu0FVECTOR direction;
821 sceVu0FVECTOR cross;
822 sceVu0FVECTOR side;
823 sceVu0FVECTOR plane;
824 sceVu0FVECTOR delta;
825 sceVu0FMATRIX parent;
826 sceVu0FMATRIX matrix;
827 sceVu0FMATRIX rotation;
828 float angle;
829
830 sceVu0UnitMatrix(parent);
831 frame->parent->GetLWMatrix(parent);
832 sceVu0UnitMatrix(matrix);
833 sceVu0CopyMatrix(matrix, frame->local);
834
835 if (constraint == FRAME_CONSTRAINT_X) {
836 sceVu0Normalize(up, matrix[0]);
837 }
838
839 if (constraint == FRAME_CONSTRAINT_Y) {
840 sceVu0Normalize(up, matrix[1]);
841 }
842
843 if (constraint == FRAME_CONSTRAINT_Z) {
844 sceVu0Normalize(up, matrix[2]);
845 }
846
847 up[3] = 1.0f;
848 delta[3] = 1.0f;
849 sceVu0SubVector(delta, target, parent[3]);
850 local[0] = sceVu0InnerProduct(delta, parent[0]);
851 local[1] = sceVu0InnerProduct(delta, parent[1]);
852 local[2] = sceVu0InnerProduct(delta, parent[2]);
853 local[3] = 1.0f;
854
855 if (DistVector(local) > 0.0000001f) {
856 sceVu0Normalize(direction, local);
857 direction[3] = 1.0f;
858 sceVu0Normalize(axis, up);
859 axis[3] = 1.0f;
860 sceVu0OuterProduct(cross, axis, direction);
861 sceVu0Normalize(side, cross);
862 side[3] = 1.0f;
863 sceVu0OuterProduct(cross, side, axis);
864 sceVu0Normalize(plane, cross);
865 plane[3] = 1.0f;
866 angle = sceVu0InnerProduct(direction, plane);
867
868 if (angle < 0.99999f) {
869 sceVu0UnitMatrix(rotation);
870 angle = acosf(angle);
871
872 if (angle > QUARTER_PI) {
873 angle = QUARTER_PI;
874 }
875
876 if (acosf(sceVu0InnerProduct(axis, direction)) > HALF_PI) {
877 SetRotationMatrixFromDir(rotation, side, -angle);
878 } else {
879 SetRotationMatrixFromDir(rotation, side, angle);
880 }
881
882 MulMatrix(matrix, rotation, matrix);
883 frame->SetTransMatrix(matrix);
884 }
885 }
886
887 return 0;
888}
889
890int LookAt(CFrameVu1 *frame, CFrameVu1 *target, _FRAMECONSTRAINT constraint) {
891 sceVu0FMATRIX matrix;
892
893 target->GetLWMatrix(matrix);
894 return LookAt(frame, matrix[3], constraint);
895}
896#ifndef PORT
897/* Loads a box's corners into VU0 registers vf10 and vf11 for the box tests that follow. */
898static inline void vu_hold_box(float *max, float *min) {
899 register float *p0 = max;
900 register float *p1 = min;
901
902 asm {
903 lqc2 vf10, 0(p0)
904 lqc2 vf11, 0(p1)
905 }
906}
907
908/* Whether a triangle's bound misses the box held in VU0: the sign flags of the two subtractions,
909 cleared before them and read back after. */
910static inline int vu_box_missed(float *max, float *min) {
911 register float *p0 = max;
912 register float *p1 = min;
913 register int status;
914
915 asm {
916 lqc2 vf12, 0(p0)
917 lqc2 vf13, 0(p1)
918 vnop
919 vnop
920 vnop
921 ctc2 $0, $vi16
922 vsub.xyz vf25, vf10, vf13
923 vsub.xyz vf25, vf12, vf11
924 vnop
925 vnop
926 vnop
927 vnop
928 vnop
929 cfc2 status, $vi16
930 }
931
932 return status & 0xc0;
933}
934#endif
942int PickUpNearPoly(CCPoly *out, CBoxVu0 box, CCPoly *poly, int count) {
943 sceVu0FVECTOR poly_max;
944 sceVu0FVECTOR poly_min;
945 int picked = 0;
946 int i;
947 CCPoly *src;
948 CCPoly *dst;
949
950 vu_hold_box(box.max, box.min);
951 src = poly;
952 dst = out;
953
954 for (i = 0; i < count; i++, src++) {
955 VectorMaxMin(poly_max, poly_min, src->vertex[0], src->vertex[1], src->vertex[2]);
956
957 if (vu_box_missed(poly_max, poly_min)) {
958 continue;
959 }
960
961 memcpy(dst, src, sizeof(CCPoly));
962 dst++;
963 picked++;
964 }
965
966 return picked;
967}
968
969int CheckHit(CCPoly *poly, int count, float *from, float *to, float *hit_point, int nearest, int mode) {
970 sceVu0FVECTOR point;
971 sceVu0FVECTOR diff;
972 sceVu0FVECTOR poly_min;
973 sceVu0FVECTOR poly_max;
974 CBoxVu0 line;
975 sceVu0FVECTOR offset;
976 int i;
977 int hit = -1;
978 int found = 0;
979 float best;
980 float from_side;
981 float to_side;
982 float dist;
983
984 VectorMaxMin(line.max, line.min, from, to);
985 vu_hold_box(line.max, line.min);
986
987 for (i = 0; i < count; i++, poly++) {
988 if (poly->attr.ignore_mask & mode) {
989 continue;
990 }
991
992 VectorMaxMin(poly_max, poly_min, poly->vertex[0], poly->vertex[1], poly->vertex[2]);
993
994 if (line.max[0] < poly_min[0] || line.max[1] < poly_min[1] || line.max[2] < poly_min[2]) {
995 continue;
996 }
997
998 if (line.min[0] > poly_max[0] || line.min[1] > poly_max[1] || line.min[2] > poly_max[2]) {
999 continue;
1000 }
1001
1002 sceVu0SubVector(offset, from, poly->vertex[0]);
1003 from_side = sceVu0InnerProduct(poly->normal, offset);
1004 sceVu0SubVector(offset, to, poly->vertex[0]);
1005 to_side = sceVu0InnerProduct(poly->normal, offset);
1006
1007 if (from_side > 0.0f && to_side > 0.0f) {
1008 continue;
1009 }
1010
1011 if (from_side < 0.0f && to_side < 0.0f) {
1012 continue;
1013 }
1014
1015 if (IntersectionPoint_line_poly3(from, to, poly->vertex[0], poly->vertex[1], poly->vertex[2], poly->normal, point) == 0) {
1016 continue;
1017 }
1018
1019 if (nearest == 0) {
1020 hit = i;
1021 sceVu0CopyVector(hit_point, point);
1022 break;
1023 }
1024
1025 diff[0] = from[0] - point[0];
1026 diff[1] = from[1] - point[1];
1027 diff[2] = from[2] - point[2];
1028 dist = diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
1029
1030 if (found == 0) {
1031 hit = i;
1032 best = dist;
1033 sceVu0CopyVector(hit_point, point);
1034 } else if (best > dist) {
1035 hit = i;
1036 best = dist;
1037 sceVu0CopyVector(hit_point, point);
1038 }
1039
1040 found = 1;
1041 }
1042
1043 return hit;
1044}
1045
1046int CheckHitVertical(CCPoly *poly, int count, float *from, float depth, float *hit_point, int mode) {
1047 sceVu0FVECTOR to;
1048 float best_y;
1049 int i;
1050 int best = -1;
1051
1052 to[0] = from[0];
1053 to[1] = from[1] + depth;
1054 to[2] = from[2];
1055
1056 for (i = 0; i < count; i++, poly++) {
1057 if (poly->attr.ignore_mask & mode) {
1058 continue;
1059 }
1060
1061 if (!IntersectionPoint_line_poly3(from, to, poly->vertex[0], poly->vertex[1], poly->vertex[2], poly->normal, hit_point)) {
1062 continue;
1063 }
1064
1065 if (depth <= 0.0f) {
1066 if (from[1] > hit_point[1] && (best < 0 || (best >= 0 && best_y <= hit_point[1]))) {
1067 best_y = hit_point[1];
1068 best = i;
1069 }
1070 } else {
1071 if (from[1] < hit_point[1] && (best < 0 || (best >= 0 && !(best_y < hit_point[1])))) {
1072 best_y = hit_point[1];
1073 best = i;
1074 }
1075 }
1076 }
1077
1078 if (best >= 0) {
1079 hit_point[1] = best_y;
1080 }
1081
1082 return best;
1083}
1084
1085int CheckHits(CCPoly *poly, int count, float *from, float *to, int max, int *hit_poly, float (*hit_point)[4], int sort, int mode) {
1086 sceVu0FVECTOR point;
1087 sceVu0FVECTOR poly_min;
1088 sceVu0FVECTOR poly_max;
1089 CBoxVu0 line;
1090 sceVu0FVECTOR offset;
1091 sceVu0FVECTOR swap;
1092 int i;
1093 int j;
1094 int hits;
1095 float from_side;
1096 float to_side;
1097
1098 hits = 0;
1099 VectorMaxMin(line.max, line.min, from, to);
1100 vu_hold_box(line.max, line.min);
1101
1102 for (i = 0; i < count; i++, poly++) {
1103 if (poly->attr.ignore_mask & mode) {
1104 continue;
1105 }
1106
1107 VectorMaxMin(poly_max, poly_min, poly->vertex[0], poly->vertex[1], poly->vertex[2]);
1108
1109 if (line.max[0] < poly_min[0] || line.max[1] < poly_min[1] || line.max[2] < poly_min[2]) {
1110 continue;
1111 }
1112
1113 if (line.min[0] > poly_max[0] || line.min[1] > poly_max[1] || line.min[2] > poly_max[2]) {
1114 continue;
1115 }
1116
1117 sceVu0SubVector(offset, from, poly->vertex[0]);
1118 from_side = sceVu0InnerProduct(poly->normal, offset);
1119 sceVu0SubVector(offset, to, poly->vertex[0]);
1120 to_side = sceVu0InnerProduct(poly->normal, offset);
1121
1122 if (from_side > 0.0f && to_side > 0.0f) {
1123 continue;
1124 }
1125
1126 if (from_side < 0.0f && to_side < 0.0f) {
1127 continue;
1128 }
1129
1130 if (IntersectionPoint_line_poly3(from, to, poly->vertex[0], poly->vertex[1], poly->vertex[2], poly->normal, point) == 0) {
1131 continue;
1132 }
1133
1134 if (hits >= max) {
1135 break;
1136 }
1137
1138 hit_poly[hits] = i;
1139 sceVu0CopyVector(hit_point[hits], point);
1140 hit_point[hits][3] = DistVector(from, point);
1141 hits++;
1142 }
1143
1144 if (sort == 0) {
1145 return hits;
1146 }
1147
1148 if (sort > 0) {
1149 for (i = 0; i < hits - 1; i++) {
1150 for (j = i + 1; j < hits; j++) {
1151 if (hit_point[i][3] > hit_point[j][3]) {
1152 int index = hit_poly[i];
1153 hit_poly[i] = hit_poly[j];
1154 hit_poly[j] = index;
1155 sceVu0CopyVector(swap, hit_point[i]);
1156 sceVu0CopyVector(hit_point[i], hit_point[j]);
1157 sceVu0CopyVector(hit_point[j], swap);
1158 }
1159 }
1160 }
1161 }
1162
1163 if (sort < 0) {
1164 for (i = 0; i < hits - 1; i++) {
1165 for (j = i + 1; j < hits; j++) {
1166 if (hit_point[i][3] > hit_point[j][3]) {
1167 int index = hit_poly[i];
1168 hit_poly[i] = hit_poly[j];
1169 hit_poly[j] = index;
1170 sceVu0CopyVector(swap, hit_point[i]);
1171 sceVu0CopyVector(hit_point[i], hit_point[j]);
1172 sceVu0CopyVector(hit_point[j], swap);
1173 }
1174 }
1175 }
1176 }
1177
1178 return hits;
1179}
1180
1181int MoveCheck(float *pos, float *velocity, float *out_pos, MoveCheckInfo *out_info, CCPoly *polys, int poly_num, int mode) {
1182 int hit_wall;
1183 CCPoly *wall;
1184 int poly_no;
1185 int i;
1186 float drop;
1187 sceVu0FVECTOR hit;
1188 sceVu0FVECTOR from;
1189 sceVu0FVECTOR to;
1190 CCPoly poly;
1191 sceVu0FVECTOR probe;
1192 sceVu0FVECTOR centre;
1193
1194 out_pos[0] = pos[0];
1195 out_pos[1] = pos[1];
1196 out_pos[2] = pos[2];
1197 from[0] = pos[0] + velocity[0];
1198 from[1] = 17.0f + pos[1];
1199 // The probe steps along Z by the X velocity; retail uses velocity[0] for both horizontal coordinates.
1200 from[2] = pos[2] + velocity[0];
1201 to[0] = from[0];
1202 to[1] = 4.0f + pos[1] + velocity[1];
1203 to[2] = from[2];
1204
1205 if (CheckHit(polys, poly_num, from, to, hit, 0, mode) >= 0) {
1206 velocity[2] = 0.0f;
1207 velocity[0] = 0.0f;
1208 }
1209
1210 from[0] = pos[0];
1211 from[1] = 4.0f + pos[1];
1212 from[2] = pos[2];
1213 to[0] = from[0] + velocity[0];
1214 to[1] = from[1] + velocity[1];
1215 to[2] = from[2] + velocity[2];
1216 poly_no = CheckHit(polys, poly_num, from, to, hit, 0, mode);
1217 hit_wall = poly_no >= 0;
1218
1219 if (hit_wall == 0) {
1220 from[0] = to[0];
1221 from[1] = to[1];
1222 from[2] = to[2];
1223 to[0] = from[0];
1224 to[1] = from[1] - 4.0f;
1225 to[2] = from[2];
1226 } else {
1227 wall = &polys[poly_no];
1228 sceVu0Normalize(wall->normal, wall->normal);
1229
1230 if (wall->normal[1] < 0.5f && !(wall->normal[1] <= -0.5f)) {
1231 to[0] = pos[0];
1232 to[1] = pos[1];
1233 to[2] = pos[2];
1234 } else {
1235 from[0] = hit[0];
1236 from[1] = 4.0f + hit[1];
1237 from[2] = hit[2];
1238 to[0] = from[0];
1239 to[1] = from[1] - 4.0f;
1240 to[2] = from[2];
1241 }
1242 }
1243
1244 out_info->ground_found = 0;
1245 out_info->landed = 0;
1246 drop = 2.0f;
1247
1248 if (!(velocity[1] <= 0.1f)) {
1249 drop = 0.0f;
1250 }
1251
1252 sceVu0CopyVector(probe, from);
1253
1254 for (i = 0; i < 2; i++) {
1255 if (GetFootPoly(probe, 15.0f, &poly, hit, polys, poly_num, mode) != 0) {
1256 sceVu0Normalize(poly.normal, poly.normal);
1257 out_info->ground_poly = poly;
1258 out_info->poly = poly;
1259 out_info->ground_found = 1;
1260 *(u_long128 *) out_info->ground_point = *(u_long128 *) hit;
1261
1262 if (!(hit[1] <= from[1] + velocity[1] - 4.0f - drop)) {
1263 out_info->landed = 1;
1264
1265 if (poly.normal[1] < 0.5f && !(poly.normal[1] <= -0.5f)) {
1266 i = 2;
1267 }
1268 } else {
1269 out_info->landed = 0;
1270 }
1271
1272 break;
1273 }
1274
1275 probe[0] += 0.01f;
1276 probe[2] += 0.001f;
1277 }
1278
1279 if (out_info->landed != 0) {
1280 out_pos[0] = hit[0];
1281 out_pos[1] = hit[1];
1282 out_pos[2] = hit[2];
1283 } else {
1284 out_pos[0] = to[0];
1285 out_pos[1] = to[1];
1286 out_pos[2] = to[2];
1287 }
1288
1289 *(u_long128 *) centre = *(u_long128 *) out_pos;
1290 centre[1] += 4.0f;
1291
1292 if (CheckWidth(polys, poly_num, centre, 5.0f, to, mode) != 0) {
1293 out_pos[0] = to[0];
1294 out_pos[2] = to[2];
1295 }
1296
1297 return hit_wall;
1298}
1299
1307 s16 light;
1309 u8 unk_48[8];
1310};
1311
1320int GetFootPoly(float *position, float depth, CCPoly *found, float *ground, CCPoly *polys, int count, int mode) {
1321 int hits;
1322 int i;
1323 int hit_no[32];
1324 sceVu0FVECTOR from;
1325 sceVu0FVECTOR to;
1326 sceVu0FVECTOR hit_point[64];
1327 CCPolyAttr attr;
1328 CCPolyAttr saved;
1329
1330 sceVu0CopyVector(from, position);
1331 sceVu0CopyVector(to, position);
1332 to[1] -= depth;
1333 hits = CheckHits(polys, count, from, to, 32, hit_no, hit_point, 1, mode);
1334 attr.ground_kind = 0;
1335 attr.foot_sound = 0;
1336 attr.light = 0;
1337 saved = attr;
1338
1339 if (hits > 0) {
1340 *found = polys[hit_no[0]];
1341 sceVu0CopyVector(ground, hit_point[0]);
1342 }
1343
1344 for (i = 0; i < hits; i++) {
1345 CCPolyAttr *hit_attr = (CCPolyAttr *) &polys[hit_no[i]].attr;
1346
1347 if (attr.ground_kind == 0) {
1348 attr.ground_kind = hit_attr->ground_kind;
1349 }
1350
1351 if (attr.foot_sound == 0) {
1352 attr.foot_sound = hit_attr->foot_sound;
1353 }
1354
1355 if (attr.light == 0) {
1356 attr.light = hit_attr->light;
1357 }
1358 }
1359
1360 found->info = *(CCPolyInfo *) &attr;
1361 return hits > 0;
1362}
1363
1364int GetEventPoly(float *position, float *velocity, CCPoly *found, int *found_no, float *hit, CCPoly *polys, int count, int mode) {
1365 int hits;
1366 int i;
1367 int hit_no[32];
1368 sceVu0FVECTOR from;
1369 sceVu0FVECTOR to;
1370 sceVu0FVECTOR hit_point[32];
1371 CCPolyAttr attr;
1372
1373 sceVu0CopyVector(from, position);
1374 sceVu0AddVector(to, position, velocity);
1375 hits = CheckHits(polys, count, from, to, 32, hit_no, hit_point, 1, mode);
1376 attr.ground_kind = 0;
1377 attr.foot_sound = 0;
1378 attr.light = 0;
1379 *found_no = -1;
1380
1381 if (hits > 0) {
1382 *found = polys[hit_no[0]];
1383 *found_no = hit_no[0];
1384 sceVu0CopyVector(hit, hit_point[0]);
1385 }
1386
1387 for (i = 0; i < hits; i++) {
1388 CCPolyAttr *hit_attr = (CCPolyAttr *) &polys[hit_no[i]].attr;
1389
1390 if (attr.ground_kind == 0) {
1391 attr.ground_kind = hit_attr->ground_kind;
1392 }
1393
1394 if (attr.foot_sound == 0) {
1395 attr.foot_sound = hit_attr->foot_sound;
1396 }
1397
1398 if (attr.light == 0) {
1399 attr.light = hit_attr->light;
1400 }
1401 }
1402
1403 found->info = *(CCPolyInfo *) &attr;
1404 return attr.ground_kind;
1405}
1406
1407int CheckWidth(CCPoly *polys, int count, float *position, float radius, float *out, int flags) {
1408 int hit;
1409 int hit_plus;
1410 int hit_minus;
1411 int poly_no;
1412 float step;
1413 sceVu0FVECTOR to;
1414 sceVu0FVECTOR hit_a;
1415 sceVu0FVECTOR hit_b;
1416 sceVu0FVECTOR pos;
1417 sceVu0FVECTOR normal;
1418
1419 hit = 0;
1420 step = radius / 1.4142135f;
1421 sceVu0CopyVector(pos, position);
1422 sceVu0CopyVector(out, position);
1423 hit_minus = 0;
1424 hit_plus = 0;
1425 to[0] = pos[0] + step;
1426 to[1] = pos[1];
1427 to[2] = pos[2] + step;
1428 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1429
1430 if (poly_no != -1) {
1431 sceVu0Normalize(normal, polys[poly_no].normal);
1432
1433 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1434 hit_plus = 1;
1435 hit = 1;
1436 }
1437 }
1438
1439 to[0] = pos[0] - step;
1440 to[1] = pos[1];
1441 to[2] = pos[2] - step;
1442 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1443
1444 if (poly_no != -1) {
1445 sceVu0Normalize(normal, polys[poly_no].normal);
1446
1447 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1448 hit = 1;
1449 hit_minus = 1;
1450 }
1451 }
1452
1453 if (hit_plus != 0 && hit_minus != 0) {
1454 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1455 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1456 } else {
1457 if (hit_plus != 0) {
1458 out[0] = hit_a[0] - step;
1459 out[2] = hit_a[2] - step;
1460 }
1461
1462 if (hit_minus != 0) {
1463 out[0] = hit_b[0] + step;
1464 out[2] = hit_b[2] + step;
1465 }
1466 }
1467
1468 sceVu0CopyVector(pos, out);
1469 hit_minus = 0;
1470 hit_plus = 0;
1471 to[0] = pos[0] + step;
1472 to[1] = pos[1];
1473 to[2] = pos[2] - step;
1474 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1475
1476 if (poly_no != -1) {
1477 sceVu0Normalize(normal, polys[poly_no].normal);
1478
1479 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1480 hit_plus = 1;
1481 hit = 1;
1482 }
1483 }
1484
1485 to[0] = pos[0] - step;
1486 to[1] = pos[1];
1487 to[2] = pos[2] + step;
1488 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1489
1490 if (poly_no != -1) {
1491 sceVu0Normalize(normal, polys[poly_no].normal);
1492
1493 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1494 hit = 1;
1495 hit_minus = 1;
1496 }
1497 }
1498
1499 if (hit_plus != 0 && hit_minus != 0) {
1500 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1501 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1502 } else {
1503 if (hit_plus != 0) {
1504 out[0] = hit_a[0] - step;
1505 out[2] = hit_a[2] + step;
1506 }
1507
1508 if (hit_minus != 0) {
1509 out[0] = hit_b[0] + step;
1510 out[2] = hit_b[2] - step;
1511 }
1512 }
1513
1514 sceVu0CopyVector(pos, out);
1515 hit_minus = 0;
1516 hit_plus = 0;
1517 to[0] = pos[0] + radius;
1518 to[1] = pos[1];
1519 to[2] = pos[2];
1520 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1521
1522 if (poly_no != -1) {
1523 sceVu0Normalize(normal, polys[poly_no].normal);
1524
1525 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1526 hit_plus = 1;
1527 hit = 1;
1528 }
1529 }
1530
1531 to[0] = pos[0] - radius;
1532 to[1] = pos[1];
1533 to[2] = pos[2];
1534 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1535
1536 if (poly_no != -1) {
1537 sceVu0Normalize(normal, polys[poly_no].normal);
1538
1539 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1540 hit = 1;
1541 hit_minus = 1;
1542 }
1543 }
1544
1545 if (hit_plus != 0 && hit_minus != 0) {
1546 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1547 } else {
1548 if (hit_plus != 0) {
1549 out[0] = hit_a[0] - radius;
1550 }
1551
1552 if (hit_minus != 0) {
1553 out[0] = hit_b[0] + radius;
1554 }
1555 }
1556
1557 sceVu0CopyVector(pos, out);
1558 hit_minus = 0;
1559 hit_plus = 0;
1560 to[0] = pos[0];
1561 to[1] = pos[1];
1562 to[2] = pos[2] + radius;
1563 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1564
1565 if (poly_no != -1) {
1566 sceVu0Normalize(normal, polys[poly_no].normal);
1567
1568 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1569 hit = 1;
1570 hit_plus = 1;
1571 }
1572 }
1573
1574 to[0] = pos[0];
1575 to[1] = pos[1];
1576 to[2] = pos[2] - radius;
1577 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1578
1579 if (poly_no != -1) {
1580 sceVu0Normalize(normal, polys[poly_no].normal);
1581
1582 if (normal[1] < 0.5f && !(normal[1] <= -0.5f)) {
1583 hit = 1;
1584 hit_minus = 1;
1585 }
1586 }
1587
1588 if (hit_plus != 0 && hit_minus != 0) {
1589 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1590 } else {
1591 if (hit_plus != 0) {
1592 out[2] = hit_a[2] - radius;
1593 }
1594
1595 if (hit_minus != 0) {
1596 out[2] = hit_b[2] + radius;
1597 }
1598 }
1599
1600 return hit;
1601}
1602
1603int CheckCameraWidth(CCPoly *polys, int count, float *position, float radius, float *out, int flags) {
1604 int hit;
1605 int hit_plus;
1606 int hit_minus;
1607 int poly_no;
1608 float step;
1609 sceVu0FVECTOR to;
1610 sceVu0FVECTOR hit_a;
1611 sceVu0FVECTOR hit_b;
1612 sceVu0FVECTOR pos;
1613 sceVu0FVECTOR normal;
1614
1615 hit = 0;
1616 step = radius / 1.4142135f;
1617 sceVu0CopyVector(pos, position);
1618 sceVu0CopyVector(out, position);
1619 hit_minus = 0;
1620 hit_plus = 0;
1621 to[0] = pos[0] + step;
1622 to[1] = pos[1];
1623 to[2] = pos[2] + step;
1624 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1625
1626 if (poly_no != -1) {
1627 sceVu0Normalize(normal, polys[poly_no].normal);
1628
1629 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * step + normal[2] * step < 0.0f) {
1630 hit_plus = 1;
1631 hit = 1;
1632 }
1633 }
1634
1635 to[0] = pos[0] - step;
1636 to[1] = pos[1];
1637 to[2] = pos[2] - step;
1638 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1639
1640 if (poly_no != -1) {
1641 sceVu0Normalize(normal, polys[poly_no].normal);
1642
1643 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * -step + normal[2] * -step < 0.0f) {
1644 hit = 1;
1645 hit_minus = 1;
1646 }
1647 }
1648
1649 if (hit_plus != 0 && hit_minus != 0) {
1650 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1651 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1652 } else {
1653 if (hit_plus != 0) {
1654 out[0] = hit_a[0] - step;
1655 out[2] = hit_a[2] - step;
1656 }
1657
1658 if (hit_minus != 0) {
1659 out[0] = hit_b[0] + step;
1660 out[2] = hit_b[2] + step;
1661 }
1662 }
1663
1664 sceVu0CopyVector(pos, out);
1665 hit_minus = 0;
1666 hit_plus = 0;
1667 to[0] = pos[0] + step;
1668 to[1] = pos[1];
1669 to[2] = pos[2] - step;
1670 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1671
1672 if (poly_no != -1) {
1673 sceVu0Normalize(normal, polys[poly_no].normal);
1674
1675 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * step + normal[2] * -step < 0.0f) {
1676 hit_plus = 1;
1677 hit = 1;
1678 }
1679 }
1680
1681 to[0] = pos[0] - step;
1682 to[1] = pos[1];
1683 to[2] = pos[2] + step;
1684 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1685
1686 if (poly_no != -1) {
1687 sceVu0Normalize(normal, polys[poly_no].normal);
1688
1689 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * -step + normal[2] * step < 0.0f) {
1690 hit = 1;
1691 hit_minus = 1;
1692 }
1693 }
1694
1695 if (hit_plus != 0 && hit_minus != 0) {
1696 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1697 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1698 } else {
1699 if (hit_plus != 0) {
1700 out[0] = hit_a[0] - step;
1701 out[2] = hit_a[2] + step;
1702 }
1703
1704 if (hit_minus != 0) {
1705 out[0] = hit_b[0] + step;
1706 out[2] = hit_b[2] - step;
1707 }
1708 }
1709
1710 sceVu0CopyVector(pos, out);
1711 hit_minus = 0;
1712 hit_plus = 0;
1713 to[0] = pos[0] + radius;
1714 to[1] = pos[1];
1715 to[2] = pos[2];
1716 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1717
1718 if (poly_no != -1) {
1719 sceVu0Normalize(normal, polys[poly_no].normal);
1720
1721 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * radius < 0.0f) {
1722 hit_plus = 1;
1723 hit = 1;
1724 }
1725 }
1726
1727 to[0] = pos[0] - radius;
1728 to[1] = pos[1];
1729 to[2] = pos[2];
1730 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1731
1732 if (poly_no != -1) {
1733 sceVu0Normalize(normal, polys[poly_no].normal);
1734
1735 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[0] * -radius < 0.0f) {
1736 hit = 1;
1737 hit_minus = 1;
1738 }
1739 }
1740
1741 if (hit_plus != 0 && hit_minus != 0) {
1742 out[0] = 0.5f * (hit_a[0] + hit_b[0]);
1743 } else {
1744 if (hit_plus != 0) {
1745 out[0] = hit_a[0] - radius;
1746 }
1747
1748 if (hit_minus != 0) {
1749 out[0] = hit_b[0] + radius;
1750 }
1751 }
1752
1753 sceVu0CopyVector(pos, out);
1754 hit_minus = 0;
1755 hit_plus = 0;
1756 to[0] = pos[0];
1757 to[1] = pos[1];
1758 to[2] = pos[2] + radius;
1759 poly_no = CheckHit(polys, count, pos, to, hit_a, 0, flags);
1760
1761 if (poly_no != -1) {
1762 sceVu0Normalize(normal, polys[poly_no].normal);
1763
1764 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[2] * radius < 0.0f) {
1765 hit = 1;
1766 hit_plus = 1;
1767 }
1768 }
1769
1770 to[0] = pos[0];
1771 to[1] = pos[1];
1772 to[2] = pos[2] - radius;
1773 poly_no = CheckHit(polys, count, pos, to, hit_b, 0, flags);
1774
1775 if (poly_no != -1) {
1776 sceVu0Normalize(normal, polys[poly_no].normal);
1777
1778 if (normal[1] < 0.5f && !(normal[1] <= -0.5f) && normal[2] * -radius < 0.0f) {
1779 hit = 1;
1780 hit_minus = 1;
1781 }
1782 }
1783
1784 if (hit_plus != 0 && hit_minus != 0) {
1785 out[2] = 0.5f * (hit_a[2] + hit_b[2]);
1786 } else {
1787 if (hit_plus != 0) {
1788 out[2] = hit_a[2] - radius;
1789 }
1790
1791 if (hit_minus != 0) {
1792 out[2] = hit_b[2] + radius;
1793 }
1794 }
1795
1796 return hit;
1797}
1798
1799// clang-format off
1800u32 MesWinClut[256] = {
1801 0x00000000, 0x80304045, 0x80BFBFBF, 0x8040BDBD,
1802 0x80BDBD40, 0x8040BD40, 0xFF304045, 0x8066CEE7,
1803 0x808F8F8F, 0x808F8F8F, 0x808F8F8F, 0x808F8F8F,
1804 0x808F8F8F, 0x808F8F8F, 0x808F8F8F, 0x808F8F8F,
1805 0x808F8F8F, 0x80BF3FBF, 0x808F8F8F, 0x808F8F8F,
1806};
1807// clang-format on
1808
1809s32 GaijiDataTbl[][8] = {
1810 {-768, 0, 176, 110, 22, 0, 3, 8},
1811 {-767, 0, 154, 110, 22, 0, 3, 8},
1812 {-766, 32, 132, 32, 22, 0, 3, 2},
1813 {-765, 64, 132, 32, 22, 0, 3, 2},
1814 {-764, 0, 132, 32, 22, 0, 3, 2},
1815 {-763, 96, 132, 32, 22, 0, 3, 2},
1816 {-762, 0, 22, 22, 22, 0, 3, 2},
1817 {-761, 22, 22, 22, 22, 0, 3, 2},
1818 {-760, 66, 22, 22, 22, 0, 3, 2},
1819 {-759, 44, 22, 22, 22, 0, 3, 2},
1820 {-758, 0, 66, 22, 22, 0, 3, 2},
1821 {-757, 44, 66, 22, 22, 0, 3, 2},
1822 {-756, 22, 66, 22, 22, 0, 3, 2},
1823 {-755, 0, 88, 22, 22, 0, 2, 2},
1824 {-754, 22, 88, 22, 22, 0, 2, 2},
1825 {-753, 44, 88, 22, 22, 0, 2, 2},
1826 {-752, 66, 66, 22, 22, 0, 2, 2},
1827 {-751, 66, 88, 22, 22, 0, 2, 2},
1828 {-750, 0, 204, 26, 26, 0, 2, 2},
1829 {-749, 26, 204, 26, 26, 0, 2, 2},
1830 {-748, 0, 230, 26, 26, 0, 2, 2},
1831 {-747, 26, 230, 26, 26, 0, 2, 2},
1832 {-746, 0, 110, 22, 22, 0, 2, 2},
1833 {-745, 22, 110, 66, 22, 8, 2, 5},
1834 {-744, 0, 44, 56, 22, 0, 2, 5},
1835 {-743, 88, 224, 40, 32, 0, 2, 4},
1836 {-742, 88, 22, 22, 22, 0, 2, 2},
1837 {-741, 100, 44, 22, 22, 0, 0, 2},
1838 {-740, 56, 44, 22, 22, 0, 0, 2},
1839 {-739, 100, 66, 22, 22, 0, 0, 2},
1840 {-738, 78, 44, 22, 22, 0, 0, 2},
1841 {-737, 52, 204, 25, 26, 0, 0, 3},
1842 {-736, 52, 230, 33, 18, 0, 0, 3},
1843 {-735, 128, 0, 14, 20, 0, 3, 1},
1844 {-734, 142, 0, 14, 20, 0, 3, 1},
1845 {-733, 156, 0, 14, 20, 0, 3, 1},
1846 {-732, 170, 0, 14, 20, 0, 3, 1},
1847 {-731, 184, 0, 14, 20, 0, 3, 1},
1848 {-730, 198, 0, 14, 20, 0, 3, 1},
1849 {-729, 212, 0, 14, 20, 0, 3, 1},
1850 {-728, 226, 0, 14, 20, 0, 3, 1},
1851 {-727, 240, 0, 14, 20, 0, 3, 1},
1852 {-726, 128, 20, 14, 20, 0, 3, 1},
1853 {-725, 142, 20, 14, 20, 0, 3, 1},
1854 {-724, 156, 20, 14, 20, 0, 3, 1},
1855 {-723, 170, 20, 14, 20, 0, 3, 1},
1856 {-722, 184, 20, 14, 20, 0, 3, 1},
1857 {-721, 198, 20, 14, 20, 0, 3, 1},
1858 {-720, 212, 20, 14, 20, 0, 3, 1},
1859 {-719, 226, 20, 14, 20, 0, 3, 1},
1860 {-718, 240, 20, 14, 20, 0, 3, 1},
1861 {-717, 128, 40, 14, 20, 0, 3, 1},
1862 {-716, 142, 40, 14, 20, 0, 3, 1},
1863 {-715, 156, 40, 14, 20, 0, 3, 1},
1864 {-714, 170, 40, 14, 20, 0, 3, 1},
1865 {-713, 184, 40, 14, 20, 0, 3, 1},
1866 {-712, 198, 40, 14, 20, 0, 3, 1},
1867 {-711, 212, 40, 14, 20, 0, 3, 1},
1868 {-710, 226, 40, 14, 20, 0, 3, 1},
1869 {-709, 240, 40, 14, 20, 0, 3, 1},
1870 {-708, 128, 60, 14, 20, 0, 3, 1},
1871 {-707, 142, 60, 14, 20, 0, 3, 1},
1872 {-706, 156, 60, 14, 20, 0, 3, 1},
1873 {-705, 170, 60, 14, 20, 0, 3, 1},
1874 {-704, 184, 60, 14, 20, 0, 3, 1},
1875 {-703, 198, 60, 14, 20, 0, 3, 1},
1876 {-702, 212, 60, 14, 20, 0, 3, 1},
1877 {-701, 226, 60, 14, 20, 0, 3, 1},
1878 {-700, 240, 60, 14, 20, 0, 3, 1},
1879 {-699, 128, 80, 14, 20, 0, 3, 1},
1880 {-698, 142, 80, 14, 20, 0, 3, 1},
1881 {-697, 156, 80, 14, 20, 0, 3, 1},
1882 {-696, 170, 80, 14, 20, 0, 3, 1},
1883 {-695, 184, 80, 14, 20, 0, 3, 1},
1884 {-694, 198, 80, 14, 20, 0, 3, 1},
1885 {-693, 212, 80, 14, 20, 0, 3, 1},
1886 {-692, 226, 80, 14, 20, 0, 3, 1},
1887 {-691, 240, 80, 14, 20, 0, 3, 1},
1888 {-690, 128, 100, 14, 20, 0, 3, 1},
1889 {-689, 142, 100, 14, 20, 0, 3, 1},
1890 {-688, 156, 100, 14, 20, 0, 3, 1},
1891 {-687, 170, 100, 14, 20, 0, 3, 1},
1892 {-686, 184, 100, 14, 20, 0, 3, 1},
1893 {-685, 198, 100, 14, 20, 0, 3, 1},
1894 {-684, 212, 100, 14, 20, 0, 3, 1},
1895 {-683, 226, 100, 14, 20, 0, 3, 1},
1896 {-682, 240, 100, 14, 20, 0, 3, 1},
1897 {-681, 128, 120, 14, 20, 0, 3, 1},
1898 {-680, 142, 120, 14, 20, 0, 3, 1},
1899 {-679, 156, 120, 14, 20, 0, 3, 1},
1900 {-678, 170, 120, 14, 20, 0, 3, 1},
1901 {-677, 184, 120, 14, 20, 0, 3, 1},
1902 {-676, 198, 120, 14, 20, 0, 3, 1},
1903 {-675, 212, 120, 14, 20, 0, 3, 1},
1904 {-674, 226, 120, 14, 20, 0, 3, 1},
1905 {-673, 240, 120, 14, 20, 0, 3, 1},
1906 {-672, 128, 140, 14, 20, 0, 3, 1},
1907 {-671, 142, 140, 14, 20, 0, 3, 1},
1908 {-670, 156, 140, 14, 20, 0, 3, 1},
1909 {-669, 170, 140, 14, 20, 0, 3, 1},
1910 {-668, 184, 140, 14, 20, 0, 3, 1},
1911 {-667, 198, 140, 14, 20, 0, 3, 1},
1912 {-666, 212, 140, 14, 20, 0, 3, 1},
1913 {-665, 226, 140, 14, 20, 0, 3, 1},
1914 {-664, 240, 140, 14, 20, 0, 3, 1},
1915 {-663, 128, 160, 14, 20, 0, 3, 1},
1916 {-662, 142, 160, 14, 20, 0, 3, 1},
1917 {-661, 156, 160, 14, 20, 0, 3, 1},
1918 {-660, 170, 160, 14, 20, 0, 3, 1},
1919 {-659, 184, 160, 14, 20, 0, 3, 1},
1920 {-658, 198, 160, 14, 20, 0, 3, 1},
1921 {-657, 212, 160, 14, 20, 0, 3, 1},
1922 {-656, 226, 160, 14, 20, 0, 3, 1},
1923 {-655, 240, 160, 14, 20, 0, 3, 1},
1924 {-654, 128, 180, 14, 20, 0, 3, 1},
1925 {-653, 142, 180, 14, 20, 0, 3, 1},
1926 {-652, 156, 180, 14, 20, 0, 3, 1},
1927 {-651, 170, 180, 14, 20, 0, 3, 1},
1928 {-650, 184, 180, 14, 20, 0, 3, 1},
1929 {-649, 198, 180, 14, 20, 0, 3, 1},
1930 {-648, 212, 180, 14, 20, 0, 3, 1},
1931 {-647, 226, 200, 14, 20, 0, 3, 1},
1932 {-646, 198, 200, 14, 20, 0, 3, 1},
1933 {-645, 184, 200, 14, 20, 0, 3, 1},
1934 {-644, 128, 0, 14, 20, 0, 3, 1},
1935#ifdef PAL
1936 {-643, 142, 220, 14, 20, 0, 3, 1},
1937#else
1938 {-643, 156, 0, 14, 20, 0, 3, 1},
1939#endif
1940 {-642, 184, 0, 14, 20, 0, 3, 1},
1941 {-641, 184, 0, 14, 20, 0, 3, 1},
1942 {-640, 198, 20, 14, 20, 0, 3, 1},
1943 {-639, 156, 40, 14, 20, 0, 3, 1},
1944 {-638, 212, 200, 14, 20, 0, 3, 1},
1945 {-637, 240, 40, 14, 20, 0, 3, 1},
1946 {-636, 240, 40, 14, 20, 0, 3, 1},
1947 {-635, 240, 40, 14, 20, 0, 3, 1},
1948 {-634, 240, 40, 14, 20, 0, 3, 1},
1949#ifdef PAL
1950 {-633, 156, 220, 14, 20, 0, 3, 1},
1951#else
1952 {-633, 142, 60, 14, 20, 0, 3, 1},
1953#endif
1954 {-632, 170, 60, 14, 20, 0, 3, 1},
1955 {-631, 170, 60, 14, 20, 0, 3, 1},
1956 {-630, 170, 60, 14, 20, 0, 3, 1},
1957 {-629, 170, 60, 14, 20, 0, 3, 1},
1958#ifdef PAL
1959 {-628, 128, 220, 14, 20, 0, 3, 1},
1960 {-627, 128, 220, 14, 20, 0, 3, 1},
1961 {-626, 128, 220, 14, 20, 0, 3, 1},
1962 {-625, 128, 220, 14, 20, 0, 3, 1},
1963#else
1964 {-628, 226, 60, 14, 20, 0, 3, 1},
1965 {-627, 226, 60, 14, 20, 0, 3, 1},
1966 {-626, 226, 60, 14, 20, 0, 3, 1},
1967 {-625, 226, 60, 14, 20, 0, 3, 1},
1968#endif
1969 {-624, 170, 80, 14, 20, 0, 3, 1},
1970 {-623, 184, 80, 14, 20, 0, 3, 1},
1971 {-622, 184, 80, 14, 20, 0, 3, 1},
1972 {-621, 184, 80, 14, 20, 0, 3, 1},
1973 {-620, 184, 80, 14, 20, 0, 3, 1},
1974 {-619, 142, 100, 14, 20, 0, 3, 1},
1975 {-618, 142, 100, 14, 20, 0, 3, 1},
1976 {-617, 142, 100, 14, 20, 0, 3, 1},
1977 {-616, 142, 100, 14, 20, 0, 3, 1},
1978 {-615, 156, 40, 14, 20, 0, 3, 1},
1979 {-614, 128, 0, 14, 20, 0, 3, 1},
1980 {-613, 240, 200, 14, 20, 0, 3, 1},
1981 {-612, 198, 20, 14, 20, 0, 3, 1},
1982#ifdef PAL
1983 {-611, 156, 120, 14, 20, 0, 3, 1},
1984 {-610, 156, 120, 14, 20, 0, 3, 1},
1985 {-609, 156, 120, 14, 20, 0, 3, 1},
1986 {-608, 128, 0, 14, 20, 0, 3, 1},
1987 {-607, 128, 0, 14, 20, 0, 3, 1},
1988 {-606, 240, 0, 14, 20, 0, 3, 1},
1989 {-605, 240, 0, 14, 20, 0, 3, 1},
1990 {-604, 240, 0, 14, 20, 0, 3, 1},
1991 {-603, 240, 0, 14, 20, 0, 3, 1},
1992 {-602, 156, 40, 14, 20, 0, 3, 1},
1993 {-601, 156, 40, 14, 20, 0, 3, 1},
1994 {-600, 184, 0, 14, 20, 0, 3, 1},
1995 {-599, 184, 0, 14, 20, 0, 3, 1},
1996 {-598, 198, 20, 14, 20, 0, 3, 1},
1997 {-597, 198, 20, 14, 20, 0, 3, 1},
1998 {-596, 184, 20, 14, 20, 0, 3, 1},
1999 {-595, 156, 120, 14, 20, 0, 3, 1},
2000 {-594, 156, 120, 14, 20, 0, 3, 1},
2001#endif
2002 {-1, 96, 96, 32, 32, 0, 0, 1},
2003};
2004
2005// clang-format off
2012// clang-format on
2013
2014int Mes1MakeFlg = 1;
2015int Mes2MakeFlg = 1;
2016int MesAbsDrawOff;
2017
2019u8 MesWinTexBuff_01[0x100];
2021u8 MesWinTexBuff_02[0x100];
2023u8 MesWinTexBuff_11[0x100];
2025u8 MesWinTexBuff_12[0x100];
2026
2027static s32 linear; // Nonzero selects linear filtering for sprite batches.
2028static u_long128 *data_top; // First quadword of the open sprite batch.
2029static u_long128 *pdata; // Current write cursor of the open sprite batch.
2030static u_int *dma_cnt; // DMA and VIF tag words patched when the batch closes.
2031
2032void set2DSprite_Start(sceVif1Packet *packet, CTexture *texture) {
2033 u_int *p;
2034 u_long *ad;
2035 sceGsTest test;
2036 sceGsZbuf zbuf;
2037
2038 if (texture == 0) {
2039 return;
2040 }
2041
2042 sceVif1PkTerminate(packet);
2043 data_top = (u_long128 *) packet->pCurrent;
2044 pdata = data_top;
2045 dma_cnt = p = (u_int *) pdata;
2046 p[0] = 0x10000000 | 5;
2047 p[1] = 0;
2048 p[2] = 0;
2049 p[3] = 0x50000000 | 5;
2050 p[4] = 0x8004;
2051 p[5] = 0x10000000;
2052 p[6] = 14;
2053 p[7] = 0;
2054 ad = (u_long *) (p + 8);
2055 ad[0] = 0;
2056 ad[1] = SCE_GS_TEXFLUSH;
2057 ad[2] = ((u_long) linear << 5) | 0x41;
2058 ad[3] = SCE_GS_TEX1_1;
2059 ad[4] = texture->tex0;
2060 ad[5] = SCE_GS_TEX0_1;
2061 ad[6] = SCE_GS_SET_PRIM(SCE_GS_PRIM_SPRITE, 0, 1, 0, 1, 1, 1, 0, 0);
2062 ad[7] = SCE_GS_PRIM;
2063 test = mgPixelTest;
2064 test.bits.ate = 0;
2065 test.bits.aref = 0;
2066 test.bits.atst = SCE_GS_ALWAYS;
2067 test.bits.zte = 1;
2068 test.bits.ztst = SCE_GS_ALWAYS;
2069 ad[8] = *(u_long *) &test;
2070 ad[9] = SCE_GS_TEST_1;
2071 zbuf = mgZBuffer;
2072 zbuf.bits.zmsk = 1;
2073 ad[10] = *(u_long *) &zbuf;
2074 ad[11] = SCE_GS_ZBUF_1;
2075 pdata = (u_long128 *) (ad + 12);
2076}
2077
2078void set2DSprite_Core(sceVif1Packet *packet, CTexture *texture, const CRect_i_ &screen, const CRect_i_ &texel, u8 red, u8 green, u8 blue, u8 alpha) {
2079 u_long *ad;
2080
2081 if (texture == 0) {
2082 return;
2083 }
2084
2085 ad = (u_long *) pdata;
2086 ad[0] = SCE_GS_SET_RGBAQ(red, green, blue, alpha, 0);
2087 ad[1] = SCE_GS_RGBAQ;
2088 ad[2] = SCE_GS_SET_UV(texel.x << 4, texel.y << 4);
2089 ad[3] = SCE_GS_UV;
2090 ad[4] = SCE_GS_SET_XYZF2((screen.x << 4) + 27648, (screen.y << 3) + GS_Y_OFFSET, 0, 0);
2091 ad[5] = SCE_GS_XYZF2;
2092 ad[6] = SCE_GS_SET_UV((texel.x + texel.width) << 4, (texel.y + texel.height) << 4);
2093 ad[7] = SCE_GS_UV;
2094 ad[8] = SCE_GS_SET_XYZF2(((screen.x + screen.width) << 4) + 27647, ((screen.y + screen.height) << 3) + GS_Y_OFFSET, 0, 0);
2095 ad[9] = SCE_GS_XYZF2;
2096 pdata = (u_long128 *) (ad + 10);
2097}
2098
2099void set2DSprite_End(sceVif1Packet *packet, CTexture *texture) {
2100 u_long *ad;
2101 u_int *dma_count;
2102 s32 qwc;
2103
2104 ad = (u_long *) pdata;
2105 ad[0] = *(u_long *) &mgPixelTest;
2106 ad[1] = SCE_GS_TEST_1;
2107 ad[2] = *(u_long *) &mgZBuffer;
2108 ad[3] = SCE_GS_ZBUF_1;
2109 pdata = (u_long128 *) (ad + 4);
2110 dma_count = dma_cnt;
2111 qwc = pdata - data_top - 1;
2112 dma_count[0] = 0x10000000 | qwc;
2113 dma_count[3] = 0x50000000 | qwc;
2114 dma_count[4] = (qwc - 1) | 0x8000;
2115 sceVif1PkReserve(packet, (pdata - data_top) * 4);
2116}
2117
2118void SetClut(sceVif1Packet *packet, CTexture *texture, i *clut) {
2119 sceGsTex0 tex0 = *(sceGsTex0 *) &texture->tex0;
2120 sceVif1PkRefLoadImage(packet, tex0.bits.cbp, SCE_GS_PSMCT32, 1, (u_long128 *) clut, 64, 0, 0, 16, 16);
2121}
2122
2123float LinerInterpolation(float from, float to, float at) {
2124 return from + (at * (to - from));
2125}
2126
2127void AreaAddPos(int *area, int *pos, int *out) {
2128 int left = area[0];
2129 int top = area[1];
2130 int right = area[2];
2131 int bottom = area[3];
2132 int x = pos[0];
2133 int y = pos[1];
2134
2135 if (x < left) {
2136 left = x;
2137 }
2138
2139 if (right < x) {
2140 right = x;
2141 }
2142
2143 if (y < top) {
2144 top = y;
2145 }
2146
2147 if (bottom < y) {
2148 bottom = y;
2149 }
2150
2151 out[0] = left;
2152 out[1] = top;
2153 out[2] = right;
2154 out[3] = bottom;
2155}
2156
2157void RollPos(float *centre, float *point, float angle, float *out) {
2158 float centre_x = centre[0];
2159 float centre_y = centre[1];
2160 float point_x = point[0];
2161 float point_y = point[1];
2162
2163 out[0] = centre_x + ((point_x - centre_x) * cos(angle) - (point_y - centre_y) * sin(angle));
2164 out[1] = centre_y - ((point_x - centre_x) * sin(angle) + (point_y - centre_y) * cos(angle));
2165}
2166
2167int CheckPosInOutForRect(RECT *rect, int x, int y) {
2168 s32 top;
2169 s32 left;
2170
2171 left = rect->x;
2172
2173 if (x < left) {
2174 return 0;
2175 }
2176
2177 if ((left + rect->width) < x) {
2178 return 0;
2179 }
2180
2181 top = rect->y;
2182
2183 if (y < top) {
2184 return 0;
2185 }
2186
2187 return ((top + rect->height) < y) ? 0 : 1;
2188}
2189
2190float GetDisPosToRect(RECT *rect, int x, int y) {
2191 float dx = rect->x + (rect->width >> 1) - x;
2192 float dy = rect->y + (rect->height >> 1) - y;
2193
2194 return sqrt(dx * dx + dy * dy);
2195}
2196
2197void GetScrPosFromChar(CCharacter *chara, int *out_pos) {
2198 float position[4];
2199 int screen[4];
2200
2201 chara->GetPosition(position);
2202 position[1] += 0.85f * chara->body_height;
2203 position[3] = 1.0f;
2204 MGRotTransPers2D(screen, position, 0);
2205 out_pos[0] = screen[0];
2206 out_pos[1] = screen[1];
2207}
2208
2209unsigned int Color2Clut(unsigned int colour) {
2210 for (int i = 0; i < 16; i++) {
2211 if (colour == FontColorTbl[i]) {
2212 return i;
2213 }
2214 }
2215
2216 return 0;
2217}
2218
2219int NameRegistCodeJtoE(int code) {
2220#ifdef PAL
2221 int table[109] = {
2222 -683, -682, -681, -680, -679, -678, -677, -676, -675, -674,
2223 -673, -672, -679, -671, -670, -669, -254, -668, -667, -666,
2224 -665, -664, -679, -679, -679, -679, -679, -679, -657, -656,
2225 -655, -654, -653, -652, -651, -650, -649, -648, -679, -679,
2226 -679, -661, -660, -659, -658, -679, -644, -614, -608, -607,
2227 -679, -634, -636, -637, -635, -679, -606, -605, -604, -603,
2228 -679, -625, -627, -628, -626, -679, -639, -615, -602, -601,
2229 -679, -616, -618, -619, -617, -679, -600, -641, -642, -599,
2230 -679, -629, -631, -632, -630, -679, -640, -612, -598, -597,
2231 -679, -620, -622, -623, -621, -679, -596, -624, -643, -633,
2232 -613, -647, -595, -594, -679, -679, -638, -646, -645};
2233
2234 if (code >= 0xD5 && code < 0x142) {
2235#else
2236 int table[82] = {
2237 -683, -682, -681, -680, -679, -678, -677, -676, -675, -674,
2238 -673, -672, -679, -671, -670, -669, -254, -668, -667, -666,
2239 -665, -664, -679, -679, -679, -679, -679, -679, -657, -656,
2240 -655, -654, -653, -652, -651, -650, -649, -648, -679, -679,
2241 -679, -661, -660, -659, -658, -679, -647, -646, -645, -644,
2242 -643, -642, -641, -640, -639, -638, -637, -636, -635, -634,
2243 -633, -632, -631, -630, -629, -628, -627, -626, -625, -624,
2244 -623, -622, -621, -620, -619, -618, -617, -616, -615, -614,
2245 -613, -612};
2246
2247 if (code >= 0xD5 && code < 0x102) {
2248#endif
2249 return table[code - 0xD5];
2250 }
2251
2252 if (code >= 0xA1 && code < 0xBB) {
2253 return code - 0x380;
2254 }
2255
2256 if (code >= 0xBB && code < 0xD5) {
2257 return code - 0x380;
2258 }
2259
2260 return -0x2A7;
2261}
sceVu0FVECTOR min
Definition collision.hpp:29
sceVu0FVECTOR max
Definition collision.hpp:28
CCPolyInfo info
Definition collision.hpp:52
s16 ignore_mask
Definition collision.hpp:58
sceVu0FVECTOR normal
Definition collision.hpp:49
sceVu0FVECTOR vertex[3]
Definition collision.hpp:48
void SetRef(CFrame *parent, float x, float y, float z)
Definition camera.cpp:124
void SetPos(CFrame *parent, float x, float y, float z)
Definition camera.cpp:103
void SetRoll(float roll)
Definition camera.cpp:226
virtual void GetPosition(float *out_position)
float body_height
short draw_on
Definition frame.hpp:27
char remake_pending
Definition frame.hpp:31
CVisualVu1 * GetVisual()
Definition frame.cpp:1337
sceVu0FMATRIX local
Definition frame.hpp:74
CFrameAttr attr
Definition frame.hpp:65
int world_valid
Definition frame.hpp:78
CFrame * parent
Definition frame.hpp:66
s32 x
Definition rect.hpp:18
s32 width
Definition rect.hpp:20
s32 height
Definition rect.hpp:21
s32 y
Definition rect.hpp:19
u_long tex0
Definition texture.hpp:57
@ FONT_COLOR_BROWN
Definition gameutil.hpp:16
@ FONT_COLOR_GOLD
Definition gameutil.hpp:21
@ FONT_COLOR_CYAN
Definition gameutil.hpp:18
@ FONT_COLOR_GREEN
Definition gameutil.hpp:19
@ FONT_COLOR_WHITE
Definition gameutil.hpp:15
@ FONT_COLOR_GREY
Definition gameutil.hpp:22
@ FONT_COLOR_BROWN_OPAQUE
Definition gameutil.hpp:20
@ FONT_COLOR_YELLOW
Definition gameutil.hpp:17
@ MOTION_KEY_CAMERA_ROLL
Definition gameutil.hpp:39
@ MOTION_KEY_VERTEX
Definition gameutil.hpp:36
@ MOTION_KEY_MATERIAL_ALPHA
Definition gameutil.hpp:41
@ MOTION_KEY_SCALE
Definition gameutil.hpp:34
@ MOTION_KEY_MATERIAL_COLOR
Definition gameutil.hpp:42
@ MOTION_KEY_UNK_33
Definition gameutil.hpp:44
@ MOTION_KEY_CAMERA_FOV
Definition gameutil.hpp:40
@ MOTION_KEY_ROTATION
Definition gameutil.hpp:33
@ MOTION_KEY_CAMERA_POSITION
Definition gameutil.hpp:37
@ MOTION_KEY_TRANSLATION
Definition gameutil.hpp:35
@ MOTION_KEY_SKIP
Definition gameutil.hpp:45
@ MOTION_KEY_VISIBLE
Definition gameutil.hpp:43
@ MOTION_KEY_CAMERA_TARGET
Definition gameutil.hpp:38
int vertex_ofs
Definition mdt.hpp:14
int vertex_num
Definition mdt.hpp:13
int info_ofs
Definition mdt.hpp:21
sceVu0FVECTOR diffuse
Definition mdt.hpp:28
unsigned int * data
Definition gameutil.hpp:291
unsigned int next_frame
Definition gameutil.hpp:192
CFrameVu1 * look_frame
Definition gameutil.hpp:198
_FRAMECONSTRAINT look_constraint
Definition gameutil.hpp:201
sceVu0FVECTOR look_position
Definition gameutil.hpp:200
CCamera * camera
Definition gameutil.hpp:196
unsigned int frame
Definition gameutil.hpp:191
float blend_step
Definition gameutil.hpp:190
CFrameVu1 * look_target
Definition gameutil.hpp:199
sceVu0FVECTOR value
Definition gameutil.hpp:229
u32 frame
Definition gameutil.hpp:227
Mot_List * next
Definition gameutil.hpp:243
u32 key_count
Definition gameutil.hpp:241
Mot_Key * keys
Definition gameutil.hpp:242
sceVu0FVECTOR ground_point
Definition gameutil.hpp:475
CCPoly ground_poly
Definition gameutil.hpp:471
Definition title.cpp:61
s32 width
Definition rect.hpp:36
s32 height
Definition rect.hpp:37
int y
Definition title.cpp:63
int x
Definition title.cpp:62
sceSifServeData * server
Definition sifrpc.h:25
void * data
Definition sifdma.h:7
void * addr
Definition sifdma.h:8
sceVu0FVECTOR * base_vertices
Definition gameutil.hpp:213
sceVu0FMATRIX bone_matrix
Definition gameutil.hpp:217
sceVu0FMATRIX bone_base_matrix
Definition gameutil.hpp:216
sceVu0FMATRIX matrix
Definition gameutil.hpp:215
sceVu0FMATRIX * base_matrices
Definition gameutil.hpp:269
Mot_List * proc_list
Definition gameutil.hpp:270
Mot_List * proc_list2
Definition gameutil.hpp:271