1
0
forked from mirrors/0ad

Return vectors instead of objects with x and z property for all Placers and Painters, refs #4992, #4845.

Create the vectors sooner, so that the conditions can use them
eventually too and since the loops might be replaced with vector
getters.
Fix missing comma in Deep Forest de2f30c7b9.
No measurable performance decline was noticed.

This was SVN commit r20977.
This commit is contained in:
elexis
2018-01-23 14:49:31 +00:00
parent 5447a20318
commit 2ac3d3119d
6 changed files with 40 additions and 29 deletions
@@ -181,7 +181,7 @@ for (var x = 0; x < mapSize; x++)
{
let border = tDensActual < randFloat(0, bushChance * maxTreeDensity);
createArea(
new RectPlacer(position.x, position.y, position.x + 1, position.y + 1)
new RectPlacer(position.x, position.y, position.x + 1, position.y + 1),
[
new TerrainPainter(border ? terrainWoodBorder : terrainWood),
new ElevationPainter(randFloat(0, 1)),
@@ -120,7 +120,7 @@ function randomizeCoordinatesFromAreas(obj, areas)
{
let pt = pickRandom(pickRandom(areas).points);
obj.x = pt.x;
obj.z = pt.z;
obj.z = pt.y;
}
// TODO this is a hack to simulate the old behaviour of those functions
@@ -14,7 +14,7 @@ function TileClassPainter(tileClass)
TileClassPainter.prototype.paint = function(area)
{
for (let point of area.points)
this.tileClass.add(point.x, point.z);
this.tileClass.add(point.x, point.y);
};
/**
@@ -28,7 +28,7 @@ function TileClassUnPainter(tileClass)
TileClassUnPainter.prototype.paint = function(area)
{
for (let point of area.points)
this.tileClass.remove(point.x, point.z);
this.tileClass.remove(point.x, point.y);
};
/**
@@ -57,7 +57,7 @@ function TerrainPainter(terrain)
TerrainPainter.prototype.paint = function(area)
{
for (let point of area.points)
this.terrain.place(point.x, point.z);
this.terrain.place(point.x, point.y);
};
/**
@@ -96,7 +96,7 @@ LayeredPainter.prototype.paint = function(area)
break;
}
this.terrains[i].place(point.x, point.z);
this.terrains[i].place(point.x, point.y);
}
});
};
@@ -115,7 +115,7 @@ ElevationPainter.prototype.paint = function(area)
for (let [dx, dz] of [[0, 0], [1, 0], [0, 1], [1, 1]])
{
let x = point.x + dx;
let z = point.z + dz;
let z = point.y + dz;
if (g_Map.inMapBounds(x, z))
g_Map.height[x][z] = this.elevation;
@@ -175,13 +175,14 @@ SmoothElevationPainter.prototype.paint = function(area)
let nx = point.x + dx;
for (let dz = -1; dz < 1 + brushSize; ++dz)
{
let nz = point.z + dz;
let nz = point.y + dz;
let position = new Vector2D(nx, nz);
if (g_Map.validH(nx, nz) && !gotHeightPt[nx][nz])
{
newHeight[nx][nz] = g_Map.height[nx][nz];
gotHeightPt[nx][nz] = 1;
heightPoints.push({ "x": nx, "z": nz });
heightPoints.push(position);
}
}
}
@@ -207,9 +208,9 @@ SmoothElevationPainter.prototype.paint = function(area)
a = (distance - 1) / this.blendRadius;
if (this.type == ELEVATION_SET)
newHeight[point.x][point.z] = (1 - a) * g_Map.height[point.x][point.z];
newHeight[point.x][point.y] = (1 - a) * g_Map.height[point.x][point.y];
newHeight[point.x][point.z] += a * this.elevation + randFloat(-0.5, 0.5) * this.randomElevation;
newHeight[point.x][point.y] += a * this.elevation + randFloat(-0.5, 0.5) * this.randomElevation;
}
});
@@ -217,7 +218,7 @@ SmoothElevationPainter.prototype.paint = function(area)
let areaID = area.getID();
for (let point of heightPoints)
{
if (!withinArea(areaID, point.x, point.z))
if (!withinArea(areaID, point.x, point.y))
continue;
let count = 0;
@@ -229,7 +230,7 @@ SmoothElevationPainter.prototype.paint = function(area)
for (let dz = -1; dz <= 1; ++dz)
{
let nz = point.z + dz;
let nz = point.y + dz;
if (g_Map.validH(nx, nz))
{
@@ -239,7 +240,7 @@ SmoothElevationPainter.prototype.paint = function(area)
}
}
g_Map.height[point.x][point.z] = (newHeight[point.x][point.z] + sum / count) / 2;
g_Map.height[point.x][point.y] = (newHeight[point.x][point.y] + sum / count) / 2;
}
};
@@ -277,14 +278,15 @@ function breadthFirstSearchPaint(args)
let nx = point.x + dx;
for (let dz = -1; dz < 1 + args.brushSize; ++dz)
{
let nz = point.z + dz;
let nz = point.y + dz;
let position = new Vector2D(nx, nz);
if (!withinGrid(nx, nz) || args.withinArea(areaID, nx, nz) || saw[nx][nz])
continue;
saw[nx][nz] = 1;
dist[nx][nz] = 0;
pointQueue.push({ "x": nx, "z": nz });
pointQueue.push(position);
}
}
@@ -293,9 +295,9 @@ function breadthFirstSearchPaint(args)
while (pointQueue.length)
{
let point = pointQueue.shift();
let distance = dist[point.x][point.z];
let distance = dist[point.x][point.y];
if (args.withinArea(areaID, point.x, point.z))
if (args.withinArea(areaID, point.x, point.y))
args.paintTile(point, distance);
// Enqueue neighboring points
@@ -304,14 +306,15 @@ function breadthFirstSearchPaint(args)
let nx = point.x + dx;
for (let dz = -1; dz <= 1; ++dz)
{
let nz = point.z + dz;
let nz = point.y + dz;
let position = new Vector2D(nx, nz);
if (!withinGrid(nx, nz) || !args.withinArea(areaID, nx, nz) || saw[nx][nz])
continue;
saw[nx][nz] = 1;
dist[nx][nz] = distance + 1;
pointQueue.push({ "x": nx, "z": nz });
pointQueue.push(position);
}
}
}
@@ -90,13 +90,15 @@ ClumpPlacer.prototype.place = function(constraint)
{
var i = Math.floor(xx);
var j = Math.floor(yy);
let position = new Vector2D(i, j);
if (g_Map.inMapBounds(i, j) && constraint.allows(i, j))
{
if (!gotRet[i][j])
{
// Only include each point once
gotRet[i][j] = 1;
retVec.push({ "x": i, "z": j });
retVec.push(position);
}
}
else
@@ -179,6 +181,7 @@ ChainPlacer.prototype.place = function(constraint)
for (var ix = sx; ix <= lx; ++ix)
for (var iz = sz; iz <= lz; ++ iz)
{
let position = new Vector2D(ix, iz);
dx = ix - cx;
dz = iz - cz;
if (dx * dx + dz * dz <= radius2)
@@ -188,7 +191,7 @@ ChainPlacer.prototype.place = function(constraint)
var state = gotRet[ix][iz];
if (state == -1)
{
retVec.push({ "x": ix, "z": iz });
retVec.push(position);
gotRet[ix][iz] = -2;
}
else if (state >= 0)
@@ -24,8 +24,11 @@ RectPlacer.prototype.place = function(constraint)
for (let x = this.x1; x <= this.x2; ++x)
for (let z = this.z1; z <= this.z2; ++z)
{
let position = new Vector2D(x, z);
if (constraint.allows(x, z))
points.push({ "x": x, "z": z });
points.push(position);
}
return points;
};
@@ -76,8 +79,11 @@ HeightPlacer.prototype.place = function(constraint)
for (let x = 0; x < mapSize; ++x)
for (let z = 0; z < mapSize; ++z)
{
let position = new Vector2D(x, z);
if (this.withinHeightRange(x, z) && constraint.allows(x, z))
points.push({ "x": x, "z": z });
points.push(position);
}
return points;
};
@@ -203,18 +209,17 @@ PathPlacer.prototype.place = function(constraint)
var right = Math.round(Math.max(x1, x2));
for (var x = left; x <= right; x++)
{
let position = new Vector2D(x, z);
if (constraint.allows(x, z))
{
if (g_Map.inMapBounds(x, z) && !gotRet[x][z])
{
retVec.push({ "x": x, "z": z });
retVec.push(position);
gotRet[x][z] = 1;
}
}
else
{
failed++;
}
}
};
@@ -286,7 +291,7 @@ RandomPathPlacer.prototype.place = function(constraint)
this.clumpPlacer.z = position.y;
for (let point of this.clumpPlacer.place(constraint) || [])
if (points.every(p => p.x != point.x || p.z != point.z))
if (points.every(p => p.x != point.x || p.y != point.y))
points.push(point);
}
@@ -213,7 +213,7 @@ RandomMap.prototype.createArea = function(points)
{
let areaID = ++this.areaID;
for (let p of points)
this.area[p.x][p.z] = areaID;
this.area[p.x][p.y] = areaID;
return new Area(points, areaID);
};