petra: fix choice of target when enemy resigned + some cleanings

This was SVN commit r16047.
This commit is contained in:
mimo
2014-12-13 16:47:37 +00:00
parent add113a2a1
commit 639a174388
8 changed files with 54 additions and 76 deletions
@@ -60,24 +60,6 @@ m.AssocArraytoArray = function(assocArray) {
return endArray;
};
m.MemoizeInit = function(obj)
{
obj._memoizeCache = {};
}
m.Memoize = function(funcname, func)
{
return function() {
var args = funcname + '|' + Array.prototype.join.call(arguments, '|');
if (args in this._memoizeCache)
return this._memoizeCache[args];
var ret = func.apply(this, arguments);
this._memoizeCache[args] = ret;
return ret;
};
}
m.ShallowClone = function(obj)
{
var ret = {};
@@ -1513,8 +1513,8 @@ m.AttackPlan.prototype.update = function(gameState, events)
this.unitCollUpdateArray.splice(0, lgth);
this.startingAttack = false;
// check if this enemy is defeated
if (this.target && this.target.owner() == 0 && this.targetPlayer != 0)
// check if this enemy has resigned
if (this.target && this.target.owner() === 0 && this.targetPlayer !== 0)
this.target = undefined;
// updating targets.
@@ -1528,11 +1528,11 @@ m.AttackPlan.prototype.update = function(gameState, events)
// Check if we could help any current attack
var attackManager = gameState.ai.HQ.attackManager;
var accessIndex = gameState.ai.accessibility.getAccessValue(this.targetPos);
for (var attackType in attackManager.startedAttacks)
for (let attackType in attackManager.startedAttacks)
{
if (this.target)
break;
for (var attack of attackManager.startedAttacks[attackType])
for (let attack of attackManager.startedAttacks[attackType])
{
if (attack.name === this.name)
continue;
@@ -1540,6 +1540,8 @@ m.AttackPlan.prototype.update = function(gameState, events)
continue;
if (!attack.target || !gameState.getEntityById(attack.target.id()))
continue;
if (attack.target.owner() === 0 && attack.targetPlayer !== 0) // looks like it has resigned
continue;
this.target = attack.target;
this.targetPlayer = attack.targetPlayer;
break;
@@ -21,7 +21,7 @@ m.chatSentTribute = function(gameState, player)
if (proba < 0.5)
var message = "/team " + markForTranslation("Here is a gift for %(name)s, make a good use of it.");
else
var message = "/team " + markForTranslation("I see you are in a bad situation %(name)s, hope this will help.");
var message = "/team " + markForTranslation("I see you are in a bad situation %(name)s, I hope this will help.");
var chat = { "type": "aichat", "message": message, "translateMessage": true, "translateParameters": ["name"], "parameters": { "name": name } };
Engine.PostCommand(PlayerID, chat);
@@ -56,7 +56,7 @@ m.Config = function(difficulty)
"athen" : [ "structures/{civ}_gymnasion", "structures/{civ}_prytaneion", "structures/{civ}_theatron" ],
"brit" : [ "structures/{civ}_rotarymill" ],
"cart" : [ "structures/{civ}_embassy_celtic",
"structures/{civ}_embassy_iberian", "structures/{civ}_embassy_italiote" ],
"structures/{civ}_embassy_iberian", "structures/{civ}_embassy_italiote" ],
"gaul" : [ "structures/{civ}_tavern" ],
"hele" : [ "structures/{civ}_gymnasion", "structures/{civ}_prytaneion", "structures/{civ}_theatron" ],
"iber" : [ "structures/{civ}_monument" ],
@@ -8,12 +8,11 @@ m.getMaxStrength = function(ent, againstClass)
var attackTypes = ent.attackTypes();
var armourStrength = ent.armourStrengths();
var hp = ent.maxHitpoints() / 100.0; // some normalization
for (var typeKey in attackTypes) {
var type = attackTypes[typeKey];
for (let type of attackTypes)
{
if (type == "Slaughter" || type == "Charged")
continue;
var attackStrength = ent.attackStrengths(type);
var attackRange = ent.attackRange(type);
var attackTimes = ent.attackTimes(type);
@@ -797,7 +797,7 @@ m.HQ.prototype.findEconomicCCLocation = function(gameState, template, resource,
{
if (this.territoryMap.getOwnerIndex(j) != 0 || this.borderMap.map[j] == 2)
continue;
// We require that it is accessible from our starting position
// We require that it is accessible
var index = gameState.ai.accessibility.landPassMap[j];
if (!this.allowedRegions[index])
continue;
@@ -948,7 +948,7 @@ m.HQ.prototype.findStrategicCCLocation = function(gameState, template)
{
if (this.territoryMap.getOwnerIndex(j) != 0 || this.borderMap.map[j] == 2)
continue;
// We require that it is accessible from our starting position
// We require that it is accessible
var index = gameState.ai.accessibility.landPassMap[j];
if (!this.allowedRegions[index])
continue;
@@ -30,14 +30,14 @@ m.QueueManager = function(Config, queues)
this.accounts = {};
// the sorting is updated on priority change.
var self = this;
this.queueArrays = [];
for (var p in this.queues)
{
this.accounts[p] = new API3.Resources();
this.queueArrays.push([p,this.queues[p]]);
this.queueArrays.push([p, this.queues[p]]);
}
this.queueArrays.sort(function (a,b) { return (self.priorities[b[0]] - self.priorities[a[0]]) });
var priorities = this.priorities;
this.queueArrays.sort(function (a,b) { return (priorities[b[0]] - priorities[a[0]]) });
};
m.QueueManager.prototype.getAvailableResources = function(gameState)
@@ -524,34 +524,34 @@ m.QueueManager.prototype.unpauseAll = function(but)
m.QueueManager.prototype.addQueue = function(queueName, priority)
{
if (this.queues[queueName] == undefined)
{
this.queues[queueName] = new m.Queue();
this.priorities[queueName] = priority;
this.accounts[queueName] = new API3.Resources();
if (this.queues[queueName] !== undefined)
return;
var self = this;
this.queueArrays = [];
for (var p in this.queues)
this.queueArrays.push([p,this.queues[p]]);
this.queueArrays.sort(function (a,b) { return (self.priorities[b[0]] - self.priorities[a[0]]) });
}
this.queues[queueName] = new m.Queue();
this.priorities[queueName] = priority;
this.accounts[queueName] = new API3.Resources();
this.queueArrays = [];
for (var p in this.queues)
this.queueArrays.push([p, this.queues[p]]);
var priorities = this.priorities;
this.queueArrays.sort(function (a,b) { return (priorities[b[0]] - priorities[a[0]]) });
};
m.QueueManager.prototype.removeQueue = function(queueName)
{
if (this.queues[queueName] !== undefined)
{
delete this.queues[queueName];
delete this.priorities[queueName];
delete this.accounts[queueName];
if (this.queues[queueName] === undefined)
return;
delete this.queues[queueName];
delete this.priorities[queueName];
delete this.accounts[queueName];
var self = this;
this.queueArrays = [];
for (var p in this.queues)
this.queueArrays.push([p,this.queues[p]]);
this.queueArrays.sort(function (a,b) { return (self.priorities[b[0]] - self.priorities[a[0]]) });
}
this.queueArrays = [];
for (var p in this.queues)
this.queueArrays.push([p, this.queues[p]]);
var priorities = this.priorities;
this.queueArrays.sort(function (a,b) { return (priorities[b[0]] - priorities[a[0]]) });
};
m.QueueManager.prototype.getPriority = function(queueName)
@@ -563,13 +563,10 @@ m.QueueManager.prototype.changePriority = function(queueName, newPriority)
{
if (this.Config.debug > 1)
API3.warn(">>> Priority of queue " + queueName + " changed from " + this.priorities[queueName] + " to " + newPriority);
var self = this;
if (this.queues[queueName] !== undefined)
this.priorities[queueName] = newPriority;
this.queueArrays = [];
for (var p in this.queues)
this.queueArrays.push([p,this.queues[p]]);
this.queueArrays.sort(function (a,b) { return (self.priorities[b[0]] - self.priorities[a[0]]) });
var priorities = this.priorities;
this.queueArrays.sort(function (a,b) { return (priorities[b[0]] - priorities[a[0]]) });
};
m.QueueManager.prototype.Serialize = function()
@@ -728,32 +728,30 @@ m.Worker.prototype.gatherNearestField = function(gameState, baseID)
*/
m.Worker.prototype.buildAnyField = function(gameState, baseID)
{
var self = this;
var baseFoundations = gameState.getOwnFoundations().filter(API3.Filters.byMetadata(PlayerID, "base", baseID));
var baseFoundations = gameState.getOwnFoundations().filter(API3.Filters.byMetadata(PlayerID, "base", baseID));
var maxGatherers = gameState.getTemplate(gameState.applyCiv("structures/{civ}_field")).maxGatherers();
var bestFarmEnt = false;
var bestFarmDist = 10000000;
var pos = this.ent.position();
baseFoundations.forEach(function (found) {
if (found.hasClass("Field")) {
var current = found.getBuildersNb();
if (current === undefined || current >= maxGatherers)
return;
var dist = API3.SquareVectorDistance(found.position(), self.ent.position());
if (dist < bestFarmDist)
{
bestFarmEnt = found;
bestFarmDist = dist;
}
}
if (!found.hasClass("Field"))
return;
var current = found.getBuildersNb();
if (current === undefined || current >= maxGatherers)
return;
var dist = API3.SquareVectorDistance(found.position(), pos);
if (dist > bestFarmDist)
return;
bestFarmEnt = found;
bestFarmDist = dist;
});
return bestFarmEnt;
};
// Workers elephant should move away from the buildings they've built to avoid being trapped in between constructions
// For the time being, we move towards the nearest gatherer (providing him a dropsite)
m.Worker.prototype.moveAway = function(gameState){
m.Worker.prototype.moveAway = function(gameState)
{
var gatherers = this.baseManager.workersBySubrole(gameState, "gatherer");
var pos = this.ent.position();
var dist = Math.min();