Skip to content

Commit dd3fb4c

Browse files
Merge pull request #197 from Chesyon/main
Decomped LoadScriptVariableValueAtIndex
2 parents 9397be8 + 4f645e6 commit dd3fb4c

File tree

8 files changed

+76
-157
lines changed

8 files changed

+76
-157
lines changed

asm/include/main_0204B678.inc renamed to asm/include/main_0204B820.inc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
.public Debug_Print
66
.public Debug_Print0
77
.public GetGameMode
8-
.public GetLanguageType
9-
.public GetMoneyCarried
10-
.public GetMoneyStored
11-
.public GetNotifyNote
12-
.public GetPartyMembers
138
.public LoadScriptVariableRaw
149
.public LoadScriptVariableValue
10+
.public LoadScriptVariableValueAtIndex
1511
.public MemcpySimple
1612
.public RandInt
1713
.public SCRIPT_VARS_VALUES

asm/main_0204B678.s renamed to asm/main_0204B820.s

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,8 @@
11
.include "asm/macros.inc"
2-
.include "main_0204B678.inc"
2+
.include "main_0204B820.inc"
33

44
.text
55

6-
arm_func_start LoadScriptVariableValueAtIndex
7-
LoadScriptVariableValueAtIndex: ; 0x0204B678
8-
stmdb sp!, {r3, r4, r5, lr}
9-
sub sp, sp, #8
10-
mov r3, r0
11-
mov r5, r1
12-
mov r4, r2
13-
add r0, sp, #0
14-
mov r1, r3
15-
mov r2, r5
16-
bl LoadScriptVariableRaw
17-
ldr r1, [sp]
18-
ldrsh r0, [r1]
19-
cmp r0, #9
20-
addls pc, pc, r0, lsl #2
21-
b _0204B814
22-
_0204B6B0: ; jump table
23-
b _0204B814 ; case 0
24-
b _0204B6D8 ; case 1
25-
b _0204B70C ; case 2
26-
b _0204B70C ; case 3
27-
b _0204B718 ; case 4
28-
b _0204B724 ; case 5
29-
b _0204B734 ; case 6
30-
b _0204B744 ; case 7
31-
b _0204B744 ; case 8
32-
b _0204B750 ; case 9
33-
_0204B6D8:
34-
ldrsh r1, [r1, #6]
35-
ldr r2, [sp, #4]
36-
mov r0, #1
37-
add r1, r4, r1
38-
mov r1, r1, lsl #0x10
39-
mov r3, r1, lsr #0x10
40-
and r1, r3, #7
41-
mov r1, r0, lsl r1
42-
ldrb r2, [r2, r3, lsr #3]
43-
and r1, r1, #0xff
44-
tst r2, r1
45-
moveq r0, #0
46-
b _0204B818
47-
_0204B70C:
48-
ldr r0, [sp, #4]
49-
ldrb r0, [r0, r4]
50-
b _0204B818
51-
_0204B718:
52-
ldr r0, [sp, #4]
53-
ldrsb r0, [r0, r4]
54-
b _0204B818
55-
_0204B724:
56-
ldr r1, [sp, #4]
57-
mov r0, r4, lsl #1
58-
ldrh r0, [r1, r0]
59-
b _0204B818
60-
_0204B734:
61-
ldr r1, [sp, #4]
62-
mov r0, r4, lsl #1
63-
ldrsh r0, [r1, r0]
64-
b _0204B818
65-
_0204B744:
66-
ldr r0, [sp, #4]
67-
ldr r0, [r0, r4, lsl #2]
68-
b _0204B818
69-
_0204B750:
70-
cmp r5, #0x48
71-
bgt _0204B78C
72-
bge _0204B7D4
73-
cmp r5, #0x3d
74-
bgt _0204B780
75-
subs r0, r5, #0x3a
76-
addpl pc, pc, r0, lsl #2
77-
b _0204B814
78-
_0204B770: ; jump table
79-
b _0204B7A8 ; case 0
80-
b _0204B7B0 ; case 1
81-
b _0204B7BC ; case 2
82-
b _0204B7C4 ; case 3
83-
_0204B780:
84-
cmp r5, #0x47
85-
beq _0204B7CC
86-
b _0204B814
87-
_0204B78C:
88-
cmp r5, #0x49
89-
bgt _0204B79C
90-
beq _0204B7DC
91-
b _0204B814
92-
_0204B79C:
93-
cmp r5, #0x70
94-
beq _0204B80C
95-
b _0204B814
96-
_0204B7A8:
97-
mov r0, #1
98-
b _0204B818
99-
_0204B7B0:
100-
mov r0, #0
101-
bl GetPartyMembers
102-
b _0204B818
103-
_0204B7BC:
104-
bl GetMoneyCarried
105-
b _0204B818
106-
_0204B7C4:
107-
bl GetMoneyStored
108-
b _0204B818
109-
_0204B7CC:
110-
bl GetLanguageType
111-
b _0204B818
112-
_0204B7D4:
113-
bl GetGameMode
114-
b _0204B818
115-
_0204B7DC:
116-
bl GetGameMode
117-
cmp r0, #1
118-
beq _0204B7F4
119-
cmp r0, #3
120-
beq _0204B7FC
121-
b _0204B804
122-
_0204B7F4:
123-
bl sub_0204C918
124-
b _0204B818
125-
_0204B7FC:
126-
bl GetSpecialEpisodeType
127-
b _0204B818
128-
_0204B804:
129-
mvn r0, #0
130-
b _0204B818
131-
_0204B80C:
132-
bl GetNotifyNote
133-
b _0204B818
134-
_0204B814:
135-
mov r0, #0
136-
_0204B818:
137-
add sp, sp, #8
138-
ldmia sp!, {r3, r4, r5, pc}
139-
arm_func_end LoadScriptVariableValueAtIndex
140-
1416
arm_func_start SaveScriptVariableValue
1427
SaveScriptVariableValue: ; 0x0204B820
1438
stmdb sp!, {r3, r4, r5, lr}

include/enums.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,4 +3429,16 @@ enum special_episode_type {
34293429
NUM_SPECIAL_EPISODES = 5
34303430
};
34313431

3432+
enum game_mode
3433+
{
3434+
GAME_MODE_MENU = 0, // top menu
3435+
GAME_MODE_1 = 1, // also related to special episodes? But 3 is the main one
3436+
GAME_MODE_NORMAL = 2, // normal play, including both overworld/dungeons
3437+
GAME_MODE_SPECIAL_EPISODE = 3, // during special episodes
3438+
// Pelipper Island (and likely also in the dungeon while rescuing, need to confirm)
3439+
GAME_MODE_RESCUE = 4,
3440+
GAME_MODE_5 = 5,
3441+
// Possibly more?
3442+
};
3443+
34323444
#endif //PMDSKY_ENUMS_H

include/main_0204AFF8.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33

44
#include "util.h"
55

6-
enum game_mode
7-
{
8-
GAME_MODE_MENU = 0, // top menu
9-
GAME_MODE_1 = 1, // also related to special episodes? But 3 is the main one
10-
GAME_MODE_NORMAL = 2, // normal play, including both overworld/dungeons
11-
GAME_MODE_SPECIAL_EPISODE = 3, // during special episodes
12-
// Pelipper Island (and likely also in the dungeon while rescuing, need to confirm)
13-
GAME_MODE_RESCUE = 4,
14-
GAME_MODE_5 = 5,
15-
// Possibly more?
16-
};
17-
186
bool8 IsGameModeRescue();
197

208
#endif //PMDSKY_MAIN_0204AFF8_H

include/scripting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ void LoadScriptVariableRaw(struct script_var_raw* sv_raw,
3636
const enum script_var_id sv_id);
3737

3838
s32 LoadScriptVariableValue(union script_var_value sv_local[], enum script_var_id sv_id);
39+
s32 LoadScriptVariableValueAtIndex(union script_var_value sv_local[], enum script_var_id id, int idx);
3940

4041
#endif //PMDSKY_SCRIPTING_H

main.lsf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Static main
8181
Object src/main_0204AFF8.o
8282
Object asm/main_0204B018.o
8383
Object src/scripting.o
84-
Object asm/main_0204B678.o
84+
Object asm/main_0204B820.o
8585
Object src/main_0204C938.o
8686
Object asm/main_0204C94C.o
8787
Object src/dungeon_recruitment_3.o

src/main_0204AFF8.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "main_0204AFF8.h"
2+
#include "enums.h"
23

34
extern enum game_mode GAME_MODE;
45

src/scripting.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern s32 GetPartyMembers(s32 param1);
1212
extern s32 GetMoneyCarried();
1313
extern s32 GetMoneyStored();
1414
extern s32 GetLanguageType();
15-
extern s32 GetGameMode();
15+
extern enum game_mode GetGameMode();
1616
extern s32 sub_0204C918();
1717
extern s32 GetSpecialEpisodeType();
1818
extern s32 GetNotifyNote();
@@ -77,9 +77,9 @@ s32 LoadScriptVariableValue(union script_var_value sv_local[], enum script_var_i
7777
return GetGameMode();
7878
case VAR_EXECUTE_SPECIAL_EPISODE_TYPE:
7979
switch(GetGameMode()) {
80-
case EPISODE_IGGLYBUFF_THE_PRODIGY:
80+
case GAME_MODE_1:
8181
return sub_0204C918();
82-
case EPISODE_HERE_COMES_TEAM_CHARM:
82+
case GAME_MODE_SPECIAL_EPISODE:
8383
return GetSpecialEpisodeType();
8484
default:
8585
return -1;
@@ -91,3 +91,59 @@ s32 LoadScriptVariableValue(union script_var_value sv_local[], enum script_var_i
9191

9292
return 0;
9393
}
94+
95+
s32 LoadScriptVariableValueAtIndex(union script_var_value sv_local[], enum script_var_id id, int idx)
96+
{
97+
struct script_var_raw result;
98+
LoadScriptVariableRaw(&result, sv_local, id);
99+
100+
switch ((s16)result.def->type) {
101+
case VARTYPE_NONE:
102+
break;
103+
case VARTYPE_BIT: {
104+
u16 offset = idx + result.def->bitshift;
105+
u8 val = ((u8*)result.value)[offset / 8];
106+
u8 bit = (1 << (offset & (8 - 1)));
107+
return (val & bit) != 0;
108+
}
109+
case VARTYPE_STRING:
110+
case VARTYPE_UINT8:
111+
return ((u8*)result.value)[idx];
112+
case VARTYPE_INT8:
113+
return ((s8*)result.value)[idx];
114+
case VARTYPE_UINT16:
115+
return ((u16*)result.value)[idx];
116+
case VARTYPE_INT16:
117+
return ((s16*)result.value)[idx];
118+
case VARTYPE_UINT32:
119+
case VARTYPE_INT32:
120+
return ((u32*)result.value)[idx];
121+
case VARTYPE_SPECIAL:
122+
switch (id) {
123+
case VAR_FRIEND_SUM:
124+
return 1;
125+
case VAR_UNIT_SUM:
126+
return GetPartyMembers(0);
127+
case VAR_CARRY_GOLD:
128+
return GetMoneyCarried();
129+
case VAR_BANK_GOLD:
130+
return GetMoneyStored();
131+
case VAR_LANGUAGE_TYPE:
132+
return GetLanguageType();
133+
case VAR_GAME_MODE:
134+
return GetGameMode();
135+
case VAR_EXECUTE_SPECIAL_EPISODE_TYPE:
136+
switch (GetGameMode()) {
137+
case GAME_MODE_1:
138+
return sub_0204C918();
139+
case GAME_MODE_SPECIAL_EPISODE:
140+
return GetSpecialEpisodeType();
141+
default:
142+
return -1;
143+
}
144+
case VAR_NOTE_MODIFY_FLAG:
145+
return GetNotifyNote();
146+
}
147+
}
148+
return 0;
149+
}

0 commit comments

Comments
 (0)