pokeemerald/src/mevent2.c

627 lines
15 KiB
C
Raw Normal View History

2019-03-24 23:20:35 +01:00
#include "global.h"
#include "util.h"
#include "main.h"
#include "event_data.h"
#include "easy_chat.h"
#include "script.h"
#include "battle_tower.h"
#include "mevent_news.h"
#include "string_util.h"
#include "new_game.h"
#include "mevent.h"
#include "constants/species.h"
static EWRAM_DATA bool32 gUnknown_02022C70 = FALSE;
2019-03-25 00:13:32 +01:00
static void sub_801B180(void);
2019-04-01 00:59:52 +02:00
static void s_DestroyWonderNews(void);
2019-03-25 00:13:32 +01:00
static bool32 sub_801B114(const struct MEventBuffer_3120_Sub *data);
static bool32 sub_801B2CC(const struct MEventBuffer_32E0_Sub *data);
static void sub_801B330(void);
static void sub_801B368(void);
static void sub_801B9F8(void);
static void sub_801BA8C(u32 a0, u32 a1, u32 *a2, int a3);
2019-03-24 23:20:35 +01:00
void sub_801AFD8(void)
{
CpuFill32(0, &gSaveBlock1Ptr->unk_322C, sizeof(gSaveBlock1Ptr->unk_322C));
sub_801B180();
sub_811F8BC();
}
2019-04-01 00:59:52 +02:00
struct MEventBuffer_3120_Sub *sav1_get_mevent_buffer_0(void)
2019-03-24 23:20:35 +01:00
{
return &gSaveBlock1Ptr->unk_322C.buffer_000.data;
}
2019-03-25 00:13:32 +01:00
struct MEventBuffer_32E0_Sub *sav1_get_mevent_buffer_1(void)
2019-03-24 23:20:35 +01:00
{
return &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
}
2019-03-25 00:13:32 +01:00
struct MEventBuffer_3430_Sub *sav1_get_mevent_buffer_2(void)
2019-03-24 23:20:35 +01:00
{
return &gSaveBlock1Ptr->unk_322C.buffer_310.data;
}
2019-03-25 00:13:32 +01:00
struct MysteryEventStruct *sub_801B044(void)
2019-03-24 23:20:35 +01:00
{
return &gSaveBlock1Ptr->unk_322C.unk_340;
}
2019-03-25 00:13:32 +01:00
u16 *sub_801B058(void)
2019-03-24 23:20:35 +01:00
{
return gSaveBlock1Ptr->unk_322C.unk_338;
}
2019-04-01 00:59:52 +02:00
void DestroyWonderNews(void)
2019-03-24 23:20:35 +01:00
{
2019-04-01 00:59:52 +02:00
s_DestroyWonderNews();
2019-03-24 23:20:35 +01:00
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B078(const struct MEventBuffer_3120_Sub *src)
2019-03-24 23:20:35 +01:00
{
if (!sub_801B114(src))
return FALSE;
2019-04-01 00:59:52 +02:00
s_DestroyWonderNews();
2019-03-24 23:20:35 +01:00
gSaveBlock1Ptr->unk_322C.buffer_000.data = *src;
gSaveBlock1Ptr->unk_322C.buffer_000.crc = CalcCRC16WithTable((void *)&gSaveBlock1Ptr->unk_322C.buffer_000.data, sizeof(struct MEventBuffer_3120_Sub));
return TRUE;
}
2019-04-01 00:59:52 +02:00
bool32 ValidateReceivedWonderNews(void)
2019-03-24 23:20:35 +01:00
{
if (CalcCRC16WithTable((void *)&gSaveBlock1Ptr->unk_322C.buffer_000.data, sizeof(struct MEventBuffer_3120_Sub)) != gSaveBlock1Ptr->unk_322C.buffer_000.crc)
return FALSE;
if (!sub_801B114(&gSaveBlock1Ptr->unk_322C.buffer_000.data))
return FALSE;
return TRUE;
}
2019-03-25 00:13:32 +01:00
static bool32 sub_801B114(const struct MEventBuffer_3120_Sub *data)
2019-03-24 23:20:35 +01:00
{
if (data->unk_00 == 0)
return FALSE;
return TRUE;
}
2019-04-01 00:59:52 +02:00
bool32 WonderNews_Test_Unk_02(void)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
const struct MEventBuffer_3120_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_000.data;
2019-03-24 23:20:35 +01:00
if (data->unk_02 == 0)
return FALSE;
return TRUE;
}
2019-04-01 00:59:52 +02:00
static void s_DestroyWonderNews(void)
2019-03-24 23:20:35 +01:00
{
2019-04-01 00:59:52 +02:00
CpuFill32(0, sav1_get_mevent_buffer_0(), sizeof(gSaveBlock1Ptr->unk_322C.buffer_000.data));
2019-03-24 23:20:35 +01:00
gSaveBlock1Ptr->unk_322C.buffer_000.crc = 0;
}
2019-03-25 00:13:32 +01:00
static void sub_801B180(void)
2019-03-24 23:20:35 +01:00
{
CpuFill32(0, sub_801B044(), sizeof(struct MysteryEventStruct));
sub_801DBC0();
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B1A4(const u8 *src)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
const u8 *r5 = (const u8 *)&gSaveBlock1Ptr->unk_322C.buffer_000.data;
2019-03-24 23:20:35 +01:00
u32 i;
2019-04-01 00:59:52 +02:00
if (!ValidateReceivedWonderNews())
2019-03-24 23:20:35 +01:00
return FALSE;
for (i = 0; i < sizeof(struct MEventBuffer_3120_Sub); i++)
{
if (r5[i] != src[i])
return FALSE;
}
return TRUE;
}
2019-04-01 00:59:52 +02:00
void DestroyWonderCard(void)
2019-03-24 23:20:35 +01:00
{
sub_801B330();
sub_801B368();
sub_801B9F8();
ClearRamScript();
sub_809D4D8();
sub_809D570();
ClearEReaderTrainer(&gSaveBlock2Ptr->frontier.ereaderTrainer);
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B21C(const struct MEventBuffer_32E0_Sub *data)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_3430_Sub *r2;
struct MEventBuffer_32E0_Sub *r1;
2019-03-24 23:20:35 +01:00
if (!sub_801B2CC(data))
return FALSE;
2019-04-01 00:59:52 +02:00
DestroyWonderCard();
2019-03-24 23:20:35 +01:00
memcpy(&gSaveBlock1Ptr->unk_322C.buffer_1c0.data, data, sizeof(struct MEventBuffer_32E0_Sub));
gSaveBlock1Ptr->unk_322C.buffer_1c0.crc = CalcCRC16WithTable((void *)&gSaveBlock1Ptr->unk_322C.buffer_1c0.data, sizeof(struct MEventBuffer_32E0_Sub));
r2 = &gSaveBlock1Ptr->unk_322C.buffer_310.data;
r1 = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
r2->unk_06 = r1->unk_02;
return TRUE;
}
2019-04-01 00:59:52 +02:00
bool32 ValidateReceivedWonderCard(void)
2019-03-24 23:20:35 +01:00
{
if (gSaveBlock1Ptr->unk_322C.buffer_1c0.crc != CalcCRC16WithTable((void *)&gSaveBlock1Ptr->unk_322C.buffer_1c0.data, sizeof(struct MEventBuffer_32E0_Sub)))
return FALSE;
if (!sub_801B2CC(&gSaveBlock1Ptr->unk_322C.buffer_1c0.data))
return FALSE;
if (!sub_80991F8())
return FALSE;
return TRUE;
}
2019-03-25 00:13:32 +01:00
static bool32 sub_801B2CC(const struct MEventBuffer_32E0_Sub *data)
2019-03-24 23:20:35 +01:00
{
if (data->unk_00 == 0)
return FALSE;
if (data->unk_08_0 > 2)
return FALSE;
if (!(data->unk_08_6 == 0 || data->unk_08_6 == 1 || data->unk_08_6 == 2))
return FALSE;
if (data->unk_08_2 > 7)
return FALSE;
if (data->unk_09 > 7)
return FALSE;
return TRUE;
}
2019-04-01 00:59:52 +02:00
bool32 WonderCard_Test_Unk_08_6(void)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
const struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
2019-03-24 23:20:35 +01:00
if (data->unk_08_6 == 0)
return FALSE;
return TRUE;
}
2019-03-25 00:13:32 +01:00
static void sub_801B330(void)
2019-03-24 23:20:35 +01:00
{
CpuFill32(0, &gSaveBlock1Ptr->unk_322C.buffer_1c0.data, sizeof(struct MEventBuffer_32E0_Sub));
gSaveBlock1Ptr->unk_322C.buffer_1c0.crc = 0;
}
2019-03-25 00:13:32 +01:00
static void sub_801B368(void)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
CpuFill32(0, sav1_get_mevent_buffer_2(), 18 *sizeof(u16));
2019-03-24 23:20:35 +01:00
gSaveBlock1Ptr->unk_322C.buffer_310.crc = 0;
}
2019-04-01 00:59:52 +02:00
u16 GetWonderCardFlagID(void)
2019-03-24 23:20:35 +01:00
{
2019-04-01 00:59:52 +02:00
if (ValidateReceivedWonderCard())
2019-03-24 23:20:35 +01:00
return gSaveBlock1Ptr->unk_322C.buffer_1c0.data.unk_00;
return 0;
}
2019-03-25 00:13:32 +01:00
void sub_801B3C0(struct MEventBuffer_32E0_Sub *buffer)
2019-03-24 23:20:35 +01:00
{
if (buffer->unk_08_6 == 1)
buffer->unk_08_6 = 0;
}
2019-04-01 00:59:52 +02:00
static bool32 IsWonderCardFlagIDInValidRange(u16 a0)
2019-03-24 23:20:35 +01:00
{
if (a0 >= 1000 && a0 < 1020)
return TRUE;
return FALSE;
}
static const u16 sMysteryGiftFlags[] =
{
FLAG_RECEIVED_AURORA_TICKET,
FLAG_RECEIVED_MYSTIC_TICKET,
FLAG_RECEIVED_OLD_SEA_MAP,
FLAG_UNUSED_MYSTERY_GIFT_0x13D,
FLAG_UNUSED_MYSTERY_GIFT_0x13E,
FLAG_UNUSED_MYSTERY_GIFT_0x13F,
FLAG_UNUSED_MYSTERY_GIFT_0x140,
FLAG_UNUSED_MYSTERY_GIFT_0x141,
FLAG_UNUSED_MYSTERY_GIFT_0x142,
FLAG_UNUSED_MYSTERY_GIFT_0x143,
FLAG_UNUSED_MYSTERY_GIFT_0x144,
FLAG_UNUSED_MYSTERY_GIFT_0x145,
FLAG_UNUSED_MYSTERY_GIFT_0x146,
FLAG_UNUSED_MYSTERY_GIFT_0x147,
FLAG_UNUSED_MYSTERY_GIFT_0x148,
FLAG_UNUSED_MYSTERY_GIFT_0x149,
FLAG_UNUSED_MYSTERY_GIFT_0x14A,
FLAG_UNUSED_MYSTERY_GIFT_0x14B,
FLAG_UNUSED_MYSTERY_GIFT_0x14C,
FLAG_UNUSED_MYSTERY_GIFT_0x14D,
};
2019-04-01 00:59:52 +02:00
bool32 CheckReceivedGiftFromWonderCard(void)
2019-03-24 23:20:35 +01:00
{
2019-04-01 00:59:52 +02:00
u16 value = GetWonderCardFlagID();
if (!IsWonderCardFlagIDInValidRange(value))
2019-03-24 23:20:35 +01:00
return FALSE;
if (FlagGet(sMysteryGiftFlags[value - 1000]) == TRUE)
return FALSE;
return TRUE;
}
2019-03-25 00:13:32 +01:00
static int sub_801B438(const struct MEventBuffer_3430_Sub *data, int size)
2019-03-24 23:20:35 +01:00
{
int r3 = 0;
int i;
for (i = 0; i < size; i++)
{
if (data->unk_08[1][i] && data->unk_08[0][i])
r3++;
}
return r3;
}
2019-03-25 00:13:32 +01:00
static bool32 sub_801B460(const struct MEventBuffer_3430_Sub *data1, const u16 *data2, int size)
2019-03-24 23:20:35 +01:00
{
int i;
for (i = 0; i < size; i++)
{
if (data1->unk_08[1][i] == data2[1])
return TRUE;
if (data1->unk_08[0][i] == data2[0])
return TRUE;
}
return FALSE;
}
2019-03-25 00:13:32 +01:00
static bool32 sub_801B4A4(const u16 *data)
2019-03-24 23:20:35 +01:00
{
if (data[1] == 0)
return FALSE;
if (data[0] == 0)
return FALSE;
if (data[0] >= NUM_SPECIES)
return FALSE;
return TRUE;
}
2019-03-25 00:13:32 +01:00
static int sub_801B4CC(void)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_32E0_Sub *data;
2019-04-01 00:59:52 +02:00
if (!ValidateReceivedWonderCard())
2019-03-24 23:20:35 +01:00
return 0;
data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 != 1)
return 0;
return sub_801B438(&gSaveBlock1Ptr->unk_322C.buffer_310.data, data->unk_09);
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B508(const u16 *data)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_32E0_Sub *buffer = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
2019-03-24 23:20:35 +01:00
int size = buffer->unk_09;
int i;
if (!sub_801B4A4(data))
return FALSE;
if (sub_801B460(&gSaveBlock1Ptr->unk_322C.buffer_310.data, data, size))
return FALSE;
for (i = 0; i < size; i++)
{
if (gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_08[1][i] == 0 && gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_08[0][i] == 0)
{
gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_08[1][i] = data[1];
gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_08[0][i] = data[0];
return TRUE;
}
}
return FALSE;
}
2019-03-25 00:13:32 +01:00
void sub_801B580(struct MEventStruct_Unk1442CC *data, bool32 a1)
2019-03-24 23:20:35 +01:00
{
int i;
CpuFill32(0, data, sizeof(struct MEventStruct_Unk1442CC));
data->unk_00 = 0x101;
data->unk_04 = 1;
data->unk_08 = 1;
if (a1)
{
data->unk_0C = 5;
data->unk_10 = 0x0201;
}
else
{
data->unk_0C = 4;
data->unk_10 = 0x0200;
}
2019-04-01 00:59:52 +02:00
if (ValidateReceivedWonderCard())
2019-03-24 23:20:35 +01:00
{
data->unk_14 = sav1_get_mevent_buffer_1()->unk_00;
data->unk_20 = *sav1_get_mevent_buffer_2();
data->unk_44 = sav1_get_mevent_buffer_1()->unk_09;
}
else
{
data->unk_14 = 0;
}
for (i = 0; i < 4; i++)
data->unk_16[i] = gSaveBlock1Ptr->unk_322C.unk_338[i];
CopyTrainerId(data->unk_4C, gSaveBlock2Ptr->playerTrainerId);
StringCopy(data->unk_45, gSaveBlock2Ptr->playerName);
for (i = 0; i < 6; i++)
data->unk_50[i] = gSaveBlock1Ptr->easyChatProfile[i];
memcpy(data->unk_5C, RomHeaderGameCode, 4);
data->unk_60 = RomHeaderSoftwareVersion;
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B6A0(const struct MEventStruct_Unk1442CC *data, bool32 a1)
2019-03-24 23:20:35 +01:00
{
if (data->unk_00 != 0x101)
return FALSE;
if (!(data->unk_04 & 1))
return FALSE;
if (!(data->unk_08 & 1))
return FALSE;
if (!a1)
{
if (!(data->unk_0C & 4))
return FALSE;
if (!(data->unk_10 & 0x380))
return FALSE;
}
return TRUE;
}
2019-03-25 00:13:32 +01:00
u32 sub_801B6EC(const u16 *a0, const struct MEventStruct_Unk1442CC *a1, void *unused)
2019-03-24 23:20:35 +01:00
{
if (a1->unk_14 == 0)
return 0;
if (*a0 == a1->unk_14)
return 1;
return 2;
}
2019-03-25 00:13:32 +01:00
u32 sub_801B708(const u16 *a0, const struct MEventStruct_Unk1442CC *a1, void *unused)
2019-03-24 23:20:35 +01:00
{
int r4 = a1->unk_44 - sub_801B438(&a1->unk_20, a1->unk_44);
if (r4 == 0)
return 1;
if (sub_801B460(&a1->unk_20, a0, a1->unk_44))
return 3;
if (r4 == 1)
return 4;
return 2;
}
2019-03-25 00:13:32 +01:00
bool32 sub_801B748(const struct MEventStruct_Unk1442CC *a0, const u16 *a1)
2019-03-24 23:20:35 +01:00
{
int i;
for (i = 0; i < 4; i++)
{
if (a0->unk_16[i] != a1[i])
return FALSE;
}
return TRUE;
}
2019-03-25 00:13:32 +01:00
static int sub_801B770(const struct MEventStruct_Unk1442CC *a0)
2019-03-24 23:20:35 +01:00
{
return sub_801B438(&a0->unk_20, a0->unk_44);
}
2019-03-25 00:13:32 +01:00
u16 sub_801B784(const struct MEventStruct_Unk1442CC *a0, u32 command)
2019-03-24 23:20:35 +01:00
{
switch (command)
{
case 0:
return a0->unk_20.unk_00;
case 1:
return a0->unk_20.unk_02;
case 2:
return a0->unk_20.unk_04;
case 3:
return sub_801B770(a0);
case 4:
return a0->unk_44;
default:
AGB_ASSERT(0);
return 0;
}
}
2019-03-25 00:13:32 +01:00
static void sub_801B7D8(u32 command)
2019-03-24 23:20:35 +01:00
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
2019-03-24 23:20:35 +01:00
if (data->unk_08_0 == 2)
{
2019-03-25 00:13:32 +01:00
u16 *dest = NULL;
2019-03-24 23:20:35 +01:00
switch (command)
{
case 0:
dest = &gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_00;
break;
case 1:
dest = &gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_02;
break;
case 2:
dest = &gSaveBlock1Ptr->unk_322C.buffer_310.data.unk_04;
break;
case 3:
break;
case 4:
break;
}
if (dest == NULL)
AGB_ASSERT(0);
else if (++(*dest) > 999)
*dest = 999;
}
}
u16 mevent_081445C0(u32 command)
{
switch (command)
{
case 0:
{
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 == 2)
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_3430_Sub *buffer = &gSaveBlock1Ptr->unk_322C.buffer_310.data;
2019-03-24 23:20:35 +01:00
return buffer->unk_00;
}
break;
}
case 1:
{
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 == 2)
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_3430_Sub *buffer = &gSaveBlock1Ptr->unk_322C.buffer_310.data;
2019-03-24 23:20:35 +01:00
return buffer->unk_02;
}
break;
}
case 2:
{
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 == 2)
{
2019-03-25 00:13:32 +01:00
struct MEventBuffer_3430_Sub *buffer = &gSaveBlock1Ptr->unk_322C.buffer_310.data;
2019-03-24 23:20:35 +01:00
return buffer->unk_04;
}
break;
}
case 3:
{
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 == 1)
return sub_801B4CC();
break;
}
case 4:
{
struct MEventBuffer_32E0_Sub *data = &gSaveBlock1Ptr->unk_322C.buffer_1c0.data;
if (data->unk_08_0 == 1)
return data->unk_09;
break;
}
}
AGB_ASSERT(0);
return 0;
}
void sub_801B940(void)
{
gUnknown_02022C70 = FALSE;
}
bool32 sub_801B94C(u16 a0)
{
gUnknown_02022C70 = FALSE;
if (a0 == 0)
return FALSE;
2019-04-01 00:59:52 +02:00
if (!ValidateReceivedWonderCard())
2019-03-24 23:20:35 +01:00
return FALSE;
if (gSaveBlock1Ptr->unk_322C.buffer_1c0.data.unk_00 != a0)
return FALSE;
gUnknown_02022C70 = TRUE;
return TRUE;
}
void sub_801B990(u32 a0, u32 a1)
{
if (gUnknown_02022C70)
{
switch (a0)
{
case 2:
sub_801BA8C(2, a1, gSaveBlock1Ptr->unk_322C.unk_344[1], 5);
break;
case 0:
sub_801BA8C(0, a1, gSaveBlock1Ptr->unk_322C.unk_344[0], 5);
break;
case 1:
sub_801BA8C(1, a1, gSaveBlock1Ptr->unk_322C.unk_344[0], 5);
break;
default:
AGB_ASSERT(0);
}
}
}
2019-03-25 00:13:32 +01:00
static void sub_801B9F8(void)
2019-03-24 23:20:35 +01:00
{
CpuFill32(0, gSaveBlock1Ptr->unk_322C.unk_344, sizeof(gSaveBlock1Ptr->unk_322C.unk_344));
}
2019-03-25 00:13:32 +01:00
static bool32 sub_801BA24(u32 a0, u32 *a1, int size)
2019-03-24 23:20:35 +01:00
{
int i;
int j;
for (i = 0; i < size; i++)
{
if (a1[i] == a0)
break;
}
if (i == size)
{
for (j = size - 1; j > 0; j--)
a1[j] = a1[j - 1];
a1[0] = a0;
return TRUE;
}
else
{
for (j = i; j > 0; j--)
a1[j] = a1[j - 1];
a1[0] = a0;
return FALSE;
}
}
2019-03-25 00:13:32 +01:00
static void sub_801BA8C(u32 a0, u32 a1, u32 *a2, int a3)
2019-03-24 23:20:35 +01:00
{
if (sub_801BA24(a1, a2, a3))
sub_801B7D8(a0);
}