This commit is contained in:
alexei-zebra 2024-01-03 23:57:50 +03:00
parent 3c7f46987a
commit 405f8b3d14
8 changed files with 36 additions and 29 deletions

View file

@ -348,7 +348,7 @@ void BlocksRenderer::render(const voxel* voxels) {
break;
}
case BlockModel::aabb: {
blockAABB(ivec3(x,y,z), texfaces, &def, vox.rotation(), !def.rt.emissive);
blockAABB(ivec3(x,y,z), texfaces, &def, vox.getDir(), !def.rt.emissive);
break;
}
default:

View file

@ -57,7 +57,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, voxel{0,0});
lighting->onBlockSet(x,y,z, 0);
if (def->rt.funcsset.onbroken) {
scripting::on_block_broken(player, def, x, y, z);

View file

@ -236,27 +236,27 @@ void PlayerController::updateInteraction(){
int x = iend.x;
int y = iend.y;
int z = iend.z;
uint8_t states = 0;
voxel voxel;
Block* def = contentIds->getBlockDef(player->chosenBlock);
if (def->rotatable){
const std::string& name = def->rotations.name;
if (name == "pipe") {
if (norm.x < 0.0f) states = BLOCK_DIR_WEST;
else if (norm.x > 0.0f) states = BLOCK_DIR_EAST;
else if (norm.y > 0.0f) states = BLOCK_DIR_UP;
else if (norm.y < 0.0f) states = BLOCK_DIR_DOWN;
else if (norm.z > 0.0f) states = BLOCK_DIR_NORTH;
else if (norm.z < 0.0f) states = BLOCK_DIR_SOUTH;
if (norm.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST);
else if (norm.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST);
else if (norm.y > 0.0f) voxel.setDir(BLOCK_DIR_UP);
else if (norm.y < 0.0f) voxel.setDir(BLOCK_DIR_DOWN);
else if (norm.z > 0.0f) voxel.setDir(BLOCK_DIR_NORTH);
else if (norm.z < 0.0f) voxel.setDir(BLOCK_DIR_SOUTH);
} else if (name == "pane") {
glm::vec3 vec = camera->dir;
if (abs(vec.x) > abs(vec.z)){
if (vec.x > 0.0f) states = BLOCK_DIR_EAST;
if (vec.x < 0.0f) states = BLOCK_DIR_WEST;
if (vec.x > 0.0f) voxel.setDir(BLOCK_DIR_EAST);
if (vec.x < 0.0f) voxel.setDir(BLOCK_DIR_WEST);
}
if (abs(vec.x) < abs(vec.z)){
if (vec.z > 0.0f) states = BLOCK_DIR_SOUTH;
if (vec.z < 0.0f) states = BLOCK_DIR_NORTH;
if (vec.z > 0.0f) voxel.setDir(BLOCK_DIR_SOUTH);
if (vec.z < 0.0f) voxel.setDir(BLOCK_DIR_NORTH);
}
}
}
@ -285,7 +285,8 @@ void PlayerController::updateInteraction(){
chosenBlock = 0;
}
if (chosenBlock != vox->id) {
chunks->set(x, y, z, chosenBlock, states);
voxel.id = chosenBlock;
chunks->set(x, y, z, voxel);
lighting->onBlockSet(x,y,z, chosenBlock);
if (def->rt.funcsset.onplaced) {
scripting::on_block_placed(player, def, x, y, z);

View file

@ -39,13 +39,14 @@ int l_block_index(lua_State* L) {
}
int l_set_block(lua_State* L) {
voxel voxel;
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);
scripting::level->chunks->set(x, y, z, id, states);
scripting::level->lighting->onBlockSet(x,y,z, id);
voxel.id = lua_tointeger(L, 4);
voxel.states = lua_tointeger(L, 5);
scripting::level->chunks->set(x, y, z, voxel);
scripting::level->lighting->onBlockSet(x,y,z, voxel.id);
return 0;
}

View file

@ -84,7 +84,7 @@ bool Chunk::decode(u_char8* data) {
void Chunk::convert(u_char8* data, const ContentLUT* lut) {
for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = data[i];
data[i] = lut->getBlockId(id);
blockid_t id = ((u_short16*)data)[i];
((u_short16*)data)[i] = lut->getBlockId(id);
}
}

View file

@ -77,7 +77,7 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
const Block* def = contentIds->getBlockDef(v->id);
if (def->obstacle) {
const AABB& hitbox = def->rotatable
? def->rt.hitboxes[v->rotation()]
? def->rt.hitboxes[v->getDir()]
: def->hitbox;
if (def->rt.solid) {
return &hitbox;
@ -158,7 +158,7 @@ Chunk* Chunks::getChunk(int x, int z){
return chunks[z * w + x].get();
}
void Chunks::set(int x, int y, int z, int id, uint8_t states){
void Chunks::set(int x, int y, int z, voxel voxel) {
if (y < 0 || y >= CHUNK_H)
return;
x -= ox * CHUNK_W;
@ -172,14 +172,14 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
return;
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = id;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = states;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = voxel.id;
chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = voxel.states;
chunk->setUnsaved(true);
chunk->setModified(true);
if (y < chunk->bottom) chunk->bottom = y;
else if (y + 1 > chunk->top) chunk->top = y + 1;
else if (id == 0) chunk->updateHeights();
else if (voxel.id == 0) chunk->updateHeights();
if (lx == 0 && (chunk = getChunk(cx+ox-1, cz+oz)))
chunk->setModified(true);
@ -246,7 +246,7 @@ voxel* Chunks::rayCast(vec3 start,
if (!def->rt.solid) {
const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->rotation()]
? def->rt.hitboxes[voxel->getDir()]
: def->hitbox;
scalar_t distance;
Ray ray(start, dir);
@ -344,7 +344,7 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
if (def->obstacle) {
if (!def->rt.solid) {
const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->rotation()]
? def->rt.hitboxes[voxel->getDir()]
: def->hitbox;
scalar_t distance;
ivec3 norm;

View file

@ -42,7 +42,7 @@ public:
voxel* get(int x, int y, int z);
light_t getLight(int x, int y, int z);
u_char8 getLight(int x, int y, int z, int channel);
void set(int x, int y, int z, int id, uint8_t states);
void set(int x, int y, int z, voxel voxel);
voxel* rayCast(glm::vec3 start,
glm::vec3 dir,

View file

@ -19,10 +19,15 @@ struct voxel {
blockid_t id;
u_short16 states;
inline u_char8 rotation() const {
inline u_char8 getDir() const {
return states & BLOCK_ROT_MASK;
}
inline void setDir(u_char8 dir) {
states &= ~BLOCK_ROT_MASK;
states |= dir;
}
inline u_char8 variant() const {
return (states & BLOCK_VARIANT_MASK) >> 4;
}