Changeset View
Standalone View
binaries/data/mods/public/maps/random/rmgen/RandomMap.js
Show All 10 Lines | function RandomMap(baseHeight, baseTerrain) | ||||
// Size must be 0 to 1024, divisible by patches | // Size must be 0 to 1024, divisible by patches | ||||
this.size = g_MapSettings.Size; | this.size = g_MapSettings.Size; | ||||
// Create name <-> id maps for textures | // Create name <-> id maps for textures | ||||
this.nameToID = {}; | this.nameToID = {}; | ||||
this.IDToName = []; | this.IDToName = []; | ||||
// Texture 2D array | // Texture 2D array | ||||
this.texture = []; | this.textureIDs = []; | ||||
lyv: Just noticed the singluar and plural variable names, will fix with next update | |||||
lyvAuthorUnsubmitted Done Inline ActionsShould be renamed to textureIndex lyv: Should be renamed to `textureIndex` | |||||
this.texturePriority = []; | |||||
for (let x = 0; x < this.size; ++x) | for (let x = 0; x < this.size; ++x) | ||||
{ | { | ||||
this.texture[x] = new Uint16Array(this.size); | this.textureIDs[x] = new Uint16Array(this.size); | ||||
this.texturePriority = new Uint16Array(this.size); | |||||
Not Done Inline ActionsShould mirror the engine and make this signed. lyv: Should mirror the engine and make this signed. | |||||
for (let z = 0; z < this.size; ++z) | 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)); | typeof baseTerrain == "string" ? baseTerrain : pickRandom(baseTerrain)); | ||||
} | } | ||||
Not Done Inline ActionsI'd rather make an array and put textures (or IDs, not sure yet) in as required. The actual priority can be got at export. FeXoR: I'd rather make an array and put textures (or IDs, not sure yet) in as required. The actual… | |||||
Not Done Inline ActionsThat would lead to an even longer delay to actually finish mapgen. (Considering the things which happen after ExportMap();), IMO, it is better to spread negligible latency over the whole duration than to concentrate them and make it noticeable to the user. I have no better argument though, and would do whats best interms of design. lyv: That would lead to an even longer delay to actually finish mapgen. (Considering the things… | |||||
// Create 2D arrays for terrain objects and areas | // Create 2D arrays for terrain objects and areas | ||||
this.terrainEntities = []; | this.terrainEntities = []; | ||||
for (let i = 0; i < this.size; ++i) | for (let i = 0; i < this.size; ++i) | ||||
{ | { | ||||
this.terrainEntities[i] = []; | this.terrainEntities[i] = []; | ||||
for (let j = 0; j < this.size; ++j) | for (let j = 0; j < this.size; ++j) | ||||
this.terrainEntities[i][j] = undefined; | this.terrainEntities[i][j] = undefined; | ||||
▲ Show 20 Lines • Show All 186 Lines • ▼ Show 20 Lines | RandomMap.prototype.randomCoordinate = function(passableOnly) | ||||
// Rectangular coordinates | // Rectangular coordinates | ||||
return new Vector2D( | return new Vector2D( | ||||
randIntExclusive(border, this.size - border), | randIntExclusive(border, this.size - border), | ||||
randIntExclusive(border, this.size - border)); | randIntExclusive(border, this.size - border)); | ||||
}; | }; | ||||
/** | /** | ||||
* 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) | RandomMap.prototype.getTexture = function(position) | ||||
{ | { | ||||
if (!this.inMapBounds(position)) | if (!this.inMapBounds(position)) | ||||
throw new Error("getTexture: invalid tile position " + uneval(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]]; | ||||
}; | }; | ||||
Not Done Inline ActionsSome users of this expect a string returned elexis: Some users of this expect a string returned | |||||
/** | /** | ||||
* 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 || | if (position.x < 0 || | ||||
position.y < 0 || | position.y < 0 || | ||||
position.x >= this.texture.length || | position.x >= this.textureIDs.length || | ||||
position.y >= this.texture[position.x].length) | position.y >= this.textureIDs[position.x].length) | ||||
throw new Error("setTexture: invalid tile position " + uneval(position)); | 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. | |||||
Not Done Inline Actionsg_AdjacentCoordinates elexis: g_AdjacentCoordinates | |||||
let surroundingTiles = [ | |||||
this.texturePriority[x - 1][y - 1], | |||||
this.texturePriority[x][y - 1], | |||||
this.texturePriority[x + 1][y - 1], | |||||
this.texturePriority[x - 1][y + 1], | |||||
this.texturePriority[x][y + 1], | |||||
this.texturePriority[x + 1][y + 1], | |||||
this.texturePriority[x - 1][y], | |||||
this.texturePriority[x + 1][y] | |||||
]; | |||||
let priorityIndex = priority == "LOW" ? Math.min(...surroundingTiles.priority) - 1 : Math.max(...surroundingTiles.priority) + 1; | |||||
lyvAuthorUnsubmitted Done Inline ActionsThis is cannot be signed. lyv: This is cannot be signed. | |||||
this.textureIDs[x][y] = this.getTextureID(texture); | |||||
this.texturePriority[x][y] = priorityIndex; | |||||
}; | }; | ||||
RandomMap.prototype.getHeight = function(position) | RandomMap.prototype.getHeight = function(position) | ||||
{ | { | ||||
if (!this.validHeight(position)) | if (!this.validHeight(position)) | ||||
throw new Error("getHeight: invalid vertex position " + uneval(position)); | throw new Error("getHeight: invalid vertex position " + uneval(position)); | ||||
return this.height[position.x][position.y]; | return this.height[position.x][position.y]; | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | |||||
RandomMap.prototype.exportTerrainTextures = function() | RandomMap.prototype.exportTerrainTextures = function() | ||||
{ | { | ||||
let tileIndex = new Uint16Array(Math.square(this.size)); | let tileIndex = new Uint16Array(Math.square(this.size)); | ||||
let tilePriority = new Uint16Array(Math.square(this.size)); | let tilePriority = new Uint16Array(Math.square(this.size)); | ||||
for (let x = 0; x < this.size; ++x) | for (let x = 0; x < this.size; ++x) | ||||
for (let z = 0; z < this.size; ++z) | 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.textureIDs[x][z]; | ||||
tileIndex[z * this.size + x] = this.texture[x][z]; | tilePriority[z * this.size + x] = this.texturePriority[x][z] || this.textureIDs[x][z]; | ||||
Not Done Inline ActionsUnneeded fallback, also unexpected if one didn't read this line but the other priority ones. elexis: Unneeded fallback, also unexpected if one didn't read this line but the other priority ones. | |||||
tilePriority[z * this.size + x] = this.texture[x][z]; | |||||
Not Done Inline ActionsAlso, why pretend this doesn't exist. As of now, it's pretty much a useless duplicate of tileIndex. That is if you don't count it's purpose of painting textures in the order defined in the textureID array. lyv: Also, why pretend this doesn't exist. As of now, it's pretty much a useless duplicate of… | |||||
} | } | ||||
return { | return { | ||||
"index": tileIndex, | "index": tileIndex, | ||||
"priority": tilePriority | "priority": tilePriority | ||||
}; | }; | ||||
}; | }; | ||||
RandomMap.prototype.ExportMap = function() | RandomMap.prototype.ExportMap = function() | ||||
{ | { | ||||
if (g_Environment.Water.WaterBody.Height === undefined) | if (g_Environment.Water.WaterBody.Height === undefined) | ||||
g_Environment.Water.WaterBody.Height = SEA_LEVEL - 0.1; | g_Environment.Water.WaterBody.Height = SEA_LEVEL - 0.1; | ||||
this.logger.close(); | this.logger.close(); | ||||
Engine.ExportMap({ | Engine.ExportMap({ | ||||
"entities": this.exportEntityList(), | "entities": this.exportEntityList(), | ||||
"height": this.exportHeightData(), | "height": this.exportHeightData(), | ||||
"seaLevel": SEA_LEVEL, | "seaLevel": SEA_LEVEL, | ||||
"size": this.size, | "size": this.size, | ||||
"textureNames": this.IDToName, | "textureNames": this.IDToName, | ||||
"tileData": this.exportTerrainTextures(), | "tileData": this.exportTerrainTextures(), | ||||
lyvAuthorUnsubmitted Done Inline ActionsNot sure if its worth it, but not necessary to have two mapSize^2 arrays. Can do with one. 8bits for index, 8bits for priority. lyv: Not sure if its worth it, but not necessary to have two mapSize^2 arrays. Can do with one. | |||||
lyvAuthorUnsubmitted Done Inline ActionsExperimented with in P143 lyv: Experimented with in P143 | |||||
"Camera": g_Camera, | "Camera": g_Camera, | ||||
"Environment": g_Environment | "Environment": g_Environment | ||||
}); | }); | ||||
}; | }; |
Just noticed the singluar and plural variable names, will fix with next update