1
0
forked from mirrors/0ad

Implement StaticConstraint, refs #5011.

This allows random map scripts to evaluate a set of slow constraints
once and then only access a cache of the results later,
rather than reevaluating the constraints for every randomized
coordinate.
Remove an unused comment following d35d6cc9f9.

This was SVN commit r21401.
This commit is contained in:
elexis
2018-03-01 12:48:13 +00:00
parent 2be4293dff
commit 99797313fe
2 changed files with 26 additions and 1 deletions
@@ -528,7 +528,6 @@ function createTributaryRivers(riverAngle, riverCount, riverWidth, heightRiverbe
* @property {number} endWidth
* @property {number} [startHeight] - Fixed height to be used if the height at the location shouldn't be used.
* @property {number} [endHeight]
* @property {number} [maxHeight] - If given, do not touch any terrain above this height.
* @property {number} smoothWidth - Number of tiles at the passage border to apply height interpolation.
* @property {number} [tileClass] - Marks the passage with this tile class.
* @property {string} [terrain] - Texture to be painted on the passage area.
@@ -185,3 +185,29 @@ SlopeConstraint.prototype.allows = function(position)
{
return this.minSlope <= g_Map.getSlope(position) && g_Map.getSlope(position) <= this.maxSlope;
};
/**
* The StaticConstraint is used for performance improvements of existing Constraints.
* It is evaluated for the entire map when the Constraint is created.
* So when a createAreas or createObjectGroups call uses this, it can rely on the cache,
* rather than reevaluating it for every randomized coordinate.
* Account for the fact that the cache is never updated!
*/
function StaticConstraint(constraints)
{
let mapSize = g_Map.getSize();
let constraint = new AndConstraint(constraints);
this.cache = [];
for (let x = 0; x < mapSize; ++x)
{
this.cache[x] = new Uint8Array(mapSize);
for (let y = 0; y < mapSize; ++y)
this.cache[x][y] = constraint.allows(new Vector2D(x, y));
}
}
StaticConstraint.prototype.allows = function(position)
{
return !!this.cache[position.x][position.y];
};