1
0
forked from mirrors/0ad

Use vectors for getHeight and remove the global proxy, refs #4992.

This was SVN commit r20983.
This commit is contained in:
elexis
2018-01-23 17:54:30 +00:00
parent 5025bcb1c5
commit c02db59c96
12 changed files with 55 additions and 54 deletions
@@ -84,7 +84,7 @@ for (var ix = 0; ix < mapSize; ix++)
for (var iz = 0; iz < mapSize; iz++)
{
let position = new Vector2D(ix, iz);
var h = getHeight(ix,iz);
let h = g_Map.getHeight(position);
if (h > heightRavineHill)
{
addToClass(ix,iz,clHill);
@@ -260,7 +260,7 @@ for (var ix = 0; ix < mapSize; ix++)
for (var iz = 0; iz < mapSize; iz++)
{
let position = new Vector2D(ix, iz);
var h = getHeight(ix,iz);
let h = g_Map.getHeight(position);
if(h > 15 && h < 45 && playerClass.countMembersInRadius(ix, iz, 1) == 0)
explorablePoints.push(position);
@@ -282,7 +282,7 @@ for (var ix = 0; ix < mapSize; ix++)
{
let position = new Vector2D(ix, iz);
var z = iz / (mapSize + 1.0);
var h = getHeight(ix,iz);
var h = g_Map.getHeight(position);
var pn = playerNearness(x,z);
var n = (noise0.get(x,z) - 0.5) * 10;
g_Map.setHeight(position, h + (n * pn));
@@ -126,7 +126,8 @@ while (!goodStartPositionsFound)
for (var x = distToBorder + minTerrainDistToBorder; x < mapSize - distToBorder - minTerrainDistToBorder; x++)
for (var y = distToBorder + minTerrainDistToBorder; y < mapSize - distToBorder - minTerrainDistToBorder; y++)
{
var actualHeight = getHeight(x, y);
let position = new Vector2D(x, y);
let actualHeight = g_Map.getHeight(position);
if (actualHeight > lowerHeightLimit && actualHeight < upperHeightLimit)
{
// Check for points within a valid area by height (rectangular since faster)
@@ -134,7 +135,7 @@ while (!goodStartPositionsFound)
for (var offX = - neededDistance; offX <= neededDistance; offX++)
for (var offY = - neededDistance; offY <= neededDistance; offY++)
{
var testHeight = getHeight(x + offX, y + offY);
var testHeight = g_Map.getHeight(Vector2D.add(position, new Vector2D(offX, offY)));
if (testHeight <= lowerHeightLimit || testHeight >= upperHeightLimit)
{
isPossible = false;
@@ -171,7 +172,7 @@ while (!goodStartPositionsFound)
{
for (var dy = - maxDistToResources; dy < maxDistToResources; dy++)
{
var testHeight = getHeight(possibleStartPositions[i][0] + dx, possibleStartPositions[i][1] + dy);
var testHeight = g_Map.getHeight(new Vector2D(possibleStartPositions[i][0] + dx, possibleStartPositions[i][1] + dy));
if (testHeight < lowerHeightLimit)
numLowTiles++;
@@ -265,7 +266,7 @@ for(var x = minTerrainDistToBorder; x < mapSize - minTerrainDistToBorder; x++)
var textureMinHeight = heightRange.min;
for (var i = 0; i < textueByHeight.length; i++)
{
if (getHeight(x, y) >= textureMinHeight && getHeight(x, y) <= textueByHeight[i].upperHeightLimit)
if (g_Map.getHeight(position) >= textureMinHeight && g_Map.getHeight(position) <= textueByHeight[i].upperHeightLimit)
{
createTerrain(textueByHeight[i].terrain).place(position);
@@ -290,7 +290,7 @@ function getCosricaSardiniaTerrain(mapX, mapZ)
if (isSettlement)
return undefined;
let height = getHeight(mapX, mapZ);
let height = g_Map.getHeight(position);
let slope = g_Map.getSlope(position);
if (height >= 0.5 && height < 1.5 && isShore)
@@ -193,7 +193,7 @@ for (var x = 0; x < mapSize; x++)
// General hight map
var hVarMiddleHill = mapSize / 64 * (1 + Math.cos(3/2 * Math.PI * radius / mapRadius));
var hVarHills = 5 * (1 + Math.sin(x / 10) * Math.sin(z / 10));
g_Map.setHeight(position, getHeight(x, z) + hVarMiddleHill + hVarHills + 1);
g_Map.setHeight(position, g_Map.getHeight(position) + hVarMiddleHill + hVarHills + 1);
}
Engine.SetProgress(95);
@@ -143,7 +143,7 @@ for (var ix = 0; ix <= mapSize; ix++)
let c = startAngle ? z : x;
let distToWater = stayClasses(clWater, 1).allows(position) ? 0 : (0.5 - WATER_WIDTH - Math.abs(c - 0.5));
let h = distToWater ? heightHill * (1 - Math.abs(c - 0.5) / (0.5 - WATER_WIDTH)) : getHeight(ix, iz);
let h = distToWater ? heightHill * (1 - Math.abs(c - 0.5) / (0.5 - WATER_WIDTH)) : g_Map.getHeight(position);
// add some base noise
var baseNoise = 16*noise0.get(x,z) + 8*noise1.get(x,z) + 4*noise2.get(x,z) - (16+8+4)/2;
@@ -200,10 +200,10 @@ for (var ix = 0; ix < mapSize; ix++)
var pn = playerNearness(x, z);
// get heights of surrounding vertices
var h00 = getHeight(ix, iz);
var h01 = getHeight(ix, iz+1);
var h10 = getHeight(ix+1, iz);
var h11 = getHeight(ix+1, iz+1);
var h00 = g_Map.getHeight(Vector2D.add(position, new Vector2D(0, 0));
var h01 = g_Map.getHeight(Vector2D.add(position, new Vector2D(0, 1));
var h10 = g_Map.getHeight(Vector2D.add(position, new Vector2D(1, 0));
var h11 = g_Map.getHeight(Vector2D.add(position, new Vector2D(1, 1));
// find min and max height
var maxH = Math.max(h00, h01, h10, h11);
@@ -218,7 +218,7 @@ for (var ix = 0; ix < mapSize; ix++)
var maxNz = Math.min(iz + 2, mapSize);
for (let nx = Math.max(ix - 1, 0); nx <= maxNx; ++nx)
for (let nz = Math.max(iz - 1, 0); nz <= maxNz; ++nz)
minAdjHeight = Math.min(minAdjHeight, getHeight(nx, nz));
minAdjHeight = Math.min(minAdjHeight, getHeight(new Vector2D(nx, nz)));
}
// choose a terrain based on elevation
@@ -266,7 +266,7 @@ for (var ix = 0; ix < mapSize; ix++)
addToClass(ix, iz, clCliff);
// forests
if (getHeight(ix, iz) < 11 && diffH < 2 && minH > 1)
if (g_Map.getHeight(position) < 11 && diffH < 2 && minH > 1)
{
var forestNoise = (noise6.get(x,z) + 0.5*noise7.get(x,z)) / 1.5 * pn - 0.59;
+14 -6
View File
@@ -33,7 +33,11 @@ const pOasisForestLight = [tForestFloor + TERRAIN_SEPARATOR + ePalmShort, tFores
,tForestFloor,tForestFloor,tForestFloor,tForestFloor];
const heightSeaGround = -3;
const heightFloraMin = -2.5
const heightFloraReedsMax = -1.9;
const heightFloraMax = -1;
const heightLand = 1;
const heightSand = 3.4;
const heightOasisPath = 4;
const heightOffsetBump = 4;
const heightOffsetDune = 18;
@@ -280,34 +284,38 @@ log("Creating sand blows and beautifications");
for (var sandx = 0; sandx < mapSize; sandx += 4)
for (var sandz = 0; sandz < mapSize; sandz += 4)
{
if (getHeight(sandx,sandz) > 3.4)
let position = new Vector2D(sandx, sandz);
let height = g_Map.getHeight(position);
if (height > heightSand)
{
if (randBool((getHeight(sandx,sandz) - 3.4) / 1.4))
if (randBool((height - heightSand) / 1.4))
{
group = new SimpleGroup( [new SimpleObject(aSand, 0,1, 0,2)], true, undefined, sandx,sandz );
createObjectGroup(group, 0);
}
}
else if (getHeight(sandx, sandz) > -2.5 && getHeight(sandx,sandz) < -1)
else if (height > heightFloraMin && height < heightFloraMax)
{
if (randBool(0.4))
{
group = new SimpleGroup( [new SimpleObject(aWaterFlower, 1,4, 1,2)], true, undefined, sandx,sandz );
createObjectGroup(group, 0);
}
else if (randBool(0.7) && getHeight(sandx,sandz) < -1.9)
else if (randBool(0.7) && height < heightFloraReedsMax)
{
group = new SimpleGroup( [new SimpleObject(aReedsA, 5,12, 0,2),new SimpleObject(aReedsB, 5,12, 0,2)], true, undefined, sandx,sandz );
createObjectGroup(group, 0);
}
if (getTileClass(clPassage).countInRadius(sandx,sandz,2,true) > 0) {
if (getTileClass(clPassage).countMembersInRadius(sandx, sandz, 2) > 0)
{
if (randBool(0.4))
{
group = new SimpleGroup( [new SimpleObject(aWaterFlower, 1,4, 1,2)], true, undefined, sandx,sandz );
createObjectGroup(group, 0);
}
else if (randBool(0.7) && getHeight(sandx,sandz) < -1.9)
else if (randBool(0.7) && height < heightFloraReedsMax)
{
group = new SimpleGroup( [new SimpleObject(aReedsA, 5,12, 0,2),new SimpleObject(aReedsB, 5,12, 0,2)], true, undefined, sandx,sandz );
createObjectGroup(group, 0);
@@ -246,7 +246,7 @@ for (let ix = 1; ix < mapSize - 1; ++ix)
let position = new Vector2D(ix, iz);
if (g_Map.validH(ix, iz) && getTileClass(clPyrenneans).countMembersInRadius(ix, iz, 1))
{
let height = getHeight(ix, iz);
let height = g_Map.getHeight(position);
let index = 1 / (1 + Math.max(0, height / 7));
g_Map.setHeight(position, height * (1 - index) + g_Map.getAverageHeight(position) * index);
}
@@ -283,8 +283,8 @@ for (let ix = 1; ix < mapSize - 1; ++ix)
if (g_Map.inMapBounds(position) && getTileClass(clPyrenneans).countMembersInRadius(ix, iz, 1))
{
let heightNeighbor = g_Map.getAverageHeight(position);
let index = 1 / (1 + Math.max(0, (getHeight(ix,iz) - 10) / 7));
g_Map.setHeight(position, getHeight(ix, iz) * (1 - index) + heightNeighbor * index);
let index = 1 / (1 + Math.max(0, (g_Map.getHeight(position) - 10) / 7));
g_Map.setHeight(position, g_Map.getHeight(position) * (1 - index) + heightNeighbor * index);
}
}
@@ -313,11 +313,11 @@ for (let ix = 1; ix < mapSize - 1; ++ix)
let smoothPos = Vector2D.add(position, new Vector2D(xx, yy));
if (g_Map.inMapBounds(smoothPos) && (xx != 0 || yy != 0))
{
averageHeight += getHeight(smoothPos.x, smoothPos.y) / (Math.abs(xx) + Math.abs(yy));
averageHeight += g_Map.getHeight(smoothPos) / (Math.abs(xx) + Math.abs(yy));
todivide += 1 / (Math.abs(xx) + Math.abs(yy));
}
}
g_Map.setHeight(position, (averageHeight + 2 * getHeight(ix, iz)) / (todivide + 2));
g_Map.setHeight(position, (averageHeight + 2 * g_Map.getHeight(position)) / (todivide + 2));
}
Engine.SetProgress(55);
@@ -357,7 +357,7 @@ for (let x = 0; x < mapSize; ++x)
for (let z = 0; z < mapSize; ++z)
{
let position = new Vector2D(x, z);
let height = getHeight(x, z);
let height = g_Map.getHeight(position);
let heightDiff = g_Map.getSlope(position);
if (getTileClass(clPyrenneans).countMembersInRadius(x, z, 2))
@@ -139,7 +139,7 @@ for (let i = 0; i < numRivers; ++i)
let newHeight = isShallow ? heightShallows : Math.max(height, heightSeaGround);
if (getHeight(position.x, position.y) < newHeight)
if (g_Map.getHeight(position) < newHeight)
return;
g_Map.setHeight(position, newHeight);
@@ -190,9 +190,9 @@ function createMountain(maxHeight, minRadius, maxRadius, numCircles, constraints
if (distance > radius)
continue;
if (getHeight(ix, iz) < newHeight)
if (g_Map.getHeight(position) < newHeight)
g_Map.setHeight(position, newHeight);
else if (getHeight(ix, iz) >= newHeight && getHeight(ix, iz) < newHeight + 4)
else if (g_Map.getHeight(position) >= newHeight && g_Map.getHeight(position) < newHeight + 4)
g_Map.setHeight(position, newHeight + 4);
if (terrain !== undefined)
@@ -536,8 +536,8 @@ function createPassage(args)
{
let bound = x => Math.max(0, Math.min(Math.round(x), getMapSize()));
let startHeight = args.startHeight !== undefined ? args.startHeight : getHeight(bound(args.start.x), bound(args.start.y));
let endHeight = args.endHeight !== undefined ? args.endHeight : getHeight(bound(args.end.x), bound(args.end.y));
let startHeight = args.startHeight !== undefined ? args.startHeight : g_Map.getHeight(new Vector2D(bound(args.start.x), bound(args.start.y)));
let endHeight = args.endHeight !== undefined ? args.endHeight : g_Map.getHeight(new Vector2D(bound(args.end.x), bound(args.end.y)));
let passageVec = Vector2D.sub(args.end, args.start);
let widthDirection = passageVec.perpendicular().normalize();
@@ -554,7 +554,7 @@ function createPassage(args)
let location = Vector2D.add(locationLength, Vector2D.mult(widthDirection, stepWidth)).round();
if (!g_Map.inMapBounds(location) ||
args.maxHeight !== undefined && getHeight(location.x, location.y) > args.maxHeight)
args.maxHeight !== undefined && g_Map.getHeight(location) > args.maxHeight)
continue;
let smoothDistance = args.smoothWidth + Math.abs(stepWidth) - halfPassageWidth;
@@ -562,7 +562,7 @@ function createPassage(args)
g_Map.setHeight(
location,
smoothDistance > 0 ?
(getHeight(location.x, location.y) * smoothDistance + passageHeight / smoothDistance) / (smoothDistance + 1 / smoothDistance) :
(g_Map.getHeight(location) * smoothDistance + passageHeight / smoothDistance) / (smoothDistance + 1 / smoothDistance) :
passageHeight);
if (args.tileClass !== undefined)
@@ -591,8 +591,8 @@ function findLocationInDirectionBasedOnHeight(startPoint, endPoint, minHeight, m
let ipos = pos.clone().round();
if (g_Map.validH(ipos.x, ipos.y) &&
getHeight(ipos.x, ipos.y) >= minHeight &&
getHeight(ipos.x, ipos.y) <= maxHeight)
g_Map.getHeight(ipos) >= minHeight &&
g_Map.getHeight(ipos) <= maxHeight)
return pos.add(stepVec.mult(offset));
}
@@ -328,15 +328,6 @@ function getPlayerTeam(playerID)
return g_MapSettings.PlayerData[playerID].Team;
}
function getHeight(x, z)
{
return g_Map.getHeight(x, z);
}
/**
* Utility functions for classes
*/
/**
* Add point to given class by id
*/
@@ -160,12 +160,12 @@ RandomMap.prototype.setTexture = function(x, z, texture)
this.texture[x][z] = this.getTextureID(texture);
};
RandomMap.prototype.getHeight = function(x, z)
RandomMap.prototype.getHeight = function(position)
{
if (!this.validH(x, z))
throw new Error("getHeight: invalid vertex position (" + x + ", " + z + ")");
if (!this.validH(position.x, position.y))
throw new Error("getHeight: invalid vertex position " + uneval(position));
return this.height[x][z];
return this.height[position.x][position.y];
};
RandomMap.prototype.setHeight = function(position, height)
@@ -289,7 +289,7 @@ RandomMap.prototype.getAverageHeight = function(position)
if (!adjacentPositions.length)
return 0;
return adjacentPositions.reduce((totalHeight, pos) => totalHeight + this.getHeight(pos.x, pos.y), 0) / adjacentPositions.length;
return adjacentPositions.reduce((totalHeight, pos) => totalHeight + this.getHeight(pos), 0) / adjacentPositions.length;
}
/**
@@ -301,7 +301,8 @@ RandomMap.prototype.getSlope = function(position)
if (!adjacentPositions.length)
return 0;
return adjacentPositions.reduce((totalSlope, adjacentPos) => totalSlope + Math.abs(this.getHeight(adjacentPos.x, adjacentPos.y) - this.getHeight(position.x, position.y)), 0) / adjacentPositions.length;
return adjacentPositions.reduce((totalSlope, adjacentPos) =>
totalSlope + Math.abs(this.getHeight(adjacentPos) - this.getHeight(position)), 0) / adjacentPositions.length;
}
/**
@@ -109,7 +109,7 @@ function addBluffs(constraint, size, deviation, fill, baseHeight)
new Vector2D(baseLine.x2, baseLine.z2),
position));
var curHeight = g_Map.getHeight(pt.x, pt.z);
var curHeight = g_Map.getHeight(position);
var newHeight = curHeight - curHeight * (dist / slopeLength) - 2;
newHeight = Math.max(newHeight, endLine.height);
@@ -1014,7 +1014,7 @@ function createBoundingBox(points, corners)
bb[w] = [];
for (var l = 0; l < length; ++l)
{
var curHeight = g_Map.getHeight(w + corners.minX, l + corners.minZ);
var curHeight = g_Map.getHeight(new Vector2D(w + corners.minX, l + corners.minZ));
bb[w][l] = {
"height": curHeight,
"isFeature": false