From f1c9d9ef07d288f53b527039d99e514642461ffe Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 13 Jan 2024 12:19:17 -0500 Subject: [PATCH] Randomize lobe types --- island/generators.ts | 3 +++ island/grid.ts | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/island/generators.ts b/island/generators.ts index b8d6a31..baf7495 100644 --- a/island/generators.ts +++ b/island/generators.ts @@ -72,3 +72,6 @@ export const ERODED_LOBE: LobeGeneratorConstructor = return true; }; + +export const BIG_ISLANDS = [BIG_MOUNTAIN]; +export const ALL_ISLANDS = [BIG_MOUNTAIN, BEACH_LOBE, FOREST_LOBE, ERODED_LOBE]; diff --git a/island/grid.ts b/island/grid.ts index 50e662b..da3001c 100644 --- a/island/grid.ts +++ b/island/grid.ts @@ -1,11 +1,6 @@ import { Prng, mulberry32 } from "../lib/prng"; -import { BEACH, ICECAP, LIGHT_FOREST, MOUNTAIN, WATER } from "./data"; -import { - BEACH_LOBE, - BIG_MOUNTAIN, - FOREST_LOBE, - LobeGenerator, -} from "./generators"; +import { BEACH, ICECAP } from "./data"; +import { ALL_ISLANDS, BIG_ISLANDS, LobeGenerator } from "./generators"; export class IslandGrid { data: number[]; @@ -19,9 +14,9 @@ export class IslandGrid { this.data = Array(width * height).fill(0); this.rng = mulberry32(seed); - this.generators.push(BIG_MOUNTAIN(this, this.data.length >> 1)); + this.generators.push(this.choose(BIG_ISLANDS)(this, this.data.length >> 1)); this.generators.push( - FOREST_LOBE( + this.choose(ALL_ISLANDS)( this, this.xy( (width >> 1) + (this.rng() % 48) - 24, @@ -30,7 +25,7 @@ export class IslandGrid { ) ); this.generators.push( - BEACH_LOBE( + this.choose(ALL_ISLANDS)( this, this.xy( (width >> 1) + (this.rng() % 48) - 24, @@ -119,9 +114,16 @@ export class IslandGrid { } } + public choose(list: T[]) { + if (list.length == 0) { + throw new Error("Picking from empty list"); + } + return list[this.rng() % list.length]; + } + public dropWithin(tiles: number[]) { if (tiles.length > 0) { - this.drop(tiles[this.rng() % tiles.length]); + this.drop(this.choose(tiles)); } }