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.
This commit is contained in:
Fabio Pedretti authored and Phosit committed 2026-10-15 16:55:16 +02:00
1 parent 79c0a48ce3
commit a8ba4945e9
1 file changed
+11 -7
@@ -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)