Skip to content

Commit

Permalink
Make lighting a bit faster
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Sep 28, 2010
1 parent 7c81b31 commit a8514c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var Chunk = function () {
this.sizeZ = 16;

this.sectionSize = this.sizeX * this.sizeY * this.sizeZ;
this.lit = false;
this.lit = 0;

this.data = new Buffer(this.sizeX * this.sizeY * this.sizeZ * 2.5);
for (var i = 0; i < this.data.length; i++) {
Expand Down
20 changes: 16 additions & 4 deletions src/terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
for (var dx = -1; dx <= 1; dx++) {
for (var dz = -1; dz <= 1; dz++) {
var chunk_data = dchunk(dx, dz);
if (!chunk.lit) {
if (chunk_data.lit < 1) {
chunk_data.clearLight();
chunk_data.setSkyLight(0xf);
chunk_data.lit = 1;
}
}
}

var numFlooded = 0;
// step 2: find lit blocks that haven't correctly filled adjacent blocks.
// TODO: don't hardcode chunk size :/
var baseX = (x_i-1) << me.chunk_xz_shift;
Expand All @@ -179,24 +181,31 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
for (var z = 0; z < 16*3; z++) {
for (var y = 0; y < 128; y++) {
if (!inBounds(x+baseX, y, z+baseZ)) continue;
// don't flood from impenetrable blocks, since they are all dark.
if (!isPenetrable(x+baseX, y, z+baseZ)) continue;
if (!isFlooded(x+baseX, y, z+baseZ)) {
floodLightFrom(x+baseX, y, z+baseZ);
numFlooded++;
}
}
}
}
sys.debug("flooded: " + numFlooded);

me.chunks[[x_i, z_i]].lit = true;
dchunk(0,0).lit = 2;

cb();

function isFlooded(x,y,z) {
var light = lightingAt(x,y,z);
if (light <= 1) return true;
for (var dx = -1; dx <= 1; dx++) {
for (var dz = -1; dz <= 1; dz++) {
for (var dy = -1; dy <= 1; dy++) {
if (Math.abs(dx)+Math.abs(dz)+Math.abs(dy) > 1) continue;
if (inBounds(x+dx,y+dy,z+dz)) {
if (lightingAt(x+dx,y+dy,z+dz) < light-1) {
if (isPenetrable(x+dx,y+dy,z+dz) &&
lightingAt(x+dx,y+dy,z+dz) < light-1) {
return false;
}
}
Expand All @@ -212,11 +221,14 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
for (var dx = -1; dx <= 1; dx++) {
for (var dz = -1; dz <= 1; dz++) {
for (var dy = -1; dy <= 1; dy++) {
if (dx === 0 && dz === 0 && dy === 0) { continue; }
if (!inBounds(x+dx, y+dy, z+dz)) { continue; }
if (!isPenetrable(x+dx, y+dy, z+dz)) { continue; }
if (lightingAt(x+dx, y+dy, z+dz) < light-1) {
setLightingAt(x+dx, y+dy, z+dz, light-1);
floodLightFrom(x+dx, y+dy, z+dz);
if (light-1 > 1) {
floodLightFrom(x+dx, y+dy, z+dz);
}
}
}
}
Expand Down

0 comments on commit a8514c4

Please sign in to comment.