DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
main.cpp
1// C imports
2#include <libcdvd.h>
3#include <libmc.h>
4#include <sifdev.h>
5#include <sifrpc.h>
6
7#include <cstdio>
8#include <cstring>
9
10// Our imports
11#include "btsysscript.hpp"
12#include "character.hpp"
13#include "clsmes.hpp"
14#include "dataalloc.hpp"
15#include "dataread.hpp"
16#include "dataset.hpp"
17#include "debugfont.hpp"
18#include "dungeonmap.hpp"
19#include "editloop.hpp"
20#include "fakepointlight.hpp"
21#include "frame.hpp"
22#include "gamemode.hpp"
23#include "gamepad.hpp"
24#include "gameutil.hpp"
25#include "hitmark.hpp"
26#include "literals.hpp"
27#include "main.hpp"
28#include "mainselect.hpp"
29#include "mathutil.hpp"
30#include "memcard.hpp"
31#include "menu_draw.hpp"
32#include "menu_save.hpp"
33#include "mglib.hpp"
34#include "object.hpp"
35#include "rect.hpp"
36#include "runscript.hpp"
37#include "savedata.hpp"
38#include "shot_effect.hpp"
39#include "snd.hpp"
40#include "sound.hpp"
41#include "sysmes.hpp"
42#include "texture.hpp"
43#include "textureanime.hpp"
44#include "title/bombeffect.hpp"
45#include "title/majinbeem.hpp"
46#include "visualvu1.hpp"
47#include "vudata.hpp"
48#include "vutext.hpp"
49#include "water.hpp"
50
54// clang-format off
55enum MemCheckMode {
56 MEM_CHECK_CHECK = 0,
57 MEM_CHECK_UNK_1 = 1,
58 MEM_CHECK_NO_CARD = 2,
59 MEM_CHECK_NO_SPACE = 3,
60 MEM_CHECK_DONE = 4,
61};
62
63// clang-format on
64
69public:
70 float lod[4];
71 int lowest;
72 int highest;
82
90 void Initialize();
91};
92
93STATIC_ASSERT(sizeof(CategoryAttr) == 0x18);
94
95#pragma helper_mask_gpr 0x30
96#pragma helper_mask_fpr 0x1000
97#pragma name_counter 873
98
99/* Global, GLOBAL-linkage per retail `nm` (0x1cbc9b0, `T`). Purpose beyond
100 * being constructed here is unanalyzed -- out of scope for this pass. */
101CRunScript RunScript;
102
103/* Retail `nm`: 0x1cbca00, LOCAL. Declared GLOBAL here because
104 * LoadSystemMessage__Fv is still a raw .s and references it externally,
105 * which a `static` in this object cannot satisfy. That costs symbol binding
106 * only, not code bytes; restore `static` once it lands in this TU. */
107CDataAlloc<1, 6000> SystemMesBuffer;
108
109/* The global save-data instance and the pointer used to reach it. Retail's
110 * symbol table:
111 * 2433: 01cd4140 78272 OBJECT LOCAL 4 save_data
112 * 11183: 002a250c 4 OBJECT GLOBAL 4 SaveData
113 * 78272 is exactly sizeof(CSaveData). `save_data` is LOCAL, so it has to
114 * share a translation unit with whatever assigns `SaveData = &save_data;` --
115 * main(), below. `SaveData` is unmangled and sits in main.sbss at gp-0x72E4
116 * from _gp = 0x2A97F0, confirmed against the `lw $4,-0x72E4($28)` preamble of
117 * every CSaveData call in ref/asm/sections/dun/dun.text.s. */
118static CSaveData save_data __attribute__((aligned(64)));
119CSaveData *SaveData;
120
121/* main()'s own top-level game-state globals -- moved here from
122 * ps2/src/mainselect.cpp/ps2/include/mainselect.hpp for the same reason as `pBound` etc. above:
123 * retail's `main.sbss` interleaves these (GLOBAL linkage) with `SaveData`
124 * just above and `mode`/etc. just below, all in one contiguous run
125 * (0x2a2510-0x2a2548) that must come from ONE compiled object. Still
126 * *declared* `extern` in ps2/include/mainselect.hpp (in case another, not-yet-
127 * decompiled TU needs them), just no longer *defined* there. Declaration
128 * order here matches retail's exact main.sbss address order. */
129s32 GameClearFlag;
130s32 main_select_menu_no;
131s32 MapNo;
132s32 OldMapNo;
133s32 LocalMapNo;
134s32 StartEventNo;
135short *SystemMes;
136CSound CSnd;
137s32 main_select_padrup;
138
139/* Scratch buffer for the "reset trial character names, keep the rest"
140 * round-trip below. Retail has it as a real named static rather than a stack
141 * local: `readelf -s` gives `config_data`, 64 bytes (= sizeof(SV_CONFIG_SYS)),
142 * LOCAL, main.bss, and retail allocates no stack for it.
143 *
144 * Declared before `mainCDataAlloc2` to match retail's .bss order
145 * (config_data 0x1ce7300, mainCDataAlloc2 0x1ce7560). It is a POD with no
146 * constructor, so this does not disturb sinit order. */
147static SV_CONFIG_SYS config_data;
148
149/* Unidentified retail global: `nm` DebugFont, 0x1ce7340, LOCAL, 0x21C bytes.
150 * A same-sized stand-in is needed or this object's .bss runs 0x220 short and
151 * everything after it lands early. The 4 bytes before mainCDataAlloc2 at
152 * 0x1ce7560 are the linker's alignment pad, not part of the object --
153 * declaring 0x220 made the symbol four bytes longer than retail's.
154 * TODO: identify the real type; the name suggests a font glyph table.
155 *
156 * GLOBAL rather than retail's LOCAL for the same reason as SystemMesBuffer:
157 * MenuLoop__Fv is still a raw .s and references it. */
158CDebugFont DebugFont;
159
160/* Global, GLOBAL linkage per retail `nm` (0x1ce7560, `T`). Declared last,
161 * after `save_data`/`SaveData`, since retail's sinit thunk constructs it
162 * fourth (`SaveData` itself is a plain pointer with no constructor, so it
163 * doesn't participate in sinit ordering either way). */
164CDataAlloc2<1> mainCDataAlloc2;
165
166/* Everything below is called from main() but not itself decompiled here
167 * (out of scope for this pass -- each is its own subsystem). Declared with
168 * their exact retail mangled/symbol names so the linker resolves them
169 * against the existing reassembled .s files. Functions with no retained
170 * retail name use this project's "func_ADDRESS" convention. */
171extern "C" {
172void init_all__Fv();
173void initialize_data__Fv();
174void GlobalNameInit__Fv();
175void InitReadBG__Fv();
176void SndInit__Fv();
177void LoadOverlay__Fi(int mode);
178void MGSetRenderInfo__Ffff(float a1, float a2, float a3);
179void FlushCache(int mode);
180void init_now_loading__Fi(int map_no);
181void LoadSystemMessage__Fv();
182void SndInitialize__Fiiii(int a1, int a2, int a3, int a4);
183int InitExistData__Fv();
184void MapJump__Fii(int map_no, int event_no);
185void EditInit__FPv(void *param);
186void func_01DAF1C0();
187void MenuInit__Fv(int mode);
188void func_01DC1420(int mode);
189void MemCheckInit__Fv(int mode);
190void TrialEndInit__Fv(int mode);
191void InitSave__Fv(int mode);
192void LangsetInit__Fv(int mode);
193void func_01DAC1C0();
194void func_01DD1AB0(int inited);
195void func_01DC8C50();
196/* Takes no arguments -- the mangled name is __Fv, and retail's call sites
197 * (the `while (check_now_loading...) ` spin loop at 0x1415C0) set up no
198 * argument registers at all. An earlier pass of this file passed a saved
199 * copy of `mode` here, which forced an extra callee-saved register copy
200 * (`paddub s4,a0`) retail doesn't have. */
201int check_now_loading__Fv();
202void MGInitVSyncCallBack__FPFi_i(void (*callback)(int));
203void PlayTimeCount__Fi(int add);
204void MGBeginFrame__Fv();
205/* Retail passes Vif1Packet as the (pointer-typed, per the mangling) first
206 * argument: `lw $4,Vif1Packet; paddub $20,$4; jal SetEnv...` -- the load
207 * goes straight into $a0 and is copied to $s4 only to survive the call for
208 * reuse by sceVif1PkCall below. An earlier pass declared this () and made
209 * the copy direction come out backwards (lw s4 then paddub a0,s4). */
210void SetEnv__FP13sceVif1Packet(sceVif1Packet *vif1_packet);
211int func_01DD2220();
212int func_01DC8EB0();
213int EditLoop__Fv();
214int func_01DAF970();
215int MenuLoop__Fv();
216int func_01DC1510();
217int MemCheckLoop__Fv();
218int TrialEndLoop__Fv();
219int LoopSave__Fv();
220int LangsetLoop__Fv();
221int func_01DAD980(int mode);
222void MGEndFrame__Fv();
223int CheckTrialEnd__Fv();
224/* No arguments (mangled __Fv); retail's spin loop at 0x141D10 sets up no
225 * argument registers. An earlier pass passed `d2` here -- `d2` is in fact
226 * never referenced by retail main() at all (no -0x72B4($gp) access
227 * anywhere in ref/asm/split/main/main.s). */
228int ReadBGSync__Fv();
229void TrialStart__Fv();
230}
231
232/* File-scope statics of the original main translation unit. Retail has them
233 * unsuffixed and LOCAL (`nm`: mode, d1, d2, d8, mc_mode, in that main.sbss
234 * address order, 0x2a2534-0x2a2548), so file-scope `static` rather than
235 * function-local statics, which would get a `$NNN` suffix. No raw .s
236 * references them by gp-offset, so real `static` works. `d2`/`d8` are never
237 * touched by main() but must still be defined, so this object's .sbss covers
238 * their retail byte range. See re/ai/build_pipeline.md. */
240s32 mode;
241sceDmaChan *d1;
242sceDmaChan *d2;
243sceDmaChan *d8;
245s32 mc_mode;
246
247/* `PolyCount` (GLOBAL linkage) immediately follows `mc_mode` in retail's
248 * main.sbss (0x2a2548) -- moved here from ps2/src/mainselect.cpp for the same reason
249 * as `GameClearFlag` etc. above. Still declared `extern` in
250 * include/mainselect.hpp. */
251s32 PolyCount;
252
256s32 NextMapNo = -1;
257
259static int play_time_count = 1;
260
261/* Low byte of an EE DMA channel's CHCR, as a bitfield. The "kick this
262 * channel" write in main() is a 1-bit field assignment in the original
263 * source, not hand masking: only that form makes MWCC materialise the OR
264 * operand with `daddiu $3,$0,0x40` -- its bitfield engine works in 64-bit --
265 * instead of folding it into an `ori`. The same lbu/daddiu/li/and/or/sb
266 * sequence appears twice in MGEndFrame against DmaCH8. */
267typedef struct {
268 u8 chcr_low : 6; /* DIR/MOD/ASP/TTE etc. -- untouched here */
269 u8 str : 1; /* channel start/busy */
270 u8 chcr_top : 1;
272
274static char *binfile[15] = {"TITLE.BIN", "TITLE.BIN", "", "DUN.BIN", "DUN.BIN",
275 "TITLE.BIN", "", "", "DUN.BIN", "DUN.BIN",
276 "", "", "", "", ""};
277
279static char now_binfile[128] = "";
280
281void LoadOverlay(int mode) {
282 if (binfile[mode][0] == '\0') {
283 return;
284 }
285
286 if (strcmp(binfile[mode], now_binfile) == 0) {
287 return;
288 }
289
290 strcpy(now_binfile, binfile[mode]);
291 void *address = _overlay_group_addresses[1];
292 char path[128] = "cdrom0:\\";
293 strcat(path, binfile[mode]);
294 strcat(path, ";1");
295 mwLoadOverlay(path, address);
296}
297
298void init_all() {
299 sceSifInitRpc(0);
300 sceCdInit(0);
301 sceCdMmode(2);
302
303 while (!sceSifRebootIop("cdrom0:\\MODULES\\IOPRP211.IMG;1")) {
304 }
305
306 while (!sceSifSyncIop()) {
307 }
308
309 sceSifInitRpc(0);
310 sceCdInit(0);
311 sceCdMmode(2);
312 sceFsReset();
313
314 while (sceSifLoadModule("cdrom0:\\MODULES\\SIO2MAN.IRX;1", 0, 0) < 0) {
315 }
316
317 while (sceSifLoadModule("cdrom0:\\MODULES\\PADMAN.IRX;1", 0, 0) < 0) {
318 }
319
320 while (sceSifLoadModule("cdrom0:\\MODULES\\MCMAN.IRX;1", 0, 0) < 0) {
321 }
322
323 while (sceSifLoadModule("cdrom0:\\MODULES\\MCSERV.IRX;1", 0, 0) < 0) {
324 }
325
326 while (sceSifLoadModule("cdrom0:\\MODULES\\LIBSD.IRX;1", 0, 0) < 0) {
327 }
328
329 while (sceSifLoadModule("cdrom0:\\MODULES\\SDRDRV.IRX;1", 0, 0) < 0) {
330 }
331
332 while (sceSifLoadModule("cdrom0:\\MODULES\\MODMIDI.IRX;1", 0, 0) < 0) {
333 }
334
335 while (sceSifLoadModule("cdrom0:\\MODULES\\MODHSYN.IRX;1", 0, 0) < 0) {
336 }
337
338 while (sceSifLoadModule("cdrom0:\\MODULES\\EZMIDI.IRX;1", 0, 0) < 0) {
339 }
340
341 InitCDFile();
342 DevInit();
343 d1 = sceDmaGetChan(1);
344 d2 = sceDmaGetChan(2);
345 d8 = sceDmaGetChan(8);
346 MGInit();
347 InitMemoryFile();
348 BufferAllClear();
349 InitReadBG();
350}
351
353sceVu0FMATRIX light = {
354 {0.578f, 0.0f, 0.0f, 0.0f},
355 {0.578f, 0.0f, 0.0f, 0.0f},
356 {0.578f, 0.0f, 0.0f, 0.0f},
357 {0.0f, 0.0f, 0.0f, 0.0f},
358};
359
361sceVu0FMATRIX lightcolor = {
362 {120.0f, 120.0f, 120.0f, 128.0f},
363 {0.0f, 0.0f, 0.0f, 0.0f },
364 {0.0f, 0.0f, 0.0f, 0.0f },
365 {0.0f, 0.0f, 0.0f, 0.0f },
366};
367
369sceVu0FVECTOR ambientlight = {64.0f, 64.0f, 64.0f, 128.0f};
370
371void SetEnv(sceVif1Packet *packet) {
372 sceVif1PkCnt(packet, 0);
373 sceVif1PkOpenDirectCode(packet, 0);
374 sceVif1PkOpenGifTag(packet, *(u_long128 *) &GiftagAD);
375 sceVif1PkAddGsAD(packet, SCE_GS_TEX1_1, *(u_long *) &mgTEX1Env);
376 sceVif1PkAddGsAD(packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
377 sceVif1PkAddGsAD(packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
378 sceVif1PkAddGsAD(packet, SCE_GS_ALPHA_1, *(u_long *) &mgAlpha);
379 sceVif1PkAddGsAD(packet, SCE_GS_CLAMP_1, 5);
380 sceVif1PkCloseGifTag(packet);
381 sceVif1PkCloseDirectCode(packet);
382}
383
384void LoadSndTxt() {
385 u_char work[48000];
386 u_char *buffer = work;
387 int offset = (int) buffer % 64;
388
389 if (offset) {
390 buffer += 64 - offset;
391 }
392
393 CSnd.LoadSeInf("sound/tbl/setbl.txt", (u_int *) buffer);
394 CSnd.LoadSqInf("sound/tbl/sqtbl.txt", (u_int *) buffer);
395 SndInitSeTable();
396}
397
398void PlayTimeCountFlag(int flag) {
399 play_time_count = flag;
400}
401
402int PlayTimeCount(int unused) {
403 if (play_time_count) {
404 SaveData->AddPlayTime(1);
405 }
406 PORT_RETURN(play_time_count);
407}
408
409void LoadSystemMessage() {
410 int size;
411 SystemMesBuffer.used = 0;
412 SystemMesBuffer.Align64();
413 SystemMes = (short *) &SystemMesBuffer.block[SystemMesBuffer.used];
414 char name[64] = "meswin/system";
415
416 if (LanguageCode > LANG_JAPANESE) {
417 sprintf(name, "meswin/system_%d", LanguageCode);
418 }
419
420 strcat(name, ".mes");
421
422 if (!LoadFile2(name, SystemMes, &size, 0)) {
423 LoadFile("meswin/systeme.bin", SystemMes, &size);
424 }
425
426 SystemMesBuffer.Alloc((size >> 4) + 1);
427 InitSystemMes();
428}
429
430void initialize_data() {
431 LoadSystemMessage();
432 BtMapJumpFloor = -1;
433 LoadSndTxt();
434}
435
436/* @ 0x1410B0 (0xC80 bytes) -- main */
437int main(int argc, const char **argv, const char **envp) {
438 /* Function-local statics: retail names ("depth$682", "init$683", ...)
439 * confirmed via IDA's global list against this exact function; the
440 * "$NNN" suffix is MWCC's disambiguation counter for function-local
441 * statics (see CLAUDE.md), so these belong here, not in mainselect.hpp. */
442 /* `bool` compiles to an unsigned byte load (`lbu`) for these checks;
443 * retail uses a signed load (`lb`), confirmed by diffing against
444 * retail -- so these are declared `s8`, not `bool`, matching every
445 * other signed-byte flag in this codebase (see CLAUDE.md on `s8`
446 * being plain `char`, signed by default on this target). */
447 static s32 depth;
448 static s8 init;
449 static s32 wt;
450 static s8 init2;
451 static s32 init_flag;
452 static s8 init3;
453
454 /* Locals in retail's callee-saved order; MWCC assigns s0,s1,... to
455 * top-level locals in declaration order:
456 * loop_result -> s0 per-iteration loop-exit result
457 * skip_title -> s1 "skip the title demo" flag, set in the first switch's
458 * case 1 and consumed in the second's
459 * i -> s2 warmup counter, reused as the case-0 once-flag
460 * ($18 in retail for both)
461 * exist_data -> s3 InitExistData() result
462 * j -> s4 chara-name copy counter, sharing its register with the
463 * other short-lived temporaries as retail does with $20
464 * idle_result -> s5 never written; retail reads an uninitialised
465 * callee-saved register in the second switch's case 12,
466 * so this is declared last to take the last slot
467 */
468 int loop_result;
469 int skip_title;
470 int i;
471 int exist_data;
472 int j;
473 int idle_result;
474 char chara_names[6][64];
475 int in_range;
476
477 mwInit(argc, argv, envp);
478#ifdef PAL
479 DebugMode = 0;
480#endif
481
482 if (!init) {
483 depth = 0;
484 init = true;
485 }
486
487 if (!init2) {
488 wt = 1;
489 init2 = true;
490 }
491
492 /* Plain statements, not declaration initializers -- retail zeroes
493 * s0/s1 here, *after* the two static-init blocks above (an
494 * initializer at declaration gets scheduled before the mwInit call
495 * instead). */
496 loop_result = 0;
497 skip_title = 0;
498
499 mode = GAME_MODE_MENU;
500 main_select_menu_no = 0;
501
502 strcpy(main_select_param, "e01");
503 init_all__Fv();
504
505 while (sceGsSyncV(0) == 0) {
506 }
507
508 GamePad.Init();
509
510 SaveData = &save_data;
511 NextMapNo = -1;
512 MapNo = -1;
513 OldMapNo = -1;
514 StartEventNo = -1;
515 GameClearFlag = 0;
516
517 memset(&save_data, 0, sizeof(CSaveData));
518 save_data.Initialize();
519 GlobalNameInit__Fv();
520
521 /* `(in_range = ...) != 0` rather than a bare `i < 60`: the anonymous
522 * form routes the slti result through $at, the named form through a
523 * real register ($v0, as retail has) -- see CLAUDE.md's named-
524 * variable register trick, applied throughout ps2/src/savedata.cpp. */
525 for (i = 0; (in_range = i < 60) != 0; i++) {
526 sceGsSyncV(0);
527 GamePad.UpDate();
528
529#ifdef PAL
530 /* Holding four buttons through the first second turns on debug mode. */
531 if (GamePad.On2(PAD_R1) != 0 && GamePad.On2(PAD_R2) != 0 && GamePad.On2(PAD_L1) != 0 && GamePad.On2(PAD_L2) != 0) {
532 DebugMode = 1;
533 }
534#else
535 if (GamePad.On2(PAD_R1) != 0 && GamePad.On2(PAD_R2) != 0 && GamePad.On2(PAD_L1) != 0) {
536 GamePad.On2(PAD_L2);
537 }
538#endif
539 }
540
541#ifdef PAL
542 /* Debug mode starts at the developer menu instead of the language select. */
543 if (!DebugMode) {
544 MapNo = -1;
545 mode = GAME_MODE_LANGUAGE;
546 GamePad.KeyLock2(1);
547 }
548#else
549 MapNo = -1;
551 GamePad.KeyLock2(1);
552#endif
553
554 i = 0;
555 exist_data = 0;
556
557 if (!init3) {
558 init_flag = 0;
559 init3 = true;
560 }
561
562 while (1) {
563 if (mode != GAME_MODE_UNUSED_12 && init_flag == 0) {
564 initialize_data__Fv();
565 init_flag = 1;
566 }
567
568 InitReadBG__Fv();
569 SndInit__Fv();
570
571#ifdef PAL
572 if (!DebugMode && mode == GAME_MODE_MENU) {
573#else
574 if (mode == GAME_MODE_MENU) {
575#endif
576 MapNo = 801;
578 }
579
580 LoadOverlay__Fi(mode);
581 /* The third argument is the int 65535 (0xFFFF) converted to float
582 * at runtime -- retail: `ori $2,$0,0xFFFF; mtc1 $2,$f0; cvt.s.w
583 * $f14,$f0` -- not a compile-time float constant (an earlier pass
584 * had `(float)-1`, which the front end folds to a `lui 0xbf80`
585 * -1.0f immediate). */
586 MGSetRenderInfo__Ffff(800.0f, 10.0f, 65535);
587 sceGsSyncPath(0, 0);
588
589 /* A `switch`, not an `||` chain: as a chain MWCC merges 9 and 10
590 * into a range check (`addiu v0,v1,-9; sltiu at,v0,2`), which retail
591 * does not have. MWCC emits a sparse switch's comparisons in reverse
592 * written order, so these are written 9,7,10,14 to get retail's
593 * 14,10,7,9. */
594 switch (mode) {
595 case GAME_MODE_LOADER:
596 case GAME_MODE_MENU:
599 MapNo = -1;
600 OldMapNo = -1;
601 break;
602 }
603
604 FlushCache(0);
605 sceDmaSend(d1, My_dma_start0);
606 sceGsSyncPath(0, 0);
607 FlushCache(0);
608 sceDmaSend(d1, Vu_progmain);
609 sceGsSyncPath(0, 0);
610
611 if (mode != GAME_MODE_UNUSED_12 && StartEventNo < 0) {
612 init_now_loading__Fi(MapNo);
613 }
614
615 GamePad.StopVibration();
616
617#ifdef PAL
618 if (DebugMode) {
619 LoadSystemMessage__Fv();
620 }
621#endif
622
623 /* Case bodies are in retail's physical body order rather than
624 * case-value order, taken from the @875 table in
625 * ref/asm/sections/main/main.rodata.s. MWCC lays bodies out in
626 * written order, and a jump table's indices are right regardless of
627 * that order while the bodies' own addresses are not. */
628 switch (mode) {
629 case GAME_MODE_LOADER:
630 func_01DC1420(mode);
631 break;
633 LangsetInit__Fv(mode);
634 break;
635 case GAME_MODE_TITLE:
636 LoadSystemMessage__Fv();
637 GlobalNameInit__Fv();
638 SndInitialize__Fiiii(4, 30, 4, 5);
639
640 /* `i` is the warmup counter reused as a "ran once" flag,
641 * $18 in retail for both. Retail stores GameClearFlag = 1
642 * here (gp-0x72E0), not main_select_menu_no (gp-0x72DC),
643 * and emits the 1 as a copy of the flag's own register
644 * (`paddub $2,$18`), which the literal reproduces via copy
645 * propagation. */
646 if (i == 0) {
647 i = 1;
648 exist_data = InitExistData__Fv();
649
650 if (*(s32 *) &((SV_CONFIG_SYS *) SaveData->GetConfigData())->game_clear_area[2] != 0) {
651 GameClearFlag = 1;
652 }
653 }
654
655 func_01DD1AB0(exist_data);
656 break;
658 SndInitialize__Fiiii(4, 30, 4, 5);
659 func_01DC8C50();
660
661 /* Sets skip_title (the "skip title demo" flag consumed by the
662 * second switch's case 1 below), not loop_result -- retail writes
663 * $17/s1 here, the same register case 1 below tests and
664 * clears. */
665 if (GamePad.On(PAD_START) != 0) {
666 MapJump__Fii(800, -1);
667 skip_title = 1;
668 }
669
670 break;
671 case GAME_MODE_EDIT:
672 EditInit__FPv(0);
673 break;
674 case GAME_MODE_MENU:
675 MenuInit__Fv(mode);
676 break;
677 case GAME_MODE_SAVE:
678 InitSave__Fv(mode);
679 break;
681 MemCheckInit__Fv(mode);
682 break;
684 TrialEndInit__Fv(mode);
685 break;
687 SndInitialize__Fiiii(4, 30, 4, 5);
688 func_01DAF1C0();
689 break;
693 break;
694 default:
695 func_01DAC1C0();
696 break;
697 }
698
699 NextMapNo = -1;
700
701 while (check_now_loading__Fv() == 0) {
702 }
703
704 MGInitVSyncCallBack__FPFi_i(PlayTimeCount__Fi);
705 FlushCache(0);
706 sceDmaSend(d1, My_dma_start0);
707 sceGsSyncPath(0, 0);
708 FlushCache(0);
709 sceDmaSend(d1, Vu_progmain);
710 sceGsSyncPath(0, 0);
711
712 *(volatile s32 *) 0x10000010 = 131;
713 sceGsSyncV(0);
714
715 /* Kick the DMA channel: set the STR bit via a volatile bitfield
716 * store (see DMA_CHCR_BYTE above for why this exact source form
717 * is required for the daddiu/li/and/or sequence to match). */
718 ((volatile DMA_CHCR_BYTE *) d1)->str = 1;
719 /* CSaveData::map_no is private, and retail reaches it by offset
720 * from main() -- a different translation unit -- so this is direct
721 * pointer arithmetic rather than a getter. Value and base both go
722 * through named locals, value first: the only form whose temp
723 * numbering matches retail (`lw v0,MapNo; lw v1,SaveData;
724 * sw v0,0x1c8(v1)`). See re/ai/main.md. */
725 {
726 s32 map_no = MapNo;
727 char *save_bytes = (char *) SaveData;
728 *(s32 *) (save_bytes + 0x1C8) = map_no;
729 }
730
731 do {
732#ifdef PAL
733 {
734 s32 *config_words = (s32 *) SaveData->GetConfigData();
735 MGAdjustScreen(config_words[12], config_words[13]);
736 }
737#endif
738 int game_clear = GameClearFlag;
739 *(s32 *) &((SV_CONFIG_SYS *) SaveData->GetConfigData())->game_clear_area[2] = game_clear;
740
741 *(volatile s32 *) 0x10000000 = 0;
742 MGBeginFrame__Fv();
743 /* Discarded read of the same hardware register just written --
744 * retail does this too (`lw v0,0(at)` with the result never
745 * used again), confirmed missing by diffing against retail. */
746 (void) *(volatile s32 *) 0x10000000;
747
748 /* Retail's shape (lw a0,Vif1Packet; paddub s4,a0; jal SetEnv;
749 * lui/addiu a1; paddub a0,s4; paddub a2; jal PkCall) needs all
750 * three of: SetEnv taking the global directly, so the load lands
751 * in $a0 and the local is a copy from it; a named local holding
752 * the pre-call value, since retail does not reload after SetEnv;
753 * and the cast on Vu_prog0f, which hoists the lui/addiu pair
754 * ahead of the first-argument copy, MWCC evaluating arguments
755 * strictly left to right. */
756 int vif1_packet = (int) Vif1Packet;
757 SetEnv__FP13sceVif1Packet(Vif1Packet);
758 sceVif1PkCall((sceVif1Packet *) vif1_packet, (u_long128 *) Vu_prog0f, 0);
759
760 /* gp-0x72A8 here is PolyCount, not mc_mode (gp-0x72AC) -- an
761 * earlier pass had the wrong global (off by one slot in the
762 * sbss layout; verified against `nm` addresses vs. gp =
763 * 0x2A97F0: PolyCount = 0x2A2548 = gp-0x72A8). */
764 PolyCount = 0;
765 /* Second discarded read of the VIF/DMA register block, present
766 * in retail between the PolyCount clear and the old_main_mode
767 * store (`lui $1,0x1000; lw $2,0($1)`, result unused). */
768 (void) *(volatile s32 *) 0x10000000;
769 old_main_mode = mode;
770
771 /* Body order matches retail's compiled address order rather
772 * than case-value order, from the .L-label order in
773 * ref/asm/split/main/main.s: 14 (0x141710), 0 (0x14173C),
774 * 1 (0x141930), 2 (0x141984), 7 (0x1419D8), 13 (0x1419EC),
775 * 12 (0x141A10), 10 (0x141A2C), 11 (0x141A58), 5 (0x141A80),
776 * 9 (0x141AB8), default (0x141ADC). Cases 3 and 4 share
777 * `default`'s slot in retail, so they are not written. */
778 switch (mode) {
780 loop_result = LangsetLoop__Fv();
781
782 if (loop_result != 0) {
783#ifdef PAL
784 MapNo = -1;
786#else
787 MapNo = 801;
789#endif
790 }
791
792 break;
793 case GAME_MODE_TITLE:
794 loop_result = func_01DD2220();
795
796 if (loop_result == 1) {
797 main_select_menu_no = 0;
798 strcpy(main_select_param, "e01");
799 mode = GAME_MODE_EDIT;
800 }
801
802 if (loop_result == 2) {
803 main_select_menu_no = 1;
804 strcpy(main_select_param, "e01");
805 mode = GAME_MODE_EDIT;
806 }
807
808 if (loop_result == 3) {
809 mode = GAME_MODE_DUNGEON;
810 main_select_menu_no = 0;
811 }
812
813 if (loop_result == 5) {
814 mode = GAME_MODE_OPENING;
815 }
816
817 if (loop_result == 1) {
818 MapJump__Fii(400, -1);
819
820 for (j = 0; (in_range = j < 6) != 0; j++) {
821 memcpy(chara_names[j], save_data.GetCharaName(j), 64);
822 }
823
824 save_data.ConvertConfig(&config_data);
825 memset(&save_data, 0, sizeof(CSaveData));
826 save_data.Initialize();
827 save_data.InvertConfig(&config_data);
828
829 for (j = 0; (in_range = j < 6) != 0; j++) {
830 memcpy(save_data.GetCharaName(j), chara_names[j], 64);
831 }
832
833 TrialStart__Fv();
834 }
835
836 if (loop_result == 4) {
837 MapJump__Fii(801, -1);
838 }
839
840 if (loop_result == 2) {
841 SndInitialize__Fiiii(4, 30, 4, 5);
842 }
843
844 break;
846 if (skip_title != 0) {
847 loop_result = 1;
848 MapJump__Fii(800, -1);
849 skip_title = 0;
850 } else {
851 loop_result = func_01DC8EB0();
852
853 if (loop_result != 0) {
854 MapJump__Fii(800, -1);
855 }
856 }
857
858 break;
859 case GAME_MODE_EDIT:
860 loop_result = EditLoop__Fv();
861
862 if (loop_result == 1) {
863 mode = GAME_MODE_TITLE;
864 }
865
866 if (loop_result == 2) {
867 mode = GAME_MODE_EDIT;
868 }
869
870 if (loop_result != 0) {
871 mode = GAME_MODE_MENU;
872 }
873
874 if (loop_result == 3) {
875 mode = GAME_MODE_DUNGEON;
876 }
877
878 break;
879 case GAME_MODE_MENU:
880 loop_result = MenuLoop__Fv();
881 break;
882 case GAME_MODE_SAVE:
883 loop_result = LoopSave__Fv();
884
885 if (loop_result != 0) {
886 mode = GAME_MODE_MENU;
887 }
888
889 break;
891 loop_result = idle_result;
892
893 if (idle_result != 0) {
894 mode = GAME_MODE_MENU;
895 }
896
897 break;
899 loop_result = MemCheckLoop__Fv();
900
901 if (loop_result != 0) {
902 MapNo = 801;
904 }
905
906 break;
908 loop_result = TrialEndLoop__Fv();
909
910 if (loop_result != 0) {
911 MapNo = 800;
912 mode = GAME_MODE_TITLE;
913 }
914
915 break;
917 /* Two separate ifs, not one -- retail emits two
918 * back-to-back `beqz $16` tests (0x141A8C skipping only
919 * the `mode = GAME_MODE_TITLE` store, 0x141A98 skipping the MapJump),
920 * which a single merged if does not produce. */
921 loop_result = func_01DAF970();
922
923 if (loop_result != 0) {
924 mode = GAME_MODE_TITLE;
925 }
926
927 if (loop_result != 0) {
928 MapJump__Fii(0, -1);
929 }
930
931 break;
932 case GAME_MODE_LOADER:
933 loop_result = func_01DC1510();
934
935 if (loop_result != 0) {
936 mode = GAME_MODE_DUNGEON;
937 }
938
939 break;
942 break;
943 default:
944 loop_result = func_01DAD980(mode);
945
946 if (loop_result != 0) {
947 mode = GAME_MODE_MENU;
948 }
949
950 break;
951 }
952
953 GamePad.UpDate();
954 SV_CONFIG_SYS *config = (SV_CONFIG_SYS *) SaveData->GetConfigData();
955 /* `!x`, not `x == 0`: `!` compiles to sltu/xori/andi-0xff
956 * (materialize x!=0, invert, truncate the bool) as retail has;
957 * `== 0` compiles to a bare xor/sltiu instead (confirmed by
958 * isolated compiles of both forms). */
959 GamePad.VibrationEnable(!*(s32 *) &config->values_copy1[10]);
960 GamePad.Step();
961 MGEndFrame__Fv();
962
963#ifdef PAL
964 if (GamePad.On2(PAD_R1) != 0 && GamePad.On2(PAD_R2) != 0 && GamePad.On2(PAD_L1) != 0 && GamePad.On2(PAD_L2) != 0 && GamePad.Down2(PAD_R3) != 0) {
965 DebugMode = !DebugMode;
966 }
967#else
968 if (GamePad.On2(PAD_R1) != 0 && GamePad.On2(PAD_R2) != 0 && GamePad.On2(PAD_L1) != 0) {
969 GamePad.On2(PAD_L2);
970 }
971#endif
972 } while (loop_result == 0);
973
974 MGBeginFrame__Fv();
975 MGEndFrame__Fv();
976 sceGsSyncPath(0, 0);
977
978 /* A flat else-if chain in ascending range order, exactly as retail
979 * branches it (slti 200 / slti 300 / bne 400 / slti 800, each
980 * falling through to the next test) -- an earlier pass's nested
981 * `>= 200 { >= 300 { ... } else ... } else ...` shape mirrored
982 * the whole structure and couldn't match. */
983 if (NextMapNo >= 0) {
984 OldMapNo = MapNo;
985
986 if (NextMapNo < 200) {
987 mode = GAME_MODE_EDIT;
988 MapNo = NextMapNo;
989 } else if (NextMapNo < 300) {
990 mode = GAME_MODE_DUNGEON;
991 MapNo = NextMapNo;
992 LocalMapNo = NextMapNo - 200;
993 /* Written as a copy of LocalMapNo (retail stores then
994 * reloads it: `sw $2,LocalMapNo; lw $2,LocalMapNo; sw
995 * main_select_menu_no`), not a recomputed
996 * `NextMapNo - 200`. */
997 main_select_menu_no = LocalMapNo;
998 } else if (NextMapNo == 400) {
999 mode = GAME_MODE_OPENING;
1000 MapNo = NextMapNo;
1001 LocalMapNo = 0;
1002 main_select_menu_no = 0;
1003 } else if ((in_range = NextMapNo < 800) == 0) {
1004 /* Named-variable trick (see CLAUDE.md): retail's `>= 800`
1005 * check is `slti $2,$3,0x320; bnez $2` -- a *real* register
1006 * ($v0) where the two range checks above it use $at; only
1007 * the named-assignment form (`in_range = NextMapNo < 800`,
1008 * tested `== 0` so no bool materialization is needed)
1009 * reproduces that without adding an xori. */
1010 main_select_menu_no = 0;
1011 strcpy(main_select_param, "title");
1012
1013 if (NextMapNo == 800) {
1014 mode = GAME_MODE_TITLE;
1015 }
1016
1017 if (NextMapNo == 801) {
1018 mode = GAME_MODE_RUSH_MOVIE;
1019 }
1020
1021 MapNo = NextMapNo;
1022 LocalMapNo = 0;
1023 }
1024
1025 if (NextMapNo == 1000) {
1026 MapNo = -1;
1027 LocalMapNo = 0;
1028 mode = GAME_MODE_SAVE;
1029 /* gp-0x72AC is mc_mode, not d8 (gp-0x72B0) -- same
1030 * off-by-one-slot global mixup as the PolyCount store
1031 * above, fixed against the retail nm addresses. */
1032 mc_mode = SAVE_MENU_MODE_ENDING;
1033 }
1034 }
1035
1036 if (CheckTrialEnd__Fv() != 0) {
1037 mode = GAME_MODE_TRIAL_END;
1038 MapNo = -1;
1039 LocalMapNo = -1;
1040 }
1041
1042 while (ReadBGSync__Fv() != 0) {
1043 }
1044 }
1045}
1046
1047static int edit_map;
1048static int sub_map;
1049static int event_no;
1050
1051/* The game's top-level modes and the transitions between them: map jumps, the
1052 * menu, the save and memory-card screens, the trial ending. */
1053
1054void MapJump(int map_no, int event_no) {
1055 NextMapNo = map_no;
1056 StartEventNo = event_no;
1057}
1058
1059void MenuInit() {
1060 static LOADTEXTURE_INFO texdata[64] = {{"#frame_buff#640#" HALF_BUFFER_HEIGHT_STR "#4"},
1061 {"img/ankfont.img"},
1063 float background;
1064 int result;
1065 InitializeDataBuffer();
1066 SetDataBuffer(&TextureData, 100000);
1067 SetPacketReadBuffer(10000, 100000);
1068 TexManager.Initialize(16352);
1069 result = TexManager.EnterTextureFile(texdata);
1070 result = TexManager.LoadTextureBlock(-1, read_buffer);
1071#ifdef PAL
1072 MGSetBGColor(0.0f, 0.0f, 128.0f, 128.0f);
1073#else
1074 MGSetBGColor(0.0f, 0.0f, background = 128.0f, background);
1075#endif
1076 DebugFont.texture_name = "frame_buff";
1077 DebugFont.x = 16;
1078 DebugFont.y = 16;
1079 DebugFont.width = 256;
1080 DebugFont.height = SCREEN_HALF_HEIGHT;
1081 DebugFont.alpha = 64;
1082 GamePad.SetAutoRepeat(PAD_DPAD, 25, 3);
1083}
1084
1088int MenuLoop() {
1089 static char *menu[10] = {
1090 "game start",
1091 "%se0%d\n",
1092 "%ss%d\n",
1093 "interior",
1094 "dungeon",
1095 "opening",
1096 "%sevent%d\n",
1097 "%smemory card %d\n",
1098 "%sLanguage %d\n",
1100 static int map_no[10] = {800, 0, 11, 99, 200, 400, 0, 0, 0, -1};
1101
1102 DebugFont.length = 0;
1103
1104 int i = 0;
1105 static int select = 0;
1106
1107 while (menu[i][0] != 0) {
1108 i++;
1109 }
1110
1111 char *cursor[2] = {" ", ">"};
1112
1113 if (GamePad.Down(PAD_DOWN)) {
1114 select++;
1115 }
1116
1117 if (GamePad.Down(PAD_UP)) {
1118 select--;
1119 }
1120
1121 if (select < 0) {
1122 select = 0;
1123 }
1124
1125 if (select >= i) {
1126 select = i - 1;
1127 }
1128
1129 if (select == 1) {
1130 if (GamePad.Down(PAD_RIGHT)) {
1131 edit_map++;
1132 }
1133
1134 if (GamePad.Down(PAD_LEFT)) {
1135 edit_map--;
1136 }
1137
1138 if (edit_map < 0) {
1139 edit_map = 0;
1140 }
1141
1142 if (edit_map > 4) {
1143 edit_map = 4;
1144 }
1145 }
1146
1147 if (select == 2) {
1148 if (GamePad.Down(PAD_RIGHT)) {
1149 sub_map++;
1150 }
1151
1152 if (GamePad.Down(PAD_LEFT)) {
1153 sub_map--;
1154 }
1155
1156 if (GamePad.Down(PAD_R1)) {
1157 sub_map += 10;
1158 }
1159
1160 if (GamePad.Down(PAD_L1)) {
1161 sub_map -= 10;
1162 }
1163
1164 if (sub_map < 0) {
1165 sub_map = 98;
1166 }
1167
1168 if (sub_map > 98) {
1169 sub_map = 0;
1170 }
1171 }
1172
1173 if (select == 6) {
1174 if (GamePad.Down(PAD_RIGHT)) {
1175 event_no++;
1176 }
1177
1178 if (GamePad.Down(PAD_LEFT)) {
1179 event_no--;
1180 }
1181
1182 if (GamePad.Down(PAD_R1)) {
1183 event_no += 10;
1184 }
1185
1186 if (GamePad.Down(PAD_L1)) {
1187 event_no -= 10;
1188 }
1189
1190 if (event_no < 0) {
1191 event_no = 2;
1192 }
1193
1194 if (event_no > 2) {
1195 event_no = 0;
1196 }
1197 }
1198
1199 if (select == 7) {
1200 if (GamePad.Down(PAD_RIGHT)) {
1201 mc_mode++;
1202 }
1203
1204 if (GamePad.Down(PAD_LEFT)) {
1205 mc_mode--;
1206 }
1207
1208 if (mc_mode < 0) {
1209 mc_mode = SAVE_MENU_MODE_ENDING_NO_CLEAR;
1210 }
1211
1212 if (mc_mode > 3) {
1213 mc_mode = SAVE_MENU_MODE_LOAD;
1214 }
1215 }
1216
1217 if (select == 8) {
1218 if (GamePad.Down(PAD_RIGHT)) {
1219 LanguageCode++;
1220 }
1221
1222 if (GamePad.Down(PAD_LEFT)) {
1223 LanguageCode--;
1224 }
1225
1226 if (LanguageCode < LANG_JAPANESE) {
1227 LanguageCode = LANG_JAPANESE;
1228 }
1229
1230 if (LanguageCode > LANG_SPANISH) {
1231 LanguageCode = LANG_SPANISH;
1232 }
1233 }
1234
1235#ifdef PAL
1236 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], "Dark Cloud Ver3.07 2001/07/12\n");
1237#else
1238 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], "Dark Cloud Ver2.17 2001/05/11\n");
1239#endif
1240 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], "%s%s\n", cursor[select == 0], menu[0]);
1241 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], menu[1], cursor[select == 1], edit_map + 1);
1242 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], menu[2], cursor[select == 2], sub_map + 1);
1243
1244 for (i = 3; i < 6; i++) {
1245 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], "%s%s\n", cursor[i == select], menu[i]);
1246 }
1247
1248 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], menu[i], cursor[i == select], event_no);
1249 i++;
1250 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], menu[i], cursor[i == select], mc_mode);
1251 i++;
1252 DebugFont.length += sprintf(&DebugFont.text[DebugFont.length], menu[i], cursor[i == select], LanguageCode);
1253
1254 TexManager.ReloadTexture(GetVif1Packet(), 0);
1255 DebugFont.Draw();
1256
1257 if (GamePad.Down(PAD_CIRCLE) || GamePad.Down(PAD_TRIANGLE)) {
1258 static int map[3] = {23, 41, 19};
1259 static int event[3] = {310, 150, 305};
1260
1261 main_select_padrup = 0;
1262
1263 if (GamePad.Down(PAD_TRIANGLE)) {
1264 main_select_padrup = 1;
1265 }
1266
1267 OldMapNo = -1;
1268 NextMapNo = -1;
1269 GamePad.AutoRepeatOff();
1270
1271 main_select_menu_no = map_no[select];
1272 mode = GAME_MODE_EDIT;
1273
1274 if (select == 1) {
1275 main_select_menu_no = edit_map;
1276 LocalMapNo = edit_map;
1277 }
1278
1279 if (select == 2) {
1280 main_select_menu_no = sub_map + 11;
1281 LocalMapNo = main_select_menu_no;
1282 }
1283
1284 if (select == 6) {
1285 MapJump(map[event_no], event[event_no]);
1286 return 1;
1287 }
1288
1289 if (select == 7) {
1290 mode = GAME_MODE_SAVE;
1291 return 1;
1292 }
1293
1294 MapNo = main_select_menu_no;
1295
1296 if (main_select_menu_no == 200) {
1297 main_select_menu_no = 0;
1298 mode = GAME_MODE_LOADER;
1299 }
1300
1301 if (main_select_menu_no == 800) {
1302 MapNo++;
1303 main_select_menu_no = 0;
1304 strcpy(main_select_param, "title");
1305 mode = GAME_MODE_RUSH_MOVIE;
1306 }
1307
1308 if (main_select_menu_no == 400) {
1309 main_select_menu_no = 0;
1310 mode = GAME_MODE_OPENING;
1311 }
1312
1313 return 1;
1314 }
1315
1316 return 0;
1317}
1318
1319static int mem_chk_mode;
1320static int check_cancel;
1321static int taiken_cnt;
1322static int taiken_start;
1323
1324#ifdef PAL
1325void MemCheckInit() {
1326 InitializeDataBuffer();
1327 SetDataBuffer(&TextureData, 100000);
1328 SetPacketReadBuffer(10000, 100000);
1329 char filename[64] = "img/memory.img";
1330
1331 if (LanguageCode > LANG_JAPANESE && LanguageCode < LANG_COUNT) {
1332 sprintf(filename, "img_%d/memory.img", LanguageCode);
1333 }
1334
1335 TexManager.Initialize(16352);
1336 static LOADTEXTURE_INFO texdata[64] = {{filename}, {gamemode_empty_string}};
1337 TexManager.EnterTextureFile(texdata);
1338 TexManager.LoadTextureBlock(-1, read_buffer);
1339 MGSetBGColor(0.0f, 0.0f, 0.0f, 0.0f);
1340
1341 if (sceMcInit()) {
1342 printf("libmc initialize faild\n");
1343 }
1344
1345 mem_chk_mode = MEM_CHECK_CHECK;
1346 check_cancel = 2;
1347 taiken_cnt = 0;
1348 taiken_start = 0;
1349}
1350#else
1351void MemCheckInit() {
1352 static LOADTEXTURE_INFO texdata[64] = {{"#frame_buff#640#224#4"},
1353 {"img_1/memory.img"},
1355 int result;
1356 InitializeDataBuffer();
1357 SetDataBuffer(&TextureData, 100000);
1358 SetPacketReadBuffer(10000, 100000);
1359 TexManager.Initialize(16352);
1360 result = TexManager.EnterTextureFile(texdata);
1361 TexManager.LoadTextureBlock(-1, read_buffer);
1362 MGSetBGColor(0.0f, 0.0f, 0.0f, 0.0f);
1363
1364 if (sceMcInit()) {
1365 printf("libmc initialize faild\n");
1366 }
1367
1368 mem_chk_mode = MEM_CHECK_CHECK;
1369 check_cancel = 2;
1370 taiken_cnt = 0;
1371 taiken_start = 0;
1372}
1373#endif
1374
1375#ifdef PAL
1376int MemCheckLoop() {
1377 DebugFont.length = 0;
1378 TexManager.ReloadTexture(GetVif1Packet(), 0);
1379
1380 switch (mem_chk_mode) {
1381 case MEM_CHECK_CHECK:
1382 if (check_cancel > 0) {
1383 break;
1384 }
1385
1386 switch (SaveEnableCheck()) {
1387 case SAVE_ENABLE_OK:
1388 return 1;
1389 case SAVE_ENABLE_NO_CARD:
1390 mem_chk_mode = MEM_CHECK_NO_CARD;
1391 break;
1392 case SAVE_ENABLE_NO_SPACE:
1393 mem_chk_mode = MEM_CHECK_NO_SPACE;
1394 break;
1395 }
1396
1397 break;
1398 case MEM_CHECK_UNK_1:
1399 break;
1400 case MEM_CHECK_NO_CARD: {
1401 /* The warning images are drawn at NTSC height and stretched over the taller frame. */
1402 CRect_i_ rect;
1403 CRect_i_ texel;
1404 texel.x = 0;
1405 texel.y = 0;
1406 texel.width = 640;
1407 texel.height = 448;
1408 rect.x = 0;
1409 rect.y = 0;
1410 rect.width = 640;
1411 rect.height = SCREEN_HEIGHT;
1412 set2DSprite(GetVif1Packet(), TexManager.GetTexture("memory01", -1), rect, texel);
1413
1414 if (GamePad.Down(PAD_CIRCLE)) {
1415 mem_chk_mode = MEM_CHECK_CHECK;
1416 check_cancel = 3;
1417 }
1418
1419 if (GamePad.Down(PAD_CROSS)) {
1420 mem_chk_mode = MEM_CHECK_DONE;
1421 }
1422
1423 break;
1424 }
1425 case MEM_CHECK_NO_SPACE: {
1426 CRect_i_ rect;
1427 CRect_i_ texel;
1428 texel.x = 0;
1429 texel.y = 0;
1430 texel.width = 640;
1431 texel.height = 448;
1432 rect.x = 0;
1433 rect.y = 0;
1434 rect.width = 640;
1435 rect.height = SCREEN_HEIGHT;
1436 set2DSprite(GetVif1Packet(), TexManager.GetTexture("memory02", -1), rect, texel);
1437
1438 if (GamePad.Down(PAD_CIRCLE)) {
1439 mem_chk_mode = MEM_CHECK_CHECK;
1440 check_cancel = 3;
1441 }
1442
1443 if (GamePad.Down(PAD_CROSS)) {
1444 mem_chk_mode = MEM_CHECK_DONE;
1445 }
1446
1447 break;
1448 }
1449 case MEM_CHECK_DONE:
1450 return 1;
1451 }
1452
1453 check_cancel--;
1454
1455 if (check_cancel < 0) {
1456 check_cancel = 0;
1457 }
1458
1459 return 0;
1460}
1461#else
1462int MemCheckLoop() {
1463 DebugFont.length = 0;
1464 TexManager.ReloadTexture(GetVif1Packet(), 0);
1465
1466 switch (mem_chk_mode) {
1467 case MEM_CHECK_CHECK:
1468 if (check_cancel > 0) {
1469 break;
1470 }
1471
1472 switch (SaveEnableCheck()) {
1473 case SAVE_ENABLE_OK:
1474 return 1;
1475 case SAVE_ENABLE_NO_CARD:
1476 mem_chk_mode = MEM_CHECK_NO_CARD;
1477 break;
1478 case SAVE_ENABLE_NO_SPACE:
1479 mem_chk_mode = MEM_CHECK_NO_SPACE;
1480 break;
1481 }
1482
1483 break;
1484 case MEM_CHECK_UNK_1:
1485 break;
1486 case MEM_CHECK_NO_CARD: {
1487 CRect_i_ rect;
1488 rect.x = 0;
1489 rect.y = 0;
1490 rect.width = 640;
1491 rect.height = 448;
1492 set2DSprite(GetVif1Packet(), TexManager.GetTexture("memory01", -1), rect, 0, 0);
1493
1494 if (GamePad.Down(PAD_CIRCLE)) {
1495 mem_chk_mode = MEM_CHECK_CHECK;
1496 check_cancel = 3;
1497 }
1498
1499 if (GamePad.Down(PAD_CROSS)) {
1500 mem_chk_mode = MEM_CHECK_DONE;
1501 }
1502
1503 break;
1504 }
1505 case MEM_CHECK_NO_SPACE: {
1506 CRect_i_ rect;
1507 rect.x = 0;
1508 rect.y = 0;
1509 rect.width = 640;
1510 rect.height = 448;
1511 set2DSprite(GetVif1Packet(), TexManager.GetTexture("memory02", -1), rect, 0, 0);
1512
1513 if (GamePad.Down(PAD_CIRCLE)) {
1514 mem_chk_mode = MEM_CHECK_CHECK;
1515 check_cancel = 3;
1516 }
1517
1518 if (GamePad.Down(PAD_CROSS)) {
1519 mem_chk_mode = MEM_CHECK_DONE;
1520 }
1521
1522 break;
1523 }
1524 case MEM_CHECK_DONE:
1525 return 1;
1526 }
1527
1528 check_cancel--;
1529
1530 if (check_cancel < 0) {
1531 check_cancel = 0;
1532 }
1533
1534 return 0;
1535}
1536#endif
1537
1538void InitSave() {
1539 static LOADTEXTURE_INFO texdata[64] = {
1540 {"#frame_buff#640#" SCREEN_HEIGHT_STR "#4", 1 },
1541 {"#mes_frame_buff#640#" SCREEN_HEIGHT_STR "#4", 26},
1542 {"#fukidashibase#640#" HALF_BUFFER_HEIGHT_STR "#4", 26},
1543 {"#fontbase#512#256#1", 26},
1544 {"#fuki256#128#128#1", 26},
1545 {"meswin/gaiji.img", 26}
1546 };
1547 u_int *buffer;
1548 InitializeDataBuffer();
1549 SetDataBuffer(&TextureData, 100000);
1550 SetPacketReadBuffer(10000, 100000);
1551 TexManager.Initialize(16352);
1552 TexManager.EnterTextureFile(texdata);
1553 TexManager.LoadTextureBlock(-1, read_buffer);
1554 LoadFileMenuData((buffer = read_buffer, "stayframe.img"), buffer);
1555 TexManager.EnterFixTextureZ((u_char *) read_buffer);
1556 StayTexture = TexManager.GetTexture("stayframe", -1);
1557 CommonMenuMes2.SetBuff_system(SystemMes);
1558 EditSystemMes.tex_buff = MesWinTexBuff_01;
1559 InitMenuSave(mc_mode, 1, (u_long128 *) read_buffer);
1560}
1561
1562int LoopSave() {
1563 int result = MenuSaveKey();
1564 DrawMenuSave(0);
1565 return result;
1566}
1567
1568void TrialEndInit() {}
1569
1570int TrialEndLoop() { return 1; }
1571
1572void TrialStart() {}
1573
1574int CheckTrialEnd() { return 0; }
1575
1576/* MAP_NPC_MODEL's members as its copy assignment reaches them; the tail is alignment padding. */
1589
1601#pragma push
1602#pragma dont_inline on
1603
1604static void CopyNpcModel(GeneratedNpcModel &dest, const GeneratedNpcModel &src) {
1605 dest = src;
1606}
1607
1608static void CopyCharacter(CCharacter &dest, const CCharacter &src) {
1609 dest = src;
1610}
1611
1612#pragma pop
1613
1615 // The three words after the mass are alignment padding and are not carried over.
1616 mass = source.mass;
1617 float w, z, y, x;
1618 x = source.pos[0];
1619 y = source.pos[1];
1620 z = source.pos[2];
1621 w = source.pos[3];
1622 pos[0] = x;
1623 pos[1] = y;
1624 pos[2] = z;
1625 pos[3] = w;
1626 velocity = source.velocity;
1627 acceleration = source.acceleration;
1628 gravity = source.gravity;
1629 moment = source.moment;
1630 rotation = source.rotation;
1631 rot_velocity = source.rot_velocity;
1633 *(CVector3_f_ *) scale = *(const CVector3_f_ *) source.scale;
1634 return *this;
1635}
1636
1637/* CWater's members as its copy assignment reaches them. */
1655
1657 *(DraftWaterLayout *) this = *(DraftWaterLayout *) &src;
1658 return *this;
1659}
1660
1669 CVisualVu1::operator=(src);
1670 return *this;
1671}
1672
1680/* The full layout: CVisual's words, then the vector-unit block and its size. */
1688
1689CVisualVu1 &CVisualVu1::operator=(const CVisualVu1 &src) {
1690 DraftVisualVu1Layout *dst_layout = (DraftVisualVu1Layout *) this;
1691 const DraftVisualVu1Layout *src_layout = (const DraftVisualVu1Layout *) &src;
1692 dst_layout->base = src_layout->base;
1693 dst_layout->vu_state_0 = src_layout->vu_state_0;
1694 dst_layout->vu_state_1 = src_layout->vu_state_1;
1695 dst_layout->vu_data = src_layout->vu_data;
1696 dst_layout->vu_size = src_layout->vu_size;
1697 return *this;
1698}
1699
1708 unk_00 = other.unk_00;
1709 unk_04 = other.unk_04;
1710 return *this;
1711}
1712
1723
1732 lod[0] = -1.0f;
1733 lowest = 0;
1734 highest = 3;
1735}
1736
1740
1749 for (int i = 0; i < 8; i++) {
1750 active[i] = 0;
1751 }
1752}
1753
1757
1766 active = false;
1767 alphas[59] = 0.0f;
1768}
1769
1777extern "C" void *__ct__13MAP_NPC_MODELFv(void *self) {
1778 __ct__10CCharacterFv(self);
1779 return self;
1780}
1781
1789extern "C" void *__ct__10CCharacterFv(void *self) {
1790 CCharacter *chara = (CCharacter *) self;
1791
1792 __ct__7CObjectFv(self);
1793 *(void **) ((u_char *) self + 0xA0) = GeneratedCharacterVtable;
1794 __ct__13CTextureAnimeFv(&chara->tex_anime);
1797 void *params = ((CCharacter *) self)->motion_storage;
1798 __construct_array(params, (MWRuntimeObjectFunction) GeneratedMotionParamCtor, NULL, sizeof(MotionParam), CHARA_MOTION_MAX);
1799 params = ((CCharacter *) self)->shadow_motion_storage;
1800 __construct_array(params, (MWRuntimeObjectFunction) GeneratedMotionParamCtor, NULL, sizeof(MotionParam), CHARA_MOTION_MAX);
1801 __construct_array(chara->point_light, GeneratedFakePointLightCtor, NULL, sizeof(CFakePointLight), CHARA_POINT_LIGHT_MAX);
1802 chara->Initialize();
1803 return self;
1804}
1805
1814
1822extern "C" void *__ct__13CTextureAnimeFv(void *self) {
1823 return GeneratedTextureAnimeCtor(self, NULL, 0);
1824}
1825
1833extern "C" void *__ct__7CObjectFv(void *self) {
1834 return GeneratedObjectCtor(self, 1.0f);
1835}
1836
1844extern "C" void *__ct__12CSHOT_EFFECTFv(void *self) {
1845 __ct__10CCharacterFv((u_char *) self + 0x10);
1846 u_char *array = (u_char *) self + 0x11C0;
1847 __construct_array(array, (MWRuntimeObjectFunction) __ct__10CCharacterFv, NULL, 0x11B0, 8);
1848 return self;
1849}
1850
1858extern "C" void *__ct__8CHitMarkFv(void *self) {
1859 __ct__7CObjectFv(self);
1860 *(void **) ((u_char *) self + 0xA0) = GeneratedHitMarkVtable;
1861 return self;
1862}
1863
1871u_char *CDataAlloc<1, 6000>::Alloc(int quads) {
1872 int filled = used + quads;
1873
1874 if (filled > 6000) {
1875 printf("Alocation Error! %d/%d\n", used, 6000);
1876
1877 while (1)
1878 ;
1879 }
1880
1881 u_char *run = (u_char *) block + used * 16;
1882 used = filled;
1883 return run;
1884}
1885#ifndef PORT
1894 asm {
1895 bne $0, $0, done
1896 }
1897
1898 u_int slack = (u_int) ((u_char *) block + used * 16) & 63;
1899
1900 if (slack) {
1901 used += (64 - slack) >> 4;
1902 }
1903
1904 if (used >= 6000) {
1905 printf("Alocation Error! %d/%d\n", used, 6000);
1906
1907 while (1)
1908 ;
1909 }
1910
1911done:;
1912}
1913#endif
#define CHARA_MOTION_MAX
Definition character.hpp:35
#define CHARA_POINT_LIGHT_MAX
Definition character.hpp:37
void * GeneratedCharacterVtable[]
s32 active[8]
void Initialize()
Definition main.cpp:1748
CFakePointLight point_light[CHARA_POINT_LIGHT_MAX]
tagMOTION_TYPE motion_type
tagMOTION_TYPE shadow_motion_type
virtual void Initialize()
CTextureAnime tex_anime
u_long128 block[6000]
Definition dataalloc.hpp:79
u_char * Alloc(int quads)
Definition main.cpp:1871
void Draw()
Definition dispctrl.cpp:60
char * texture_name
Definition gameloop.cpp:164
char text[0x200]
Definition gameloop.cpp:167
void StopVibration()
Definition gamepad.cpp:643
void KeyLock2(int mask)
Definition gamepad.cpp:505
void UpDate()
Definition gamepad.cpp:261
void Step()
Definition gamepad.cpp:392
void Init()
Definition gamepad.cpp:16
int On(int mask)
Definition gamepad.cpp:557
int On2(int mask)
Definition gamepad.cpp:575
int Down2(int mask)
Definition gamepad.cpp:601
int Down(int mask)
Definition gamepad.cpp:583
void VibrationEnable(int enable)
Definition gamepad.cpp:639
void SetAutoRepeat(int mask, int initial_delay, int repeat_delay)
Definition gamepad.cpp:485
void AutoRepeatOff()
Definition gamepad.cpp:609
float alphas[60]
Definition majinbeem.hpp:30
void Initialize()
Definition main.cpp:1765
float scale[4]
Definition object.hpp:26
CVector3_f_ rotation
Definition object.hpp:23
CVector3_f_ rot_velocity
Definition object.hpp:24
CVector3_f_ acceleration
Definition object.hpp:20
CVector3_f_ rot_acceleration
Definition object.hpp:25
CVector3_f_ velocity
Definition object.hpp:19
float mass
Definition object.hpp:16
CVector3_f_ gravity
Definition object.hpp:21
float pos[4]
Definition object.hpp:18
CVector3_f_ moment
Definition object.hpp:22
CObject & operator=(const CObject &source)
Definition main.cpp:1614
s32 x
Definition rect.hpp:18
s32 width
Definition rect.hpp:20
s32 height
Definition rect.hpp:21
s32 y
Definition rect.hpp:19
void Initialize()
Definition savedata.cpp:628
s16 * GetCharaName(int chara_no)
Definition savedata.cpp:41
void * GetConfigData()
Definition savedata.cpp:33
void AddPlayTime(int time)
Definition savedata.cpp:49
void ConvertConfig(SV_CONFIG_SYS *out)
Definition savedata.cpp:559
int InvertConfig(SV_CONFIG_SYS *in)
Definition savedata.cpp:580
int LoadSqInf(char *name, unsigned int *buffer)
Definition sound.cpp:321
int LoadSeInf(char *name, unsigned int *buffer)
Definition sound.cpp:344
CVisualPolyVu1 & operator=(const CVisualPolyVu1 &src)
Definition main.cpp:1668
CVisual & operator=(const CVisual &other)
Definition main.cpp:1707
CWater & operator=(CWater &src)
Definition main.cpp:1656
float lod[4]
Definition main.cpp:70
void Initialize()
Definition main.cpp:1731
int lowest
Definition main.cpp:71
int highest
Definition main.cpp:72
void * tex_buff
Definition clsmes.hpp:233
void SetBuff_system(short *buff)
Definition clsmes.cpp:462
void GeneratedFakePointLightCtor(void *object, int mode)
void * GeneratedHitMarkVtable[]
Definition hitmark.hpp:219
void * GeneratedMotionParamCtor(void *self)
char gamemode_empty_string[]
@ GAME_MODE_UNUSED_8
Definition main.hpp:25
@ GAME_MODE_RUSH_MOVIE
Definition main.hpp:18
@ GAME_MODE_LOADER
Definition main.hpp:26
@ GAME_MODE_MENU
Definition main.hpp:24
@ GAME_MODE_MEMORY_CHECK
Definition main.hpp:27
@ GAME_MODE_DUNGEON
Definition main.hpp:20
@ GAME_MODE_OPENING
Definition main.hpp:22
@ GAME_MODE_EDIT
Definition main.hpp:19
@ GAME_MODE_LANGUAGE
Definition main.hpp:31
@ GAME_MODE_UNUSED_12
Definition main.hpp:29
@ GAME_MODE_SAVE
Definition main.hpp:30
@ GAME_MODE_TRIAL_END
Definition main.hpp:28
@ GAME_MODE_TITLE
Definition main.hpp:17
@ GAME_MODE_UNUSED_6
Definition main.hpp:23
void(* MWRuntimeObjectFunction)(void *object, int mode)
Definition mathutil.hpp:29
float height_scale
Definition main.cpp:1650
float height_buffers[2]
Definition main.cpp:1642
CFrameVu1 frame
Definition main.cpp:1653
float buffer_packets[2]
Definition main.cpp:1645
sceVu0FVECTOR vertex[4]
Definition main.cpp:1643
u_int * packet
Definition main.cpp:1644
CVisualPolyVu1 visual
Definition main.cpp:1646
float * height
Definition main.cpp:1641
s32 draw_param[16]
Definition main.cpp:1586
float draw_pos[16][4]
Definition main.cpp:1585
float rotation[4]
Definition main.cpp:1580
CCharacter chara
Definition main.cpp:1578
char values_copy1[18]
Definition savedata.hpp:45