converting block states to a bitfield

This commit is contained in:
MihailRis
2024-05-31 11:13:36 +03:00
parent 5dd06b233e
commit 6d933ac263
17 changed files with 197 additions and 164 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ void BlocksController::updateSides(int x, int y, int z) {
}
void BlocksController::breakBlock(Player* player, const Block* def, int x, int y, int z) {
chunks->set(x,y,z, 0, 0);
chunks->set(x,y,z, 0, {});
lighting->onBlockSet(x,y,z, 0);
if (def->rt.funcsset.onbroken) {
scripting::on_block_broken(player, def, x, y, z);
+6 -5
View File
@@ -364,7 +364,7 @@ void PlayerController::updateInteraction(){
if (vox != nullptr) {
player->selectedVoxel = *vox;
selectedBlockId = vox->id;
selectedBlockRotation = vox->rotation();
selectedBlockRotation = vox->state.rotation;
player->selectedBlockPosition = iend;
selectedPointPosition = end;
selectedBlockNormal = norm;
@@ -373,7 +373,8 @@ void PlayerController::updateInteraction(){
int z = iend.z;
Block* def = indices->getBlockDef(item->rt.placingBlock);
uint8_t states = determine_rotation(def, norm, camera->dir);
blockstate state {};
state.rotation = determine_rotation(def, norm, camera->dir);
if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) {
if (scripting::on_item_break_block(player.get(), item, x, y, z))
@@ -410,13 +411,13 @@ void PlayerController::updateInteraction(){
z = (iend.z)+(norm.z);
} else {
if (def->rotations.name == "pipe") {
states = BLOCK_DIR_UP;
state.rotation = BLOCK_DIR_UP;
}
}
vox = chunks->get(x, y, z);
blockid_t chosenBlock = def->rt.id;
if (vox && (target = indices->getBlockDef(vox->id))->replaceable) {
if (!level->physics->isBlockInside(x,y,z,def,states, player->hitbox.get())
if (!level->physics->isBlockInside(x,y,z,def,state, player->hitbox.get())
|| !def->obstacle){
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
chosenBlock = 0;
@@ -426,7 +427,7 @@ void PlayerController::updateInteraction(){
glm::ivec3(x, y, z), def,
BlockInteraction::placing
);
chunks->set(x, y, z, chosenBlock, states);
chunks->set(x, y, z, chosenBlock, state);
lighting->onBlockSet(x,y,z, chosenBlock);
if (def->rt.funcsset.onplaced) {
scripting::on_block_placed(player.get(), def, x, y, z);
+12 -12
View File
@@ -60,7 +60,7 @@ int l_set_block(lua_State* L) {
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
lua_Integer id = lua_tointeger(L, 4);
lua_Integer states = lua_tointeger(L, 5);
lua_Integer state = lua_tointeger(L, 5);
bool noupdate = lua_toboolean(L, 6);
if (id < 0 || size_t(id) >= scripting::indices->countBlockDefs()) {
return 0;
@@ -68,7 +68,7 @@ int l_set_block(lua_State* L) {
if (!scripting::level->chunks->get(x, y, z)) {
return 0;
}
scripting::level->chunks->set(x, y, z, id, states);
scripting::level->chunks->set(x, y, z, id, int2blockstate(state));
scripting::level->lighting->onBlockSet(x,y,z, id);
if (!noupdate)
scripting::blocks->updateSides(x, y, z);
@@ -97,7 +97,7 @@ int l_get_block_x(lua_State* L) {
if (!def->rotatable) {
return lua::pushivec3(L, 1, 0, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
return lua::pushivec3(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
}
}
@@ -114,7 +114,7 @@ int l_get_block_y(lua_State* L) {
if (!def->rotatable) {
return lua::pushivec3(L, 0, 1, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
return lua::pushivec3(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
}
}
@@ -131,7 +131,7 @@ int l_get_block_z(lua_State* L) {
if (!def->rotatable) {
return lua::pushivec3(L, 0, 0, 1);
} else {
const CoordSystem& rot = def->rotations.variants[vox->rotation()];
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
return lua::pushivec3(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
}
}
@@ -141,7 +141,7 @@ int l_get_block_rotation(lua_State* L) {
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int rotation = vox == nullptr ? 0 : vox->rotation();
int rotation = vox == nullptr ? 0 : vox->state.rotation;
lua_pushinteger(L, rotation);
return 1;
}
@@ -155,7 +155,7 @@ int l_set_block_rotation(lua_State* L) {
if (vox == nullptr) {
return 0;
}
vox->setRotation(value);
vox->state.rotation = value;
scripting::level->chunks->getChunkByVoxel(x, y, z)->setModified(true);
return 0;
}
@@ -165,7 +165,7 @@ int l_get_block_states(lua_State* L) {
lua_Integer y = lua_tointeger(L, 2);
lua_Integer z = lua_tointeger(L, 3);
voxel* vox = scripting::level->chunks->get(x, y, z);
int states = vox == nullptr ? 0 : vox->states;
int states = vox == nullptr ? 0 : blockstate2int(vox->state);
lua_pushinteger(L, states);
return 1;
}
@@ -181,7 +181,7 @@ int l_set_block_states(lua_State* L) {
return 0;
}
voxel* vox = scripting::level->chunks->get(x, y, z);
vox->states = states;
vox->state = int2blockstate(states);
chunk->setModified(true);
return 0;
}
@@ -199,7 +199,7 @@ int l_get_block_user_bits(lua_State* L) {
return 1;
}
uint mask = ((1 << bits) - 1) << offset;
uint data = (vox->states & mask) >> offset;
uint data = (blockstate2int(vox->state) & mask) >> offset;
lua_pushinteger(L, data);
return 1;
}
@@ -208,7 +208,7 @@ int l_set_block_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 offset = lua_tointeger(L, 4);
lua_Integer bits = lua_tointeger(L, 5);
size_t mask = ((1 << bits) - 1) << offset;
@@ -218,7 +218,7 @@ int l_set_block_user_bits(lua_State* L) {
if (vox == nullptr) {
return 0;
}
vox->states = (vox->states & (~mask)) | value;
vox->state.userbits = (vox->state.userbits & (~mask)) | value;
return 0;
}