From dc37bcde02f4cf14babf1cb217fa309543b836d8 Mon Sep 17 00:00:00 2001 From: elexis Date: Sat, 3 Mar 2018 13:46:57 +0000 Subject: [PATCH] Improve random map script performance by using squared distance where possible, refs #5011. RandomPathPlacer.js diff proposed and reviewed by FeXoR git-svn-id: https://svn.wildfiregames.com/public/ps/trunk@21425 3db68df2-c116-0410-a063-a993310a9797 --- binaries/data/mods/public/maps/random/red_sea.js | 2 +- binaries/data/mods/public/maps/random/rmgen-common/player.js | 8 ++++---- binaries/data/mods/public/maps/random/rmgen/Group.js | 2 +- binaries/data/mods/public/maps/random/rmgen/Object.js | 6 +++--- .../mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js | 4 ++-- .../maps/random/rmgen/placer/noncentered/RandomPathPlacer.js | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/binaries/data/mods/public/maps/random/red_sea.js b/binaries/data/mods/public/maps/random/red_sea.js index ae892a6a96..463b8462d5 100644 --- a/binaries/data/mods/public/maps/random/red_sea.js +++ b/binaries/data/mods/public/maps/random/red_sea.js @@ -41,7 +41,7 @@ g_Gaia.tree2 = "gaia/flora_tree_senegal_date_palm"; g_Gaia.tree3 = "gaia/flora_tree_fig"; g_Gaia.tree4 = "gaia/flora_tree_cretan_date_palm_tall"; g_Gaia.tree5 = "gaia/flora_tree_cretan_date_palm_short"; -g_Gaia.fruitBush = "gaia/flora_bush_grapes"; +g_Gaia.fruitBush = "gaia/flora_bush_berry_desert"; g_Decoratives.grass = "actor|props/flora/grass_field_dry_tall_b.xml"; g_Decoratives.grassShort = "actor|props/flora/grass_field_parched_short.xml"; g_Decoratives.rockLarge = "actor|geology/stone_desert_med.xml"; diff --git a/binaries/data/mods/public/maps/random/rmgen-common/player.js b/binaries/data/mods/public/maps/random/rmgen-common/player.js index 1cd611b045..e55551c1e5 100644 --- a/binaries/data/mods/public/maps/random/rmgen-common/player.js +++ b/binaries/data/mods/public/maps/random/rmgen-common/player.js @@ -626,7 +626,7 @@ function playerPlacementRandom(playerIDs, constraints = undefined) let resets = 0; let mapCenter = g_Map.getCenter(); - let playerMinDist = fractionToTiles(0.25); + let playerMinDistSquared = Math.square(fractionToTiles(0.25)); let borderDistance = fractionToTiles(0.08); let area = createArea(new MapBoundsPlacer(), undefined, new AndConstraint(constraints)); @@ -636,8 +636,8 @@ function playerPlacementRandom(playerIDs, constraints = undefined) let position = pickRandom(area.getPoints()); // Minimum distance between initial bases must be a quarter of the map diameter - if (locations.some(loc => loc.distanceTo(position) < playerMinDist) || - position.distanceTo(mapCenter) > mapCenter.x - borderDistance) + if (locations.some(loc => loc.distanceToSquared(position) < playerMinDistSquared) || + position.distanceToSquared(mapCenter) > Math.square(mapCenter.x - borderDistance)) { --i; ++attempts; @@ -652,7 +652,7 @@ function playerPlacementRandom(playerIDs, constraints = undefined) // Reduce minimum player distance progressively if (resets % 25 == 0) - playerMinDist *= 0.975; + playerMinDistSquared *= 0.95; // If we only pick bad locations, stop trying to place randomly if (resets == 500) diff --git a/binaries/data/mods/public/maps/random/rmgen/Group.js b/binaries/data/mods/public/maps/random/rmgen/Group.js index cfbea8e33f..d839f868ef 100644 --- a/binaries/data/mods/public/maps/random/rmgen/Group.js +++ b/binaries/data/mods/public/maps/random/rmgen/Group.js @@ -53,7 +53,7 @@ SimpleGroup.prototype.place = function(playerID, constraint) if (this.avoidSelf) avoidPositions = avoidPositions.concat(entitySpecs.map(entitySpec => ({ "position": entitySpec.position, - "distance": object.avoidDistance + "distanceSquared": object.avoidDistanceSquared }))); } diff --git a/binaries/data/mods/public/maps/random/rmgen/Object.js b/binaries/data/mods/public/maps/random/rmgen/Object.js index 13b9593b03..92bffde292 100644 --- a/binaries/data/mods/public/maps/random/rmgen/Object.js +++ b/binaries/data/mods/public/maps/random/rmgen/Object.js @@ -14,7 +14,7 @@ function SimpleObject(templateName, minCount, maxCount, minDistance, maxDistance this.maxDistance = maxDistance; this.minAngle = minAngle; this.maxAngle = maxAngle; - this.avoidDistance = avoidDistance; + this.avoidDistanceSquared = Math.square(avoidDistance); if (minCount > maxCount) throw new Error("SimpleObject: minCount should be less than or equal to maxCount"); @@ -42,8 +42,8 @@ SimpleObject.prototype.place = function(centerPosition, playerID, avoidPositions if (validTile(position) && (!avoidPositions || - entitySpecs.every(entSpec => entSpec.position.distanceTo(position) >= this.avoidDistance) && - avoidPositions.every(avoid => avoid.position.distanceTo(position) >= Math.max(this.avoidDistance, avoid.distance))) && + entitySpecs.every(entSpec => entSpec.position.distanceToSquared(position) >= this.avoidDistanceSquared) && + avoidPositions.every(avoid => avoid.position.distanceToSquared(position) >= Math.max(this.avoidDistanceSquared, avoid.distanceSquared))) && constraint.allows(position.clone().floor())) { entitySpecs.push({ diff --git a/binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js b/binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js index 6d3d4feaf6..58a5e92a3c 100644 --- a/binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js +++ b/binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js @@ -3,7 +3,7 @@ */ function DiskPlacer(radius, centerPosition = undefined) { - this.radius = radius; + this.radiusSquared = Math.square(radius); this.centerPosition = undefined; if (centerPosition) @@ -23,7 +23,7 @@ DiskPlacer.prototype.place = function(constraint) for (let y = 0; y < g_Map.getSize(); ++y) { let point = new Vector2D(x, y); - if (this.centerPosition.distanceTo(point) <= this.radius && constraint.allows(point)) + if (this.centerPosition.distanceToSquared(point) <= this.radiusSquared && constraint.allows(point)) points.push(point); } diff --git a/binaries/data/mods/public/maps/random/rmgen/placer/noncentered/RandomPathPlacer.js b/binaries/data/mods/public/maps/random/rmgen/placer/noncentered/RandomPathPlacer.js index 7b2ff6a0ea..5b6e22a0ef 100644 --- a/binaries/data/mods/public/maps/random/rmgen/placer/noncentered/RandomPathPlacer.js +++ b/binaries/data/mods/public/maps/random/rmgen/placer/noncentered/RandomPathPlacer.js @@ -7,7 +7,7 @@ function RandomPathPlacer(pathStart, pathEnd, pathWidth, offset, blended) { this.pathStart = Vector2D.add(pathStart, Vector2D.sub(pathEnd, pathStart).normalize().mult(offset)).round(); this.pathEnd = pathEnd; - this.offset = offset; + this.offsetSquared = Math.square(offset); this.blended = blended; this.diskPlacer = new DiskPlacer(pathWidth); this.maxPathLength = fractionToTiles(2); @@ -19,7 +19,7 @@ RandomPathPlacer.prototype.place = function(constraint) let points = []; let position = this.pathStart; - while (position.distanceTo(this.pathEnd) >= this.offset && pathLength++ < this.maxPathLength) + while (position.distanceToSquared(this.pathEnd) >= this.offsetSquared && pathLength++ < this.maxPathLength) { position.add( new Vector2D(1, 0).rotate( -- 2.11.4.GIT