From c02db59c96f9158dc97be2778b276b6434da5ef6 Mon Sep 17 00:00:00 2001 From: elexis Date: Tue, 23 Jan 2018 17:54:30 +0000 Subject: [PATCH] Use vectors for getHeight and remove the global proxy, refs #4992. This was SVN commit r20983. --- .../public/maps/random/ardennes_forest.js | 6 +++--- .../public/maps/random/belgian_uplands.js | 9 +++++---- .../data/mods/public/maps/random/corsica.js | 2 +- .../mods/public/maps/random/deep_forest.js | 2 +- .../data/mods/public/maps/random/latium.js | 14 ++++++------- .../data/mods/public/maps/random/oasis.js | 20 +++++++++++++------ .../public/maps/random/pyrenean_sierra.js | 12 +++++------ .../data/mods/public/maps/random/rivers.js | 2 +- .../public/maps/random/rmgen/gaia_terrain.js | 16 +++++++-------- .../mods/public/maps/random/rmgen/library.js | 9 --------- .../public/maps/random/rmgen/random_map.js | 13 ++++++------ .../mods/public/maps/random/rmgen2/gaia.js | 4 ++-- 12 files changed, 55 insertions(+), 54 deletions(-) diff --git a/binaries/data/mods/public/maps/random/ardennes_forest.js b/binaries/data/mods/public/maps/random/ardennes_forest.js index 5ca4db4b8b..3f034a60b0 100644 --- a/binaries/data/mods/public/maps/random/ardennes_forest.js +++ b/binaries/data/mods/public/maps/random/ardennes_forest.js @@ -84,7 +84,7 @@ for (var ix = 0; ix < mapSize; ix++) for (var iz = 0; iz < mapSize; iz++) { let position = new Vector2D(ix, iz); - var h = getHeight(ix,iz); + let h = g_Map.getHeight(position); if (h > heightRavineHill) { addToClass(ix,iz,clHill); @@ -260,7 +260,7 @@ for (var ix = 0; ix < mapSize; ix++) for (var iz = 0; iz < mapSize; iz++) { let position = new Vector2D(ix, iz); - var h = getHeight(ix,iz); + let h = g_Map.getHeight(position); if(h > 15 && h < 45 && playerClass.countMembersInRadius(ix, iz, 1) == 0) explorablePoints.push(position); @@ -282,7 +282,7 @@ for (var ix = 0; ix < mapSize; ix++) { let position = new Vector2D(ix, iz); var z = iz / (mapSize + 1.0); - var h = getHeight(ix,iz); + var h = g_Map.getHeight(position); var pn = playerNearness(x,z); var n = (noise0.get(x,z) - 0.5) * 10; g_Map.setHeight(position, h + (n * pn)); diff --git a/binaries/data/mods/public/maps/random/belgian_uplands.js b/binaries/data/mods/public/maps/random/belgian_uplands.js index c40d7cd007..20fde9148b 100644 --- a/binaries/data/mods/public/maps/random/belgian_uplands.js +++ b/binaries/data/mods/public/maps/random/belgian_uplands.js @@ -126,7 +126,8 @@ while (!goodStartPositionsFound) for (var x = distToBorder + minTerrainDistToBorder; x < mapSize - distToBorder - minTerrainDistToBorder; x++) for (var y = distToBorder + minTerrainDistToBorder; y < mapSize - distToBorder - minTerrainDistToBorder; y++) { - var actualHeight = getHeight(x, y); + let position = new Vector2D(x, y); + let actualHeight = g_Map.getHeight(position); if (actualHeight > lowerHeightLimit && actualHeight < upperHeightLimit) { // Check for points within a valid area by height (rectangular since faster) @@ -134,7 +135,7 @@ while (!goodStartPositionsFound) for (var offX = - neededDistance; offX <= neededDistance; offX++) for (var offY = - neededDistance; offY <= neededDistance; offY++) { - var testHeight = getHeight(x + offX, y + offY); + var testHeight = g_Map.getHeight(Vector2D.add(position, new Vector2D(offX, offY))); if (testHeight <= lowerHeightLimit || testHeight >= upperHeightLimit) { isPossible = false; @@ -171,7 +172,7 @@ while (!goodStartPositionsFound) { for (var dy = - maxDistToResources; dy < maxDistToResources; dy++) { - var testHeight = getHeight(possibleStartPositions[i][0] + dx, possibleStartPositions[i][1] + dy); + var testHeight = g_Map.getHeight(new Vector2D(possibleStartPositions[i][0] + dx, possibleStartPositions[i][1] + dy)); if (testHeight < lowerHeightLimit) numLowTiles++; @@ -265,7 +266,7 @@ for(var x = minTerrainDistToBorder; x < mapSize - minTerrainDistToBorder; x++) var textureMinHeight = heightRange.min; for (var i = 0; i < textueByHeight.length; i++) { - if (getHeight(x, y) >= textureMinHeight && getHeight(x, y) <= textueByHeight[i].upperHeightLimit) + if (g_Map.getHeight(position) >= textureMinHeight && g_Map.getHeight(position) <= textueByHeight[i].upperHeightLimit) { createTerrain(textueByHeight[i].terrain).place(position); diff --git a/binaries/data/mods/public/maps/random/corsica.js b/binaries/data/mods/public/maps/random/corsica.js index e688f4775e..a7e4a9e391 100644 --- a/binaries/data/mods/public/maps/random/corsica.js +++ b/binaries/data/mods/public/maps/random/corsica.js @@ -290,7 +290,7 @@ function getCosricaSardiniaTerrain(mapX, mapZ) if (isSettlement) return undefined; - let height = getHeight(mapX, mapZ); + let height = g_Map.getHeight(position); let slope = g_Map.getSlope(position); if (height >= 0.5 && height < 1.5 && isShore) diff --git a/binaries/data/mods/public/maps/random/deep_forest.js b/binaries/data/mods/public/maps/random/deep_forest.js index 1c658db991..333c3ddcf9 100644 --- a/binaries/data/mods/public/maps/random/deep_forest.js +++ b/binaries/data/mods/public/maps/random/deep_forest.js @@ -193,7 +193,7 @@ for (var x = 0; x < mapSize; x++) // General hight map var hVarMiddleHill = mapSize / 64 * (1 + Math.cos(3/2 * Math.PI * radius / mapRadius)); var hVarHills = 5 * (1 + Math.sin(x / 10) * Math.sin(z / 10)); - g_Map.setHeight(position, getHeight(x, z) + hVarMiddleHill + hVarHills + 1); + g_Map.setHeight(position, g_Map.getHeight(position) + hVarMiddleHill + hVarHills + 1); } Engine.SetProgress(95); diff --git a/binaries/data/mods/public/maps/random/latium.js b/binaries/data/mods/public/maps/random/latium.js index 621eb967e8..c3cda938f0 100644 --- a/binaries/data/mods/public/maps/random/latium.js +++ b/binaries/data/mods/public/maps/random/latium.js @@ -143,7 +143,7 @@ for (var ix = 0; ix <= mapSize; ix++) let c = startAngle ? z : x; let distToWater = stayClasses(clWater, 1).allows(position) ? 0 : (0.5 - WATER_WIDTH - Math.abs(c - 0.5)); - let h = distToWater ? heightHill * (1 - Math.abs(c - 0.5) / (0.5 - WATER_WIDTH)) : getHeight(ix, iz); + let h = distToWater ? heightHill * (1 - Math.abs(c - 0.5) / (0.5 - WATER_WIDTH)) : g_Map.getHeight(position); // add some base noise var baseNoise = 16*noise0.get(x,z) + 8*noise1.get(x,z) + 4*noise2.get(x,z) - (16+8+4)/2; @@ -200,10 +200,10 @@ for (var ix = 0; ix < mapSize; ix++) var pn = playerNearness(x, z); // get heights of surrounding vertices - var h00 = getHeight(ix, iz); - var h01 = getHeight(ix, iz+1); - var h10 = getHeight(ix+1, iz); - var h11 = getHeight(ix+1, iz+1); + var h00 = g_Map.getHeight(Vector2D.add(position, new Vector2D(0, 0)); + var h01 = g_Map.getHeight(Vector2D.add(position, new Vector2D(0, 1)); + var h10 = g_Map.getHeight(Vector2D.add(position, new Vector2D(1, 0)); + var h11 = g_Map.getHeight(Vector2D.add(position, new Vector2D(1, 1)); // find min and max height var maxH = Math.max(h00, h01, h10, h11); @@ -218,7 +218,7 @@ for (var ix = 0; ix < mapSize; ix++) var maxNz = Math.min(iz + 2, mapSize); for (let nx = Math.max(ix - 1, 0); nx <= maxNx; ++nx) for (let nz = Math.max(iz - 1, 0); nz <= maxNz; ++nz) - minAdjHeight = Math.min(minAdjHeight, getHeight(nx, nz)); + minAdjHeight = Math.min(minAdjHeight, getHeight(new Vector2D(nx, nz))); } // choose a terrain based on elevation @@ -266,7 +266,7 @@ for (var ix = 0; ix < mapSize; ix++) addToClass(ix, iz, clCliff); // forests - if (getHeight(ix, iz) < 11 && diffH < 2 && minH > 1) + if (g_Map.getHeight(position) < 11 && diffH < 2 && minH > 1) { var forestNoise = (noise6.get(x,z) + 0.5*noise7.get(x,z)) / 1.5 * pn - 0.59; diff --git a/binaries/data/mods/public/maps/random/oasis.js b/binaries/data/mods/public/maps/random/oasis.js index 03004c8f2a..c726f4866e 100644 --- a/binaries/data/mods/public/maps/random/oasis.js +++ b/binaries/data/mods/public/maps/random/oasis.js @@ -33,7 +33,11 @@ const pOasisForestLight = [tForestFloor + TERRAIN_SEPARATOR + ePalmShort, tFores ,tForestFloor,tForestFloor,tForestFloor,tForestFloor]; const heightSeaGround = -3; +const heightFloraMin = -2.5 +const heightFloraReedsMax = -1.9; +const heightFloraMax = -1; const heightLand = 1; +const heightSand = 3.4; const heightOasisPath = 4; const heightOffsetBump = 4; const heightOffsetDune = 18; @@ -280,34 +284,38 @@ log("Creating sand blows and beautifications"); for (var sandx = 0; sandx < mapSize; sandx += 4) for (var sandz = 0; sandz < mapSize; sandz += 4) { - if (getHeight(sandx,sandz) > 3.4) + let position = new Vector2D(sandx, sandz); + let height = g_Map.getHeight(position); + + if (height > heightSand) { - if (randBool((getHeight(sandx,sandz) - 3.4) / 1.4)) + if (randBool((height - heightSand) / 1.4)) { group = new SimpleGroup( [new SimpleObject(aSand, 0,1, 0,2)], true, undefined, sandx,sandz ); createObjectGroup(group, 0); } } - else if (getHeight(sandx, sandz) > -2.5 && getHeight(sandx,sandz) < -1) + else if (height > heightFloraMin && height < heightFloraMax) { if (randBool(0.4)) { group = new SimpleGroup( [new SimpleObject(aWaterFlower, 1,4, 1,2)], true, undefined, sandx,sandz ); createObjectGroup(group, 0); } - else if (randBool(0.7) && getHeight(sandx,sandz) < -1.9) + else if (randBool(0.7) && height < heightFloraReedsMax) { group = new SimpleGroup( [new SimpleObject(aReedsA, 5,12, 0,2),new SimpleObject(aReedsB, 5,12, 0,2)], true, undefined, sandx,sandz ); createObjectGroup(group, 0); } - if (getTileClass(clPassage).countInRadius(sandx,sandz,2,true) > 0) { + if (getTileClass(clPassage).countMembersInRadius(sandx, sandz, 2) > 0) + { if (randBool(0.4)) { group = new SimpleGroup( [new SimpleObject(aWaterFlower, 1,4, 1,2)], true, undefined, sandx,sandz ); createObjectGroup(group, 0); } - else if (randBool(0.7) && getHeight(sandx,sandz) < -1.9) + else if (randBool(0.7) && height < heightFloraReedsMax) { group = new SimpleGroup( [new SimpleObject(aReedsA, 5,12, 0,2),new SimpleObject(aReedsB, 5,12, 0,2)], true, undefined, sandx,sandz ); createObjectGroup(group, 0); diff --git a/binaries/data/mods/public/maps/random/pyrenean_sierra.js b/binaries/data/mods/public/maps/random/pyrenean_sierra.js index 617a4bd858..f491ed21e4 100644 --- a/binaries/data/mods/public/maps/random/pyrenean_sierra.js +++ b/binaries/data/mods/public/maps/random/pyrenean_sierra.js @@ -246,7 +246,7 @@ for (let ix = 1; ix < mapSize - 1; ++ix) let position = new Vector2D(ix, iz); if (g_Map.validH(ix, iz) && getTileClass(clPyrenneans).countMembersInRadius(ix, iz, 1)) { - let height = getHeight(ix, iz); + let height = g_Map.getHeight(position); let index = 1 / (1 + Math.max(0, height / 7)); g_Map.setHeight(position, height * (1 - index) + g_Map.getAverageHeight(position) * index); } @@ -283,8 +283,8 @@ for (let ix = 1; ix < mapSize - 1; ++ix) if (g_Map.inMapBounds(position) && getTileClass(clPyrenneans).countMembersInRadius(ix, iz, 1)) { let heightNeighbor = g_Map.getAverageHeight(position); - let index = 1 / (1 + Math.max(0, (getHeight(ix,iz) - 10) / 7)); - g_Map.setHeight(position, getHeight(ix, iz) * (1 - index) + heightNeighbor * index); + let index = 1 / (1 + Math.max(0, (g_Map.getHeight(position) - 10) / 7)); + g_Map.setHeight(position, g_Map.getHeight(position) * (1 - index) + heightNeighbor * index); } } @@ -313,11 +313,11 @@ for (let ix = 1; ix < mapSize - 1; ++ix) let smoothPos = Vector2D.add(position, new Vector2D(xx, yy)); if (g_Map.inMapBounds(smoothPos) && (xx != 0 || yy != 0)) { - averageHeight += getHeight(smoothPos.x, smoothPos.y) / (Math.abs(xx) + Math.abs(yy)); + averageHeight += g_Map.getHeight(smoothPos) / (Math.abs(xx) + Math.abs(yy)); todivide += 1 / (Math.abs(xx) + Math.abs(yy)); } } - g_Map.setHeight(position, (averageHeight + 2 * getHeight(ix, iz)) / (todivide + 2)); + g_Map.setHeight(position, (averageHeight + 2 * g_Map.getHeight(position)) / (todivide + 2)); } Engine.SetProgress(55); @@ -357,7 +357,7 @@ for (let x = 0; x < mapSize; ++x) for (let z = 0; z < mapSize; ++z) { let position = new Vector2D(x, z); - let height = getHeight(x, z); + let height = g_Map.getHeight(position); let heightDiff = g_Map.getSlope(position); if (getTileClass(clPyrenneans).countMembersInRadius(x, z, 2)) diff --git a/binaries/data/mods/public/maps/random/rivers.js b/binaries/data/mods/public/maps/random/rivers.js index 30c48c65ad..913fc260e0 100644 --- a/binaries/data/mods/public/maps/random/rivers.js +++ b/binaries/data/mods/public/maps/random/rivers.js @@ -139,7 +139,7 @@ for (let i = 0; i < numRivers; ++i) let newHeight = isShallow ? heightShallows : Math.max(height, heightSeaGround); - if (getHeight(position.x, position.y) < newHeight) + if (g_Map.getHeight(position) < newHeight) return; g_Map.setHeight(position, newHeight); diff --git a/binaries/data/mods/public/maps/random/rmgen/gaia_terrain.js b/binaries/data/mods/public/maps/random/rmgen/gaia_terrain.js index dda2d7dbb7..db49a0f8bb 100644 --- a/binaries/data/mods/public/maps/random/rmgen/gaia_terrain.js +++ b/binaries/data/mods/public/maps/random/rmgen/gaia_terrain.js @@ -190,9 +190,9 @@ function createMountain(maxHeight, minRadius, maxRadius, numCircles, constraints if (distance > radius) continue; - if (getHeight(ix, iz) < newHeight) + if (g_Map.getHeight(position) < newHeight) g_Map.setHeight(position, newHeight); - else if (getHeight(ix, iz) >= newHeight && getHeight(ix, iz) < newHeight + 4) + else if (g_Map.getHeight(position) >= newHeight && g_Map.getHeight(position) < newHeight + 4) g_Map.setHeight(position, newHeight + 4); if (terrain !== undefined) @@ -536,8 +536,8 @@ function createPassage(args) { let bound = x => Math.max(0, Math.min(Math.round(x), getMapSize())); - let startHeight = args.startHeight !== undefined ? args.startHeight : getHeight(bound(args.start.x), bound(args.start.y)); - let endHeight = args.endHeight !== undefined ? args.endHeight : getHeight(bound(args.end.x), bound(args.end.y)); + let startHeight = args.startHeight !== undefined ? args.startHeight : g_Map.getHeight(new Vector2D(bound(args.start.x), bound(args.start.y))); + let endHeight = args.endHeight !== undefined ? args.endHeight : g_Map.getHeight(new Vector2D(bound(args.end.x), bound(args.end.y))); let passageVec = Vector2D.sub(args.end, args.start); let widthDirection = passageVec.perpendicular().normalize(); @@ -554,7 +554,7 @@ function createPassage(args) let location = Vector2D.add(locationLength, Vector2D.mult(widthDirection, stepWidth)).round(); if (!g_Map.inMapBounds(location) || - args.maxHeight !== undefined && getHeight(location.x, location.y) > args.maxHeight) + args.maxHeight !== undefined && g_Map.getHeight(location) > args.maxHeight) continue; let smoothDistance = args.smoothWidth + Math.abs(stepWidth) - halfPassageWidth; @@ -562,7 +562,7 @@ function createPassage(args) g_Map.setHeight( location, smoothDistance > 0 ? - (getHeight(location.x, location.y) * smoothDistance + passageHeight / smoothDistance) / (smoothDistance + 1 / smoothDistance) : + (g_Map.getHeight(location) * smoothDistance + passageHeight / smoothDistance) / (smoothDistance + 1 / smoothDistance) : passageHeight); if (args.tileClass !== undefined) @@ -591,8 +591,8 @@ function findLocationInDirectionBasedOnHeight(startPoint, endPoint, minHeight, m let ipos = pos.clone().round(); if (g_Map.validH(ipos.x, ipos.y) && - getHeight(ipos.x, ipos.y) >= minHeight && - getHeight(ipos.x, ipos.y) <= maxHeight) + g_Map.getHeight(ipos) >= minHeight && + g_Map.getHeight(ipos) <= maxHeight) return pos.add(stepVec.mult(offset)); } diff --git a/binaries/data/mods/public/maps/random/rmgen/library.js b/binaries/data/mods/public/maps/random/rmgen/library.js index 36c25d7228..bb2d6e09eb 100644 --- a/binaries/data/mods/public/maps/random/rmgen/library.js +++ b/binaries/data/mods/public/maps/random/rmgen/library.js @@ -328,15 +328,6 @@ function getPlayerTeam(playerID) return g_MapSettings.PlayerData[playerID].Team; } -function getHeight(x, z) -{ - return g_Map.getHeight(x, z); -} - -/** - * Utility functions for classes - */ - /** * Add point to given class by id */ diff --git a/binaries/data/mods/public/maps/random/rmgen/random_map.js b/binaries/data/mods/public/maps/random/rmgen/random_map.js index 24064e4601..f3dace1bc8 100644 --- a/binaries/data/mods/public/maps/random/rmgen/random_map.js +++ b/binaries/data/mods/public/maps/random/rmgen/random_map.js @@ -160,12 +160,12 @@ RandomMap.prototype.setTexture = function(x, z, texture) this.texture[x][z] = this.getTextureID(texture); }; -RandomMap.prototype.getHeight = function(x, z) +RandomMap.prototype.getHeight = function(position) { - if (!this.validH(x, z)) - throw new Error("getHeight: invalid vertex position (" + x + ", " + z + ")"); + if (!this.validH(position.x, position.y)) + throw new Error("getHeight: invalid vertex position " + uneval(position)); - return this.height[x][z]; + return this.height[position.x][position.y]; }; RandomMap.prototype.setHeight = function(position, height) @@ -289,7 +289,7 @@ RandomMap.prototype.getAverageHeight = function(position) if (!adjacentPositions.length) return 0; - return adjacentPositions.reduce((totalHeight, pos) => totalHeight + this.getHeight(pos.x, pos.y), 0) / adjacentPositions.length; + return adjacentPositions.reduce((totalHeight, pos) => totalHeight + this.getHeight(pos), 0) / adjacentPositions.length; } /** @@ -301,7 +301,8 @@ RandomMap.prototype.getSlope = function(position) if (!adjacentPositions.length) return 0; - return adjacentPositions.reduce((totalSlope, adjacentPos) => totalSlope + Math.abs(this.getHeight(adjacentPos.x, adjacentPos.y) - this.getHeight(position.x, position.y)), 0) / adjacentPositions.length; + return adjacentPositions.reduce((totalSlope, adjacentPos) => + totalSlope + Math.abs(this.getHeight(adjacentPos) - this.getHeight(position)), 0) / adjacentPositions.length; } /** diff --git a/binaries/data/mods/public/maps/random/rmgen2/gaia.js b/binaries/data/mods/public/maps/random/rmgen2/gaia.js index 47eee67960..5e675d10a9 100644 --- a/binaries/data/mods/public/maps/random/rmgen2/gaia.js +++ b/binaries/data/mods/public/maps/random/rmgen2/gaia.js @@ -109,7 +109,7 @@ function addBluffs(constraint, size, deviation, fill, baseHeight) new Vector2D(baseLine.x2, baseLine.z2), position)); - var curHeight = g_Map.getHeight(pt.x, pt.z); + var curHeight = g_Map.getHeight(position); var newHeight = curHeight - curHeight * (dist / slopeLength) - 2; newHeight = Math.max(newHeight, endLine.height); @@ -1014,7 +1014,7 @@ function createBoundingBox(points, corners) bb[w] = []; for (var l = 0; l < length; ++l) { - var curHeight = g_Map.getHeight(w + corners.minX, l + corners.minZ); + var curHeight = g_Map.getHeight(new Vector2D(w + corners.minX, l + corners.minZ)); bb[w][l] = { "height": curHeight, "isFeature": false