From db2330fe1e02ecd96e1faed756d9e58943b11a12 Mon Sep 17 00:00:00 2001 From: elexis Date: Mon, 23 Oct 2017 10:13:49 +0000 Subject: [PATCH] Remove rmgen euclidian distance helper function, refs rP20328 / D969. Use Math.square(x) instead of Math.pow(x, 2) in places that don't multiply it with Math.PI. git-svn-id: https://svn.wildfiregames.com/public/ps/trunk@20331 3db68df2-c116-0410-a063-a993310a9797 --- .../mods/public/maps/random/belgian_uplands.js | 15 +++------------ .../mods/public/maps/random/caledonian_meadows.js | 22 +++++++++++----------- .../data/mods/public/maps/random/deep_forest.js | 7 ++++--- binaries/data/mods/public/maps/random/gear.js | 2 +- .../mods/public/maps/random/heightmap/heightmap.js | 16 ++++++++-------- binaries/data/mods/public/maps/random/islands.js | 2 +- binaries/data/mods/public/maps/random/oasis.js | 2 +- binaries/data/mods/public/maps/random/ratumacos.js | 4 ++-- binaries/data/mods/public/maps/random/red_sea.js | 2 +- .../data/mods/public/maps/random/rmgen/library.js | 16 ++++------------ binaries/data/mods/public/maps/random/rmgen/map.js | 2 +- .../mods/public/maps/random/rmgen/wall_builder.js | 20 +++++++++++--------- .../data/mods/public/maps/random/rmgen2/gaia.js | 2 +- .../data/mods/public/maps/random/rmgen2/setup.js | 4 ++-- .../data/mods/public/maps/random/schwarzwald.js | 7 ++++--- .../mods/public/maps/random/snowflake_searocks.js | 2 +- binaries/data/mods/public/maps/random/wild_lake.js | 9 +++++---- 17 files changed, 61 insertions(+), 73 deletions(-) diff --git a/binaries/data/mods/public/maps/random/belgian_uplands.js b/binaries/data/mods/public/maps/random/belgian_uplands.js index 6a01cc5809..75756c9d72 100644 --- a/binaries/data/mods/public/maps/random/belgian_uplands.js +++ b/binaries/data/mods/public/maps/random/belgian_uplands.js @@ -160,16 +160,11 @@ while (!goodStartPositionsFound) var maxDistToCenter = mapSize / 2; for (var i = 0; i < possibleStartPositions.length; i++) { - var deltaX = possibleStartPositions[i][0] - mapSize / 2; - var deltaY = possibleStartPositions[i][1] - mapSize / 2; - var distToCenter = Math.pow(Math.pow(deltaX, 2) + Math.pow(deltaY, 2), 1/2); - - if (distToCenter < maxDistToCenter) + if (Math.euclidDistance2D(...possibleStartPositions[i], mapSize / 2, mapSize / 2) < maxDistToCenter) possibleStartPositionsTemp.push(possibleStartPositions[i]); // placeTerrain(possibleStartPositions[i][0], possibleStartPositions[i][1], "purple"); // Only works properly for 1 loop } possibleStartPositions = clone(possibleStartPositionsTemp); - // Reduce to tiles near low and high ground (Rectangular check since faster) to make sure each player has access to all resource types. var possibleStartPositionsTemp = []; var maxDistToResources = distToBorder; // Has to be <= distToBorder! @@ -217,7 +212,7 @@ while (!goodStartPositionsFound) if (enoughTiles) { // Get some random start location derivations. NOTE: Iterating over all possible derivations is just too much (valid points * numPlayers) - var maxTries = 100000; // floor(800000 / (Math.pow(numPlayers, 2) / 2)); + var maxTries = 100000; var possibleDerivations = []; for (var i = 0; i < maxTries; i++) { @@ -238,11 +233,7 @@ while (!goodStartPositionsFound) { if (p1 != p2) { - var StartPositionP1 = possibleStartPositions[possibleDerivations[d][p1]]; - var StartPositionP2 = possibleStartPositions[possibleDerivations[d][p2]]; - var actualDist = Math.pow(Math.pow(StartPositionP1[0] - StartPositionP2[0], 2) + Math.pow(StartPositionP1[1] - StartPositionP2[1], 2), 1/2); - if (actualDist < minDist) - minDist = actualDist; + minDist = Math.min(minDist, Math.euclidDistance2D(...possibleStartPositions[possibleDerivations[d][p1]], ...possibleStartPositions[possibleDerivations[d][p2]])); if (minDist < maxMinDist) break; } diff --git a/binaries/data/mods/public/maps/random/caledonian_meadows.js b/binaries/data/mods/public/maps/random/caledonian_meadows.js index a0f0098958..54d6495722 100644 --- a/binaries/data/mods/public/maps/random/caledonian_meadows.js +++ b/binaries/data/mods/public/maps/random/caledonian_meadows.js @@ -20,16 +20,16 @@ function placeRandomPathToHeight( rectangularSmoothToHeight(position, width, width, targetHeight, strength, heightmap); if (texture) { + let painters = [new TerrainPainter(texture)]; if (tileClass !== undefined) - createArea(new ClumpPlacer(0.3 * width * width, 1, 1, 1, floor(position.x), floor(position.y)), - [new TerrainPainter(texture), paintClass(tileClass)]); - else - createArea(new ClumpPlacer(0.3 * width * width, 1, 1, 1, floor(position.x), floor(position.y)), - new TerrainPainter(texture)); + painters.push(paintClass(tileClass)); + createArea( + new ClumpPlacer(0.3 * Math.square(width), 1, 1, 1, Math.floor(position.x), Math.floor(position.y)), + painters); } pathPoints.push({ "x": position.x, "y": position.y, "dist": distance }); // Check for distance to target and setup for next loop if needed - if (getDistance(position.x, position.y, target.x, target.y) < distance / 2) + if (Math.euclidDistance2D(position.x, position.y, target.x, target.y) < distance / 2) break; let angleToTarget = getAngle(position.x, position.y, target.x, target.y); let angleOff = randFloat(-PI/2, PI/2); @@ -351,11 +351,11 @@ if (teams.length) if (t2 != t1) continue; - let l1 = startLocations[pi]; - let l2 = startLocations[pj]; - let dist = getDistance(l1.x, l1.y, l2.x, l2.y); - if (dist > maxTeamDist) - maxTeamDist = dist; + maxTeamDist = Math.max( + maxTeamDist, + Math.euclidDistance2D( + startLocations[pi].x, startLocations[pi].y, + startLocations[pj].x, startLocations[pj].y)); } } diff --git a/binaries/data/mods/public/maps/random/deep_forest.js b/binaries/data/mods/public/maps/random/deep_forest.js index 00af48f5a4..4dcbc14156 100644 --- a/binaries/data/mods/public/maps/random/deep_forest.js +++ b/binaries/data/mods/public/maps/random/deep_forest.js @@ -169,7 +169,7 @@ for (var i = 0; i < maxI; i++) x += round(cos(angle + randFloat(-pathAngleOff, pathAngleOff))); z += round(sin(angle + randFloat(-pathAngleOff, pathAngleOff))); } - if (getDistance(x, z, targetX, targetZ) < pathSucsessRadius) + if (Math.euclidDistance2D(x, z, targetX, targetZ) < pathSucsessRadius) targetReached = true; tries++; @@ -210,10 +210,11 @@ for (var x = 0; x < mapSize; x++) { for (var z = 0;z < mapSize;z++) { - var radius = Math.pow(Math.pow(mapCenterX - x - 0.5, 2) + Math.pow(mapCenterZ - z - 0.5, 2), 1/2); // The 0.5 is a correction for the entities placed on the center of tiles + // The 0.5 is a correction for the entities placed on the center of tiles + var radius = Math.euclidDistance2D(x + 0.5, z + 0.5, mapCenterX, mapCenterZ); var minDistToSL = mapSize; for (var i=0; i < numPlayers; i++) - minDistToSL = min(minDistToSL, getDistance(playerStartLocX[i], playerStartLocZ[i], x, z)); + minDistToSL = Math.min(minDistToSL, Math.euclidDistance2D(x, z, playerStartLocX[i], playerStartLocZ[i])); // Woods tile based var tDensFactSL = max(min((minDistToSL - baseRadius) / baseRadius, 1), 0); diff --git a/binaries/data/mods/public/maps/random/gear.js b/binaries/data/mods/public/maps/random/gear.js index 14a9ce1212..a315756d5e 100644 --- a/binaries/data/mods/public/maps/random/gear.js +++ b/binaries/data/mods/public/maps/random/gear.js @@ -236,7 +236,7 @@ createArea( log("Creating central island..."); createArea( - new ClumpPlacer(Math.pow(mapSize - 50, 2) * 0.09, 1, 1, 10, center, center), + new ClumpPlacer(Math.square(mapSize - 50) * 0.09, 1, 1, 10, center, center), new SmoothElevationPainter(ELEVATION_SET, 4, 3), null); diff --git a/binaries/data/mods/public/maps/random/heightmap/heightmap.js b/binaries/data/mods/public/maps/random/heightmap/heightmap.js index 9834b89fa2..879da99329 100644 --- a/binaries/data/mods/public/maps/random/heightmap/heightmap.js +++ b/binaries/data/mods/public/maps/random/heightmap/heightmap.js @@ -68,7 +68,7 @@ function getStartLocationsByHeightmap(heightRange, maxTries = 1000, minDistToBor for (let x = minDistToBorder; x < heightmap.length - minDistToBorder; ++x) for (let y = minDistToBorder; y < heightmap[0].length - minDistToBorder; ++y) if (heightmap[x][y] > heightRange.min && heightmap[x][y] < heightRange.max) // Is in height range - if (!isCircular || r - getDistance(x, y, r, r) >= minDistToBorder) // Is far enough away from map border + if (!isCircular || r - Math.euclidDistance2D(x, y, r, r) >= minDistToBorder) // Is far enough away from map border validStartLoc.push({ "x": x, "y": y }); let maxMinDist = 0; @@ -83,7 +83,7 @@ function getStartLocationsByHeightmap(heightRange, maxTries = 1000, minDistToBor { for (let p2 = p1 + 1; p2 < numberOfPlayers; ++p2) { - let dist = getDistance(startLoc[p1].x, startLoc[p1].y, startLoc[p2].x, startLoc[p2].y); + let dist = Math.euclidDistance2D(startLoc[p1].x, startLoc[p1].y, startLoc[p2].x, startLoc[p2].y); if (dist < minDist) minDist = dist; } @@ -123,10 +123,10 @@ function distributeEntitiesByHeight(heightRange, avoidPoints, minDistance, entit if (heightmap[x][y] < heightRange.min || heightmap[x][y] > heightRange.max) continue; // Out of height range let checkpoint = { "x" : x + 0.5, "y" : y + 0.5 }; - if (isCircular && r - getDistance(checkpoint.x, checkpoint.y, r, r) < minDistance) + if (isCircular && r - Math.euclidDistance2D(checkpoint.x, checkpoint.y, r, r) < minDistance) continue; // Too close to map border // Avoid points by minDistance, else add to validPoints - if (avoidPoints.every(ap => getDistance(checkpoint.x, checkpoint.y, ap.x, ap.y) > minDistance)) + if (avoidPoints.every(ap => Math.euclidDistance2D(checkpoint.x, checkpoint.y, ap.x, ap.y) > minDistance)) validPoints.push(checkpoint); } } @@ -142,7 +142,7 @@ function distributeEntitiesByHeight(heightRange, avoidPoints, minDistance, entit { let checkPointIndex = randIntExclusive(0, validPoints.length); let checkPoint = validPoints[checkPointIndex]; - if (placements.every(p => getDistance(p.x, p.y, checkPoint.x, checkPoint.y) > minDistance)) + if (placements.every(p => Math.euclidDistance2D(p.x, p.y, checkPoint.x, checkPoint.y) > minDistance)) { placeObject(checkPoint.x, checkPoint.y, pickRandom(entityList), playerID, randFloat(0, 2*PI)); placements.push(checkPoint); @@ -371,7 +371,7 @@ function getPointsByHeight(heightRange, avoidPoints = [], avoidClass = undefined continue; if (heightmap[x][y] > heightRange.min && heightmap[x][y] < heightRange.max && // Has correct height - (!isCircular || r - getDistance(x, y, r, r) >= minDistance)) // Enough distance to map border + (!isCircular || r - Math.euclidDistance2D(x, y, r, r) >= minDistance)) // Enough distance to the map border validVertices.push({ "x": x, "y": y , "dist": minDistance}); } } @@ -379,7 +379,7 @@ function getPointsByHeight(heightRange, avoidPoints = [], avoidClass = undefined for (let tries = 0; tries < maxTries; ++tries) { let point = pickRandom(validVertices); - if (placements.every(p => getDistance(p.x, p.y, point.x, point.y) > max(minDistance, p.dist))) + if (placements.every(p => Math.euclidDistance2D(p.x, p.y, point.x, point.y) > Math.max(minDistance, p.dist))) { points.push(point); placements.push(point); @@ -427,7 +427,7 @@ function getSlopeMap(inclineMap = getInclineMap(g_Map.height)) let max_y = inclineMap[x].length; slopeMap[x] = new Float32Array(max_y); for (let y = 0; y < max_y; ++y) - slopeMap[x][y] = Math.pow(inclineMap[x][y].x * inclineMap[x][y].x + inclineMap[x][y].y * inclineMap[x][y].y, 0.5); + slopeMap[x][y] = Math.euclidDistance2D(0, 0, inclineMap[x][y].x, inclineMap[x][y].y); } return slopeMap; } diff --git a/binaries/data/mods/public/maps/random/islands.js b/binaries/data/mods/public/maps/random/islands.js index df0f655dbb..22652f1c6a 100644 --- a/binaries/data/mods/public/maps/random/islands.js +++ b/binaries/data/mods/public/maps/random/islands.js @@ -215,7 +215,7 @@ playerConstraint = new AvoidTileClassConstraint(clPlayer, floor(scaleByMapSize(9 landConstraint = new AvoidTileClassConstraint(clLand, floor(scaleByMapSize(9,12))); log("Creating small islands..."); -for (let i = 0; i < 6 * Math.pow(scaleByMapSize(1, 3), 2); ++i) +for (let i = 0; i < 6 * Math.square(scaleByMapSize(1, 3)); ++i) { landAreaLen = landAreas.length; if (!landAreaLen) diff --git a/binaries/data/mods/public/maps/random/oasis.js b/binaries/data/mods/public/maps/random/oasis.js index 156e58c3bb..4e236eaa60 100644 --- a/binaries/data/mods/public/maps/random/oasis.js +++ b/binaries/data/mods/public/maps/random/oasis.js @@ -212,7 +212,7 @@ log("Creating actual oasis..."); var fx = fractionToTiles(0.5); var fz = fractionToTiles(0.5); createArea( - new ClumpPlacer(Math.pow(mapSize * 0.2, 2) * 1.1, 0.8, 0.2, 10, Math.round(fx), Math.round(fz)), + new ClumpPlacer(Math.square(mapSize * 0.2) * 1.1, 0.8, 0.2, 10, Math.round(fx), Math.round(fz)), [ new LayeredPainter([pOasisForestLight,tShoreBlend, tWater, tWater, tWater], [scaleByMapSize(6, 20), 3, 5, 2]), new SmoothElevationPainter(ELEVATION_SET, -3, 15), diff --git a/binaries/data/mods/public/maps/random/ratumacos.js b/binaries/data/mods/public/maps/random/ratumacos.js index 2c1db4fe2d..384f2831e5 100644 --- a/binaries/data/mods/public/maps/random/ratumacos.js +++ b/binaries/data/mods/public/maps/random/ratumacos.js @@ -277,8 +277,8 @@ createDecoration( [new SimpleObject(g_Decoratives.lillies, 1,2, 0,1)] ], [ - 200 * Math.pow(scaleByMapSize(3, 12), 2), - 100 * Math.pow(scaleByMapSize(3, 12), 2) + 200 * Math.square(scaleByMapSize(3, 12)), + 100 * Math.square(scaleByMapSize(3, 12)) ], stayClasses(g_TileClasses.shallowWater, 0) ); diff --git a/binaries/data/mods/public/maps/random/red_sea.js b/binaries/data/mods/public/maps/random/red_sea.js index 6fc1819cea..ec2f5fec41 100644 --- a/binaries/data/mods/public/maps/random/red_sea.js +++ b/binaries/data/mods/public/maps/random/red_sea.js @@ -287,7 +287,7 @@ createObjectGroupsDeprecated( g_TileClasses.water, 3 ) ], - Math.pow(scaleByMapSize(5, 20), 2), + Math.square(scaleByMapSize(5, 20)), 500 ); RMS.SetProgress(90); diff --git a/binaries/data/mods/public/maps/random/rmgen/library.js b/binaries/data/mods/public/maps/random/rmgen/library.js index d6f039592b..7d39e9fedc 100644 --- a/binaries/data/mods/public/maps/random/rmgen/library.js +++ b/binaries/data/mods/public/maps/random/rmgen/library.js @@ -588,14 +588,6 @@ function checkIfInClass(x, z, id) } /** - * Returns the distance between 2 points - */ -function getDistance(x1, z1, x2, z2) -{ - return Math.pow(Math.pow(x1 - x2, 2) + Math.pow(z1 - z2, 2), 1/2); -} - -/** * Returns the angle of the vector between point 1 and point 2. * The angle is counterclockwise from the positive x axis. */ @@ -654,10 +646,10 @@ function getOrderOfPointsForShortestClosePath(points) order.push(i); pointsToAdd.shift(i); if (i) - distances.push(getDistance(points[order[i]].x, points[order[i]].y, points[order[i - 1]].x, points[order[i - 1]].y)); + distances.push(Math.euclidDistance2D(points[order[i]].x, points[order[i]].y, points[order[i - 1]].x, points[order[i - 1]].y)); } - distances.push(getDistance( + distances.push(Math.euclidDistance2D( points[order[0]].x, points[order[0]].y, points[order[order.length - 1]].x, @@ -673,8 +665,8 @@ function getOrderOfPointsForShortestClosePath(points) let minDist2 = 0; for (let k = 0; k < order.length; ++k) { - let dist1 = getDistance(pointsToAdd[0].x, pointsToAdd[0].y, points[order[k]].x, points[order[k]].y); - let dist2 = getDistance(pointsToAdd[0].x, pointsToAdd[0].y, points[order[(k + 1) % order.length]].x, points[order[(k + 1) % order.length]].y); + let dist1 = Math.euclidDistance2D(pointsToAdd[0].x, pointsToAdd[0].y, points[order[k]].x, points[order[k]].y); + let dist2 = Math.euclidDistance2D(pointsToAdd[0].x, pointsToAdd[0].y, points[order[(k + 1) % order.length]].x, points[order[(k + 1) % order.length]].y); let enlengthen = dist1 + dist2 - distances[k]; if (enlengthen < minEnlengthen) { diff --git a/binaries/data/mods/public/maps/random/rmgen/map.js b/binaries/data/mods/public/maps/random/rmgen/map.js index 8dad02dfe2..9326e949a6 100644 --- a/binaries/data/mods/public/maps/random/rmgen/map.js +++ b/binaries/data/mods/public/maps/random/rmgen/map.js @@ -91,7 +91,7 @@ Map.prototype.validT = function(x, z, distance = 0) if (g_MapSettings.CircularMap) { let halfSize = Math.floor(this.size / 2); - return Math.round(getDistance(x, z, halfSize, halfSize)) < halfSize - distance - 1; + return Math.round(Math.euclidDistance2D(x, z, halfSize, halfSize)) < halfSize - distance - 1; } else return x >= distance && z >= distance && x < this.size - distance && z < this.size - distance; diff --git a/binaries/data/mods/public/maps/random/rmgen/wall_builder.js b/binaries/data/mods/public/maps/random/rmgen/wall_builder.js index 0e0679a967..c93d31134f 100644 --- a/binaries/data/mods/public/maps/random/rmgen/wall_builder.js +++ b/binaries/data/mods/public/maps/random/rmgen/wall_builder.js @@ -577,7 +577,7 @@ function placeLinearWall(startX, startY, targetX, targetY, wallPart, style, play warn("Bending is not supported by placeLinearWall but a bending wall element is used: " + wallPart[elementIndex] + " -> wallStyles[style][wallPart[elementIndex]].entity"); } // Setup number of wall parts - var totalLength = getDistance(startX, startY, targetX, targetY); + var totalLength = Math.euclidDistance2D(startX, startY, targetX, targetY); var wallPartLength = 0; for (var elementIndex = 0; elementIndex < wallPart.length; elementIndex++) wallPartLength += wallStyles[style][wallPart[elementIndex]].width; @@ -897,7 +897,7 @@ function placeIrregularPolygonalWall(centerX, centerY, radius, cornerWallElement var bestWallPart = []; // This is a simpel wall part not a wallPartsAssortment! var bestWallLength = 99999999; // NOTE: This is not exactly like the length the wall will be in the end. Has to be tweaked... - var wallLength = getDistance(corners[i][0], corners[i][1], corners[(i+1)%numCorners][0], corners[(i+1)%numCorners][1]); + var wallLength = Math.euclidDistance2D(corners[i][0], corners[i][1], corners[(i + 1) % numCorners][0], corners[(i + 1) % numCorners][1]); var numWallParts = ceil(wallLength/maxWallPartLength); for (var partIndex = 0; partIndex < wallPartsAssortment.length; partIndex++) { @@ -982,12 +982,12 @@ function placeGenericFortress(centerX, centerY, radius, playerId, style, irregul actualOffY += pointDistance*sin(tmpAngle); actualAngle = getAngle(0, 0, actualOffX, actualOffY); pointDerivation.push([actualOffX, actualOffY]); - distanceToTarget = getDistance(actualOffX, actualOffY, pointDerivation[0][0], pointDerivation[0][1]); + distanceToTarget = Math.euclidDistance2D(actualOffX, actualOffY, ...pointDerivation[0]); var numPoints = pointDerivation.length; if (numPoints > 3 && distanceToTarget < pointDistance) // Could be done better... { targetReached = true; - overlap = pointDistance - getDistance(pointDerivation[numPoints - 1][0], pointDerivation[numPoints - 1][1], pointDerivation[0][0], pointDerivation[0][1]); + overlap = pointDistance - Math.euclidDistance2D(...pointDerivation[numPoints - 1], ...pointDerivation[0]); if (overlap < minOverlap) { minOverlap = overlap; @@ -1011,11 +1011,13 @@ function placeGenericFortress(centerX, centerY, radius, playerId, style, irregul wallElement = "gate"; var entity = wallStyles[style][wallElement].entity; if (entity) - { - placeObject(startX + (getDistance(startX, startY, targetX, targetY)/2)*cos(angle), // placeX - startY + (getDistance(startX, startY, targetX, targetY)/2)*sin(angle), // placeY - entity, playerId, angle - PI/2 + wallStyles[style][wallElement].angle); - } + placeObject( + startX + (Math.euclidDistance2D(startX, startY, targetX, targetY) / 2) * Math.cos(angle), + startY + (Math.euclidDistance2D(startX, startY, targetX, targetY) / 2) * Math.sin(angle), + entity, + playerId, + angle - Math.PI / 2 + wallStyles[style][wallElement].angle); + // Place tower var startX = centerX + bestPointDerivation[(pointIndex + bestPointDerivation.length - 1) % bestPointDerivation.length][0]; var startY = centerY + bestPointDerivation[(pointIndex + bestPointDerivation.length - 1) % bestPointDerivation.length][1]; diff --git a/binaries/data/mods/public/maps/random/rmgen2/gaia.js b/binaries/data/mods/public/maps/random/rmgen2/gaia.js index a84824ad82..91bfca5a65 100644 --- a/binaries/data/mods/public/maps/random/rmgen2/gaia.js +++ b/binaries/data/mods/public/maps/random/rmgen2/gaia.js @@ -97,7 +97,7 @@ function addBluffs(constraint, size, deviation, fill, baseHeight) // Create an entrance area by using a small margin var margin = 0.08; var ground = createTerrain(g_Terrains.mainTerrain); - var slopeLength = (1 - margin) * getDistance(baseLine.midX, baseLine.midZ, endLine.midX, endLine.midZ); + var slopeLength = (1 - margin) * Math.euclidDistance2D(baseLine.midX, baseLine.midZ, endLine.midX, endLine.midZ); // Adjust the height of each point in the bluff for (var p = 0; p < points.length; ++p) diff --git a/binaries/data/mods/public/maps/random/rmgen2/setup.js b/binaries/data/mods/public/maps/random/rmgen2/setup.js index 5e61c20dde..5e51523f61 100644 --- a/binaries/data/mods/public/maps/random/rmgen2/setup.js +++ b/binaries/data/mods/public/maps/random/rmgen2/setup.js @@ -399,7 +399,7 @@ function placeRandom(playerIDs) var z = 0.5 + distance * sin(playerAngle); // Minimum distance between initial bases must be a quarter of the map diameter - if (locations.some(loc => getDistance(x, z, loc.x, loc.z) < 0.25)) + if (locations.some(loc => Math.euclidDistance2D(x, z, loc.x, loc.z) < 0.25)) { --i; ++attempts; @@ -460,7 +460,7 @@ function groupPlayersByLocations(playerIDs, locations) ++teamSize; if (team1 != -1 && team1 == team2) - teamDist += getDistance(permutation[i - 1].x, permutation[i - 1].z, permutation[i].x, permutation[i].z); + teamDist += Math.euclidDistance2D(permutation[i - 1].x, permutation[i - 1].z, permutation[i].x, permutation[i].z); else { dist += teamDist / teamSize; diff --git a/binaries/data/mods/public/maps/random/schwarzwald.js b/binaries/data/mods/public/maps/random/schwarzwald.js index ad79da020c..aa6500754b 100644 --- a/binaries/data/mods/public/maps/random/schwarzwald.js +++ b/binaries/data/mods/public/maps/random/schwarzwald.js @@ -316,7 +316,7 @@ for (var i = 0; i < maxI; i++) z += round(sin(angle + randFloat(-pathAngleOff, pathAngleOff))); } - if (getDistance(x, z, targetX, targetZ) < pathSucsessRadius) + if (Math.euclidDistance2D(x, z, targetX, targetZ) < pathSucsessRadius) targetReached = true; tries++; @@ -378,10 +378,11 @@ for (var x = 0; x < mapSize; x++) { for (var z = 0;z < mapSize;z++) { - var radius = Math.pow(Math.pow(mapCenterX - x - 0.5, 2) + Math.pow(mapCenterZ - z - 0.5, 2), 1/2); // The 0.5 is a correction for the entities placed on the center of tiles + // The 0.5 is a correction for the entities placed on the center of tiles + var radius = Math.euclidDistance2D(x + 0.5, z + 0.5, mapCenterX, mapCenterZ); var minDistToSL = mapSize; for (var i=0; i < numPlayers; i++) - minDistToSL = min(minDistToSL, getDistance(playerStartLocX[i], playerStartLocZ[i], x, z)); + minDistToSL = min(minDistToSL, Math.euclidDistance2D(playerStartLocX[i], playerStartLocZ[i], x, z)); // Woods tile based var tDensFactSL = max(min((minDistToSL - baseRadius) / baseRadius, 1), 0); diff --git a/binaries/data/mods/public/maps/random/snowflake_searocks.js b/binaries/data/mods/public/maps/random/snowflake_searocks.js index bdf18bd437..01cc34427a 100644 --- a/binaries/data/mods/public/maps/random/snowflake_searocks.js +++ b/binaries/data/mods/public/maps/random/snowflake_searocks.js @@ -307,7 +307,7 @@ for (let ix = 0; ix < mapSize; ++ix) let islandDistZ = islandZ[m] - islandZ[n]; let d1 = islandDistX * (iz - islandZ[m]) + islandDistZ * (ix - islandX[m]); - let d2 = Math.pow(islandDistX, 2) + Math.pow(islandDistZ, 2); + let d2 = Math.square(islandDistX) + Math.square(islandDistZ); let dis = Math.abs(d1) / Math.sqrt(d2); let z = iz - islandDistX * d1 / d2; diff --git a/binaries/data/mods/public/maps/random/wild_lake.js b/binaries/data/mods/public/maps/random/wild_lake.js index 366bd71ee3..a1b5ec9f3a 100644 --- a/binaries/data/mods/public/maps/random/wild_lake.js +++ b/binaries/data/mods/public/maps/random/wild_lake.js @@ -507,10 +507,11 @@ if (teams.length) if (t2 != t1) continue; - let l1 = startLocations[pi]; - let l2 = startLocations[pj]; - let dist = getDistance(l1.x, l1.y, l2.x, l2.y); - maxTeamDist = Math.max(dist, maxTeamDist); + maxTeamDist = Math.max( + maxTeamDist, + Math.euclidDistance2D( + startLocations[pi].x, startLocations[pi].y, + startLocations[pj].x, startLocations[pj].y)); } } -- 2.11.4.GIT