0ad/binaries/data/mods/public/simulation/ai/aegis/queue.js
wraitii d23b7deb98 Various improvements to AI's early game. Simplify some code and improve on other. Add a few different strategies (Rush/normal/boom, quite basic for now, refs #2344).
Aegis should mostly respect tech limitations so I'll ref #1964.
I'm going to go with #2364 is fixed, the AI should be more efficient in
early-game, and late-game is a known problem.
Fixes #2274 and fixes #2379.
Refs #2372 as it should fix several of those warnings for AIs.
Fixes #2256 with a new bartering system, in parts taken from mimo's
patch.

This was SVN commit r14582.
2014-01-14 19:54:31 +00:00

112 lines
2.4 KiB
JavaScript

var AEGIS = function(m)
{
/*
* Holds a list of wanted items to train or construct
*/
m.Queue = function() {
this.queue = [];
this.paused = false;
this.switched = 0;
};
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.4);
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);