From 99797313fe1da65403fdb02b6f1b975fbc6eb13c Mon Sep 17 00:00:00 2001 From: elexis Date: Thu, 1 Mar 2018 12:48:13 +0000 Subject: [PATCH] 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. --- .../maps/random/rmgen-common/gaia_terrain.js | 1 - .../public/maps/random/rmgen/Constraint.js | 26 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js b/binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js index 7bb26cc25b..c803777ae8 100644 --- a/binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js +++ b/binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js @@ -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. diff --git a/binaries/data/mods/public/maps/random/rmgen/Constraint.js b/binaries/data/mods/public/maps/random/rmgen/Constraint.js index 59346bd679..73e5e9e3af 100644 --- a/binaries/data/mods/public/maps/random/rmgen/Constraint.js +++ b/binaries/data/mods/public/maps/random/rmgen/Constraint.js @@ -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]; +};