"use strict"; const StairsBuilder = (function(){ const StairsBuilder = function(hollow_bond){ const is_volume_free = (occupied, xf, yf, zf, xt, yt, zt) => { for(let x = xf; x < xt; x ++) for(let y = yf; y < yt; y ++) for(let z = zf; z < zt; z ++) if(occupied.has(`${x}_${y}_${z}`)) return false; return true; }; const reserve_volume = (occupied, xf, yf, zf, xt, yt, zt) => { for(let x = xf; x < xt; x ++) for(let y = yf; y < yt; y ++) for(let z = zf; z < zt; z ++) occupied.add(`${x}_${y}_${z}`); }; this.generate = hole => { if(hole.height < hollow_bond.stairs_height) return hole.stairs; const occupied = new Set(); for(let py = hole.yf; py <= hole.yt - hollow_bond.stairs_height; py++){ if(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 0) > hollow_bond.stairs_probability) continue; const wall_i = Math.floor(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 1) * 4); let stairs_xf, stairs_zf, stairs_xt, stairs_zt, wall_name; if(wall_i === 0 && hole.width >= hollow_bond.stairs_length){ // Pared Norte (Z = zf) wall_name = "north"; stairs_xf = hole.xf + Math.floor(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 2) * (hole.width - hollow_bond.stairs_length + 1)); stairs_xt = stairs_xf + hollow_bond.stairs_length; stairs_zf = hole.zf; stairs_zt = stairs_zf + 1; }else if(wall_i === 1 && hole.width >= hollow_bond.stairs_length){ // Pared Sur (Z = zt - 1) wall_name = "south"; stairs_xf = hole.xf + Math.floor(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 2) * (hole.width - hollow_bond.stairs_length + 1)); stairs_xt = stairs_xf + hollow_bond.stairs_length; stairs_zt = hole.zt; stairs_zf = stairs_zt - 1; }else if(wall_i === 2 && hole.depth >= hollow_bond.stairs_length){ // Pared Oeste (X = xf) wall_name = "west"; stairs_zf = hole.zf + Math.floor(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 2) * (hole.depth - hollow_bond.stairs_length + 1)); stairs_zt = stairs_zf + hollow_bond.stairs_length; stairs_xf = hole.xf; stairs_xt = stairs_xf + 1; }else if(wall_i === 3 && hole.depth >= hollow_bond.stairs_length){ // Pared Este (X = xt - 1) wall_name = "east"; stairs_zf = hole.zf + Math.floor(hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 2) * (hole.depth - hollow_bond.stairs_length + 1)); stairs_zt = stairs_zf + hollow_bond.stairs_length; stairs_xt = hole.xt; stairs_xf = stairs_xt - 1; }else continue; const clearance_yt = Math.min(hole.yt, py + hollow_bond.stairs_height + 1); if(!is_volume_free(occupied, stairs_xf, py, stairs_zf, stairs_xt, clearance_yt, stairs_zt)) continue; // Hay conflicto vertical con otra escalera o descansillo reserve_volume(occupied, stairs_xf, py, stairs_zf, stairs_xt, clearance_yt, stairs_zt); hole.stairs.push(new StairsModel(hollow_bond, { hole_id : hole.id, xf : stairs_xf, yf : py, zf : stairs_zf, xt : stairs_xt, yt : py + hollow_bond.stairs_height, zt : stairs_zt, wall : wall_name, direction : hollow_bond.hash.stairs(hole.xf, py, hole.zf, hole.xt, py + hollow_bond.stairs_height, hole.zt, 3) < 0.5 ? -1 : 1 })); }; return hole.stairs; }; }; return StairsBuilder; })();