Skip to content

Commit

Permalink
create_entity varargs cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
HailSanta authored and HailSanta committed Feb 28, 2024
1 parent f1899b9 commit 4be7a2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
typedef s32 EntityScript[];
typedef s32 EntityModelScript[];

extern s32 CreateEntityVarArgBuffer[4];
extern s32 CreateEntityVarArgBuffer[];

enum {
ENTITY_SCRIPT_OP_End,
Expand Down
44 changes: 18 additions & 26 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern Addr entity_sbk_omo_ROM_START;

s32 D_8014AFB0 = 255;

SHIFT_BSS s32 CreateEntityVarArgBuffer[4];
SHIFT_BSS s32 CreateEntityVarArgBuffer[3];
SHIFT_BSS HiddenPanelsData gCurrentHiddenPanels;
SHIFT_BSS s32 gEntityHideMode;

Expand Down Expand Up @@ -1196,17 +1196,13 @@ void entity_free_static_data(EntityBlueprint* data) {

s32 create_entity(EntityBlueprint* bp, ...) {
va_list ap;
EntityBlueprint** bpPtr;
f32 x, y, z;
f32 rotY;
s32 listIndex;
Entity* entity;
s32* args;
s32 idx;

va_start(ap, bp);
// needed to match
bpPtr = &bp;
*bpPtr = bp;

load_area_specific_entity_data();

Expand All @@ -1215,19 +1211,17 @@ s32 create_entity(EntityBlueprint* bp, ...) {
z = va_arg(ap, s32);
rotY = va_arg(ap, s32);

args = &CreateEntityVarArgBuffer[2];

*args-- = 0;
*args-- = 0;
*args = 0;
for (idx = 0; idx < ARRAY_COUNT(CreateEntityVarArgBuffer); idx++) {
CreateEntityVarArgBuffer[idx] = 0;
}

for (listIndex = 3; listIndex > 0; listIndex--) {
for (idx = 0; idx < ARRAY_COUNT(CreateEntityVarArgBuffer); idx++) {
s32 arg = va_arg(ap, s32);

if (arg == MAKE_ENTITY_END) {
break;
}
*args++ = arg;
CreateEntityVarArgBuffer[idx] = arg;
}

va_end(ap);
Expand Down Expand Up @@ -1363,34 +1357,32 @@ API_CALLABLE(MakeEntity) {
s32 flags;
s32 nextArg;
s32 entityIndex;
s32 endOfArgs;
s32* varArgBufPos;
s32 idx;

if (isInitialCall != TRUE) {
return ApiStatus_DONE2;
}

entityData = (EntityBlueprint*)evt_get_variable(script, *args++);
varArgBufPos = &CreateEntityVarArgBuffer[2];
endOfArgs = MAKE_ENTITY_END;
x = evt_get_variable(script, *args++);
y = evt_get_variable(script, *args++);
z = evt_get_variable(script, *args++);
flags = evt_get_variable(script, *args++);

*varArgBufPos-- = 0;
*varArgBufPos-- = 0;
*varArgBufPos = 0;
for (idx = 0; idx < ARRAY_COUNT(CreateEntityVarArgBuffer); idx++) {
CreateEntityVarArgBuffer[idx] = 0;
}

do {
for (idx = 0;; idx++) {
nextArg = evt_get_variable(script, *args++);

if (nextArg != endOfArgs) {
*varArgBufPos++ = nextArg;
if (nextArg == MAKE_ENTITY_END) {
break;
}
} while (nextArg != endOfArgs);
CreateEntityVarArgBuffer[idx] = nextArg;
}

entityIndex = create_entity(entityData, x, y, z, flags, CreateEntityVarArgBuffer[0], CreateEntityVarArgBuffer[1], CreateEntityVarArgBuffer[2], endOfArgs);
entityIndex = create_entity(entityData, x, y, z, flags,
CreateEntityVarArgBuffer[0], CreateEntityVarArgBuffer[1], CreateEntityVarArgBuffer[2], MAKE_ENTITY_END);
gLastCreatedEntityIndex = entityIndex;
script->varTable[0] = entityIndex;
return ApiStatus_DONE2;
Expand Down

0 comments on commit 4be7a2d

Please sign in to comment.