2014-03-24 23:33:50 +01:00
|
|
|
var PETRA = function(m)
|
|
|
|
{
|
|
|
|
|
|
|
|
m.ResearchPlan = function(gameState, type, rush)
|
|
|
|
{
|
|
|
|
if (!m.QueuePlan.call(this, gameState, type, {}))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (this.template.researchTime === undefined)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this.category = "technology";
|
|
|
|
|
|
|
|
this.rush = rush ? true : false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m.ResearchPlan.prototype = Object.create(m.QueuePlan.prototype);
|
|
|
|
|
|
|
|
m.ResearchPlan.prototype.canStart = function(gameState)
|
|
|
|
{
|
|
|
|
// also checks canResearch
|
|
|
|
return (gameState.findResearchers(this.type).length !== 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
m.ResearchPlan.prototype.isInvalid = function(gameState)
|
|
|
|
{
|
|
|
|
return (gameState.isResearched(this.type) || gameState.isResearching(this.type));
|
|
|
|
};
|
|
|
|
|
|
|
|
m.ResearchPlan.prototype.start = function(gameState)
|
|
|
|
{
|
|
|
|
var trainers = gameState.findResearchers(this.type).toEntityArray();
|
|
|
|
|
|
|
|
// Prefer training buildings with short queues
|
|
|
|
// (TODO: this should also account for units added to the queue by
|
|
|
|
// plans that have already been executed this turn)
|
2014-05-18 13:34:36 +02:00
|
|
|
if (trainers.length > 0)
|
|
|
|
{
|
2014-03-24 23:33:50 +01:00
|
|
|
trainers.sort(function(a, b) {
|
|
|
|
return (a.trainingQueueTime() - b.trainingQueueTime());
|
|
|
|
});
|
|
|
|
// drop anything in the queue if we rush it.
|
|
|
|
if (this.rush)
|
|
|
|
trainers[0].stopAllProduction(0.45);
|
|
|
|
trainers[0].research(this.type);
|
|
|
|
}
|
|
|
|
this.onStart(gameState);
|
|
|
|
};
|
|
|
|
|
2014-11-10 10:03:03 +01:00
|
|
|
m.ResearchPlan.prototype.Serialize = function()
|
|
|
|
{
|
2014-11-30 20:05:39 +01:00
|
|
|
let prop = {
|
|
|
|
"category": this.category,
|
2014-11-10 10:03:03 +01:00
|
|
|
"type": this.type,
|
|
|
|
"ID": this.ID,
|
2014-11-30 20:05:39 +01:00
|
|
|
"metadata": this.metadata,
|
2014-11-10 10:03:03 +01:00
|
|
|
"cost": this.cost.Serialize(),
|
|
|
|
"number": this.number,
|
2014-11-30 20:05:39 +01:00
|
|
|
"rush": this.rush,
|
2014-11-10 10:03:03 +01:00
|
|
|
"lastIsGo": this.lastIsGo,
|
|
|
|
};
|
2014-11-30 20:05:39 +01:00
|
|
|
|
|
|
|
let func = {
|
|
|
|
"isGo": uneval(this.isGo),
|
|
|
|
"onGo": uneval(this.onGo),
|
|
|
|
"onNotGo": uneval(this.onNotGo),
|
|
|
|
"onStart": uneval(this.onStart)
|
|
|
|
};
|
|
|
|
|
|
|
|
return { "prop": prop, "func": func };
|
2014-11-10 10:03:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
m.ResearchPlan.prototype.Deserialize = function(gameState, data)
|
|
|
|
{
|
2014-11-30 20:05:39 +01:00
|
|
|
for (let key in data.prop)
|
|
|
|
this[key] = data.prop[key];
|
2014-11-10 10:03:03 +01:00
|
|
|
|
|
|
|
let cost = new API3.Resources();
|
2014-11-30 20:05:39 +01:00
|
|
|
cost.Deserialize(data.prop.cost);
|
2014-11-10 10:03:03 +01:00
|
|
|
this.cost = cost;
|
2014-11-10 19:35:52 +01:00
|
|
|
|
2014-11-30 20:05:39 +01:00
|
|
|
for (let fun in data.func)
|
|
|
|
this[fun] = eval(data.func[fun]);
|
2014-11-10 10:03:03 +01:00
|
|
|
};
|
|
|
|
|
2014-03-24 23:33:50 +01:00
|
|
|
return m;
|
|
|
|
}(PETRA);
|