mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-23 20:41:23 +01:00
findcell by coordinates
This commit is contained in:
parent
a84ccf062e
commit
38e6e2b3d9
1 changed files with 9 additions and 9 deletions
|
|
@ -317,18 +317,18 @@ window.Submap = (function () {
|
||||||
/* find the nearest cell accepted by filter f *and* having at
|
/* find the nearest cell accepted by filter f *and* having at
|
||||||
* least one *neighbor* fulfilling filter g, up to cell-distance `max`
|
* least one *neighbor* fulfilling filter g, up to cell-distance `max`
|
||||||
* returns [cellid, neighbor] tuple or undefined if no such cell.
|
* returns [cellid, neighbor] tuple or undefined if no such cell.
|
||||||
* accepts pointLike (object having .x and .y)
|
* accepts coordinates (x, y)
|
||||||
*/
|
*/
|
||||||
const findNearest = (f, g, max=3) => pointLike => {
|
const findNearest = (f, g, max=3) => (px,py) => {
|
||||||
const d2 = c => (pointLike.x-pack.cells.p[c][0])**2 + (pointLike.y-pack.cells.p[c][0])**2
|
const d2 = c => (px-pack.cells.p[c][0])**2 + (py-pack.cells.p[c][0])**2
|
||||||
const startCell = findCell(pointLike.x, pointLike.y);
|
const startCell = findCell(px, py);
|
||||||
const tested = new Set([startCell]); // ignore analyzed cells
|
const tested = new Set([startCell]); // ignore analyzed cells
|
||||||
const kernel = (cs, level) => {
|
const kernel = (cs, level) => {
|
||||||
const [bestf, bestg] = cs.filter(f).reduce(([cf, cg], c) => {
|
const [bestf, bestg] = cs.filter(f).reduce(([cf, cg], c) => {
|
||||||
const neighbors = pack.cells.c[c];
|
const neighbors = pack.cells.c[c];
|
||||||
const bestg = neighbors.filter(g).reduce((u, x) => d2(c)<d2(u)? c:x);
|
const betterg = neighbors.filter(g).reduce((u, x) => d2(x)<d2(u)? x:u);
|
||||||
if (cf === undefined) return [c, bestg];
|
if (cf === undefined) return [c, betterg];
|
||||||
return (bestg && d2(cf) < d2(c))? [c, bestg]: [cf, cg];
|
return (betterg && d2(cf) < d2(c))? [c, betterg]: [cf, cg];
|
||||||
}, [undefined, undefined]);
|
}, [undefined, undefined]);
|
||||||
if (bestf && bestg) return [bestf, bestg];
|
if (bestf && bestg) return [bestf, bestg];
|
||||||
|
|
||||||
|
|
@ -374,7 +374,7 @@ window.Submap = (function () {
|
||||||
searchFunc = findNearest(isFreeLand, nearCoast, 6);
|
searchFunc = findNearest(isFreeLand, nearCoast, 6);
|
||||||
|
|
||||||
if (searchFunc) {
|
if (searchFunc) {
|
||||||
const [newCell, neighbor] = searchFunc(b);
|
const [newCell, neighbor] = searchFunc(b.x, b.y);
|
||||||
if (!newCell) {
|
if (!newCell) {
|
||||||
WARN && console.warn(`Can not relocate Burg: ${b.name} sunk and destroyed. :-(`);
|
WARN && console.warn(`Can not relocate Burg: ${b.name} sunk and destroyed. :-(`);
|
||||||
b.cell = null;
|
b.cell = null;
|
||||||
|
|
@ -384,8 +384,8 @@ window.Submap = (function () {
|
||||||
DEBUG && console.log(`Moving ${b.name} from ${cityCell} to ${newCell} near ${neighbor}.`);
|
DEBUG && console.log(`Moving ${b.name} from ${cityCell} to ${newCell} near ${neighbor}.`);
|
||||||
[b.x, b.y] = b.port? getMiddlePoint(newCell, neighbor): cells.p[newCell];
|
[b.x, b.y] = b.port? getMiddlePoint(newCell, neighbor): cells.p[newCell];
|
||||||
if (b.port) b.port = cells.f[neighbor]; // copy feature number
|
if (b.port) b.port = cells.f[neighbor]; // copy feature number
|
||||||
if (b.port && !isWater(childMap, neighbor)) console.error('betrayal! negihbor must be water!', b);
|
|
||||||
b.cell = newCell;
|
b.cell = newCell;
|
||||||
|
if (b.port && !isWater(childMap, neighbor)) console.error('betrayal! negihbor must be water!', b);
|
||||||
} else {
|
} else {
|
||||||
b.cell = cityCell;
|
b.cell = cityCell;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue