DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
clothread.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 187
4
5#include "common.h"
6#include "sce/libcdvd.h"
7#include "sce/sifdev.h"
8#include "sce/sifrpc.h"
9
10#include <libpkt.h>
11
12#include <cassert>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16
17#include "bound.hpp"
18#include "btsysscript.hpp"
19#include "cloth.hpp"
20#include "clothread.hpp"
21#include "dataalloc.hpp"
22#include "dataread.hpp"
23#include "dataset.hpp"
24#include "framevu1.hpp"
25#include "gameutil.hpp"
26#include "mathutil.hpp"
27#include "mglib.hpp"
28#include "rect.hpp"
29#include "savedata.hpp"
30#include "scriptinterpreter.hpp"
31#include "snd.hpp"
32#include "sound.hpp"
33#include "sysmes.hpp"
34#include "visualvu1.hpp"
35
36CCloth *pCloth;
37
41static CBound *pBound;
42
46static CFrameVu1 *ParentFrame;
47
51static CDataAlloc2<1> *DataBuffer;
52
56static void CommandSIZE(void **argv);
60static void CommandFRAME(void **argv);
64static void CommandNORMAL(void **argv);
68static void CommandFOLLOW(void **argv);
72static void CommandK(void **argv);
76static void CommandWINDEFFECT(void **argv);
80static void CommandGRAVITY(void **argv);
84static void CommandPOLYDIVE(void **argv);
88static void CommandBOUND(void **argv);
89
95struct COMMAND_INFO {
96 char *name;
97 int arg_types[16];
98};
99
100/* The keywords a cloth configuration file may use. */
101static COMMAND_INFO Command[9] = {
102 {"SIZE", {SCRIPT_ARGUMENT_INTEGER, SCRIPT_ARGUMENT_INTEGER, -1} },
103 {"FRAME", {SCRIPT_ARGUMENT_STRING, -1} },
104 {"NORMAL", {SCRIPT_ARGUMENT_FLOAT, -1} },
105 {"FOLLOW", {SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, -1} },
106 {"K", {SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, -1} },
107 {"WINDEFFECT", {SCRIPT_ARGUMENT_FLOAT, -1} },
108 {"GRAVITY", {SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, -1} },
109 {"POLYDIV", {SCRIPT_ARGUMENT_STRING, -1} },
110 {"BOUND", {SCRIPT_ARGUMENT_STRING, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, SCRIPT_ARGUMENT_FLOAT, -1}},
111};
112
113/* What each of those keywords does, in the same order. */
114static void (*CommandExe[9])(void **) = {
115 CommandSIZE,
116 CommandFRAME,
117 CommandNORMAL,
118 CommandFOLLOW,
119 CommandK,
120 CommandWINDEFFECT,
121 CommandGRAVITY,
122 CommandPOLYDIVE,
123 CommandBOUND,
124};
125
126static int GetArg(input_str &input, int *arg_types, void **argv);
127static int SearchCommand(input_str &input, int *command);
128static int SkipSpace(input_str &input);
129static int CheckChar(char ch);
130
138CCloth *InitCloth(CFrameVu1 *frame, input_str &input, CDataAlloc2<1> *alloc) {
139 char arg_storage[16][256];
140 void *argv[16];
141 int command;
142
143 DataBuffer = alloc;
144 pCloth = new ((u_long128 *) alloc->Alloc(0x856)) CCloth(16, 16, 1.0f);
145 ParentFrame = frame;
146 pBound = NULL;
147
148 if (frame == NULL) {
149 return NULL;
150 }
151
152 if (pCloth == NULL) {
153 return NULL;
154 }
155
156 SkipSpace(input);
157
158 for (int i = 0; i < 16; i++) {
159 argv[i] = arg_storage[i];
160 }
161
162 while (SearchCommand(input, &command) != 0) {
163 if (command >= 9 || command < 0) {
164 printf("unknown command!!\n");
165 continue;
166 }
167
168 int arg_status = GetArg(input, Command[command].arg_types, (void **) argv);
169
170 if (arg_status == 0) {
171 break;
172 }
173
174 if (arg_status < 0) {
175 printf("error!!\n");
176 }
177
178 CommandExe[command](argv);
179 }
180
181 return pCloth;
182}
183
184static void CommandSIZE(void **argv) {
185 int num_i = *(int *) argv[0];
186 int num_j = *(int *) argv[1];
187
188 if (num_i <= 0) {
189 num_i = 1;
190 }
191
192 if (num_i > 16) {
193 num_i = 16;
194 }
195
196 if (num_j <= 0) {
197 num_j = 1;
198 }
199
200 if (num_j > 16) {
201 num_j = 16;
202 }
203
204 pCloth->num_i = num_i;
205 pCloth->num_j = num_j;
206}
207
215static void CommandFRAME(void **argv) {
216 CFrameVu1 *frame = (CFrameVu1 *) ParentFrame->SearchFrame((char *) argv[0]);
217
218 pCloth->frame = frame;
219
220 if (frame != NULL) {
221 frame->attr.draw_on = 2;
222 CVisualVu1 *visual = frame->GetVisual();
223 pCloth->Initialize((MDT_HEADER *) visual->GetMDTDataAddress(), DataBuffer);
224 }
225}
226
227static void CommandNORMAL(void **argv) {
228 pCloth->normal_scale = *(float *) argv[0];
229}
230
231static void CommandFOLLOW(void **argv) {
232 pCloth->follow[0] = *(float *) argv[0];
233 pCloth->follow[1] = *(float *) argv[1];
234 pCloth->follow[2] = *(float *) argv[2];
235}
236
237static void CommandK(void **argv) {
238 pCloth->stiffness[0] = *(float *) argv[0];
239 pCloth->stiffness[1] = *(float *) argv[1];
240 pCloth->stiffness[2] = *(float *) argv[2];
241}
242
243static void CommandWINDEFFECT(void **argv) {
244 pCloth->wind_effect = *(float *) argv[0];
245}
246
247static void CommandGRAVITY(void **argv) {
248 pCloth->gravity[0] = *(float *) argv[0];
249 pCloth->gravity[1] = *(float *) argv[1];
250 pCloth->gravity[2] = *(float *) argv[2];
251}
252
253static void CommandPOLYDIVE(void **argv) {
254 char *flags = (char *) argv[0];
255 int row = 0;
256
257 while (row < 16) {
258 char flag = *flags;
259
260 if (flag == 0) {
261 break;
262 }
263
264 pCloth->polygon_divide[row] = (flag != '0');
265 flags++;
266 row++;
267 }
268}
269
277static void CommandBOUND(void **argv) {
278 sceVu0FVECTOR box_vectors[4];
279 CBound *bound = new ((u_long128 *) DataBuffer->Alloc(0x14)) CBound(1.0f, 1.0f, 1.0f);
280
281 if (bound == NULL) {
282 return;
283 }
284
285 int arg_index = 0;
286 CFrame *frame = ParentFrame->SearchFrame((char *) argv[arg_index++]);
287
288 if (frame == NULL) {
289 return;
290 }
291
292 for (int i = 0; i < 4; i++) {
293 box_vectors[i][0] = *(float *) argv[arg_index];
294 box_vectors[i][1] = *(float *) argv[(int) (arg_index + 1)];
295 box_vectors[i][2] = *(float *) argv[(int) (arg_index + 2)];
296 arg_index += 3;
297 box_vectors[i][3] = 1.0f;
298 }
299
300 box_vectors[0][3] = 0.0f;
301 bound->SetDir(frame, box_vectors[1], box_vectors[2], box_vectors[0], box_vectors[3][0], box_vectors[3][1]);
302 float half_x;
303 float half_y;
304 float half_z;
305 half_z = box_vectors[3][2];
306 half_y = box_vectors[3][1];
307 half_x = box_vectors[3][0];
308 bound->extent[0] = half_x;
309 bound->extent[1] = half_y;
310 bound->extent[2] = half_z;
311
312 if (bound->extent[0] > 0.0f) {
313 bound->reciprocal[0] = 1.0f / half_x;
314 }
315
316 if (bound->extent[1] > 0.0f) {
317 bound->reciprocal[1] = 1.0f / half_y;
318 }
319
320 if (bound->extent[2] > 0.0f) {
321 bound->reciprocal[2] = 1.0f / half_z;
322 }
323
324 bound->friction = *(float *) argv[arg_index];
325
326 if (pBound == NULL) {
327 pCloth->bound = bound;
328 } else {
329 pBound->next = bound;
330 }
331
332 pBound = bound;
333}
334
343static int GetArg(input_str &input, int *arg_types, void **argv) {
344 char word[256];
345
346 if (!SkipSpace(input)) {
347 return 0;
348 }
349
350 int arg_count = 0;
351
352 while (arg_types[arg_count++] >= 0)
353 ;
354
355 int ch;
356
357 for (int i = 0; i < arg_count - 1; i++) {
358 int length = 0;
359
360 if (!SkipSpace(input)) {
361 return 0;
362 }
363
364 while (1) {
365 if (input.get(&ch) == 0) {
366 return 0;
367 }
368
369 if (ch == ',' || !CheckChar(ch)) {
370 break;
371 }
372
373 word[length++] = ch;
374 }
375
376 word[length] = 0;
377
378 switch (arg_types[i]) {
379 case SCRIPT_ARGUMENT_STRING:
380 if (word[0] != '"') {
381 return -1;
382 }
383
384 for (length = 1;; length++) {
385 char letter = word[length];
386
387 if (letter == '"') {
388 word[length] = 0;
389 break;
390 }
391
392 if (letter == 0) {
393 return -1;
394 }
395 }
396
397 strcpy((char *) argv[i], word + 1);
398 break;
399 case SCRIPT_ARGUMENT_INTEGER:
400 for (length = 0; word[length] != 0; length++) {
401 char letter = word[length];
402
403 if (letter < '0' || letter > '9') {
404 return -1;
405 }
406 }
407
408 *(int *) argv[i] = atoi(word);
409 break;
410 case SCRIPT_ARGUMENT_FLOAT:
411 for (length = 0; word[length] != 0; length++) {
412 char letter = word[length];
413
414 if ((letter < '0' || letter > '9') && letter != '.' && letter != '-') {
415 return -1;
416 }
417 }
418
419 *(float *) argv[i] = (float) atof(word);
420 break;
421 default:
422 return -1;
423 }
424 }
425
426 return 1;
427}
428
437static int SearchCommand(input_str &input, int *command) {
438 char word[256];
439
440 if (!SkipSpace(input)) {
441 return 0;
442 }
443
444 int length = 0;
445 int ch;
446
447 while (1) {
448 if (input.get(&ch) == 0) {
449 return 0;
450 }
451
452 if (!CheckChar(ch)) {
453 break;
454 }
455
456 word[length++] = ch;
457 }
458
459 word[length] = 0;
460
461 for (int i = 0; i < 9; i++) {
462 if (strcmp(Command[i].name, word) == 0) {
463 *command = i;
464 return 1;
465 }
466 }
467
468 *command = 10;
469 return 1;
470}
471
480static int SkipSpace(input_str &input) {
481 char *text;
482 int pos;
483
484 text = input.data;
485 pos = input.pos;
486
487 while (pos < input.size) {
488 if (CheckChar(text[pos])) {
489 break;
490 }
491
492 pos++;
493 }
494
495 input.pos = pos;
496
497 if (pos >= input.size) {
498 return 0;
499 }
500
501 return 1;
502}
503
512static int CheckChar(char ch) {
513 int is_space = 0;
514
515 if (ch == ' ') {
516 is_space = 1;
517 }
518
519 if (ch == '\t') {
520 is_space = 1;
521 }
522
523 if (ch == '\n') {
524 is_space = 1;
525 }
526
527 if (ch == '\r') {
528 is_space = 1;
529 }
530
531 return !is_space;
532}
533
537int keyCtrl(float stick_x, float stick_y, MOTION_INFO *motion) {
538 int move_state = 0;
539
540 if (stick_y != 0.0f || stick_x != 0.0f) {
541 float tilt;
542 float speed;
543
544 move_state = 2;
545
546 if (stick_x < 0.0f) {
547 stick_x *= -1.0f;
548 }
549
550 if (stick_y < 0.0f) {
551 stick_y *= -1.0f;
552 }
553
554 if (stick_x >= stick_y) {
555 tilt = stick_x;
556 } else {
557 tilt = stick_y;
558 }
559
560 speed = 0.8f * (0.1f + tilt);
561 motion[2].speed = speed;
562
563 if (speed >= 0.7f) {
564 motion[2].speed = 0.7f;
565 }
566
567 if (stick_x + stick_y >= 0.85f) {
568 move_state = 1;
569 }
570 }
571
572 return move_state;
573}
574
582void MoveImageTest(sceVif1Packet *packet, int src_base, int src_width, int src_format, const CRect_i_ &rect, int dst_base, int dst_width, int dst_format, int dst_x, int dst_y, int direction) {
583 sceVif1PkCnt(packet, 0);
584 sceVif1PkOpenDirectCode(packet, 0);
585 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
586 sceVif1PkAddGsAD(packet, SCE_GS_BITBLTBUF, SCE_GS_SET_BITBLTBUF(src_base, src_width, src_format, dst_base, dst_width, dst_format));
587 sceVif1PkAddGsAD(packet, SCE_GS_TRXPOS, SCE_GS_SET_TRXPOS(rect.x, rect.y, dst_x, dst_y, direction));
588 sceVif1PkAddGsAD(packet, SCE_GS_TRXREG, SCE_GS_SET_TRXREG(rect.width, rect.height));
589 sceVif1PkAddGsAD(packet, SCE_GS_TRXDIR, 2);
590 sceVif1PkCloseGifTag(packet);
591 sceVif1PkCloseDirectCode(packet);
592}
593
597float unitRotation(CFrameVu1 *frame, float heading) {
598 float rotation[4];
599 float delta;
600 float abs_delta;
601
602 frame->GetRotation(rotation);
603 delta = heading - rotation[1];
604
605 if (delta <= 0.0f) {
606 abs_delta = -1.0f * delta;
607 } else {
608 abs_delta = delta;
609 }
610
611 if (abs_delta <= PI_D) {
612 if (abs_delta <= TWELFTH_PI_D) {
613 delta = 0.0f;
614 }
615 } else {
616 abs_delta = TWO_PI_D - abs_delta;
617
618 if (abs_delta <= TWELFTH_PI_D) {
619 delta = 0.0f;
620 }
621 }
622
623 if (delta > 0.0f) {
624 if (delta <= PI_D) {
625 rotation[1] += TWELFTH_PI;
626 } else {
627 rotation[1] -= TWELFTH_PI;
628 }
629 }
630
631 if (delta < 0.0f) {
632 if (delta >= -PI_D) {
633 rotation[1] -= TWELFTH_PI;
634 } else {
635 rotation[1] += TWELFTH_PI;
636 }
637 }
638
639 if (0.0f == delta) {
640 rotation[1] = heading;
641 }
642
643 if (rotation[1] <= -PI_D) {
644 rotation[1] += TWO_PI;
645 }
646
647 if (rotation[1] >= PI_D) {
648 rotation[1] -= TWO_PI;
649 }
650
651 return rotation[1];
652}
CBound * next
Definition bound.hpp:29
sceVu0FVECTOR extent
Definition bound.hpp:32
float friction
Definition bound.hpp:34
void SetDir(CFrame *frame, float *from_position, float *to_position, float *up_direction, float half_width, float half_height)
Definition bound.cpp:86
sceVu0FVECTOR reciprocal
Definition bound.hpp:33
float normal_scale
Definition cloth.hpp:36
sceVu0FVECTOR stiffness
Definition cloth.hpp:40
int num_i
Definition cloth.hpp:25
float wind_effect
Definition cloth.hpp:35
virtual void Initialize(CDataAlloc2< 1 > *alloc)
Definition cloth.cpp:673
int num_j
Definition cloth.hpp:26
int polygon_divide[16]
Definition cloth.hpp:50
sceVu0FVECTOR gravity
Definition cloth.hpp:38
CFrame * frame
Definition cloth.hpp:29
sceVu0FVECTOR follow
Definition cloth.hpp:39
CBound * bound
Definition cloth.hpp:31
short draw_on
Definition frame.hpp:27
CVisualVu1 * GetVisual()
Definition frame.cpp:1337
CFrameAttr attr
Definition frame.hpp:65
s32 x
Definition rect.hpp:18
s32 width
Definition rect.hpp:20
s32 height
Definition rect.hpp:21
s32 y
Definition rect.hpp:19
int arg_types[16]
Definition chararead.cpp:28
int pos
Definition sysmes.hpp:13
char * data
Definition sysmes.hpp:11
int size
Definition sysmes.hpp:12