small refactoring

This commit is contained in:
A-lex-Ra 2024-01-05 13:13:02 +06:00
parent 73595cd558
commit d671188680
7 changed files with 31 additions and 26 deletions

View File

@ -70,7 +70,7 @@ void BlocksController::updateBlock(int x, int y, int z) {
if (vox == nullptr) if (vox == nullptr)
return; return;
const Block* def = level->content->indices->getBlockDef(vox->id); const Block* def = level->content->indices->getBlockDef(vox->id);
if (def->grounded && !chunks->isSolid(x, y-1, z)) { if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
breakBlock(nullptr, def, x, y, z); breakBlock(nullptr, def, x, y, z);
return; return;
} }

View File

@ -281,7 +281,7 @@ void PlayerController::updateInteraction(){
if (!level->physics->isBlockInside(x,y,z, player->hitbox) if (!level->physics->isBlockInside(x,y,z, player->hitbox)
|| !def->obstacle){ || !def->obstacle){
Block* def = contentIds->getBlockDef(chosenBlock); Block* def = contentIds->getBlockDef(chosenBlock);
if (def->grounded && !chunks->isSolid(x, y-1, z)) { if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
chosenBlock = 0; chosenBlock = 0;
} }
if (chosenBlock != vox->id) { if (chosenBlock != vox->id) {

View File

@ -23,7 +23,7 @@ int l_is_solid_at(lua_State* L) {
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3); int z = lua_tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isSolid(x, y, z)); lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z));
return 1; return 1;
} }
@ -200,7 +200,7 @@ int l_is_replaceable_at(lua_State* L) {
int y = lua_tointeger(L, 2); int y = lua_tointeger(L, 2);
int z = lua_tointeger(L, 3); int z = lua_tointeger(L, 3);
lua_pushboolean(L, scripting::level->chunks->isReplaceable(x, y, z)); lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
return 1; return 1;
} }

View File

@ -48,7 +48,7 @@ struct AABB {
} }
/* Check if given point is inside */ /* Check if given point is inside */
inline bool inside(const glm::vec3 pos) const { inline bool contains(const glm::vec3 pos) const {
const glm::vec3 p = min(); const glm::vec3 p = min();
const glm::vec3 s = size(); const glm::vec3 s = size();
return !(pos.x < p.x || pos.y < p.y || pos.z < p.z || return !(pos.x < p.x || pos.y < p.y || pos.z < p.z ||

View File

@ -46,7 +46,7 @@ void PhysicsSolver::step(
hitbox->grounded = false; hitbox->grounded = false;
for (float x = (px-half.x+E); x <= (px+half.x-E); x+=s){ for (float x = (px-half.x+E); x <= (px+half.x-E); x+=s){
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
if (chunks->isObstacle(x,y,z)){ if (chunks->isObstacleAt(x,y,z)){
hitbox->grounded = true; hitbox->grounded = true;
break; break;
} }
@ -58,7 +58,7 @@ void PhysicsSolver::step(
hitbox->grounded = false; hitbox->grounded = false;
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
for (float z = (pz-half.z+E); z <= (pz+half.z-E); z+=s){ for (float z = (pz-half.z+E); z <= (pz+half.z-E); z+=s){
if (chunks->isObstacle(x,y,z)){ if (chunks->isObstacleAt(x,y,z)){
hitbox->grounded = true; hitbox->grounded = true;
break; break;
} }
@ -88,7 +88,7 @@ void PhysicsSolver::colisionCalc(
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){ for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float x = (pos.x-half.x-E); float x = (pos.x-half.x-E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.x *= 0.0f; vel.x *= 0.0f;
pos.x = floor(x) + aabb->max().x + half.x + E; pos.x = floor(x) + aabb->max().x + half.x + E;
break; break;
@ -100,7 +100,7 @@ void PhysicsSolver::colisionCalc(
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){ for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float x = (pos.x+half.x+E); float x = (pos.x+half.x+E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.x *= 0.0f; vel.x *= 0.0f;
pos.x = floor(x) - half.x + aabb->min().x - E; pos.x = floor(x) - half.x + aabb->min().x - E;
break; break;
@ -113,7 +113,7 @@ void PhysicsSolver::colisionCalc(
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){ for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
float z = (pos.z-half.z-E); float z = (pos.z-half.z-E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.z *= 0.0f; vel.z *= 0.0f;
pos.z = floor(z) + aabb->max().z + half.z + E; pos.z = floor(z) + aabb->max().z + half.z + E;
break; break;
@ -126,7 +126,7 @@ void PhysicsSolver::colisionCalc(
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){ for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
float z = (pos.z+half.z+E); float z = (pos.z+half.z+E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.z *= 0.0f; vel.z *= 0.0f;
pos.z = floor(z) - half.z + aabb->min().z - E; pos.z = floor(z) - half.z + aabb->min().z - E;
break; break;
@ -139,7 +139,7 @@ void PhysicsSolver::colisionCalc(
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float y = (pos.y-half.y-E); float y = (pos.y-half.y-E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f; vel.y *= 0.0f;
pos.y = floor(y) + aabb->max().y + half.y; pos.y = floor(y) + aabb->max().y + half.y;
hitbox->grounded = true; hitbox->grounded = true;
@ -152,7 +152,7 @@ void PhysicsSolver::colisionCalc(
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){ for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){ for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
float y = (pos.y+half.y+E); float y = (pos.y+half.y+E);
if ((aabb = chunks->isObstacle(x,y,z))){ if ((aabb = chunks->isObstacleAt(x,y,z))){
vel.y *= 0.0f; vel.y *= 0.0f;
pos.y = floor(y) - half.y + aabb->min().y - E; pos.y = floor(y) - half.y + aabb->min().y - E;
break; break;

View File

@ -50,12 +50,9 @@ Chunks::~Chunks(){
voxel* Chunks::get(int x, int y, int z){ voxel* Chunks::get(int x, int y, int z){
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
z -= oz * CHUNK_D; z -= oz * CHUNK_D;
int cx = x / CHUNK_W; int cx = floordiv(x, CHUNK_W);
int cy = y / CHUNK_H; int cy = floordiv(y, CHUNK_H);
int cz = z / CHUNK_D; int cz = floordiv(z, CHUNK_D);
if (x < 0) cx--;
if (y < 0) cy--;
if (z < 0) cz--;
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d) if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
return nullptr; return nullptr;
shared_ptr<Chunk> chunk = chunks[cz * w + cx]; // chunks is 2D-array shared_ptr<Chunk> chunk = chunks[cz * w + cx]; // chunks is 2D-array
@ -67,7 +64,7 @@ voxel* Chunks::get(int x, int y, int z){
return &chunk->voxels[(ly * CHUNK_D + lz) * CHUNK_W + lx]; return &chunk->voxels[(ly * CHUNK_D + lz) * CHUNK_W + lx];
} }
const AABB* Chunks::isObstacle(float x, float y, float z){ const AABB* Chunks::isObstacleAt(float x, float y, float z){
int ix = floor(x); int ix = floor(x);
int iy = floor(y); int iy = floor(y);
int iz = floor(z); int iz = floor(z);
@ -82,7 +79,7 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
if (def->rt.solid) { if (def->rt.solid) {
return &hitbox; return &hitbox;
} else { } else {
if (hitbox.inside({x - ix, y - iy, z - iz})) if (hitbox.contains({x - ix, y - iy, z - iz}))
return &hitbox; return &hitbox;
return nullptr; return nullptr;
} }
@ -90,20 +87,27 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
return nullptr; return nullptr;
} }
bool Chunks::isSolid(int x, int y, int z) { bool Chunks::isSolidBlock(int x, int y, int z) {
voxel* v = get(x, y, z); voxel* v = get(x, y, z);
if (v == nullptr) if (v == nullptr)
return false; return false;
return contentIds->getBlockDef(v->id)->rt.solid; return contentIds->getBlockDef(v->id)->rt.solid;
} }
bool Chunks::isReplaceable(int x, int y, int z) { bool Chunks::isReplaceableBlock(int x, int y, int z) {
voxel* v = get(x, y, z); voxel* v = get(x, y, z);
if (v == nullptr) if (v == nullptr)
return false; return false;
return contentIds->getBlockDef(v->id)->replaceable; return contentIds->getBlockDef(v->id)->replaceable;
} }
bool Chunks::isObstacleBlock(int x, int y, int z) {
voxel* v = get(x, y, z);
if (v == nullptr)
return false;
return contentIds->getBlockDef(v->id)->obstacle;
}
ubyte Chunks::getLight(int x, int y, int z, int channel){ ubyte Chunks::getLight(int x, int y, int z, int channel){
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
z -= oz * CHUNK_D; z -= oz * CHUNK_D;

View File

@ -53,9 +53,10 @@ public:
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist); glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
const AABB* isObstacle(float x, float y, float z); const AABB* isObstacleAt(float x, float y, float z);
bool isSolid(int x, int y, int z); bool isSolidBlock(int x, int y, int z);
bool isReplaceable(int x, int y, int z); bool isReplaceableBlock(int x, int y, int z);
bool isObstacleBlock(int x, int y, int z);
// does not move chunks inside // does not move chunks inside
void _setOffset(int x, int z); void _setOffset(int x, int z);