summaryrefslogtreecommitdiffstats
path: root/src/World.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-03-13 15:30:05 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-03-13 16:41:49 +0100
commitcf647ebf504eb523d822fa79622cb59d009680da (patch)
treeefaa792f312bb18db07c94d1460801209279ed3f /src/World.cpp
parentFixed holes when block on section border destroyed (diff)
downloadAltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar.gz
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar.bz2
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar.lz
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar.xz
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.tar.zst
AltCraft-cf647ebf504eb523d822fa79622cb59d009680da.zip
Diffstat (limited to 'src/World.cpp')
-rw-r--r--src/World.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/World.cpp b/src/World.cpp
index 67abc02..75e0b3f 100644
--- a/src/World.cpp
+++ b/src/World.cpp
@@ -395,4 +395,117 @@ Entity* World::GetEntityPtr(unsigned int EntityId) {
}
entitiesMutex.unlock();
return nullptr;
+}
+
+unsigned char World::GetBlockLight(Vector pos)
+{
+ Vector sectionPos(std::floor(pos.x / 16.0),
+ std::floor(pos.y / 16.0),
+ std::floor(pos.z / 16.0));
+
+ Vector blockPos = pos - (sectionPos * 16);
+
+ Section* section = GetSectionPtr(sectionPos);
+ Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0));
+ Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0));
+ Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0));
+ Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0));
+ Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1));
+ Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1));
+
+ if (!section)
+ return 0;
+
+ Vector directions[] = {
+ Vector(0,0,0),
+ Vector(1,0,0),
+ Vector(-1,0,0),
+ Vector(0,1,0),
+ Vector(0,-1,0),
+ Vector(0,0,1),
+ Vector(0,0,-1),
+ };
+
+ unsigned char value = 0;
+
+ for (const Vector &dir : directions) {
+ Vector vec = blockPos + dir;
+ unsigned char dirValue = 0;
+
+ if (vec.x < 0 || vec.x > 15 || vec.y < 0 || vec.y > 15 || vec.z < 0 || vec.z > 15) {
+ if (vec.x < 0 && xn)
+ dirValue = xn->GetBlockLight(Vector(15, vec.y, vec.z));
+ if (vec.x > 15 && xp)
+ dirValue = xp->GetBlockLight(Vector(0, vec.y, vec.z));
+ if (vec.y < 0 && yn)
+ dirValue = yn->GetBlockLight(Vector(vec.x, 15, vec.z));
+ if (vec.y > 15 && yp)
+ dirValue = yp->GetBlockLight(Vector(vec.x, 0, vec.z));
+ if (vec.z < 0 && zn)
+ dirValue = zn->GetBlockLight(Vector(vec.x, vec.y, 15));
+ if (vec.z > 15 && zp)
+ dirValue = zp->GetBlockLight(Vector(vec.x, vec.y, 0));
+ } else
+ dirValue = section->GetBlockLight(vec);
+
+ value = _max(value, dirValue);
+ }
+ return value;
+}
+
+unsigned char World::GetBlockSkyLight(Vector pos)
+{
+ Vector sectionPos( std::floor(pos.x / 16.0),
+ std::floor(pos.y / 16.0),
+ std::floor(pos.z / 16.0));
+
+ Vector blockPos = pos - (sectionPos * 16);
+
+ Section* section = GetSectionPtr(sectionPos);
+ Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0));
+ Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0));
+ Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0));
+ Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0));
+ Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1));
+ Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1));
+
+ if (!section)
+ return 0;
+
+ Vector directions[] = {
+ Vector(0,0,0),
+ Vector(1,0,0),
+ Vector(-1,0,0),
+ Vector(0,1,0),
+ Vector(0,-1,0),
+ Vector(0,0,1),
+ Vector(0,0,-1),
+ };
+
+ unsigned char value = 0;
+
+ for (const Vector &dir : directions) {
+ Vector vec = blockPos + dir;
+ unsigned char dirValue = 0;
+
+ if (vec.x < 0 || vec.x > 15 || vec.y < 0 || vec.y > 15 || vec.z < 0 || vec.z > 15) {
+ if (vec.x < 0 && xn)
+ dirValue = xn->GetBlockSkyLight(Vector(15, vec.y, vec.z));
+ if (vec.x > 15 && xp)
+ dirValue = xp->GetBlockSkyLight(Vector(0, vec.y, vec.z));
+ if (vec.y < 0 && yn)
+ dirValue = yn->GetBlockSkyLight(Vector(vec.x, 15, vec.z));
+ if (vec.y > 15 && yp)
+ dirValue = yp->GetBlockSkyLight(Vector(vec.x, 0, vec.z));
+ if (vec.z < 0 && zn)
+ dirValue = zn->GetBlockSkyLight(Vector(vec.x, vec.y, 15));
+ if (vec.z > 15 && zp)
+ dirValue = zp->GetBlockSkyLight(Vector(vec.x, vec.y, 0));
+ }
+ else
+ dirValue = section->GetBlockSkyLight(vec);
+
+ value = _max(value, dirValue);
+ }
+ return value;
} \ No newline at end of file