/** * Copyright (c) 2025 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include "script/scriptcontext.hpp" #include "rpg/entity/entity.hpp" #include "assert/Assert.hpp" int32_t scriptFuncEntityAdd(lua_State *L) { assertNotNull(L, "Lua state cannot be NULL"); assertTrue(lua_isinteger(L, 1), "Expected integer entity type"); lua_Integer entityType = luaL_checkinteger(L, 1); assertTrue( entityType >= ENTITY_TYPE_NULL && entityType < ENTITY_TYPE_COUNT, "Invalid entity type passed to scriptFuncEntityAdd" ); // Pop entity uint8_t available = entityGetAvailable(); if(available == 0xFF) { lua_pushnil(L); return 1; } entity_t *ent = &ENTITIES[available]; entityInit(ent, (entitytype_t)entityType); // May include X, Y and/or Z if(lua_isinteger(L, 2)) { lua_Integer xPos = luaL_checkinteger(L, 2); ent->position.x = (int32_t)xPos; } if(lua_isinteger(L, 3)) { lua_Integer yPos = luaL_checkinteger(L, 3); ent->position.y = (int32_t)yPos; } if(lua_isinteger(L, 4)) { lua_Integer zPos = luaL_checkinteger(L, 4); ent->position.z = (int32_t)zPos; } // Send entity id. lua_pushinteger(L, ent->id); return 1; } int32_t scriptFuncEntitySetX(lua_State *L) { assertNotNull(L, "Lua state cannot be NULL"); assertTrue(lua_isinteger(L, 1), "Expected integer entity id"); assertTrue(lua_isinteger(L, 2), "Expected integer x position"); lua_Integer entityId = luaL_checkinteger(L, 1); lua_Integer xPos = luaL_checkinteger(L, 2); assertTrue( entityId >= 0 && entityId < ENTITY_COUNT, "Invalid entity id passed to scriptFuncEntitySetX" ); entity_t *ent = &ENTITIES[entityId]; assertTrue( ent->type != ENTITY_TYPE_NULL, "Cannot set position of NULL entity in scriptFuncEntitySetX" ); ent->position.x = (int32_t)xPos; return 0; } int32_t scriptFuncEntitySetY(lua_State *L) { assertNotNull(L, "Lua state cannot be NULL"); assertTrue(lua_isinteger(L, 1), "Expected integer entity id"); assertTrue(lua_isinteger(L, 2), "Expected integer y position"); lua_Integer entityId = luaL_checkinteger(L, 1); lua_Integer yPos = luaL_checkinteger(L, 2); assertTrue( entityId >= 0 && entityId < ENTITY_COUNT, "Invalid entity id passed to scriptFuncEntitySetY" ); entity_t *ent = &ENTITIES[entityId]; assertTrue( ent->type != ENTITY_TYPE_NULL, "Cannot set position of NULL entity in scriptFuncEntitySetY" ); ent->position.y = (int32_t)yPos; return 0; } int32_t scriptFuncEntitySetZ(lua_State *L) { assertNotNull(L, "Lua state cannot be NULL"); assertTrue(lua_isinteger(L, 1), "Expected integer entity id"); assertTrue(lua_isinteger(L, 2), "Expected integer z position"); lua_Integer entityId = luaL_checkinteger(L, 1); lua_Integer zPos = luaL_checkinteger(L, 2); assertTrue( entityId >= 0 && entityId < ENTITY_COUNT, "Invalid entity id passed to scriptFuncEntitySetZ" ); entity_t *ent = &ENTITIES[entityId]; assertTrue( ent->type != ENTITY_TYPE_NULL, "Cannot set position of NULL entity in scriptFuncEntitySetZ" ); ent->position.z = (int32_t)zPos; return 0; } void scriptFuncEntity(scriptcontext_t *context) { assertNotNull(context, "Script context cannot be NULL"); scriptContextRegFunc(context, "entityAdd", scriptFuncEntityAdd); scriptContextRegFunc(context, "entitySetX", scriptFuncEntitySetX); scriptContextRegFunc(context, "entitySetY", scriptFuncEntitySetY); scriptContextRegFunc(context, "entitySetZ", scriptFuncEntitySetZ); }