Ceremonial PR: fix map gen #39
| @@ -112,12 +112,12 @@ export class Point { | |||||||
|     return Math.sqrt(dx * dx + dy * dy); |     return Math.sqrt(dx * dx + dy * dy); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   neighbors() : Point[] { |   neighbors(): Point[] { | ||||||
|     return [ |     return [ | ||||||
|       new Point(this.x, this.y-1), |       new Point(this.x, this.y - 1), | ||||||
|       new Point(this.x-1, this.y), |       new Point(this.x - 1, this.y), | ||||||
|       new Point(this.x, this.y+1), |       new Point(this.x, this.y + 1), | ||||||
|       new Point(this.x+1, this.y), |       new Point(this.x + 1, this.y), | ||||||
|     ]; |     ]; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -273,13 +273,15 @@ export class Grid<T> { | |||||||
|     return new Grid(this.size, (xy) => cbCell(this.get(xy), xy)); |     return new Grid(this.size, (xy) => cbCell(this.get(xy), xy)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   #invalidPosition(position: Point) : boolean { |   #invalidPosition(position: Point): boolean { | ||||||
|     return position.x < 0 || |     return ( | ||||||
|  |       position.x < 0 || | ||||||
|       position.x >= this.size.w || |       position.x >= this.size.w || | ||||||
|       Math.floor(position.x) != position.x || |       Math.floor(position.x) != position.x || | ||||||
|       position.y < 0 || |       position.y < 0 || | ||||||
|       position.y >= this.size.h || |       position.y >= this.size.h || | ||||||
|       Math.floor(position.y) != position.y;; |       Math.floor(position.y) != position.y | ||||||
|  |     ); | ||||||
|   } |   } | ||||||
|   #checkPosition(position: Point) { |   #checkPosition(position: Point) { | ||||||
|     if (this.#invalidPosition(position)) { |     if (this.#invalidPosition(position)) { | ||||||
|   | |||||||
| @@ -103,12 +103,12 @@ class Knife { | |||||||
|               return this.#sealedWalls.get(loc) ? "◘" : "█"; |               return this.#sealedWalls.get(loc) ? "◘" : "█"; | ||||||
|             } |             } | ||||||
|             let r = this.#regions.get(loc); |             let r = this.#regions.get(loc); | ||||||
|             if(typeof r === "number") { |             if (typeof r === "number") { | ||||||
|               const resolved = merged[r]; |               const resolved = merged[r]; | ||||||
|               if (typeof resolved === "number") { |               if (typeof resolved === "number") { | ||||||
|                 r = resolved; |                 r = resolved; | ||||||
|               } else { |               } else { | ||||||
|                 errors.push(`${loc} is region ${r}, not found in merged`) |                 errors.push(`${loc} is region ${r}, not found in merged`); | ||||||
|               } |               } | ||||||
|               if (r < 0) { |               if (r < 0) { | ||||||
|                 return "!"; |                 return "!"; | ||||||
| @@ -139,12 +139,12 @@ class Knife { | |||||||
|               // Hebrew |               // Hebrew | ||||||
|               r -= 7; |               r -= 7; | ||||||
|               if (r < 27) { |               if (r < 27) { | ||||||
|                 return String.fromCodePoint(r+0x5d0); |                 return String.fromCodePoint(r + 0x5d0); | ||||||
|               } |               } | ||||||
|               // give up |               // give up | ||||||
|               return "?"; |               return "?"; | ||||||
|             } |             } | ||||||
|             return ".";  // room without region |             return "."; // room without region | ||||||
|           })(); |           })(); | ||||||
|         } |         } | ||||||
|         out += "\n"; |         out += "\n"; | ||||||
| @@ -569,7 +569,10 @@ function connectRegions(knife: Knife) { | |||||||
|     let sources: number[] = dedup(basicRegions.map((i) => merged[i])); |     let sources: number[] = dedup(basicRegions.map((i) => merged[i])); | ||||||
|     let dest: number | undefined = sources.pop(); |     let dest: number | undefined = sources.pop(); | ||||||
|     if (dest == undefined) { |     if (dest == undefined) { | ||||||
|       throw new BadMapError(`each connector should touch more than one region but ${connector} does not`, knife.map); |       throw new BadMapError( | ||||||
|  |         `each connector should touch more than one region but ${connector} does not`, | ||||||
|  |         knife.map, | ||||||
|  |       ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (Math.random() > EXTRA_CONNECTOR_CHANCE) { |     if (Math.random() > EXTRA_CONNECTOR_CHANCE) { | ||||||
| @@ -735,7 +738,7 @@ function showDebug(grid: LoadedNewMap) { | |||||||
| } | } | ||||||
|  |  | ||||||
| class TryAgainException extends Error {} | class TryAgainException extends Error {} | ||||||
| class BadMapError extends Error{ | class BadMapError extends Error { | ||||||
|   badMap: LoadedNewMap; |   badMap: LoadedNewMap; | ||||||
|  |  | ||||||
|   constructor(msg: string, badMap: LoadedNewMap) { |   constructor(msg: string, badMap: LoadedNewMap) { | ||||||
|   | |||||||
| @@ -108,13 +108,13 @@ export class LoadedNewMap { | |||||||
|  |  | ||||||
|   isConnected(): boolean { |   isConnected(): boolean { | ||||||
|     const size = this.#size; |     const size = this.#size; | ||||||
|     let reached = new Grid<boolean>(size, ()=>false); |     let reached = new Grid<boolean>(size, () => false); | ||||||
|  |  | ||||||
|     // find starting location |     // find starting location | ||||||
|     const found: Point | null = (()=>{ |     const found: Point | null = (() => { | ||||||
|       for(let x = 0; x < size.w; x++) { |       for (let x = 0; x < size.w; x++) { | ||||||
|         for(let y = 0; y < size.w; y++) { |         for (let y = 0; y < size.w; y++) { | ||||||
|           const p = new Point(x, y) |           const p = new Point(x, y); | ||||||
|           if (this.#architecture.get(p) == Architecture.Floor) { |           if (this.#architecture.get(p) == Architecture.Floor) { | ||||||
|             return p; |             return p; | ||||||
|           } |           } | ||||||
| @@ -124,25 +124,31 @@ export class LoadedNewMap { | |||||||
|     })(); |     })(); | ||||||
|     if (found === null) { |     if (found === null) { | ||||||
|       // technically, all open floors on the map are indeed connected |       // technically, all open floors on the map are indeed connected | ||||||
|       return true |       return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     let stack : Point[] = [found]; |     let stack: Point[] = [found]; | ||||||
|     reached.set(found, true); |     reached.set(found, true); | ||||||
|     while (stack.length > 0) { |     while (stack.length > 0) { | ||||||
|       const loc = stack.pop() as Point; |       const loc = stack.pop() as Point; | ||||||
|       for (var p of loc.neighbors()) { |       for (var p of loc.neighbors()) { | ||||||
|         if ((this.#architecture.maybeGet(p) === Architecture.Floor) && !reached.get(p)) { |         if ( | ||||||
|  |           this.#architecture.maybeGet(p) === Architecture.Floor && | ||||||
|  |           !reached.get(p) | ||||||
|  |         ) { | ||||||
|           reached.set(p, true); |           reached.set(p, true); | ||||||
|           stack.push(p); |           stack.push(p); | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     for(let x = 0; x < size.w; x++) { |     for (let x = 0; x < size.w; x++) { | ||||||
|       for(let y = 0; y < size.w; y++) { |       for (let y = 0; y < size.w; y++) { | ||||||
|         const p = new Point(x, y) |         const p = new Point(x, y); | ||||||
|         if (this.#architecture.get(p) == Architecture.Floor && !reached.get(p)) { |         if ( | ||||||
|  |           this.#architecture.get(p) == Architecture.Floor && | ||||||
|  |           !reached.get(p) | ||||||
|  |         ) { | ||||||
|           return false; |           return false; | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user