Ceremonial PR: fix map gen #39
| @@ -99,6 +99,11 @@ export function generateMap(): LoadedNewMap { | ||||
|       if (e instanceof TryAgainException) { | ||||
|         continue; | ||||
|       } | ||||
|       if (e instanceof BadMapError) { | ||||
|         console.log(`Bad map generated: ${e.message}:`); | ||||
|         showDebug(e.badMap); | ||||
|         continue; | ||||
|       } | ||||
|       throw e; | ||||
|     } | ||||
|   } | ||||
| @@ -108,7 +113,7 @@ export function tryGenerateMap(vaultTemplates: VaultTemplate[]): LoadedNewMap { | ||||
|   let width = WIDTH; | ||||
|   let height = HEIGHT; | ||||
|   if (width % 2 == 0 || height % 2 == 0) { | ||||
|     throw "must be odd-sized"; | ||||
|     throw new Error("map bounds must be odd-sized"); | ||||
|   } | ||||
|  | ||||
|   let grid = new LoadedNewMap("generated", new Size(width, height)); | ||||
| @@ -498,7 +503,7 @@ function connectRegions(knife: Knife) { | ||||
|     let sources: number[] = dedup(basicRegions.map((i) => merged[i])); | ||||
|     let dest: number | undefined = sources.pop(); | ||||
|     if (dest == undefined) { | ||||
|       throw "each connector should touch more than one region"; | ||||
|       throw new BadMapError(`each connector should touch more than one region but ${connector} does not`, knife.map); | ||||
|     } | ||||
|  | ||||
|     if (Math.random() > EXTRA_CONNECTOR_CHANCE) { | ||||
| @@ -624,7 +629,7 @@ function decorateRoom(_map: LoadedNewMap, _rect: Rect) {} | ||||
|  | ||||
| function randrange(lo: number, hi: number) { | ||||
|   if (lo >= hi) { | ||||
|     throw `randrange: hi must be >= lo, ${hi}, ${lo}`; | ||||
|     throw new Error(`randrange: hi must be >= lo, ${hi}, ${lo}`); | ||||
|   } | ||||
|  | ||||
|   return lo + Math.floor(Math.random() * (hi - lo)); | ||||
| @@ -658,3 +663,11 @@ function showDebug(grid: LoadedNewMap) { | ||||
| } | ||||
|  | ||||
| class TryAgainException extends Error {} | ||||
| class BadMapError extends Error{ | ||||
|   badMap: LoadedNewMap; | ||||
|  | ||||
|   constructor(msg: string, badMap: LoadedNewMap) { | ||||
|     super(msg); | ||||
|     this.badMap = badMap; | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user