mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: fix markup - ensure all cells have t defined
This commit is contained in:
parent
e427050369
commit
2877f44216
5 changed files with 182 additions and 80 deletions
|
|
@ -271,7 +271,7 @@ window.Routes = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cells.h[neibCellId] >= 20) continue; // ignore land cells
|
if (cells.h[neibCellId] >= 20) continue; // ignore land cells
|
||||||
if (temp[cells.g[neibCellId]] <= -5) continue; // ignore cells with term <= -5
|
if (temp[cells.g[neibCellId]] <= -5) continue; // ignore cells with temp <= -5
|
||||||
|
|
||||||
const dist2 =
|
const dist2 =
|
||||||
(cells.p[neibCellId][1] - cells.p[next][1]) ** 2 + (cells.p[neibCellId][0] - cells.p[next][0]) ** 2;
|
(cells.p[neibCellId][1] - cells.p[next][1]) ** 2 + (cells.p[neibCellId][0] - cells.p[next][0]) ** 2;
|
||||||
|
|
|
||||||
|
|
@ -147,9 +147,18 @@ export function markupPackFeatures(
|
||||||
// markup pack land cells
|
// markup pack land cells
|
||||||
const dfLandMarked = markup({distanceField, neighbors: cells.c, start: LANDLOCKED + 1, increment: 1});
|
const dfLandMarked = markup({distanceField, neighbors: cells.c, start: LANDLOCKED + 1, increment: 1});
|
||||||
|
|
||||||
|
// markup deep ocean cells
|
||||||
|
const dfOceanMarked = markup({
|
||||||
|
distanceField: dfLandMarked,
|
||||||
|
neighbors: cells.c,
|
||||||
|
start: DEEPER_WATER,
|
||||||
|
increment: -1,
|
||||||
|
limit: -10
|
||||||
|
});
|
||||||
|
|
||||||
TIME && console.timeEnd("markupPackFeatures");
|
TIME && console.timeEnd("markupPackFeatures");
|
||||||
|
|
||||||
return {features, featureIds, distanceField: dfLandMarked, haven, harbor};
|
return {features, featureIds, distanceField: dfOceanMarked, haven, harbor};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addFeature({
|
function addFeature({
|
||||||
|
|
@ -291,7 +300,7 @@ function markup({
|
||||||
increment: number;
|
increment: number;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
}) {
|
}) {
|
||||||
for (let distance = start, marked = Infinity; marked > 0 && distance > limit; distance += increment) {
|
for (let distance = start, marked = Infinity; marked > 0 && distance !== limit; distance += increment) {
|
||||||
marked = 0;
|
marked = 0;
|
||||||
const prevDistance = distance - increment;
|
const prevDistance = distance - increment;
|
||||||
for (let cellId = 0; cellId < neighbors.length; cellId++) {
|
for (let cellId = 0; cellId < neighbors.length; cellId++) {
|
||||||
|
|
|
||||||
|
|
@ -4,34 +4,51 @@ import FlatQueue from "flatqueue";
|
||||||
import {TIME} from "config/logging";
|
import {TIME} from "config/logging";
|
||||||
import {ELEVATION, MIN_LAND_HEIGHT, ROUTES} from "config/generation";
|
import {ELEVATION, MIN_LAND_HEIGHT, ROUTES} from "config/generation";
|
||||||
import {dist2} from "utils/functionUtils";
|
import {dist2} from "utils/functionUtils";
|
||||||
|
import {drawLine} from "utils/debugUtils";
|
||||||
|
|
||||||
export function generateRoutes(
|
type TCellsData = Pick<IPack["cells"], "c" | "p" | "g" | "h" | "t" | "haven" | "biome" | "state" | "burg">;
|
||||||
burgs: TBurgs,
|
|
||||||
cells: Pick<IPack["cells"], "c" | "p" | "h" | "biome" | "state" | "burg">
|
export function generateRoutes(burgs: TBurgs, temp: Int8Array, cells: TCellsData) {
|
||||||
) {
|
|
||||||
const cellRoutes = new Uint8Array(cells.h.length);
|
const cellRoutes = new Uint8Array(cells.h.length);
|
||||||
const validBurgs = burgs.filter(burg => burg.i && !(burg as IBurg).removed) as IBurg[];
|
|
||||||
|
const {capitalsByFeature, burgsByFeature, portsByFeature} = sortBurgsByFeature(burgs);
|
||||||
const connections: Map<string, boolean> = new Map();
|
const connections: Map<string, boolean> = new Map();
|
||||||
|
|
||||||
const mainRoads = generateMainRoads();
|
const mainRoads = generateMainRoads();
|
||||||
const trails = generateTrails();
|
const trails = generateTrails();
|
||||||
// const oceanRoutes = getSearoutes();
|
const seaRoutes = generateSeaRoutes();
|
||||||
|
|
||||||
const routes = combineRoutes();
|
const routes = combineRoutes();
|
||||||
|
console.log(routes);
|
||||||
return {cellRoutes, routes};
|
return {cellRoutes, routes};
|
||||||
|
|
||||||
|
function sortBurgsByFeature(burgs: TBurgs) {
|
||||||
|
const burgsByFeature: Dict<IBurg[]> = {};
|
||||||
|
const capitalsByFeature: Dict<IBurg[]> = {};
|
||||||
|
const portsByFeature: Dict<IBurg[]> = {};
|
||||||
|
|
||||||
|
const isBurg = (burg: IBurg | TNoBurg): burg is IBurg => burg.i !== 0;
|
||||||
|
const addBurg = (object: Dict<IBurg[]>, feature: number, burg: IBurg) => {
|
||||||
|
if (!object[feature]) object[feature] = [];
|
||||||
|
object[feature].push(burg);
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const burg of burgs) {
|
||||||
|
if (isBurg(burg)) {
|
||||||
|
const {feature, capital, port} = burg;
|
||||||
|
addBurg(burgsByFeature, feature, burg);
|
||||||
|
if (capital) addBurg(capitalsByFeature, feature, burg);
|
||||||
|
if (port) addBurg(portsByFeature, port, burg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {burgsByFeature, capitalsByFeature, portsByFeature};
|
||||||
|
}
|
||||||
|
|
||||||
function generateMainRoads() {
|
function generateMainRoads() {
|
||||||
TIME && console.time("generateMainRoads");
|
TIME && console.time("generateMainRoads");
|
||||||
const mainRoads: {feature: number; cells: number[]}[] = [];
|
const mainRoads: {feature: number; cells: number[]}[] = [];
|
||||||
|
|
||||||
const capitalsByFeature = validBurgs.reduce((acc, burg) => {
|
|
||||||
const {capital, feature} = burg;
|
|
||||||
if (!capital) return acc;
|
|
||||||
if (!acc[feature]) acc[feature] = [];
|
|
||||||
acc[feature].push(burg);
|
|
||||||
return acc;
|
|
||||||
}, {} as {[feature: string]: IBurg[]});
|
|
||||||
|
|
||||||
for (const [key, featureCapitals] of Object.entries(capitalsByFeature)) {
|
for (const [key, featureCapitals] of Object.entries(capitalsByFeature)) {
|
||||||
const points: TPoints = featureCapitals.map(burg => [burg.x, burg.y]);
|
const points: TPoints = featureCapitals.map(burg => [burg.x, burg.y]);
|
||||||
const urquhartEdges = calculateUrquhartEdges(points);
|
const urquhartEdges = calculateUrquhartEdges(points);
|
||||||
|
|
@ -39,7 +56,7 @@ export function generateRoutes(
|
||||||
const start = featureCapitals[fromId].cell;
|
const start = featureCapitals[fromId].cell;
|
||||||
const exit = featureCapitals[toId].cell;
|
const exit = featureCapitals[toId].cell;
|
||||||
|
|
||||||
const segments = findLandPathSegments(cellRoutes, connections, start, exit);
|
const segments = findPathSegments({isWater: false, cellRoutes, connections, start, exit});
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
addConnections(segment, ROUTES.MAIN_ROAD);
|
addConnections(segment, ROUTES.MAIN_ROAD);
|
||||||
mainRoads.push({feature: Number(key), cells: segment});
|
mainRoads.push({feature: Number(key), cells: segment});
|
||||||
|
|
@ -56,13 +73,6 @@ export function generateRoutes(
|
||||||
|
|
||||||
const trails: {feature: number; cells: number[]}[] = [];
|
const trails: {feature: number; cells: number[]}[] = [];
|
||||||
|
|
||||||
const burgsByFeature = validBurgs.reduce((acc, burg) => {
|
|
||||||
const {feature} = burg;
|
|
||||||
if (!acc[feature]) acc[feature] = [];
|
|
||||||
acc[feature].push(burg);
|
|
||||||
return acc;
|
|
||||||
}, {} as {[feature: string]: IBurg[]});
|
|
||||||
|
|
||||||
for (const [key, featureBurgs] of Object.entries(burgsByFeature)) {
|
for (const [key, featureBurgs] of Object.entries(burgsByFeature)) {
|
||||||
const points: TPoints = featureBurgs.map(burg => [burg.x, burg.y]);
|
const points: TPoints = featureBurgs.map(burg => [burg.x, burg.y]);
|
||||||
const urquhartEdges = calculateUrquhartEdges(points);
|
const urquhartEdges = calculateUrquhartEdges(points);
|
||||||
|
|
@ -70,7 +80,7 @@ export function generateRoutes(
|
||||||
const start = featureBurgs[fromId].cell;
|
const start = featureBurgs[fromId].cell;
|
||||||
const exit = featureBurgs[toId].cell;
|
const exit = featureBurgs[toId].cell;
|
||||||
|
|
||||||
const segments = findLandPathSegments(cellRoutes, connections, start, exit);
|
const segments = findPathSegments({isWater: false, cellRoutes, connections, start, exit});
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
addConnections(segment, ROUTES.TRAIL);
|
addConnections(segment, ROUTES.TRAIL);
|
||||||
trails.push({feature: Number(key), cells: segment});
|
trails.push({feature: Number(key), cells: segment});
|
||||||
|
|
@ -82,6 +92,30 @@ export function generateRoutes(
|
||||||
return trails;
|
return trails;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateSeaRoutes() {
|
||||||
|
TIME && console.time("generateSearoutes");
|
||||||
|
const mainRoads: {feature: number; cells: number[]}[] = [];
|
||||||
|
|
||||||
|
for (const [key, featurePorts] of Object.entries(portsByFeature)) {
|
||||||
|
const points: TPoints = featurePorts.map(burg => [burg.x, burg.y]);
|
||||||
|
const urquhartEdges = calculateUrquhartEdges(points);
|
||||||
|
urquhartEdges.forEach(([fromId, toId]) => {
|
||||||
|
const start = featurePorts[fromId].cell;
|
||||||
|
const exit = featurePorts[toId].cell;
|
||||||
|
drawLine(cells.p[start], cells.p[exit]);
|
||||||
|
|
||||||
|
const segments = findPathSegments({isWater: true, cellRoutes, connections, start, exit});
|
||||||
|
for (const segment of segments) {
|
||||||
|
addConnections(segment, ROUTES.MAIN_ROAD);
|
||||||
|
mainRoads.push({feature: Number(key), cells: segment});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
TIME && console.timeEnd("generateSearoutes");
|
||||||
|
return mainRoads;
|
||||||
|
}
|
||||||
|
|
||||||
function addConnections(segment: number[], roadTypeId: number) {
|
function addConnections(segment: number[], roadTypeId: number) {
|
||||||
for (let i = 0; i < segment.length; i++) {
|
for (let i = 0; i < segment.length; i++) {
|
||||||
const cellId = segment[i];
|
const cellId = segment[i];
|
||||||
|
|
@ -91,58 +125,25 @@ export function generateRoutes(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find land route segments from cell to cell
|
function findPathSegments({
|
||||||
function findLandPathSegments(
|
isWater,
|
||||||
cellRoutes: Uint8Array,
|
cellRoutes,
|
||||||
connections: Map<string, boolean>,
|
connections,
|
||||||
start: number,
|
start,
|
||||||
exit: number
|
exit
|
||||||
): number[][] {
|
}: {
|
||||||
const from = findPath();
|
isWater: boolean;
|
||||||
|
cellRoutes: Uint8Array;
|
||||||
|
connections: Map<string, boolean>;
|
||||||
|
start: number;
|
||||||
|
exit: number;
|
||||||
|
}): number[][] {
|
||||||
|
const from = findPath(isWater, cellRoutes, temp, cells, start, exit);
|
||||||
if (!from) return [];
|
if (!from) return [];
|
||||||
|
|
||||||
const pathCells = restorePath(start, exit, from);
|
const pathCells = restorePath(start, exit, from);
|
||||||
const segments = getRouteSegments(pathCells, connections);
|
const segments = getRouteSegments(pathCells, connections);
|
||||||
return segments;
|
return segments;
|
||||||
|
|
||||||
function findPath() {
|
|
||||||
const from: number[] = [];
|
|
||||||
const cost: number[] = [];
|
|
||||||
const queue = new FlatQueue<number>();
|
|
||||||
queue.push(start, 0);
|
|
||||||
|
|
||||||
while (queue.length) {
|
|
||||||
const priority = queue.peekValue()!;
|
|
||||||
const next = queue.pop()!;
|
|
||||||
|
|
||||||
for (const neibCellId of cells.c[next]) {
|
|
||||||
if (cells.h[neibCellId] < MIN_LAND_HEIGHT) continue; // ignore water cells
|
|
||||||
|
|
||||||
const habitability = biomesData.habitability[cells.biome[neibCellId]];
|
|
||||||
if (!habitability) continue; // inhabitable cells are not passable (eg. lava, glacier)
|
|
||||||
|
|
||||||
const distanceCost = dist2(cells.p[next], cells.p[neibCellId]);
|
|
||||||
|
|
||||||
const habitabilityModifier = 1 + Math.max(100 - habitability, 0) / 1000; // [1, 1.1];
|
|
||||||
const heightModifier = 1 + Math.max(cells.h[neibCellId] - ELEVATION.HILLS, 0) / 500; // [1, 1.1];
|
|
||||||
const roadModifier = cellRoutes[neibCellId] ? 0.5 : 1;
|
|
||||||
const burgModifier = cells.burg[neibCellId] ? 0.5 : 1;
|
|
||||||
|
|
||||||
const cellsCost = distanceCost * habitabilityModifier * heightModifier * roadModifier * burgModifier;
|
|
||||||
const totalCost = priority + cellsCost;
|
|
||||||
|
|
||||||
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
|
|
||||||
from[neibCellId] = next;
|
|
||||||
|
|
||||||
if (neibCellId === exit) return from;
|
|
||||||
|
|
||||||
cost[neibCellId] = totalCost;
|
|
||||||
queue.push(neibCellId, totalCost);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null; // path is not found
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function combineRoutes() {
|
function combineRoutes() {
|
||||||
|
|
@ -156,10 +157,99 @@ export function generateRoutes(
|
||||||
routes.push({i: routes.length, type: "trail", feature, cells});
|
routes.push({i: routes.length, type: "trail", feature, cells});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const {feature, cells} of seaRoutes) {
|
||||||
|
routes.push({i: routes.length, type: "sea", feature, cells});
|
||||||
|
}
|
||||||
|
|
||||||
return routes;
|
return routes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findPath(
|
||||||
|
isWater: boolean,
|
||||||
|
cellRoutes: Uint8Array,
|
||||||
|
temp: Int8Array,
|
||||||
|
cells: TCellsData,
|
||||||
|
start: number,
|
||||||
|
exit: number
|
||||||
|
) {
|
||||||
|
const from: number[] = [];
|
||||||
|
const cost: number[] = [];
|
||||||
|
const queue = new FlatQueue<number>();
|
||||||
|
queue.push(start, 0);
|
||||||
|
|
||||||
|
return isWater ? findWaterPath() : findLandPath();
|
||||||
|
|
||||||
|
function findLandPath() {
|
||||||
|
while (queue.length) {
|
||||||
|
const priority = queue.peekValue()!;
|
||||||
|
const next = queue.pop()!;
|
||||||
|
|
||||||
|
for (const neibCellId of cells.c[next]) {
|
||||||
|
if (cells.h[neibCellId] < MIN_LAND_HEIGHT) continue; // ignore water cells
|
||||||
|
|
||||||
|
const habitability = biomesData.habitability[cells.biome[neibCellId]];
|
||||||
|
if (!habitability) continue; // inhabitable cells are not passable (eg. lava, glacier)
|
||||||
|
|
||||||
|
const distanceCost = dist2(cells.p[next], cells.p[neibCellId]);
|
||||||
|
|
||||||
|
const habitabilityModifier = 1 + Math.max(100 - habitability, 0) / 1000; // [1, 1.1];
|
||||||
|
const heightModifier = 1 + Math.max(cells.h[neibCellId] - ELEVATION.HILLS, 0) / 500; // [1, 1.1];
|
||||||
|
const roadModifier = cellRoutes[neibCellId] ? 0.5 : 1;
|
||||||
|
const burgModifier = cells.burg[neibCellId] ? 0.5 : 1;
|
||||||
|
|
||||||
|
const cellsCost = distanceCost * habitabilityModifier * heightModifier * roadModifier * burgModifier;
|
||||||
|
const totalCost = priority + cellsCost;
|
||||||
|
|
||||||
|
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
|
||||||
|
from[neibCellId] = next;
|
||||||
|
|
||||||
|
if (neibCellId === exit) return from;
|
||||||
|
|
||||||
|
cost[neibCellId] = totalCost;
|
||||||
|
queue.push(neibCellId, totalCost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null; // path is not found
|
||||||
|
}
|
||||||
|
|
||||||
|
function findWaterPath() {
|
||||||
|
const MIN_PASSABLE_TEMP = -4;
|
||||||
|
|
||||||
|
while (queue.length) {
|
||||||
|
const priority = queue.peekValue()!;
|
||||||
|
const next = queue.pop()!;
|
||||||
|
|
||||||
|
for (const neibCellId of cells.c[next]) {
|
||||||
|
if (neibCellId === exit) {
|
||||||
|
from[neibCellId] = next;
|
||||||
|
return from;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cells.h[neibCellId] >= MIN_LAND_HEIGHT) continue; // ignore land cells
|
||||||
|
if (temp[cells.g[neibCellId]] < MIN_PASSABLE_TEMP) continue; // ignore to cold cells
|
||||||
|
|
||||||
|
const distanceCost = dist2(cells.p[next], cells.p[neibCellId]);
|
||||||
|
const typeModifier = Math.abs(cells.t[neibCellId]); // 1 for coastline, 2 for deep ocean, 3 for deeper ocean
|
||||||
|
|
||||||
|
const routeModifier = cellRoutes[neibCellId] ? 0.5 : 1;
|
||||||
|
|
||||||
|
const cellsCost = distanceCost * typeModifier * routeModifier;
|
||||||
|
const totalCost = priority + cellsCost;
|
||||||
|
|
||||||
|
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
|
||||||
|
from[neibCellId] = next;
|
||||||
|
|
||||||
|
cost[neibCellId] = totalCost;
|
||||||
|
queue.push(neibCellId, totalCost);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null; // path is not found
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function restorePath(start: number, end: number, from: number[]) {
|
function restorePath(start: number, end: number, from: number[]) {
|
||||||
const cells: number[] = [];
|
const cells: number[] = [];
|
||||||
|
|
||||||
|
|
@ -181,8 +271,6 @@ function getRouteSegments(pathCells: number[], connections: Map<string, boolean>
|
||||||
const segments: number[][] = [];
|
const segments: number[][] = [];
|
||||||
let segment: number[] = [];
|
let segment: number[] = [];
|
||||||
|
|
||||||
// if (pathCells.includes(5204)) debugger;
|
|
||||||
|
|
||||||
for (let i = 0; i < pathCells.length; i++) {
|
for (let i = 0; i < pathCells.length; i++) {
|
||||||
const cellId = pathCells[i];
|
const cellId = pathCells[i];
|
||||||
const nextCellId = pathCells[i + 1];
|
const nextCellId = pathCells[i + 1];
|
||||||
|
|
|
||||||
|
|
@ -117,10 +117,12 @@ export function createPack(grid: IGrid): IPack {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const {cellRoutes, routes} = generateRoutes(burgs, {
|
const {cellRoutes, routes} = generateRoutes(burgs, temp, {
|
||||||
c: cells.c,
|
c: cells.c,
|
||||||
p: cells.p,
|
p: cells.p,
|
||||||
|
g: cells.g,
|
||||||
h: heights,
|
h: heights,
|
||||||
|
t: distanceField,
|
||||||
biome,
|
biome,
|
||||||
state: stateIds,
|
state: stateIds,
|
||||||
burg: burgIds
|
burg: burgIds
|
||||||
|
|
@ -189,14 +191,17 @@ function repackGrid(grid: IGrid) {
|
||||||
for (const i of gridCells.i) {
|
for (const i of gridCells.i) {
|
||||||
const height = gridCells.h[i];
|
const height = gridCells.h[i];
|
||||||
const type = gridCells.t[i];
|
const type = gridCells.t[i];
|
||||||
if (height < MIN_LAND_HEIGHT && type !== WATER_COAST && type !== DEEPER_WATER) continue; // exclude all deep ocean points
|
|
||||||
|
// exclude ocean points far from coast
|
||||||
|
if (height < MIN_LAND_HEIGHT && type !== WATER_COAST && type !== DEEPER_WATER) continue;
|
||||||
|
|
||||||
const feature = features[gridCells.f[i]];
|
const feature = features[gridCells.f[i]];
|
||||||
const isLake = feature && feature.type === "lake";
|
const isLake = feature && feature.type === "lake";
|
||||||
|
|
||||||
if (type === DEEPER_WATER && (i % 4 === 0 || isLake)) continue; // exclude non-coastal lake points
|
// exclude non-coastal lake points
|
||||||
const [x, y] = points[i];
|
if (type === DEEPER_WATER && (i % 4 === 0 || isLake)) continue;
|
||||||
|
|
||||||
|
const [x, y] = points[i];
|
||||||
addNewPoint(i, x, y, height);
|
addNewPoint(i, x, y, height);
|
||||||
|
|
||||||
// add additional points for cells along coast
|
// add additional points for cells along coast
|
||||||
|
|
|
||||||
|
|
@ -375,7 +375,7 @@ const resolveDepressions = function (
|
||||||
return [initialCellHeights, {}];
|
return [initialCellHeights, {}];
|
||||||
}
|
}
|
||||||
|
|
||||||
INFO && console.info(`ⓘ Resolved all depressions. Depressions: ${depressions[0]}. Iterations: ${depressions.length}`);
|
INFO && console.info(`ⓘ resolved all ${depressions[0]} depressions in ${depressions.length} iterations`);
|
||||||
return [currentCellHeights, currentDrainableLakes];
|
return [currentCellHeights, currentDrainableLakes];
|
||||||
|
|
||||||
// define lakes that potentially can be open (drained into another water body)
|
// define lakes that potentially can be open (drained into another water body)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue