Document script_movement

This commit is contained in:
GriffinR 2019-09-15 15:30:35 -04:00 committed by huderlem
parent 6c9997a906
commit f9a8f44a29
7 changed files with 99 additions and 96 deletions

View File

@ -1,8 +1,8 @@
#ifndef GUARD_SCRIPT_MOVEMENT_H #ifndef GUARD_SCRIPT_MOVEMENT_H
#define GUARD_SCRIPT_MOVEMENT_H #define GUARD_SCRIPT_MOVEMENT_H
bool8 ScriptMovement_StartObjectMovementScript(u8, u8, u8, const u8 *); bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGroup, const u8 *movementScript);
bool8 ScriptMovement_IsObjectMovementFinished(u8, u8, u8); bool8 ScriptMovement_IsObjectMovementFinished(u8 localId, u8 mapNum, u8 mapGroup);
void sub_80D338C(void); void ScriptMovement_StopAll(void);
#endif // GUARD_SCRIPT_MOVEMENT_H #endif // GUARD_SCRIPT_MOVEMENT_H

View File

@ -5,6 +5,7 @@
#define TAIL_SENTINEL 0xFF #define TAIL_SENTINEL 0xFF
#define NUM_TASKS 16 #define NUM_TASKS 16
#define NUM_TASK_DATA 16
typedef void (*TaskFunc)(u8 taskId); typedef void (*TaskFunc)(u8 taskId);
@ -15,7 +16,7 @@ struct Task
u8 prev; u8 prev;
u8 next; u8 next;
u8 priority; u8 priority;
s16 data[16]; s16 data[NUM_TASK_DATA];
}; };
extern struct Task gTasks[]; extern struct Task gTasks[];

View File

@ -92,7 +92,7 @@ void ScriptUnfreezeEventObjects(void)
{ {
u8 playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); u8 playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]); EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]);
sub_80D338C(); ScriptMovement_StopAll();
UnfreezeEventObjects(); UnfreezeEventObjects();
} }
@ -104,7 +104,7 @@ void sub_8098524(void)
EventObjectClearHeldMovementIfFinished(&gEventObjects[gSelectedEventObject]); EventObjectClearHeldMovementIfFinished(&gEventObjects[gSelectedEventObject]);
playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]); EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]);
sub_80D338C(); ScriptMovement_StopAll();
UnfreezeEventObjects(); UnfreezeEventObjects();
} }

View File

@ -1290,7 +1290,7 @@ static bool32 sub_81963F0(u8 taskId)
sub_81973A4(); sub_81973A4();
playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]); EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]);
sub_80D338C(); ScriptMovement_StopAll();
UnfreezeEventObjects(); UnfreezeEventObjects();
ScriptContext2_Disable(); ScriptContext2_Disable();
} }

View File

@ -55,7 +55,7 @@ void FinishMossdeepGymTiles(void)
id = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); id = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[id]); EventObjectClearHeldMovementIfFinished(&gEventObjects[id]);
sub_80D338C(); ScriptMovement_StopAll();
} }
u16 MossdeepGym_MoveEvents(u8 arg0) u16 MossdeepGym_MoveEvents(u8 arg0)

View File

@ -1247,7 +1247,7 @@ bool8 ScrCmd_releaseall(struct ScriptContext *ctx)
HideFieldMessageBox(); HideFieldMessageBox();
playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]); EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]);
sub_80D338C(); ScriptMovement_StopAll();
UnfreezeEventObjects(); UnfreezeEventObjects();
return FALSE; return FALSE;
} }
@ -1261,7 +1261,7 @@ bool8 ScrCmd_release(struct ScriptContext *ctx)
EventObjectClearHeldMovementIfFinished(&gEventObjects[gSelectedEventObject]); EventObjectClearHeldMovementIfFinished(&gEventObjects[gSelectedEventObject]);
playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0); playerObjectId = GetEventObjectIdByLocalIdAndMap(EVENT_OBJ_ID_PLAYER, 0, 0);
EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]); EventObjectClearHeldMovementIfFinished(&gEventObjects[playerObjectId]);
sub_80D338C(); ScriptMovement_StopAll();
UnfreezeEventObjects(); UnfreezeEventObjects();
return FALSE; return FALSE;
} }

View File

@ -3,20 +3,22 @@
#include "event_object_movement.h" #include "event_object_movement.h"
#include "task.h" #include "task.h"
#include "util.h" #include "util.h"
#include "constants/event_objects.h"
#include "constants/event_object_movement_constants.h"
// static functions // static functions
static void sub_80D33AC(u8); static void ScriptMovement_StartMoveObjects(u8 priority);
static u8 sub_80D33F4(void); static u8 GetMoveObjectsTaskId(void);
static bool8 sub_80D3408(u8, u8, const u8 *); static bool8 ScriptMovement_TryAddNewMovement(u8 taskId, u8 eventObjId, const u8 *movementScript);
static u8 sub_80D3474(u8, u8); static u8 GetMovementScriptIdFromEventObjectId(u8 taskId, u8 eventObjId);
static bool8 sub_80D3584(u8, u8); static bool8 IsMovementScriptFinished(u8 taskId, u8 moveScrId);
static void sub_80D35DC(u8, u8, u8, const u8 *); static void ScriptMovement_AddNewMovement(u8 taskId, u8 moveScrId, u8 eventObjId, const u8 *movementScript);
static void UnfreezeObjects(u8); static void ScriptMovement_UnfreezeObjects(u8 taskId);
static void sub_80D3660(u8); static void ScriptMovement_MoveObjects(u8 taskId);
static void sub_80A2490(u8, u8, u8, const u8 *); static void ScriptMovement_TakeStep(u8 taskId, u8 moveScrId, u8 eventObjId, const u8 *movementScript);
// EWRAM_DATA // EWRAM_DATA
static EWRAM_DATA const u8 *gUnknown_02039D90[16] = {0}; static EWRAM_DATA const u8 *gMovementScripts[EVENT_OBJECTS_COUNT] = {0};
// text // text
bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGroup, const u8 *movementScript) bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGroup, const u8 *movementScript)
@ -25,207 +27,207 @@ bool8 ScriptMovement_StartObjectMovementScript(u8 localId, u8 mapNum, u8 mapGrou
if (TryGetEventObjectIdByLocalIdAndMap(localId, mapNum, mapGroup, &eventObjId)) if (TryGetEventObjectIdByLocalIdAndMap(localId, mapNum, mapGroup, &eventObjId))
return TRUE; return TRUE;
if (!FuncIsActiveTask(sub_80D3660)) if (!FuncIsActiveTask(ScriptMovement_MoveObjects))
sub_80D33AC(50); ScriptMovement_StartMoveObjects(50);
return sub_80D3408(sub_80D33F4(), eventObjId, movementScript); return ScriptMovement_TryAddNewMovement(GetMoveObjectsTaskId(), eventObjId, movementScript);
} }
bool8 ScriptMovement_IsObjectMovementFinished(u8 localId, u8 mapNum, u8 mapBank) bool8 ScriptMovement_IsObjectMovementFinished(u8 localId, u8 mapNum, u8 mapGroup)
{ {
u8 eventObjId; u8 eventObjId;
u8 r4; u8 taskId;
u8 r1; u8 moveScrId;
if (TryGetEventObjectIdByLocalIdAndMap(localId, mapNum, mapBank, &eventObjId)) if (TryGetEventObjectIdByLocalIdAndMap(localId, mapNum, mapGroup, &eventObjId))
return TRUE; return TRUE;
r4 = sub_80D33F4(); taskId = GetMoveObjectsTaskId();
r1 = sub_80D3474(r4, eventObjId); moveScrId = GetMovementScriptIdFromEventObjectId(taskId, eventObjId);
if (r1 == 16) if (moveScrId == EVENT_OBJECTS_COUNT)
return TRUE; return TRUE;
return sub_80D3584(r4, r1); return IsMovementScriptFinished(taskId, moveScrId);
} }
void sub_80D338C(void) void ScriptMovement_StopAll(void)
{ {
u8 taskId; u8 taskId;
taskId = sub_80D33F4(); taskId = GetMoveObjectsTaskId();
if (taskId != 0xFF) if (taskId != 0xFF)
{ {
UnfreezeObjects(taskId); ScriptMovement_UnfreezeObjects(taskId);
DestroyTask(taskId); DestroyTask(taskId);
} }
} }
static void ScriptMovement_StartMoveObjects(u8 priority)
static void sub_80D33AC(u8 priority)
{ {
u8 taskId; u8 taskId;
u8 i; u8 i;
taskId = CreateTask(sub_80D3660, priority); taskId = CreateTask(ScriptMovement_MoveObjects, priority);
for (i = 1; i < 16; i++)
for (i = 1; i < NUM_TASK_DATA; i++)
gTasks[taskId].data[i] = 0xFFFF; gTasks[taskId].data[i] = 0xFFFF;
} }
static u8 sub_80D33F4(void) static u8 GetMoveObjectsTaskId(void)
{ {
return FindTaskIdByFunc(sub_80D3660); return FindTaskIdByFunc(ScriptMovement_MoveObjects);
} }
static bool8 sub_80D3408(u8 taskId, u8 eventObjId, const u8 *movementScript) static bool8 ScriptMovement_TryAddNewMovement(u8 taskId, u8 eventObjId, const u8 *movementScript)
{ {
u8 r4; u8 moveScrId;
r4 = sub_80D3474(taskId, eventObjId); moveScrId = GetMovementScriptIdFromEventObjectId(taskId, eventObjId);
if (r4 != 16) if (moveScrId != EVENT_OBJECTS_COUNT)
{ {
if (sub_80D3584(taskId, r4) == 0) if (IsMovementScriptFinished(taskId, moveScrId) == 0)
{ {
return TRUE; return TRUE;
} }
else else
{ {
sub_80D35DC(taskId, r4, eventObjId, movementScript); ScriptMovement_AddNewMovement(taskId, moveScrId, eventObjId, movementScript);
return FALSE; return FALSE;
} }
} }
r4 = sub_80D3474(taskId, 0xFF); moveScrId = GetMovementScriptIdFromEventObjectId(taskId, EVENT_OBJ_ID_PLAYER);
if (r4 == 16) if (moveScrId == EVENT_OBJECTS_COUNT)
{ {
return TRUE; return TRUE;
} }
else else
{ {
sub_80D35DC(taskId, r4, eventObjId, movementScript); ScriptMovement_AddNewMovement(taskId, moveScrId, eventObjId, movementScript);
return FALSE; return FALSE;
} }
} }
static u8 sub_80D3474(u8 taskId, u8 b) static u8 GetMovementScriptIdFromEventObjectId(u8 taskId, u8 eventObjId)
{ {
u8 *ptr; u8 *moveScriptId;
u8 i; u8 i;
ptr = (u8 *)&gTasks[taskId].data[1]; moveScriptId = (u8 *)&gTasks[taskId].data[1];
for (i = 0; i < 16; i++, ptr++) for (i = 0; i < EVENT_OBJECTS_COUNT; i++, moveScriptId++)
{ {
if (*ptr == b) if (*moveScriptId == eventObjId)
return i; return i;
} }
return 16; return EVENT_OBJECTS_COUNT;
} }
static void sub_80D34B0(u8 taskId, u8 b, u8 **c) static void LoadEventObjectIdPtrFromMovementScript(u8 taskId, u8 moveScrId, u8 **pEventObjId)
{ {
u8 i; u8 i;
*c = (u8 *)&gTasks[taskId].data[1]; *pEventObjId = (u8 *)&gTasks[taskId].data[1];
for (i = 0; i < b; i++, (*c)++) for (i = 0; i < moveScrId; i++, (*pEventObjId)++)
; ;
} }
static void sub_80D34E4(u8 taskId, u8 b, u8 c) static void SetEventObjectIdAtMovementScript(u8 taskId, u8 moveScrId, u8 eventObjId)
{ {
u8 *ptr; u8 *ptr;
sub_80D34B0(taskId, b, &ptr); LoadEventObjectIdPtrFromMovementScript(taskId, moveScrId, &ptr);
*ptr = c; //what is this supposed to do? *ptr = eventObjId;
} }
static void sub_80D3508(u8 taskId, u8 b, u8 *c) static void LoadEventObjectIdFromMovementScript(u8 taskId, u8 moveScrId, u8 *eventObjId)
{ {
u8 *ptr; u8 *ptr;
sub_80D34B0(taskId, b, &ptr); LoadEventObjectIdPtrFromMovementScript(taskId, moveScrId, &ptr);
*c = *ptr; *eventObjId = *ptr;
} }
static void sub_80D352C(u8 a, u8 b) static void ClearMovementScriptFinished(u8 taskId, u8 moveScrId)
{ {
u16 var = ~gBitTable[b]; u16 mask = ~gBitTable[moveScrId];
gTasks[a].data[0] &= var; gTasks[taskId].data[0] &= mask;
} }
static void sub_80D355C(u8 taskId, u8 b) static void SetMovementScriptFinished(u8 taskId, u8 moveScrId)
{ {
gTasks[taskId].data[0] |= gBitTable[b]; gTasks[taskId].data[0] |= gBitTable[moveScrId];
} }
static bool8 sub_80D3584(u8 taskId, u8 b) static bool8 IsMovementScriptFinished(u8 taskId, u8 moveScrId)
{ {
u16 var = (u16)gTasks[taskId].data[0] & gBitTable[b]; u16 moveScriptFinished = (u16)gTasks[taskId].data[0] & gBitTable[moveScrId];
if (var != 0) if (moveScriptFinished != 0)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
} }
static void npc_obj_offscreen_culling_and_flag_update(u8 a, const u8 *movementScript) static void SetMovementScript(u8 moveScrId, const u8 *movementScript)
{ {
gUnknown_02039D90[a] = movementScript; gMovementScripts[moveScrId] = movementScript;
} }
static const u8 *sub_80D35CC(u8 a) static const u8 *GetMovementScript(u8 moveScrId)
{ {
return gUnknown_02039D90[a]; return gMovementScripts[moveScrId];
} }
static void sub_80D35DC(u8 taskId, u8 b, u8 eventObjId, const u8 *movementScript) static void ScriptMovement_AddNewMovement(u8 taskId, u8 moveScrId, u8 eventObjId, const u8 *movementScript)
{ {
sub_80D352C(taskId, b); ClearMovementScriptFinished(taskId, moveScrId);
npc_obj_offscreen_culling_and_flag_update(b, movementScript); SetMovementScript(moveScrId, movementScript);
sub_80D34E4(taskId, b, eventObjId); SetEventObjectIdAtMovementScript(taskId, moveScrId, eventObjId);
} }
static void UnfreezeObjects(u8 taskId) static void ScriptMovement_UnfreezeObjects(u8 taskId)
{ {
u8 *pEventObjId; u8 *pEventObjId;
u8 i; u8 i;
pEventObjId = (u8 *)&gTasks[taskId].data[1]; pEventObjId = (u8 *)&gTasks[taskId].data[1];
for (i = 0; i < 16; i++, pEventObjId++) for (i = 0; i < EVENT_OBJECTS_COUNT; i++, pEventObjId++)
{ {
if (*pEventObjId != 0xFF) if (*pEventObjId != 0xFF)
UnfreezeEventObject(&gEventObjects[*pEventObjId]); UnfreezeEventObject(&gEventObjects[*pEventObjId]);
} }
} }
static void sub_80D3660(u8 taskId) static void ScriptMovement_MoveObjects(u8 taskId)
{ {
u8 i; u8 i;
u8 var; u8 eventObjId;
for (i = 0; i < 16; i++) for (i = 0; i < EVENT_OBJECTS_COUNT; i++)
{ {
sub_80D3508(taskId, i, &var); LoadEventObjectIdFromMovementScript(taskId, i, &eventObjId);
if (var != 0xFF) if (eventObjId != 0xFF)
sub_80A2490(taskId, i, var, sub_80D35CC(i)); ScriptMovement_TakeStep(taskId, i, eventObjId, GetMovementScript(i));
} }
} }
static void sub_80A2490(u8 taskId, u8 b, u8 eventObjId, const u8 *d) static void ScriptMovement_TakeStep(u8 taskId, u8 moveScrId, u8 eventObjId, const u8 *movementScript)
{ {
u8 var; u8 nextMoveActionId;
if (EventObjectIsHeldMovementActive(&gEventObjects[eventObjId]) if (EventObjectIsHeldMovementActive(&gEventObjects[eventObjId])
&& !EventObjectClearHeldMovementIfFinished(&gEventObjects[eventObjId])) && !EventObjectClearHeldMovementIfFinished(&gEventObjects[eventObjId]))
return; return;
var = *d; nextMoveActionId = *movementScript;
if (var == 0xFE) if (nextMoveActionId == MOVEMENT_ACTION_STEP_END)
{ {
sub_80D355C(taskId, b); SetMovementScriptFinished(taskId, moveScrId);
FreezeEventObject(&gEventObjects[eventObjId]); FreezeEventObject(&gEventObjects[eventObjId]);
} }
else else
{ {
if (!EventObjectSetHeldMovement(&gEventObjects[eventObjId], var)) if (!EventObjectSetHeldMovement(&gEventObjects[eventObjId], nextMoveActionId))
{ {
d++; movementScript++;
npc_obj_offscreen_culling_and_flag_update(b, d); SetMovementScript(moveScrId, movementScript);
} }
} }
} }