2019-04-02 16:54:51 +02:00
|
|
|
#include "global.h"
|
|
|
|
#include "alloc.h"
|
2019-04-02 17:43:29 +02:00
|
|
|
#include "decompress.h"
|
|
|
|
#include "overworld.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "battle_tower.h"
|
2019-04-02 16:54:51 +02:00
|
|
|
#include "mevent.h"
|
2019-04-02 17:43:29 +02:00
|
|
|
#include "mystery_event_script.h"
|
2019-04-02 20:02:13 +02:00
|
|
|
#include "mevent_client.h"
|
2019-04-02 16:54:51 +02:00
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
EWRAM_DATA struct mevent_client * s_mevent_client_ptr = NULL;
|
2019-04-02 17:43:29 +02:00
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static void mevent_client_init(struct mevent_client *, u32, u32);
|
|
|
|
static u32 mevent_client_exec(struct mevent_client *);
|
|
|
|
static void mevent_client_free_resources(struct mevent_client *);
|
2019-04-02 17:43:29 +02:00
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
extern const struct mevent_client_cmd gUnknown_082F2598[];
|
2019-04-02 17:43:29 +02:00
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
void mevent_client_do_init(u32 arg)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
s_mevent_client_ptr = AllocZeroed(sizeof(struct mevent_client));
|
|
|
|
mevent_client_init(s_mevent_client_ptr, 1, 0);
|
|
|
|
s_mevent_client_ptr->unk_4C = arg;
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
u32 mevent_client_do_exec(u16 * a0)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
u32 result;
|
2019-04-02 20:02:13 +02:00
|
|
|
if (s_mevent_client_ptr == NULL)
|
2019-04-02 17:43:29 +02:00
|
|
|
return 6;
|
2019-04-02 20:02:13 +02:00
|
|
|
result = mevent_client_exec(s_mevent_client_ptr);
|
2019-04-02 17:43:29 +02:00
|
|
|
if (result == 6)
|
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
*a0 = s_mevent_client_ptr->param;
|
|
|
|
mevent_client_free_resources(s_mevent_client_ptr);
|
|
|
|
Free(s_mevent_client_ptr);
|
|
|
|
s_mevent_client_ptr = NULL;
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
void mevent_client_inc_flag(void)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
s_mevent_client_ptr->flag++;
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
void * mevent_client_get_buffer(void)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
return s_mevent_client_ptr->buffer;
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
void mevent_client_set_param(u32 a0)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
s_mevent_client_ptr->param = a0;
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static void mevent_client_init(struct mevent_client * svr, u32 sendPlayerNo, u32 recvPlayerNo)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
svr->unk_00 = 0;
|
|
|
|
svr->mainseqno = 0;
|
|
|
|
svr->flag = 0;
|
|
|
|
svr->sendBuffer = AllocZeroed(ME_SEND_BUF_SIZE);
|
|
|
|
svr->recvBuffer = AllocZeroed(ME_SEND_BUF_SIZE);
|
|
|
|
svr->cmdBuffer = AllocZeroed(ME_SEND_BUF_SIZE);
|
|
|
|
svr->buffer = AllocZeroed(0x40);
|
|
|
|
mevent_srv_sub_init(&svr->manager, sendPlayerNo, recvPlayerNo);
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static void mevent_client_free_resources(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
Free(svr->sendBuffer);
|
|
|
|
Free(svr->recvBuffer);
|
|
|
|
Free(svr->cmdBuffer);
|
|
|
|
Free(svr->buffer);
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static void mevent_client_jmp_buffer(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
memcpy(svr->cmdBuffer, svr->recvBuffer, ME_SEND_BUF_SIZE);
|
|
|
|
svr->cmdidx = 0;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static void mevent_client_send_word(struct mevent_client * svr, u32 ident, u32 word)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
CpuFill32(0, svr->sendBuffer, ME_SEND_BUF_SIZE);
|
|
|
|
*(u32 *)svr->sendBuffer = word;
|
|
|
|
mevent_srv_sub_init_send(&svr->manager, ident, svr->sendBuffer, sizeof(u32));
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_0(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// init
|
|
|
|
memcpy(svr->cmdBuffer, gUnknown_082F2598, ME_SEND_BUF_SIZE);
|
|
|
|
svr->cmdidx = 0;
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_1(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// done
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_2(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// do recv
|
|
|
|
if (mevent_srv_sub_recv(&svr->manager))
|
|
|
|
{
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_3(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// do send
|
|
|
|
if (mevent_srv_sub_send(&svr->manager))
|
|
|
|
{
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_4(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// process command
|
2019-04-02 20:02:13 +02:00
|
|
|
struct mevent_client_cmd * cmd = &svr->cmdBuffer[svr->cmdidx];
|
2019-04-02 17:43:29 +02:00
|
|
|
++svr->cmdidx;
|
|
|
|
switch (cmd->instr)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
svr->param = cmd->parameter;
|
|
|
|
svr->mainseqno = 1;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mevent_srv_sub_init_recv(&svr->manager, cmd->parameter, svr->recvBuffer);
|
|
|
|
svr->mainseqno = 2;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
svr->mainseqno = 3;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 20:
|
|
|
|
mevent_srv_sub_init_send(&svr->manager, 0x14, svr->sendBuffer, 0);
|
|
|
|
svr->mainseqno = 3;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 19:
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_send_word(svr, 0x12, GetGameStat(cmd->parameter));
|
2019-04-02 17:43:29 +02:00
|
|
|
svr->mainseqno = 3;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (svr->param == 0)
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_jmp_buffer(svr);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (svr->param == 1)
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_jmp_buffer(svr);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_jmp_buffer(svr);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
memcpy(svr->buffer, svr->recvBuffer, 0x40);
|
|
|
|
svr->mainseqno = 5;
|
|
|
|
svr->flag = 0;
|
|
|
|
return 2;
|
|
|
|
case 11:
|
|
|
|
memcpy(svr->buffer, svr->recvBuffer, 0x40);
|
|
|
|
svr->mainseqno = 5;
|
|
|
|
svr->flag = 0;
|
|
|
|
return 3;
|
|
|
|
case 12:
|
|
|
|
memcpy(svr->buffer, svr->recvBuffer, 0x40);
|
|
|
|
svr->mainseqno = 5;
|
|
|
|
svr->flag = 0;
|
|
|
|
return 5;
|
|
|
|
case 13:
|
|
|
|
svr->mainseqno = 5;
|
|
|
|
svr->flag = 0;
|
|
|
|
return 4;
|
|
|
|
case 8:
|
|
|
|
sub_801B580(svr->sendBuffer, svr->unk_4C);
|
|
|
|
mevent_srv_sub_init_send(&svr->manager, 0x11, svr->sendBuffer, sizeof(struct MEventStruct_Unk1442CC));
|
|
|
|
break;
|
|
|
|
case 14:
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_send_word(svr, 0x13, svr->param);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
sub_801B21C(svr->recvBuffer);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
if (!sub_801B1A4(svr->recvBuffer))
|
|
|
|
{
|
|
|
|
sub_801B078(svr->recvBuffer);
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_send_word(svr, 0x13, 0);
|
2019-04-02 17:43:29 +02:00
|
|
|
}
|
|
|
|
else
|
2019-04-02 20:02:13 +02:00
|
|
|
mevent_client_send_word(svr, 0x13, 1);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
svr->mainseqno = 6;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
sub_801B508(svr->recvBuffer);
|
|
|
|
break;
|
|
|
|
case 17:
|
2019-04-02 19:57:03 +02:00
|
|
|
InitRamScript_NoEventObject(svr->recvBuffer, 1000);
|
2019-04-02 17:43:29 +02:00
|
|
|
break;
|
|
|
|
case 18:
|
|
|
|
memcpy(&gSaveBlock2Ptr->frontier.ereaderTrainer, svr->recvBuffer, 0xbc);
|
|
|
|
ValidateEReaderTrainer();
|
|
|
|
break;
|
|
|
|
case 21:
|
|
|
|
memcpy(gDecompressionBuffer, svr->recvBuffer, ME_SEND_BUF_SIZE);
|
|
|
|
svr->mainseqno = 7;
|
|
|
|
svr->flag = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_5(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// wait flag
|
|
|
|
if (svr->flag)
|
|
|
|
{
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_6(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// ???
|
|
|
|
switch (svr->flag)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
sub_8153870(svr->recvBuffer);
|
|
|
|
++svr->flag;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (!sub_8153884(&svr->param))
|
|
|
|
{
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:48:27 +02:00
|
|
|
static u32 mainseq_7(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
|
|
|
// exec arbitrary code
|
|
|
|
u32 (*func)(u32 *, struct SaveBlock2 *, struct SaveBlock1 *) = (void *)gDecompressionBuffer;
|
|
|
|
if (func(&svr->param, gSaveBlock2Ptr, gSaveBlock1Ptr) == 1)
|
|
|
|
{
|
|
|
|
svr->mainseqno = 4;
|
|
|
|
svr->flag = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-02 20:02:13 +02:00
|
|
|
static u32 mevent_client_exec(struct mevent_client * svr)
|
2019-04-02 17:43:29 +02:00
|
|
|
{
|
2019-04-02 20:02:13 +02:00
|
|
|
u32 (*funcs[])(struct mevent_client *) = {
|
2019-04-02 20:48:27 +02:00
|
|
|
mainseq_0,
|
|
|
|
mainseq_1,
|
|
|
|
mainseq_2,
|
|
|
|
mainseq_3,
|
|
|
|
mainseq_4,
|
|
|
|
mainseq_5,
|
|
|
|
mainseq_6,
|
|
|
|
mainseq_7
|
2019-04-02 17:43:29 +02:00
|
|
|
};
|
|
|
|
return funcs[svr->mainseqno](svr);
|
|
|
|
}
|