Page MenuHomeWildfire Games
Paste P144

rmgen cleanup
ActivePublic

Authored by smiley on Nov 21 2018, 6:25 PM.
Index: binaries/data/mods/public/maps/random/rmgen/Constraint.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/Constraint.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/Constraint.js (working copy)
@@ -17,7 +17,7 @@
*/
function AndConstraint(constraints)
{
- if (constraints instanceof Array)
+ if (Array.isArray(constraints))
this.constraints = constraints
else if (!constraints)
this.constraints = [];
Index: binaries/data/mods/public/maps/random/rmgen/Group.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/Group.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/Group.js (working copy)
@@ -18,22 +18,14 @@
* @param tileClass - Optional TileClass that tiles with placed entities are marked with.
* @param centerPosition - The location the group is placed around. Can be omitted if the property is set externally.
*/
-function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
+function SimpleGroup(objects, avoidSelf = false, tileClass, centerPosition)
{
this.objects = objects;
this.tileClass = tileClass;
this.avoidSelf = avoidSelf;
- this.centerPosition = undefined;
-
- if (centerPosition)
- this.setCenterPosition(centerPosition);
+ this.centerPosition = centerPosition ? deepfreeze(centerPosition.clone().round()) : undefined;
}
-SimpleGroup.prototype.setCenterPosition = function(position)
-{
- this.centerPosition = deepfreeze(position.clone().round());
-};
-
SimpleGroup.prototype.place = function(playerID, constraint)
{
let entitySpecsResult = [];
@@ -75,16 +67,11 @@
/**
* Randomly choses one of the given Objects and places it just like the SimpleGroup.
*/
-function RandomGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
+function RandomGroup(objects, avoidSelf = false, tileClass, centerPosition)
{
this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, centerPosition);
}
-RandomGroup.prototype.setCenterPosition = function(position)
-{
- this.simpleGroup.setCenterPosition(position);
-};
-
RandomGroup.prototype.place = function(playerID, constraint)
{
return this.simpleGroup.place(playerID, constraint);
Index: binaries/data/mods/public/maps/random/rmgen/Object.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/Object.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/Object.js (working copy)
@@ -30,10 +30,10 @@
{
let entitySpecs = [];
let numRetries = 0;
- let validTile = pos => this.templateName.startsWith(g_ActorPrefix) ? g_Map.validTile(pos) : g_Map.validTilePassable(pos);
+ let validTile = pos => this.templateName.startsWith("actor|") ? g_Map.validTile(pos) : g_Map.validTilePassable(pos);
for (let i = 0; i < randIntInclusive(this.minCount, this.maxCount); ++i)
- while (true)
+ while (numRetries < maxRetries)
{
let distance = randFloat(this.minDistance, this.maxDistance);
let angle = randomAngle();
@@ -54,12 +54,9 @@
});
break;
}
-
- if (numRetries++ > maxRetries)
- return undefined;
}
- return entitySpecs;
+ return entitySpecs.length ? entitySpecs : undefined;
};
/**
Index: binaries/data/mods/public/maps/random/rmgen/RandomMap.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/RandomMap.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/RandomMap.js (working copy)
@@ -11,9 +11,8 @@
// Size must be 0 to 1024, divisible by patches
this.size = g_MapSettings.Size;
- // Create name <-> id maps for textures
- this.nameToID = {};
- this.IDToName = [];
+ // Texture names, the index would be the textureID
+ this.textureNames = [];
// Texture 2D array
this.texture = [];
@@ -110,13 +109,11 @@
*/
RandomMap.prototype.getTextureID = function(texture)
{
- if (texture in this.nameToID)
- return this.nameToID[texture];
+ if (texture in this.textureNames)
+ return this.textureNames.indexOf(texture);
- let id = this.IDToName.length;
- this.nameToID[texture] = id;
- this.IDToName[id] = texture;
-
+ let id = this.textureNames.length;
+ this.textureNames[id] = texture;
return id;
};
@@ -235,7 +232,7 @@
if (!this.inMapBounds(position))
throw new Error("getTexture: invalid tile position " + uneval(position));
- return this.IDToName[this.texture[position.x][position.y]];
+ return this.textureNames[this.texture[position.x][position.y]];
};
/**
@@ -426,7 +423,7 @@
this.logger.printDirectly(
"Total entities: " + this.entities.length + ", " +
"Terrain entities: " + (this.entities.length - nonTerrainCount) + ", " +
- "Textures: " + this.IDToName.length + ".\n");
+ "Textures: " + this.textureNames.length + ".\n");
return this.entities;
};
@@ -486,7 +483,7 @@
"height": this.exportHeightData(),
"seaLevel": SEA_LEVEL,
"size": this.size,
- "textureNames": this.IDToName,
+ "textureNames": this.textureNames,
"tileData": this.exportTerrainTextures(),
"Camera": g_Camera,
"Environment": g_Environment
Index: binaries/data/mods/public/maps/random/rmgen/Terrain.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/Terrain.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/Terrain.js (working copy)
@@ -8,7 +8,7 @@
* Optionally it places an entity on the affected tiles and
* replaces prior entities added by SimpleTerrain on the same tile.
*/
-function SimpleTerrain(texture, templateName = undefined)
+function SimpleTerrain(texture, templateName)
{
if (texture === undefined)
throw new Error("SimpleTerrain: texture not defined");
@@ -32,7 +32,7 @@
*/
function RandomTerrain(terrains)
{
- if (!(terrains instanceof Array) || !terrains.length)
+ if (!Array.isArray(terrains) || !terrains.length)
throw new Error("RandomTerrain: Invalid terrains array");
this.terrains = terrains;
Index: binaries/data/mods/public/maps/random/rmgen/library.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/library.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/library.js (working copy)
@@ -49,8 +49,6 @@
const g_CivData = deepfreeze(loadCivFiles(false));
-const g_ActorPrefix = "actor|";
-
/**
* Sets whether setHeight operates on the center of a tile or on the vertices.
*/
@@ -58,7 +56,7 @@
function actorTemplate(templateName)
{
- return g_ActorPrefix + templateName + ".xml";
+ return "actor|" + templateName + ".xml";
}
function getObstructionSize(templateName, margin = 0)
@@ -80,17 +78,17 @@
function fractionToTiles(f)
{
- return g_MapSettings.Size * f;
+ return g_Map.getSize() * f;
}
function tilesToFraction(t)
{
- return t / g_MapSettings.Size;
+ return t / g_Map.getSize();
}
function scaleByMapSize(min, max, minMapSize = 128, maxMapSize = 512)
{
- return min + (max - min) * (g_MapSettings.Size - minMapSize) / (maxMapSize - minMapSize);
+ return min + (max - min) * (g_Map.getSize() - minMapSize) / (maxMapSize - minMapSize);
}
function randomPositionOnTile(tilePosition)
@@ -252,8 +250,8 @@
function avoidClasses(/*class1, dist1, class2, dist2, etc*/)
{
let ar = [];
- for (let i = 0; i < arguments.length/2; ++i)
- ar.push(new AvoidTileClassConstraint(arguments[2*i], arguments[2*i+1]));
+ for (let i = 0; i < arguments.length; i += 2)
+ ar.push(new AvoidTileClassConstraint(arguments[i], arguments[i + 1]));
// Return single constraint
if (ar.length == 1)
@@ -268,8 +266,8 @@
function stayClasses(/*class1, dist1, class2, dist2, etc*/)
{
let ar = [];
- for (let i = 0; i < arguments.length/2; ++i)
- ar.push(new StayInTileClassConstraint(arguments[2*i], arguments[2*i+1]));
+ for (let i = 0; i < arguments.length; i += 2)
+ ar.push(new AvoidTileClassConstraint(arguments[i], arguments[i + 1]));
// Return single constraint
if (ar.length == 1)
@@ -284,8 +282,8 @@
function borderClasses(/*class1, idist1, odist1, class2, idist2, odist2, etc*/)
{
let ar = [];
- for (let i = 0; i < arguments.length/3; ++i)
- ar.push(new BorderTileClassConstraint(arguments[3*i], arguments[3*i+1], arguments[3*i+2]));
+ for (let i = 0; i < arguments.length; i += 3)
+ ar.push(new BorderTileClassConstraint(arguments[i], arguments[i+1], arguments[i+2]));
// Return single constraint
if (ar.length == 1)
Index: binaries/data/mods/public/maps/random/rmgen/painter/HeightmapPainter.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/painter/HeightmapPainter.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/painter/HeightmapPainter.js (working copy)
@@ -7,7 +7,7 @@
* @param {Number} [normalMinHeight] - The minimum height the elevation grid of 320 tiles would have.
* @param {Number} [normalMaxHeight] - The maximum height the elevation grid of 320 tiles would have.
*/
-function HeightmapPainter(heightmap, normalMinHeight = undefined, normalMaxHeight = undefined)
+function HeightmapPainter(heightmap, normalMinHeight, normalMaxHeight)
{
this.heightmap = heightmap;
this.bicubicInterpolation = bicubicInterpolation;
Index: binaries/data/mods/public/maps/random/rmgen/painter/LayeredPainter.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/painter/LayeredPainter.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/painter/LayeredPainter.js (working copy)
@@ -9,7 +9,7 @@
*/
function LayeredPainter(terrainArray, widths)
{
- if (!(terrainArray instanceof Array))
+ if (!Array.isArray(terrainArray))
throw new Error("LayeredPainter: terrains must be an array!");
this.terrains = terrainArray.map(terrain => createTerrain(terrain));
Index: binaries/data/mods/public/maps/random/rmgen/painter/MultiPainter.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/painter/MultiPainter.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/painter/MultiPainter.js (working copy)
@@ -3,7 +3,7 @@
*/
function MultiPainter(painters)
{
- if (painters instanceof Array)
+ if (Array.isArray(painters))
this.painters = painters;
else if (!painters)
this.painters = [];
Index: binaries/data/mods/public/maps/random/rmgen/painter/SmoothElevationPainter.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/painter/SmoothElevationPainter.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/painter/SmoothElevationPainter.js (working copy)
@@ -49,15 +49,15 @@
for (let dx = -1; dx < 1 + brushSize; ++dx)
{
let nx = point.x + dx;
- for (let dz = -1; dz < 1 + brushSize; ++dz)
+ for (let dy = -1; dy < 1 + brushSize; ++dy)
{
- let nz = point.y + dz;
- let position = new Vector2D(nx, nz);
+ let ny = point.y + dy;
+ let position = new Vector2D(nx, ny);
- if (g_Map.validHeight(position) && !gotHeightPt[nx][nz])
+ if (g_Map.validHeight(position) && !gotHeightPt[nx][ny])
{
- newHeight[nx][nz] = g_Map.getHeight(position);
- gotHeightPt[nx][nz] = 1;
+ newHeight[nx][ny] = g_Map.getHeight(position);
+ gotHeightPt[nx][ny] = 1;
heightPoints.push(position);
}
}
@@ -96,13 +96,13 @@
{
let nx = point.x + dx;
- for (let dz = -1; dz <= 1; ++dz)
+ for (let dy = -1; dy <= 1; ++dy)
{
- let nz = point.y + dz;
+ let ny = point.y + dy;
- if (g_Map.validHeight(new Vector2D(nx, nz)))
+ if (g_Map.validHeight(new Vector2D(nx, ny)))
{
- sum += newHeight[nx][nz];
+ sum += newHeight[nx][ny];
++count;
}
}
@@ -143,16 +143,16 @@
for (let dx = -1; dx < 1 + args.brushSize; ++dx)
{
let nx = point.x + dx;
- for (let dz = -1; dz < 1 + args.brushSize; ++dz)
+ for (let dy = -1; dy < 1 + args.brushSize; ++dy)
{
- let nz = point.y + dz;
- let position = new Vector2D(nx, nz);
+ let ny = point.y + dy;
+ let position = new Vector2D(nx, ny);
- if (!withinGrid(nx, nz) || args.withinArea(args.area, position) || saw[nx][nz])
+ if (!withinGrid(nx, ny) || args.withinArea(args.area, position) || saw[nx][ny])
continue;
- saw[nx][nz] = 1;
- dist[nx][nz] = 0;
+ saw[nx][ny] = 1;
+ dist[nx][ny] = 0;
pointQueue.push(position);
}
}
@@ -171,16 +171,16 @@
for (let dx = -1; dx <= 1; ++dx)
{
let nx = point.x + dx;
- for (let dz = -1; dz <= 1; ++dz)
+ for (let dy = -1; dy <= 1; ++dy)
{
- let nz = point.y + dz;
- let position = new Vector2D(nx, nz);
+ let ny = point.y + dy;
+ let position = new Vector2D(nx, ny);
- if (!withinGrid(nx, nz) || !args.withinArea(args.area, position) || saw[nx][nz])
+ if (!withinGrid(nx, ny) || !args.withinArea(args.area, position) || saw[nx][ny])
continue;
- saw[nx][nz] = 1;
- dist[nx][nz] = distance + 1;
+ saw[nx][ny] = 1;
+ dist[nx][ny] = distance + 1;
pointQueue.push(position);
}
}
Index: binaries/data/mods/public/maps/random/rmgen/placer/centered/ChainPlacer.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/placer/centered/ChainPlacer.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/placer/centered/ChainPlacer.js (working copy)
@@ -9,7 +9,7 @@
* @param {number} [maxDistance] - Farthest distance from the center.
* @param {number[]} [queue] - When given, uses these radiuses for the first circles.
*/
-function ChainPlacer(minRadius, maxRadius, numCircles, failFraction = 0, centerPosition = undefined, maxDistance = 0, queue = [])
+function ChainPlacer(minRadius, maxRadius, numCircles, failFraction = 0, centerPosition, maxDistance = 0, queue = [])
{
this.minRadius = minRadius;
this.maxRadius = maxRadius;
@@ -17,17 +17,9 @@
this.failFraction = failFraction;
this.maxDistance = maxDistance;
this.queue = queue.map(radius => Math.floor(radius));
- this.centerPosition = undefined;
-
- if (centerPosition)
- this.setCenterPosition(centerPosition);
+ this.centerPosition = centerPosition ? deepfreeze(centerPosition.round()) : undefined;
}
-ChainPlacer.prototype.setCenterPosition = function(position)
-{
- this.centerPosition = deepfreeze(position.clone().round());
-};
-
ChainPlacer.prototype.place = function(constraint)
{
// Preliminary bounds check
Index: binaries/data/mods/public/maps/random/rmgen/placer/centered/ClumpPlacer.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/placer/centered/ClumpPlacer.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/placer/centered/ClumpPlacer.js (working copy)
@@ -13,17 +13,9 @@
this.coherence = coherence;
this.smoothness = smoothness;
this.failFraction = failFraction;
- this.centerPosition = undefined;
-
- if (centerPosition)
- this.setCenterPosition(centerPosition);
+ this.centerPosition = centerPosition ? deepfreeze(centerPosition.round()) : undefined;
}
-ClumpPlacer.prototype.setCenterPosition = function(position)
-{
- this.centerPosition = deepfreeze(position.clone().round());
-};
-
ClumpPlacer.prototype.place = function(constraint)
{
// Preliminary bounds check
@@ -38,7 +30,7 @@
var perim = 4 * radius * 2 * Math.PI;
var intPerim = Math.ceil(perim);
- var ctrlPts = 1 + Math.floor(1.0/Math.max(this.smoothness,1.0/intPerim));
+ var ctrlPts = 1 + Math.floor(1.0 / Math.max(this.smoothness, 1.0 / intPerim));
if (ctrlPts > radius * 2 * Math.PI)
ctrlPts = Math.floor(radius * 2 * Math.PI) + 1;
@@ -48,7 +40,7 @@
var ctrlVals = new Float32Array(ctrlPts+1); //float32
// Generate some interpolated noise
- for (var i=0; i < ctrlPts; i++)
+ for (let i = 0; i < ctrlPts; ++i)
{
ctrlCoords[i] = i * perim / ctrlPts;
ctrlVals[i] = randFloat(0, 2);
@@ -55,14 +47,14 @@
}
let c = 0;
- let looped = 0;
+ let looped = false;
for (let i = 0; i < intPerim; ++i)
{
- if (ctrlCoords[(c+1) % ctrlPts] < i && !looped)
+ if (ctrlCoords[(c + 1) % ctrlPts] < i && !looped)
{
- c = (c+1) % ctrlPts;
- if (c == ctrlPts-1)
- looped = 1;
+ c = (c + 1) % ctrlPts;
+ if (c == ctrlPts - 1)
+ looped = true;
}
noise[i] = cubicInterpolation(
Index: binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen/placer/centered/DiskPlacer.js (working copy)
@@ -4,17 +4,9 @@
function DiskPlacer(radius, centerPosition = undefined)
{
this.radiusSquared = Math.square(radius);
- this.centerPosition = undefined;
-
- if (centerPosition)
- this.setCenterPosition(centerPosition);
+ this.centerPosition = centerPosition ? deepfreeze(centerPosition.round()) : undefined;
}
-DiskPlacer.prototype.setCenterPosition = function(position)
-{
- this.centerPosition = deepfreeze(position.clone().round());
-};
-
DiskPlacer.prototype.place = function(constraint)
{
let points = [];
Index: binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js
===================================================================
--- binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js (revision 21929)
+++ binaries/data/mods/public/maps/random/rmgen-common/gaia_terrain.js (working copy)
@@ -10,7 +10,7 @@
g_Map.log("Creating bumps");
createAreas(
new ChainPlacer(
- minSize || 1,
+ minSize || 1, // would have been prettier if these were up there
maxSize || Math.floor(scaleByMapSize(4, 6)),
spread || Math.floor(scaleByMapSize(2, 5)),
failFraction),
@@ -65,7 +65,7 @@
/**
* Create a mountain using a technique very similar to ChainPlacer.
*/
-function createMountain(maxHeight, minRadius, maxRadius, numCircles, constraints, x, z, terrain, tileClass, fcc = 0, q = [])
+function createMountain(maxHeight, minRadius, maxRadius, numCircles, constraints, x, z, terrain, tileClass, fcc = 0, q = []) // Vector
{
let position = new Vector2D(x, z);
let constraint = new AndConstraint(constraints);

Event Timeline

smiley created this paste.Nov 21 2018, 6:25 PM

Havent really checked for correctness. This was pretty old. Pasted because this was almost lost. Some stuff might be useful.
And I didnt really feel like creating a whole new proposal just for these. Can fix when changing something near.

elexis added a subscriber: elexis.Nov 21 2018, 6:56 PM

-function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
+function SimpleGroup(objects, avoidSelf = false, tileClass, centerPosition)

Consequentially the = false could be dropped too. But mostly the = undefined is a way to inform the reader that the argument is optional. It's not obvious for the centerPosition and tileclass.

// Vector -> Why? @param

+ minSize || 1, // would have been prettier if these were up there

I feel the hardcoded constants in rmgen-common make more trouble than they save work. If they are removed, the proxies don't save work but only hide the mechanics of placement, making it harder to adjust, growing the libraries bigger without providing something useful. Just endless monkeywork to replace all that.

Any reason for not having centered placers implement the centered placer interface method setCenterPosition?


  • return min + (max - min) * (g_MapSettings.Size - minMapSize) / (maxMapSize - minMapSize);

+ return min + (max - min) * (g_Map.getSize() - minMapSize) / (maxMapSize - minMapSize);

g_Map should be and is defined by the map. Where the library refers to it its a defect.

One could consider passing g_MapSettings to the mapinit and read via g_Map if one wants to get rid of g_MapSettings. The g_Map instance should be passed, or it could be called to become the this keyword or similar.

+ if (texture in this.textureNames)
could also be this.textureNames[texture], also maybe textureName, anyway

would be the textureID

is

g_ActorPrefix

Ideally not existing, but inlining it sounds a bit like cheating. The logic itself should become unneeded by redesigning the code flow somehow - otherwise we can keep it.
Looking at the header of maps where all the terrain textures and entity template names are defined, we could perhaps have a constructor for each of them, like Terrain(textureName, actorTemplate = undefined), ActorEntityTemplate(), SimulationEntityTemplate(). Then the actorTemplate function could be removed, the global too and there was only occurrence of "actor|". Maybe. Would have to test if that's better.

tilesToFraction exists only in 5 lines, could be daring and inline.
Wondering if we could silently dump fractionToTiles as well. foo(x) doesnt seem much more readable or useful than x * g_Map.getSize().
Problem is 277 replacements for the gain of 3 lines less. (So you see why it's important to insert new code cleanly rather than cleaning up afterwards)

1.0 -> 1

Didn't check if Object.js change is correct.
Better split patches by logic and start writing them when we can commit them, so we avoid merge conflict hell.

(Missing increment in the while loop L74)

Sounds good, would make separate revision proposals.

elexis changed the visibility from "All Users" to "Public (No Login Required)".Mar 20 2019, 4:17 PM