Generate archipelago ring

This commit is contained in:
Tangent 2024-01-13 12:55:17 -05:00
parent aaa165013e
commit b0ed6f9640
3 changed files with 46 additions and 21 deletions

View file

@ -91,10 +91,17 @@ export const ERODED_LOBE: LobeGeneratorConstructor =
return true; return true;
}; };
export const BIG_ISLANDS = [BIG_BEACH, BIG_MOUNTAIN]; /** form low-lying beach with eroded sections */
export const NO_ISLAND: LobeGeneratorConstructor =
(islands: IslandGrid, basePos: number) => () => {
return true;
};
export const BIG_ISLANDS = [BIG_MOUNTAIN, BIG_BEACH];
export const ALL_ISLANDS = [ export const ALL_ISLANDS = [
BIG_MOUNTAIN, BIG_MOUNTAIN,
SMALL_BEACH, SMALL_BEACH,
BIG_BEACH,
FOREST_LOBE, FOREST_LOBE,
ERODED_LOBE, ERODED_LOBE,
]; ];

View file

@ -1,6 +1,11 @@
import { Prng, mulberry32 } from "../lib/prng"; import { Prng, mulberry32 } from "../lib/prng";
import { BEACH, ICECAP } from "./data"; import { BEACH, ICECAP } from "./data";
import { ALL_ISLANDS, BIG_ISLANDS, LobeGenerator } from "./generators"; import {
ALL_ISLANDS,
BIG_ISLANDS,
LobeGenerator,
NO_ISLAND,
} from "./generators";
export class IslandGrid { export class IslandGrid {
data: number[]; data: number[];
@ -14,25 +19,27 @@ export class IslandGrid {
this.data = Array(width * height).fill(0); this.data = Array(width * height).fill(0);
this.rng = mulberry32(seed); this.rng = mulberry32(seed);
this.generators.push(this.choose(BIG_ISLANDS)(this, this.data.length >> 1)); const islandBag = this.shuffle([
this.generators.push( this.choose(BIG_ISLANDS),
this.choose(ALL_ISLANDS)( this.choose(BIG_ISLANDS),
this, this.choose(ALL_ISLANDS),
this.xy( this.choose(ALL_ISLANDS),
(width >> 1) + (this.rng() % 48) - 24, this.choose(ALL_ISLANDS),
(height >> 1) + (this.rng() % 48) - 24 this.choose(ALL_ISLANDS),
) NO_ISLAND,
) NO_ISLAND,
); ]);
this.generators.push(
this.choose(ALL_ISLANDS)( const islandCount = islandBag.length;
this, const spacing = (Math.PI * 2) / islandCount;
this.xy( for (let i = 0; i < islandCount; i++) {
(width >> 1) + (this.rng() % 48) - 24, const y = height / 2 + (Math.sin(spacing * i) * height) / 4;
(height >> 1) + (this.rng() % 48) - 24 const x = width / 2 + (Math.cos(spacing * i) * width) / 4;
)
) console.log("xy", x | 0, y | 0);
);
this.generators.push(islandBag[i](this, this.xy(x | 0, y | 0)));
}
} }
public xy(x: number, y: number): number { public xy(x: number, y: number): number {
@ -121,6 +128,15 @@ export class IslandGrid {
return list[this.rng() % list.length]; return list[this.rng() % list.length];
} }
public shuffle<T>(list: T[]) {
const shuffled = list.slice();
for (let i = 0; i < shuffled.length - 1; i++) {
const swapIndex = (this.rng() % (shuffled.length - i)) + i;
[shuffled[i], shuffled[swapIndex]] = [shuffled[swapIndex], shuffled[i]];
}
return shuffled;
}
public dropWithin(tiles: number[]) { public dropWithin(tiles: number[]) {
if (tiles.length > 0) { if (tiles.length > 0) {
this.drop(this.choose(tiles)); this.drop(this.choose(tiles));

View file

@ -1,5 +1,7 @@
export type Prng = () => number; export type Prng = () => number;
export const UINT_MAX = 0xffffffff;
export function mulberry32(state: number) { export function mulberry32(state: number) {
return function () { return function () {
let t = (state += 0x6d2b79f5); let t = (state += 0x6d2b79f5);