diff --git a/README.md b/README.md index 28f264e..32818dc 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,11 @@ const nodeEnd = path.getNodeEnd(): Node const length = path.getLength(): number ``` +#### Each path positions +```ts +path.each(callback: (position: Position) => void) +``` + #### Get path buildings ```ts const buildings = path.getBuildings(): Building[] @@ -176,6 +181,11 @@ const width: number = building.width const height: number = building.height ``` +#### Each building positions +```ts +building.each(callback: (position: Position) => void) +``` + . ## Example diff --git a/dist/building.d.ts b/dist/building.d.ts index 11cc9c3..e65edde 100644 --- a/dist/building.d.ts +++ b/dist/building.d.ts @@ -5,4 +5,5 @@ export declare class Building { readonly width: number; readonly height: number; constructor(vertices: Position[]); + each(callback: (position: Position) => void): void; } diff --git a/dist/index.js b/dist/index.js index 999f6c2..4b4356b 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){if(4!==t.length)throw Error("Invalid building vertices");this.vertices=t;const e=this.vertices.map((t=>t.x)),i=this.vertices.map((t=>t.y));this.position={x:Math.min(...e),y:Math.min(...i)},this.width=Math.max(...e)-this.position.x+1,this.height=Math.max(...i)-this.position.y+1}}},863:function(t,e,i){var n=this&&this.__awaiter||function(t,e,i,n){return new(i||(i=Promise))((function(s,r){function o(t){try{a(n.next(t))}catch(t){r(t)}}function h(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,h)}a((n=n.apply(t,e||[])).next())}))};Object.defineProperty(e,"__esModule",{value:!0}),e.City=void 0;const s=i(82),r=i(792),o=i(309),h=i(679),a=i(895);e.City=class{constructor({width:t,height:e}){this.seed=null,this.matrix=[],this.nodes=[],this.gauge=1,this.params={},this.width=t,this.height=e}getMatrix(){return this.matrix}getSeed(){return this.seed}getAllBuildings(){return this.getAllPaths().map((t=>t.getBuildings())).flat()}getBuildingAt(t){const e=this.getAt(t);return e instanceof a.Building?e:null}getAllNodes(){return this.nodes}getNodeAt(t){const e=this.getAt(t);return e instanceof r.Node?e:null}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}getPathAt(t){const e=this.getAt(t);return e instanceof s.Path?e:null}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)}generate(t={}){var e;return n(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:0,buildingMinSize:3,buildingMaxSize:6,buildingMinSpace:1,buildingMaxSpace:3,probabilityIntersection:.1,probabilityTurn:.05,probabilityStreetEnd:.001},t),this.params.mode===o.CityGenerationMode.SEED&&(this.seed=null!==(e=this.params.seed)&&void 0!==e?e:(0,h.generateSeed)());const i=this.addNode(this.params.startPosition);this.markAt(this.params.startPosition,i),this.params.startDirections.forEach((t=>{i.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 s.Path&&(e=this.splitPath(i.tile,i.position)),void(e&&t.setNodeEnd(e))}t.setCursor(e),this.markAt(t.getCursor(),t);const n=this.params.streetMinLength;t.getLength()>n&&!this.getCross(t,n)&&(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,n=t.getNodeBeg().position.y+e.y;(0,h.turnDirection)(t.direction).forEach((e=>{let s=0;for(;t.getLength()>s;){const r=(0,h.getShift)(t.direction,s),o=(0,h.getShift)(e),a={x:i+r.x+o.x,y:n+r.y+o.y},u=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(s+u[0]>t.getLength())break;this.processingBuilding(t,a,u,[t.direction,e]);const d=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);s+=u[0]+d}}))}))}processingBuilding(t,e,i,n){const s=[],r=[];for(let t=0;t{this.markAt(t,o)}))}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),n=i.addOutputPath(t.direction);this.markAt(e,i);const r=t.getNodeEnd();return r?n.setNodeEnd(r):n.setCursor(t.getCursor()),n.each((t=>{this.getAt(t)instanceof s.Path&&this.markAt(t,n)})),t.setNodeEnd(i),i}getCross(t,e=1){const i=t.getCursor();for(let n=1;n<=e;n++){const e=(0,h.getShift)(t.direction,n),s={x:i.x+e.x,y:i.y+e.y};if(!this.isEmptyAt(s))return{tile:this.getAt(s),position:s}}return null}filterDirections(t,e){return e.filter((e=>{const i=(0,h.getShift)(e),n=t.getCursor(),s={x:n.x+i.x,y:n.y+i.y};return this.isEmptyAt(s)}))}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 n=this&&this.__createBinding||(Object.create?function(t,e,i,n){void 0===n&&(n=i);var s=Object.getOwnPropertyDescriptor(e,i);s&&!("get"in s?!e.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,n,s)}:function(t,e,i,n){void 0===n&&(n=i),t[n]=e[i]}),s=this&&this.__exportStar||function(t,e){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(e,i)||n(e,t,i)};Object.defineProperty(e,"__esModule",{value:!0}),s(i(863),e),s(i(895),e),s(i(792),e),s(i(82),e),s(i(309),e)},792:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;const n=i(82),s=i(309);e.Node=class{constructor(t,e){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=e}addOutputPath(t){const e=new n.Path(this,t);return this.outputPaths.push(e),e}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?s.NodeType.TURN:1===t?s.NodeType.END:s.NodeType.CROSS}}},82:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Path=void 0;const n=i(679),s=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,n.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 s.Building(t);return this.buildings.push(e),e}getLength(){return Math.hypot(this.nodeBeg.position.x-this.cursor.x,this.nodeBeg.position.y-this.cursor.y)}each(t){const e=(0,n.getShift)(this.direction),i=this.getLength(),s=Object.assign({},this.nodeBeg.position);for(let n=0;n{var i,n;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"}(n||(e.NodeType=n={}))},679:(t,e)=>{function i(t){return t*(Math.PI/180)}function n(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(n){var s=e[n];if(void 0!==s)return s.exports;var r=e[n]={exports:{}};return t[n].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,i)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.Building=void 0,i.Building=class{constructor(t){if(4!==t.length)throw Error("Invalid building vertices");this.vertices=t;const i=this.vertices.map((t=>t.x)),e=this.vertices.map((t=>t.y));this.position={x:Math.min(...i),y:Math.min(...e)},this.width=Math.max(...i)-this.position.x+1,this.height=Math.max(...e)-this.position.y+1}each(t){for(let i=0;it.getBuildings())).flat()}getBuildingAt(t){const i=this.getAt(t);return i instanceof a.Building?i:null}getAllNodes(){return this.nodes}getNodeAt(t){const i=this.getAt(t);return i instanceof r.Node?i:null}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}getPathAt(t){const i=this.getAt(t);return i instanceof s.Path?i:null}getAt(t){var i,e;return null===(e=null===(i=this.matrix)||void 0===i?void 0:i[t.y])||void 0===e?void 0:e[t.x]}markAt(t,i){this.matrix[t.y][t.x]=i}isEmptyAt(t){return null===this.getAt(t)}generate(t={}){var i;return n(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=null!==(i=this.params.seed)&&void 0!==i?i:(0,h.generateSeed)());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 i=this.getAllPaths();for(let t=0,e=i.length;tt.isCompleted()))||t()};t()}processingPath(t){if(t.isCompleted())return;const i=t.getNextCursor();if(void 0===this.getAt(i)||this.variability(this.params.probabilityStreetEnd))return void this.closePath(t);const e=this.getCross(t);if(null==e?void 0:e.tile){let i=null;return e.tile instanceof r.Node?i=e.tile:e.tile instanceof s.Path&&(i=this.splitPath(e.tile,e.position)),void(i&&t.setNodeEnd(i))}t.setCursor(i),this.markAt(t.getCursor(),t);const n=this.params.streetMinLength;t.getLength()>n&&!this.getCross(t,n)&&(this.variability(this.params.probabilityIntersection)?this.forkPath(t):this.variability(this.params.probabilityTurn)&&this.turnPath(t))}generateBuildings(){this.getAllPaths().forEach((t=>{const i=(0,h.getShift)(t.direction),e=t.getNodeBeg().position.x+i.x,n=t.getNodeBeg().position.y+i.y;(0,h.turnDirection)(t.direction).forEach((i=>{let s=0;for(;t.getLength()>s;){const r=(0,h.getShift)(t.direction,s),o=(0,h.getShift)(i),a={x:e+r.x+o.x,y:n+r.y+o.y},u=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(s+u[0]>t.getLength())break;this.processingBuilding(t,a,u,[t.direction,i]);const d=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);s+=u[0]+d}}))}))}processingBuilding(t,i,e,n){const s=[],r=[];for(let t=0;t{this.markAt(t,o)}))}addNode(t){const i=new r.Node(this.nodes.length,t);return this.nodes.push(i),i}closePath(t){const i=t.getCursor(),e=this.addNode(i);return t.setNodeEnd(e),this.markAt(i,e),e}forkPath(t){let i=(0,h.forkDirection)(t.direction).sort((()=>this.variability(.5)?1:-1));if(i=this.filterDirections(t,i),0===i.length||1===i.length&&i[0]===t.direction)return;const e=this.closePath(t);for(let t=0;tthis.variability(.5)?1:-1));if(i=this.filterDirections(t,i),0===i.length)return;const e=this.closePath(t);e.addOutputPath(i[0]),this.markAt(e.position,e)}splitPath(t,i){const e=this.addNode(i),n=e.addOutputPath(t.direction);this.markAt(i,e);const r=t.getNodeEnd();return r?n.setNodeEnd(r):n.setCursor(t.getCursor()),n.each((t=>{this.getAt(t)instanceof s.Path&&this.markAt(t,n)})),t.setNodeEnd(e),e}getCross(t,i=1){const e=t.getCursor();for(let n=1;n<=i;n++){const i=(0,h.getShift)(t.direction,n),s={x:e.x+i.x,y:e.y+i.y};if(!this.isEmptyAt(s))return{tile:this.getAt(s),position:s}}return null}filterDirections(t,i){return i.filter((i=>{const e=(0,h.getShift)(i),n=t.getCursor(),s={x:n.x+e.x,y:n.y+e.y};return this.isEmptyAt(s)}))}variability(t){if(!this.seed)return(0,h.randomChance)(t);const i=this.seed[this.gauge%this.seed.length];return this.gauge++,i/1e3>=1-t}}},465:function(t,i,e){var n=this&&this.__createBinding||(Object.create?function(t,i,e,n){void 0===n&&(n=e);var s=Object.getOwnPropertyDescriptor(i,e);s&&!("get"in s?!i.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return i[e]}}),Object.defineProperty(t,n,s)}:function(t,i,e,n){void 0===n&&(n=e),t[n]=i[e]}),s=this&&this.__exportStar||function(t,i){for(var e in t)"default"===e||Object.prototype.hasOwnProperty.call(i,e)||n(i,t,e)};Object.defineProperty(i,"__esModule",{value:!0}),s(e(863),i),s(e(895),i),s(e(792),i),s(e(82),i),s(e(309),i)},792:(t,i,e)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.Node=void 0;const n=e(82),s=e(309);i.Node=class{constructor(t,i){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=i}addOutputPath(t){const i=new n.Path(this,t);return this.outputPaths.push(i),i}getOutputPaths(){return this.outputPaths}addInputPath(t){const i=t.getNodeEnd();i&&i.removeInputPath(t),this.inputPaths.push(t)}removeInputPath(t){const i=this.inputPaths.indexOf(t);-1!==i&&this.inputPaths.splice(i,1)}getInputPaths(){return this.inputPaths}getAllPaths(){return this.outputPaths.concat(this.inputPaths)}getType(){const t=this.outputPaths.length+this.inputPaths.length;return 2===t?s.NodeType.TURN:1===t?s.NodeType.END:s.NodeType.CROSS}}},82:(t,i,e)=>{Object.defineProperty(i,"__esModule",{value:!0}),i.Path=void 0;const n=e(679),s=e(895);i.Path=class{constructor(t,i){this.buildings=[],this.nodeEnd=null,this.direction=i,this.nodeBeg=t,this.cursor=t.position}getBuildings(){return this.buildings}getPositions(){var t,i;return{beg:this.nodeBeg.position,end:null!==(i=null===(t=this.nodeEnd)||void 0===t?void 0:t.position)&&void 0!==i?i:this.cursor}}isCompleted(){return Boolean(this.nodeEnd)}getNextCursor(){const t=(0,n.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 i=new s.Building(t);return this.buildings.push(i),i}getLength(){return Math.hypot(this.nodeBeg.position.x-this.cursor.x,this.nodeBeg.position.y-this.cursor.y)}each(t){const i=(0,n.getShift)(this.direction),e=this.getLength(),s=Object.assign({},this.nodeBeg.position);for(let n=0;n{var e,n;Object.defineProperty(i,"__esModule",{value:!0}),i.NodeType=i.CityGenerationMode=void 0,function(t){t.RUNTIME="RUNTIME",t.SEED="SEED"}(e||(i.CityGenerationMode=e={})),function(t){t.TURN="TURN",t.CROSS="CROSS",t.END="END"}(n||(i.NodeType=n={}))},679:(t,i)=>{function e(t){return t*(Math.PI/180)}function n(t){return[(t+90)%360,(t-90)%360]}Object.defineProperty(i,"__esModule",{value:!0}),i.between=i.getShift=i.forkDirection=i.turnDirection=i.degToRad=i.generateSeed=i.randomRange=i.randomItem=i.randomChance=void 0,i.randomChance=function(t){return Math.random()>1-t},i.randomItem=function(t){return t[Math.floor(Math.random()*t.length)]},i.randomRange=function(t,i){return Math.floor(t+Math.random()*(i-t+1))},i.generateSeed=function(t=32){const i=[];for(let e=0;e=i[0]&&t<=i[1]}}},i={},e=function e(n){var s=i[n];if(void 0!==s)return s.exports;var r=i[n]={exports:{}};return t[n].call(r.exports,r,r.exports,e),r.exports}(465);module.exports=e})(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 3464a46..413abed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gen-city", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gen-city", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT", "devDependencies": { "eslint": "8.36.0", diff --git a/package.json b/package.json index 4a2651d..8b1b6bc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gen-city", "description": "Procedural generation city", - "version": "1.0.1", + "version": "1.1.0", "keywords": [ "map", "generation", diff --git a/src/building.ts b/src/building.ts index d1131ae..c5b0ea7 100644 --- a/src/building.ts +++ b/src/building.ts @@ -28,4 +28,15 @@ export class Building { this.width = Math.max(...xs) - this.position.x + 1; this.height = Math.max(...ys) - this.position.y + 1; } + + public each(callback: (position: Position) => void) { + for (let x = 0; x < this.width; x++) { + for (let y = 0; y < this.height; y++) { + callback({ + x: this.position.x + x, + y: this.position.y + y, + }); + } + } + } } diff --git a/src/city.ts b/src/city.ts index a696ed4..22f1da4 100644 --- a/src/city.ts +++ b/src/city.ts @@ -95,7 +95,7 @@ export class City { y: Math.round(this.height / 2), }, startDirections: [0, 90, 180, 270], - streetMinLength: 0, + streetMinLength: 10, buildingMinSize: 3, buildingMaxSize: 6, buildingMinSpace: 1,