petra: some cleanings and tweaks

This was SVN commit r16589.
This commit is contained in:
mimo 2015-04-27 18:34:24 +00:00
parent 04c4e666de
commit f9a025ba6b
5 changed files with 33 additions and 49 deletions

View File

@ -313,11 +313,11 @@ m.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource)
var obstructions = m.createObstructionMap(gameState, this.accessIndex, template);
var DPFoundations = gameState.getOwnFoundations().filter(API3.Filters.byClass("Storehouse")).toEntityArray();
var ccEnts = gameState.getOwnStructures().filter(API3.Filters.byClass("CivCentre")).toEntityArray();
var dpEnts = gameState.getOwnEntitiesByClass("Storehouse", true).toEntityArray();
var ccEnts = gameState.getOwnEntitiesByClass("CivCentre", true).toEntityArray();
var bestIdx = undefined;
var bestVal = undefined;
var bestVal = 0;
var radius = Math.ceil(template.obstructionRadius() / obstructions.cellSize);
var territoryMap = gameState.ai.HQ.territoryMap;
@ -343,28 +343,12 @@ m.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource)
}
total = 0.7*total; // Just a normalisation factor as the locateMap is limited to 255
var pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)];
for (var id in this.dropsites)
{
if (!gameState.getEntityById(id))
continue;
var dpPos = gameState.getEntityById(id).position();
if (!dpPos)
continue;
var dist = API3.SquareVectorDistance(dpPos, pos);
if (dist < 3600)
{
total = 0;
break;
}
else if (dist < 6400)
total /= 2;
}
if (total == 0)
if (total <= bestVal)
continue;
for (let dp of DPFoundations)
var pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)];
for (let dp of dpEnts)
{
let dpPos = dp.position();
if (!dpPos)
@ -378,15 +362,15 @@ m.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource)
else if (dist < 6400)
total /= 2;
}
if (total == 0)
if (total <= bestVal)
continue;
for (var cc of ccEnts)
for (let cc of ccEnts)
{
var ccPos = cc.position();
let ccPos = cc.position();
if (!ccPos)
continue;
var dist = API3.SquareVectorDistance(ccPos, pos);
let dist = API3.SquareVectorDistance(ccPos, pos);
if (dist < 3600)
{
total = 0;
@ -395,11 +379,9 @@ m.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource)
else if (dist < 6400)
total /= 2;
}
if (total == 0)
if (total <= bestVal)
continue;
if (bestVal !== undefined && total < bestVal)
continue;
bestVal = total;
bestIdx = i;
}

View File

@ -373,13 +373,16 @@ m.DefenseManager.prototype.garrisonRangedUnitsInside = function(gameState, targe
return;
if (target.hitpoints() < target.garrisonEjectHealth() * target.maxHitpoints())
return;
var attackTypes = target.attackTypes();
if (!attackTypes || attackTypes.indexOf("Ranged") === -1)
return;
var dist = API3.SquareVectorDistance(attacker.position(), target.position());
var range = target.attackRange("Ranged").max;
if (dist >= range*range)
return;
if (attacker)
{
let attackTypes = target.attackTypes();
if (!attackTypes || attackTypes.indexOf("Ranged") === -1)
return;
let dist = API3.SquareVectorDistance(attacker.position(), target.position());
let range = target.attackRange("Ranged").max;
if (dist >= range*range)
return;
}
var index = gameState.ai.accessibility.getAccessValue(target.position());
var garrisonManager = gameState.ai.HQ.garrisonManager;
var garrisonArrowClasses = target.getGarrisonArrowClasses();

View File

@ -22,11 +22,12 @@ m.GarrisonManager.prototype.update = function(gameState, queues)
continue;
var holder = gameState.getEntityById(id);
if (!holder) // this holder was certainly destroyed. Let's remove it
if (!holder || holder.owner() !== PlayerID)
{
for (var entId of this.holders[id])
// this holder was certainly destroyed or captured. Let's remove it
for (let entId of this.holders[id])
{
var ent = gameState.getEntityById(entId);
let ent = gameState.getEntityById(entId);
if (ent && ent.getMetadata(PlayerID, "garrisonHolder") == +id)
{
this.leaveGarrison(ent);

View File

@ -1217,7 +1217,8 @@ m.HQ.prototype.checkBaseExpansion = function(gameState, queues)
if (queues.civilCentre.length() > 0)
return;
// first build one cc if all have been destroyed
if (this.numActiveBase() < 1)
let activeBases = this.numActiveBase();
if (activeBases == 0)
{
this.buildFirstBase(gameState);
return;
@ -1231,14 +1232,11 @@ m.HQ.prototype.checkBaseExpansion = function(gameState, queues)
return;
}
// then expand if we have lots of units
var numUnits = gameState.getOwnUnits().length;
var popForBase = this.Config.Economy.popForTown + 20;
if (this.saveResources)
popForBase = this.Config.Economy.popForTown + 5;
if (Math.floor(numUnits/popForBase) >= gameState.getOwnStructures().filter(API3.Filters.byClass("CivCentre")).length)
let numUnits = gameState.getOwnUnits().length;
if (numUnits > activeBases * (70 + 15*(activeBases-1)) || (this.saveResources && numUnits > 50))
{
if (this.Config.debug > 2)
API3.warn("try to build a new base because of population " + numUnits + " for " + this.numActiveBase() + " CCs");
API3.warn("try to build a new base because of population " + numUnits + " for " + activeBases + " CCs");
this.buildNewBase(gameState, queues);
}
};

View File

@ -170,8 +170,8 @@ m.createFrontierMap = function(gameState)
var map = new API3.Map(gameState.sharedScript, "territory");
var width = map.width;
var insideSmall = Math.round(40 / map.cellSize);
var insideLarge = Math.round(60 / map.cellSize);
var insideSmall = Math.round(45 / map.cellSize);
var insideLarge = Math.round(80 / map.cellSize); // should be about the range of towers
for (var j = 0; j < territoryMap.length; ++j)
{