pokeemerald/src/script.c

471 lines
11 KiB
C
Raw Normal View History

2017-09-10 23:05:23 +02:00
#include "global.h"
#include "script.h"
#include "event_data.h"
#include "mystery_gift.h"
2017-09-10 23:05:23 +02:00
#include "util.h"
2021-11-15 16:57:22 +01:00
#include "constants/event_objects.h"
#include "constants/map_scripts.h"
2017-09-10 23:05:23 +02:00
#define RAM_SCRIPT_MAGIC 51
2021-02-20 00:52:24 +01:00
enum {
SCRIPT_MODE_STOPPED,
SCRIPT_MODE_BYTECODE,
SCRIPT_MODE_NATIVE,
};
enum {
CONTEXT_RUNNING,
CONTEXT_WAITING,
CONTEXT_SHUTDOWN,
};
extern const u8 *gRamScriptRetAddr;
2017-09-10 23:05:23 +02:00
static u8 sGlobalScriptContextStatus;
static struct ScriptContext sGlobalScriptContext;
static struct ScriptContext sImmediateScriptContext;
static bool8 sLockFieldControls;
2017-09-10 23:05:23 +02:00
extern ScrCmdFunc gScriptCmdTable[];
extern ScrCmdFunc gScriptCmdTableEnd[];
extern void *gNullScriptPtr;
void InitScriptContext(struct ScriptContext *ctx, void *cmdTable, void *cmdTableEnd)
{
s32 i;
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_STOPPED;
ctx->scriptPtr = NULL;
2017-09-10 23:05:23 +02:00
ctx->stackDepth = 0;
2021-02-20 00:52:24 +01:00
ctx->nativePtr = NULL;
2017-09-10 23:05:23 +02:00
ctx->cmdTable = cmdTable;
ctx->cmdTableEnd = cmdTableEnd;
2021-02-20 00:52:24 +01:00
for (i = 0; i < (int)ARRAY_COUNT(ctx->data); i++)
2017-09-10 23:05:23 +02:00
ctx->data[i] = 0;
2021-02-20 00:52:24 +01:00
for (i = 0; i < (int)ARRAY_COUNT(ctx->stack); i++)
ctx->stack[i] = NULL;
2017-09-10 23:05:23 +02:00
}
u8 SetupBytecodeScript(struct ScriptContext *ctx, const u8 *ptr)
{
ctx->scriptPtr = ptr;
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_BYTECODE;
2017-09-10 23:05:23 +02:00
return 1;
}
void SetupNativeScript(struct ScriptContext *ctx, bool8 (*ptr)(void))
{
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_NATIVE;
2017-09-10 23:05:23 +02:00
ctx->nativePtr = ptr;
}
void StopScript(struct ScriptContext *ctx)
{
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_STOPPED;
ctx->scriptPtr = NULL;
2017-09-10 23:05:23 +02:00
}
bool8 RunScriptCommand(struct ScriptContext *ctx)
{
2021-02-20 00:52:24 +01:00
if (ctx->mode == SCRIPT_MODE_STOPPED)
2017-09-10 23:05:23 +02:00
return FALSE;
switch (ctx->mode)
{
2021-02-20 00:52:24 +01:00
case SCRIPT_MODE_STOPPED:
2017-09-10 23:05:23 +02:00
return FALSE;
2021-02-20 00:52:24 +01:00
case SCRIPT_MODE_NATIVE:
// Try to call a function in C
2021-02-24 17:26:26 +01:00
// Continue to bytecode if no function or it returns TRUE
2017-09-10 23:05:23 +02:00
if (ctx->nativePtr)
{
if (ctx->nativePtr() == TRUE)
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_BYTECODE;
2017-09-10 23:05:23 +02:00
return TRUE;
}
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_BYTECODE;
// fallthrough
case SCRIPT_MODE_BYTECODE:
2017-09-10 23:05:23 +02:00
while (1)
{
u8 cmdCode;
ScrCmdFunc *func;
if (!ctx->scriptPtr)
{
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_STOPPED;
2017-09-10 23:05:23 +02:00
return FALSE;
}
if (ctx->scriptPtr == gNullScriptPtr)
{
while (1)
asm("svc 2"); // HALT
}
cmdCode = *(ctx->scriptPtr);
ctx->scriptPtr++;
func = &ctx->cmdTable[cmdCode];
if (func >= ctx->cmdTableEnd)
{
2021-02-20 00:52:24 +01:00
ctx->mode = SCRIPT_MODE_STOPPED;
2017-09-10 23:05:23 +02:00
return FALSE;
}
2021-02-20 00:52:24 +01:00
if ((*func)(ctx) == TRUE)
2017-09-10 23:05:23 +02:00
return TRUE;
}
}
return TRUE;
}
2021-02-20 00:52:24 +01:00
static bool8 ScriptPush(struct ScriptContext *ctx, const u8 *ptr)
2017-09-10 23:05:23 +02:00
{
2021-02-20 00:52:24 +01:00
if (ctx->stackDepth + 1 >= (int)ARRAY_COUNT(ctx->stack))
2017-09-10 23:05:23 +02:00
{
2021-02-20 00:52:24 +01:00
return TRUE;
2017-09-10 23:05:23 +02:00
}
else
{
ctx->stack[ctx->stackDepth] = ptr;
ctx->stackDepth++;
2021-02-20 00:52:24 +01:00
return FALSE;
2017-09-10 23:05:23 +02:00
}
}
2021-02-20 00:52:24 +01:00
static const u8 *ScriptPop(struct ScriptContext *ctx)
2017-09-10 23:05:23 +02:00
{
if (ctx->stackDepth == 0)
return NULL;
ctx->stackDepth--;
return ctx->stack[ctx->stackDepth];
}
2017-10-12 21:36:26 +02:00
void ScriptJump(struct ScriptContext *ctx, const u8 *ptr)
2017-09-10 23:05:23 +02:00
{
ctx->scriptPtr = ptr;
}
2017-10-12 21:36:26 +02:00
void ScriptCall(struct ScriptContext *ctx, const u8 *ptr)
2017-09-10 23:05:23 +02:00
{
ScriptPush(ctx, ctx->scriptPtr);
ctx->scriptPtr = ptr;
}
void ScriptReturn(struct ScriptContext *ctx)
{
ctx->scriptPtr = ScriptPop(ctx);
}
u16 ScriptReadHalfword(struct ScriptContext *ctx)
{
u16 value = *(ctx->scriptPtr++);
value |= *(ctx->scriptPtr++) << 8;
return value;
}
u32 ScriptReadWord(struct ScriptContext *ctx)
{
u32 value0 = *(ctx->scriptPtr++);
u32 value1 = *(ctx->scriptPtr++);
u32 value2 = *(ctx->scriptPtr++);
u32 value3 = *(ctx->scriptPtr++);
return (((((value3 << 8) + value2) << 8) + value1) << 8) + value0;
}
void LockPlayerFieldControls(void)
2017-09-10 23:05:23 +02:00
{
sLockFieldControls = TRUE;
2017-09-10 23:05:23 +02:00
}
void UnlockPlayerFieldControls(void)
2017-09-10 23:05:23 +02:00
{
sLockFieldControls = FALSE;
2017-09-10 23:05:23 +02:00
}
bool8 ArePlayerFieldControlsLocked(void)
2017-09-10 23:05:23 +02:00
{
return sLockFieldControls;
2017-09-10 23:05:23 +02:00
}
// The ScriptContext_* functions work with the primary script context,
// which yields control back to native code should the script make a wait call.
// Checks if the global script context is able to be run right now.
bool8 ScriptContext_IsEnabled(void)
2017-09-10 23:05:23 +02:00
{
if (sGlobalScriptContextStatus == CONTEXT_RUNNING)
2017-09-10 23:05:23 +02:00
return TRUE;
else
return FALSE;
}
// Re-initializes the global script context to zero.
void ScriptContext_Init(void)
2017-09-10 23:05:23 +02:00
{
InitScriptContext(&sGlobalScriptContext, gScriptCmdTable, gScriptCmdTableEnd);
sGlobalScriptContextStatus = CONTEXT_SHUTDOWN;
2017-09-10 23:05:23 +02:00
}
2022-09-11 20:14:49 +02:00
// Runs the script until the script makes a wait* call, then returns true if
// there's more script to run, or false if the script has hit the end.
// This function also returns false if the context is finished
// or waiting (after a call to _Stop)
bool8 ScriptContext_RunScript(void)
2017-09-10 23:05:23 +02:00
{
if (sGlobalScriptContextStatus == CONTEXT_SHUTDOWN)
2021-02-20 00:52:24 +01:00
return FALSE;
2017-09-10 23:05:23 +02:00
if (sGlobalScriptContextStatus == CONTEXT_WAITING)
2021-02-20 00:52:24 +01:00
return FALSE;
2017-09-10 23:05:23 +02:00
LockPlayerFieldControls();
2017-09-10 23:05:23 +02:00
if (!RunScriptCommand(&sGlobalScriptContext))
2017-09-10 23:05:23 +02:00
{
sGlobalScriptContextStatus = CONTEXT_SHUTDOWN;
UnlockPlayerFieldControls();
2021-02-20 00:52:24 +01:00
return FALSE;
2017-09-10 23:05:23 +02:00
}
2021-02-20 00:52:24 +01:00
return TRUE;
2017-09-10 23:05:23 +02:00
}
// Sets up a new script in the global context and enables the context
void ScriptContext_SetupScript(const u8 *ptr)
2017-09-10 23:05:23 +02:00
{
InitScriptContext(&sGlobalScriptContext, gScriptCmdTable, gScriptCmdTableEnd);
SetupBytecodeScript(&sGlobalScriptContext, ptr);
LockPlayerFieldControls();
sGlobalScriptContextStatus = CONTEXT_RUNNING;
2017-09-10 23:05:23 +02:00
}
// Puts the script into waiting mode; usually called from a wait* script command.
void ScriptContext_Stop(void)
2017-09-10 23:05:23 +02:00
{
sGlobalScriptContextStatus = CONTEXT_WAITING;
2017-09-10 23:05:23 +02:00
}
// Puts the script into running mode.
void ScriptContext_Enable(void)
2017-09-10 23:05:23 +02:00
{
sGlobalScriptContextStatus = CONTEXT_RUNNING;
LockPlayerFieldControls();
2017-09-10 23:05:23 +02:00
}
// Sets up and runs a script in its own context immediately. The script will be
// finished when this function returns. Used mainly by all of the map header
// scripts (except the frame table scripts).
void RunScriptImmediately(const u8 *ptr)
2017-09-10 23:05:23 +02:00
{
InitScriptContext(&sImmediateScriptContext, gScriptCmdTable, gScriptCmdTableEnd);
SetupBytecodeScript(&sImmediateScriptContext, ptr);
while (RunScriptCommand(&sImmediateScriptContext) == TRUE);
2017-09-10 23:05:23 +02:00
}
u8 *MapHeaderGetScriptTable(u8 tag)
2017-09-10 23:05:23 +02:00
{
2018-02-15 23:09:52 +01:00
const u8 *mapScripts = gMapHeader.mapScripts;
2017-09-10 23:05:23 +02:00
if (!mapScripts)
return NULL;
while (1)
{
if (!*mapScripts)
return NULL;
if (*mapScripts == tag)
{
mapScripts++;
2021-02-20 00:52:24 +01:00
return T2_READ_PTR(mapScripts);
2017-09-10 23:05:23 +02:00
}
mapScripts += 5;
}
}
void MapHeaderRunScriptType(u8 tag)
2017-09-10 23:05:23 +02:00
{
u8 *ptr = MapHeaderGetScriptTable(tag);
2017-09-10 23:05:23 +02:00
if (ptr)
RunScriptImmediately(ptr);
2017-09-10 23:05:23 +02:00
}
u8 *MapHeaderCheckScriptTable(u8 tag)
2017-09-10 23:05:23 +02:00
{
u8 *ptr = MapHeaderGetScriptTable(tag);
2017-09-10 23:05:23 +02:00
if (!ptr)
return NULL;
while (1)
{
u16 varIndex1;
u16 varIndex2;
2021-02-20 00:52:24 +01:00
// Read first var (or .2byte terminal value)
varIndex1 = T1_READ_16(ptr);
2017-09-10 23:05:23 +02:00
if (!varIndex1)
2021-02-20 00:52:24 +01:00
return NULL; // Reached end of table
2017-09-10 23:05:23 +02:00
ptr += 2;
2021-02-20 00:52:24 +01:00
// Read second var
varIndex2 = T1_READ_16(ptr);
2017-09-10 23:05:23 +02:00
ptr += 2;
2021-02-20 00:52:24 +01:00
// Run map script if vars are equal
2017-09-10 23:05:23 +02:00
if (VarGet(varIndex1) == VarGet(varIndex2))
2021-02-20 00:52:24 +01:00
return T2_READ_PTR(ptr);
2017-09-10 23:05:23 +02:00
ptr += 4;
}
}
void RunOnLoadMapScript(void)
2017-09-10 23:05:23 +02:00
{
MapHeaderRunScriptType(MAP_SCRIPT_ON_LOAD);
2017-09-10 23:05:23 +02:00
}
void RunOnTransitionMapScript(void)
2017-09-10 23:05:23 +02:00
{
MapHeaderRunScriptType(MAP_SCRIPT_ON_TRANSITION);
2017-09-10 23:05:23 +02:00
}
void RunOnResumeMapScript(void)
2017-09-10 23:05:23 +02:00
{
MapHeaderRunScriptType(MAP_SCRIPT_ON_RESUME);
2017-09-10 23:05:23 +02:00
}
void RunOnReturnToFieldMapScript(void)
2017-09-10 23:05:23 +02:00
{
MapHeaderRunScriptType(MAP_SCRIPT_ON_RETURN_TO_FIELD);
2017-09-10 23:05:23 +02:00
}
void RunOnDiveWarpMapScript(void)
2017-09-10 23:05:23 +02:00
{
MapHeaderRunScriptType(MAP_SCRIPT_ON_DIVE_WARP);
2017-09-10 23:05:23 +02:00
}
bool8 TryRunOnFrameMapScript(void)
2017-09-10 23:05:23 +02:00
{
u8 *ptr = MapHeaderCheckScriptTable(MAP_SCRIPT_ON_FRAME_TABLE);
2017-09-10 23:05:23 +02:00
if (!ptr)
return FALSE;
2017-09-10 23:05:23 +02:00
ScriptContext_SetupScript(ptr);
return TRUE;
2017-09-10 23:05:23 +02:00
}
void TryRunOnWarpIntoMapScript(void)
2017-09-10 23:05:23 +02:00
{
u8 *ptr = MapHeaderCheckScriptTable(MAP_SCRIPT_ON_WARP_INTO_MAP_TABLE);
2017-09-10 23:05:23 +02:00
if (ptr)
RunScriptImmediately(ptr);
2017-09-10 23:05:23 +02:00
}
u32 CalculateRamScriptChecksum(void)
{
return CalcCRC16WithTable((u8 *)(&gSaveBlock1Ptr->ramScript.data), sizeof(gSaveBlock1Ptr->ramScript.data));
2017-09-10 23:05:23 +02:00
}
void ClearRamScript(void)
{
CpuFill32(0, &gSaveBlock1Ptr->ramScript, sizeof(struct RamScript));
}
2018-10-17 04:47:08 +02:00
bool8 InitRamScript(const u8 *script, u16 scriptSize, u8 mapGroup, u8 mapNum, u8 objectId)
2017-09-10 23:05:23 +02:00
{
struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data;
ClearRamScript();
if (scriptSize > sizeof(scriptData->script))
return FALSE;
scriptData->magic = RAM_SCRIPT_MAGIC;
scriptData->mapGroup = mapGroup;
scriptData->mapNum = mapNum;
scriptData->objectId = objectId;
memcpy(scriptData->script, script, scriptSize);
gSaveBlock1Ptr->ramScript.checksum = CalculateRamScriptChecksum();
return TRUE;
}
2018-10-17 04:47:08 +02:00
const u8 *GetRamScript(u8 objectId, const u8 *script)
2017-09-10 23:05:23 +02:00
{
struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data;
2021-01-26 08:16:26 +01:00
gRamScriptRetAddr = NULL;
2017-09-10 23:05:23 +02:00
if (scriptData->magic != RAM_SCRIPT_MAGIC)
return script;
if (scriptData->mapGroup != gSaveBlock1Ptr->location.mapGroup)
return script;
if (scriptData->mapNum != gSaveBlock1Ptr->location.mapNum)
return script;
if (scriptData->objectId != objectId)
return script;
if (CalculateRamScriptChecksum() != gSaveBlock1Ptr->ramScript.checksum)
{
ClearRamScript();
return script;
}
else
{
2021-01-26 08:16:26 +01:00
gRamScriptRetAddr = script;
2017-09-10 23:05:23 +02:00
return scriptData->script;
}
}
2021-11-15 16:57:22 +01:00
#define NO_OBJECT OBJ_EVENT_ID_PLAYER
2019-04-02 19:57:03 +02:00
bool32 ValidateSavedRamScript(void)
2017-09-10 23:05:23 +02:00
{
struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data;
if (scriptData->magic != RAM_SCRIPT_MAGIC)
return FALSE;
if (scriptData->mapGroup != MAP_GROUP(UNDEFINED))
2017-09-10 23:05:23 +02:00
return FALSE;
if (scriptData->mapNum != MAP_NUM(UNDEFINED))
2017-09-10 23:05:23 +02:00
return FALSE;
2021-11-15 16:57:22 +01:00
if (scriptData->objectId != NO_OBJECT)
2017-09-10 23:05:23 +02:00
return FALSE;
if (CalculateRamScriptChecksum() != gSaveBlock1Ptr->ramScript.checksum)
return FALSE;
return TRUE;
}
2019-04-02 19:57:03 +02:00
u8 *GetSavedRamScriptIfValid(void)
2017-09-10 23:05:23 +02:00
{
struct RamScriptData *scriptData = &gSaveBlock1Ptr->ramScript.data;
if (!ValidateSavedWonderCard())
2017-09-10 23:05:23 +02:00
return NULL;
if (scriptData->magic != RAM_SCRIPT_MAGIC)
return NULL;
if (scriptData->mapGroup != MAP_GROUP(UNDEFINED))
2017-09-10 23:05:23 +02:00
return NULL;
if (scriptData->mapNum != MAP_NUM(UNDEFINED))
2017-09-10 23:05:23 +02:00
return NULL;
2021-11-15 16:57:22 +01:00
if (scriptData->objectId != NO_OBJECT)
2017-09-10 23:05:23 +02:00
return NULL;
if (CalculateRamScriptChecksum() != gSaveBlock1Ptr->ramScript.checksum)
{
ClearRamScript();
return NULL;
}
else
{
return scriptData->script;
}
}
void InitRamScript_NoObjectEvent(u8 *script, u16 scriptSize)
2017-09-10 23:05:23 +02:00
{
if (scriptSize > sizeof(gSaveBlock1Ptr->ramScript.data.script))
scriptSize = sizeof(gSaveBlock1Ptr->ramScript.data.script);
2021-11-15 16:57:22 +01:00
InitRamScript(script, scriptSize, MAP_GROUP(UNDEFINED), MAP_NUM(UNDEFINED), NO_OBJECT);
2017-09-10 23:05:23 +02:00
}