refactor: 'lua' namespace expansion

This commit is contained in:
MihailRis
2024-06-11 03:37:35 +03:00
parent 7973a9c32b
commit 0647bc6f90
26 changed files with 613 additions and 723 deletions
+53 -60
View File
@@ -1,23 +1,21 @@
#include "lua_commons.hpp"
#include "api_lua.hpp"
#include "lua_util.hpp"
#include "../scripting.hpp"
#include "../../../content/Content.hpp"
#include "../../../world/Level.hpp"
#include "../../../items/ItemStack.hpp"
#include "../../../items/Inventories.hpp"
#include "../../../logic/BlocksController.hpp"
using namespace scripting;
static void validate_itemid(itemid_t id) {
if (id >= scripting::indices->countItemDefs()) {
if (id >= indices->countItemDefs()) {
throw std::runtime_error("invalid item id");
}
}
static std::shared_ptr<Inventory> get_inventory(int64_t id) {
auto inv = scripting::level->inventories->get(id);
auto inv = level->inventories->get(id);
if (inv == nullptr) {
throw std::runtime_error("inventory not found: "+std::to_string(id));
}
@@ -25,7 +23,7 @@ static std::shared_ptr<Inventory> get_inventory(int64_t id) {
}
static std::shared_ptr<Inventory> get_inventory(int64_t id, int arg) {
auto inv = scripting::level->inventories->get(id);
auto inv = level->inventories->get(id);
if (inv == nullptr) {
throw std::runtime_error("inventory not found: "+std::to_string(id)+
" argument "+std::to_string(arg));
@@ -40,21 +38,21 @@ static void validate_slotid(int slotid, Inventory* inv) {
}
static int l_inventory_get(lua_State* L) {
lua_Integer invid = lua_tointeger(L, 1);
lua_Integer slotid = lua_tointeger(L, 2);
auto invid = lua::tointeger(L, 1);
auto slotid = lua::tointeger(L, 2);
auto inv = get_inventory(invid);
validate_slotid(slotid, inv.get());
const ItemStack& item = inv->getSlot(slotid);
lua_pushinteger(L, item.getItemId());
lua_pushinteger(L, item.getCount());
lua::pushinteger(L, item.getItemId());
lua::pushinteger(L, item.getCount());
return 2;
}
static int l_inventory_set(lua_State* L) {
lua_Integer invid = lua_tointeger(L, 1);
lua_Integer slotid = lua_tointeger(L, 2);
lua_Integer itemid = lua_tointeger(L, 3);
lua_Integer count = lua_tointeger(L, 4);
auto invid = lua::tointeger(L, 1);
auto slotid = lua::tointeger(L, 2);
auto itemid = lua::tointeger(L, 3);
auto count = lua::tointeger(L, 4);
validate_itemid(itemid);
auto inv = get_inventory(invid);
@@ -66,90 +64,85 @@ static int l_inventory_set(lua_State* L) {
}
static int l_inventory_size(lua_State* L) {
lua_Integer invid = lua_tointeger(L, 1);
auto invid = lua::tointeger(L, 1);
auto inv = get_inventory(invid);
lua_pushinteger(L, inv->size());
return 1;
return lua::pushinteger(L, inv->size());
}
static int l_inventory_add(lua_State* L) {
lua_Integer invid = lua_tointeger(L, 1);
lua_Integer itemid = lua_tointeger(L, 2);
lua_Integer count = lua_tointeger(L, 3);
auto invid = lua::tointeger(L, 1);
auto itemid = lua::tointeger(L, 2);
auto count = lua::tointeger(L, 3);
validate_itemid(itemid);
auto inv = get_inventory(invid);
ItemStack item(itemid, count);
inv->move(item, scripting::indices);
lua_pushinteger(L, item.getCount());
return 1;
inv->move(item, indices);
return lua::pushinteger(L, item.getCount());
}
static int l_inventory_get_block(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
int64_t id = scripting::blocks->createBlockInventory(x, y, z);
lua_pushinteger(L, id);
return 1;
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
int64_t id = blocks->createBlockInventory(x, y, z);
return lua::pushinteger(L, id);
}
static int l_inventory_bind_block(lua_State* L) {
lua_Integer id = lua_tointeger(L, 1);
lua_Integer x = lua_tointeger(L, 2);
lua_Integer y = lua_tointeger(L, 3);
lua_Integer z = lua_tointeger(L, 4);
scripting::blocks->bindInventory(id, x, y, z);
auto id = lua::tointeger(L, 1);
auto x = lua::tointeger(L, 2);
auto y = lua::tointeger(L, 3);
auto z = lua::tointeger(L, 4);
blocks->bindInventory(id, x, y, z);
return 0;
}
static int l_inventory_unbind_block(lua_State* L) {
lua_Integer x = lua_tointeger(L, 1);
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
scripting::blocks->unbindInventory(x, y, z);
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
blocks->unbindInventory(x, y, z);
return 0;
}
static int l_inventory_clone(lua_State* L) {
lua_Integer id = lua_tointeger(L, 1);
auto clone = scripting::level->inventories->clone(id);
auto id = lua::tointeger(L, 1);
auto clone = level->inventories->clone(id);
if (clone == nullptr) {
lua_pushinteger(L, 0);
return 1;
return lua::pushinteger(L, 0);
}
lua_pushinteger(L, clone->getId());
return 1;
return lua::pushinteger(L, clone->getId());
}
static int l_inventory_move(lua_State* L) {
lua_Integer invAid = lua_tointeger(L, 1);
lua_Integer slotAid = lua_tointeger(L, 2);
auto invAid = lua::tointeger(L, 1);
auto slotAid = lua::tointeger(L, 2);
auto invA = get_inventory(invAid, 1);
validate_slotid(slotAid, invA.get());
lua_Integer invBid = lua_tointeger(L, 3);
lua_Integer slotBid = lua_isnil(L, 4) ? -1 : lua_tointeger(L, 4);
auto invBid = lua::tointeger(L, 3);
auto slotBid = lua_isnil(L, 4) ? -1 : lua::tointeger(L, 4);
auto invB = get_inventory(invBid, 3);
auto& slot = invA->getSlot(slotAid);
if (slotBid == -1) {
invB->move(slot, scripting::content->getIndices());
invB->move(slot, content->getIndices());
} else {
invB->move(slot, scripting::content->getIndices(), slotBid, slotBid+1);
invB->move(slot, content->getIndices(), slotBid, slotBid+1);
}
return 0;
}
const luaL_Reg inventorylib [] = {
{"get", lua_wrap_errors<l_inventory_get>},
{"set", lua_wrap_errors<l_inventory_set>},
{"size", lua_wrap_errors<l_inventory_size>},
{"add", lua_wrap_errors<l_inventory_add>},
{"move", lua_wrap_errors<l_inventory_move>},
{"get_block", lua_wrap_errors<l_inventory_get_block>},
{"bind_block", lua_wrap_errors<l_inventory_bind_block>},
{"unbind_block", lua_wrap_errors<l_inventory_unbind_block>},
{"clone", lua_wrap_errors<l_inventory_clone>},
{"get", lua::wrap<l_inventory_get>},
{"set", lua::wrap<l_inventory_set>},
{"size", lua::wrap<l_inventory_size>},
{"add", lua::wrap<l_inventory_add>},
{"move", lua::wrap<l_inventory_move>},
{"get_block", lua::wrap<l_inventory_get_block>},
{"bind_block", lua::wrap<l_inventory_bind_block>},
{"unbind_block", lua::wrap<l_inventory_unbind_block>},
{"clone", lua::wrap<l_inventory_clone>},
{NULL, NULL}
};