Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/maps/random/heightmap/heightmap.js
Show First 20 Lines • Show All 229 Lines • ▼ Show 20 Lines | |||||
* @param {Object} [avoidClass=undefined] - TileClass to be avoided | * @param {Object} [avoidClass=undefined] - TileClass to be avoided | ||||
* @param {integer} [minDistance=30] - How many tile widths the entities to place have to be away from each other, start locations and the map border | * @param {integer} [minDistance=30] - How many tile widths the entities to place have to be away from each other, start locations and the map border | ||||
* @param {array} [heightmap=g_Map.height] - The reliefmap the entities should be distributed on | * @param {array} [heightmap=g_Map.height] - The reliefmap the entities should be distributed on | ||||
* @param {integer} [maxTries=2 * g_Map.size] - How often random player distributions are rolled to be compared (256 to 1024) | * @param {integer} [maxTries=2 * g_Map.size] - How often random player distributions are rolled to be compared (256 to 1024) | ||||
* @param {boolean} [isCircular=g_MapSettings.CircularMap] - If the map is circular or rectangular | * @param {boolean} [isCircular=g_MapSettings.CircularMap] - If the map is circular or rectangular | ||||
*/ | */ | ||||
function getPointsByHeight(heightRange, avoidPoints = [], avoidClass = undefined, minDistance = 20, maxTries = 2 * g_Map.size, heightmap = g_Map.height, isCircular = g_MapSettings.CircularMap) | function getPointsByHeight(heightRange, avoidPoints = [], avoidClass = undefined, minDistance = 20, maxTries = 2 * g_Map.size, heightmap = g_Map.height, isCircular = g_MapSettings.CircularMap) | ||||
{ | { | ||||
let points = []; | const points = []; | ||||
let placements = clone(avoidPoints); | const placements = clone(avoidPoints); | ||||
let validVertices = []; | const validVertices = []; | ||||
let r = 0.5 * (heightmap.length - 1); // Map center x/y as well as radius | const r = 0.5 * (heightmap.length - 1); // Map center x/y as well as radius | ||||
let avoidMap; | |||||
if (avoidClass) | |||||
avoidMap = avoidClass.inclusionCount; | |||||
for (let x = minDistance; x < heightmap.length - minDistance; ++x) | for (let x = minDistance; x < heightmap.length - minDistance; ++x) | ||||
for (let y = minDistance; y < heightmap[x].length - minDistance; ++y) | for (let y = minDistance; y < heightmap[x].length - minDistance; ++y) | ||||
{ | { | ||||
if (avoidClass && | if (avoidClass && | ||||
(avoidMap[Math.max(x - 1, 0)][y] > 0 || | (avoidClass.has(Math.max(x - 1, 0), y) || | ||||
avoidMap[x][Math.max(y - 1, 0)] > 0 || | avoidClass.has(x, Math.max(y - 1, 0)) || | ||||
avoidMap[Math.min(x + 1, avoidMap.length - 1)][y] > 0 || | avoidClass.has(Math.min(x + 1, avoidClass.size - 1), y) || | ||||
avoidMap[x][Math.min(y + 1, avoidMap[0].length - 1)] > 0)) | avoidClass.has(x, Math.min(y + 1, avoidClass.size - 1)))) | ||||
continue; | continue; | ||||
if (heightmap[x][y] > heightRange.min && heightmap[x][y] < heightRange.max && // Has correct height | if (heightmap[x][y] > heightRange.min && heightmap[x][y] < heightRange.max && // Has correct height | ||||
(!isCircular || r - Math.euclidDistance2D(x, y, r, r) >= minDistance)) // Enough distance to the map border | (!isCircular || r - Math.euclidDistance2D(x, y, r, r) >= minDistance)) // Enough distance to the map border | ||||
validVertices.push({ "x": x, "y": y, "dist": minDistance }); | validVertices.push({ "x": x, "y": y, "dist": minDistance }); | ||||
} | } | ||||
for (let tries = 0; tries < maxTries; ++tries) | for (let tries = 0; tries < maxTries; ++tries) | ||||
{ | { | ||||
let point = pickRandom(validVertices); | const point = pickRandom(validVertices); | ||||
if (placements.every(p => Math.euclidDistance2D(p.x, p.y, point.x, point.y) > Math.max(minDistance, p.dist))) | if (placements.every(p => Math.euclidDistance2D(p.x, p.y, point.x, point.y) > Math.max(minDistance, p.dist))) | ||||
{ | { | ||||
points.push(point); | points.push(point); | ||||
placements.push(point); | placements.push(point); | ||||
} | } | ||||
} | } | ||||
return points; | return points; | ||||
▲ Show 20 Lines • Show All 142 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator