refactor: roads - Urquhart graph approach

This commit is contained in:
max 2022-08-20 15:10:51 +03:00
parent bc98757b96
commit b0f081b3ba
6 changed files with 167 additions and 46 deletions

View file

@ -61,6 +61,6 @@ export const FOREST_BIOMES = [
export const ROUTES = { export const ROUTES = {
MAIN_ROAD: 1, MAIN_ROAD: 1,
SMALL_ROAD: 2, TRAIL: 2,
SEA_ROUTE: 3 SEA_ROUTE: 3
}; };

View file

@ -6,7 +6,7 @@ export function drawRoutes() {
routes.selectAll("path").remove(); routes.selectAll("path").remove();
const {cells, burgs} = pack; const {cells, burgs} = pack;
const lineGen = d3.line().curve(d3.curveBasis); const lineGen = d3.line().curve(d3.curveCatmullRom.alpha(0.1));
const getBurgCoords = (burgId: number): TPoint => { const getBurgCoords = (burgId: number): TPoint => {
if (!burgId) throw new Error("burgId must be positive"); if (!burgId) throw new Error("burgId must be positive");
@ -24,8 +24,8 @@ export function drawRoutes() {
const routePaths: Dict<string[]> = {}; const routePaths: Dict<string[]> = {};
for (const {i, type, cells: routeCells} of pack.routes) { for (const {i, type, cells} of pack.routes) {
const points = getPathPoints(routeCells); const points = getPathPoints(cells);
const path = round(lineGen(points)!, 1); const path = round(lineGen(points)!, 1);
if (!routePaths[type]) routePaths[type] = []; if (!routePaths[type]) routePaths[type] = [];

View file

@ -64,8 +64,8 @@ async function generate(options?: IGenerationOptions) {
// temp rendering for debug // temp rendering for debug
// renderLayer("cells"); // renderLayer("cells");
renderLayer("features"); renderLayer("features");
renderLayer("heightmap"); // renderLayer("heightmap");
renderLayer("rivers"); // renderLayer("rivers");
// renderLayer("biomes"); // renderLayer("biomes");
renderLayer("burgs"); renderLayer("burgs");
renderLayer("routes"); renderLayer("routes");

View file

@ -1,14 +1,16 @@
import Delaunator from "delaunator";
import FlatQueue from "flatqueue"; import FlatQueue from "flatqueue";
import {TIME} from "config/logging"; import {TIME} from "config/logging";
import {ROUTES} from "config/generation"; import {ROUTES} from "config/generation";
import {dist2} from "utils/functionUtils";
const isBurg = (burg: TNoBurg | IBurg): burg is IBurg => burg.i > 0; import {drawLine} from "utils/debugUtils";
export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" | "h" | "biome" | "state" | "burg">) { export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" | "h" | "biome" | "state" | "burg">) {
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 mainRoads = generateMainRoads(); const mainRoads = generateMainRoads();
// const townRoutes = getTrails(); const trails = generateTrails();
// const oceanRoutes = getSearoutes(); // const oceanRoutes = getSearoutes();
const routes = combineRoutes(); const routes = combineRoutes();
@ -18,50 +20,85 @@ export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" |
function generateMainRoads() { function generateMainRoads() {
TIME && console.time("generateMainRoads"); TIME && console.time("generateMainRoads");
const mainRoads: {feature: number; from: number; to: number; end: number; cells: number[]}[] = []; const mainRoads: {feature: number; cells: number[]}[] = [];
const capitalsByFeature = burgs.reduce((acc, burg) => {
if (!isBurg(burg)) return acc;
const {capital, removed, feature} = burg;
if (!capital || removed) return acc;
const capitalsByFeature = validBurgs.reduce((acc, burg) => {
const {capital, feature} = burg;
if (!capital) return acc;
if (!acc[feature]) acc[feature] = []; if (!acc[feature]) acc[feature] = [];
acc[feature].push(burg); acc[feature].push(burg);
return acc; return acc;
}, {} as {[feature: string]: IBurg[]}); }, {} as {[feature: string]: IBurg[]});
for (const [key, featureCapitals] of Object.entries(capitalsByFeature)) { for (const [key, featureCapitals] of Object.entries(capitalsByFeature)) {
for (let i = 0; i < featureCapitals.length; i++) { const points: TPoints = featureCapitals.map(burg => [burg.x, burg.y]);
const {cell: from} = featureCapitals[i]; const urquhartEdges = calculateUrquhartEdges(points);
urquhartEdges.forEach(([fromId, toId]) => {
drawLine(points[fromId], points[toId], {stroke: "red", strokeWidth: 0.05});
for (let j = i + 1; j < featureCapitals.length; j++) { const start = featureCapitals[fromId].cell;
const {cell: to} = featureCapitals[j]; const exit = featureCapitals[toId].cell;
const {end, pathCells} = findLandPath({start: from, exit: to}); const segments = findLandPathSegments(cellRoutes, start, exit);
if (end !== null && pathCells.length) { for (const segment of segments) {
pathCells.forEach(cellId => { segment.forEach(cellId => {
cellRoutes[cellId] = ROUTES.MAIN_ROAD; cellRoutes[cellId] = ROUTES.MAIN_ROAD;
}); });
mainRoads.push({feature: Number(key), from, to, end, cells: pathCells}); mainRoads.push({feature: Number(key), cells: segment});
}
} }
} });
} }
TIME && console.timeEnd("generateMainRoads"); TIME && console.timeEnd("generateMainRoads");
return mainRoads; return mainRoads;
} }
// find land path to a specific cell or to a closest road function generateTrails() {
function findLandPath({start, exit}: {start: number; exit: number}) { TIME && console.time("generateTrails");
const from: number[] = [];
const end = findPath();
if (end === null) return {end, pathCells: []};
const pathCells = restorePath(start, end, from); const trails: {feature: number; cells: number[]}[] = [];
return {end, pathCells};
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)) {
const points: TPoints = featureBurgs.map(burg => [burg.x, burg.y]);
const urquhartEdges = calculateUrquhartEdges(points);
urquhartEdges.forEach(([fromId, toId]) => {
drawLine(points[fromId], points[toId], {strokeWidth: 0.05});
const start = featureBurgs[fromId].cell;
const exit = featureBurgs[toId].cell;
const segments = findLandPathSegments(cellRoutes, start, exit);
for (const segment of segments) {
segment.forEach(cellId => {
cellRoutes[cellId] = ROUTES.TRAIL;
});
trails.push({feature: Number(key), cells: segment});
}
});
}
TIME && console.timeEnd("generateTrails");
return trails;
}
// find land route segments from cell to cell
function findLandPathSegments(cellRoutes: Uint8Array, start: number, exit: number): number[][] {
const from = findPath();
if (!from) return [];
const pathCells = restorePath(start, exit, from);
const segments = getRouteSegments(pathCells, cellRoutes);
return segments;
function findPath() { function findPath() {
const from: number[] = [];
const cost: number[] = []; const cost: number[] = [];
const queue = new FlatQueue<number>(); const queue = new FlatQueue<number>();
queue.push(start, 0); queue.push(start, 0);
@ -70,38 +107,40 @@ export function generateRoutes(burgs: TBurgs, cells: Pick<IPack["cells"], "c" |
const priority = queue.peekValue()!; const priority = queue.peekValue()!;
const next = queue.pop()!; const next = queue.pop()!;
if (cellRoutes[next]) return next;
for (const neibCellId of cells.c[next]) { for (const neibCellId of cells.c[next]) {
if (cells.h[neibCellId] < 20) continue; // ignore water cells if (cells.h[neibCellId] < 20) continue; // ignore water cells
const stateChangeCost = cells.state && cells.state[neibCellId] !== cells.state[next] ? 400 : 0; // trails tend to lay within the same state const stateChangeCost = cells.state && cells.state[neibCellId] !== cells.state[next] ? 400 : 0; // prefer to lay within the same state
const habitability = biomesData.habitability[cells.biome[neibCellId]]; const habitability = biomesData.habitability[cells.biome[neibCellId]];
if (!habitability) continue; // avoid inhabitable cells (eg. lava, glacier) if (!habitability) continue; // avoid inhabitable cells (eg. lava, glacier)
const habitedCost = habitability ? Math.max(100 - habitability, 0) : 400; // routes tend to lay within populated areas const habitedCost = habitability ? Math.max(100 - habitability, 0) : 400; // routes tend to lay within populated areas
const heightChangeCost = Math.abs(cells.h[neibCellId] - cells.h[next]) * 10; // routes tend to avoid elevation changes const heightChangeCost = Math.abs(cells.h[neibCellId] - cells.h[next]) * 10; // routes tend to avoid elevation changes
const heightCost = cells.h[neibCellId] > 80 ? cells.h[neibCellId] : 0; // routes tend to avoid mountainous areas const heightCost = cells.h[neibCellId] > 80 ? cells.h[neibCellId] : 0; // routes tend to avoid mountainous areas
const cellCoast = 10 + stateChangeCost + habitedCost + heightChangeCost + heightCost; const cellCoast = 10 + stateChangeCost + habitedCost + heightChangeCost + heightCost;
const totalCost = priority + (cellRoutes[neibCellId] || cells.burg[neibCellId] ? cellCoast / 3 : cellCoast); const totalCost = priority + (cellRoutes[neibCellId] || cells.burg[neibCellId] ? cellCoast / 2 : cellCoast);
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue; if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
from[neibCellId] = next; from[neibCellId] = next;
if (neibCellId === exit) return exit; if (neibCellId === exit) return from;
cost[neibCellId] = totalCost; cost[neibCellId] = totalCost;
queue.push(neibCellId, totalCost); queue.push(neibCellId, totalCost);
} }
} }
return null; return null; // path is not found
} }
} }
function combineRoutes() { function combineRoutes() {
const routes: TRoutes = []; const routes: TRoutes = [];
for (const {feature, from, to, end, cells} of mainRoads) { for (const {feature, cells} of mainRoads) {
routes.push({i: routes.length, type: "road", feature, from, to, end, cells}); routes.push({i: routes.length, type: "road", feature, cells});
}
for (const {feature, cells} of trails) {
routes.push({i: routes.length, type: "trail", feature, cells});
} }
return routes; return routes;
@ -115,10 +154,84 @@ function restorePath(start: number, end: number, from: number[]) {
let prev = end; let prev = end;
while (current !== start) { while (current !== start) {
prev = from[current];
cells.push(current); cells.push(current);
prev = from[current];
current = prev; current = prev;
} }
cells.push(current);
return cells; return cells;
} }
function getRouteSegments(pathCells: number[], cellRoutes: Uint8Array) {
const hasRoute = (cellId: number) => cellRoutes[cellId] !== 0;
const noRoute = (cellId: number) => cellRoutes[cellId] === 0;
const segments: number[][] = [];
let segment: number[] = [];
// UC: complitely new route
if (pathCells.every(noRoute)) return [pathCells];
// UC: all cells already have route
if (pathCells.every(hasRoute)) return [];
// UC: only first and/or last cell have route
if (pathCells.slice(1, -1).every(noRoute)) return [pathCells];
// UC: discontinuous route
for (let i = 0; i < pathCells.length; i++) {
const cellId = pathCells[i];
const nextCellId = pathCells[i + 1];
const hasRoute = cellRoutes[cellId] !== 0;
const nextHasRoute = cellRoutes[nextCellId] !== 0;
const noConnection = !hasRoute || !nextHasRoute;
if (noConnection) segment.push(cellId);
}
return segments;
}
// Urquhart graph is obtained by removing the longest edge from each triangle in the Delaunay triangulation
// this gives us an aproximation of a desired road network, i.e. connections between burgs
// code from https://observablehq.com/@mbostock/urquhart-graph
function calculateUrquhartEdges(points: TPoints) {
const score = (p0: number, p1: number) => dist2(points[p0], points[p1]);
const {halfedges, triangles} = Delaunator.from(points);
const n = triangles.length;
const removed = new Uint8Array(n);
const edges = [];
for (let e = 0; e < n; e += 3) {
const p0 = triangles[e],
p1 = triangles[e + 1],
p2 = triangles[e + 2];
const p01 = score(p0, p1),
p12 = score(p1, p2),
p20 = score(p2, p0);
removed[
p20 > p01 && p20 > p12
? Math.max(e + 2, halfedges[e + 2])
: p12 > p01 && p12 > p20
? Math.max(e + 1, halfedges[e + 1])
: Math.max(e, halfedges[e])
] = 1;
}
for (let e = 0; e < n; ++e) {
if (e > halfedges[e] && !removed[e]) {
const t0 = triangles[e];
const t1 = triangles[e % 3 === 2 ? e - 2 : e + 1];
edges.push([t0, t1]);
}
}
return edges;
}

View file

@ -2,9 +2,6 @@ interface IRoute {
i: number; i: number;
type: "road" | "trail" | "sea"; type: "road" | "trail" | "sea";
feature: number; feature: number;
from: number;
to: number;
end: number;
cells: number[]; cells: number[];
} }

View file

@ -17,6 +17,17 @@ export function drawPolygon(
.attr("stroke-width", strokeWidth); .attr("stroke-width", strokeWidth);
} }
export function drawLine([x1, y1]: TPoint, [x2, y2]: TPoint, {stroke = "#444", strokeWidth = 0.2} = {}) {
debug
.append("line")
.attr("x1", x1)
.attr("y1", y1)
.attr("x2", x2)
.attr("y2", y2)
.attr("stroke", stroke)
.attr("stroke-width", strokeWidth);
}
export function drawArrow([x1, y1]: TPoint, [x2, y2]: TPoint, {width = 1, color = "#444"} = {}): void { export function drawArrow([x1, y1]: TPoint, [x2, y2]: TPoint, {width = 1, color = "#444"} = {}): void {
const angle = Math.atan2(y2 - y1, x2 - x1); const angle = Math.atan2(y2 - y1, x2 - x1);
const normal = angle + Math.PI / 2; const normal = angle + Math.PI / 2;