1
1
forked from 0ad/0ad

Area getClosestPointTo helper function that allows dock placement on random maps without preknowledge about the water location.

This was SVN commit r21224.
This commit is contained in:
elexis 2018-02-16 18:17:30 +00:00
parent c91fd9c2c3
commit a30ab829f4
2 changed files with 26 additions and 14 deletions

View File

@ -117,7 +117,10 @@ g_Map.log("Marking shoreline");
var areaShoreline = createArea(
new MapBoundsPlacer(),
undefined,
new HeightConstraint(heightShorelineMin, heightShorelineMax));
[
new HeightConstraint(heightShorelineMin, heightShorelineMax),
new NearTileClassConstraint(g_TileClasses.water, 2)
]);
Engine.SetProgress(35);
g_Map.log("Marking land");
@ -174,19 +177,7 @@ g_Map.log("Creating docks");
for (let i = 0; i < scaleByMapSize(2, 4); ++i)
{
let positionLand = pickRandom(areaDockStart.points);
// Find closest point on the shoreline and face the resulting direction
let dockPosition = areaShoreline.points[0];
let shortestDistance = Infinity;
for (let positionShoreline of areaShoreline.points)
{
let currentDistance = positionLand.distanceToSquared(positionShoreline);
if (currentDistance < shortestDistance)
{
shortestDistance = currentDistance;
dockPosition = positionShoreline;
}
}
let dockPosition = areaShoreline.getClosestPointTo(positionLand);
if (!avoidClasses(g_TileClasses.mountain, scaleByMapSize(4, 6), g_TileClasses.dock, 10).allows(dockPosition))
{

View File

@ -12,3 +12,24 @@ Area.prototype.getID = function()
{
return this.id;
};
Area.prototype.getClosestPointTo = function(position)
{
if (!this.points.length)
return undefined;
let closestPoint = this.points[0];
let shortestDistance = Infinity;
for (let point of this.points)
{
let currentDistance = point.distanceToSquared(position);
if (currentDistance < shortestDistance)
{
shortestDistance = currentDistance;
closestPoint = point;
}
}
return closestPoint;
};