975 lines
46 KiB
JavaScript
975 lines
46 KiB
JavaScript
import { INF, MAP_H, MAP_W, SIZE, MinHeap, clamp, indexOf, inside, weightedScore, xyOf } from "./mapUtils.js";
|
|
|
|
function neighbors8(x, y) {
|
|
const out = [];
|
|
for (let dy = -1; dy <= 1; dy++) {
|
|
for (let dx = -1; dx <= 1; dx++) {
|
|
if (dx === 0 && dy === 0) continue;
|
|
const nx = x + dx;
|
|
const ny = y + dy;
|
|
if (inside(nx, ny)) out.push([nx, ny, Math.hypot(dx, dy)]);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function neighbors4(x, y) {
|
|
const out = [];
|
|
for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
|
|
const nx = x + dx;
|
|
const ny = y + dy;
|
|
if (inside(nx, ny)) out.push([nx, ny, 1]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function generateAdminRegions(centers, prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, populationDensity, landuse) {
|
|
const adminId = new Int16Array(SIZE);
|
|
adminId.fill(-1);
|
|
const dist = new Float32Array(SIZE);
|
|
dist.fill(INF);
|
|
const heap = new MinHeap();
|
|
|
|
centers.forEach((center, regionId) => {
|
|
const i = indexOf(center.x, center.y);
|
|
dist[i] = 0;
|
|
adminId[i] = regionId;
|
|
heap.push({ i, f: 0, regionId });
|
|
});
|
|
|
|
let guard = 0;
|
|
while (heap.length > 0 && guard++ < SIZE * 12) {
|
|
const current = heap.pop();
|
|
if (!current) continue;
|
|
const curIndex = current.i;
|
|
const curRegion = adminId[curIndex];
|
|
if (curRegion < 0 || current.f > dist[curIndex] + 1e-5) continue;
|
|
|
|
const [cx, cy] = xyOf(curIndex);
|
|
for (const [nx, ny, step] of neighbors8(cx, cy)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
|
|
const ridgeBarrier = Math.max(ridgeField[ni], ridgeField[curIndex]);
|
|
const riverBarrier = Math.max(river[ni], river[curIndex]);
|
|
const highDivide = Math.max(elevation[ni], elevation[curIndex]);
|
|
const watershedBarrier = ridgeBarrier * (27.0 + Math.max(0, highDivide - 0.46) * 46.0);
|
|
const ridgePenalty = Math.max(0, highDivide - 0.36) * 16.0 + Math.abs(elevation[ni] - elevation[curIndex]) * 12.4 + watershedBarrier;
|
|
const slopePenalty = slope[ni] * 10.6;
|
|
const valleyBarrier = valleyField[ni] > 0.50 ? valleyField[ni] * (riverBarrier > 0.16 ? 7.2 : 2.6) : 0;
|
|
const riverPenalty = riverBarrier > 0.7 ? 22.0 : riverBarrier > 0.42 ? 14.8 : riverBarrier > 0.22 ? 7.4 : riverBarrier > 0.12 ? 2.2 : 0;
|
|
const urbanContinuityBonus = (landuse[ni] >= 2 && landuse[ni] <= 4 && populationDensity[ni] > 0.20) ? 1.65 : 0;
|
|
const valleyLocalityBonus = valleyField[ni] * 0.16;
|
|
const stepCost = Math.max(0.25, 0.72 + ridgePenalty + slopePenalty + riverPenalty + valleyBarrier - valleyLocalityBonus - urbanContinuityBonus) * step;
|
|
const nextDist = dist[curIndex] + stepCost;
|
|
|
|
if (nextDist < dist[ni]) {
|
|
dist[ni] = nextDist;
|
|
adminId[ni] = curRegion;
|
|
heap.push({ i: ni, f: nextDist, regionId: curRegion });
|
|
}
|
|
}
|
|
}
|
|
|
|
return adminId;
|
|
}
|
|
|
|
function terrainBoundaryStrength(i, elevation, slope, river, ridgeField, valleyField) {
|
|
return clamp(weightedScore([
|
|
[ridgeField[i], 3.15],
|
|
[river[i], 2.45],
|
|
[valleyField[i], 0.62],
|
|
[slope[i], 1.06],
|
|
[Math.max(0, elevation[i] - 0.5), 1.18],
|
|
]));
|
|
}
|
|
|
|
export function smoothAdminRegionsTerrainAware(adminId, prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, populationDensity, landuse, passes = 5) {
|
|
let current = new Int16Array(adminId);
|
|
for (let pass = 0; pass < passes; pass++) {
|
|
const next = new Int16Array(current);
|
|
for (let y = 1; y < MAP_H - 1; y++) {
|
|
for (let x = 1; x < MAP_W - 1; x++) {
|
|
const i = indexOf(x, y);
|
|
const own = current[i];
|
|
if (!prefectureMask[i] || sea[i] || own < 0) continue;
|
|
const urbanCell = (landuse[i] >= 2 && landuse[i] <= 4) || landuse[i] === 7 || populationDensity[i] > 0.24;
|
|
const barrier = terrainBoundaryStrength(i, elevation, slope, river, ridgeField, valleyField);
|
|
if (barrier > 0.62 || urbanCell) continue;
|
|
|
|
const counts = new Map();
|
|
let ownCount = 0;
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
const id = current[ni];
|
|
if (id < 0) continue;
|
|
const weight = terrainBoundaryStrength(ni, elevation, slope, river, ridgeField, valleyField) > 0.72 ? 0.45 : 1;
|
|
counts.set(id, (counts.get(id) || 0) + weight);
|
|
if (id === own) ownCount += weight;
|
|
}
|
|
let bestId = own;
|
|
let best = ownCount;
|
|
for (const [id, score] of counts) if (score > best) { best = score; bestId = id; }
|
|
if (bestId !== own && (best >= 4.2 || ownCount <= 2.1)) next[i] = bestId;
|
|
}
|
|
}
|
|
current = next;
|
|
}
|
|
adminId.set(current);
|
|
}
|
|
|
|
export function lockSmallUrbanComponentsToMunicipality(adminId, prefectureMask, sea, landuse, populationDensity, maxCells = 360) {
|
|
const seen = new Uint8Array(SIZE);
|
|
const queue = [];
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (seen[i] || !prefectureMask[i] || sea[i]) continue;
|
|
const isUrbanStart = (landuse[i] >= 2 && landuse[i] <= 4) || landuse[i] === 7 || landuse[i] === 8 || populationDensity[i] > 0.20;
|
|
if (!isUrbanStart) continue;
|
|
const component = [];
|
|
queue.length = 0;
|
|
queue.push(i);
|
|
seen[i] = 1;
|
|
for (let q = 0; q < queue.length; q++) {
|
|
const cur = queue[q];
|
|
component.push(cur);
|
|
const [x, y] = xyOf(cur);
|
|
for (const [nx, ny] of neighbors8(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (seen[ni] || !prefectureMask[ni] || sea[ni]) continue;
|
|
const isUrban = (landuse[ni] >= 2 && landuse[ni] <= 4) || landuse[ni] === 7 || landuse[ni] === 8 || populationDensity[ni] > 0.20;
|
|
if (!isUrban) continue;
|
|
seen[ni] = 1;
|
|
queue.push(ni);
|
|
}
|
|
}
|
|
if (component.length === 0 || component.length > maxCells) continue;
|
|
const counts = new Map();
|
|
for (const ci of component) {
|
|
const id = adminId[ci];
|
|
if (id >= 0) counts.set(id, (counts.get(id) || 0) + 1 + populationDensity[ci]);
|
|
}
|
|
let bestId = -1;
|
|
let best = -1;
|
|
for (const [id, score] of counts) if (score > best) { best = score; bestId = id; }
|
|
if (bestId >= 0) for (const ci of component) adminId[ci] = bestId;
|
|
}
|
|
}
|
|
|
|
export function mergeTinyMunicipalities(adminId, prefectureMask, sea, populationDensity, modernCities = [], minArea = 320, options = {}) {
|
|
const area = new Map();
|
|
const pop = new Map();
|
|
const adjacency = new Map();
|
|
const cityMunicipalities = new Set();
|
|
for (const city of modernCities || []) {
|
|
if (!inside(city.x, city.y)) continue;
|
|
const id = adminId[indexOf(city.x, city.y)];
|
|
if (id < 0) continue;
|
|
if (options.protectAllModernCities !== false || city.isPrefecturalCapital || (city.population || 0) >= (options.majorCityPopulationThreshold || 120000)) cityMunicipalities.add(id);
|
|
}
|
|
for (const point of options.protectedPoints || []) {
|
|
if (!point || !inside(point.x, point.y)) continue;
|
|
const id = adminId[indexOf(point.x, point.y)];
|
|
if (id >= 0) cityMunicipalities.add(id);
|
|
}
|
|
const satelliteByAdmin = new Map();
|
|
for (const sat of options.satelliteCities || []) {
|
|
if (!sat || !inside(sat.x, sat.y)) continue;
|
|
const id = adminId[indexOf(sat.x, sat.y)];
|
|
if (id < 0) continue;
|
|
if (!satelliteByAdmin.has(id)) satelliteByAdmin.set(id, []);
|
|
satelliteByAdmin.get(id).push(sat);
|
|
}
|
|
const satelliteStats = options.satelliteStats || null;
|
|
|
|
for (let y = 0; y < MAP_H; y++) {
|
|
for (let x = 0; x < MAP_W; x++) {
|
|
const i = indexOf(x, y);
|
|
if (!prefectureMask[i] || sea[i]) continue;
|
|
const id = adminId[i];
|
|
if (id < 0) continue;
|
|
area.set(id, (area.get(id) || 0) + 1);
|
|
pop.set(id, (pop.get(id) || 0) + populationDensity[i]);
|
|
for (const [nx, ny] of [[x + 1, y], [x, y + 1]]) {
|
|
if (!inside(nx, ny)) continue;
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
const other = adminId[ni];
|
|
if (other < 0 || other === id) continue;
|
|
const key = id < other ? `${id}:${other}` : `${other}:${id}`;
|
|
adjacency.set(key, (adjacency.get(key) || 0) + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
const mergeTarget = new Map();
|
|
for (const [id, cells] of area) {
|
|
const score = cells + (pop.get(id) || 0) * 16;
|
|
if (cells >= minArea || cityMunicipalities.has(id)) continue;
|
|
const satellites = satelliteByAdmin.get(id) || [];
|
|
const protectedSatellite = satellites.some((sat) => {
|
|
const minSatelliteArea = sat.satelliteMinArea || options.satelliteMinArea || 110;
|
|
return sat.municipalityClass === "independentSatelliteMunicipality" && (
|
|
cells >= minSatelliteArea ||
|
|
(sat.population || 0) >= (options.satelliteIndependentPopulationThreshold || 60000) ||
|
|
(sat.distinctUrbanComponentArea || 0) >= 80 ||
|
|
sat.separatedByBarrier
|
|
);
|
|
});
|
|
if (protectedSatellite) continue;
|
|
let bestNeighbor = -1;
|
|
let bestScore = -1;
|
|
for (const [key, border] of adjacency) {
|
|
const [a, b] = key.split(":").map(Number);
|
|
if (a !== id && b !== id) continue;
|
|
const other = a === id ? b : a;
|
|
const parentBias = satellites.some((sat) => inside(sat.parentX ?? -1, sat.parentY ?? -1) && adminId[indexOf(sat.parentX, sat.parentY)] === other) ? 26 : 0;
|
|
const ruralBias = satellites.some((sat) => sat.municipalityClass === "smallTownAttachedToRuralMunicipality") ? Math.min(12, (area.get(other) || 0) * 0.01) : 0;
|
|
const candidate = border * 3 + (area.get(other) || 0) * 0.012 + (pop.get(other) || 0) * 0.24 + parentBias + ruralBias;
|
|
if (candidate > bestScore) { bestScore = candidate; bestNeighbor = other; }
|
|
}
|
|
if (bestNeighbor >= 0 && (area.get(bestNeighbor) || 0) >= score * 0.35) {
|
|
mergeTarget.set(id, bestNeighbor);
|
|
if (satelliteStats && satellites.length) {
|
|
satelliteStats.satelliteMunicipalitiesMerged += satellites.length;
|
|
for (const sat of satellites) sat.mergedMunicipalityTarget = bestNeighbor;
|
|
}
|
|
}
|
|
}
|
|
if (mergeTarget.size === 0) return;
|
|
for (let i = 0; i < SIZE; i++) if (mergeTarget.has(adminId[i])) adminId[i] = mergeTarget.get(adminId[i]);
|
|
}
|
|
|
|
export function removeMunicipalExclaves(adminId, prefectureMask, sea, adminCenters = [], protectedPoints = [], maxIslandCells = 220) {
|
|
const protectedByAdmin = new Map();
|
|
for (const p of [...adminCenters, ...protectedPoints]) {
|
|
if (!p || !inside(p.x, p.y)) continue;
|
|
const id = adminId[indexOf(p.x, p.y)];
|
|
if (id < 0) continue;
|
|
if (!protectedByAdmin.has(id)) protectedByAdmin.set(id, new Set());
|
|
protectedByAdmin.get(id).add(indexOf(p.x, p.y));
|
|
}
|
|
|
|
const ids = new Set();
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i] && adminId[i] >= 0) ids.add(adminId[i]);
|
|
|
|
const globalSeen = new Uint8Array(SIZE);
|
|
const queue = [];
|
|
for (const id of ids) {
|
|
const components = [];
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (globalSeen[i] || adminId[i] !== id || !prefectureMask[i] || sea[i]) continue;
|
|
const comp = [];
|
|
let hasProtected = protectedByAdmin.get(id)?.has(i) || false;
|
|
queue.length = 0;
|
|
queue.push(i);
|
|
globalSeen[i] = 1;
|
|
for (let q = 0; q < queue.length; q++) {
|
|
const cur = queue[q];
|
|
comp.push(cur);
|
|
if (protectedByAdmin.get(id)?.has(cur)) hasProtected = true;
|
|
const [x, y] = xyOf(cur);
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (globalSeen[ni] || adminId[ni] !== id || !prefectureMask[ni] || sea[ni]) continue;
|
|
globalSeen[ni] = 1;
|
|
queue.push(ni);
|
|
}
|
|
}
|
|
components.push({ cells: comp, hasProtected });
|
|
}
|
|
if (components.length <= 1) continue;
|
|
components.sort((a, b) => (b.hasProtected ? 1000000 : 0) + b.cells.length - ((a.hasProtected ? 1000000 : 0) + a.cells.length));
|
|
for (const component of components.slice(1)) {
|
|
const mainSize = components[0].cells.length;
|
|
if (component.hasProtected && component.cells.length > maxIslandCells && component.cells.length > mainSize * 0.42) continue;
|
|
if (component.cells.length > maxIslandCells && component.cells.length > mainSize * 0.36) continue;
|
|
const counts = new Map();
|
|
for (const ci of component.cells) {
|
|
const [x, y] = xyOf(ci);
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
const other = adminId[ni];
|
|
if (other >= 0 && other !== id) counts.set(other, (counts.get(other) || 0) + 1);
|
|
}
|
|
}
|
|
let target = -1;
|
|
let best = -1;
|
|
for (const [other, count] of counts) if (count > best) { best = count; target = other; }
|
|
if (target >= 0) for (const ci of component.cells) adminId[ci] = target;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function terrainBoundaryTargetScore(i, elevation, slope, river, ridgeField, valleyField, flowAccum, populationDensity, landuse) {
|
|
const urbanPenalty = urbanBoundaryPenalty(i, populationDensity, landuse);
|
|
const majorRiver = clamp(Math.max(river[i] - 0.32, 0) * 1.9 + Math.max(flowAccum[i] - 0.38, 0) * 0.75);
|
|
const minorStream = clamp(river[i] * 0.34 + flowAccum[i] * 0.18);
|
|
const ridgeDivide = clamp(ridgeField[i] * 1.55 + Math.max(0, elevation[i] - 0.54) * ridgeField[i] * 0.95);
|
|
const slopeBreak = clamp(slope[i] * 0.58 + Math.max(0, slope[i] - 0.32) * 0.68);
|
|
const highGround = Math.max(0, elevation[i] - 0.56) * 0.22;
|
|
const valleyFloorPenalty = valleyField[i] * (majorRiver > 0.34 ? -0.10 : -0.62);
|
|
return clamp(ridgeDivide + majorRiver * 0.88 + minorStream * 0.22 + slopeBreak + highGround + valleyFloorPenalty - urbanPenalty * 0.72);
|
|
}
|
|
|
|
function urbanBoundaryPenalty(i, populationDensity, landuse) {
|
|
const lu = landuse[i];
|
|
const core = lu === 3 ? 1.45 : lu === 2 ? 1.12 : lu === 4 ? 0.95 : lu === 7 ? 0.90 : lu === 8 ? 0.64 : lu === 5 || lu === 6 ? 0.48 : 0;
|
|
return clamp(core + populationDensity[i] * 1.35);
|
|
}
|
|
|
|
function isAdminBoundaryCell(labels, prefectureMask, sea, x, y, useEight = true) {
|
|
const i = indexOf(x, y);
|
|
const own = labels[i];
|
|
if (!prefectureMask[i] || sea[i] || own < 0) return false;
|
|
const neighbors = useEight ? neighbors8(x, y) : neighbors4(x, y);
|
|
for (const [nx, ny] of neighbors) {
|
|
const ni = indexOf(nx, ny);
|
|
if (prefectureMask[ni] && !sea[ni] && labels[ni] >= 0 && labels[ni] !== own) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function buildBoundaryBand(labels, prefectureMask, sea, radius = 5) {
|
|
const band = new Uint8Array(SIZE);
|
|
for (let y = 1; y < MAP_H - 1; y++) {
|
|
for (let x = 1; x < MAP_W - 1; x++) {
|
|
if (!isAdminBoundaryCell(labels, prefectureMask, sea, x, y, true)) continue;
|
|
for (let dy = -radius; dy <= radius; dy++) {
|
|
for (let dx = -radius; dx <= radius; dx++) {
|
|
if (Math.hypot(dx, dy) > radius) continue;
|
|
const nx = x + dx;
|
|
const ny = y + dy;
|
|
if (!inside(nx, ny)) continue;
|
|
const ni = indexOf(nx, ny);
|
|
if (prefectureMask[ni] && !sea[ni]) band[ni] = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return band;
|
|
}
|
|
|
|
function buildAdminProtectedMask(adminId, prefectureMask, sea, adminCenters = [], protectedPoints = [], populationDensity, landuse) {
|
|
const protectedMask = new Uint8Array(SIZE);
|
|
function protectDisk(p, radius) {
|
|
if (!p || !inside(p.x, p.y)) return;
|
|
const owner = adminId[indexOf(p.x, p.y)];
|
|
if (owner < 0) return;
|
|
const r = Math.ceil(radius);
|
|
for (let dy = -r; dy <= r; dy++) {
|
|
for (let dx = -r; dx <= r; dx++) {
|
|
if (Math.hypot(dx, dy) > radius) continue;
|
|
const x = p.x + dx;
|
|
const y = p.y + dy;
|
|
if (!inside(x, y)) continue;
|
|
const i = indexOf(x, y);
|
|
if (prefectureMask[i] && !sea[i] && adminId[i] === owner) protectedMask[i] = 1;
|
|
}
|
|
}
|
|
}
|
|
for (const center of adminCenters) protectDisk(center, 2.2);
|
|
for (const p of protectedPoints || []) protectDisk(p, p.population ? clamp(1.6 + Math.sqrt(p.population) / 520, 2.1, 6.0) : p.portClass ? 2.0 : 1.7);
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (prefectureMask[i] && !sea[i] && (landuse[i] === 3 || populationDensity[i] > 0.72)) protectedMask[i] = 1;
|
|
}
|
|
return protectedMask;
|
|
}
|
|
|
|
function localBoundaryEnergy(labels, i, candidateId, targetScore, centerDist, populationDensity, landuse, river, valleyField) {
|
|
const [x, y] = xyOf(i);
|
|
const oldId = labels[i];
|
|
let energy = centerDist[candidateId]?.[i] ?? 0;
|
|
let same4 = 0;
|
|
let diff4 = 0;
|
|
let diagDiff = 0;
|
|
|
|
for (const [nx, ny] of neighbors8(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
const neighborId = labels[ni];
|
|
if (neighborId < 0) continue;
|
|
const isCardinal = nx === x || ny === y;
|
|
const differs = neighborId !== candidateId;
|
|
if (isCardinal) {
|
|
if (differs) {
|
|
diff4++;
|
|
const boundaryTarget = (targetScore[i] + targetScore[ni]) * 0.5;
|
|
const urbanCut = (urbanBoundaryPenalty(i, populationDensity, landuse) + urbanBoundaryPenalty(ni, populationDensity, landuse)) * 0.5;
|
|
const minorValley = (valleyField[i] + valleyField[ni]) * 0.5 > 0.34 && Math.max(river[i], river[ni]) < 0.30 ? 0.72 : 0;
|
|
const dHere = centerDist[candidateId]?.[i] ?? 99;
|
|
const dThere = centerDist[neighborId]?.[i] ?? 99;
|
|
const weakBisectorPenalty = Math.abs(dHere - dThere) < 4.0 && boundaryTarget < 0.42 ? 0.62 : 0;
|
|
energy += 2.15 - boundaryTarget * 1.55 + urbanCut * 3.0 + minorValley + weakBisectorPenalty;
|
|
} else same4++;
|
|
} else if (differs) diagDiff++;
|
|
}
|
|
|
|
if (same4 === 0) energy += 5.2;
|
|
if (same4 === 1) energy += 1.7;
|
|
if (diff4 >= 3 && targetScore[i] < 0.42) energy += 1.25;
|
|
if (diagDiff >= 3 && diff4 >= 2 && targetScore[i] < 0.50) energy += 0.42;
|
|
if (candidateId !== oldId && centerDist[candidateId] && centerDist[oldId]) {
|
|
const drift = centerDist[candidateId][i] - centerDist[oldId][i];
|
|
if (drift > 0) energy += Math.min(0.9, drift * 0.012);
|
|
}
|
|
return energy;
|
|
}
|
|
|
|
function buildCenterDistanceFields(adminIds, adminCenters, prefectureMask, sea) {
|
|
const fields = [];
|
|
for (const id of adminIds) {
|
|
const center = adminCenters[id];
|
|
const field = new Float32Array(SIZE);
|
|
if (!center || !inside(center.x, center.y) || sea[indexOf(center.x, center.y)] || !prefectureMask[indexOf(center.x, center.y)]) field.fill(24);
|
|
else {
|
|
for (let y = 0; y < MAP_H; y++) for (let x = 0; x < MAP_W; x++) field[indexOf(x, y)] = Math.hypot(x - center.x, y - center.y);
|
|
}
|
|
fields[id] = field;
|
|
}
|
|
return fields;
|
|
}
|
|
|
|
function repairAdminTopology(adminId, prefectureMask, sea, adminCenters = [], targetScore = null, populationDensity = null, landuse = null) {
|
|
const ids = new Set();
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i] && adminId[i] >= 0) ids.add(adminId[i]);
|
|
const queue = [];
|
|
|
|
for (const id of ids) {
|
|
const seen = new Uint8Array(SIZE);
|
|
const components = [];
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (seen[i] || adminId[i] !== id || !prefectureMask[i] || sea[i]) continue;
|
|
const cells = [];
|
|
queue.length = 0;
|
|
queue.push(i);
|
|
seen[i] = 1;
|
|
for (let q = 0; q < queue.length; q++) {
|
|
const cur = queue[q];
|
|
cells.push(cur);
|
|
const [x, y] = xyOf(cur);
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (seen[ni] || adminId[ni] !== id || !prefectureMask[ni] || sea[ni]) continue;
|
|
seen[ni] = 1;
|
|
queue.push(ni);
|
|
}
|
|
}
|
|
components.push(cells);
|
|
}
|
|
if (components.length <= 1) continue;
|
|
|
|
const centerIndex = adminCenters[id] && inside(adminCenters[id].x, adminCenters[id].y) ? indexOf(adminCenters[id].x, adminCenters[id].y) : -1;
|
|
let keepIndex = centerIndex >= 0 ? components.findIndex((cells) => cells.includes(centerIndex)) : -1;
|
|
if (keepIndex < 0) {
|
|
let bestSize = -1;
|
|
for (let c = 0; c < components.length; c++) if (components[c].length > bestSize) { bestSize = components[c].length; keepIndex = c; }
|
|
}
|
|
|
|
for (let c = 0; c < components.length; c++) {
|
|
if (c === keepIndex) continue;
|
|
const counts = new Map();
|
|
for (const ci of components[c]) {
|
|
const [x, y] = xyOf(ci);
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
const other = adminId[ni];
|
|
if (other < 0 || other === id) continue;
|
|
const terrainFit = targetScore ? targetScore[ci] * 0.18 : 0;
|
|
const urbanFit = populationDensity && landuse ? (1 - urbanBoundaryPenalty(ci, populationDensity, landuse)) * 0.08 : 0;
|
|
counts.set(other, (counts.get(other) || 0) + 1 + terrainFit + urbanFit);
|
|
}
|
|
}
|
|
let target = -1;
|
|
let best = -1;
|
|
for (const [other, score] of counts) if (score > best) { best = score; target = other; }
|
|
if (target >= 0) for (const ci of components[c]) adminId[ci] = target;
|
|
}
|
|
}
|
|
}
|
|
|
|
function classifyLandscapeCell(i, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse) {
|
|
if (landuse[i] === 3 || populationDensity[i] > 0.70) return 1;
|
|
if ((landuse[i] >= 2 && landuse[i] <= 4) || landuse[i] === 7 || landuse[i] === 8 || populationDensity[i] > 0.24) return 2;
|
|
if (landuse[i] === 5 || landuse[i] === 6) return 3;
|
|
if ((river[i] > 0.50 && flowAccum[i] > 0.34) || flowAccum[i] > 0.68) return 4;
|
|
if (coastalLowland[i] > 0.42 && elevation[i] < 0.44) return 5;
|
|
if (basinField[i] > 0.38 && plain[i] > 0.26) return 6;
|
|
if (valleyField[i] > 0.42 && ridgeField[i] < 0.55) return 7;
|
|
if (ridgeField[i] > 0.54 || (ridgeField[i] > 0.40 && elevation[i] > 0.54)) return 8;
|
|
if (elevation[i] > 0.62 || slope[i] > 0.42) return 9;
|
|
if (landuse[i] === 0 || agriculture[i] > 0.45 || plain[i] > 0.48) return 10;
|
|
return 11;
|
|
}
|
|
|
|
export function buildNaturalBarrierScore(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, crestCrossingScore = null, plain = null, agriculture = null, populationDensity = null, landuse = null) {
|
|
const score = new Float32Array(SIZE);
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (!prefectureMask[i] || sea[i]) continue;
|
|
const urbanContinuity = populationDensity && landuse ? urbanBoundaryPenalty(i, populationDensity, landuse) : 0;
|
|
const majorRiver = clamp(Math.max(river[i] - 0.34, 0) * 1.95 + Math.max(flowAccum[i] - 0.42, 0) * 0.82);
|
|
const ridgeDivide = clamp(ridgeField[i] * 1.65 + Math.max(0, elevation[i] - 0.52) * ridgeField[i] * 1.05);
|
|
const crest = crestCrossingScore ? crestCrossingScore[i] : clamp(Math.max(0, elevation[i] - 0.55) * ridgeField[i] * 1.4 + slope[i] * ridgeField[i] * 0.8);
|
|
const basinRim = basinField ? clamp(Math.max(0, basinField[i] - 0.32) * Math.max(0, slope[i] - 0.18) * 1.15 + Math.max(0, ridgeField[i] - 0.34) * basinField[i] * 0.62) : 0;
|
|
const foothillBreak = clamp(Math.max(0, slope[i] - 0.30) * Math.max(ridgeField[i], Math.max(0, elevation[i] - 0.48)) * 0.82);
|
|
const livingCorridor = clamp((plain?.[i] || 0) * 0.34 + (agriculture?.[i] || 0) * 0.26 + valleyField[i] * (majorRiver > 0.34 ? 0.10 : 0.46) + coastalLowland[i] * 0.18);
|
|
score[i] = clamp(
|
|
ridgeDivide * 0.92 +
|
|
crest * 0.72 +
|
|
majorRiver * 0.86 +
|
|
basinRim * 0.54 +
|
|
foothillBreak * 0.48 +
|
|
terrainBoundaryTargetScore(i, elevation, slope, river, ridgeField, valleyField, flowAccum, populationDensity || score, landuse || score) * 0.38 -
|
|
livingCorridor * 0.50 -
|
|
urbanContinuity * 0.72
|
|
);
|
|
}
|
|
return score;
|
|
}
|
|
|
|
function canShareNaturalCompartment(a, b, classA, classB, barrier, river, flowAccum, valleyField, populationDensity, landuse) {
|
|
if (classA !== classB) {
|
|
const bothUrban = classA <= 3 && classB <= 3;
|
|
const bothLivingCorridor = [5, 6, 7, 10].includes(classA) && [5, 6, 7, 10].includes(classB);
|
|
if (!bothUrban && !bothLivingCorridor) return false;
|
|
}
|
|
const majorRiverEdge = Math.max(river[a], river[b]) > 0.58 || Math.max(flowAccum[a], flowAccum[b]) > 0.72;
|
|
const urbanEdge = ((landuse[a] >= 2 && landuse[a] <= 4) || landuse[a] === 7 || populationDensity[a] > 0.34) &&
|
|
((landuse[b] >= 2 && landuse[b] <= 4) || landuse[b] === 7 || populationDensity[b] > 0.34);
|
|
const valleyContinuity = (valleyField[a] + valleyField[b]) * 0.5 > 0.42 && !majorRiverEdge;
|
|
const threshold = urbanEdge ? 0.84 : valleyContinuity ? 0.76 : classA === 8 || classB === 8 ? 0.42 : 0.62;
|
|
return barrier < threshold && (!majorRiverEdge || urbanEdge);
|
|
}
|
|
|
|
function naturalGroupKey(unit) {
|
|
if (unit.classId <= 3) return `urban:${Math.round(unit.x / 10)}:${Math.round(unit.y / 10)}`;
|
|
if (unit.classId === 5) return `coast:${Math.round(unit.y / 8)}`;
|
|
if (unit.classId === 6) return `basin:${Math.round(unit.x / 12)}:${Math.round(unit.y / 12)}`;
|
|
if (unit.classId === 7) return `valley:${Math.round((unit.x + unit.y) / 12)}`;
|
|
if (unit.classId === 8 || unit.classId === 9) return `mountain:${Math.round(unit.x / 14)}:${Math.round(unit.y / 14)}`;
|
|
return `plain:${Math.round(unit.x / 14)}:${Math.round(unit.y / 14)}`;
|
|
}
|
|
|
|
export function buildNaturalCompartments(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, crestCrossingScore = null, plain = null, agriculture = null, populationDensity = null, landuse = null) {
|
|
const naturalBarrierScore = buildNaturalBarrierScore(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, crestCrossingScore, plain, agriculture, populationDensity, landuse);
|
|
const compartmentId = new Int32Array(SIZE);
|
|
compartmentId.fill(-1);
|
|
const cellClass = new Int16Array(SIZE);
|
|
cellClass.fill(-1);
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i]) cellClass[i] = classifyLandscapeCell(i, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse);
|
|
|
|
const compartments = [];
|
|
const queue = [];
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (cellClass[i] < 0 || compartmentId[i] >= 0) continue;
|
|
const id = compartments.length;
|
|
const startClass = cellClass[i];
|
|
const cells = [];
|
|
let sx = 0, sy = 0, pop = 0, urbanWeight = 0, ridgeExposure = 0, riverExposure = 0, coastalExposure = 0, basinIdentity = 0, valleyIdentity = 0;
|
|
queue.length = 0;
|
|
queue.push(i);
|
|
compartmentId[i] = id;
|
|
for (let q = 0; q < queue.length; q++) {
|
|
const cur = queue[q];
|
|
const [x, y] = xyOf(cur);
|
|
cells.push(cur);
|
|
sx += x; sy += y; pop += populationDensity[cur];
|
|
urbanWeight += urbanBoundaryPenalty(cur, populationDensity, landuse);
|
|
ridgeExposure += ridgeField[cur];
|
|
riverExposure += river[cur] + flowAccum[cur] * 0.45;
|
|
coastalExposure += coastalLowland[cur];
|
|
basinIdentity += basinField[cur];
|
|
valleyIdentity += valleyField[cur];
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (compartmentId[ni] >= 0 || cellClass[ni] < 0) continue;
|
|
const edgeBarrier = (naturalBarrierScore[cur] + naturalBarrierScore[ni]) * 0.5;
|
|
if (!canShareNaturalCompartment(cur, ni, startClass, cellClass[ni], edgeBarrier, river, flowAccum, valleyField, populationDensity, landuse)) continue;
|
|
compartmentId[ni] = id;
|
|
queue.push(ni);
|
|
}
|
|
}
|
|
const area = cells.length;
|
|
compartments.push({
|
|
id,
|
|
cells,
|
|
area,
|
|
x: sx / area,
|
|
y: sy / area,
|
|
classId: startClass,
|
|
dominantLandscapeClass: startClass,
|
|
population: pop,
|
|
urbanWeight: urbanWeight / area,
|
|
ridgeExposure: ridgeExposure / area,
|
|
riverExposure: riverExposure / area,
|
|
coastalExposure: coastalExposure / area,
|
|
basinIdentity: basinIdentity / area,
|
|
valleyIdentity: valleyIdentity / area,
|
|
centerIds: [],
|
|
adjacent: new Map(),
|
|
});
|
|
}
|
|
rebuildLandscapeUnitAdjacency(compartmentId, compartments, naturalBarrierScore, prefectureMask, sea);
|
|
mergeTinyLandscapeUnits(compartmentId, compartments, 12);
|
|
rebuildLandscapeUnitAdjacency(compartmentId, compartments, naturalBarrierScore, prefectureMask, sea);
|
|
return { compartmentId, compartments, naturalBarrierScore };
|
|
}
|
|
|
|
function buildLandscapeUnits(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse) {
|
|
const unitId = new Int32Array(SIZE);
|
|
unitId.fill(-1);
|
|
const cellClass = new Int16Array(SIZE);
|
|
cellClass.fill(-1);
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i]) cellClass[i] = classifyLandscapeCell(i, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse);
|
|
|
|
const units = [];
|
|
const queue = [];
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (cellClass[i] < 0 || unitId[i] >= 0) continue;
|
|
const id = units.length;
|
|
const klass = cellClass[i];
|
|
const cells = [];
|
|
let sx = 0, sy = 0, pop = 0, urbanWeight = 0, ridgeExposure = 0, riverExposure = 0;
|
|
queue.length = 0;
|
|
queue.push(i);
|
|
unitId[i] = id;
|
|
for (let q = 0; q < queue.length; q++) {
|
|
const cur = queue[q];
|
|
const [x, y] = xyOf(cur);
|
|
cells.push(cur);
|
|
sx += x; sy += y; pop += populationDensity[cur];
|
|
urbanWeight += urbanBoundaryPenalty(cur, populationDensity, landuse);
|
|
ridgeExposure += ridgeField[cur];
|
|
riverExposure += river[cur] + flowAccum[cur] * 0.45;
|
|
for (const [nx, ny] of neighbors4(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (unitId[ni] >= 0 || cellClass[ni] !== klass) continue;
|
|
unitId[ni] = id;
|
|
queue.push(ni);
|
|
}
|
|
}
|
|
units.push({ id, classId: klass, cells, area: cells.length, x: sx / cells.length, y: sy / cells.length, population: pop, urbanWeight: urbanWeight / cells.length, ridgeExposure: ridgeExposure / cells.length, riverExposure: riverExposure / cells.length, adjacent: new Map(), centerIds: [], owner: -1 });
|
|
}
|
|
|
|
const targetScore = new Float32Array(SIZE);
|
|
for (let i = 0; i < SIZE; i++) if (cellClass[i] >= 0) targetScore[i] = terrainBoundaryTargetScore(i, elevation, slope, river, ridgeField, valleyField, flowAccum, populationDensity, landuse);
|
|
rebuildLandscapeUnitAdjacency(unitId, units, targetScore, prefectureMask, sea);
|
|
mergeTinyLandscapeUnits(unitId, units, 10);
|
|
rebuildLandscapeUnitAdjacency(unitId, units, targetScore, prefectureMask, sea);
|
|
return { unitId, units, targetScore };
|
|
}
|
|
|
|
function rebuildLandscapeUnitAdjacency(unitId, units, targetScore, prefectureMask, sea) {
|
|
for (const unit of units) unit.adjacent = new Map();
|
|
for (let y = 0; y < MAP_H; y++) {
|
|
for (let x = 0; x < MAP_W; x++) {
|
|
const i = indexOf(x, y);
|
|
if (!prefectureMask[i] || sea[i]) continue;
|
|
const a = unitId[i];
|
|
if (a < 0 || !units[a] || units[a].area === 0) continue;
|
|
for (const [nx, ny] of [[x + 1, y], [x, y + 1]]) {
|
|
if (!inside(nx, ny)) continue;
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni]) continue;
|
|
const b = unitId[ni];
|
|
if (b < 0 || b === a || !units[b] || units[b].area === 0) continue;
|
|
const v = (targetScore[i] + targetScore[ni]) * 0.5;
|
|
const keyA = units[a].adjacent.get(b) || { count: 0, target: 0 };
|
|
keyA.count++; keyA.target += v; units[a].adjacent.set(b, keyA);
|
|
const keyB = units[b].adjacent.get(a) || { count: 0, target: 0 };
|
|
keyB.count++; keyB.target += v; units[b].adjacent.set(a, keyB);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function mergeTinyLandscapeUnits(unitId, units, minArea = 10) {
|
|
for (const unit of units) {
|
|
if (unit.area === 0 || unit.area >= minArea) continue;
|
|
let bestId = -1, bestScore = -INF;
|
|
for (const [otherId, edge] of unit.adjacent) {
|
|
const other = units[otherId];
|
|
if (!other || other.area === 0) continue;
|
|
const score = edge.count * 3 + (other.classId === unit.classId ? 8 : 0) + other.area * 0.01 - edge.target / Math.max(1, edge.count);
|
|
if (score > bestScore) { bestScore = score; bestId = otherId; }
|
|
}
|
|
if (bestId < 0) continue;
|
|
const target = units[bestId];
|
|
for (const i of unit.cells) { unitId[i] = bestId; target.cells.push(i); }
|
|
const totalArea = target.area + unit.area;
|
|
target.x = (target.x * target.area + unit.x * unit.area) / totalArea;
|
|
target.y = (target.y * target.area + unit.y * unit.area) / totalArea;
|
|
target.population += unit.population;
|
|
target.urbanWeight = (target.urbanWeight * target.area + unit.urbanWeight * unit.area) / totalArea;
|
|
target.ridgeExposure = (target.ridgeExposure * target.area + unit.ridgeExposure * unit.area) / totalArea;
|
|
target.riverExposure = (target.riverExposure * target.area + unit.riverExposure * unit.area) / totalArea;
|
|
target.area = totalArea;
|
|
unit.area = 0;
|
|
unit.cells = [];
|
|
}
|
|
}
|
|
|
|
function naturalOwnershipAffinity(unit, neighbor, edge) {
|
|
const boundaryTarget = edge.target / Math.max(1, edge.count);
|
|
const sameClass = unit.classId === neighbor.classId ? 1.0 : 0;
|
|
const sameGroup = naturalGroupKey(unit) === naturalGroupKey(neighbor) ? 1.1 : 0;
|
|
const bothUrban = unit.classId <= 3 && neighbor.classId <= 3;
|
|
const bothCorridor = [5, 6, 7, 10].includes(unit.classId) && [5, 6, 7, 10].includes(neighbor.classId);
|
|
const urbanContinuity = bothUrban ? 1.35 : (unit.urbanWeight + neighbor.urbanWeight) > 0.75 && Math.abs(unit.urbanWeight - neighbor.urbanWeight) < 0.35 ? 0.58 : 0;
|
|
const strongDividerPenalty = boundaryTarget * (edge.count > 2 ? 2.8 : 1.8);
|
|
return edge.count * 0.55 + sameClass + sameGroup + urbanContinuity + (bothCorridor ? 0.72 : 0) - strongDividerPenalty;
|
|
}
|
|
|
|
function averageFinalBorderBarrier(adminId, prefectureMask, sea, naturalBarrierScore) {
|
|
let sum = 0;
|
|
let count = 0;
|
|
for (let y = 1; y < MAP_H - 1; y++) {
|
|
for (let x = 1; x < MAP_W - 1; x++) {
|
|
const i = indexOf(x, y);
|
|
if (!prefectureMask[i] || sea[i] || adminId[i] < 0) continue;
|
|
for (const [nx, ny] of [[x + 1, y], [x, y + 1]]) {
|
|
const ni = indexOf(nx, ny);
|
|
if (!prefectureMask[ni] || sea[ni] || adminId[ni] < 0 || adminId[ni] === adminId[i]) continue;
|
|
sum += (naturalBarrierScore[i] + naturalBarrierScore[ni]) * 0.5;
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
return count ? sum / count : 0;
|
|
}
|
|
|
|
function weakVoronoiLikeRate(adminId, adminCenters, prefectureMask, sea, naturalBarrierScore) {
|
|
let weak = 0;
|
|
let total = 0;
|
|
for (let y = 1; y < MAP_H - 1; y++) {
|
|
for (let x = 1; x < MAP_W - 1; x++) {
|
|
const i = indexOf(x, y);
|
|
if (!prefectureMask[i] || sea[i] || adminId[i] < 0) continue;
|
|
for (const [nx, ny] of [[x + 1, y], [x, y + 1]]) {
|
|
const ni = indexOf(nx, ny);
|
|
const a = adminId[i], b = adminId[ni];
|
|
if (!prefectureMask[ni] || sea[ni] || a < 0 || b < 0 || a === b) continue;
|
|
total++;
|
|
const ca = adminCenters[a], cb = adminCenters[b];
|
|
if (!ca || !cb) continue;
|
|
const mx = (x + nx) * 0.5, my = (y + ny) * 0.5;
|
|
const nearBisector = Math.abs(Math.hypot(mx - ca.x, my - ca.y) - Math.hypot(mx - cb.x, my - cb.y)) < 4.0;
|
|
if (nearBisector && (naturalBarrierScore[i] + naturalBarrierScore[ni]) * 0.5 < 0.38) weak++;
|
|
}
|
|
}
|
|
}
|
|
return total ? weak / total : 0;
|
|
}
|
|
|
|
export function applyLandscapeUnitAdminPartition(adminId, prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse, adminCenters = []) {
|
|
const before = new Int16Array(adminId);
|
|
const initialNaturalBarrierScore = buildNaturalBarrierScore(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, null, plain, agriculture, populationDensity, landuse);
|
|
const beforeVoronoiLikeRate = weakVoronoiLikeRate(adminId, adminCenters, prefectureMask, sea, initialNaturalBarrierScore);
|
|
const { compartmentId, compartments, naturalBarrierScore } = buildNaturalCompartments(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, null, plain, agriculture, populationDensity, landuse);
|
|
if (compartments.length === 0) return;
|
|
for (let id = 0; id < adminCenters.length; id++) {
|
|
const center = adminCenters[id];
|
|
if (!center || !inside(center.x, center.y)) continue;
|
|
const unit = compartments[compartmentId[indexOf(center.x, center.y)]];
|
|
if (unit) unit.centerIds.push(id);
|
|
}
|
|
|
|
const owner = new Int16Array(compartments.length);
|
|
owner.fill(-1);
|
|
for (const unit of compartments) {
|
|
if (unit.area === 0 || unit.centerIds.length === 0) continue;
|
|
owner[unit.id] = unit.centerIds[0];
|
|
}
|
|
|
|
for (let pass = 0; pass < compartments.length + 4; pass++) {
|
|
let changed = 0;
|
|
for (const unit of compartments) {
|
|
if (!unit || unit.area === 0 || owner[unit.id] >= 0) continue;
|
|
let bestOwner = -1;
|
|
let bestScore = -INF;
|
|
for (const [neighborId, edge] of unit.adjacent) {
|
|
const neighborOwner = owner[neighborId];
|
|
if (neighborOwner < 0) continue;
|
|
const neighbor = compartments[neighborId];
|
|
if (!neighbor || neighbor.area === 0) continue;
|
|
const score = naturalOwnershipAffinity(unit, neighbor, edge) + Math.min(0.8, Math.sqrt(Math.max(1, neighbor.area)) * 0.018);
|
|
if (score > bestScore) { bestScore = score; bestOwner = neighborOwner; }
|
|
}
|
|
const accept = unit.classId <= 3 ? bestScore > -0.15 : unit.classId === 8 || unit.classId === 9 ? bestScore > -0.80 : bestScore > -0.45;
|
|
if (bestOwner >= 0 && accept) { owner[unit.id] = bestOwner; changed++; }
|
|
}
|
|
if (changed === 0) break;
|
|
}
|
|
|
|
for (const unit of compartments) {
|
|
if (!unit || unit.area === 0 || owner[unit.id] >= 0) continue;
|
|
let bestId = -1, bestScore = -INF;
|
|
for (let id = 0; id < adminCenters.length; id++) {
|
|
const center = adminCenters[id];
|
|
if (!center || !inside(center.x, center.y)) continue;
|
|
const centerComp = compartments[compartmentId[indexOf(center.x, center.y)]];
|
|
const sameGroup = centerComp && naturalGroupKey(centerComp) === naturalGroupKey(unit) ? 2.4 : 0;
|
|
const sameClass = centerComp && centerComp.classId === unit.classId ? 0.9 : 0;
|
|
const urbanFit = unit.urbanWeight > 0.55 && centerComp?.urbanWeight > 0.55 ? 1.2 : 0;
|
|
const d = Math.hypot(unit.x - center.x, unit.y - center.y);
|
|
const score = sameGroup + sameClass + urbanFit - d * 0.018 - unit.ridgeExposure * 0.18;
|
|
if (score > bestScore) { bestScore = score; bestId = id; }
|
|
}
|
|
owner[unit.id] = bestId >= 0 ? bestId : 0;
|
|
}
|
|
|
|
for (const unit of compartments) {
|
|
const assigned = owner[unit.id];
|
|
if (assigned >= 0) for (const i of unit.cells) adminId[i] = assigned;
|
|
}
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (!prefectureMask[i] || sea[i] || adminId[i] >= 0) continue;
|
|
const comp = compartments[compartmentId[i]];
|
|
adminId[i] = comp && owner[comp.id] >= 0 ? owner[comp.id] : 0;
|
|
}
|
|
for (let id = 0; id < adminCenters.length; id++) {
|
|
const center = adminCenters[id];
|
|
if (!center || !inside(center.x, center.y)) continue;
|
|
for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) {
|
|
if (Math.hypot(dx, dy) > 2) continue;
|
|
const x = center.x + dx, y = center.y + dy;
|
|
if (!inside(x, y)) continue;
|
|
const i = indexOf(x, y);
|
|
if (prefectureMask[i] && !sea[i]) adminId[i] = id;
|
|
}
|
|
}
|
|
repairAdminTopology(adminId, prefectureMask, sea, adminCenters, naturalBarrierScore, populationDensity, landuse);
|
|
let changedCells = 0;
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i] && before[i] !== adminId[i]) changedCells++;
|
|
const activeCompartments = compartments.filter((unit) => unit.area > 0);
|
|
applyLandscapeUnitAdminPartition.lastDebug = {
|
|
compartmentCount: activeCompartments.length,
|
|
averageCompartmentArea: activeCompartments.length ? activeCompartments.reduce((sum, unit) => sum + unit.area, 0) / activeCompartments.length : 0,
|
|
changedAfterNaturalCompartmentPartition: changedCells,
|
|
finalBorderNaturalBarrierAverage: averageFinalBorderBarrier(adminId, prefectureMask, sea, naturalBarrierScore),
|
|
voronoiLikeRateBefore: beforeVoronoiLikeRate,
|
|
voronoiLikeRateAfter: weakVoronoiLikeRate(adminId, adminCenters, prefectureMask, sea, naturalBarrierScore),
|
|
};
|
|
}
|
|
|
|
export function snapAdminBoundariesToTerrain(adminId, prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, flowAccum, populationDensity, landuse, adminCenters = [], protectedPoints = [], passes = 6) {
|
|
const targetScore = new Float32Array(SIZE);
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i]) targetScore[i] = terrainBoundaryTargetScore(i, elevation, slope, river, ridgeField, valleyField, flowAccum, populationDensity, landuse);
|
|
const protectedMask = buildAdminProtectedMask(adminId, prefectureMask, sea, adminCenters, protectedPoints, populationDensity, landuse);
|
|
const band = buildBoundaryBand(adminId, prefectureMask, sea, 5);
|
|
const adminIds = [...new Set([...adminId].filter((id) => id >= 0))];
|
|
const centerDist = buildCenterDistanceFields(adminIds, adminCenters, prefectureMask, sea);
|
|
let current = new Int16Array(adminId);
|
|
|
|
for (let pass = 0; pass < passes; pass++) {
|
|
const next = new Int16Array(current);
|
|
let changed = 0;
|
|
for (let y = 1; y < MAP_H - 1; y++) {
|
|
for (let x = 1; x < MAP_W - 1; x++) {
|
|
const i = indexOf(x, y);
|
|
const own = current[i];
|
|
if (!band[i] || protectedMask[i] || !prefectureMask[i] || sea[i] || own < 0) continue;
|
|
if (!isAdminBoundaryCell(current, prefectureMask, sea, x, y, true)) continue;
|
|
const candidates = new Set();
|
|
for (const [nx, ny] of neighbors8(x, y)) {
|
|
const ni = indexOf(nx, ny);
|
|
if (prefectureMask[ni] && !sea[ni] && current[ni] >= 0 && current[ni] !== own) candidates.add(current[ni]);
|
|
}
|
|
if (candidates.size === 0) continue;
|
|
const currentEnergy = localBoundaryEnergy(current, i, own, targetScore, centerDist, populationDensity, landuse, river, valleyField);
|
|
let bestId = own, bestEnergy = currentEnergy;
|
|
for (const candidate of candidates) {
|
|
const candidateEnergy = localBoundaryEnergy(current, i, candidate, targetScore, centerDist, populationDensity, landuse, river, valleyField);
|
|
const threshold = 0.18 + (targetScore[i] < 0.36 ? 0.16 : 0) + urbanBoundaryPenalty(i, populationDensity, landuse) * 0.25;
|
|
if (bestEnergy - candidateEnergy > threshold) { bestEnergy = candidateEnergy; bestId = candidate; }
|
|
}
|
|
if (bestId !== own) { next[i] = bestId; changed++; }
|
|
}
|
|
}
|
|
current = next;
|
|
if (changed === 0) break;
|
|
}
|
|
adminId.set(current);
|
|
repairAdminTopology(adminId, prefectureMask, sea, adminCenters, targetScore, populationDensity, landuse);
|
|
}
|
|
|
|
export function splitOversizedRuralMunicipalities(adminId, prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, plain, agriculture, populationDensity, landuse, adminCenters = [], settlements = []) {
|
|
const before = new Int16Array(adminId);
|
|
const area = new Map();
|
|
const lowland = new Map();
|
|
const mountain = new Map();
|
|
for (let i = 0; i < SIZE; i++) {
|
|
if (!prefectureMask[i] || sea[i] || adminId[i] < 0) continue;
|
|
const id = adminId[i];
|
|
area.set(id, (area.get(id) || 0) + 1);
|
|
const living = (plain[i] || 0) * 0.42 + (agriculture[i] || 0) * 0.28 + basinField[i] * 0.20 + coastalLowland[i] * 0.20 + valleyField[i] * 0.12;
|
|
const rough = ridgeField[i] * 0.54 + slope[i] * 0.36 + Math.max(0, elevation[i] - 0.58) * 0.38;
|
|
lowland.set(id, (lowland.get(id) || 0) + living);
|
|
mountain.set(id, (mountain.get(id) || 0) + rough);
|
|
}
|
|
const areas = [...area.values()].sort((a, b) => a - b);
|
|
const median = areas.length ? areas[Math.floor(areas.length / 2)] : 0;
|
|
if (!median) return { changedCells: 0, splitMunicipalities: 0 };
|
|
|
|
const { compartmentId, compartments, naturalBarrierScore } = buildNaturalCompartments(prefectureMask, sea, elevation, slope, river, ridgeField, valleyField, basinField, coastalLowland, flowAccum, null, plain, agriculture, populationDensity, landuse);
|
|
const unitOwner = new Int16Array(compartments.length);
|
|
unitOwner.fill(-1);
|
|
for (const unit of compartments) {
|
|
if (!unit || unit.area === 0) continue;
|
|
const counts = new Map();
|
|
for (const i of unit.cells) {
|
|
const id = adminId[i];
|
|
if (id >= 0) counts.set(id, (counts.get(id) || 0) + 1);
|
|
}
|
|
let bestId = -1, best = -1;
|
|
for (const [id, count] of counts) if (count > best) { best = count; bestId = id; }
|
|
unitOwner[unit.id] = bestId;
|
|
}
|
|
|
|
const adminCenterIndex = new Map();
|
|
for (let id = 0; id < adminCenters.length; id++) {
|
|
const c = adminCenters[id];
|
|
if (c && inside(c.x, c.y)) adminCenterIndex.set(id, indexOf(c.x, c.y));
|
|
}
|
|
|
|
let splitMunicipalities = 0;
|
|
for (const [id, cells] of area) {
|
|
const averageLowland = (lowland.get(id) || 0) / cells;
|
|
const averageMountain = (mountain.get(id) || 0) / cells;
|
|
if (cells < median * 2.25 || averageLowland < 0.28 || averageMountain > 0.44) continue;
|
|
const localSettlements = settlements.filter((p) => p && inside(p.x, p.y) && adminId[indexOf(p.x, p.y)] === id);
|
|
const meaningfulNodes = localSettlements.filter((p) => p.kind === "Satellite City" || p.kind === "New Town" || p.kind === "Market Town" || (p.population || 0) >= 30000);
|
|
if (meaningfulNodes.length < 2) continue;
|
|
let changedHere = 0;
|
|
for (const unit of compartments) {
|
|
if (!unit || unit.area === 0 || unitOwner[unit.id] !== id) continue;
|
|
const centerIndex = adminCenterIndex.get(id);
|
|
if (centerIndex >= 0 && unit.cells.includes(centerIndex)) continue;
|
|
if (unit.classId === 8 || unit.classId === 9) continue;
|
|
let bestNeighbor = -1;
|
|
let bestScore = -INF;
|
|
for (const [neighborId, edge] of unit.adjacent) {
|
|
const neighborOwner = unitOwner[neighborId];
|
|
if (neighborOwner < 0 || neighborOwner === id) continue;
|
|
const boundaryTarget = edge.target / Math.max(1, edge.count);
|
|
const neighbor = compartments[neighborId];
|
|
const nodePull = meaningfulNodes.reduce((best, p) => Math.max(best, 1 / (1 + Math.hypot(p.x - unit.x, p.y - unit.y) / 6)), 0);
|
|
const score = edge.count * 0.7 + boundaryTarget * 1.4 + nodePull * 1.2 - Math.max(0, (neighbor?.ridgeExposure || 0) - unit.ridgeExposure) * 0.35;
|
|
if (score > bestScore) { bestScore = score; bestNeighbor = neighborOwner; }
|
|
}
|
|
if (bestNeighbor < 0 || bestScore < 2.2) continue;
|
|
for (const ci of unit.cells) {
|
|
if (adminId[ci] === id) {
|
|
adminId[ci] = bestNeighbor;
|
|
changedHere++;
|
|
}
|
|
}
|
|
}
|
|
if (changedHere > Math.max(28, cells * 0.035)) splitMunicipalities++;
|
|
}
|
|
repairAdminTopology(adminId, prefectureMask, sea, adminCenters, naturalBarrierScore, populationDensity, landuse);
|
|
let changedCells = 0;
|
|
for (let i = 0; i < SIZE; i++) if (prefectureMask[i] && !sea[i] && before[i] !== adminId[i]) changedCells++;
|
|
return { changedCells, splitMunicipalities };
|
|
}
|