Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/maps/random/rmgen-common/wall_builder.js
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | |||||
function createDefaultFortressTypes() | function createDefaultFortressTypes() | ||||
{ | { | ||||
let defaultFortresses = {}; | let defaultFortresses = {}; | ||||
/** | /** | ||||
* Define some basic default fortress types. | * Define some basic default fortress types. | ||||
*/ | */ | ||||
let addFortress = (type, walls) => defaultFortresses[type] = { "wall": walls.concat(walls, walls, walls) }; | let addFortress = (type, walls) => defaultFortresses[type] = { "wall": walls.concat(walls, walls, walls) }; | ||||
Lint: ESLintBear (no-return-assign): `Arrow function should not return assignment.` | |||||
addFortress("tiny", ["gate", "tower", "short", "cornerIn", "short", "tower"]); | addFortress("tiny", ["gate", "tower", "short", "cornerIn", "short", "tower"]); | ||||
addFortress("small", ["gate", "tower", "medium", "cornerIn", "medium", "tower"]); | addFortress("small", ["gate", "tower", "medium", "cornerIn", "medium", "tower"]); | ||||
addFortress("medium", ["gate", "tower", "long", "cornerIn", "long", "tower"]); | addFortress("medium", ["gate", "tower", "long", "cornerIn", "long", "tower"]); | ||||
addFortress("normal", ["gate", "tower", "medium", "cornerIn", "medium", "cornerOut", "medium", "cornerIn", "medium", "tower"]); | addFortress("normal", ["gate", "tower", "medium", "cornerIn", "medium", "cornerOut", "medium", "cornerIn", "medium", "tower"]); | ||||
addFortress("large", ["gate", "tower", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "tower"]); | addFortress("large", ["gate", "tower", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "tower"]); | ||||
addFortress("veryLarge", ["gate", "tower", "medium", "cornerIn", "medium", "cornerOut", "long", "cornerIn", "long", "cornerOut", "medium", "cornerIn", "medium", "tower"]); | addFortress("veryLarge", ["gate", "tower", "medium", "cornerIn", "medium", "cornerOut", "long", "cornerIn", "long", "cornerOut", "medium", "cornerIn", "medium", "tower"]); | ||||
addFortress("giant", ["gate", "tower", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "tower"]); | addFortress("giant", ["gate", "tower", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "cornerOut", "long", "cornerIn", "long", "tower"]); | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | default: | ||||
else | else | ||||
{ | { | ||||
if (!g_CivData[civ]) | if (!g_CivData[civ]) | ||||
civ = Object.keys(g_CivData)[0]; | civ = Object.keys(g_CivData)[0]; | ||||
let templateName = "structures/" + civ + "_" + element; | let templateName = "structures/" + civ + "_" + element; | ||||
if (Engine.TemplateExists(templateName)) | if (Engine.TemplateExists(templateName)) | ||||
{ | { | ||||
ret.indent = ret.length * (element == "outpost" || element.endsWith("_tower") ? -3 : 3.5); | ret.indent = ret.length * (element == "lookout" || element.endsWith("_tower") ? -3 : 3.5); | ||||
ret.templateName = templateName; | ret.templateName = templateName; | ||||
ret.length = 0; | ret.length = 0; | ||||
} | } | ||||
else | else | ||||
warn("Unrecognised wall element: '" + element + "' (" + style + "). Defaulting to " + (wallset.tower ? "'tower'." : "a blank element.")); | warn("Unrecognised wall element: '" + element + "' (" + style + "). Defaulting to " + (wallset.tower ? "'tower'." : "a blank element.")); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | |||||
* @param {array} [wall] - Array of wall element types. Example: ["start", "long", "tower", "long", "end"] | * @param {array} [wall] - Array of wall element types. Example: ["start", "long", "tower", "long", "end"] | ||||
* @param {string} [style] - Wall style string. | * @param {string} [style] - Wall style string. | ||||
* @param {number} [playerId] - Identifier of the player for whom the wall will be placed. | * @param {number} [playerId] - Identifier of the player for whom the wall will be placed. | ||||
* @param {number} [orientation] - Angle at which the first wall element is placed. | * @param {number} [orientation] - Angle at which the first wall element is placed. | ||||
* 0 means "outside" or "front" of the wall is right (positive X) like placeObject | * 0 means "outside" or "front" of the wall is right (positive X) like placeObject | ||||
* It will then be build towards top/positive Y (if no bending wall elements like corners are used) | * It will then be build towards top/positive Y (if no bending wall elements like corners are used) | ||||
* Raising orientation means the wall is rotated counter-clockwise like placeObject | * Raising orientation means the wall is rotated counter-clockwise like placeObject | ||||
*/ | */ | ||||
function placeWall(position, wall = [], style, playerId = 0, orientation = 0, constraints = undefined) | function placeWall(position, wall = [], style, playerId = 0, orientation = 0, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
let entities = []; | let entities = []; | ||||
let constraint = new StaticConstraint(constraints); | let constraint = new StaticConstraint(constraints); | ||||
for (let align of getWallAlignment(position, wall, style, orientation)) | for (let align of getWallAlignment(position, wall, style, orientation)) | ||||
if (align.templateName && g_Map.inMapBounds(align.position) && constraint.allows(align.position.clone().floor())) | if (align.templateName && g_Map.inMapBounds(align.position) && constraint.allows(align.position.clone().floor())) | ||||
Show All 37 Lines | |||||
/** | /** | ||||
* Places a predefined fortress centered around the provided point. | * Places a predefined fortress centered around the provided point. | ||||
* | * | ||||
* @see Fortress | * @see Fortress | ||||
* | * | ||||
* @param {string} [type] - Predefined fortress type, as used as a key in g_FortressTypes. | * @param {string} [type] - Predefined fortress type, as used as a key in g_FortressTypes. | ||||
*/ | */ | ||||
function placeFortress(centerPosition, type = "medium", style, playerId = 0, orientation = 0, constraints = undefined) | function placeFortress(centerPosition, type = "medium", style, playerId = 0, orientation = 0, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
return placeCustomFortress(centerPosition, g_FortressTypes[type], style, playerId, orientation, constraints); | return placeCustomFortress(centerPosition, g_FortressTypes[type], style, playerId, orientation, constraints); | ||||
} | } | ||||
/** | /** | ||||
* Places a straight wall from a given point to another, using the provided | * Places a straight wall from a given point to another, using the provided | ||||
* wall parts repeatedly. | * wall parts repeatedly. | ||||
* | * | ||||
* Note: Any "bending" wall pieces passed will be complained about. | * Note: Any "bending" wall pieces passed will be complained about. | ||||
* | * | ||||
* @param {Vector2D} startPosition - Approximate start point of the wall. | * @param {Vector2D} startPosition - Approximate start point of the wall. | ||||
* @param {Vector2D} targetPosition - Approximate end point of the wall. | * @param {Vector2D} targetPosition - Approximate end point of the wall. | ||||
* @param {array} [wallPart=["tower", "long"]] | * @param {array} [wallPart=["tower", "long"]] | ||||
* @param {number} [playerId] | * @param {number} [playerId] | ||||
* @param {boolean} [endWithFirst] - If true, the first wall element will also be the last. | * @param {boolean} [endWithFirst] - If true, the first wall element will also be the last. | ||||
*/ | */ | ||||
function placeLinearWall(startPosition, targetPosition, wallPart = undefined, style, playerId = 0, endWithFirst = true, constraints = undefined) | function placeLinearWall(startPosition, targetPosition, wallPart = undefined, style, playerId = 0, endWithFirst = true, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
wallPart = wallPart || ["tower", "long"]; | wallPart = wallPart || ["tower", "long"]; | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
// Check arguments | // Check arguments | ||||
for (let element of wallPart) | for (let element of wallPart) | ||||
if (getWallElement(element, style).bend != 0) | if (getWallElement(element, style).bend != 0) | ||||
warn("placeLinearWall : Bending is not supported by this function, but the following bending wall element was used: " + element); | warn("placeLinearWall : Bending is not supported by this function, but the following bending wall element was used: " + element); | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
* @param {array} [wallPart] | * @param {array} [wallPart] | ||||
* @param {string} [style] | * @param {string} [style] | ||||
* @param {number} [playerId] | * @param {number} [playerId] | ||||
* @param {number} [orientation] - Angle at which the first wall element is placed. | * @param {number} [orientation] - Angle at which the first wall element is placed. | ||||
* @param {number} [maxAngle] - How far the wall should circumscribe the center. Default is Pi * 2 (for a full circle). | * @param {number} [maxAngle] - How far the wall should circumscribe the center. Default is Pi * 2 (for a full circle). | ||||
* @param {boolean} [endWithFirst] - If true, the first wall element will also be the last. For full circles, the default is false. For arcs, true. | * @param {boolean} [endWithFirst] - If true, the first wall element will also be the last. For full circles, the default is false. For arcs, true. | ||||
* @param {number} [maxBendOff] Optional. How irregular the circle should be. 0 means regular circle, PI/2 means very irregular. Default is 0 (regular circle) | * @param {number} [maxBendOff] Optional. How irregular the circle should be. 0 means regular circle, PI/2 means very irregular. Default is 0 (regular circle) | ||||
*/ | */ | ||||
function placeCircularWall(center, radius, wallPart, style, playerId = 0, orientation = 0, maxAngle = 2 * Math.PI, endWithFirst, maxBendOff = 0, constraints = undefined) | function placeCircularWall(center, radius, wallPart, style, playerId = 0, orientation = 0, maxAngle = 2 * Math.PI, endWithFirst, maxBendOff = 0, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
wallPart = wallPart || ["tower", "long"]; | wallPart = wallPart || ["tower", "long"]; | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
if (endWithFirst === undefined) | if (endWithFirst === undefined) | ||||
endWithFirst = maxAngle < Math.PI * 2 - 0.001; // Can this be done better? | endWithFirst = maxAngle < Math.PI * 2 - 0.001; // Can this be done better? | ||||
// Check arguments | // Check arguments | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | |||||
* @param {array} [wallPart] | * @param {array} [wallPart] | ||||
* @param {string} [cornerWallElement] - Wall element to be placed at the polygon's corners. | * @param {string} [cornerWallElement] - Wall element to be placed at the polygon's corners. | ||||
* @param {string} [style] | * @param {string} [style] | ||||
* @param {number} [playerId] | * @param {number} [playerId] | ||||
* @param {number} [orientation] - Direction the first wall piece or opening in the wall faces. | * @param {number} [orientation] - Direction the first wall piece or opening in the wall faces. | ||||
* @param {number} [numCorners] - How many corners the polygon will have. | * @param {number} [numCorners] - How many corners the polygon will have. | ||||
* @param {boolean} [skipFirstWall] - If the first linear wall part will be left opened as entrance. | * @param {boolean} [skipFirstWall] - If the first linear wall part will be left opened as entrance. | ||||
*/ | */ | ||||
function placePolygonalWall(centerPosition, radius, wallPart, cornerWallElement = "tower", style, playerId = 0, orientation = 0, numCorners = 8, skipFirstWall = true, constraints = undefined) | function placePolygonalWall(centerPosition, radius, wallPart, cornerWallElement = "tower", style, playerId = 0, orientation = 0, numCorners = 8, skipFirstWall = true, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
wallPart = wallPart || ["long", "tower"]; | wallPart = wallPart || ["long", "tower"]; | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
let entities = []; | let entities = []; | ||||
let constraint = new StaticConstraint(constraints); | let constraint = new StaticConstraint(constraints); | ||||
let angleAdd = Math.PI * 2 / numCorners; | let angleAdd = Math.PI * 2 / numCorners; | ||||
let angleStart = orientation - angleAdd / 2; | let angleStart = orientation - angleAdd / 2; | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
* @param {string} [style] | * @param {string} [style] | ||||
* @param {number} [playerId] | * @param {number} [playerId] | ||||
* @param {number} [orientation] - Direction the first wallpiece or opening in the wall faces. | * @param {number} [orientation] - Direction the first wallpiece or opening in the wall faces. | ||||
* @param {number} [numCorners] - How many corners the polygon will have. | * @param {number} [numCorners] - How many corners the polygon will have. | ||||
* @param {number} [irregularity] - How irregular the polygon will be. 0 = regular, 1 = VERY irregular. | * @param {number} [irregularity] - How irregular the polygon will be. 0 = regular, 1 = VERY irregular. | ||||
* @param {boolean} [skipFirstWall] - If true, the first linear wall part will be left open as an entrance. | * @param {boolean} [skipFirstWall] - If true, the first linear wall part will be left open as an entrance. | ||||
* @param {array} [wallPartsAssortment] - An array of wall part arrays to choose from for each linear wall connecting the corners. | * @param {array} [wallPartsAssortment] - An array of wall part arrays to choose from for each linear wall connecting the corners. | ||||
*/ | */ | ||||
function placeIrregularPolygonalWall(centerPosition, radius, cornerWallElement = "tower", style, playerId = 0, orientation = 0, numCorners, irregularity = 0.5, skipFirstWall = false, wallPartsAssortment = undefined, constraints = undefined) | function placeIrregularPolygonalWall(centerPosition, radius, cornerWallElement = "tower", style, playerId = 0, orientation = 0, numCorners, irregularity = 0.5, skipFirstWall = false, wallPartsAssortment = undefined, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
numCorners = numCorners || randIntInclusive(5, 7); | numCorners = numCorners || randIntInclusive(5, 7); | ||||
// Generating a generic wall part assortment with each wall part including 1 gate lengthened by walls and towers | // Generating a generic wall part assortment with each wall part including 1 gate lengthened by walls and towers | ||||
// NOTE: It might be a good idea to write an own function for that... | // NOTE: It might be a good idea to write an own function for that... | ||||
let defaultWallPartsAssortment = [["short"], ["medium"], ["long"], ["gate", "tower", "short"]]; | let defaultWallPartsAssortment = [["short"], ["medium"], ["long"], ["gate", "tower", "short"]]; | ||||
let centeredWallPart = ["gate"]; | let centeredWallPart = ["gate"]; | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | |||||
* @param {Vector2D} center - The approximate center coordinates of the fortress | * @param {Vector2D} center - The approximate center coordinates of the fortress | ||||
* @param {number} [radius] - The approximate radius of the wall to be placed. | * @param {number} [radius] - The approximate radius of the wall to be placed. | ||||
* @param {number} [playerId] | * @param {number} [playerId] | ||||
* @param {string} [style] | * @param {string} [style] | ||||
* @param {number} [irregularity] - 0 = circle, 1 = very spiky | * @param {number} [irregularity] - 0 = circle, 1 = very spiky | ||||
* @param {number} [gateOccurence] - Integer number, every n-th walls will be a gate instead. | * @param {number} [gateOccurence] - Integer number, every n-th walls will be a gate instead. | ||||
* @param {number} [maxTries] - How often the function tries to find a better fitting shape. | * @param {number} [maxTries] - How often the function tries to find a better fitting shape. | ||||
*/ | */ | ||||
function placeGenericFortress(center, radius = 20, playerId = 0, style, irregularity = 0.5, gateOccurence = 3, maxTries = 100, constraints = undefined) | function placeGenericFortress(center, radius = 20, playerId = 0, style, irregularity = 0.5, gateOccurence = 3, maxTries = 100, constraints = undefined) | ||||
Lint: JSHintBear Regular parameters should not come after default parameters. Lint: JSHintBear: `Regular parameters should not come after default parameters.` | |||||
{ | { | ||||
style = validateStyle(style, playerId); | style = validateStyle(style, playerId); | ||||
// Setup some vars | // Setup some vars | ||||
let startAngle = randomAngle(); | let startAngle = randomAngle(); | ||||
let actualOff = new Vector2D(radius, 0).rotate(-startAngle); | let actualOff = new Vector2D(radius, 0).rotate(-startAngle); | ||||
let actualAngle = startAngle; | let actualAngle = startAngle; | ||||
let pointDistance = getWallLength(style, ["long", "tower"]); | let pointDistance = getWallLength(style, ["long", "tower"]); | ||||
▲ Show 20 Lines • Show All 70 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Arrow function should not return assignment.