Index: binaries/data/mods/public/maps/random/rmgen/RandomMap.js =================================================================== --- binaries/data/mods/public/maps/random/rmgen/RandomMap.js +++ binaries/data/mods/public/maps/random/rmgen/RandomMap.js @@ -16,13 +16,15 @@ this.IDToName = []; // Texture 2D array - this.texture = []; + this.textureIDs = []; + this.texturePriorities = []; + for (let x = 0; x < this.size; ++x) { - this.texture[x] = new Uint16Array(this.size); - + this.textureIDs[x] = new Uint16Array(this.size); + this.texturePriorities = new Uint16Array(this.size); for (let z = 0; z < this.size; ++z) - this.texture[x][z] = this.getTextureID( + this.textureIDs[x][z] = this.getTextureID( typeof baseTerrain == "string" ? baseTerrain : pickRandom(baseTerrain)); } @@ -228,7 +230,7 @@ }; /** - * Returns the name of the texture of the given tile. + * Returns the name of the texture and the priority of the given tile. */ RandomMap.prototype.getTexture = function(position) { @@ -235,21 +237,38 @@ if (!this.inMapBounds(position)) throw new Error("getTexture: invalid tile position " + uneval(position)); - return this.IDToName[this.texture[position.x][position.y]]; + return this.IDToName[this.textureIDs[position.x][position.y]]; }; /** - * Paints the given texture on the given tile. + * Paints the given texture on the given tile at the given priority. */ -RandomMap.prototype.setTexture = function(position, texture) +RandomMap.prototype.setTexture = function(position, texture, priority = HIGH) { if (position.x < 0 || position.y < 0 || - position.x >= this.texture.length || - position.y >= this.texture[position.x].length) + position.x >= this.textureIDs.length || + position.y >= this.textureIDs[position.x].length) throw new Error("setTexture: invalid tile position " + uneval(position)); - this.texture[position.x][position.y] = this.getTextureID(texture); + let [x, y] = [position.x, position.y]; + + // the 8 surrounding squares. + let surroundingTiles = [ + this.texturePriorities[x - 1][y - 1], + this.texturePriorities[x][y - 1], + this.texturePriorities[x + 1][y - 1], + this.texturePriorities[x - 1][y + 1], + this.texturePriorities[x][y + 1], + this.texturePriorities[x + 1][y + 1], + this.texturePriorities[x - 1][y], + this.texturePriorities[x + 1][y] + ]; + + let priorityIndex = priority == LOW ? Math.max(0, Math.min(...surroundingTiles.priority) - 1) : Math.min(65535, Math.max(...surroundingTiles.priority) + 1); + + this.textureIDs[x][y] = this.getTextureID(texture); + this.texturePriorities[x][y] = priorityIndex; }; RandomMap.prototype.getHeight = function(position) @@ -463,9 +482,8 @@ for (let x = 0; x < this.size; ++x) for (let z = 0; z < this.size; ++z) { - // TODO: For now just use the texture's index as priority, might want to do this another way - tileIndex[z * this.size + x] = this.texture[x][z]; - tilePriority[z * this.size + x] = this.texture[x][z]; + tileIndex[z * this.size + x] = this.textureIDs[x][z]; + tilePriority[z * this.size + x] = this.texturePriorities[x][z] || this.textureIDs[x][z]; } return { Index: binaries/data/mods/public/maps/random/rmgen/Terrain.js =================================================================== --- binaries/data/mods/public/maps/random/rmgen/Terrain.js +++ binaries/data/mods/public/maps/random/rmgen/Terrain.js @@ -2,6 +2,9 @@ * @file A Terrain is a class that modifies an arbitrary property of a given tile. */ +const LOW = 0; +const HIGH = 1; + /** * SimpleTerrain paints the given texture on the terrain. * @@ -8,7 +11,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 = undefined, priority = HIGH) { if (texture === undefined) throw new Error("SimpleTerrain: texture not defined"); @@ -15,6 +18,7 @@ this.texture = texture; this.templateName = templateName; + this.priority = priority; } SimpleTerrain.prototype.place = function(position) @@ -22,7 +26,7 @@ if (this.templateName && g_Map.validTilePassable(position)) g_Map.setTerrainEntity(this.templateName, 0, Vector2D.add(position, new Vector2D(0.5, 0.5)), randomAngle()); - g_Map.setTexture(position, this.texture); + g_Map.setTexture(position, this.texture, this.priority); }; /**