refactor: LuaState replaced with lua_engine

This commit is contained in:
MihailRis
2024-06-11 13:18:30 +03:00
parent 0647bc6f90
commit 90bc86408b
22 changed files with 643 additions and 675 deletions
+98 -113
View File
@@ -9,9 +9,11 @@
#include "../../../content/Content.hpp"
#include "../../../logic/BlocksController.hpp"
using namespace scripting;
static Block* require_block(lua_State* L) {
auto indices = scripting::content->getIndices();
lua_Integer id = lua_tointeger(L, 1);
auto indices = content->getIndices();
auto id = lua::tointeger(L, 1);
if (static_cast<size_t>(id) >= indices->countBlockDefs()) {
return nullptr;
}
@@ -20,44 +22,38 @@ static Block* require_block(lua_State* L) {
static int l_name(lua_State* L) {
if (auto def = require_block(L)) {
lua_pushstring(L, def->name.c_str());
return 1;
return lua::pushstring(L, def->name);
}
return 0;
}
static int l_material(lua_State* L) {
if (auto def = require_block(L)) {
lua_pushstring(L, def->material.c_str());
return 1;
return lua::pushstring(L, def->material);
}
return 0;
}
static int l_is_solid_at(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z));
return 1;
return lua::pushboolean(L, level->chunks->isSolidBlock(x, y, z));
}
static int l_count(lua_State* L) {
lua_pushinteger(L, scripting::indices->countBlockDefs());
return 1;
return lua::pushinteger(L, indices->countBlockDefs());
}
static int l_index(lua_State* L) {
std::string name = lua_tostring(L, 1);
lua_pushinteger(L, scripting::content->requireBlock(name).rt.id);
return 1;
auto name = lua::require_string(L, 1);
return lua::pushinteger(L, content->requireBlock(name).rt.id);
}
static int l_is_extended(lua_State* L) {
if (auto def = require_block(L)) {
lua_pushboolean(L, def->rt.extended);
return 1;
return lua::pushboolean(L, def->rt.extended);
}
return 0;
}
@@ -70,65 +66,61 @@ static int l_get_size(lua_State* L) {
}
static int l_is_segment(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
auto vox = scripting::level->chunks->get(x, y, z);
lua_pushboolean(L, vox->state.segment);
return 1;
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
return lua::pushboolean(L, vox->state.segment);
}
static int l_seek_origin(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
auto vox = scripting::level->chunks->get(x, y, z);
auto def = scripting::indices->getBlockDef(vox->id);
return lua::pushivec3(L, scripting::level->chunks->seekOrigin({x, y, z}, def, vox->state));
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
auto def = indices->getBlockDef(vox->id);
return lua::pushivec3(L, level->chunks->seekOrigin({x, y, z}, def, vox->state));
}
static int l_set(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer id = lua_tointeger(L, 4);
lua_Integer state = lua_tointeger(L, 5);
bool noupdate = lua_toboolean(L, 6);
if (static_cast<size_t>(id) >= scripting::indices->countBlockDefs()) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto id = lua::tointeger(L, 4);
auto state = lua::tointeger(L, 5);
bool noupdate = lua::toboolean(L, 6);
if (static_cast<size_t>(id) >= indices->countBlockDefs()) {
return 0;
}
if (!scripting::level->chunks->get(x, y, z)) {
if (!level->chunks->get(x, y, z)) {
return 0;
}
scripting::level->chunks->set(x, y, z, id, int2blockstate(state));
scripting::level->lighting->onBlockSet(x,y,z, id);
level->chunks->set(x, y, z, id, int2blockstate(state));
level->lighting->onBlockSet(x,y,z, id);
if (!noupdate) {
scripting::blocks->updateSides(x, y, z);
blocks->updateSides(x, y, z);
}
return 0;
}
static int l_get(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
int id = vox == nullptr ? -1 : vox->id;
lua_pushinteger(L, id);
return 1;
return lua::pushinteger(L, id);
}
static int l_get_x(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua::pushivec3(L, 1, 0, 0);
}
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
auto def = level->content->getIndices()->getBlockDef(vox->id);
if (!def->rotatable) {
return lua::pushivec3(L, 1, 0, 0);
} else {
@@ -138,14 +130,14 @@ static int l_get_x(lua_State* L) {
}
static int l_get_y(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua::pushivec3(L, 0, 1, 0);
}
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
auto def = level->content->getIndices()->getBlockDef(vox->id);
if (!def->rotatable) {
return lua::pushivec3(L, 0, 1, 0);
} else {
@@ -155,14 +147,14 @@ static int l_get_y(lua_State* L) {
}
static int l_get_z(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
return lua::pushivec3(L, 0, 0, 1);
}
auto def = scripting::level->content->getIndices()->getBlockDef(vox->id);
auto def = level->content->getIndices()->getBlockDef(vox->id);
if (!def->rotatable) {
return lua::pushivec3(L, 0, 0, 1);
} else {
@@ -172,83 +164,79 @@ static int l_get_z(lua_State* L) {
}
static int l_get_rotation(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
voxel* vox = level->chunks->get(x, y, z);
int rotation = vox == nullptr ? 0 : vox->state.rotation;
lua_pushinteger(L, rotation);
return 1;
return lua::pushinteger(L, rotation);
}
static int l_set_rotation(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer value = lua_tointeger(L, 4);
scripting::level->chunks->setRotation(x, y, z, value);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto value = lua::tointeger(L, 4);
level->chunks->setRotation(x, y, z, value);
return 0;
}
static int l_get_states(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = level->chunks->get(x, y, z);
int states = vox == nullptr ? 0 : blockstate2int(vox->state);
lua_pushinteger(L, states);
return 1;
return lua::pushinteger(L, states);
}
static int l_set_states(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer states = lua_tointeger(L, 4);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto states = lua::tointeger(L, 4);
Chunk* chunk = scripting::level->chunks->getChunkByVoxel(x, y, z);
auto chunk = level->chunks->getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
return 0;
}
voxel* vox = scripting::level->chunks->get(x, y, z);
auto vox = level->chunks->get(x, y, z);
vox->state = int2blockstate(states);
chunk->setModifiedAndUnsaved();
return 0;
}
static int l_get_user_bits(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
lua_Integer bits = lua_tointeger(L, 5);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto offset = lua::tointeger(L, 4) + VOXEL_USER_BITS_OFFSET;
auto bits = lua::tointeger(L, 5);
voxel* vox = scripting::level->chunks->get(x, y, z);
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
lua_pushinteger(L, 0);
return 1;
return lua::pushinteger(L, 0);
}
uint mask = ((1 << bits) - 1) << offset;
uint data = (blockstate2int(vox->state) & mask) >> offset;
lua_pushinteger(L, data);
return 1;
return lua::pushinteger(L, data);
}
static int l_set_user_bits(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer offset = lua_tointeger(L, 4);
lua_Integer bits = lua_tointeger(L, 5);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto offset = lua::tointeger(L, 4);
auto bits = lua::tointeger(L, 5);
size_t mask = ((1 << bits) - 1) << offset;
lua_Integer value = (lua_tointeger(L, 6) << offset) & mask;
auto value = (lua::tointeger(L, 6) << offset) & mask;
Chunk* chunk = scripting::level->chunks->getChunkByVoxel(x, y, z);
auto chunk = level->chunks->getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
return 0;
}
voxel* vox = scripting::level->chunks->get(x, y, z);
auto vox = level->chunks->get(x, y, z);
if (vox == nullptr) {
return 0;
}
@@ -258,18 +246,15 @@ static int l_set_user_bits(lua_State* L) {
}
static int l_is_replaceable_at(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
return 1;
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
return lua::pushboolean(L, level->chunks->isReplaceableBlock(x, y, z));
}
static int l_caption(lua_State* L) {
if (auto def = require_block(L)) {
lua_pushstring(L, def->caption.c_str());
return 1;
return lua::pushstring(L, def->caption);
}
return 0;
}