diff --git a/dist/index.js b/dist/index.js index 640dc4d..aa1a856 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -(()=>{"use strict";var t={895:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Building=void 0,e.Building=class{constructor(t,e){if(4!==e.length)throw Error("Invalid building vertices");this.path=t,this.vertices=e;const i=this.vertices.map((t=>t.x)),s=this.vertices.map((t=>t.y));this.position={x:Math.min(...i),y:Math.min(...s)},this.width=Math.max(...i)-this.position.x+1,this.height=Math.max(...s)-this.position.y+1}remove(){const t=this.path.getBuildings(),e=t.findIndex((t=>t===this));-1!==e&&t.splice(e,1)}each(t){for(let e=0;et.getBuildings())).flat()}getAllNodes(){return this.nodes}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}generate(t={}){return s(this,void 0,void 0,(function*(){this.reset(),this.params=Object.assign({mode:o.CityGenerationMode.RUNTIME,seed:[],startPosition:{x:Math.round(this.width/2),y:Math.round(this.height/2)},startDirections:[0,90,180,270],streetMinLength:10,buildingMinSize:3,buildingMaxSize:6,buildingMinSpace:1,buildingMaxSpace:3,probabilityIntersection:.1,probabilityTurn:.05,probabilityStreetEnd:.001},t),this.params.mode===o.CityGenerationMode.SEED&&(this.seed=0===this.params.seed.length?(0,h.generateSeed)():this.params.seed);const e=this.addNode(this.params.startPosition);this.markAt(this.params.startPosition,e),this.params.startDirections.forEach((t=>{e.addOutputPath(t)})),this.generatePaths(),this.generateBuildings()}))}reset(){for(let t=0;t{const e=this.getAllPaths();for(let t=0,i=e.length;tt.isCompleted()))||t()};t()}processingPath(t){if(t.isCompleted())return;const e=t.getNextCursor();if(void 0===this.getAt(e)||this.variability(this.params.probabilityStreetEnd))return void this.closePath(t);const i=this.getCross(t);if(null==i?void 0:i.tile){let e=null;return i.tile instanceof r.Node?e=i.tile:i.tile instanceof n.Path&&(e=this.splitPath(i.tile,i.position)),void(e&&t.setNodeEnd(e))}t.setCursor(e),this.markAt(t.getCursor(),t);const s=this.params.streetMinLength;t.getLength()>s&&!this.getCross(t,s)&&(this.variability(this.params.probabilityIntersection)?this.forkPath(t):this.variability(this.params.probabilityTurn)&&this.turnPath(t))}generateBuildings(){this.getAllPaths().forEach((t=>{const e=(0,h.getShift)(t.direction),i=t.getNodeBeg().position.x+e.x,s=t.getNodeBeg().position.y+e.y;(0,h.turnDirection)(t.direction).forEach((e=>{let n=0;for(;t.getLength()>n;){const r=(0,h.getShift)(t.direction,n),o=(0,h.getShift)(e),a={x:i+r.x+o.x,y:s+r.y+o.y},d=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(n+d[0]>t.getLength())break;this.processingBuilding(t,a,d,[t.direction,e]);const u=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);n+=d[0]+u}}))}))}processingBuilding(t,e,i,s){const n=[],r=[];for(let t=0;t{this.markAt(t,o)}))}getAt(t){var e,i;return null===(i=null===(e=this.matrix)||void 0===e?void 0:e[t.y])||void 0===i?void 0:i[t.x]}markAt(t,e){this.matrix[t.y][t.x]=e}isEmptyAt(t){return null===this.getAt(t)}addNode(t){const e=new r.Node(this.nodes.length,t);return this.nodes.push(e),e}closePath(t){const e=t.getCursor(),i=this.addNode(e);return t.setNodeEnd(i),this.markAt(e,i),i}forkPath(t){let e=(0,h.forkDirection)(t.direction).sort((()=>this.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length||1===e.length&&e[0]===t.direction)return;const i=this.closePath(t);for(let t=0;tthis.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length)return;const i=this.closePath(t);i.addOutputPath(e[0]),this.markAt(i.position,i)}splitPath(t,e){const i=this.addNode(e),s=i.addOutputPath(t.direction);this.markAt(e,i);const r=t.getNodeEnd();return r?s.setNodeEnd(r):s.setCursor(t.getCursor()),s.each((t=>{this.getAt(t)instanceof n.Path&&this.markAt(t,s)})),t.setNodeEnd(i),i}getCross(t,e=1){const i=t.getCursor();for(let s=1;s<=e;s++){const e=(0,h.getShift)(t.direction,s),n={x:i.x+e.x,y:i.y+e.y};if(!this.isEmptyAt(n))return{tile:this.getAt(n),position:n}}return null}filterDirections(t,e){return e.filter((e=>{const i=(0,h.getShift)(e),s=t.getCursor(),n={x:s.x+i.x,y:s.y+i.y};return this.isEmptyAt(n)}))}variability(t){if(!this.seed)return(0,h.randomChance)(t);const e=this.seed[this.gauge%this.seed.length];return this.gauge++,e/1e3>=1-t}}},465:function(t,e,i){var s=this&&this.__createBinding||(Object.create?function(t,e,i,s){void 0===s&&(s=i);var n=Object.getOwnPropertyDescriptor(e,i);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,s,n)}:function(t,e,i,s){void 0===s&&(s=i),t[s]=e[i]}),n=this&&this.__exportStar||function(t,e){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(e,i)||s(e,t,i)};Object.defineProperty(e,"__esModule",{value:!0}),n(i(863),e),n(i(895),e),n(i(792),e),n(i(82),e),n(i(309),e)},792:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;const s=i(82),n=i(309);e.Node=class{constructor(t,e){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=e}addOutputPath(t){const e=new s.Path(this,t);return this.outputPaths.push(e),e}removeOutputPath(t){const e=this.outputPaths.indexOf(t);-1!==e&&this.outputPaths.splice(e,1)}getOutputPaths(){return this.outputPaths}addInputPath(t){const e=t.getNodeEnd();e&&e.removeInputPath(t),this.inputPaths.push(t)}removeInputPath(t){const e=this.inputPaths.indexOf(t);-1!==e&&this.inputPaths.splice(e,1)}getInputPaths(){return this.inputPaths}getAllPaths(){return this.outputPaths.concat(this.inputPaths)}getType(){const t=this.outputPaths.length+this.inputPaths.length;return 2===t?n.NodeType.TURN:1===t?n.NodeType.END:n.NodeType.CROSS}}},82:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Path=void 0;const s=i(679),n=i(895);e.Path=class{constructor(t,e){this.buildings=[],this.nodeEnd=null,this.direction=e,this.nodeBeg=t,this.cursor=t.position}getBuildings(){return this.buildings}getPositions(){var t,e;return{beg:this.nodeBeg.position,end:null!==(e=null===(t=this.nodeEnd)||void 0===t?void 0:t.position)&&void 0!==e?e:this.cursor}}isCompleted(){return Boolean(this.nodeEnd)}getNextCursor(){const t=(0,s.getShift)(this.direction);return{x:this.cursor.x+t.x,y:this.cursor.y+t.y}}getCursor(){return this.cursor}setCursor(t){this.cursor=t}getNodeBeg(){return this.nodeBeg}getNodeEnd(){return this.nodeEnd}setNodeEnd(t){t.addInputPath(this),this.nodeEnd=t,this.cursor=t.position}addBuilding(t){const e=new n.Building(this,t);return this.buildings.push(e),e}getLength(){const t=this.getPositions();return Math.hypot(t.beg.x-t.end.x,t.beg.y-t.end.y)}remove(){this.nodeBeg.removeOutputPath(this),this.nodeEnd&&this.nodeEnd.removeInputPath(this)}each(t){const e=(0,s.getShift)(this.direction),i=this.getLength(),n=Object.assign({},this.nodeBeg.position);for(let s=0;s{var i,s;Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=e.CityGenerationMode=void 0,function(t){t.RUNTIME="RUNTIME",t.SEED="SEED"}(i||(e.CityGenerationMode=i={})),function(t){t.TURN="TURN",t.CROSS="CROSS",t.END="END"}(s||(e.NodeType=s={}))},679:(t,e)=>{function i(t){return t*(Math.PI/180)}function s(t){return[(t+90)%360,(t-90)%360]}Object.defineProperty(e,"__esModule",{value:!0}),e.between=e.getShift=e.forkDirection=e.turnDirection=e.degToRad=e.generateSeed=e.randomRange=e.randomItem=e.randomChance=void 0,e.randomChance=function(t){return Math.random()>1-t},e.randomItem=function(t){return t[Math.floor(Math.random()*t.length)]},e.randomRange=function(t,e){return Math.floor(t+Math.random()*(e-t+1))},e.generateSeed=function(t=32){const e=[];for(let i=0;i=e[0]&&t<=e[1]}}},e={},i=function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s].call(r.exports,r,r.exports,i),r.exports}(465);module.exports=i})(); \ No newline at end of file +(()=>{"use strict";var t={895:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Building=void 0,e.Building=class{constructor(t,e){if(4!==e.length)throw Error("Invalid building vertices");this.path=t,this.vertices=e;const i=this.vertices.map((t=>t.x)),s=this.vertices.map((t=>t.y));this.position={x:Math.min(...i),y:Math.min(...s)},this.width=Math.max(...i)-this.position.x+1,this.height=Math.max(...s)-this.position.y+1}remove(){const t=this.path.getBuildings(),e=t.findIndex((t=>t===this));-1!==e&&t.splice(e,1)}each(t){for(let e=0;et.getBuildings())).flat()}getAllNodes(){return this.nodes}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}generate(t={}){return s(this,void 0,void 0,(function*(){this.reset(),this.params=Object.assign({mode:o.CityGenerationMode.RUNTIME,seed:[],startPosition:{x:Math.round(this.width/2),y:Math.round(this.height/2)},startDirections:[0,90,180,270],streetMinLength:10,buildingMinSize:3,buildingMaxSize:6,buildingMinSpace:1,buildingMaxSpace:3,probabilityIntersection:.1,probabilityTurn:.05,probabilityStreetEnd:.001},t),this.params.mode===o.CityGenerationMode.SEED&&(this.seed=0===this.params.seed.length?(0,h.generateSeed)():this.params.seed);const e=this.addNode(this.params.startPosition);this.markAt(this.params.startPosition,e),this.params.startDirections.forEach((t=>{e.addOutputPath(t)})),this.generatePaths(),this.generateBuildings()}))}reset(){for(let t=0;t{const e=this.getAllPaths();for(let t=0,i=e.length;tt.isCompleted()))||t()};t()}processingPath(t){if(t.isCompleted())return;const e=t.getNextCursor();if(void 0===this.getAt(e)||this.variability(this.params.probabilityStreetEnd))return void this.closePath(t);const i=this.getCross(t);if(null==i?void 0:i.tile){let e=null;return i.tile instanceof r.Node?e=i.tile:i.tile instanceof n.Path&&(e=this.splitPath(i.tile,i.position)),void(e&&t.setNodeEnd(e))}t.setCursor(e),this.markAt(t.getCursor(),t);const s=this.params.streetMinLength;t.getLength()>s&&!this.getCross(t,s)&&(this.variability(this.params.probabilityIntersection)?this.forkPath(t):this.variability(this.params.probabilityTurn)&&this.turnPath(t))}generateBuildings(){this.getAllPaths().forEach((t=>{const e=(0,h.getShift)(t.direction),i=t.getNodeBeg().position.x+e.x,s=t.getNodeBeg().position.y+e.y;(0,h.turnDirection)(t.direction).forEach((e=>{let n=0;for(;t.getLength()>n;){const r=(0,h.getShift)(t.direction,n),o=(0,h.getShift)(e),a={x:i+r.x+o.x,y:s+r.y+o.y},d=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(n+d[0]>t.getLength())break;this.processingBuilding(t,a,d,[t.direction,e]);const u=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);n+=d[0]+u}}))}))}processingBuilding(t,e,i,s){const n=[],r=[];for(let t=0;t{this.markAt(t,o)}))}getAt(t){var e,i;return null===(i=null===(e=this.matrix)||void 0===e?void 0:e[t.y])||void 0===i?void 0:i[t.x]}markAt(t,e){this.matrix[t.y][t.x]=e}isEmptyAt(t){return null===this.getAt(t)}addNode(t,e){const i=new r.Node(this.nodes.length,t);return void 0===e?this.nodes.push(i):this.nodes.splice(e,0,i),i}closePath(t){const e=t.getCursor(),i=this.addNode(e);return t.setNodeEnd(i),this.markAt(e,i),i}forkPath(t){let e=(0,h.forkDirection)(t.direction).sort((()=>this.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length||1===e.length&&e[0]===t.direction)return;const i=this.closePath(t);for(let t=0;tthis.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length)return;const i=this.closePath(t);i.addOutputPath(e[0]),this.markAt(i.position,i)}splitPath(t,e){const i=t.getNodeBeg(),s=this.nodes.findIndex((t=>t===i)),r=this.addNode(e,s+1),o=r.addOutputPath(t.direction);this.markAt(e,r);const h=t.getNodeEnd();return h?o.setNodeEnd(h):o.setCursor(t.getCursor()),o.each((t=>{this.getAt(t)instanceof n.Path&&this.markAt(t,o)})),t.setNodeEnd(r),r}getCross(t,e=1){const i=t.getCursor();for(let s=1;s<=e;s++){const e=(0,h.getShift)(t.direction,s),n={x:i.x+e.x,y:i.y+e.y};if(!this.isEmptyAt(n))return{tile:this.getAt(n),position:n}}return null}filterDirections(t,e){return e.filter((e=>{const i=(0,h.getShift)(e),s=t.getCursor(),n={x:s.x+i.x,y:s.y+i.y};return this.isEmptyAt(n)}))}variability(t){if(!this.seed)return(0,h.randomChance)(t);const e=this.seed[this.gauge%this.seed.length];return this.gauge++,e/1e3>=1-t}}},465:function(t,e,i){var s=this&&this.__createBinding||(Object.create?function(t,e,i,s){void 0===s&&(s=i);var n=Object.getOwnPropertyDescriptor(e,i);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,s,n)}:function(t,e,i,s){void 0===s&&(s=i),t[s]=e[i]}),n=this&&this.__exportStar||function(t,e){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(e,i)||s(e,t,i)};Object.defineProperty(e,"__esModule",{value:!0}),n(i(863),e),n(i(895),e),n(i(792),e),n(i(82),e),n(i(309),e)},792:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;const s=i(82),n=i(309);e.Node=class{constructor(t,e){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=e}addOutputPath(t){const e=new s.Path(this,t);return this.outputPaths.push(e),e}removeOutputPath(t){const e=this.outputPaths.indexOf(t);-1!==e&&this.outputPaths.splice(e,1)}getOutputPaths(){return this.outputPaths}addInputPath(t){const e=t.getNodeEnd();e&&e.removeInputPath(t),this.inputPaths.push(t)}removeInputPath(t){const e=this.inputPaths.indexOf(t);-1!==e&&this.inputPaths.splice(e,1)}getInputPaths(){return this.inputPaths}getAllPaths(){return this.outputPaths.concat(this.inputPaths)}getType(){const t=this.outputPaths.length+this.inputPaths.length;return 2===t?n.NodeType.TURN:1===t?n.NodeType.END:n.NodeType.CROSS}}},82:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Path=void 0;const s=i(679),n=i(895);e.Path=class{constructor(t,e){this.buildings=[],this.nodeEnd=null,this.direction=e,this.nodeBeg=t,this.cursor=t.position}getBuildings(){return this.buildings}getPositions(){var t,e;return{beg:this.nodeBeg.position,end:null!==(e=null===(t=this.nodeEnd)||void 0===t?void 0:t.position)&&void 0!==e?e:this.cursor}}isCompleted(){return Boolean(this.nodeEnd)}getNextCursor(){const t=(0,s.getShift)(this.direction);return{x:this.cursor.x+t.x,y:this.cursor.y+t.y}}getCursor(){return this.cursor}setCursor(t){this.cursor=t}getNodeBeg(){return this.nodeBeg}getNodeEnd(){return this.nodeEnd}setNodeEnd(t){t.addInputPath(this),this.nodeEnd=t,this.cursor=t.position}addBuilding(t){const e=new n.Building(this,t);return this.buildings.push(e),e}getLength(){const t=this.getPositions();return Math.hypot(t.beg.x-t.end.x,t.beg.y-t.end.y)}remove(){this.nodeBeg.removeOutputPath(this),this.nodeEnd&&this.nodeEnd.removeInputPath(this)}each(t){const e=(0,s.getShift)(this.direction),i=this.getLength(),n=Object.assign({},this.nodeBeg.position);for(let s=0;s<=i;s++)t(n),n.x+=e.x,n.y+=e.y}}},309:(t,e)=>{var i,s;Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=e.CityGenerationMode=void 0,function(t){t.RUNTIME="RUNTIME",t.SEED="SEED"}(i||(e.CityGenerationMode=i={})),function(t){t.TURN="TURN",t.CROSS="CROSS",t.END="END"}(s||(e.NodeType=s={}))},679:(t,e)=>{function i(t){return t*(Math.PI/180)}function s(t){return[(t+90)%360,(t-90)%360]}Object.defineProperty(e,"__esModule",{value:!0}),e.between=e.getShift=e.forkDirection=e.turnDirection=e.degToRad=e.generateSeed=e.randomRange=e.randomItem=e.randomChance=void 0,e.randomChance=function(t){return Math.random()>1-t},e.randomItem=function(t){return t[Math.floor(Math.random()*t.length)]},e.randomRange=function(t,e){return Math.floor(t+Math.random()*(e-t+1))},e.generateSeed=function(t=32){const e=[];for(let i=0;i=e[0]&&t<=e[1]}}},e={},i=function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s].call(r.exports,r,r.exports,i),r.exports}(465);module.exports=i})(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 968396c..de43ab8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gen-city", - "version": "1.3.1", + "version": "1.3.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gen-city", - "version": "1.3.1", + "version": "1.3.2", "license": "MIT", "devDependencies": { "eslint": "8.36.0", diff --git a/package.json b/package.json index e0dbd88..2bbf3be 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gen-city", "description": "Procedural generation city", - "version": "1.3.1", + "version": "1.3.2", "keywords": [ "map", "generation",