DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
script.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 440
4
5#include "title/script.hpp"
6
7#include <cstdlib>
8#include <cstring>
9
10#include "dataread.hpp"
11
12/* The command table the parser matches a line against, in the order it matches them: a name is
13 compared by the length the row carries rather than by its own, so a name that is another's prefix
14 has to stand in front of it ([title-script.md](../../docs/formats/title-script.md)). */
15CSCRIPT_COMMAND Command[TSC_COMMAND_COUNT] = {
16 {"OBJ_MOTION2", 11, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
17 {"OBJ_MOTION", 10, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, 0} },
18 {"OBJ_MOVE", 8, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
19 {"OBJ_TALK", 8, 7, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE}},
20 {"OBJ_EYE", 7, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
21 {"OBJ_MOUTH", 9, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
22 {"OBJ_DISP", 8, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
23 {"OBJ_STEP", 8, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
24 {"CAMERA", 6, 4, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
25 {"SE_STOP", 7, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
26 {"SE", 2, 4, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
27 {"SOUND_FADE", 10, 3, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
28 {"FADE_IN", 7, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
29 {"FADE_OUT", 8, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
30 {"MESSAGE", 7, 6, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
31 {"NEXT_MES", 8, 1, {TSARG_VALUE} },
32 {"MES_CLR", 7, 0, {0} },
33 {"LOAD_OBJ", 8, 2, {TSARG_VALUE, TSARG_COMMA_VALUE} },
34 {"SPRITE", 6, 1, {TSARG_VALUE} },
35 {"BOM", 3, 4, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
36 {"BEEM", 4, 6, {TSARG_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE, TSARG_COMMA_VALUE} },
37 {"SCENE_LOAD", 10, 1, {TSARG_VALUE} },
38 {"SCENE", 5, 1, {TSARG_VALUE} },
39 {"END", 3, 0, {0} },
40 {"WAIT_KEY", 8, 1, {TSARG_VALUE} },
41 {"WAIT", 4, 1, {TSARG_VALUE} },
42};
43
44/* The one script the overlay runs. It is 58112 bytes because the file is held whole inside it and
45 re-parsed from the cursor every tick. */
46class CScript CScript__2;
47
48/* Read the whole script in and put every actor back to a state nothing has asked anything of yet.
49 Nothing is reset when the load fails, so a missing file leaves the previous script's state
50 standing rather than an empty one. */
51void CScript::Load(const char *name) {
52 int i;
53
54 p = data;
55
56 if (LoadFile((char *) name, p, &size)) {
57 wait = 0;
58 mes_no = 0;
59 mes_page_wait = 40;
60 pos = 0;
61 camera_start = -1;
62 camera_no = -1;
63 fade = 0;
64 sprite = 0;
65 end = 0;
66 scene = 0;
67 load_no = -1;
68 mes_wait = false;
69 bom_no = 0;
70 beem_no = 0;
71 reset_flag = 0;
72 se_no = 0;
73 se_voice = 0;
74 se_stop = false;
75 bgm_fade = 0;
76
77 for (i = 0; i < 128; i++) {
78 arg[i] = 0;
79 }
80
81 for (i = 0; i < 23; i++) {
82 obj[i].disp = true;
83 obj[i].motion = 0;
84 obj[i].motion_end = -1;
85 obj[i].move = 0;
86 obj[i].move_req = false;
87 obj[i].eye = 0;
88 obj[i].eye_time = 0;
89 obj[i].mouth = 0;
90 obj[i].mouth_time = 0;
91 obj[i].talk = false;
92 obj[i].load = -1;
93 obj[i].load_step = TSLOAD_NONE;
94 obj[i].step = 0.05f;
95 }
96 }
97}
98
99/* One tick of the script: nothing happens while the wait is still running, and when it runs out
100 the parser reads one command after another out of the text until it reaches one that ends the
101 tick. A line matching no row is fatal - there is no way to skip it, because nothing in the format
102 says where a line ends. */
104 bool unmatched;
105 bool done;
106 int i;
107
108 if (this->end) {
109 return;
110 }
111
112 if (mes_wait) {
113 wait = 1.0f + motion_step;
114 }
115
116 if (wait < 1.0f) {
117 done = false;
118 pos = SkipSpace(p, pos);
119
120 do {
121 unmatched = true;
122
123 for (i = 0; i < TSC_COMMAND_COUNT; i++) {
124 if (memcmp(&p[pos], Command[i].name, Command[i].length) == 0) {
125 unmatched = false;
126 pos = SkipSpace(p, pos + Command[i].length);
127 pos = CheckScript(p, pos, &Command[i], i);
128
129 if (pos != -1) {
130 if (i == TSC_END || i == TSC_WAIT_KEY || i == TSC_WAIT) {
131 done = true;
132 }
133 }
134
135 break;
136 }
137 }
138
139 if (unmatched) {
140 exit__2(-1);
141 }
142 } while (!done);
143 } else {
145 }
146}
147
148/* One command, once its arguments have been read: every case is a write into the object and
149 nothing acts on it here, so a command is a request the overlay's own per-tick code picks up
150 ([title-script.md](../../docs/formats/title-script.md)). */
151int CScript::CheckScript(char *buffer, int position, CSCRIPT_COMMAND *command, int command_no) {
152 int cursor;
153
154 cursor = position;
155
156 if (command->arg_count) {
157 cursor = CheckArg(buffer, cursor, command);
158 }
159
160 switch (command_no) {
161 case TSC_OBJ_MOTION2:
162 obj[(int) arg[0]].motion = (int) arg[1];
163 obj[(int) arg[0]].motion_end = (int) arg[2];
164 break;
165
166 case TSC_OBJ_MOTION:
167 obj[(int) arg[0]].motion = (int) arg[1];
168 obj[(int) arg[0]].motion_end = -1;
169 break;
170
171 case TSC_OBJ_MOVE:
172 obj[(int) arg[0]].move = (int) arg[1];
173 obj[(int) arg[0]].move_req = true;
174 break;
175
176 case TSC_OBJ_TALK:
177 obj[(int) arg[0]].talk = true;
178 mes_no = (int) arg[1];
179 mes_timer = arg[2];
180 obj[(int) arg[0]].mouth_time = arg[2];
181 mes_x = (int) arg[3];
182 mes_y = (int) arg[4];
183 mes_talker = (int) arg[0];
184 mes_tail_x = (int) arg[5];
185 mes_tail_y = (int) arg[6];
186 break;
187
188 case TSC_OBJ_EYE:
189 obj[(int) arg[0]].eye = (char) arg[1];
190 obj[(int) arg[0]].eye_time = arg[2];
191 break;
192
193 case TSC_OBJ_MOUTH:
194 obj[(int) arg[0]].mouth = (char) arg[1];
195 obj[(int) arg[0]].mouth_time = arg[2];
196 break;
197
198 case TSC_OBJ_DISP:
199 if (arg[1] == 1.0f) {
200 obj[(int) arg[0]].disp = true;
201 } else {
202 obj[(int) arg[0]].disp = false;
203 }
204
205 break;
206
207 case TSC_OBJ_STEP:
208 obj[(int) arg[0]].step = arg[1];
209 break;
210
211 case TSC_CAMERA:
213 camera_start = (int) arg[0];
214 motion_start = (int) arg[1];
215 motion_end = (int) arg[2];
216#ifdef PAL_TIMING
217 motion_step = 1.2f * arg[3];
218#else
219 motion_step = arg[3];
220#endif
221 motion_req = true;
222 break;
223
224 case TSC_SE_STOP:
225 se_kind = (int) arg[0];
226 se_no = (int) arg[1];
227 se_voice = (int) arg[2];
228 se_stop = true;
229 break;
230
231 case TSC_SE:
232 se_kind = (int) arg[0];
233 se_no = (int) arg[1];
234 se_voice = (int) arg[2];
235 se_fade_time = (int) arg[3];
236 break;
237
238 case TSC_SOUND_FADE:
239 se_kind = (int) arg[0];
240 se_fade_time = (int) arg[1];
241 bgm_fade = (int) arg[2];
242 break;
243
244 case TSC_FADE_IN:
245 if (arg[0] == 0.0f) {
246 fade = TSFADE_IN_BLACK;
247 } else {
248 fade = TSFADE_IN_WHITE;
249 }
250
251 fade_speed = arg[1];
252 break;
253
254 case TSC_FADE_OUT:
255 if (arg[0] == 0.0f) {
256 fade = TSFADE_OUT_BLACK;
257 } else {
258 fade = TSFADE_OUT_WHITE;
259 }
260
261 fade_speed = arg[1];
262 break;
263
264 case TSC_MESSAGE:
265 mes_no = (int) arg[0];
266 mes_timer = arg[1];
267 mes_x = (int) arg[2];
268 mes_y = (int) arg[3];
269 mes_talker = -1;
270 mes_tail_x = (int) arg[4];
271 mes_tail_y = (int) arg[5];
272 break;
273
274 case TSC_NEXT_MES:
275 mes_page_wait = (int) arg[0];
276 break;
277
278 case TSC_MES_CLR:
279 mes_no = 0;
280 break;
281
282 case TSC_LOAD_OBJ:
283 obj[(int) arg[1]].load = (int) arg[0];
284 obj[(int) arg[1]].load_step = TSLOAD_START;
285 break;
286
287 case TSC_SPRITE:
288 sprite = (char) arg[0];
289 break;
290
291 case TSC_BOM:
292 bom_no++;
293
294 if (bom_no > 2) {
295 bom_no = 0;
296 }
297
298 bom_pos[bom_no][0] = arg[0];
299 bom_pos[bom_no][1] = arg[1];
300 bom_pos[bom_no][2] = arg[2];
301 bom_size[bom_no] = arg[3];
302 bom_req = true;
303 break;
304
305 case TSC_BEEM:
306 beem_no++;
307
308 if (beem_no > 2) {
309 beem_no = 0;
310 }
311
312 if (arg[0] == -1.0f) {
313 beem_end = true;
314 } else {
315 beem_end = false;
316 beem_from[beem_no][0] = arg[0];
317 beem_from[beem_no][1] = arg[1];
318 beem_from[beem_no][2] = arg[2];
319 beem_to[beem_no][0] = arg[3];
320 beem_to[beem_no][1] = arg[4];
321 beem_to[beem_no][2] = arg[5];
322 }
323
324 beem_req = true;
325 break;
326
327 case TSC_SCENE_LOAD:
328 load_no = (int) arg[0];
329 break;
330
331 case TSC_SCENE:
332 scene = (int) arg[0];
333 init_no = (int) arg[0];
334 break;
335
336 case TSC_WAIT:
337 wait = arg[0] - 1.0f;
338 break;
339
340 case TSC_WAIT_KEY:
341 mes_wait = (int) (1.0f + arg[0]);
342 break;
343
344 case TSC_END:
345 end = 1;
346 break;
347 }
348
349 return cursor;
350}
351
352/* One command's arguments. Both kinds read the same three forms and differ only in what stands
353 before them: a kind-1 argument must be preceded by a comma and a kind-2 one stands where it is.
354 Anything the three forms do not cover hands back -1, which the caller stores and then parses
355 from ([title-script.md](../../docs/formats/title-script.md)). */
356int CScript::CheckArg(char *buffer, int position, CSCRIPT_COMMAND *command) {
357 int cursor;
358 int i;
359 int digit_count;
360 int accepted;
361
362 cursor = position;
363
364 for (i = 0; i < command->arg_count; i++) {
365 switch (command->arg_type[i]) {
366 case TSARG_COMMA_VALUE:
367 if (buffer[cursor] != ',') {
368 return -1;
369 }
370
371 cursor = SkipSpace(buffer, cursor + 1);
372
373 if (memcmp(&buffer[cursor], "ON", 2) == 0) {
374 arg[i] = 1.0f;
375 cursor += 2;
376 } else if (memcmp(&buffer[cursor], "OFF", 3) == 0) {
377 arg[i] = 0;
378 cursor += 3;
379 } else {
380 accepted = 0;
381
382 if (buffer[cursor] == '-') {
383 accepted = 1;
384 }
385
386 if (buffer[cursor] >= '0' && buffer[cursor] <= '9') {
387 accepted = 1;
388 }
389
390 if (!accepted) {
391 return -1;
392 }
393
394 arg[i] = (float) atof(&buffer[cursor]);
395
396 for (digit_count = 0; digit_count < 32; digit_count++) {
397 accepted = 0;
398
399 if (buffer[cursor] == '-') {
400 cursor++;
401 accepted = 1;
402 }
403
404 if (buffer[cursor] >= '0' && buffer[cursor] <= '9') {
405 cursor++;
406 accepted = 1;
407 }
408
409 if (buffer[cursor] == '.') {
410 cursor++;
411 accepted = 1;
412 }
413
414 if (!accepted) {
415 break;
416 }
417 }
418
419 if (digit_count == 32) {
420 return -1;
421 }
422 }
423
424 cursor = SkipSpace(buffer, cursor);
425 break;
426
427 case TSARG_VALUE:
428 if (memcmp(&buffer[cursor], "ON", 2) == 0) {
429 arg[i] = 1.0f;
430 cursor += 2;
431 } else if (memcmp(&buffer[cursor], "OFF", 3) == 0) {
432 arg[i] = 0;
433 cursor += 3;
434 } else {
435 accepted = 0;
436
437 if (buffer[cursor] == '-') {
438 accepted = 1;
439 }
440
441 if (buffer[cursor] >= '0' && buffer[cursor] <= '9') {
442 accepted = 1;
443 }
444
445 if (!accepted) {
446 return -1;
447 }
448
449 arg[i] = (float) atof(&buffer[cursor]);
450
451 for (digit_count = 0; digit_count < 32; digit_count++) {
452 accepted = 0;
453
454 if (buffer[cursor] == '-') {
455 cursor++;
456 accepted = 1;
457 }
458
459 if (buffer[cursor] >= '0' && buffer[cursor] <= '9') {
460 cursor++;
461 accepted = 1;
462 }
463
464 if (buffer[cursor] == '.') {
465 cursor++;
466 accepted = 1;
467 }
468
469 if (!accepted) {
470 break;
471 }
472 }
473
474 if (digit_count == 32) {
475 return -1;
476 }
477 }
478
479 cursor = SkipSpace(buffer, cursor);
480 break;
481 }
482 }
483
484 return cursor;
485}
486
487/* The run of separators standing before a token. Two of the five consume more than the byte they
488 are found cursor - a comment runs to its line ending inclusive and the ideographic space is two bytes -
489 which is why the skip is a loop over the whole file rather than a walk over one kind of byte
490 ([title-script.md](../../docs/formats/title-script.md)). */
491int CScript::SkipSpace(char *buffer, int position) {
492 bool stop;
493
494 while (position < size) {
495 stop = true;
496
497 if (memcmp(&buffer[position], "\x81\x40", 2) == 0) {
498 position++;
499 stop = false;
500 }
501
502 if (buffer[position] == ' ') {
503 stop = false;
504 }
505
506 if (buffer[position] == '\t') {
507 stop = false;
508 }
509
510 if (buffer[position] == '\n') {
511 position++;
512 stop = false;
513 }
514
515 if (buffer[position] == '\r') {
516 position++;
517 stop = false;
518 }
519
520 if (memcmp(&buffer[position], "//", 2) == 0) {
521 while (buffer[position] != '\n' && buffer[position] != '\r') {
522 position++;
523 }
524
525 position++;
526 stop = false;
527 }
528
529 if (stop) {
530 return position;
531 }
532
533 position++;
534 }
535
536 return size;
537}
SCRIPT_OBJ obj[23]
Definition script.hpp:149
int mes_y
Definition script.hpp:158
int mes_tail_y
Definition script.hpp:161
int bgm_fade
Definition script.hpp:180
sceVu0FVECTOR beem_to[3]
Definition script.hpp:174
int mes_page_wait
Definition script.hpp:162
float motion_step
Definition script.hpp:152
int load_no
Definition script.hpp:143
sceVu0FVECTOR bom_pos[3]
Definition script.hpp:167
sceVu0FVECTOR beem_from[3]
Definition script.hpp:173
void Load(const char *name)
Definition script.cpp:51
int pos
Definition script.hpp:185
int mes_talker
Definition script.hpp:159
int camera_no
Definition script.hpp:136
float wait
Definition script.hpp:146
int mes_x
Definition script.hpp:157
int mes_tail_x
Definition script.hpp:160
int se_no
Definition script.hpp:176
float arg[128]
Definition script.hpp:186
int fade
Definition script.hpp:137
int scene
Definition script.hpp:142
int motion_end
Definition script.hpp:151
int beem_no
Definition script.hpp:169
int sprite
Definition script.hpp:141
int motion_start
Definition script.hpp:150
u8 motion_req
Definition script.hpp:153
u8 beem_req
Definition script.hpp:170
u8 bom_req
Definition script.hpp:165
int se_kind
Definition script.hpp:175
float mes_timer
Definition script.hpp:156
int size
Definition script.hpp:181
int se_voice
Definition script.hpp:177
int mes_no
Definition script.hpp:155
int CheckScript(char *buffer, int position, CSCRIPT_COMMAND *command, int command_no)
Definition script.cpp:151
int CheckArg(char *buffer, int position, CSCRIPT_COMMAND *command)
Definition script.cpp:356
int bom_no
Definition script.hpp:164
float bom_size[4]
Definition script.hpp:168
int mes_wait
Definition script.hpp:145
int camera_start
Definition script.hpp:135
u8 beem_end
Definition script.hpp:171
float fade_speed
Definition script.hpp:138
char * p
Definition script.hpp:184
u8 reset_flag
Definition script.hpp:147
char data[56000]
Definition script.hpp:183
int SkipSpace(char *buffer, int position)
Definition script.cpp:491
int se_fade_time
Definition script.hpp:178
int se_stop
Definition script.hpp:179
int init_no
Definition script.hpp:144
void Step()
Definition script.cpp:103
int arg_type[8]
Definition script.hpp:101
float step
Definition script.hpp:125
float eye_time
Definition script.hpp:118
int motion_end
Definition script.hpp:113
int load_step
Definition script.hpp:124
float mouth_time
Definition script.hpp:120