From a8ba4945e910da4f4fa52173c61be1f28b218d28 Mon Sep 17 00:00:00 2001 From: Fabio Pedretti Date: Tue, 13 Oct 2026 08:45:26 +0200 Subject: [PATCH] Bail out early and guard against an empty start area Create the water search area first and return before computing the land search area if there is no water at all, so that work is not wasted. Also return if the land search area is empty. Previously pickRandom would have been called on an empty array and returned undefined, making getClosestPointTo fail on an undefined position. Cache the start points instead of calling getPoints() on every try. --- .../maps/random/rmgen-common/gaia_entities.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/binaries/data/mods/public/maps/random/rmgen-common/gaia_entities.js b/binaries/data/mods/public/maps/random/rmgen-common/gaia_entities.js index bd2f5869a8..8dc4520c94 100644 --- a/binaries/data/mods/public/maps/random/rmgen-common/gaia_entities.js +++ b/binaries/data/mods/public/maps/random/rmgen-common/gaia_entities.js @@ -282,12 +282,6 @@ function placeDocks(template, playerID, count, tileClassWater, tileClassDock, he { const mapCenter = g_Map.getCenter(); - g_Map.log("Marking dock search start area"); - const areaSearchStart = createArea( - new DiskPlacer(fractionToTiles(0.5) - 10, mapCenter), - undefined, - avoidClasses(tileClassWater, 6)); - g_Map.log("Marking dock search end area"); const areaSearchEnd = createArea( new DiskPlacer(fractionToTiles(0.5) - 10, mapCenter), @@ -297,13 +291,23 @@ function placeDocks(template, playerID, count, tileClassWater, tileClassDock, he if (!areaSearchEnd || !areaSearchEnd.getPoints().length) return; + g_Map.log("Marking dock search start area"); + const areaSearchStart = createArea( + new DiskPlacer(fractionToTiles(0.5) - 10, mapCenter), + undefined, + avoidClasses(tileClassWater, 6)); + + const startPoints = areaSearchStart?.getPoints(); + if (!startPoints?.length) + return; + // TODO: computing the exact intersection with the waterplane would both not require us to pass reasonable heights and be more precise const constraint = new AndConstraint(constraints); g_Map.log("Placing docks"); for (let i = 0; i < count; ++i) for (let tries = 0; tries < retryFactor; ++tries) { - const positionLand = pickRandom(areaSearchStart.getPoints()); + const positionLand = pickRandom(startPoints); const positionWaterLarge = areaSearchEnd.getClosestPointTo(positionLand); const positionDock = findLocationInDirectionBasedOnHeight(positionWaterLarge, positionLand, heightMin, heightMax, offset); if (!positionDock)