mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: roads generation - fix route segment detection
This commit is contained in:
parent
1dd185354b
commit
fc3e3b844d
2 changed files with 52 additions and 35 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import * as d3 from "d3";
|
import * as d3 from "d3";
|
||||||
|
import {drawPoint} from "utils/debugUtils";
|
||||||
|
|
||||||
import {round} from "utils/stringUtils";
|
import {round} from "utils/stringUtils";
|
||||||
|
|
||||||
|
|
@ -22,11 +23,21 @@ export function drawRoutes() {
|
||||||
return cells.p[cellId];
|
return cells.p[cellId];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const normalizePoints = (points: TPoints): TPoints =>
|
||||||
|
points.map(([x, y], index) => {
|
||||||
|
return [x, y];
|
||||||
|
|
||||||
|
if (i === 17) {
|
||||||
|
cells.forEach(cellId => drawPoint(pack.cells.p[cellId]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const routePaths: Dict<string[]> = {};
|
const routePaths: Dict<string[]> = {};
|
||||||
|
|
||||||
for (const {i, type, cells} of pack.routes) {
|
for (const {i, type, cells} of pack.routes) {
|
||||||
const points = getPathPoints(cells);
|
const points = getPathPoints(cells);
|
||||||
const path = round(lineGen(points)!, 1);
|
const normalizedPoints = normalizePoints(points);
|
||||||
|
const path = round(lineGen(normalizedPoints)!, 1);
|
||||||
|
|
||||||
if (!routePaths[type]) routePaths[type] = [];
|
if (!routePaths[type]) routePaths[type] = [];
|
||||||
routePaths[type].push(`<path id="${type}${i}" d="${path}"/>`);
|
routePaths[type].push(`<path id="${type}${i}" d="${path}"/>`);
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,13 @@ export function generateRoutes(
|
||||||
) {
|
) {
|
||||||
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 validBurgs = burgs.filter(burg => burg.i && !(burg as IBurg).removed) as IBurg[];
|
||||||
|
const connections: Map<string, boolean> = new Map();
|
||||||
|
|
||||||
const mainRoads = generateMainRoads();
|
const mainRoads = generateMainRoads();
|
||||||
const trails = generateTrails();
|
const trails = generateTrails();
|
||||||
// const oceanRoutes = getSearoutes();
|
// const oceanRoutes = getSearoutes();
|
||||||
|
|
||||||
const routes = combineRoutes();
|
const routes = combineRoutes();
|
||||||
|
|
||||||
console.log(routes);
|
|
||||||
return {cellRoutes, routes};
|
return {cellRoutes, routes};
|
||||||
|
|
||||||
function generateMainRoads() {
|
function generateMainRoads() {
|
||||||
|
|
@ -37,16 +37,14 @@ export function generateRoutes(
|
||||||
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);
|
||||||
urquhartEdges.forEach(([fromId, toId]) => {
|
urquhartEdges.forEach(([fromId, toId]) => {
|
||||||
drawLine(points[fromId], points[toId], {stroke: "red", strokeWidth: 0.03});
|
drawLine(points[fromId], points[toId], {stroke: "red", strokeWidth: 0.01});
|
||||||
|
|
||||||
const start = featureCapitals[fromId].cell;
|
const start = featureCapitals[fromId].cell;
|
||||||
const exit = featureCapitals[toId].cell;
|
const exit = featureCapitals[toId].cell;
|
||||||
|
|
||||||
const segments = findLandPathSegments(cellRoutes, start, exit);
|
const segments = findLandPathSegments(cellRoutes, connections, start, exit);
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
segment.forEach(cellId => {
|
addConnections(segment, ROUTES.MAIN_ROAD);
|
||||||
cellRoutes[cellId] = ROUTES.MAIN_ROAD;
|
|
||||||
});
|
|
||||||
mainRoads.push({feature: Number(key), cells: segment});
|
mainRoads.push({feature: Number(key), cells: segment});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -72,16 +70,14 @@ export function generateRoutes(
|
||||||
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);
|
||||||
urquhartEdges.forEach(([fromId, toId]) => {
|
urquhartEdges.forEach(([fromId, toId]) => {
|
||||||
drawLine(points[fromId], points[toId], {strokeWidth: 0.03});
|
drawLine(points[fromId], points[toId], {strokeWidth: 0.01});
|
||||||
|
|
||||||
const start = featureBurgs[fromId].cell;
|
const start = featureBurgs[fromId].cell;
|
||||||
const exit = featureBurgs[toId].cell;
|
const exit = featureBurgs[toId].cell;
|
||||||
|
|
||||||
const segments = findLandPathSegments(cellRoutes, start, exit);
|
const segments = findLandPathSegments(cellRoutes, connections, start, exit);
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
segment.forEach(cellId => {
|
addConnections(segment, ROUTES.TRAIL);
|
||||||
cellRoutes[cellId] = ROUTES.TRAIL;
|
|
||||||
});
|
|
||||||
trails.push({feature: Number(key), cells: segment});
|
trails.push({feature: Number(key), cells: segment});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -91,13 +87,27 @@ export function generateRoutes(
|
||||||
return trails;
|
return trails;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addConnections(segment: number[], roadTypeId: number) {
|
||||||
|
for (let i = 0; i < segment.length; i++) {
|
||||||
|
const cellId = segment[i];
|
||||||
|
const nextCellId = segment[i + 1];
|
||||||
|
if (nextCellId) connections.set(`${cellId}-${nextCellId}`, true);
|
||||||
|
cellRoutes[cellId] = roadTypeId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// find land route segments from cell to cell
|
// find land route segments from cell to cell
|
||||||
function findLandPathSegments(cellRoutes: Uint8Array, start: number, exit: number): number[][] {
|
function findLandPathSegments(
|
||||||
|
cellRoutes: Uint8Array,
|
||||||
|
connections: Map<string, boolean>,
|
||||||
|
start: number,
|
||||||
|
exit: number
|
||||||
|
): number[][] {
|
||||||
const from = findPath();
|
const from = findPath();
|
||||||
if (!from) return [];
|
if (!from) return [];
|
||||||
|
|
||||||
const pathCells = restorePath(start, exit, from);
|
const pathCells = restorePath(start, exit, from);
|
||||||
const segments = getRouteSegments(pathCells, cellRoutes);
|
const segments = getRouteSegments(pathCells, connections);
|
||||||
return segments;
|
return segments;
|
||||||
|
|
||||||
function findPath() {
|
function findPath() {
|
||||||
|
|
@ -172,36 +182,32 @@ function restorePath(start: number, end: number, from: number[]) {
|
||||||
return cells;
|
return cells;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRouteSegments(pathCells: number[], cellRoutes: Uint8Array) {
|
function getRouteSegments(pathCells: number[], connections: Map<string, boolean>) {
|
||||||
const hasRoute = (cellId: number) => cellRoutes[cellId] !== 0;
|
|
||||||
const noRoute = (cellId: number) => cellRoutes[cellId] === 0;
|
|
||||||
|
|
||||||
// UC: only first and/or last cell have route
|
|
||||||
if (pathCells.slice(1, -1).every(noRoute)) return [pathCells];
|
|
||||||
|
|
||||||
// UC: all cells already have route
|
|
||||||
if (pathCells.every(hasRoute)) return [];
|
|
||||||
|
|
||||||
// UC: discontinuous route
|
|
||||||
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];
|
||||||
|
const isConnected = connections.has(`${cellId}-${nextCellId}`) || connections.has(`${nextCellId}-${cellId}`);
|
||||||
|
|
||||||
const hasRoute = cellRoutes[cellId] !== 0;
|
if (isConnected) {
|
||||||
const nextHasRoute = cellRoutes[nextCellId] !== 0;
|
if (segment.length) {
|
||||||
|
// segment stepped into existing segment
|
||||||
const noConnection = !hasRoute || !nextHasRoute;
|
segment.push(pathCells[i]);
|
||||||
if (noConnection) segment.push(cellId);
|
segments.push(segment);
|
||||||
|
segment = [];
|
||||||
if (nextHasRoute) {
|
}
|
||||||
if (segment.length) segments.push(segment);
|
continue;
|
||||||
segment = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
segment.push(pathCells[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (segment.length) segments.push(segment);
|
||||||
|
|
||||||
return segments;
|
return segments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue