Fixes a bug in random map library placer.js function SimpleObject.place() that placed entities outside of the playable map area. Add a constant MAP_BORDER_WIDTH to remove magic numbers. Also removed some unneeded brackets in the changed files. Fixes 4012

This was SVN commit r18450.
This commit is contained in:
FeXoR
2016-06-28 11:37:56 +00:00
parent 0f1cef8766
commit d30ee0292b
3 changed files with 8 additions and 22 deletions
@@ -6,6 +6,7 @@ const CELL_SIZE = 4;
const HEIGHT_UNITS_PER_METRE = 92;
const MIN_MAP_SIZE = 128;
const MAX_MAP_SIZE = 512;
const MAP_BORDER_WIDTH = 3;
const FALLBACK_CIV = "athen";
/**
* Constants needed for heightmap_manipulation.js
@@ -283,7 +284,7 @@ function createSimpleTerrain(terrain)
function placeObject(x, z, type, player, angle)
{
if (g_Map.validT(x, z, 3))
if (g_Map.validT(x, z, MAP_BORDER_WIDTH))
g_Map.addObject(new Entity(type, player, x, z, angle));
}
@@ -453,9 +453,7 @@ SimpleObject.prototype.place = function(cx, cz, player, avoidSelf, constraint, m
{
failCount++;
if (failCount > maxFailCount)
{
return undefined;
}
}
}
}
@@ -555,9 +553,7 @@ RandomObject.prototype.place = function(cx, cz, player, avoidSelf, constraint, m
{
failCount++;
if (failCount > maxFailCount)
{
return undefined;
}
}
}
}
@@ -602,9 +598,7 @@ SimpleGroup.prototype.place = function(player, constraint)
else
{
for (var j = 0; j < objs.length; ++j)
{
resultObjs.push(objs[j]);
}
}
}
@@ -612,13 +606,12 @@ SimpleGroup.prototype.place = function(player, constraint)
length = resultObjs.length;
for (var i=0; i < length; i++)
{
if (g_Map.validT(round(resultObjs[i].position.x/CELL_SIZE), round(resultObjs[i].position.z/CELL_SIZE), 3))
if (g_Map.validT(resultObjs[i].position.x / CELL_SIZE, resultObjs[i].position.z / CELL_SIZE, MAP_BORDER_WIDTH))
g_Map.addObject(resultObjs[i]);
// Convert position to integer number of tiles
if (this.tileClass !== undefined)
{ // Convert position to integer number of tiles
this.tileClass.add(Math.floor(resultObjs[i].position.x/CELL_SIZE), Math.floor(resultObjs[i].position.z/CELL_SIZE));
}
}
return true;
@@ -653,16 +646,15 @@ RandomGroup.prototype.place = function(player, constraint)
var placer = this.elements[randInt(this.elements.length)];
var objs = placer.place(this.x, this.z, player, this.avoidSelf, constraint);
// Failure
if (objs === undefined)
{ // Failure
{
return false;
}
else
{
for (var j = 0; j < objs.length; ++j)
{
resultObjs.push(objs[j]);
}
}
// Add placed objects to map
@@ -671,10 +663,9 @@ RandomGroup.prototype.place = function(player, constraint)
{
g_Map.addObject(resultObjs[i]);
// Convert position to integer number of tiles
if (this.tileClass !== undefined)
{ // Convert position to integer number of tiles
this.tileClass.add(Math.floor(resultObjs[i].position.x/CELL_SIZE), Math.floor(resultObjs[i].position.z/CELL_SIZE));
}
}
return true;
@@ -31,9 +31,7 @@ Terrain.prototype.placeNew = function() {};
function SimpleTerrain(texture, treeType)
{
if (texture === undefined)
{
throw("SimpleTerrain: texture not defined");
}
this.texture = texture;
this.treeType = treeType;
@@ -43,10 +41,8 @@ SimpleTerrain.prototype = new Terrain();
SimpleTerrain.prototype.constructor = SimpleTerrain;
SimpleTerrain.prototype.placeNew = function(x, z)
{
if (this.treeType !== undefined && g_Map.validT(round(x), round(z), 3))
{
if (this.treeType !== undefined && g_Map.validT(round(x), round(z), MAP_BORDER_WIDTH))
g_Map.terrainObjects[x][z] = new Entity(this.treeType, 0, x+0.5, z+0.5, randFloat()*TWO_PI);
}
g_Map.texture[x][z] = g_Map.getTextureID(this.texture);
};
@@ -63,9 +59,7 @@ SimpleTerrain.prototype.placeNew = function(x, z)
function RandomTerrain(terrains)
{
if (!(terrains instanceof Array) || !terrains.length)
{
throw("RandomTerrain: Invalid terrains array");
}
this.terrains = terrains;
}