mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 02:01:22 +01:00
refactor: markupPackFeatures
This commit is contained in:
parent
87d8c1024d
commit
f82fcbae0f
8 changed files with 271 additions and 178 deletions
|
|
@ -3,10 +3,12 @@ import {TIME} from "config/logging";
|
|||
import {INT8_MAX} from "constants";
|
||||
// @ts-expect-error js module
|
||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||
import {createTypedArray} from "utils/arrayUtils";
|
||||
import {dist2} from "utils/functionUtils";
|
||||
|
||||
const {UNMARKED, LAND_COAST, WATER_COAST} = DISTANCE_FIELD;
|
||||
const {UNMARKED, LAND_COAST, WATER_COAST, LANDLOCKED, DEEPER_WATER} = DISTANCE_FIELD;
|
||||
|
||||
// define features (grid.features: ocean, lakes, islands) and calculate distance field (cells.t)
|
||||
// define features (oceans, lakes, islands)
|
||||
export function markupGridFeatures(grid: IGridWithHeights) {
|
||||
TIME && console.time("markupGridFeatures");
|
||||
Math.random = aleaPRNG(seed); // get the same result on heightmap edit in Erase mode
|
||||
|
|
@ -20,7 +22,7 @@ export function markupGridFeatures(grid: IGridWithHeights) {
|
|||
const n = cells.i.length;
|
||||
|
||||
const featureIds = new Uint16Array(n); // starts from 1
|
||||
let distanceField = new Int8Array(n);
|
||||
const distanceField = new Int8Array(n);
|
||||
const features: TGridFeatures = [0];
|
||||
|
||||
const queue = [0];
|
||||
|
|
@ -55,33 +57,166 @@ export function markupGridFeatures(grid: IGridWithHeights) {
|
|||
}
|
||||
|
||||
// markup deep ocean cells
|
||||
distanceField = markup({graph: grid, distanceField, start: -2, increment: -1, limit: -10});
|
||||
const dfOceanMarked = markup({
|
||||
distanceField,
|
||||
neighbors: grid.cells.c,
|
||||
start: DEEPER_WATER,
|
||||
increment: -1,
|
||||
limit: -10
|
||||
});
|
||||
|
||||
TIME && console.timeEnd("markupGridFeatures");
|
||||
return {featureIds, distanceField, features};
|
||||
return {featureIds, distanceField: dfOceanMarked, features};
|
||||
}
|
||||
|
||||
// define features (oceans, lakes, islands) add related details
|
||||
export function markupPackFeatures(grid: IGrid, cells: Pick<IPack["cells"], "c" | "b" | "p" | "h">) {
|
||||
TIME && console.time("markupPackFeatures");
|
||||
|
||||
const packCellsNumber = cells.h.length;
|
||||
const gridCellsNumber = grid.cells.h.length;
|
||||
|
||||
const features: TPackFeatures = [0];
|
||||
const featureIds = new Uint16Array(packCellsNumber); // ids of features, starts from 1
|
||||
const distanceField = new Int8Array(packCellsNumber); // distance from coast; 1 = land along coast; -1 = water along coast
|
||||
const haven = createTypedArray({maxValue: packCellsNumber, length: packCellsNumber}); // haven (opposite water cell)
|
||||
const harbor = new Uint8Array(packCellsNumber); // harbor (number of adjacent water cells)
|
||||
|
||||
const defineHaven = (cellId: number) => {
|
||||
const waterCells = cells.c[cellId].filter(c => cells.h[c] < MIN_LAND_HEIGHT);
|
||||
const distances = waterCells.map(c => dist2(cells.p[cellId], cells.p[c]));
|
||||
const closest = distances.indexOf(Math.min.apply(Math, distances));
|
||||
|
||||
haven[cellId] = waterCells[closest];
|
||||
harbor[cellId] = waterCells.length;
|
||||
};
|
||||
|
||||
const OCEAN_MIN_SIZE = gridCellsNumber / 25;
|
||||
const SEA_MIN_SIZE = gridCellsNumber / 1000;
|
||||
const CONTINENT_MIN_SIZE = gridCellsNumber / 10;
|
||||
const ISLAND_MIN_SIZE = gridCellsNumber / 1000;
|
||||
|
||||
function defineOceanGroup(cellsNumber: number) {
|
||||
if (cellsNumber > OCEAN_MIN_SIZE) return "ocean";
|
||||
if (cellsNumber > SEA_MIN_SIZE) return "sea";
|
||||
return "gulf";
|
||||
}
|
||||
|
||||
function defineIslandGroup(firstCell: number, cellsNumber: number) {
|
||||
const prevCellFeature = features[featureIds[firstCell - 1]];
|
||||
|
||||
if (prevCellFeature && prevCellFeature.type === "lake") return "lake_island";
|
||||
if (cellsNumber > CONTINENT_MIN_SIZE) return "continent";
|
||||
if (cellsNumber > ISLAND_MIN_SIZE) return "island";
|
||||
return "isle";
|
||||
}
|
||||
|
||||
function addIsland(featureId: number, border: boolean, firstCell: number, cells: number) {
|
||||
const group = defineIslandGroup(firstCell, cells);
|
||||
const feature: IPackFeatureIsland = {i: featureId, type: "island", group, land: true, border, cells, firstCell};
|
||||
features.push(feature);
|
||||
}
|
||||
|
||||
function addOcean(featureId: number, firstCell: number, cells: number) {
|
||||
const group = defineOceanGroup(cells);
|
||||
const feature: IPackFeatureOcean = {
|
||||
i: featureId,
|
||||
type: "ocean",
|
||||
group,
|
||||
land: false,
|
||||
border: false,
|
||||
cells,
|
||||
firstCell
|
||||
};
|
||||
features.push(feature);
|
||||
}
|
||||
|
||||
function addLake(featureId: number, firstCell: number, cells: number) {
|
||||
const group = "freshwater"; // temp, to be defined later
|
||||
const name = ""; // temp, to be defined later
|
||||
const feature: IPackFeatureLake = {
|
||||
i: featureId,
|
||||
type: "lake",
|
||||
group,
|
||||
name,
|
||||
land: false,
|
||||
border: false,
|
||||
cells,
|
||||
firstCell
|
||||
};
|
||||
features.push(feature);
|
||||
}
|
||||
|
||||
const queue = [0];
|
||||
for (let featureId = 1; queue[0] !== -1; featureId++) {
|
||||
const firstCell = queue[0];
|
||||
featureIds[firstCell] = featureId; // assign feature number
|
||||
|
||||
const land = cells.h[firstCell] >= MIN_LAND_HEIGHT;
|
||||
let border = false; // true if feature touches map border
|
||||
let cellNumber = 1; // count cells in a feature
|
||||
|
||||
while (queue.length) {
|
||||
const cellId = queue.pop()!;
|
||||
if (cells.b[cellId]) border = true;
|
||||
|
||||
for (const neighborId of cells.c[cellId]) {
|
||||
const isNeibLand = cells.h[neighborId] >= MIN_LAND_HEIGHT;
|
||||
|
||||
if (land && !isNeibLand) {
|
||||
distanceField[cellId] = LAND_COAST;
|
||||
distanceField[neighborId] = WATER_COAST;
|
||||
if (!haven[cellId]) defineHaven(cellId);
|
||||
} else if (land && isNeibLand) {
|
||||
if (distanceField[neighborId] === UNMARKED && distanceField[cellId] === LAND_COAST)
|
||||
distanceField[neighborId] = LANDLOCKED;
|
||||
else if (distanceField[cellId] === UNMARKED && distanceField[neighborId] === LAND_COAST)
|
||||
distanceField[cellId] = LANDLOCKED;
|
||||
}
|
||||
|
||||
if (!featureIds[neighborId] && land === isNeibLand) {
|
||||
queue.push(neighborId);
|
||||
featureIds[neighborId] = featureId;
|
||||
cellNumber++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// const vertices = detectFeatureVertices(cells, firstCell);
|
||||
|
||||
if (land) addIsland(featureId, border, firstCell, cellNumber);
|
||||
else if (border) addOcean(featureId, firstCell, cellNumber);
|
||||
else addLake(featureId, firstCell, cellNumber);
|
||||
|
||||
queue[0] = featureIds.findIndex(f => f === UNMARKED); // find unmarked cell
|
||||
}
|
||||
|
||||
// markup pack land cells
|
||||
const dfLandMarked = markup({distanceField, neighbors: cells.c, start: LANDLOCKED + 1, increment: 1});
|
||||
|
||||
TIME && console.timeEnd("markupPackFeatures");
|
||||
|
||||
return {features, featureIds, distanceField: dfLandMarked, haven, harbor};
|
||||
}
|
||||
|
||||
// calculate distance to coast for every cell
|
||||
function markup({
|
||||
graph,
|
||||
distanceField,
|
||||
neighbors,
|
||||
start,
|
||||
increment,
|
||||
limit
|
||||
limit = INT8_MAX
|
||||
}: {
|
||||
graph: IGraph;
|
||||
distanceField: Int8Array;
|
||||
neighbors: number[][];
|
||||
start: number;
|
||||
increment: number;
|
||||
limit: number;
|
||||
limit?: number;
|
||||
}) {
|
||||
const cellsLength = graph.cells.i.length;
|
||||
const neighbors = graph.cells.c;
|
||||
|
||||
for (let distance = start, marked = Infinity; marked > 0 && distance > limit; distance += increment) {
|
||||
marked = 0;
|
||||
const prevDistance = distance - increment;
|
||||
for (let cellId = 0; cellId < cellsLength; cellId++) {
|
||||
for (let cellId = 0; cellId < neighbors.length; cellId++) {
|
||||
if (distanceField[cellId] !== prevDistance) continue;
|
||||
|
||||
for (const neighborId of neighbors[cellId]) {
|
||||
|
|
@ -95,94 +230,24 @@ function markup({
|
|||
return distanceField;
|
||||
}
|
||||
|
||||
// Re-mark features (ocean, lakes, islands)
|
||||
export function reMarkFeatures(pack: IPackBase, grid: IGrid) {
|
||||
TIME && console.time("reMarkFeatures");
|
||||
const {cells} = pack;
|
||||
const features: TPackFeatures = [0];
|
||||
const n = cells.i.length;
|
||||
|
||||
cells.f = new Uint16Array(n); // cell feature number
|
||||
cells.t = new Int8Array(n); // cell type: 1 = land along coast; -1 = water along coast;
|
||||
cells.haven = n < 65535 ? new Uint16Array(n) : new Uint32Array(n); // cell haven (opposite water cell);
|
||||
cells.harbor = new Uint8Array(n); // cell harbor (number of adjacent water cells);
|
||||
|
||||
const defineHaven = (i: number) => {
|
||||
const water = cells.c[i].filter(c => cells.h[c] < 20);
|
||||
const dist2 = water.map(c => (cells.p[i][0] - cells.p[c][0]) ** 2 + (cells.p[i][1] - cells.p[c][1]) ** 2);
|
||||
const closest = water[dist2.indexOf(Math.min.apply(Math, dist2))];
|
||||
|
||||
cells.haven[i] = closest;
|
||||
cells.harbor[i] = water.length;
|
||||
};
|
||||
|
||||
for (let i = 1, queue = [0]; queue[0] !== -1; i++) {
|
||||
const start = queue[0]; // first cell
|
||||
cells.f[start] = i; // assign feature number
|
||||
const land = cells.h[start] >= 20;
|
||||
let border = false; // true if feature touches map border
|
||||
let cellNumber = 1; // to count cells number in a feature
|
||||
|
||||
while (queue.length) {
|
||||
const firstCellId = queue.pop()!;
|
||||
|
||||
if (cells.b[firstCellId]) border = true;
|
||||
cells.c[firstCellId].forEach(function (e) {
|
||||
const eLand = cells.h[e] >= 20;
|
||||
if (land && !eLand) {
|
||||
cells.t[firstCellId] = 1;
|
||||
cells.t[e] = -1;
|
||||
if (!cells.haven[firstCellId]) defineHaven(firstCellId);
|
||||
} else if (land && eLand) {
|
||||
if (!cells.t[e] && cells.t[firstCellId] === 1) cells.t[e] = 2;
|
||||
else if (!cells.t[firstCellId] && cells.t[e] === 1) cells.t[firstCellId] = 2;
|
||||
}
|
||||
if (!cells.f[e] && land === eLand) {
|
||||
queue.push(e);
|
||||
cells.f[e] = i;
|
||||
cellNumber++;
|
||||
}
|
||||
});
|
||||
// connect vertices to chain
|
||||
function connectVertices(start, t) {
|
||||
const chain = []; // vertices chain to form a path
|
||||
for (let i = 0, current = start; i === 0 || (current !== start && i < 50000); i++) {
|
||||
const prev = chain[chain.length - 1]; // previous vertex in chain
|
||||
chain.push(current); // add current vertex to sequence
|
||||
const c = vertices.c[current]; // cells adjacent to vertex
|
||||
const v = vertices.v[current]; // neighboring vertices
|
||||
const c0 = c[0] >= n || cells.t[c[0]] === t;
|
||||
const c1 = c[1] >= n || cells.t[c[1]] === t;
|
||||
const c2 = c[2] >= n || cells.t[c[2]] === t;
|
||||
if (v[0] !== prev && c0 !== c1) current = v[0];
|
||||
else if (v[1] !== prev && c1 !== c2) current = v[1];
|
||||
else if (v[2] !== prev && c0 !== c2) current = v[2];
|
||||
if (current === chain[chain.length - 1]) {
|
||||
ERROR && console.error("Next vertex is not found");
|
||||
break;
|
||||
}
|
||||
|
||||
if (land) {
|
||||
const group = defineIslandGroup(start, cellNumber);
|
||||
const feature: IPackFeatureIsland = {i, type: "island", group, land, border, cells: cellNumber, firstCell: start};
|
||||
features.push(feature);
|
||||
} else if (border) {
|
||||
const group = defineOceanGroup(cellNumber);
|
||||
const feature: IPackFeatureOcean = {i, type: "ocean", group, land, border, cells: cellNumber, firstCell: start};
|
||||
features.push(feature);
|
||||
} else {
|
||||
const group = "freshwater"; // temp, to be defined later
|
||||
const name = ""; // temp, to be defined later
|
||||
const cells = cellNumber;
|
||||
const feature: IPackFeatureLake = {i, type: "lake", group, name, land, border, cells, firstCell: start};
|
||||
features.push(feature);
|
||||
}
|
||||
|
||||
queue[0] = cells.f.findIndex(f => f === UNMARKED); // find unmarked cell
|
||||
}
|
||||
|
||||
// markupPackLand
|
||||
markup({graph: pack, distanceField: cells.t, start: 3, increment: 1, limit: INT8_MAX});
|
||||
|
||||
function defineOceanGroup(number: number) {
|
||||
if (number > grid.cells.i.length / 25) return "ocean";
|
||||
if (number > grid.cells.i.length / 100) return "sea";
|
||||
return "gulf";
|
||||
}
|
||||
|
||||
function defineIslandGroup(cellId: number, number: number) {
|
||||
const prevCellFeature = features[cells.f[cellId - 1]];
|
||||
|
||||
if (cellId && prevCellFeature && prevCellFeature.type === "lake") return "lake_island";
|
||||
if (number > grid.cells.i.length / 10) return "continent";
|
||||
if (number > grid.cells.i.length / 1000) return "island";
|
||||
return "isle";
|
||||
}
|
||||
|
||||
pack.features = features;
|
||||
|
||||
TIME && console.timeEnd("reMarkFeatures");
|
||||
return chain;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue