From e95f4e9744e601fc61983cbc1e23a9e76e2b5faf Mon Sep 17 00:00:00 2001 From: elexis Date: Wed, 6 Sep 2017 20:58:27 +0000 Subject: [PATCH] Unify deepcopy and clone. Delete clone globalscripts function introduced by 9f47ed536d. Rename the better supported deepcopy function to clone. Delete unused Vector2D and Vector3D clone prototype functions that can just use clone if needed. Differential Revision: https://code.wildfiregames.com/D870 Reviewed By: leper This was SVN commit r20125. --- .../data/mods/public/globalscripts/utility.js | 19 ------------------- .../data/mods/public/globalscripts/vector.js | 10 ---------- .../mods/public/gui/gamesetup/gamesetup.js | 2 +- .../data/mods/public/gui/session/session.js | 2 +- .../public/maps/random/belgian_uplands.js | 6 +++--- .../public/maps/random/caledonian_meadows.js | 6 +++--- .../public/maps/random/heightmap/heightmap.js | 6 +++--- .../mods/public/maps/random/rmgen/library.js | 2 +- .../data/mods/public/maps/random/wild_lake.js | 4 ++-- .../public/simulation/components/UnitAI.js | 2 +- source/scriptinterface/ScriptInterface.cpp | 2 +- 11 files changed, 16 insertions(+), 45 deletions(-) diff --git a/binaries/data/mods/public/globalscripts/utility.js b/binaries/data/mods/public/globalscripts/utility.js index 61632c88cc..88caff2543 100644 --- a/binaries/data/mods/public/globalscripts/utility.js +++ b/binaries/data/mods/public/globalscripts/utility.js @@ -1,22 +1,3 @@ -/** - * returns a clone of a simple object or array - * Only valid JSON objects are accepted - * So no recursion, and only plain objects or arrays - */ -function clone(o) -{ - let r; - if (o instanceof Array) - r = []; - else if (o instanceof Object) - r = {}; - else // native data type - return o; - for (let key in o) - r[key] = clone(o[key]); - return r; -} - /** * "Inside-out" implementation of Fisher-Yates shuffle */ diff --git a/binaries/data/mods/public/globalscripts/vector.js b/binaries/data/mods/public/globalscripts/vector.js index 7a63ae4e04..e0da95892e 100644 --- a/binaries/data/mods/public/globalscripts/vector.js +++ b/binaries/data/mods/public/globalscripts/vector.js @@ -136,11 +136,6 @@ Vector2D.prototype.distanceTo = function(v) // Static functions that return a new vector object. // Note that object creation is slow in JS, so use them only when necessary -Vector2D.clone = function(v) -{ - return new Vector2D(v.x, v.y); -}; - Vector2D.from3D = function(v) { return new Vector2D(v.x, v.z); @@ -311,11 +306,6 @@ Vector3D.prototype.horizDistanceTo = function(v) // Static functions that return a new vector object. // Note that object creation is slow in JS, so use them only when really necessary -Vector3D.clone = function(v) -{ - return new Vector3D(v.x, v.y, v.z); -}; - Vector3D.add = function(v1, v2) { return new Vector3D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); diff --git a/binaries/data/mods/public/gui/gamesetup/gamesetup.js b/binaries/data/mods/public/gui/gamesetup/gamesetup.js index 8a302969b6..b98f92901f 100644 --- a/binaries/data/mods/public/gui/gamesetup/gamesetup.js +++ b/binaries/data/mods/public/gui/gamesetup/gamesetup.js @@ -1731,7 +1731,7 @@ function selectMap(name) g_GameAttributes.settings[prop] = undefined; let mapData = loadMapData(name); - let mapSettings = mapData && mapData.settings ? deepcopy(mapData.settings) : {}; + let mapSettings = mapData && mapData.settings ? clone(mapData.settings) : {}; // Reset victory conditions if (g_GameAttributes.mapType != "random") diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index a88fec77da..948cd87c90 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -1123,7 +1123,7 @@ function updateDebug() debug.hidden = false; - let conciseSimState = deepcopy(GetSimState()); + let conciseSimState = clone(GetSimState()); conciseSimState.players = "<<>>"; let text = "simulation: " + uneval(conciseSimState); diff --git a/binaries/data/mods/public/maps/random/belgian_uplands.js b/binaries/data/mods/public/maps/random/belgian_uplands.js index 5bb48387ba..4a30742573 100644 --- a/binaries/data/mods/public/maps/random/belgian_uplands.js +++ b/binaries/data/mods/public/maps/random/belgian_uplands.js @@ -118,7 +118,7 @@ while (!goodStartPositionsFound) log("Starting giant while loop try " + tries); // Generate reliefmap - var myReliefmap = deepcopy(g_Map.height); + var myReliefmap = clone(g_Map.height); setRandomHeightmap(heightRange.min, heightRange.max, myReliefmap); for (var i = 0; i < 50 + mapSize/4; i++) // Cycles depend on mapsize (more cycles -> bigger structures) globalSmoothHeightmap(0.8, myReliefmap); @@ -175,7 +175,7 @@ while (!goodStartPositionsFound) possibleStartPositionsTemp.push(possibleStartPositions[i]); // placeTerrain(possibleStartPositions[i][0], possibleStartPositions[i][1], "purple"); // Only works properly for 1 loop } - possibleStartPositions = deepcopy(possibleStartPositionsTemp); + 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 = []; @@ -210,7 +210,7 @@ while (!goodStartPositionsFound) // placeTerrain(possibleStartPositions[i][0], possibleStartPositions[i][1], "red"); // Only works properly for 1 loop } - possibleStartPositions = deepcopy(possibleStartPositionsTemp); + possibleStartPositions = clone(possibleStartPositionsTemp); if(possibleStartPositions.length > numPlayers) enoughTiles = true; diff --git a/binaries/data/mods/public/maps/random/caledonian_meadows.js b/binaries/data/mods/public/maps/random/caledonian_meadows.js index 1e8468cf4f..e0872ae0aa 100644 --- a/binaries/data/mods/public/maps/random/caledonian_meadows.js +++ b/binaries/data/mods/public/maps/random/caledonian_meadows.js @@ -13,7 +13,7 @@ function placeRandomPathToHeight( width = 10, distance = 4, strength = 0.08, heightmap = g_Map.height) { let pathPoints = []; - let position = deepcopy(start); + let position = clone(start); while (true) { rectangularSmoothToHeight(position, width, width, targetHeight, strength, heightmap); @@ -85,7 +85,7 @@ let fences = [ ]; let num = fences.length; for (let i = 0; i < num; ++i) - fences.push(new Fortress("fence", deepcopy(fences[i].wall).reverse())); + fences.push(new Fortress("fence", clone(fences[i].wall).reverse())); // Groves, only Wood let groveEntities = ["gaia/flora_bush_temperate", "gaia/flora_tree_euro_beech"]; @@ -400,7 +400,7 @@ RMS.SetProgress(45); /** * Get resource spots after players start locations calculation */ -let avoidPoints = deepcopy(startLocations); +let avoidPoints = clone(startLocations); for (let i = 0; i < avoidPoints.length; ++i) avoidPoints[i].dist = 30; let resourceSpots = getPointsByHeight({ "min": (heighLimits[3] + heighLimits[4]) / 2, "max": (heighLimits[5] + heighLimits[6]) / 2 }, avoidPoints, clPath); diff --git a/binaries/data/mods/public/maps/random/heightmap/heightmap.js b/binaries/data/mods/public/maps/random/heightmap/heightmap.js index 564ed7de47..7dec1a255b 100644 --- a/binaries/data/mods/public/maps/random/heightmap/heightmap.js +++ b/binaries/data/mods/public/maps/random/heightmap/heightmap.js @@ -250,7 +250,7 @@ function setBaseTerrainDiamondSquare(minHeight = MIN_HEIGHT, maxHeight = MAX_HEI } } } - initialHeightmap = deepcopy(newHeightmap); + initialHeightmap = clone(newHeightmap); offset /= Math.pow(2, smoothness); } @@ -269,7 +269,7 @@ function setBaseTerrainDiamondSquare(minHeight = MIN_HEIGHT, maxHeight = MAX_HEI */ function globalSmoothHeightmap(strength = 0.8, heightmap = g_Map.height, smoothMap = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]) { - let referenceHeightmap = deepcopy(heightmap); + let referenceHeightmap = clone(heightmap); let max_x = heightmap.length; let max_y = heightmap[0].length; for (let x = 0; x < max_x; ++x) @@ -350,7 +350,7 @@ function rectangularSmoothToHeight(center, dx, dy, targetHeight, strength = 0.8, function getPointsByHeight(heightRange, avoidPoints = [], avoidClass = undefined, minDistance = 20, maxTries = 2 * g_Map.size, heightmap = g_Map.height, isCircular = g_MapSettings.CircularMap) { let points = []; - let placements = deepcopy(avoidPoints); + let placements = clone(avoidPoints); let validVertices = []; let r = 0.5 * (heightmap.length - 1); // Map center x/y as well as radius let avoidMap; diff --git a/binaries/data/mods/public/maps/random/rmgen/library.js b/binaries/data/mods/public/maps/random/rmgen/library.js index a2259dd9db..7f4e984363 100644 --- a/binaries/data/mods/public/maps/random/rmgen/library.js +++ b/binaries/data/mods/public/maps/random/rmgen/library.js @@ -570,7 +570,7 @@ function getOrderOfPointsForShortestClosePath(points) } // Just add the first 3 points - let pointsToAdd = deepcopy(points); + let pointsToAdd = clone(points); for (let i = 0; i < 3; ++i) { order.push(i); diff --git a/binaries/data/mods/public/maps/random/wild_lake.js b/binaries/data/mods/public/maps/random/wild_lake.js index 11df70bed5..0b9c44ff13 100644 --- a/binaries/data/mods/public/maps/random/wild_lake.js +++ b/binaries/data/mods/public/maps/random/wild_lake.js @@ -299,7 +299,7 @@ let fences = [ ]; let num = fences.length; for (let i = 0; i < num; ++i) - fences.push(new Fortress("fence", deepcopy(fences[i].wall).reverse())); + fences.push(new Fortress("fence", clone(fences[i].wall).reverse())); // Camps with fire and gold treasure function placeCamp(point, @@ -647,7 +647,7 @@ RMS.SetProgress(80); /** * Get resource spots after players start locations calculation and paths */ -let avoidPoints = deepcopy(startLocations); +let avoidPoints = clone(startLocations); for (let i = 0; i < avoidPoints.length; ++i) avoidPoints[i].dist = 30; let resourceSpots = getPointsByHeight(resourceSpotHeightRange, avoidPoints, clPath); diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index b92ecc85cb..940ac478a0 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -3890,7 +3890,7 @@ UnitAI.prototype.GetOrderData = function() var orders = []; for (let order of this.orderQueue) if (order.data) - orders.push(deepcopy(order.data)); + orders.push(clone(order.data)); return orders; }; diff --git a/source/scriptinterface/ScriptInterface.cpp b/source/scriptinterface/ScriptInterface.cpp index f52b078890..65c04bb514 100644 --- a/source/scriptinterface/ScriptInterface.cpp +++ b/source/scriptinterface/ScriptInterface.cpp @@ -389,7 +389,7 @@ ScriptInterface_impl::ScriptInterface_impl(const char* nativeScopeName, const sh JS_DefineFunction(m_cx, globalRootedVal, "log", ::logmsg, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "warn", ::warn, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "error", ::error, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); - JS_DefineFunction(m_cx, globalRootedVal, "deepcopy", ::deepcopy, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); + JS_DefineFunction(m_cx, globalRootedVal, "clone", ::deepcopy, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(m_cx, globalRootedVal, "deepfreeze", ::deepfreeze, 1, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); Register("ProfileStart", ::ProfileStart, 1);