diff --git a/src/logic/scripting/api_lua.h b/src/logic/scripting/api_lua.h index 7ea0d477..f8a50a7d 100644 --- a/src/logic/scripting/api_lua.h +++ b/src/logic/scripting/api_lua.h @@ -218,23 +218,28 @@ static const luaL_Reg playerlib [] = { /* == blocks-related functions == */ static int l_block_name(lua_State* L) { - int id = lua_tointeger(L, 1); - auto def = scripting::content->getIndices()->getBlockDef(id); + auto indices = scripting::content->getIndices(); + lua::luaint id = lua_tointeger(L, 1); + if (id < 0 || size_t(id) >= indices->countBlockDefs()) { + lua_pushnil(L); + return 1; + } + auto def = indices->getBlockDef(id); lua_pushstring(L, def->name.c_str()); return 1; } static int l_is_solid_at(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z)); return 1; } static int l_blocks_count(lua_State* L) { - lua_pushinteger(L, scripting::content->getIndices()->countBlockDefs()); + lua_pushinteger(L, scripting::indices->countBlockDefs()); return 1; } @@ -245,12 +250,15 @@ static int l_block_index(lua_State* L) { } static int l_set_block(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); - int id = lua_tointeger(L, 4); - int states = lua_tointeger(L, 5); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); + lua::luaint id = lua_tointeger(L, 4); + lua::luaint states = lua_tointeger(L, 5); bool noupdate = lua_toboolean(L, 6); + if (id < 0 || size_t(id) >= scripting::indices->countBlockDefs()) { + return 0; + } scripting::level->chunks->set(x, y, z, id, states); scripting::level->lighting->onBlockSet(x,y,z, id); if (!noupdate) @@ -259,9 +267,9 @@ static int l_set_block(lua_State* L) { } static int l_get_block(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); int id = vox == nullptr ? -1 : vox->id; lua_pushinteger(L, id); @@ -269,9 +277,9 @@ static int l_get_block(lua_State* L) { } static int l_get_block_x(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 1, 0, 0); @@ -286,9 +294,9 @@ static int l_get_block_x(lua_State* L) { } static int l_get_block_y(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 1, 0); @@ -303,9 +311,9 @@ static int l_get_block_y(lua_State* L) { } static int l_get_block_z(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 0, 1); @@ -320,9 +328,9 @@ static int l_get_block_z(lua_State* L) { } static int l_get_block_states(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); int states = vox == nullptr ? 0 : vox->states; lua_pushinteger(L, states); @@ -330,11 +338,11 @@ static int l_get_block_states(lua_State* L) { } static int l_get_block_user_bits(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); - int offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; - int bits = lua_tointeger(L, 5); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); + lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; + lua::luaint bits = lua_tointeger(L, 5); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { @@ -348,14 +356,14 @@ static int l_get_block_user_bits(lua_State* L) { } static int l_set_block_user_bits(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); - int offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; - int bits = lua_tointeger(L, 5); + lua::luaint x = lua_tointeger(L, 1); + lua::luaint y = lua_tointeger(L, 2); + lua::luaint z = lua_tointeger(L, 3); + lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; + lua::luaint bits = lua_tointeger(L, 5); uint mask = ((1 << bits) - 1) << offset; - int value = (lua_tointeger(L, 6) << offset) & mask; + lua::luaint value = (lua_tointeger(L, 6) << offset) & mask; voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 4d50f9fc..edd11c5b 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -25,6 +25,7 @@ Engine* scripting::engine = nullptr; lua::LuaState* scripting::state = nullptr; Level* scripting::level = nullptr; const Content* scripting::content = nullptr; +const ContentIndices* scripting::indices = nullptr; BlocksController* scripting::blocks = nullptr; void load_script(fs::path name) { @@ -77,6 +78,7 @@ wstringconsumer scripting::create_wstring_consumer( void scripting::on_world_load(Level* level, BlocksController* blocks) { scripting::level = level; scripting::content = level->content; + scripting::indices = level->content->getIndices(); scripting::blocks = blocks; load_script("world.lua"); @@ -103,6 +105,7 @@ void scripting::on_world_quit() { } scripting::level = nullptr; scripting::content = nullptr; + scripting::indices = nullptr; } void scripting::on_blocks_tick(const Block* block, int tps) { @@ -224,5 +227,6 @@ void scripting::close() { state = nullptr; content = nullptr; + indices = nullptr; level = nullptr; } diff --git a/src/logic/scripting/scripting.h b/src/logic/scripting/scripting.h index 5cf2636c..dde825ff 100644 --- a/src/logic/scripting/scripting.h +++ b/src/logic/scripting/scripting.h @@ -9,6 +9,7 @@ class LuaState; class Engine; class Content; +class ContentIndices; class Level; class Block; class Player; @@ -20,6 +21,7 @@ class BlocksController; namespace scripting { extern Engine* engine; extern const Content* content; + extern const ContentIndices* indices; extern Level* level; extern BlocksController* blocks;