format fix

This commit is contained in:
MihailRis 2024-05-17 21:37:02 +03:00
parent a93906d0f1
commit 7957a12b84

View File

@ -16,18 +16,20 @@
#include <limits.h> #include <limits.h>
#include <vector> #include <vector>
Chunks::Chunks(uint32_t w, uint32_t d, Chunks::Chunks(
uint32_t w, uint32_t d,
int32_t ox, int32_t oz, int32_t ox, int32_t oz,
WorldFiles* wfile, WorldFiles* wfile,
LevelEvents* events, LevelEvents* events,
const Content* content) const Content* content
: contentIds(content->getIndices()), ) : contentIds(content->getIndices()),
chunks(w*d), chunks(w*d),
chunksSecond(w*d), chunksSecond(w*d),
w(w), d(d), ox(ox), oz(oz), w(w), d(d), ox(ox), oz(oz),
worldFiles(wfile), worldFiles(wfile),
events(events) { events(events)
volume = (size_t)w*(size_t)d; {
volume = static_cast<size_t>(w)*static_cast<size_t>(d);
chunksCount = 0; chunksCount = 0;
} }
@ -37,11 +39,13 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) {
int cx = floordiv(x, CHUNK_W); int cx = floordiv(x, CHUNK_W);
int cy = floordiv(y, CHUNK_H); int cy = floordiv(y, CHUNK_H);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) {
return nullptr; return nullptr;
}
auto& chunk = chunks[cz * w + cx]; // not thread safe auto& chunk = chunks[cz * w + cx]; // not thread safe
if (chunk == nullptr) if (chunk == nullptr) {
return nullptr; return nullptr;
}
int lx = x - cx * CHUNK_W; int lx = x - cx * CHUNK_W;
int ly = y - cy * CHUNK_H; int ly = y - cy * CHUNK_H;
int lz = z - cz * CHUNK_D; int lz = z - cz * CHUNK_D;
@ -67,10 +71,11 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z){
? def->rt.hitboxes[v->rotation()] ? def->rt.hitboxes[v->rotation()]
: def->hitboxes; : def->hitboxes;
for (const auto& hitbox : boxes) { for (const auto& hitbox : boxes) {
if (hitbox.contains({x - ix, y - iy, z - iz})) if (hitbox.contains({x - ix, y - iy, z - iz})) {
return &hitbox; return &hitbox;
} }
} }
}
return nullptr; return nullptr;
} }
@ -101,11 +106,13 @@ ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel){
int cx = floordiv(x, CHUNK_W); int cx = floordiv(x, CHUNK_W);
int cy = floordiv(y, CHUNK_H); int cy = floordiv(y, CHUNK_H);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) {
return 0; return 0;
}
const auto& chunk = chunks[(cy * d + cz) * w + cx]; const auto& chunk = chunks[(cy * d + cz) * w + cx];
if (chunk == nullptr) if (chunk == nullptr) {
return 0; return 0;
}
int lx = x - cx * CHUNK_W; int lx = x - cx * CHUNK_W;
int ly = y - cy * CHUNK_H; int ly = y - cy * CHUNK_H;
int lz = z - cz * CHUNK_D; int lz = z - cz * CHUNK_D;
@ -118,18 +125,20 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z){
int cx = floordiv(x, CHUNK_W); int cx = floordiv(x, CHUNK_W);
int cy = floordiv(y, CHUNK_H); int cy = floordiv(y, CHUNK_H);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) {
return 0; return 0;
}
const auto& chunk = chunks[(cy * d + cz) * w + cx]; const auto& chunk = chunks[(cy * d + cz) * w + cx];
if (chunk == nullptr) if (chunk == nullptr) {
return 0; return 0;
}
int lx = x - cx * CHUNK_W; int lx = x - cx * CHUNK_W;
int ly = y - cy * CHUNK_H; int ly = y - cy * CHUNK_H;
int lz = z - cz * CHUNK_D; int lz = z - cz * CHUNK_D;
return chunk->lightmap.get(lx,ly,lz); return chunk->lightmap.get(lx,ly,lz);
} }
Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z){ Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) {
if (y < 0 || y >= CHUNK_H) if (y < 0 || y >= CHUNK_H)
return nullptr; return nullptr;
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
@ -149,7 +158,7 @@ Chunk* Chunks::getChunk(int x, int z){
return chunks[z * w + x].get(); return chunks[z * w + x].get();
} }
void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states){ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states) {
if (y < 0 || y >= CHUNK_H) if (y < 0 || y >= CHUNK_H)
return; return;
x -= ox * CHUNK_W; x -= ox * CHUNK_W;
@ -189,12 +198,14 @@ void Chunks::set(int32_t x, int32_t y, int32_t z, uint32_t id, uint8_t states){
chunk->setModified(true); chunk->setModified(true);
} }
voxel* Chunks::rayCast(glm::vec3 start, voxel* Chunks::rayCast(
glm::vec3 start,
glm::vec3 dir, glm::vec3 dir,
float maxDist, float maxDist,
glm::vec3& end, glm::vec3& end,
glm::ivec3& norm, glm::ivec3& norm,
glm::ivec3& iend) { glm::ivec3& iend
) {
float px = start.x; float px = start.x;
float py = start.y; float py = start.y;
float pz = start.z; float pz = start.z;
@ -230,8 +241,9 @@ voxel* Chunks::rayCast(glm::vec3 start,
while (t <= maxDist){ while (t <= maxDist){
voxel* voxel = get(ix, iy, iz); voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr){ return nullptr; } if (voxel == nullptr){
return nullptr;
}
const Block* def = contentIds->getBlockDef(voxel->id); const Block* def = contentIds->getBlockDef(voxel->id);
if (def->selectable){ if (def->selectable){
end.x = px + t * dx; end.x = px + t * dx;
@ -346,8 +358,9 @@ glm::vec3 Chunks::rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDis
while (t <= maxDist) { while (t <= maxDist) {
voxel* voxel = get(ix, iy, iz); voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr) { return glm::vec3(px + t * dx, py + t * dy, pz + t * dz); } if (voxel == nullptr) {
return glm::vec3(px + t * dx, py + t * dy, pz + t * dz);
}
const Block* def = contentIds->getBlockDef(voxel->id); const Block* def = contentIds->getBlockDef(voxel->id);
if (def->obstacle) { if (def->obstacle) {
if (!def->rt.solid) { if (!def->rt.solid) {
@ -414,14 +427,14 @@ void Chunks::translate(int32_t dx, int32_t dz) {
for (uint i = 0; i < volume; i++){ for (uint i = 0; i < volume; i++){
chunksSecond[i] = nullptr; chunksSecond[i] = nullptr;
} }
for (uint32_t z = 0; z < d; z++){ for (uint32_t z = 0; z < d; z++) {
for (uint32_t x = 0; x < w; x++){ for (uint32_t x = 0; x < w; x++) {
auto chunk = chunks[z * w + x]; auto chunk = chunks[z * w + x];
int nx = x - dx; int nx = x - dx;
int nz = z - dz; int nz = z - dz;
if (chunk == nullptr) if (chunk == nullptr)
continue; continue;
if (nx < 0 || nz < 0 || nx >= int(w) || nz >= int(d)){ if (nx < 0 || nz < 0 || nx >= int(w) || nz >= int(d)) {
events->trigger(EVT_CHUNK_HIDDEN, chunk.get()); events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
regions.put(chunk.get()); regions.put(chunk.get());
chunksCount--; chunksCount--;