block inventories (backend)

This commit is contained in:
MihailRis
2024-02-12 18:28:18 +03:00
parent fe57c38edf
commit 87f50f79c9
12 changed files with 108 additions and 18 deletions
+22
View File
@@ -11,6 +11,7 @@
#include "../util/timeutil.h"
#include "../maths/fastmaths.h"
#include "../items/Inventory.h"
#include "../items/Inventories.h"
#include "scripting/scripting.h"
@@ -147,3 +148,24 @@ void BlocksController::randomTick(int tickid, int parts) {
}
}
}
int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
return 0;
}
int lx = x - chunk->x * CHUNK_W;
int lz = z - chunk->z * CHUNK_D;
auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) {
auto indices = level->content->getIndices();
auto def = indices->getBlockDef(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def->inventorySize;
if (invsize == 0) {
return 0;
}
inv = level->inventories->create(invsize);
chunk->addBlockInventory(inv, lx, y, lz);
}
return inv->getId();
}
+1 -1
View File
@@ -47,7 +47,7 @@ public:
void update(float delta);
void randomTick(int tickid, int parts);
void onBlocksTick(int tickid, int parts);
uint createBlockInventory(int x, int y, int z);
int64_t createBlockInventory(int x, int y, int z);
};
#endif // LOGIC_BLOCKS_CONTROLLER_H_
+10
View File
@@ -299,11 +299,21 @@ static int l_inventory_add(lua_State* L) {
return 1;
}
static int l_inventory_get_block(lua_State* L) {
lua::luaint x = lua_tointeger(L, 1);
lua::luaint y = lua_tointeger(L, 2);
lua::luaint z = lua_tointeger(L, 3);
int64_t id = scripting::blocks->createBlockInventory(x, y, z);
lua_pushinteger(L, id);
return 1;
}
static const luaL_Reg inventorylib [] = {
{"get", l_inventory_get},
{"set", l_inventory_set},
{"size", l_inventory_size},
{"add", l_inventory_add},
{"get_block", l_inventory_get_block},
{NULL, NULL}
};