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