From ac525fcd2ad38ec507af9a3ec915c88159bbf4d4 Mon Sep 17 00:00:00 2001 From: mimo Date: Sat, 14 May 2016 12:14:12 +0000 Subject: [PATCH] some cleanup in petra This was SVN commit r18170. --- .../mods/public/simulation/ai/petra/army.js | 16 ++++++------- .../mods/public/simulation/ai/petra/config.js | 6 ++--- .../simulation/ai/petra/headquarters.js | 23 ++++++++----------- .../simulation/ai/petra/navalManager.js | 2 +- .../mods/public/simulation/ai/petra/queue.js | 16 ++++++------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/petra/army.js b/binaries/data/mods/public/simulation/ai/petra/army.js index a7b0e3ca12..207a05a1c3 100644 --- a/binaries/data/mods/public/simulation/ai/petra/army.js +++ b/binaries/data/mods/public/simulation/ai/petra/army.js @@ -95,7 +95,7 @@ m.Army.prototype.evaluateStrength = function (ent, isOwn, remove) if (ent.hasClass("Structure")) { if (ent.owner() !== PlayerID) - entStrength = (ent.getDefaultArrow() ? 6*ent.getDefaultArrow() : 4); + entStrength = ent.getDefaultArrow() ? 6*ent.getDefaultArrow() : 4; else // small strength used only when we try to recover capture points entStrength = 2; } @@ -156,7 +156,7 @@ m.Army.prototype.removeFoe = function (gameState, enemyId, enemyEntity) if (this.assignedTo[to] == enemyId) this.assignedTo[to] = undefined; - let ent = (enemyEntity ? enemyEntity : gameState.getEntityById(enemyId)); + let ent = enemyEntity ? enemyEntity : gameState.getEntityById(enemyId); if (ent) // TODO recompute strength when no entities (could happen if capture+destroy) { this.evaluateStrength(ent, false, true); @@ -209,7 +209,7 @@ m.Army.prototype.removeOwn = function (gameState, id, Entity) } this.assignedTo[id] = undefined; - let ent = (Entity ? Entity : gameState.getEntityById(id)); + let ent = Entity ? Entity : gameState.getEntityById(id); if (ent) // TODO recompute strength when no entities (could happen if capture+destroy) { this.evaluateStrength(ent, true, true); @@ -219,7 +219,7 @@ m.Army.prototype.removeOwn = function (gameState, id, Entity) else ent.setMetadata(PlayerID, "plan", undefined); - var formerSubrole = ent.getMetadata(PlayerID, "formerSubrole"); + let formerSubrole = ent.getMetadata(PlayerID, "formerSubrole"); if (formerSubrole !== undefined) ent.setMetadata(PlayerID, "subrole", formerSubrole); else @@ -252,7 +252,7 @@ m.Army.prototype.isCapturing = function (gameState) if (this.foeEntities.length != 1) return false; let ent = gameState.getEntityById(this.foeEntities[0]); - return (ent && ent.hasClass("Structure")); + return ent && ent.hasClass("Structure"); }; // this one is "undefined entity" proof because it's called at odd times. @@ -397,10 +397,10 @@ m.Army.prototype.onUpdate = function (gameState) this.positionLastUpdate = gameState.ai.elapsedTime; // Check for breakaways. - for (var i = 0; i < this.foeEntities.length; ++i) + for (let i = 0; i < this.foeEntities.length; ++i) { - var id = this.foeEntities[i]; - var ent = gameState.getEntityById(id); + let id = this.foeEntities[i]; + let ent = gameState.getEntityById(id); if (!ent || !ent.position()) continue; if (API3.SquareVectorDistance(ent.position(), this.foePosition) > this.breakawaySize) diff --git a/binaries/data/mods/public/simulation/ai/petra/config.js b/binaries/data/mods/public/simulation/ai/petra/config.js index c403a3e05c..4d22d538fb 100644 --- a/binaries/data/mods/public/simulation/ai/petra/config.js +++ b/binaries/data/mods/public/simulation/ai/petra/config.js @@ -4,7 +4,7 @@ var PETRA = function(m) m.Config = function(difficulty) { // 0 is sandbox, 1 is very easy, 2 is easy, 3 is medium, 4 is hard and 5 is very hard. - this.difficulty = (difficulty !== undefined) ? difficulty : 3; + this.difficulty = difficulty !== undefined ? difficulty : 3; // debug level: 0=none, 1=sanity checks, 2=debug, 3=detailed debug, -100=serializatio debug this.debug = 0; @@ -127,14 +127,14 @@ m.Config.prototype.setConfig = function(gameState) this.Economy.cityPhase = 240000; this.Economy.supportRatio = 0.5; this.Economy.provisionFields = 1; - this.Military.numWoodenTowers = (this.personality.defensive > 0.66) ? 1 : 0; + this.Military.numWoodenTowers = this.personality.defensive > 0.66 ? 1 : 0; } else if (this.difficulty < 3) { this.Economy.cityPhase = 1800; this.Economy.supportRatio = 0.4; this.Economy.provisionFields = 1; - this.Military.numWoodenTowers = (this.personality.defensive > 0.66) ? 1 : 0; + this.Military.numWoodenTowers = this.personality.defensive > 0.66 ? 1 : 0; } else { diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index a78900265c..ed0ba75ed7 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -514,9 +514,9 @@ m.HQ.prototype.trainMoreWorkers = function(gameState, queues) m.HQ.prototype.findBestTrainableUnit = function(gameState, classes, requirements) { var units; - if (classes.indexOf("Hero") != -1) + if (classes.indexOf("Hero") !== -1) units = gameState.findTrainableUnits(classes, []); - else if (classes.indexOf("Siege") != -1) // We do not want siege tower as AI does not know how to use it + else if (classes.indexOf("Siege") !== -1) // We do not want siege tower as AI does not know how to use it units = gameState.findTrainableUnits(classes, ["SiegeTower"]); else // We do not want hero when not explicitely specified units = gameState.findTrainableUnits(classes, ["Hero"]); @@ -744,7 +744,7 @@ m.HQ.prototype.findEconomicCCLocation = function(gameState, template, resource, for (let j = 0; j < this.territoryMap.length; ++j) { - if (this.territoryMap.getOwnerIndex(j) != 0) + if (this.territoryMap.getOwnerIndex(j) !== 0) continue; // with enough room around to build the cc let i = this.territoryMap.getNonObstructedTile(j, radius, obstructions); @@ -762,10 +762,7 @@ m.HQ.prototype.findEconomicCCLocation = function(gameState, template, resource, let pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)]; if (proximity) // this is our first cc, let's do it near our units - { - let dist = API3.SquareVectorDistance(proximity, pos); - norm /= (1 + dist/scale); - } + norm /= (1 + API3.SquareVectorDistance(proximity, pos) / scale); else { let minDist = Math.min(); @@ -914,7 +911,7 @@ m.HQ.prototype.findStrategicCCLocation = function(gameState, template) for (let j = 0; j < this.territoryMap.length; ++j) { - if (this.territoryMap.getOwnerIndex(j) != 0) + if (this.territoryMap.getOwnerIndex(j) !== 0) continue; // with enough room around to build the cc let i = this.territoryMap.getNonObstructedTile(j, radius, obstructions); @@ -1119,12 +1116,13 @@ m.HQ.prototype.findDefensiveLocation = function(gameState, template) var ownStructures = gameState.getOwnStructures().filter(API3.Filters.byClassesOr(["Fortress", "Tower"])).toEntityArray(); var enemyStructures = gameState.getEnemyStructures().filter(API3.Filters.byClassesOr(["CivCentre", "Fortress", "Tower"])).toEntityArray(); - var wonderMode = (gameState.getGameType() === "wonder"); + var wonderMode = gameState.getGameType() === "wonder"; var wonderDistmin; + var wonders; if (wonderMode) { - var wonders = gameState.getOwnStructures().filter(API3.Filters.byClass("Wonder")).toEntityArray(); - wonderMode = (wonders.length != 0); + wonders = gameState.getOwnStructures().filter(API3.Filters.byClass("Wonder")).toEntityArray(); + wonderMode = wonders.length !== 0; if (wonderMode) wonderDistmin = (50 + wonders[0].footprintRadius()) * (50 + wonders[0].footprintRadius()); } @@ -1768,8 +1766,7 @@ m.HQ.prototype.canBuild = function(gameState, structure) { if (this.stopBuilding.get(type) > gameState.ai.elapsedTime) return false; - else - this.stopBuilding.delete(type); + this.stopBuilding.delete(type); } if (gameState.isDisabledTemplates(type)) diff --git a/binaries/data/mods/public/simulation/ai/petra/navalManager.js b/binaries/data/mods/public/simulation/ai/petra/navalManager.js index 32b37e8c5e..0c2b45980d 100644 --- a/binaries/data/mods/public/simulation/ai/petra/navalManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/navalManager.js @@ -403,7 +403,7 @@ m.NavalManager.prototype.splitTransport = function(gameState, plan) API3.warn(">>>> previous plan left with units " + plan.units.length); if (nbUnits) this.transportPlans.push(newplan); - return (nbUnits !== 0); + return nbUnits !== 0; }; /** diff --git a/binaries/data/mods/public/simulation/ai/petra/queue.js b/binaries/data/mods/public/simulation/ai/petra/queue.js index 2d0eb71bbf..2222e12f24 100644 --- a/binaries/data/mods/public/simulation/ai/petra/queue.js +++ b/binaries/data/mods/public/simulation/ai/petra/queue.js @@ -49,8 +49,7 @@ m.Queue.prototype.getNext = function() { if (this.plans.length > 0) return this.plans[0]; - else - return null; + return null; }; m.Queue.prototype.startNext = function(gameState) @@ -60,15 +59,14 @@ m.Queue.prototype.startNext = function(gameState) this.plans.shift().start(gameState); return true; } - else - return false; + return false; }; // returns the maximal account we'll accept for this queue. // Currently all the cost of the first element and fraction of that of the second m.Queue.prototype.maxAccountWanted = function(gameState, fraction) { - var cost = new API3.Resources(); + let cost = new API3.Resources(); if (this.plans.length > 0 && this.plans[0].isGo(gameState)) cost.add(this.plans[0].getCost()); if (this.plans.length > 1 && this.plans[1].isGo(gameState) && fraction > 0) @@ -82,7 +80,7 @@ m.Queue.prototype.maxAccountWanted = function(gameState, fraction) m.Queue.prototype.queueCost = function() { - var cost = new API3.Resources(); + let cost = new API3.Resources(); for (let plan of this.plans) cost.add(plan.getCost()); return cost; @@ -100,7 +98,7 @@ m.Queue.prototype.hasQueuedUnits = function() m.Queue.prototype.countQueuedUnits = function() { - var count = 0; + let count = 0; for (let plan of this.plans) count += plan.number; return count; @@ -113,7 +111,7 @@ m.Queue.prototype.hasQueuedUnitsWithClass = function(classe) m.Queue.prototype.countQueuedUnitsWithClass = function(classe) { - var count = 0; + let count = 0; for (let plan of this.plans) if (plan.template && plan.template.hasClass(classe)) count += plan.number; @@ -122,7 +120,7 @@ m.Queue.prototype.countQueuedUnitsWithClass = function(classe) m.Queue.prototype.countQueuedUnitsWithMetadata = function(data, value) { - var count = 0; + let count = 0; for (let plan of this.plans) if (plan.metadata[data] && plan.metadata[data] == value) count += plan.number;