summaryrefslogtreecommitdiff
path: root/src/mystery_event_script.c
blob: 06c1c9bbfefbc5953fb2258f5b7f565fa7df7d27 (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "global.h"
#include "gflib.h"
#include "berry.h"
#include "battle_tower.h"
#include "easy_chat.h"
#include "event_data.h"
#include "mail_data.h"
#include "mystery_event_script.h"
#include "pokedex.h"
#include "pokemon_size_record.h"
#include "script.h"
#include "strings.h"
#include "util.h"
#include "mystery_event_msg.h"
#include "pokemon_storage_system.h"

extern ScrCmdFunc gMysteryEventScriptCmdTable[];
extern ScrCmdFunc gMysteryEventScriptCmdTableEnd[];

#define LANGUAGE_MASK 0x1
#if defined(FIRERED)
#define VERSION_MASK 0x1
#elif defined(LEAFGREEN)
#define VERSION_MASK 0x2
#endif

EWRAM_DATA static struct ScriptContext sMysteryEventScriptContext = {0};

static bool32 CheckCompatibility(u16 a1, u32 a2, u16 a3, u32 a4)
{
    if (!(a1 & LANGUAGE_MASK))
        return FALSE;

    if (!(a2 & LANGUAGE_MASK))
        return FALSE;

    if (!(a3 & 0x1))
        return FALSE;

    if (!(a4 & VERSION_MASK))
        return FALSE;

    return TRUE;
}

static void SetIncompatible(void)
{
    StringExpandPlaceholders(gStringVar4, gText_MysteryGiftCantBeUsed);
    SetMysteryEventScriptStatus(3);
}

static void InitMysteryEventScript(struct ScriptContext *ctx, u8 *script)
{
    InitScriptContext(ctx, gMysteryEventScriptCmdTable, gMysteryEventScriptCmdTableEnd);
    SetupBytecodeScript(ctx, script);
    ctx->data[0] = (u32)script;
    ctx->data[1] = 0;
    ctx->data[2] = 0;
    ctx->data[3] = 0;
}

static bool32 RunMysteryEventScriptCommand(struct ScriptContext *ctx)
{
    if (RunScriptCommand(ctx) && ctx->data[3])
        return TRUE;
    else
        return FALSE;
}

void MEventScript_InitContext(u8 *script)
{
    InitMysteryEventScript(&sMysteryEventScriptContext, script);
}

bool32 MEventScript_Run(u32 *a0)
{
    bool32 ret = RunMysteryEventScriptCommand(&sMysteryEventScriptContext);
    *a0 = sMysteryEventScriptContext.data[2];

    return ret;
}

u32 RunMysteryEventScript(u8 *script)
{
    u32 ret;
    MEventScript_InitContext(script);
    while (MEventScript_Run(&ret));

    return ret;
}

void SetMysteryEventScriptStatus(u32 val)
{
    sMysteryEventScriptContext.data[2] = val;
}

bool8 MEScrCmd_end(struct ScriptContext *ctx)
{
    StopScript(ctx);
    return TRUE;
}

bool8 MEScrCmd_checkcompat(struct ScriptContext *ctx)
{
    u16 v1;
    u32 v2;
    u16 v3;
    u32 v4;

    ctx->data[1] = ScriptReadWord(ctx);
    v1 = ScriptReadHalfword(ctx);
    v2 = ScriptReadWord(ctx);
    v3 = ScriptReadHalfword(ctx);
    v4 = ScriptReadWord(ctx);

    if (CheckCompatibility(v1, v2, v3, v4) == TRUE)
        ctx->data[3] = 1;
    else
        SetIncompatible();

    return TRUE;
}

bool8 MEScrCmd_nop(struct ScriptContext *ctx)
{
    return FALSE;
}

bool8 MEScrCmd_setstatus(struct ScriptContext *ctx)
{
    u8 value = ScriptReadByte(ctx);
    ctx->data[2] = value;
    return FALSE;
}

bool8 MEScrCmd_setmsg(struct ScriptContext *ctx)
{
    u8 value = ScriptReadByte(ctx);
    u8 *str = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    if (value == 0xFF || value == ctx->data[2])
        StringExpandPlaceholders(gStringVar4, str);
    return FALSE;
}

bool8 MEScrCmd_runscript(struct ScriptContext *ctx)
{
    u8 *script = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    ScriptContext2_RunNewScript(script);
    return FALSE;
}

bool8 MEScrCmd_setenigmaberry(struct ScriptContext *ctx)
{
    u8 *str;
    const u8 *message;
    bool32 haveBerry = IsEnigmaBerryValid();
    u8 *berry = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    StringCopyN(gStringVar1, gSaveBlock1Ptr->enigmaBerry.berry.name, BERRY_NAME_LENGTH + 1);
    SetEnigmaBerry(berry);
    StringCopyN(gStringVar2, gSaveBlock1Ptr->enigmaBerry.berry.name, BERRY_NAME_LENGTH + 1);

    if (!haveBerry)
    {
        str = gStringVar4;
        message = gText_MysteryGiftBerry;
    }
    else if (StringCompare(gStringVar1, gStringVar2))
    {
        str = gStringVar4;
        message = gText_MysteryGiftBerryTransform;
    }
    else
    {
        str = gStringVar4;
        message = gText_MysteryGiftBerryObtained;
    }

    StringExpandPlaceholders(str, message);

    ctx->data[2] = 2;

    if (IsEnigmaBerryValid() == TRUE)
        VarSet(VAR_ENIGMA_BERRY_AVAILABLE, 1);
    else
        ctx->data[2] = 1;

    return FALSE;
}

bool8 MEScrCmd_giveribbon(struct ScriptContext *ctx)
{
    u8 index = ScriptReadByte(ctx);
    u8 ribbonId = ScriptReadByte(ctx);
    GiveGiftRibbonToParty(index, ribbonId);
    StringExpandPlaceholders(gStringVar4, gText_MysteryGiftSpecialRibbon);
    ctx->data[2] = 2;
    return FALSE;
}

bool8 MEScrCmd_initramscript(struct ScriptContext *ctx)
{
    u8 mapGroup = ScriptReadByte(ctx);
    u8 mapNum = ScriptReadByte(ctx);
    u8 objectId = ScriptReadByte(ctx);
    u8 *script = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    u8 *scriptEnd = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    InitRamScript(script, scriptEnd - script, mapGroup, mapNum, objectId);
    return FALSE;
}

bool8 MEScrCmd_givenationaldex(struct ScriptContext *ctx)
{
    EnableNationalPokedex();
    StringExpandPlaceholders(gStringVar4, gText_MysteryGiftNationalDex);
    ctx->data[2] = 2;
    return FALSE;
}

bool8 MEScrCmd_addrareword(struct ScriptContext *ctx)
{
    EnableRareWord(ScriptReadByte(ctx));
    StringExpandPlaceholders(gStringVar4, gText_MysteryGiftRareWord);
    ctx->data[2] = 2;
    return FALSE;
}

bool8 MEScrCmd_setrecordmixinggift(struct ScriptContext *ctx)
{
    SetIncompatible();
    ctx->data[3] = 0;
    return TRUE;
}

bool8 MEScrCmd_givepokemon(struct ScriptContext *ctx)
{
    struct Mail mail;
    struct Pokemon pokemon;
    u16 species;
    u16 heldItem;
    u32 data = ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0];
    void *pokemonPtr = (void *)data;
    void *mailPtr = (void *)(data + sizeof(struct Pokemon));

    pokemon = *(struct Pokemon *)pokemonPtr;
    species = GetMonData(&pokemon, MON_DATA_SPECIES2);

    if (species == SPECIES_EGG)
        StringCopyN(gStringVar1, gText_EggNickname, POKEMON_NAME_LENGTH + 1);
    else
        StringCopyN(gStringVar1, gStartMenuText_Pokemon, POKEMON_NAME_LENGTH + 1);

    if (gPlayerPartyCount == PARTY_SIZE)
    {
        StringExpandPlaceholders(gStringVar4, gText_MysteryGiftFullParty);
        ctx->data[2] = 3;
    }
    else
    {
        memcpy(&gPlayerParty[5], pokemonPtr, sizeof(struct Pokemon));
        memcpy(&mail, mailPtr, sizeof(struct Mail));

        if (species != SPECIES_EGG)
        {
            u16 pokedexNum = SpeciesToNationalPokedexNum(species);
            GetSetPokedexFlag(pokedexNum, FLAG_SET_SEEN);
            GetSetPokedexFlag(pokedexNum, FLAG_SET_CAUGHT);
        }

        heldItem = GetMonData(&gPlayerParty[5], MON_DATA_HELD_ITEM);
        if (ItemIsMail(heldItem))
            GiveMailToMon2(&gPlayerParty[5], &mail);
        CompactPartySlots();
        CalculatePlayerPartyCount();
        StringExpandPlaceholders(gStringVar4, gText_MysteryGiftSentOver);
        ctx->data[2] = 2;
    }

    return FALSE;
}

bool8 MEScrCmd_addtrainer(struct ScriptContext *ctx)
{
    u32 data = ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0];
    memcpy(&gSaveBlock2Ptr->battleTower.ereaderTrainer, (void *)data, sizeof(struct BattleTowerEReaderTrainer));
    ValidateEReaderTrainer();
    StringExpandPlaceholders(gStringVar4, gText_MysteryGiftNewTrainer);
    ctx->data[2] = 2;
    return FALSE;
}

bool8 MEScrCmd_enableresetrtc(struct ScriptContext *ctx)
{
    SetIncompatible();
    ctx->data[3] = 0;
    return TRUE;
}

bool8 MEScrCmd_checksum(struct ScriptContext *ctx)
{
    int checksum = ScriptReadWord(ctx);
    u8 *data = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    u8 *dataEnd = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    if (checksum != CalcByteArraySum(data, dataEnd - data))
    {
        ctx->data[3] = 0;
        ctx->data[2] = 1;
    }
    return TRUE;
}

bool8 MEScrCmd_crc(struct ScriptContext *ctx)
{
    int crc = ScriptReadWord(ctx);
    u8 *data = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    u8 *dataEnd = (u8 *)(ScriptReadWord(ctx) - ctx->data[1] + ctx->data[0]);
    if (crc != CalcCRC16(data, dataEnd - data))
    {
        ctx->data[3] = 0;
        ctx->data[2] = 1;
    }
    return TRUE;
}