1
0
forked from 0ad/0ad

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.
This commit is contained in:
elexis 2017-09-06 20:58:27 +00:00
parent e1bf09977c
commit e95f4e9744
11 changed files with 16 additions and 45 deletions

View File

@ -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
*/

View File

@ -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);

View File

@ -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")

View File

@ -1123,7 +1123,7 @@ function updateDebug()
debug.hidden = false;
let conciseSimState = deepcopy(GetSimState());
let conciseSimState = clone(GetSimState());
conciseSimState.players = "<<<omitted>>>";
let text = "simulation: " + uneval(conciseSimState);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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;
};

View File

@ -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);