/** * @class Precise shadowcasting algorithm * @augments ROT.FOV */ ROT.FOV.PreciseShadowcasting = function(lightPassesCallback, options) { ROT.FOV.call(this, lightPassesCallback, options); } ROT.FOV.PreciseShadowcasting.extend(ROT.FOV); /** * @see ROT.FOV#compute */ ROT.FOV.PreciseShadowcasting.prototype.compute = function(x, y, R, callback) { /* this place is always visible */ callback(x, y, 0, 1); /* standing in a dark place. FIXME is this a good idea? */ if (!this._lightPasses(x, y)) { return; } /* list of all shadows */ var SHADOWS = []; var cx, cy, blocks, A1, A2, visibility; /* analyze surrounding cells in concentric rings, starting from the center */ for (var r=1; r<=R; r++) { var neighbors = this._getCircle(x, y, r); var neighborCount = neighbors.length; for (var i=0;i A2[0]) { /* split into two sub-arcs */ var v1 = this._checkVisibility(A1, [A1[1], A1[1]], blocks, SHADOWS); var v2 = this._checkVisibility([0, 1], A2, blocks, SHADOWS); return (v1+v2)/2; } /* index1: first shadow >= A1 */ var index1 = 0, edge1 = false; while (index1 < SHADOWS.length) { var old = SHADOWS[index1]; var diff = old[0]*A1[1] - A1[0]*old[1]; if (diff >= 0) { /* old >= A1 */ if (diff == 0 && !(index1 % 2)) { edge1 = true; } break; } index1++; } /* index2: last shadow <= A2 */ var index2 = SHADOWS.length, edge2 = false; while (index2--) { var old = SHADOWS[index2]; var diff = A2[0]*old[1] - old[0]*A2[1]; if (diff >= 0) { /* old <= A2 */ if (diff == 0 && (index2 % 2)) { edge2 = true; } break; } } var visible = true; if (index1 == index2 && (edge1 || edge2)) { /* subset of existing shadow, one of the edges match */ visible = false; } else if (edge1 && edge2 && index1+1==index2 && (index2 % 2)) { /* completely equivalent with existing shadow */ visible = false; } else if (index1 > index2 && (index1 % 2)) { /* subset of existing shadow, not touching */ visible = false; } if (!visible) { return 0; } /* fast case: not visible */ var visibleLength, P; /* compute the length of visible arc, adjust list of shadows (if blocking) */ var remove = index2-index1+1; if (remove % 2) { if (index1 % 2) { /* first edge within existing shadow, second outside */ var P = SHADOWS[index1]; visibleLength = (A2[0]*P[1] - P[0]*A2[1]) / (P[1] * A2[1]); if (blocks) { SHADOWS.splice(index1, remove, A2); } } else { /* second edge within existing shadow, first outside */ var P = SHADOWS[index2]; visibleLength = (P[0]*A1[1] - A1[0]*P[1]) / (A1[1] * P[1]); if (blocks) { SHADOWS.splice(index1, remove, A1); } } } else { if (index1 % 2) { /* both edges within existing shadows */ var P1 = SHADOWS[index1]; var P2 = SHADOWS[index2]; visibleLength = (P2[0]*P1[1] - P1[0]*P2[1]) / (P1[1] * P2[1]); if (blocks) { SHADOWS.splice(index1, remove); } } else { /* both edges outside existing shadows */ if (blocks) { SHADOWS.splice(index1, remove, A1, A2); } return 1; /* whole arc visible! */ } } var arcLength = (A2[0]*A1[1] - A1[0]*A2[1]) / (A1[1] * A2[1]); return visibleLength/arcLength; }