-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjoy.c
282 lines (240 loc) · 6.55 KB
/
joy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <sce/memset.h>
#include <sdk/ee/libpad.h>
#include <joy.h>
#include <clock.h>
#include <transition.h>
#include <game.h>
#include <text.h>
#include <screen.h>
//static char g_chzThePasswordIs[] = "The password is: %s";
//static char g_chzCiphertext[] = "@KFWHJGL"; // decrypts to "chetkido"
extern char g_chzThePasswordIs[];
extern char g_chzCiphertext[];
extern void* PvAllocGlobalImpl(int); // todo: remove when function is known
void StartupJoy()
{
UpdateGrfjoytFromGrfusr();
scePadInit(0);
InitJoy(&g_joy, 0, 0);
}
void AddGrfusr(int mask)
{
g_grfusr |= mask;
UpdateGrfjoytFromGrfusr();
}
void RemoveGrfusr(int mask)
{
g_grfusr &= ~mask;
UpdateGrfjoytFromGrfusr();
}
void UpdateGrfjoytFromGrfusr()
{
if (g_grfusr & 0x84)
{
// Unknown state
g_grfjoyt = 0;
}
else if (g_grfusr & FUSR_Menu)
{
// Pause menu/dialog box is open
g_grfjoyt = 5;
}
else if (g_grfusr & FUSR_HandsOff)
{
// In-game cutscene/transition/animation is playing
g_grfjoyt = 4;
}
else
{
// Game is running normally
g_grfjoyt = 7;
}
}
// todo: investigate if aullDma assignment can be cleaned up when type is known
void InitJoy(JOY *pjoy, int nPort, int nSlot)
{
memset(pjoy, 0, sizeof(JOY));
SetJoyJoys(pjoy, JOYS_Searching, JOYK_Unknown);
pjoy->nPort = nPort;
pjoy->nSlot = nSlot;
pjoy->aullDma = (uint*)(((int)PvAllocGlobalImpl(0x140) + 0x3F) & ~0x3F);
scePadPortOpen(pjoy->nPort, pjoy->nSlot, pjoy->aullDma);
pjoy->prumble = (RUMBLE *)PvAllocGlobalImpl(0x20);
pjoy->fRumbleEnabled = true;
}
INCLUDE_ASM(const s32, "P2/joy", UpdateJoy__FP3JOY);
void SetJoyJoys(JOY *pjoy,JOYS joys,JOYK joyk)
{
JOYK joykPrev;
LM *almDeflect2;
LM *almDeflect;
float *almDeflect2Max;
float *almDeflectMax;
uint i;
if (joys != pjoy->joys || joyk != pjoy->joyk)
{
if (joyk == JOYK_Unknown)
{
pjoy->term = 0;
joys = JOYS_Searching;
joykPrev = pjoy->joyk;
}
else
{
joykPrev = pjoy->joyk;
}
if (joykPrev < 3 && 2 < joyk)
{
InitRumble(pjoy->prumble,pjoy->nPort,pjoy->nSlot);
}
if (joys == JOYS_Ready)
{
i = 0;
pjoy->tRead = 0.0;
pjoy->fStickMoved2 = 0;
almDeflectMax = &pjoy->almDeflect[0].gMax;
pjoy->fStickMoved = 0;
almDeflect2Max = &pjoy->almDeflect2[0].gMax;
pjoy->grfbtn = 0;
almDeflect = pjoy->almDeflect;
almDeflect2 = pjoy->almDeflect2;
for (; i < 4; i++) {
almDeflect2->gMin = -0.75;
almDeflect->gMin = -0.75;
almDeflect2++;
*almDeflect2Max = 0.75;
almDeflect++;
*almDeflectMax = 0.75;
almDeflect2Max += 2;
almDeflectMax += 2;
}
}
pjoy->joys = joys;
pjoy->joyk = joyk;
pjoy->tJoys = g_clock.tReal;
}
}
INCLUDE_ASM(const s32, "P2/joy", GetJoyXYDeflection__FP3JOYUcUcPfN23PUcT6PiP2LM);
INCLUDE_ASM(const s32, "P2/joy", FReadJoy__FP3JOY);
void SetJoyBtnHandled(JOY *pjoy, GRFBTN btn)
{
pjoy->grfbtnPressed &= ~btn;
}
void TriggerJoyRumbleRumk(JOY *pjoy, RUMK rumk, float dt)
{
if (pjoy->fRumbleEnabled && pjoy->joyk > JOYK_Analog && pjoy->joys == JOYS_Ready)
{
TriggerRumbleRumk(pjoy->prumble, rumk, dt);
}
}
INCLUDE_ASM(const s32, "P2/joy", func_0016EC80);
void StartJoySelection(JOY *pjoy)
{
pjoy->dxLatch = 0;
pjoy->dyLatch = 0;
}
INCLUDE_ASM(const s32, "P2/joy", DxSelectionJoy__FP3JOY);
INCLUDE_ASM(const s32, "P2/joy", DySelectionJoy__FP3JOY);
INCLUDE_ASM(const s32, "P2/joy", UBtnpJoy__FP3JOY4BTNP);
void AddCode(CODE *pcode)
{
CODE *p_inputs_counter = g_pcode;
while (p_inputs_counter != nullptr)
{
if (pcode == p_inputs_counter)
{
return;
}
p_inputs_counter = p_inputs_counter->pchtNext;
}
pcode->pchtNext = g_pcode;
g_pcode = pcode;
if (g_joy.grfbtn == *pcode->ajbc)
{
pcode->nInputCounter = 1;
}
else
{
pcode->nInputCounter = 0;
}
}
INCLUDE_ASM(const s32, "P2/joy", func_0016F1F0);
void _ResetCodes()
{
CODE *pcode = g_pcode;
if (g_pcode != nullptr)
{
g_pcode->nInputCounter = 0;
while (pcode = pcode->pchtNext, pcode != nullptr)
{
pcode->nInputCounter = 0;
}
}
}
void _MatchCodes(JOY *pjoy, GRFBTN btn)
{
if (g_pcode != nullptr && pjoy == &g_joy && btn != g_joy.grfbtn && g_joy.grfbtn != 0)
{
CODE * pcode = g_pcode;
while(pcode != nullptr)
{
if (pcode->nInputCounter < pcode->cjbc)
{
if (g_joy.grfbtn == pcode->ajbc[pcode->nInputCounter])
{
pcode->nInputCounter++;
}
else
{
pcode->nInputCounter = 0;
}
}
pcode = pcode->pchtNext;
}
g_tCodeCheck = g_clock.tReal + 1.0f;
}
}
INCLUDE_ASM(const s32, "P2/joy", UpdateCodes__Fv);
void ClearFchts()
{
g_grfcht = FCHT_None;
ResetWorld(FTRANS_None);
}
void AddFcht(int nParam)
{
g_grfcht |= nParam & ~FCHT_ResetWorld;
if (nParam & FCHT_ResetWorld)
{
ResetWorld(FTRANS_None);
}
}
INCLUDE_ASM(const s32, "P2/joy", func_0016F470);
void Chetkido() {
// Check preconditions
int widCur;
FGS cmpFlags;
GS *gsCur;
widCur = (g_pgsCur->gameworldCur << 8) | (g_pgsCur->worldlevelCur);
if (widCur != 0x400) // level is "A Perilous Ascent"
return;
cmpFlags = get_game_completion();
if ((cmpFlags & (FGS_HalfClues|FGS_AllClues)) != (FGS_HalfClues|FGS_AllClues)) // clues collected
return;
gsCur = g_pgsCur;
if (gsCur->ccoin != 99 || gsCur->clife != 0) // 99 coins and 0 lives
return;
// Decrypt chetkido using XOR cipher, key 0x23
char buf[64];
char achzPlaintext[16];
char *pchCur = &achzPlaintext[0];
strcpy(achzPlaintext, g_chzCiphertext);
while (*pchCur != '\0') {
*pchCur++ ^= 0x23;
}
// Show note blot with message
sprintf(buf, g_chzThePasswordIs, achzPlaintext);
((NOTE*)&g_note.unk278)->pvtnote->pfnSetNoteAchzDraw((NOTE*)&g_note.unk278, buf);
SetBlotDtVisible((NOTE *)&g_note.unk278, 10.0f);
((NOTE*)&g_note.unk278)->pvtnote->pfnShowBlot((NOTE*)&g_note.unk278);
}
INCLUDE_ASM(const s32, "P2/joy", StartupCodes__Fv);