mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
addOverseaRoute
This commit is contained in:
parent
e3da664e56
commit
af1d369e31
1 changed files with 99 additions and 74 deletions
|
|
@ -1,33 +1,36 @@
|
||||||
(function (global, factory) {
|
(function (global, factory) {
|
||||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
typeof exports === "object" && typeof module !== "undefined" ? (module.exports = factory()) : typeof define === "function" && define.amd ? define(factory) : (global.Routes = factory());
|
||||||
typeof define === 'function' && define.amd ? define(factory) :
|
})(this, function () {
|
||||||
(global.Routes = factory());
|
"use strict";
|
||||||
}(this, (function () {'use strict';
|
|
||||||
|
|
||||||
const getRoads = function () {
|
const getRoads = function () {
|
||||||
TIME && console.time("generateMainRoads");
|
TIME && console.time("generateMainRoads");
|
||||||
const cells = pack.cells, burgs = pack.burgs.filter(b => b.i && !b.removed);
|
const cells = pack.cells;
|
||||||
|
const burgs = pack.burgs.filter(b => b.i && !b.removed);
|
||||||
const capitals = burgs.filter(b => b.capital);
|
const capitals = burgs.filter(b => b.capital);
|
||||||
|
|
||||||
if (capitals.length < 2) return []; // not enough capitals to build main roads
|
if (capitals.length < 2) return []; // not enough capitals to build main roads
|
||||||
const paths = []; // array to store path segments
|
const paths = []; // array to store path segments
|
||||||
|
|
||||||
for (const b of capitals) {
|
for (const b of capitals) {
|
||||||
const connect = capitals.filter(c => c.i > b.i && c.feature === b.feature);
|
const connect = capitals.filter(c => c.i > b.i && c.feature === b.feature);
|
||||||
if (!connect.length) continue;
|
if (!connect.length) continue;
|
||||||
const farthest = d3.scan(connect, (a, c) => ((c.y - b.y) ** 2 + (c.x - b.x) ** 2) - ((a.y - b.y) ** 2 + (a.x - b.x) ** 2));
|
const farthest = d3.scan(connect, (a, c) => (c.y - b.y) ** 2 + (c.x - b.x) ** 2 - ((a.y - b.y) ** 2 + (a.x - b.x) ** 2));
|
||||||
const [from, exit] = findLandPath(b.cell, connect[farthest].cell, null);
|
const [from, exit] = findLandPath(b.cell, connect[farthest].cell, null);
|
||||||
const segments = restorePath(b.cell, exit, "main", from);
|
const segments = restorePath(b.cell, exit, "main", from);
|
||||||
segments.forEach(s => paths.push(s));
|
segments.forEach(s => paths.push(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
cells.i.forEach(i => cells.s[i] += cells.road[i] / 2); // add roads to suitability score
|
cells.i.forEach(i => (cells.s[i] += cells.road[i] / 2)); // add roads to suitability score
|
||||||
TIME && console.timeEnd("generateMainRoads");
|
TIME && console.timeEnd("generateMainRoads");
|
||||||
return paths;
|
return paths;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getTrails = function () {
|
const getTrails = function () {
|
||||||
TIME && console.time("generateTrails");
|
TIME && console.time("generateTrails");
|
||||||
const cells = pack.cells, burgs = pack.burgs.filter(b => b.i && !b.removed);
|
const cells = pack.cells;
|
||||||
|
const burgs = pack.burgs.filter(b => b.i && !b.removed);
|
||||||
|
|
||||||
if (burgs.length < 2) return []; // not enough burgs to build trails
|
if (burgs.length < 2) return []; // not enough burgs to build trails
|
||||||
|
|
||||||
let paths = []; // array to store path segments
|
let paths = []; // array to store path segments
|
||||||
|
|
@ -39,7 +42,7 @@
|
||||||
let path = [];
|
let path = [];
|
||||||
if (!i) {
|
if (!i) {
|
||||||
// build trail from the first burg on island to the farthest one on the same island
|
// build trail from the first burg on island to the farthest one on the same island
|
||||||
const farthest = d3.scan(isle, (a, c) => ((c.y - b.y) ** 2 + (c.x - b.x) ** 2) - ((a.y - b.y) ** 2 + (a.x - b.x) ** 2));
|
const farthest = d3.scan(isle, (a, c) => (c.y - b.y) ** 2 + (c.x - b.x) ** 2 - ((a.y - b.y) ** 2 + (a.x - b.x) ** 2));
|
||||||
const to = isle[farthest].cell;
|
const to = isle[farthest].cell;
|
||||||
if (cells.road[to]) return;
|
if (cells.road[to]) return;
|
||||||
const [from, exit] = findLandPath(b.cell, to, null);
|
const [from, exit] = findLandPath(b.cell, to, null);
|
||||||
|
|
@ -57,21 +60,26 @@
|
||||||
|
|
||||||
TIME && console.timeEnd("generateTrails");
|
TIME && console.timeEnd("generateTrails");
|
||||||
return paths;
|
return paths;
|
||||||
}
|
};
|
||||||
|
|
||||||
const getSearoutes = function () {
|
const getSearoutes = function () {
|
||||||
TIME && console.time("generateSearoutes");
|
TIME && console.time("generateSearoutes");
|
||||||
const allPorts = pack.burgs.filter(b => b.port > 0 && !b.removed);
|
const {cells, burgs, features} = pack;
|
||||||
if (allPorts.length < 2) return [];
|
const allPorts = burgs.filter(b => b.port > 0 && !b.removed);
|
||||||
|
|
||||||
const bodies = new Set(allPorts.map(b => b.port)); // features with ports
|
if (!allPorts.length) return [];
|
||||||
|
|
||||||
|
const bodies = new Set(allPorts.map(b => b.port)); // water features with ports
|
||||||
let paths = []; // array to store path segments
|
let paths = []; // array to store path segments
|
||||||
const connected = []; // store cell id of connected burgs
|
const connected = []; // store cell id of connected burgs
|
||||||
|
|
||||||
bodies.forEach(function(f) {
|
bodies.forEach(f => {
|
||||||
const ports = allPorts.filter(b => b.port === f); // all ports on the same feature
|
const ports = allPorts.filter(b => b.port === f); // all ports on the same feature
|
||||||
if (ports.length < 2) return;
|
if (!ports.length) return;
|
||||||
|
|
||||||
|
if (features[f].border) addOverseaRoute(f, ports[0]);
|
||||||
|
|
||||||
|
// get inner-map routes
|
||||||
for (let s = 0; s < ports.length; s++) {
|
for (let s = 0; s < ports.length; s++) {
|
||||||
const source = ports[s].cell;
|
const source = ports[s].cell;
|
||||||
if (connected[source]) continue;
|
if (connected[source]) continue;
|
||||||
|
|
@ -90,51 +98,53 @@
|
||||||
connected[target] = 1;
|
connected[target] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function addOverseaRoute(f, port) {
|
||||||
|
const {x, y, cell: source} = port;
|
||||||
|
const dist = p => Math.abs(p[0] - x) + Math.abs(p[1] - y);
|
||||||
|
const [x1, y1] = [
|
||||||
|
[0, y],
|
||||||
|
[x, 0],
|
||||||
|
[graphWidth, y],
|
||||||
|
[x, graphHeight]
|
||||||
|
].sort((a, b) => dist(a) - dist(b))[0];
|
||||||
|
const target = findCell(x1, y1);
|
||||||
|
|
||||||
|
if (cells.f[target] === f && cells.h[target] < 20) {
|
||||||
|
const [from, exit, passable] = findOceanPath(target, source, true);
|
||||||
|
|
||||||
|
if (passable) {
|
||||||
|
const path = restorePath(target, exit, "ocean", from);
|
||||||
|
paths = paths.concat(path);
|
||||||
|
last(path).push([x1, y1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TIME && console.timeEnd("generateSearoutes");
|
TIME && console.timeEnd("generateSearoutes");
|
||||||
return paths;
|
return paths;
|
||||||
}
|
};
|
||||||
|
|
||||||
const draw = function(main, small, ocean) {
|
const draw = function (main, small, water) {
|
||||||
TIME && console.time("drawRoutes");
|
TIME && console.time("drawRoutes");
|
||||||
const cells = pack.cells, burgs = pack.burgs;
|
const {cells, burgs} = pack;
|
||||||
|
const {burg, p} = cells;
|
||||||
|
|
||||||
|
const getBurgCoords = b => [burgs[b].x, burgs[b].y];
|
||||||
|
const getPathPoints = cells => cells.map(i => (Array.isArray(i) ? i : burg[i] ? getBurgCoords(burg[i]) : p[i]));
|
||||||
|
const getPath = segment => round(lineGen(getPathPoints(segment)), 1);
|
||||||
|
const getPathsHTML = (paths, type) => paths.map((path, i) => `<path id="${type}${i}" d="${getPath(path)}" />`).join("");
|
||||||
|
|
||||||
lineGen.curve(d3.curveCatmullRom.alpha(0.1));
|
lineGen.curve(d3.curveCatmullRom.alpha(0.1));
|
||||||
|
roads.html(getPathsHTML(main, "road"));
|
||||||
|
trails.html(getPathsHTML(small, "trail"));
|
||||||
|
|
||||||
// main routes
|
|
||||||
roads.selectAll("path").data(main).enter().append("path")
|
|
||||||
.attr("id", (d, i) => "road" + i)
|
|
||||||
.attr("d", d => round(lineGen(d.map(c => {
|
|
||||||
const b = cells.burg[c];
|
|
||||||
const x = b ? burgs[b].x : cells.p[c][0];
|
|
||||||
const y = b ? burgs[b].y : cells.p[c][1];
|
|
||||||
return [x, y];
|
|
||||||
})), 1));
|
|
||||||
|
|
||||||
// small routes
|
|
||||||
trails.selectAll("path").data(small).enter().append("path")
|
|
||||||
.attr("id", (d, i) => "trail" + i)
|
|
||||||
.attr("d", d => round(lineGen(d.map(c => {
|
|
||||||
const b = cells.burg[c];
|
|
||||||
const x = b ? burgs[b].x : cells.p[c][0];
|
|
||||||
const y = b ? burgs[b].y : cells.p[c][1];
|
|
||||||
return [x, y];
|
|
||||||
})), 1));
|
|
||||||
|
|
||||||
// ocean routes
|
|
||||||
lineGen.curve(d3.curveBundle.beta(1));
|
lineGen.curve(d3.curveBundle.beta(1));
|
||||||
searoutes.selectAll("path").data(ocean).enter().append("path")
|
searoutes.html(getPathsHTML(water, "searoute"));
|
||||||
.attr("id", (d, i) => "searoute" + i)
|
|
||||||
.attr("d", d => round(lineGen(d.map(c => {
|
|
||||||
const b = cells.burg[c];
|
|
||||||
const x = b ? burgs[b].x : cells.p[c][0];
|
|
||||||
const y = b ? burgs[b].y : cells.p[c][1];
|
|
||||||
return [x, y];
|
|
||||||
})), 1));
|
|
||||||
|
|
||||||
TIME && console.timeEnd("drawRoutes");
|
TIME && console.timeEnd("drawRoutes");
|
||||||
}
|
};
|
||||||
|
|
||||||
const regenerate = function () {
|
const regenerate = function () {
|
||||||
routes.selectAll("path").remove();
|
routes.selectAll("path").remove();
|
||||||
|
|
@ -142,9 +152,9 @@
|
||||||
pack.cells.crossroad = new Uint16Array(pack.cells.i.length);
|
pack.cells.crossroad = new Uint16Array(pack.cells.i.length);
|
||||||
const main = getRoads();
|
const main = getRoads();
|
||||||
const small = getTrails();
|
const small = getTrails();
|
||||||
const ocean = getSearoutes();
|
const water = getSearoutes();
|
||||||
draw(main, small, ocean);
|
draw(main, small, water);
|
||||||
}
|
};
|
||||||
|
|
||||||
return {getRoads, getTrails, getSearoutes, draw, regenerate};
|
return {getRoads, getTrails, getSearoutes, draw, regenerate};
|
||||||
|
|
||||||
|
|
@ -152,11 +162,14 @@
|
||||||
function findLandPath(start, exit = null, toRoad = null) {
|
function findLandPath(start, exit = null, toRoad = null) {
|
||||||
const cells = pack.cells;
|
const cells = pack.cells;
|
||||||
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
|
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
|
||||||
const cost = [], from = [];
|
const cost = [],
|
||||||
|
from = [];
|
||||||
queue.queue({e: start, p: 0});
|
queue.queue({e: start, p: 0});
|
||||||
|
|
||||||
while (queue.length) {
|
while (queue.length) {
|
||||||
const next = queue.dequeue(), n = next.e, p = next.p;
|
const next = queue.dequeue(),
|
||||||
|
n = next.e,
|
||||||
|
p = next.p;
|
||||||
if (toRoad && cells.road[n]) return [from, n];
|
if (toRoad && cells.road[n]) return [from, n];
|
||||||
|
|
||||||
for (const c of cells.c[n]) {
|
for (const c of cells.c[n]) {
|
||||||
|
|
@ -175,7 +188,6 @@
|
||||||
cost[c] = totalCost;
|
cost[c] = totalCost;
|
||||||
queue.queue({e: c, p: totalCost});
|
queue.queue({e: c, p: totalCost});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return [from, exit];
|
return [from, exit];
|
||||||
}
|
}
|
||||||
|
|
@ -183,7 +195,9 @@
|
||||||
function restorePath(start, end, type, from) {
|
function restorePath(start, end, type, from) {
|
||||||
const cells = pack.cells;
|
const cells = pack.cells;
|
||||||
const path = []; // to store all segments;
|
const path = []; // to store all segments;
|
||||||
let segment = [], current = end, prev = end;
|
let segment = [],
|
||||||
|
current = end,
|
||||||
|
prev = end;
|
||||||
const score = type === "main" ? 5 : 1; // to incrade road score at cell
|
const score = type === "main" ? 5 : 1; // to incrade road score at cell
|
||||||
|
|
||||||
if (type === "ocean" || !cells.road[prev]) segment.push(end);
|
if (type === "ocean" || !cells.road[prev]) segment.push(end);
|
||||||
|
|
@ -197,8 +211,14 @@
|
||||||
if (segment.length) {
|
if (segment.length) {
|
||||||
segment.push(current);
|
segment.push(current);
|
||||||
path.push(segment);
|
path.push(segment);
|
||||||
if (segment[0] !== end) {cells.road[segment[0]] += score; cells.crossroad[segment[0]] += score;}
|
if (segment[0] !== end) {
|
||||||
if (current !== start) {cells.road[current] += score; cells.crossroad[current] += score;}
|
cells.road[segment[0]] += score;
|
||||||
|
cells.crossroad[segment[0]] += score;
|
||||||
|
}
|
||||||
|
if (current !== start) {
|
||||||
|
cells.road[current] += score;
|
||||||
|
cells.crossroad[current] += score;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
segment = [];
|
segment = [];
|
||||||
prev = current;
|
prev = current;
|
||||||
|
|
@ -218,29 +238,34 @@
|
||||||
|
|
||||||
// find water paths
|
// find water paths
|
||||||
function findOceanPath(start, exit = null, toRoute = null) {
|
function findOceanPath(start, exit = null, toRoute = null) {
|
||||||
const cells = pack.cells, temp = grid.cells.temp;
|
const cells = pack.cells,
|
||||||
|
temp = grid.cells.temp;
|
||||||
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
|
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
|
||||||
const cost = [], from = [];
|
const cost = [],
|
||||||
|
from = [];
|
||||||
queue.queue({e: start, p: 0});
|
queue.queue({e: start, p: 0});
|
||||||
|
|
||||||
while (queue.length) {
|
while (queue.length) {
|
||||||
const next = queue.dequeue(), n = next.e, p = next.p;
|
const next = queue.dequeue(),
|
||||||
|
n = next.e,
|
||||||
|
p = next.p;
|
||||||
if (toRoute && n !== start && cells.road[n]) return [from, n, true];
|
if (toRoute && n !== start && cells.road[n]) return [from, n, true];
|
||||||
|
|
||||||
for (const c of cells.c[n]) {
|
for (const c of cells.c[n]) {
|
||||||
if (c === exit) {from[c] = n; return [from, exit, true];}
|
if (c === exit) {
|
||||||
|
from[c] = n;
|
||||||
|
return [from, exit, true];
|
||||||
|
}
|
||||||
if (cells.h[c] >= 20) continue; // ignore land cells
|
if (cells.h[c] >= 20) continue; // ignore land cells
|
||||||
if (temp[cells.g[c]] <= -5) continue; // ignore cells with term <= -5
|
if (temp[cells.g[c]] <= -5) continue; // ignore cells with term <= -5
|
||||||
const dist2 = (cells.p[c][1] - cells.p[n][1]) ** 2 + (cells.p[c][0] - cells.p[n][0]) ** 2;
|
const dist2 = (cells.p[c][1] - cells.p[n][1]) ** 2 + (cells.p[c][0] - cells.p[n][0]) ** 2;
|
||||||
const totalCost = p + (cells.road[c] ? 1 + dist2 / 2 : dist2 + (cells.t[c] ? 1 : 100));
|
const totalCost = p + (cells.road[c] ? 1 + dist2 / 2 : dist2 + (cells.t[c] ? 1 : 100));
|
||||||
|
|
||||||
if (from[c] || totalCost >= cost[c]) continue;
|
if (from[c] || totalCost >= cost[c]) continue;
|
||||||
from[c] = n, cost[c] = totalCost;
|
(from[c] = n), (cost[c] = totalCost);
|
||||||
queue.queue({e: c, p: totalCost});
|
queue.queue({e: c, p: totalCost});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return [from, exit, false];
|
return [from, exit, false];
|
||||||
}
|
}
|
||||||
|
});
|
||||||
})));
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue