Fix eslint rule 'prefer-const' in petra/queueplan

eslint --no-config-lookup --fix --rule '"prefer-const": 1' \
    binaries/data/mods/public/simulation/ai/petra/queueplan*

Ref: #7812
Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser 2025-05-07 15:35:06 +02:00
parent baa8d99be3
commit 06b8857c04
No known key found for this signature in database
4 changed files with 151 additions and 151 deletions

View file

@ -47,7 +47,7 @@ PETRA.QueuePlan.prototype.start = function(gameState)
PETRA.QueuePlan.prototype.getCost = function()
{
let costs = new API3.Resources();
const costs = new API3.Resources();
costs.add(this.cost);
if (this.number !== 1)
costs.multiply(this.number);

View file

@ -38,7 +38,7 @@ PETRA.ConstructionPlan.prototype.start = function(gameState)
// We don't care which builder we assign, since they won't actually do
// the building themselves - all we care about is that there is at least
// one unit that can start the foundation (should always be the case here).
let builder = gameState.findBuilder(this.type);
const builder = gameState.findBuilder(this.type);
if (!builder)
{
API3.warn("petra error: builder not found when starting construction.");
@ -46,7 +46,7 @@ PETRA.ConstructionPlan.prototype.start = function(gameState)
return;
}
let pos = this.findGoodPosition(gameState);
const pos = this.findGoodPosition(gameState);
if (!pos)
{
gameState.ai.HQ.buildManager.setUnbuildable(gameState, this.type, 90, "room");
@ -58,7 +58,7 @@ PETRA.ConstructionPlan.prototype.start = function(gameState)
gameState.getOwnEntitiesByClass("Market", true).hasEntities()))
{
// Check if this Market is still worth building (others may have been built making it useless).
let tradeManager = gameState.ai.HQ.tradeManager;
const tradeManager = gameState.ai.HQ.tradeManager;
tradeManager.checkRoutes(gameState);
if (!tradeManager.isNewMarketWorth(this.metadata.expectedGain))
{
@ -81,9 +81,9 @@ PETRA.ConstructionPlan.prototype.start = function(gameState)
if (this.template.buildPlacementType() == "shore")
{
// adjust a bit the position if needed
let cosa = Math.cos(pos.angle);
let sina = Math.sin(pos.angle);
let shiftMax = gameState.ai.HQ.territoryMap.cellSize;
const cosa = Math.cos(pos.angle);
const sina = Math.sin(pos.angle);
const shiftMax = gameState.ai.HQ.territoryMap.cellSize;
for (let shift = 0; shift <= shiftMax; shift += 2)
{
builder.construct(this.type, pos.x-shift*sina, pos.z-shift*cosa, pos.angle, this.metadata);
@ -113,16 +113,16 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
if (template.buildPlacementType() == "shore")
return this.findDockPosition(gameState);
let HQ = gameState.ai.HQ;
const HQ = gameState.ai.HQ;
if (template.hasClass("Storehouse") && this.metadata && this.metadata.base)
{
// recompute the best dropsite location in case some conditions have changed
let base = HQ.getBaseByID(this.metadata.base);
let type = this.metadata.type ? this.metadata.type : "wood";
const base = HQ.getBaseByID(this.metadata.base);
const type = this.metadata.type ? this.metadata.type : "wood";
const newpos = base.findBestDropsiteLocation(gameState, type, template._templateName);
if (newpos && newpos.quality > 0)
{
let pos = newpos.pos;
const pos = newpos.pos;
return { "x": pos[0], "z": pos[1], "angle": 3*Math.PI/4, "base": this.metadata.base };
}
}
@ -134,7 +134,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
let pos;
if (this.metadata && this.metadata.resource)
{
let proximity = this.metadata.proximity ? this.metadata.proximity : undefined;
const proximity = this.metadata.proximity ? this.metadata.proximity : undefined;
pos = HQ.findEconomicCCLocation(gameState, template, this.metadata.resource, proximity);
}
else
@ -143,7 +143,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
if (pos)
return { "x": pos[0], "z": pos[1], "angle": 3*Math.PI/4, "base": 0 };
// No possible location, try to build instead a dock in a not-enemy island
let templateName = gameState.applyCiv("structures/{civ}/dock");
const templateName = gameState.applyCiv("structures/{civ}/dock");
if (gameState.ai.HQ.canBuild(gameState, templateName) && !gameState.isTemplateDisabled(templateName))
{
template = gameState.getTemplate(templateName);
@ -154,7 +154,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
}
else if (template.hasClasses(["Tower", "Fortress", "ArmyCamp"]))
{
let pos = HQ.findDefensiveLocation(gameState, template);
const pos = HQ.findDefensiveLocation(gameState, template);
if (pos)
return { "x": pos[0], "z": pos[1], "angle": 3*Math.PI/4, "base": pos[2] };
// if this fortress is our first one, just try the standard placement
@ -163,7 +163,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
}
else if (template.hasClass("Market")) // Docks are done before.
{
let pos = HQ.findMarketLocation(gameState, template);
const pos = HQ.findMarketLocation(gameState, template);
if (pos && pos[2] > 0)
{
if (!this.metadata)
@ -178,15 +178,15 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
// Compute each tile's closeness to friendly structures:
let placement = new API3.Map(gameState.sharedScript, "territory");
let cellSize = placement.cellSize; // size of each tile
const placement = new API3.Map(gameState.sharedScript, "territory");
const cellSize = placement.cellSize; // size of each tile
let alreadyHasHouses = false;
if (this.position) // If a position was specified then place the building as close to it as possible
{
let x = Math.floor(this.position[0] / cellSize);
let z = Math.floor(this.position[1] / cellSize);
const x = Math.floor(this.position[0] / cellSize);
const z = Math.floor(this.position[1] / cellSize);
placement.addInfluence(x, z, 255);
}
else // No position was specified so try and find a sensible place to build
@ -195,7 +195,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
// if we really need houses (i.e. Phasing without enough village building), do not apply these constraints
if (this.metadata && this.metadata.base !== undefined)
{
let base = this.metadata.base;
const base = this.metadata.base;
for (let j = 0; j < placement.map.length; ++j)
if (HQ.baseAtIndex(j) == base)
placement.set(j, 45);
@ -210,11 +210,11 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
if (!HQ.requireHouses || !template.hasClass("House"))
{
gameState.getOwnStructures().forEach(function(ent) {
let pos = ent.position();
let x = Math.round(pos[0] / cellSize);
let z = Math.round(pos[1] / cellSize);
const pos = ent.position();
const x = Math.round(pos[0] / cellSize);
const z = Math.round(pos[1] / cellSize);
let struct = PETRA.getBuiltEntity(gameState, ent);
const struct = PETRA.getBuiltEntity(gameState, ent);
if (struct.resourceDropsiteTypes() && struct.resourceDropsiteTypes().indexOf("food") != -1)
{
if (template.hasClasses(["Field", "Corral"]))
@ -257,13 +257,13 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
// Requires to be inside our territory, and inside our base territory if required
// and if our first market, put it on border if possible to maximize distance with next Market.
let favorBorder = template.hasClass("Market");
let disfavorBorder = gameState.currentPhase() > 1 && !template.hasDefensiveFire();
let favoredBase = this.metadata && (this.metadata.favoredBase ||
const favorBorder = template.hasClass("Market");
const disfavorBorder = gameState.currentPhase() > 1 && !template.hasDefensiveFire();
const favoredBase = this.metadata && (this.metadata.favoredBase ||
(this.metadata.militaryBase ? HQ.findBestBaseForMilitary(gameState) : undefined));
if (this.metadata && this.metadata.base !== undefined)
{
let base = this.metadata.base;
const base = this.metadata.base;
for (let j = 0; j < placement.map.length; ++j)
{
if (HQ.baseAtIndex(j) != base)
@ -275,8 +275,8 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
else if (disfavorBorder && !(HQ.borderMap.map[j] & PETRA.fullBorder_Mask))
placement.set(j, placement.map[j] + 10);
let x = (j % placement.width + 0.5) * cellSize;
let z = (Math.floor(j / placement.width) + 0.5) * cellSize;
const x = (j % placement.width + 0.5) * cellSize;
const z = (Math.floor(j / placement.width) + 0.5) * cellSize;
if (HQ.isNearInvadingArmy([x, z]))
placement.map[j] = 0;
}
@ -295,8 +295,8 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
else if (disfavorBorder && !(HQ.borderMap.map[j] & PETRA.fullBorder_Mask))
placement.set(j, placement.map[j] + 10);
let x = (j % placement.width + 0.5) * cellSize;
let z = (Math.floor(j / placement.width) + 0.5) * cellSize;
const x = (j % placement.width + 0.5) * cellSize;
const z = (Math.floor(j / placement.width) + 0.5) * cellSize;
if (HQ.isNearInvadingArmy([x, z]))
placement.map[j] = 0;
else if (favoredBase && HQ.baseAtIndex(j) == favoredBase)
@ -311,7 +311,7 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
// note: not for houses and dropsites who ought to be closer to either each other or a resource.
// also not for fields who can be stacked quite a bit
let obstructions = PETRA.createObstructionMap(gameState, 0, template);
const obstructions = PETRA.createObstructionMap(gameState, 0, template);
// obstructions.dumpIm(template.buildPlacementType() + "_obstructions.png");
let radius = 0;
@ -338,13 +338,13 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
if (!bestTile.val)
return false;
let bestIdx = bestTile.idx;
const bestIdx = bestTile.idx;
let x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
let z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
const x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
const z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
let territorypos = placement.gamePosToMapPos([x, z]);
let territoryIndex = territorypos[0] + territorypos[1]*placement.width;
const territorypos = placement.gamePosToMapPos([x, z]);
const territoryIndex = territorypos[0] + territorypos[1]*placement.width;
// default angle = 3*Math.PI/4;
return { "x": x, "z": z, "angle": 3*Math.PI/4, "base": HQ.baseAtIndex(territoryIndex) };
};
@ -361,10 +361,10 @@ PETRA.ConstructionPlan.prototype.findGoodPosition = function(gameState)
*/
PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
{
let template = this.template;
let territoryMap = gameState.ai.HQ.territoryMap;
const template = this.template;
const territoryMap = gameState.ai.HQ.territoryMap;
let obstructions = PETRA.createObstructionMap(gameState, 0, template);
const obstructions = PETRA.createObstructionMap(gameState, 0, template);
// obstructions.dumpIm(template.buildPlacementType() + "_obstructions.png");
let bestIdx;
@ -373,23 +373,23 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
let bestLand;
let bestWater;
let bestVal = -1;
let navalPassMap = gameState.ai.accessibility.navalPassMap;
const navalPassMap = gameState.ai.accessibility.navalPassMap;
let width = gameState.ai.HQ.territoryMap.width;
let cellSize = gameState.ai.HQ.territoryMap.cellSize;
const width = gameState.ai.HQ.territoryMap.width;
const cellSize = gameState.ai.HQ.territoryMap.cellSize;
let nbShips = gameState.ai.HQ.navalManager.transportShips.length;
const nbShips = gameState.ai.HQ.navalManager.transportShips.length;
let wantedLand = this.metadata && this.metadata.land ? this.metadata.land : null;
let wantedSea = this.metadata && this.metadata.sea ? this.metadata.sea : null;
let proxyAccess = this.metadata && this.metadata.proximity ? gameState.ai.accessibility.getAccessValue(this.metadata.proximity) : null;
let oversea = this.metadata && this.metadata.oversea ? this.metadata.oversea : null;
const wantedSea = this.metadata && this.metadata.sea ? this.metadata.sea : null;
const proxyAccess = this.metadata && this.metadata.proximity ? gameState.ai.accessibility.getAccessValue(this.metadata.proximity) : null;
const oversea = this.metadata && this.metadata.oversea ? this.metadata.oversea : null;
if (nbShips == 0 && proxyAccess && proxyAccess > 1)
{
wantedLand = {};
wantedLand[proxyAccess] = true;
}
let dropsiteTypes = template.resourceDropsiteTypes();
let radius = Math.ceil(template.obstructionRadius().max / obstructions.cellSize);
const dropsiteTypes = template.resourceDropsiteTypes();
const radius = Math.ceil(template.obstructionRadius().max / obstructions.cellSize);
let halfSize = 0; // used for dock angle
let halfDepth = 0; // used by checkPlacement
@ -411,10 +411,10 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
// water is a measure of the water space around, and maxWater is the max value that can be returned by checkDockPlacement
const maxRes = 10;
const maxWater = 16;
let ccEnts = oversea ? gameState.updatingGlobalCollection("allCCs", API3.Filters.byClass("CivCentre")) : null;
let docks = oversea ? gameState.getOwnStructures().filter(API3.Filters.byClass("Dock")) : null;
const ccEnts = oversea ? gameState.updatingGlobalCollection("allCCs", API3.Filters.byClass("CivCentre")) : null;
const docks = oversea ? gameState.getOwnStructures().filter(API3.Filters.byClass("Dock")) : null;
// Normalisation factors (only guessed, no attempt to optimize them)
let factor = proxyAccess ? 1 : oversea ? 0.2 : 40;
const factor = proxyAccess ? 1 : oversea ? 0.2 : 40;
for (let j = 0; j < territoryMap.length; ++j)
{
if (!this.isDockLocation(gameState, j, halfDepth, wantedLand, wantedSea))
@ -428,14 +428,14 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
continue;
score *= factor;
}
let i = territoryMap.getNonObstructedTile(j, radius, obstructions);
const i = territoryMap.getNonObstructedTile(j, radius, obstructions);
if (i < 0)
continue;
if (wantedSea && navalPassMap[i] != wantedSea)
continue;
let res = dropsiteTypes ? Math.min(maxRes, this.getResourcesAround(gameState, dropsiteTypes, j, 80)) : maxRes;
let pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)];
const res = dropsiteTypes ? Math.min(maxRes, this.getResourcesAround(gameState, dropsiteTypes, j, 80)) : maxRes;
const pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)];
// If proximity is given, we look for the nearest point
if (proxyAccess)
@ -449,12 +449,12 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
// Not much farther to one of our cc than to enemy ones
let enemyDist;
let ownDist;
for (let cc of ccEnts.values())
for (const cc of ccEnts.values())
{
let owner = cc.owner();
const owner = cc.owner();
if (owner != PlayerID && !gameState.isPlayerEnemy(owner))
continue;
let dist = API3.SquareVectorDistance(pos, cc.position());
const dist = API3.SquareVectorDistance(pos, cc.position());
if (owner == PlayerID && (!ownDist || dist < ownDist))
ownDist = dist;
if (gameState.isPlayerEnemy(owner) && (!enemyDist || dist < enemyDist))
@ -465,11 +465,11 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
// And maximize distance for trade.
let dockDist = 0;
for (let dock of docks.values())
for (const dock of docks.values())
{
if (PETRA.getSeaAccess(gameState, dock) != navalPassMap[i])
continue;
let dist = API3.SquareVectorDistance(pos, dock.position());
const dist = API3.SquareVectorDistance(pos, dock.position());
if (dist > dockDist)
dockDist = dist;
}
@ -490,12 +490,12 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
if (bestIdx !== undefined && score > bestVal + 5 * maxWater)
continue;
let x = (i % obstructions.width + 0.5) * obstructions.cellSize;
let z = (Math.floor(i / obstructions.width) + 0.5) * obstructions.cellSize;
let angle = this.getDockAngle(gameState, x, z, halfSize);
const x = (i % obstructions.width + 0.5) * obstructions.cellSize;
const z = (Math.floor(i / obstructions.width) + 0.5) * obstructions.cellSize;
const angle = this.getDockAngle(gameState, x, z, halfSize);
if (angle == false)
continue;
let ret = this.checkDockPlacement(gameState, x, z, halfDepth, halfWidth, angle);
const ret = this.checkDockPlacement(gameState, x, z, halfDepth, halfWidth, angle);
if (!ret || !gameState.ai.HQ.landRegions[ret.land] || wantedLand && !wantedLand[ret.land])
continue;
// Final selection now that the checkDockPlacement water is known
@ -520,8 +520,8 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
if (!this.metadata.proximity && bestWater < 10 && gameState.currentPhase() == 1)
return false;
let x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
let z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
const x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
const z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
// Assign this dock to a base
let baseIndex = gameState.ai.HQ.baseAtIndex(bestJdx);
@ -536,15 +536,15 @@ PETRA.ConstructionPlan.prototype.findDockPosition = function(gameState)
*/
PETRA.ConstructionPlan.prototype.buildOverseaDock = function(gameState, template)
{
let docks = gameState.getOwnStructures().filter(API3.Filters.byClass("Dock"));
const docks = gameState.getOwnStructures().filter(API3.Filters.byClass("Dock"));
if (!docks.hasEntities())
return;
let passabilityMap = gameState.getPassabilityMap();
let cellArea = passabilityMap.cellSize * passabilityMap.cellSize;
let ccEnts = gameState.updatingGlobalCollection("allCCs", API3.Filters.byClass("CivCentre"));
const passabilityMap = gameState.getPassabilityMap();
const cellArea = passabilityMap.cellSize * passabilityMap.cellSize;
const ccEnts = gameState.updatingGlobalCollection("allCCs", API3.Filters.byClass("CivCentre"));
let land = {};
const land = {};
let found;
for (let i = 0; i < gameState.ai.accessibility.regionSize.length; ++i)
{
@ -552,7 +552,7 @@ PETRA.ConstructionPlan.prototype.buildOverseaDock = function(gameState, template
cellArea * gameState.ai.accessibility.regionSize[i] < 3600)
continue;
let keep = true;
for (let dock of docks.values())
for (const dock of docks.values())
{
if (PETRA.getLandAccess(gameState, dock) != i)
continue;
@ -562,9 +562,9 @@ PETRA.ConstructionPlan.prototype.buildOverseaDock = function(gameState, template
if (!keep)
continue;
let sea;
for (let cc of ccEnts.values())
for (const cc of ccEnts.values())
{
let ccAccess = PETRA.getLandAccess(gameState, cc);
const ccAccess = PETRA.getLandAccess(gameState, cc);
if (ccAccess != i)
{
if (cc.owner() == PlayerID && !sea)
@ -588,21 +588,21 @@ PETRA.ConstructionPlan.prototype.buildOverseaDock = function(gameState, template
if (!gameState.ai.HQ.navalMap)
API3.warn("petra.findOverseaLand on a non-naval map??? we should never go there ");
let oldTemplate = this.template;
let oldMetadata = this.metadata;
const oldTemplate = this.template;
const oldMetadata = this.metadata;
this.template = template;
let pos;
this.metadata = { "land": land, "oversea": true };
pos = this.findDockPosition(gameState);
if (pos)
{
let type = template.templateName();
let builder = gameState.findBuilder(type);
const type = template.templateName();
const builder = gameState.findBuilder(type);
this.metadata.base = pos.base;
// Adjust a bit the position if needed
let cosa = Math.cos(pos.angle);
let sina = Math.sin(pos.angle);
let shiftMax = gameState.ai.HQ.territoryMap.cellSize;
const cosa = Math.cos(pos.angle);
const sina = Math.sin(pos.angle);
const shiftMax = gameState.ai.HQ.territoryMap.cellSize;
for (let shift = 0; shift <= shiftMax; shift += 2)
{
builder.construct(type, pos.x-shift*sina, pos.z-shift*cosa, pos.angle, this.metadata);
@ -618,30 +618,30 @@ PETRA.ConstructionPlan.prototype.buildOverseaDock = function(gameState, template
PETRA.ConstructionPlan.prototype.getDockAngle = function(gameState, x, z, size)
{
let pos = gameState.ai.accessibility.gamePosToMapPos([x, z]);
let k = pos[0] + pos[1]*gameState.ai.accessibility.width;
let seaRef = gameState.ai.accessibility.navalPassMap[k];
const k = pos[0] + pos[1]*gameState.ai.accessibility.width;
const seaRef = gameState.ai.accessibility.navalPassMap[k];
if (seaRef < 2)
return false;
const numPoints = 16;
for (let dist = 0; dist < 4; ++dist)
{
let waterPoints = [];
const waterPoints = [];
for (let i = 0; i < numPoints; ++i)
{
let angle = 2 * Math.PI * i / numPoints;
const angle = 2 * Math.PI * i / numPoints;
pos = [x - (1+dist)*size*Math.sin(angle), z + (1+dist)*size*Math.cos(angle)];
pos = gameState.ai.accessibility.gamePosToMapPos(pos);
if (pos[0] < 0 || pos[0] >= gameState.ai.accessibility.width ||
pos[1] < 0 || pos[1] >= gameState.ai.accessibility.height)
continue;
let j = pos[0] + pos[1]*gameState.ai.accessibility.width;
const j = pos[0] + pos[1]*gameState.ai.accessibility.width;
if (gameState.ai.accessibility.navalPassMap[j] == seaRef)
waterPoints.push(i);
}
let length = waterPoints.length;
const length = waterPoints.length;
if (!length)
continue;
let consec = [];
const consec = [];
for (let i = 0; i < length; ++i)
{
let count = 0;
@ -656,7 +656,7 @@ PETRA.ConstructionPlan.prototype.getDockAngle = function(gameState, x, z, size)
}
let start = 0;
let count = 0;
for (let c in consec)
for (const c in consec)
{
if (consec[c] > count)
{
@ -679,12 +679,12 @@ PETRA.ConstructionPlan.prototype.getDockAngle = function(gameState, x, z, size)
*/
PETRA.ConstructionPlan.prototype.checkDockPlacement = function(gameState, x, z, halfDepth, halfWidth, angle)
{
let sz = halfDepth * Math.sin(angle);
let cz = halfDepth * Math.cos(angle);
const sz = halfDepth * Math.sin(angle);
const cz = halfDepth * Math.cos(angle);
// center back position
let pos = gameState.ai.accessibility.gamePosToMapPos([x - sz, z - cz]);
let j = pos[0] + pos[1]*gameState.ai.accessibility.width;
let land = gameState.ai.accessibility.landPassMap[j];
const land = gameState.ai.accessibility.landPassMap[j];
if (land < 2)
return null;
// center front position
@ -693,8 +693,8 @@ PETRA.ConstructionPlan.prototype.checkDockPlacement = function(gameState, x, z,
if (gameState.ai.accessibility.landPassMap[j] > 1 || gameState.ai.accessibility.navalPassMap[j] < 2)
return null;
// additional constraints compared to BuildRestriction.js to assure we have enough place to build
let sw = halfWidth * Math.cos(angle) * 3 / 4;
let cw = halfWidth * Math.sin(angle) * 3 / 4;
const sw = halfWidth * Math.cos(angle) * 3 / 4;
const cw = halfWidth * Math.sin(angle) * 3 / 4;
pos = gameState.ai.accessibility.gamePosToMapPos([x - sz + sw, z - cz - cw]);
j = pos[0] + pos[1]*gameState.ai.accessibility.width;
if (gameState.ai.accessibility.landPassMap[j] != land)
@ -704,8 +704,8 @@ PETRA.ConstructionPlan.prototype.checkDockPlacement = function(gameState, x, z,
if (gameState.ai.accessibility.landPassMap[j] != land)
return null;
let water = 0;
let sp = 15 * Math.sin(angle);
let cp = 15 * Math.cos(angle);
const sp = 15 * Math.sin(angle);
const cp = 15 * Math.cos(angle);
for (let i = 1; i < 5; ++i)
{
pos = gameState.ai.accessibility.gamePosToMapPos([x + sz + i*(sp+sw), z + cz + i*(cp-cw)]);
@ -744,14 +744,14 @@ PETRA.ConstructionPlan.prototype.around = [[ 1.0, 0.0], [ 0.87, 0.50], [ 0.50, 0
PETRA.ConstructionPlan.prototype.isDockLocation = function(gameState, j, dimension, wantedLand, wantedSea)
{
let width = gameState.ai.HQ.territoryMap.width;
let cellSize = gameState.ai.HQ.territoryMap.cellSize;
let dimLand = dimension + 1.5 * cellSize;
let dimSea = dimension + 2 * cellSize;
const width = gameState.ai.HQ.territoryMap.width;
const cellSize = gameState.ai.HQ.territoryMap.cellSize;
const dimLand = dimension + 1.5 * cellSize;
const dimSea = dimension + 2 * cellSize;
let accessibility = gameState.ai.accessibility;
let x = (j%width + 0.5) * cellSize;
let z = (Math.floor(j/width) + 0.5) * cellSize;
const accessibility = gameState.ai.accessibility;
const x = (j%width + 0.5) * cellSize;
const z = (Math.floor(j/width) + 0.5) * cellSize;
let pos = accessibility.gamePosToMapPos([x, z]);
let k = pos[0] + pos[1]*accessibility.width;
let landPass = accessibility.landPassMap[k];
@ -759,7 +759,7 @@ PETRA.ConstructionPlan.prototype.isDockLocation = function(gameState, j, dimensi
landPass < 2 && accessibility.navalPassMap[k] < 2)
return false;
for (let a of this.around)
for (const a of this.around)
{
pos = accessibility.gamePosToMapPos([x + dimLand*a[0], z + dimLand*a[1]]);
if (pos[0] < 0 || pos[0] >= accessibility.width)
@ -791,26 +791,26 @@ PETRA.ConstructionPlan.prototype.isDockLocation = function(gameState, j, dimensi
*/
PETRA.ConstructionPlan.prototype.getFrontierProximity = function(gameState, j)
{
let alliedVictory = gameState.getAlliedVictory();
let territoryMap = gameState.ai.HQ.territoryMap;
const alliedVictory = gameState.getAlliedVictory();
const territoryMap = gameState.ai.HQ.territoryMap;
let territoryOwner = territoryMap.getOwnerIndex(j);
if (territoryOwner == PlayerID || alliedVictory && gameState.isPlayerAlly(territoryOwner))
return 0;
let borderMap = gameState.ai.HQ.borderMap;
let width = territoryMap.width;
let step = Math.round(24 / territoryMap.cellSize);
let ix = j % width;
let iz = Math.floor(j / width);
const borderMap = gameState.ai.HQ.borderMap;
const width = territoryMap.width;
const step = Math.round(24 / territoryMap.cellSize);
const ix = j % width;
const iz = Math.floor(j / width);
let best = 5;
for (let a of this.around)
for (const a of this.around)
{
for (let i = 1; i < 5; ++i)
{
let jx = ix + Math.round(i*step*a[0]);
const jx = ix + Math.round(i*step*a[0]);
if (jx < 0 || jx >= width)
continue;
let jz = iz + Math.round(i*step*a[1]);
const jz = iz + Math.round(i*step*a[1]);
if (jz < 0 || jz >= width)
continue;
if (borderMap.map[jx+width*jz] & PETRA.outside_Mask)
@ -835,32 +835,32 @@ PETRA.ConstructionPlan.prototype.getFrontierProximity = function(gameState, j)
*/
PETRA.ConstructionPlan.prototype.getResourcesAround = function(gameState, types, i, radius)
{
let resourceMaps = gameState.sharedScript.resourceMaps;
let w = resourceMaps.wood.width;
let cellSize = resourceMaps.wood.cellSize;
let size = Math.floor(radius / cellSize);
let ix = i % w;
let iy = Math.floor(i / w);
const resourceMaps = gameState.sharedScript.resourceMaps;
const w = resourceMaps.wood.width;
const cellSize = resourceMaps.wood.cellSize;
const size = Math.floor(radius / cellSize);
const ix = i % w;
const iy = Math.floor(i / w);
let total = 0;
let nbcell = 0;
for (let k of types)
for (const k of types)
{
if (k == "food" || !resourceMaps[k])
continue;
let weigh0 = k == "wood" ? 2 : 1;
const weigh0 = k == "wood" ? 2 : 1;
for (let dy = 0; dy <= size; ++dy)
{
let dxmax = size - dy;
const dxmax = size - dy;
let ky = iy + dy;
if (ky >= 0 && ky < w)
{
for (let dx = -dxmax; dx <= dxmax; ++dx)
{
let kx = ix + dx;
const kx = ix + dx;
if (kx < 0 || kx >= w)
continue;
let ddx = dx > 0 ? dx : -dx;
let weight = weigh0 * (dxmax - ddx) / size;
const ddx = dx > 0 ? dx : -dx;
const weight = weigh0 * (dxmax - ddx) / size;
total += weight * resourceMaps[k].map[kx + w * ky];
nbcell += weight;
}
@ -872,11 +872,11 @@ PETRA.ConstructionPlan.prototype.getResourcesAround = function(gameState, types,
{
for (let dx = -dxmax; dx <= dxmax; ++dx)
{
let kx = ix + dx;
const kx = ix + dx;
if (kx < 0 || kx >= w)
continue;
let ddx = dx > 0 ? dx : -dx;
let weight = weigh0 * (dxmax - ddx) / size;
const ddx = dx > 0 ? dx : -dx;
const weight = weigh0 * (dxmax - ddx) / size;
total += weight * resourceMaps[k].map[kx + w * ky];
nbcell += weight;
}
@ -896,9 +896,9 @@ PETRA.ConstructionPlan.prototype.isGo = function(gameState)
if (gameState.getPopulationMax() <= gameState.getPopulationLimit())
return false;
let freeSlots = gameState.getPopulationLimit() - gameState.getPopulation();
for (let ent of gameState.getOwnFoundations().values())
for (const ent of gameState.getOwnFoundations().values())
{
let template = gameState.getBuiltTemplate(ent.templateName());
const template = gameState.getBuiltTemplate(ent.templateName());
if (template)
freeSlots += template.getPopulationBonus();
}
@ -937,7 +937,7 @@ PETRA.ConstructionPlan.prototype.Serialize = function()
PETRA.ConstructionPlan.prototype.Deserialize = function(gameState, data)
{
for (let key in data)
for (const key in data)
this[key] = data[key];
this.cost = new API3.Resources();

View file

@ -7,7 +7,7 @@ PETRA.ResearchPlan = function(gameState, type, rush = false)
return false;
// Refine the estimated cost
let researchers = this.getBestResearchers(gameState, true);
const researchers = this.getBestResearchers(gameState, true);
if (researchers)
this.cost = new API3.Resources(this.template.cost(researchers[0]));
@ -30,16 +30,16 @@ PETRA.ResearchPlan.prototype.canStart = function(gameState)
PETRA.ResearchPlan.prototype.getBestResearchers = function(gameState, noRequirementCheck = false)
{
let allResearchers = gameState.findResearchers(this.type, noRequirementCheck);
const allResearchers = gameState.findResearchers(this.type, noRequirementCheck);
if (!allResearchers || !allResearchers.hasEntities())
return undefined;
// Keep only researchers with smallest cost
let costMin = Math.min();
let researchers;
for (let ent of allResearchers.values())
for (const ent of allResearchers.values())
{
let cost = this.template.costSum(ent);
const cost = this.template.costSum(ent);
if (cost === costMin)
researchers.push(ent);
else if (cost < costMin)
@ -99,7 +99,7 @@ PETRA.ResearchPlan.prototype.Serialize = function()
PETRA.ResearchPlan.prototype.Deserialize = function(gameState, data)
{
for (let key in data)
for (const key in data)
this[key] = data[key];
this.cost = new API3.Resources();

View file

@ -7,8 +7,8 @@ PETRA.TrainingPlan = function(gameState, type, metadata, number = 1, maxMerge =
}
// Refine the estimated cost and add pop cost
let trainers = this.getBestTrainers(gameState);
let trainer = trainers ? trainers[0] : undefined;
const trainers = this.getBestTrainers(gameState);
const trainer = trainers ? trainers[0] : undefined;
this.cost = new API3.Resources(this.template.cost(trainer), +this.template._template.Cost.Population);
this.category = "unit";
@ -33,7 +33,7 @@ PETRA.TrainingPlan.prototype.getBestTrainers = function(gameState)
{
if (this.metadata && this.metadata.trainer)
{
let trainer = gameState.getEntityById(this.metadata.trainer);
const trainer = gameState.getEntityById(this.metadata.trainer);
if (trainer)
return [trainer];
}
@ -49,9 +49,9 @@ PETRA.TrainingPlan.prototype.getBestTrainers = function(gameState)
// Keep only trainers with smallest cost
let costMin = Math.min();
let trainers;
for (let ent of allTrainers.values())
for (const ent of allTrainers.values())
{
let cost = this.template.costSum(ent);
const cost = this.template.costSum(ent);
if (cost === costMin)
trainers.push(ent);
else if (cost < costMin)
@ -67,8 +67,8 @@ PETRA.TrainingPlan.prototype.start = function(gameState)
{
if (this.metadata && this.metadata.trainer)
{
let metadata = {};
for (let key in this.metadata)
const metadata = {};
for (const key in this.metadata)
if (key !== "trainer")
metadata[key] = this.metadata[key];
this.metadata = metadata;
@ -80,7 +80,7 @@ PETRA.TrainingPlan.prototype.start = function(gameState)
if (this.metadata && this.metadata.index)
wantedIndex = this.metadata.index;
const workerUnit = this.metadata && this.metadata.role && this.metadata.role === PETRA.Worker.ROLE_WORKER;
let supportUnit = this.template.hasClass("Support");
const supportUnit = this.template.hasClass("Support");
this.trainers.sort(function(a, b) {
// Prefer training buildings with short queues
let aa = a.trainingQueueTime();
@ -99,8 +99,8 @@ PETRA.TrainingPlan.prototype.start = function(gameState)
bb += 50;
}
// Give also priority to buildings with the right accessibility
let aBase = a.getMetadata(PlayerID, "base");
let bBase = b.getMetadata(PlayerID, "base");
const aBase = a.getMetadata(PlayerID, "base");
const bBase = b.getMetadata(PlayerID, "base");
if (wantedIndex)
{
if (!aBase || gameState.ai.HQ.getBaseByID(aBase).accessIndex != wantedIndex)
@ -111,8 +111,8 @@ PETRA.TrainingPlan.prototype.start = function(gameState)
// Then, if workers, small preference for bases with less workers
if (workerUnit && aBase && bBase && aBase != bBase)
{
let apop = gameState.ai.HQ.getBaseByID(aBase).workers.length;
let bpop = gameState.ai.HQ.getBaseByID(bBase).workers.length;
const apop = gameState.ai.HQ.getBaseByID(aBase).workers.length;
const bpop = gameState.ai.HQ.getBaseByID(bBase).workers.length;
if (apop > bpop)
aa++;
else if (bpop > apop)
@ -149,7 +149,7 @@ PETRA.TrainingPlan.prototype.Serialize = function()
PETRA.TrainingPlan.prototype.Deserialize = function(gameState, data)
{
for (let key in data)
for (const key in data)
this[key] = data[key];
this.cost = new API3.Resources();