From 8585040f2743cf1a2fbc766a1bf1dd66f53a1174 Mon Sep 17 00:00:00 2001 From: elexis Date: Sat, 27 Jan 2018 04:37:00 +0000 Subject: [PATCH] Cleanup TileClass prototype and use vector arguments for countInRadius, countMembersInRadius, countNonMembersInRadius, refs #4992. This was SVN commit r21026. --- .../public/maps/random/ardennes_forest.js | 4 +- .../data/mods/public/maps/random/corsica.js | 13 ++-- .../data/mods/public/maps/random/oasis.js | 2 +- .../public/maps/random/pyrenean_sierra.js | 16 ++--- .../public/maps/random/rmgen/constraint.js | 8 +-- .../public/maps/random/rmgen/tileclass.js | 62 ++++++++----------- 6 files changed, 48 insertions(+), 57 deletions(-) diff --git a/binaries/data/mods/public/maps/random/ardennes_forest.js b/binaries/data/mods/public/maps/random/ardennes_forest.js index b2b4246235..87134bb3f0 100644 --- a/binaries/data/mods/public/maps/random/ardennes_forest.js +++ b/binaries/data/mods/public/maps/random/ardennes_forest.js @@ -260,11 +260,11 @@ for (var ix = 0; ix < mapSize; ix++) let position = new Vector2D(ix, iz); let h = g_Map.getHeight(position); - if (h > 15 && h < 45 && clPlayer.countMembersInRadius(ix, iz, 1) == 0) + if (h > 15 && h < 45 && clPlayer.countMembersInRadius(position, 1) == 0) explorablePoints.push(position); if (h > 35 && randBool(0.1) || - h < 15 && randBool(0.05) && clHillDeco.countMembersInRadius(ix, iz, 1) == 0) + h < 15 && randBool(0.05) && clHillDeco.countMembersInRadius(position, 1) == 0) placeObject(Vector2D.add(position, new Vector2D(1, 1).mult(randFloat(0, 1))), pickRandom(aTrees), 0, randomAngle()); } diff --git a/binaries/data/mods/public/maps/random/corsica.js b/binaries/data/mods/public/maps/random/corsica.js index 2a23d943e1..12cff6efb8 100644 --- a/binaries/data/mods/public/maps/random/corsica.js +++ b/binaries/data/mods/public/maps/random/corsica.js @@ -269,7 +269,7 @@ for (let mapX = 0; mapX < mapSize; ++mapX) for (let mapZ = 0; mapZ < mapSize; ++mapZ) { let position = new Vector2D(mapX, mapZ); - let terrain = getCosricaSardiniaTerrain(mapX, mapZ); + let terrain = getCosricaSardiniaTerrain(position); if (!terrain) continue; @@ -279,13 +279,12 @@ for (let mapX = 0; mapX < mapSize; ++mapX) clCliffs.add(position); } -function getCosricaSardiniaTerrain(mapX, mapZ) +function getCosricaSardiniaTerrain(position) { - let position = new Vector2D(mapX, mapZ); - let isWater = clWater.countMembersInRadius(mapX, mapZ, 3); - let isShore = clShore.countMembersInRadius(mapX, mapZ, 2); - let isPassage = clPassage.countMembersInRadius(mapX, mapZ, 2); - let isSettlement = clSettlement.countMembersInRadius(mapX, mapZ, 2); + let isWater = clWater.countMembersInRadius(position, 3); + let isShore = clShore.countMembersInRadius(position, 2); + let isPassage = clPassage.countMembersInRadius(position, 2); + let isSettlement = clSettlement.countMembersInRadius(position, 2); if (isSettlement) return undefined; diff --git a/binaries/data/mods/public/maps/random/oasis.js b/binaries/data/mods/public/maps/random/oasis.js index ee966abce4..e33ae86fc0 100644 --- a/binaries/data/mods/public/maps/random/oasis.js +++ b/binaries/data/mods/public/maps/random/oasis.js @@ -308,7 +308,7 @@ for (var sandx = 0; sandx < mapSize; sandx += 4) createObjectGroup(group, 0); } - if (clPassage.countMembersInRadius(sandx, sandz, 2)) + if (clPassage.countMembersInRadius(position, 2)) { if (randBool(0.4)) { diff --git a/binaries/data/mods/public/maps/random/pyrenean_sierra.js b/binaries/data/mods/public/maps/random/pyrenean_sierra.js index 3ad492b9c2..4feb2ad1c2 100644 --- a/binaries/data/mods/public/maps/random/pyrenean_sierra.js +++ b/binaries/data/mods/public/maps/random/pyrenean_sierra.js @@ -244,7 +244,7 @@ for (let ix = 1; ix < mapSize - 1; ++ix) for (let iz = 1; iz < mapSize - 1; ++iz) { let position = new Vector2D(ix, iz); - if (g_Map.validHeight(position) && clPyrenneans.countMembersInRadius(ix, iz, 1)) + if (g_Map.validHeight(position) && clPyrenneans.countMembersInRadius(position, 1)) { let height = g_Map.getHeight(position); let index = 1 / (1 + Math.max(0, height / 7)); @@ -280,7 +280,7 @@ for (let ix = 1; ix < mapSize - 1; ++ix) for (let iz = 1; iz < mapSize - 1; ++iz) { let position = new Vector2D(ix, iz); - if (g_Map.inMapBounds(position) && clPyrenneans.countMembersInRadius(ix, iz, 1)) + if (g_Map.inMapBounds(position) && clPyrenneans.countMembersInRadius(position, 1)) { let heightNeighbor = g_Map.getAverageHeight(position); let index = 1 / (1 + Math.max(0, (g_Map.getHeight(position) - 10) / 7)); @@ -303,7 +303,7 @@ for (let ix = 1; ix < mapSize - 1; ++ix) for (let iz = 1; iz < mapSize - 1; ++iz) { let position = new Vector2D(ix, iz); - if (!g_Map.inMapBounds(position) || !clWater.countMembersInRadius(ix, iz, smoothDist)) + if (!g_Map.inMapBounds(position) || !clWater.countMembersInRadius(position, smoothDist)) continue; let averageHeight = 0; let todivide = 0; @@ -360,26 +360,26 @@ for (let x = 0; x < mapSize; ++x) let height = g_Map.getHeight(position); let heightDiff = g_Map.getSlope(position); - if (clPyrenneans.countMembersInRadius(x, z, 2)) + if (clPyrenneans.countMembersInRadius(position, 2)) { let layer = terrainPerHeight.find(layer => height < layer.maxHeight); createTerrain(heightDiff > layer.steepness ? layer.terrainSteep : layer.terrainGround).place(position); } - let terrainShore = getShoreTerrain(height, heightDiff, x, z); + let terrainShore = getShoreTerrain(position, height, heightDiff); if (terrainShore) createTerrain(terrainShore).place(position); } -function getShoreTerrain(height, heightDiff, x, z) +function getShoreTerrain(position, height, heightDiff) { if (height <= -14) return tWater; - if (height <= -2 && clWater.countMembersInRadius(x, z, 2)) + if (height <= -2 && clWater.countMembersInRadius(position, 2)) return heightDiff < 2.5 ? tSand : tMidRangeCliffs; - if (height <= 0 && clWater.countMembersInRadius(x, z, 3)) + if (height <= 0 && clWater.countMembersInRadius(position, 3)) return heightDiff < 2.5 ? tSandTransition : tMidRangeCliffs; return undefined; diff --git a/binaries/data/mods/public/maps/random/rmgen/constraint.js b/binaries/data/mods/public/maps/random/rmgen/constraint.js index 1c83e6b869..4206128fe9 100644 --- a/binaries/data/mods/public/maps/random/rmgen/constraint.js +++ b/binaries/data/mods/public/maps/random/rmgen/constraint.js @@ -67,7 +67,7 @@ function AvoidTileClassConstraint(tileClass, distance) AvoidTileClassConstraint.prototype.allows = function(position) { - return this.tileClass.countMembersInRadius(position.x, position.y, this.distance) == 0; + return this.tileClass.countMembersInRadius(position, this.distance) == 0; }; /** @@ -81,7 +81,7 @@ function StayInTileClassConstraint(tileClass, distance) StayInTileClassConstraint.prototype.allows = function(position) { - return this.tileClass.countNonMembersInRadius(position.x, position.y, this.distance) == 0; + return this.tileClass.countNonMembersInRadius(position, this.distance) == 0; }; /** @@ -98,8 +98,8 @@ function BorderTileClassConstraint(tileClass, distanceInside, distanceOutside) BorderTileClassConstraint.prototype.allows = function(position) { - return this.tileClass.countMembersInRadius(position.x, position.y, this.distanceOutside) > 0 && - this.tileClass.countNonMembersInRadius(position.x, position.y, this.distanceInside) > 0; + return this.tileClass.countMembersInRadius(position, this.distanceOutside) > 0 && + this.tileClass.countNonMembersInRadius(position, this.distanceInside) > 0; }; /** diff --git a/binaries/data/mods/public/maps/random/rmgen/tileclass.js b/binaries/data/mods/public/maps/random/rmgen/tileclass.js index 5f21361e37..a515a63553 100644 --- a/binaries/data/mods/public/maps/random/rmgen/tileclass.js +++ b/binaries/data/mods/public/maps/random/rmgen/tileclass.js @@ -92,54 +92,46 @@ TileClass.prototype.remove = function(position) this.rangeCount[position.y].add(position.x, -1); }; -TileClass.prototype.countInRadius = function(cx, cy, radius, returnMembers) +TileClass.prototype.countInRadius = function(position, radius, returnMembers) { - var members = 0; - var nonMembers = 0; - var size = this.size; + let members = 0; + let nonMembers = 0; + let radius2 = Math.square(radius); - var ymax = cy+radius; - var radius2 = radius*radius; - - for (var y = cy-radius; y <= ymax; y++) + for (let y = position.y - radius; y <= position.y + radius; ++y) { - var iy = Math.floor(y); + let iy = Math.floor(y); if (radius >= 27) // Switchover point before RangeOp actually performs better than a straight algorithm { - if(iy >= 0 && iy < size) + if (iy >= 0 && iy < this.size) { - var dy = y - cy; - var dx = Math.sqrt(radius*radius - dy*dy); + let dx = Math.sqrt(Math.square(radius) - Math.square(y - position.y)); - var lowerX = Math.floor(cx - dx); - var upperX = Math.floor(cx + dx); + let minX = Math.max(Math.floor(position.x - dx), 0); + let maxX = Math.min(Math.floor(position.x + dx), this.size - 1) + 1; - var minX = (lowerX > 0 ? lowerX : 0); - var maxX = (upperX < size ? upperX+1 : size); + let newMembers = this.rangeCount[iy].get(minX, maxX); - var total = maxX - minX; - var mem = this.rangeCount[iy].get(minX, maxX); - - members += mem; - nonMembers += total - mem; + members += newMembers; + nonMembers += maxX - minX - newMembers; } } else // Simply check the tiles one by one to find the number { - var dy = (iy - cy); - var xmin = Math.floor(cx - radius); - var xmax = Math.ceil(cx + radius); - xmin = (xmin >= 0) ? xmin : 0; - xmax = ( xmax < size) ? xmax : size - 1; - for (var ix = xmin; ix <= xmax; ++ix) + let dy = iy - position.y; + + let xMin = Math.max(Math.floor(position.x - radius), 0); + let xMax = Math.max(Math.ceil(position.x + radius), this.size - 1); + + for (let ix = xMin; ix <= xMax; ++ix) { - var dx = (ix - cx); - if (dx*dx + dy*dy <= radius2) + let dx = ix - position.x; + if (Math.square(dx) + Math.square(dy) <= radius2) { if (this.inclusionCount[ix] && this.inclusionCount[ix][iy] && this.inclusionCount[ix][iy] > 0) - members += 1; + ++members; else - nonMembers += 1; + ++nonMembers; } } } @@ -151,12 +143,12 @@ TileClass.prototype.countInRadius = function(cx, cy, radius, returnMembers) return nonMembers; }; -TileClass.prototype.countMembersInRadius = function(cx, cy, radius) +TileClass.prototype.countMembersInRadius = function(position, radius) { - return this.countInRadius(cx, cy, radius, true); + return this.countInRadius(position, radius, true); }; -TileClass.prototype.countNonMembersInRadius = function(cx, cy, radius) +TileClass.prototype.countNonMembersInRadius = function(position, radius) { - return this.countInRadius(cx, cy, radius, false); + return this.countInRadius(position, radius, false); };