Skip to content

Commit

Permalink
Improve lighting performance by removing a try/catch in lightingAt
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Sep 30, 2010
1 parent a8514c4 commit b37aa24
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/terrain.js
Expand Up @@ -180,7 +180,6 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
for (var x = 0; x < 16*3; x++) {
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)) {
Expand All @@ -199,10 +198,11 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
function isFlooded(x,y,z) {
var light = lightingAt(x,y,z);
if (light <= 1) return true;
// TODO: unroll?
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 ((dx<0?-dx:dx)+(dz<0?-dz:dz)+(dy<0?-dy:dy) != 1) continue;
if (inBounds(x+dx,y+dy,z+dz)) {
if (isPenetrable(x+dx,y+dy,z+dz) &&
lightingAt(x+dx,y+dy,z+dz) < light-1) {
Expand Down Expand Up @@ -249,12 +249,7 @@ WorldTerrain.prototype.recalculateLighting = function (x, z, cb) {
cz_i = z >> me.chunk_xz_shift,
cx = cx_i << me.chunk_xz_shift,
cz = cz_i << me.chunk_xz_shift;
try {
return chunkFor(x,z).getLighting(x-cx, y, z-cz);
} catch (err) {
sys.debug('x: '+x+' z: '+z+ ' x_i: '+x_i + ' z_i: '+z_i);
throw err;
}
return chunkFor(x,z).getLighting(x-cx, y, z-cz);
}
function setLightingAt(x,y,z, light) {
var cx_i = x >> me.chunk_xz_shift,
Expand Down

0 comments on commit b37aa24

Please sign in to comment.