0ad/binaries/data/mods/public/simulation/ai/aegis/queue.js
Yves 3362c591f5 Moves AI players to one global using the module pattern.
This avoids wrapping overhead that would otherwise be required because
multiple globals per compartment aren't supported anymore in newer
versions of SpiderMonkey.

Check the ticket for a detailed explanation.

Closes #2322
Refs #2241
Refs #1886

This was SVN commit r14441.
2013-12-30 10:04:59 +00:00

111 lines
2.3 KiB
JavaScript

var AEGIS = function(m)
{
/*
* Holds a list of wanted items to train or construct
*/
m.Queue = function() {
this.queue = [];
this.paused = false;
};
m.Queue.prototype.empty = function() {
this.queue = [];
};
m.Queue.prototype.addItem = function(plan) {
for (var i in this.queue)
{
if (plan.category === "unit" && this.queue[i].type == plan.type && this.queue[i].number + plan.number <= this.queue[i].maxMerge)
{
this.queue[i].addItem(plan.number)
return;
}
}
this.queue.push(plan);
};
m.Queue.prototype.getNext = function() {
if (this.queue.length > 0) {
return this.queue[0];
} else {
return null;
}
};
m.Queue.prototype.startNext = function(gameState) {
if (this.queue.length > 0) {
this.queue.shift().start(gameState);
return true;
} else {
return false;
}
};
// returns the maximal account we'll accept for this queue.
// Currently 100% of the cost of the first element and 80% of that of the second
m.Queue.prototype.maxAccountWanted = function(gameState) {
var cost = new API3.Resources();
if (this.queue.length > 0 && this.queue[0].isGo(gameState))
cost.add(this.queue[0].getCost());
if (this.queue.length > 1 && this.queue[1].isGo(gameState))
{
var costs = this.queue[1].getCost();
costs.multiply(0.8);
cost.add(costs);
}
return cost;
};
m.Queue.prototype.queueCost = function(){
var cost = new API3.Resources();
for (var key in this.queue){
cost.add(this.queue[key].getCost());
}
return cost;
};
m.Queue.prototype.length = function() {
return this.queue.length;
};
m.Queue.prototype.countQueuedUnits = function(){
var count = 0;
for (var i in this.queue){
count += this.queue[i].number;
}
return count;
};
m.Queue.prototype.countQueuedUnitsWithClass = function(classe){
var count = 0;
for (var i in this.queue){
if (this.queue[i].template && this.queue[i].template.hasClass(classe))
count += this.queue[i].number;
}
return count;
};
m.Queue.prototype.countQueuedUnitsWithMetadata = function(data,value){
var count = 0;
for (var i in this.queue){
if (this.queue[i].metadata[data] && this.queue[i].metadata[data] == value)
count += this.queue[i].number;
}
return count;
};
m.Queue.prototype.countAllByType = function(t){
var count = 0;
for (var i = 0; i < this.queue.length; i++){
if (this.queue[i].type === t){
count += this.queue[i].number;
}
}
return count;
};
return m;
}(AEGIS);