0ad/binaries/data/mods/public/simulation/ai/common-api/resources.js
wraitii bd22432fe8 Rename common-api-v3 to common-api.
Fix warnings with the new AI.
Fix an error with the per-player gatherer counts.

This was SVN commit r14559.
2014-01-10 17:46:48 +00:00

82 lines
1.5 KiB
JavaScript

var API3 = function(m)
{
m.Resources = function(amounts, population) {
if (amounts === undefined) {
amounts = {
food : 0,
wood : 0,
stone : 0,
metal : 0
};
}
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] = amounts[t] || 0;
}
if (population > 0) {
this.population = parseInt(population);
} else {
this.population = 0;
}
}
m.Resources.prototype.types = [ "food", "wood", "stone", "metal" ];
m.Resources.prototype.reset = function() {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] = 0;
}
this.population = 0;
};
m.Resources.prototype.canAfford = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
if (this[t] < that[t]) {
return false;
}
}
return true;
};
m.Resources.prototype.add = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] += that[t];
}
this.population += that.population;
};
m.Resources.prototype.subtract = function(that) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] -= that[t];
}
this.population += that.population;
};
m.Resources.prototype.multiply = function(n) {
for ( var tKey in this.types) {
var t = this.types[tKey];
this[t] *= n;
}
this.population *= n;
};
m.Resources.prototype.toInt = function() {
var sum = 0;
for ( var tKey in this.types) {
var t = this.types[tKey];
sum += this[t];
}
sum += this.population * 50; // based on typical unit costs
return sum;
};
return m;
}(API3);