2013-08-17 15:59:53 +02:00
|
|
|
/*
|
|
|
|
* Holds a list of wanted items to train or construct
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Queue = function() {
|
|
|
|
this.queue = [];
|
2013-09-29 15:32:52 +02:00
|
|
|
this.paused = false;
|
2013-08-17 15:59:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Queue.prototype.empty = function() {
|
|
|
|
this.queue = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
Queue.prototype.getNext = function() {
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
return this.queue[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-29 15:32:52 +02:00
|
|
|
Queue.prototype.startNext = function(gameState) {
|
|
|
|
if (this.queue.length > 0) {
|
|
|
|
this.queue.shift().start(gameState);
|
|
|
|
return true;
|
2013-08-17 15:59:53 +02:00
|
|
|
} else {
|
2013-09-29 15:32:52 +02:00
|
|
|
return false;
|
2013-08-17 15:59:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-29 15:32:52 +02:00
|
|
|
// 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
|
|
|
|
Queue.prototype.maxAccountWanted = function(gameState) {
|
2013-08-17 15:59:53 +02:00
|
|
|
var cost = new Resources();
|
2013-09-29 15:32:52 +02:00
|
|
|
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);
|
2013-08-17 15:59:53 +02:00
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
};
|
|
|
|
|
|
|
|
Queue.prototype.queueCost = function(){
|
|
|
|
var cost = new Resources();
|
|
|
|
for (var key in this.queue){
|
|
|
|
cost.add(this.queue[key].getCost());
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
};
|
|
|
|
|
|
|
|
Queue.prototype.length = function() {
|
|
|
|
return this.queue.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
Queue.prototype.countQueuedUnits = function(){
|
|
|
|
var count = 0;
|
|
|
|
for (var i in this.queue){
|
|
|
|
count += this.queue[i].number;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
|
2013-09-29 15:32:52 +02:00
|
|
|
Queue.prototype.countQueuedUnitsWithClass = function(classe){
|
2013-08-17 15:59:53 +02:00
|
|
|
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;
|
|
|
|
};
|
2013-09-29 15:32:52 +02:00
|
|
|
Queue.prototype.countQueuedUnitsWithMetadata = function(data,value){
|
2013-08-17 15:59:53 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2013-09-29 15:32:52 +02:00
|
|
|
};
|