petra cleanup

This was SVN commit r17968.
This commit is contained in:
mimo 2016-04-02 14:49:08 +00:00
parent 18960ddcf8
commit 8211f95978
7 changed files with 46 additions and 46 deletions

View File

@ -56,8 +56,8 @@ m.BaseManager.prototype.init = function(gameState, state)
this.gatherers = {};
for (let type of this.Config.resources)
{
this.dropsiteSupplies[type] = {"nearby": [], "medium": [], "faraway": []};
this.gatherers[type] = {"nextCheck": 0, "used": 0, "lost": 0};
this.dropsiteSupplies[type] = { "nearby": [], "medium": [], "faraway": [] };
this.gatherers[type] = { "nextCheck": 0, "used": 0, "lost": 0 };
}
};
@ -403,7 +403,7 @@ m.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource)
var x = (bestIdx % obstructions.width + 0.5) * obstructions.cellSize;
var z = (Math.floor(bestIdx / obstructions.width) + 0.5) * obstructions.cellSize;
return {"quality": bestVal, "pos": [x, z]};
return { "quality": bestVal, "pos": [x, z] };
};
m.BaseManager.prototype.getResourceLevel = function (gameState, type, nearbyOnly = false)
@ -447,7 +447,10 @@ m.BaseManager.prototype.checkResourceLevels = function (gameState, queues)
if (numFarms + numQueue === 0) // starting game, rely on fruits as long as we have enough of them
{
if (count < 600)
{
queues.field.addPlan(new m.ConstructionPlan(gameState, "structures/{civ}_field", { "base": this.ID }));
gameState.ai.HQ.needFarm = true;
}
}
else
{
@ -460,7 +463,7 @@ m.BaseManager.prototype.checkResourceLevels = function (gameState, queues)
}
}
}
else if (!queues.dropsites.length() && !gameState.getOwnFoundations().filter(API3.Filters.byClass("Storehouse")).length)
else if (!queues.dropsites.hasQueuedUnits() && !gameState.getOwnFoundations().filter(API3.Filters.byClass("Storehouse")).length)
{
if (gameState.ai.playedTurn > this.gatherers[type].nextCheck)
{
@ -481,7 +484,7 @@ m.BaseManager.prototype.checkResourceLevels = function (gameState, queues)
let newDP = this.findBestDropsiteLocation(gameState, type);
if (newDP.quality > 50 && gameState.ai.HQ.canBuild(gameState, "structures/{civ}_storehouse"))
queues.dropsites.addPlan(new m.ConstructionPlan(gameState, "structures/{civ}_storehouse", { "base": this.ID, "type": type }, newDP.pos));
else if (!gameState.getOwnFoundations().filter(API3.Filters.byClass("CivCentre")).length && !queues.civilCentre.length())
else if (!gameState.getOwnFoundations().filter(API3.Filters.byClass("CivCentre")).length && !queues.civilCentre.hasQueuedUnits())
{
// No good dropsite, try to build a new base if no base already planned,
// and if not possible, be less strict on dropsite quality

View File

@ -71,7 +71,7 @@ m.DiplomacyManager.prototype.tributes = function(gameState)
API3.warn("Tribute " + uneval(tribute) + " sent to player " + i);
if (this.Config.chat)
m.chatSentTribute(gameState, i);
Engine.PostCommand(PlayerID, {"type": "tribute", "player": i, "amounts": tribute});
Engine.PostCommand(PlayerID, { "type": "tribute", "player": i, "amounts": tribute });
}
};

View File

@ -617,8 +617,8 @@ m.HQ.prototype.bulkPickWorkers = function(gameState, baseRef, number)
m.HQ.prototype.getTotalResourceLevel = function(gameState)
{
var total = { "food": 0, "wood": 0, "stone": 0, "metal": 0 };
for (var base of this.baseManagers)
for (var type in total)
for (let base of this.baseManagers)
for (let type in total)
total[type] += base.getResourceLevel(gameState, type);
return total;
@ -665,13 +665,12 @@ m.HQ.prototype.pickMostNeededResources = function(gameState)
var currentRates = this.GetCurrentGatherRates(gameState);
var needed = [];
for (var res in this.wantedRates)
for (let res in this.wantedRates)
needed.push({ "type": res, "wanted": this.wantedRates[res], "current": currentRates[res] });
needed.sort(function(a, b) {
var va = (Math.max(0, a.wanted - a.current))/ (a.current+1);
var vb = (Math.max(0, b.wanted - b.current))/ (b.current+1);
needed.sort((a, b) => {
let va = Math.max(0, a.wanted - a.current) / (a.current + 1);
let vb = Math.max(0, b.wanted - b.current) / (b.current + 1);
// If they happen to be equal (generally this means "0" aka no need), make it fair.
if (va === vb)
return (a.current - b.current);
@ -720,7 +719,7 @@ m.HQ.prototype.findEconomicCCLocation = function(gameState, template, resource,
radius = Math.ceil((template.obstructionRadius() + 8) / obstructions.cellSize);
// scale is the typical scale at which we want to find a location for our first base
// look for bigger scale if we start from a ship (access < 2) or from a small island
var cellArea = gameState.getMap().cellSize * gameState.getMap().cellSize;
let cellArea = gameState.getMap().cellSize * gameState.getMap().cellSize;
proxyAccess = gameState.ai.accessibility.getAccessValue(proximity);
if (proxyAccess < 2 || cellArea*gameState.ai.accessibility.regionSize[proxyAccess] < 24000)
scale = 400 * 400;
@ -1378,7 +1377,7 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues)
// checks the status of the territory expansion. If no new economic bases created, build some strategic ones.
m.HQ.prototype.checkBaseExpansion = function(gameState, queues)
{
if (queues.civilCentre.length() > 0)
if (queues.civilCentre.hasQueuedUnits())
return;
// first build one cc if all have been destroyed
let activeBases = this.numActiveBase();
@ -1410,7 +1409,7 @@ m.HQ.prototype.buildNewBase = function(gameState, queues, resource)
{
if (this.numActiveBase() > 0 && gameState.currentPhase() == 1 && !gameState.isResearching(gameState.townPhase()))
return false;
if (gameState.getOwnFoundations().filter(API3.Filters.byClass("CivCentre")).length > 0 || queues.civilCentre.length() > 0)
if (gameState.getOwnFoundations().filter(API3.Filters.byClass("CivCentre")).length > 0 || queues.civilCentre.hasQueuedUnits())
return false;
let template = (this.numActiveBase() > 0) ? this.bBase[0] : gameState.applyCiv("structures/{civ}_civil_centre");
if (!this.canBuild(gameState, template))
@ -1426,7 +1425,7 @@ m.HQ.prototype.buildNewBase = function(gameState, queues, resource)
// Deals with building fortresses and towers along our border with enemies.
m.HQ.prototype.buildDefenses = function(gameState, queues)
{
if (this.saveResources || queues.defenseBuilding.length())
if (this.saveResources || queues.defenseBuilding.hasQueuedUnits())
return;
if (gameState.currentPhase() > 2 || gameState.isResearching(gameState.cityPhase()))
@ -1477,7 +1476,7 @@ m.HQ.prototype.buildDefenses = function(gameState, queues)
m.HQ.prototype.buildBlacksmith = function(gameState, queues)
{
if (gameState.getPopulation() < this.Config.Military.popForBlacksmith ||
queues.militaryBuilding.length() || gameState.getOwnEntitiesByClass("Blacksmith", true).length)
queues.militaryBuilding.hasQueuedUnits() || gameState.getOwnEntitiesByClass("Blacksmith", true).length)
return;
// build a market before the blacksmith
if (!gameState.getOwnEntitiesByClass("BarterMarket", true).length)
@ -1491,7 +1490,7 @@ m.HQ.prototype.buildWonder = function(gameState, queues)
{
if (!this.canBuild(gameState, "structures/{civ}_wonder"))
return;
if (queues.wonder && queues.wonder.length() > 0)
if (queues.wonder && queues.wonder.hasQueuedUnits())
return;
if (gameState.getOwnEntitiesByClass("Wonder", true).length > 0)
return;
@ -1507,7 +1506,7 @@ m.HQ.prototype.buildWonder = function(gameState, queues)
// TODO: building placement is bad. Choice of buildings is also fairly dumb.
m.HQ.prototype.constructTrainingBuildings = function(gameState, queues)
{
if (this.canBuild(gameState, "structures/{civ}_barracks") && queues.militaryBuilding.length() == 0)
if (this.canBuild(gameState, "structures/{civ}_barracks") && !queues.militaryBuilding.hasQueuedUnits())
{
var barrackNb = gameState.getOwnEntitiesByClass("Barracks", true).length;
// first barracks.
@ -1540,7 +1539,7 @@ m.HQ.prototype.constructTrainingBuildings = function(gameState, queues)
}
//build advanced military buildings
if (gameState.currentPhase() > 2 && gameState.getPopulation() > 80 && queues.militaryBuilding.length() == 0 && this.bAdvanced.length != 0)
if (gameState.currentPhase() > 2 && gameState.getPopulation() > 80 && !queues.militaryBuilding.hasQueuedUnits() && this.bAdvanced.length != 0)
{
let nAdvanced = 0;
for (let advanced of this.bAdvanced)
@ -2017,10 +2016,10 @@ m.HQ.prototype.update = function(gameState, queues, events)
if (gameState.ai.playedTurn % 2 == 1)
this.buildMoreHouses(gameState,queues);
if (gameState.ai.playedTurn % 4 == 2 && !this.saveResources)
if (!this.saveResources && gameState.ai.playedTurn % 4 == 2)
this.buildFarmstead(gameState, queues);
if (queues.minorTech.length() == 0 && gameState.ai.playedTurn % 5 == 1)
if (!queues.minorTech.hasQueuedUnits() && gameState.ai.playedTurn % 5 == 1)
this.researchManager.update(gameState, queues);
}

View File

@ -448,7 +448,7 @@ m.NavalManager.prototype.setMinimalTransportShips = function(gameState, sea, num
// bumps up the number of ships we want if we need more.
m.NavalManager.prototype.checkLevels = function(gameState, queues)
{
if (queues.ships.length() !== 0)
if (queues.ships.hasQueuedUnits())
return;
for (let sea = 0; sea < this.neededTransportShips.length; sea++)
@ -477,7 +477,7 @@ m.NavalManager.prototype.checkLevels = function(gameState, queues)
m.NavalManager.prototype.maintainFleet = function(gameState, queues)
{
if (queues.ships.length() > 0)
if (queues.ships.hasQueuedUnits())
return;
if (gameState.getOwnEntitiesByClass("Dock", true).filter(API3.Filters.isBuilt()).length +
gameState.getOwnEntitiesByClass("Shipyard", true).filter(API3.Filters.isBuilt()).length === 0)
@ -608,7 +608,7 @@ m.NavalManager.prototype.buildNavalStructures = function(gameState, queues)
}
if (gameState.currentPhase() < 2 || gameState.getPopulation() < this.Config.Economy.popForTown + 15 ||
queues.militaryBuilding.length() !== 0 || this.bNaval.length === 0)
queues.militaryBuilding.hasQueuedUnits() || this.bNaval.length === 0)
return;
var docks = gameState.getOwnStructures().filter(API3.Filters.byClass("Dock"));
if (!docks.length)

View File

@ -63,7 +63,7 @@ m.QueueManager.prototype.currentNeeds = function(gameState)
for (let q of this.queueArrays)
{
let queue = q[1];
if (!queue.length() || !queue.plans[0].isGo(gameState))
if (!queue.hasQueuedUnits() || !queue.plans[0].isGo(gameState))
continue;
let costs = queue.plans[0].getCost();
needed.add(costs);
@ -157,7 +157,7 @@ m.QueueManager.prototype.printQueues = function(gameState)
{
let qStr = "";
let q = this.queues[i];
if (q.length() > 0)
if (q.hasQueuedUnits())
{
API3.warn(i + ": ( with priority " + this.priorities[i] +" and accounts " + uneval(this.accounts[i]) +")");
API3.warn(" while maxAccountWanted(0.6) is " + uneval(q.maxAccountWanted(gameState, 0.6)));
@ -259,7 +259,7 @@ m.QueueManager.prototype.distributeResources = function(gameState)
{
// returns exactly the correct amount, ie 0 if we're not go.
let queueCost = this.queues[j].maxAccountWanted(gameState, 0.6);
if (this.queues[j].length() > 0 && this.accounts[j][res] < queueCost[res] && !this.queues[j].paused)
if (this.queues[j].hasQueuedUnits() && this.accounts[j][res] < queueCost[res] && !this.queues[j].paused)
{
// adding us to the list of queues that need an update.
tempPrio[j] = this.priorities[j];
@ -316,7 +316,7 @@ m.QueueManager.prototype.switchResource = function(gameState, res)
// TODO: this isn't perfect compression.
for (let j in this.queues)
{
if (this.queues[j].length() === 0 || this.queues[j].paused)
if (!this.queues[j].hasQueuedUnits() || this.queues[j].paused)
continue;
let queue = this.queues[j];
@ -352,7 +352,7 @@ m.QueueManager.prototype.startNextItems = function(gameState)
{
let name = q[0];
let queue = q[1];
if (queue.length() > 0 && !queue.paused)
if (queue.hasQueuedUnits() && !queue.paused)
{
let item = queue.getNext();
if (this.accounts[name].canAfford(item.getCost()) && item.canStart(gameState))
@ -363,7 +363,7 @@ m.QueueManager.prototype.startNextItems = function(gameState)
queue.switched = 0;
}
}
else if (queue.length() === 0)
else if (!queue.hasQueuedUnits())
{
this.accounts[name].reset();
queue.switched = 0;

View File

@ -15,7 +15,7 @@ m.ResearchManager = function(Config)
*/
m.ResearchManager.prototype.checkPhase = function(gameState, queues)
{
if (queues.majorTech.length() !== 0)
if (queues.majorTech.hasQueuedUnits())
return;
var townPhase = gameState.townPhase();
@ -48,7 +48,7 @@ m.ResearchManager.prototype.checkPhase = function(gameState, queues)
m.ResearchManager.prototype.researchPopulationBonus = function(gameState, queues)
{
if (queues.minorTech.length() !== 0)
if (queues.minorTech.hasQueuedUnits())
return;
var techs = gameState.findAvailableTech();
@ -66,7 +66,7 @@ m.ResearchManager.prototype.researchPopulationBonus = function(gameState, queues
m.ResearchManager.prototype.researchTradeBonus = function(gameState, queues)
{
if (queues.minorTech.length() !== 0)
if (queues.minorTech.hasQueuedUnits())
return;
var techs = gameState.findAvailableTech();
@ -161,7 +161,7 @@ m.ResearchManager.prototype.researchPreferredTechs = function(gameState, techs)
m.ResearchManager.prototype.update = function(gameState, queues)
{
if (queues.minorTech.length() || queues.majorTech.length())
if (queues.minorTech.hasQueuedUnits() || queues.majorTech.hasQueuedUnits())
return;
var techs = gameState.findAvailableTech();
@ -187,7 +187,7 @@ m.ResearchManager.prototype.update = function(gameState, queues)
return;
// remove some techs not yet used by this AI
// remove alse sharedLos if we have no ally
// remove also sharedLos if we have no ally
for (let i = 0; i < techs.length; ++i)
{
let template = techs[i][1]._template;

View File

@ -61,13 +61,13 @@ m.HQ.prototype.gameAnalysis = function(gameState)
*/
m.HQ.prototype.assignStartingEntities = function(gameState)
{
for (var ent of gameState.getOwnEntities().values())
for (let ent of gameState.getOwnEntities().values())
{
// do not affect merchant ship immediately to trade as they may-be useful for transport
if (ent.hasClass("Trader") && !ent.hasClass("Ship"))
this.tradeManager.assignTrader(ent);
var pos = ent.position();
let pos = ent.position();
if (!pos)
{
// TODO should support recursive garrisoning. Make a warning for now
@ -94,8 +94,8 @@ m.HQ.prototype.assignStartingEntities = function(gameState)
ent.setMetadata(PlayerID, "access", gameState.ai.accessibility.getAccessValue(pos));
let bestbase;
var territorypos = this.territoryMap.gamePosToMapPos(pos);
var territoryIndex = territorypos[0] + territorypos[1]*this.territoryMap.width;
let territorypos = this.territoryMap.gamePosToMapPos(pos);
let territoryIndex = territorypos[0] + territorypos[1]*this.territoryMap.width;
for (let i = 1; i < this.baseManagers.length; ++i)
{
let base = this.baseManagers[i];
@ -148,7 +148,7 @@ m.HQ.prototype.regionAnalysis = function(gameState)
}
if (!landIndex)
{
var civ = gameState.civ();
let civ = gameState.civ();
for (let ent of gameState.getOwnEntities().values())
{
if (!ent.position() || (!ent.hasClass("Unit") && !ent.trainableEntities(civ)))
@ -364,7 +364,7 @@ m.HQ.prototype.dispatchUnits = function(gameState)
return;
if (ent.getMetadata(PlayerID, "allied"))
return;
var access = gameState.ai.accessibility.getAccessValue(ent.position());
let access = gameState.ai.accessibility.getAccessValue(ent.position());
for (let cc of allycc)
{
if (!cc.position())
@ -385,7 +385,7 @@ m.HQ.prototype.dispatchUnits = function(gameState)
return;
if (ent.getMetadata(PlayerID, "allied"))
return;
var access = gameState.ai.accessibility.getAccessValue(ent.position());
let access = gameState.ai.accessibility.getAccessValue(ent.position());
for (let cc of allycc)
{
if (!cc.position())
@ -438,10 +438,8 @@ m.HQ.prototype.configFirstBase = function(gameState)
for (let land of startingLand)
{
for (let sea of gameState.ai.accessibility.regionLinks[land])
{
if (gameState.ai.HQ.navalRegions[sea])
this.navalManager.updateFishingBoats(sea, num);
}
}
}