Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/queue.js
var PETRA = function(m) | |||||
{ | |||||
/** | /** | ||||
* Holds a list of wanted plans to train or construct | * Holds a list of wanted plans to train or construct | ||||
*/ | */ | ||||
PETRA.Queue = function() | |||||
m.Queue = function() | |||||
{ | { | ||||
this.plans = []; | this.plans = []; | ||||
this.paused = false; | this.paused = false; | ||||
this.switched = 0; | this.switched = 0; | ||||
}; | }; | ||||
m.Queue.prototype.empty = function() | PETRA.Queue.prototype.empty = function() | ||||
{ | { | ||||
this.plans = []; | this.plans = []; | ||||
}; | }; | ||||
m.Queue.prototype.addPlan = function(newPlan) | PETRA.Queue.prototype.addPlan = function(newPlan) | ||||
{ | { | ||||
if (!newPlan) | if (!newPlan) | ||||
return; | return; | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
{ | { | ||||
if (newPlan.category === "unit" && plan.type == newPlan.type && plan.number + newPlan.number <= plan.maxMerge) | if (newPlan.category === "unit" && plan.type == newPlan.type && plan.number + newPlan.number <= plan.maxMerge) | ||||
{ | { | ||||
plan.addItem(newPlan.number); | plan.addItem(newPlan.number); | ||||
return; | return; | ||||
} | } | ||||
else if (newPlan.category === "technology" && plan.type === newPlan.type) | else if (newPlan.category === "technology" && plan.type === newPlan.type) | ||||
return; | return; | ||||
} | } | ||||
this.plans.push(newPlan); | this.plans.push(newPlan); | ||||
}; | }; | ||||
m.Queue.prototype.check= function(gameState) | PETRA.Queue.prototype.check= function(gameState) | ||||
{ | { | ||||
while (this.plans.length > 0) | while (this.plans.length > 0) | ||||
{ | { | ||||
if (!this.plans[0].isInvalid(gameState)) | if (!this.plans[0].isInvalid(gameState)) | ||||
return; | return; | ||||
let plan = this.plans.shift(); | let plan = this.plans.shift(); | ||||
if (plan.queueToReset) | if (plan.queueToReset) | ||||
gameState.ai.queueManager.changePriority(plan.queueToReset, gameState.ai.Config.priorities[plan.queueToReset]); | gameState.ai.queueManager.changePriority(plan.queueToReset, gameState.ai.Config.priorities[plan.queueToReset]); | ||||
} | } | ||||
}; | }; | ||||
m.Queue.prototype.getNext = function() | PETRA.Queue.prototype.getNext = function() | ||||
{ | { | ||||
if (this.plans.length > 0) | if (this.plans.length > 0) | ||||
return this.plans[0]; | return this.plans[0]; | ||||
return null; | return null; | ||||
}; | }; | ||||
m.Queue.prototype.startNext = function(gameState) | PETRA.Queue.prototype.startNext = function(gameState) | ||||
{ | { | ||||
if (this.plans.length > 0) | if (this.plans.length > 0) | ||||
{ | { | ||||
this.plans.shift().start(gameState); | this.plans.shift().start(gameState); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}; | }; | ||||
/** | /** | ||||
* returns the maximal account we'll accept for this queue. | * returns the maximal account we'll accept for this queue. | ||||
* Currently all the cost of the first element and fraction of that of the second | * Currently all the cost of the first element and fraction of that of the second | ||||
*/ | */ | ||||
m.Queue.prototype.maxAccountWanted = function(gameState, fraction) | PETRA.Queue.prototype.maxAccountWanted = function(gameState, fraction) | ||||
{ | { | ||||
let cost = new API3.Resources(); | let cost = new API3.Resources(); | ||||
if (this.plans.length > 0 && this.plans[0].isGo(gameState)) | if (this.plans.length > 0 && this.plans[0].isGo(gameState)) | ||||
cost.add(this.plans[0].getCost()); | cost.add(this.plans[0].getCost()); | ||||
if (this.plans.length > 1 && this.plans[1].isGo(gameState) && fraction > 0) | if (this.plans.length > 1 && this.plans[1].isGo(gameState) && fraction > 0) | ||||
{ | { | ||||
let costs = this.plans[1].getCost(); | let costs = this.plans[1].getCost(); | ||||
costs.multiply(fraction); | costs.multiply(fraction); | ||||
cost.add(costs); | cost.add(costs); | ||||
} | } | ||||
return cost; | return cost; | ||||
}; | }; | ||||
m.Queue.prototype.queueCost = function() | PETRA.Queue.prototype.queueCost = function() | ||||
{ | { | ||||
let cost = new API3.Resources(); | let cost = new API3.Resources(); | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
cost.add(plan.getCost()); | cost.add(plan.getCost()); | ||||
return cost; | return cost; | ||||
}; | }; | ||||
m.Queue.prototype.length = function() | PETRA.Queue.prototype.length = function() | ||||
{ | { | ||||
return this.plans.length; | return this.plans.length; | ||||
}; | }; | ||||
m.Queue.prototype.hasQueuedUnits = function() | PETRA.Queue.prototype.hasQueuedUnits = function() | ||||
{ | { | ||||
return this.plans.length > 0; | return this.plans.length > 0; | ||||
}; | }; | ||||
m.Queue.prototype.countQueuedUnits = function() | PETRA.Queue.prototype.countQueuedUnits = function() | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
count += plan.number; | count += plan.number; | ||||
return count; | return count; | ||||
}; | }; | ||||
m.Queue.prototype.hasQueuedUnitsWithClass = function(classe) | PETRA.Queue.prototype.hasQueuedUnitsWithClass = function(classe) | ||||
{ | { | ||||
return this.plans.some(plan => plan.template && plan.template.hasClass(classe)); | return this.plans.some(plan => plan.template && plan.template.hasClass(classe)); | ||||
}; | }; | ||||
m.Queue.prototype.countQueuedUnitsWithClass = function(classe) | PETRA.Queue.prototype.countQueuedUnitsWithClass = function(classe) | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
if (plan.template && plan.template.hasClass(classe)) | if (plan.template && plan.template.hasClass(classe)) | ||||
count += plan.number; | count += plan.number; | ||||
return count; | return count; | ||||
}; | }; | ||||
m.Queue.prototype.countQueuedUnitsWithMetadata = function(data, value) | PETRA.Queue.prototype.countQueuedUnitsWithMetadata = function(data, value) | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
if (plan.metadata[data] && plan.metadata[data] == value) | if (plan.metadata[data] && plan.metadata[data] == value) | ||||
count += plan.number; | count += plan.number; | ||||
return count; | return count; | ||||
}; | }; | ||||
m.Queue.prototype.Serialize = function() | PETRA.Queue.prototype.Serialize = function() | ||||
{ | { | ||||
let plans = []; | let plans = []; | ||||
for (let plan of this.plans) | for (let plan of this.plans) | ||||
plans.push(plan.Serialize()); | plans.push(plan.Serialize()); | ||||
return { "plans": plans, "paused": this.paused, "switched": this.switched }; | return { "plans": plans, "paused": this.paused, "switched": this.switched }; | ||||
}; | }; | ||||
m.Queue.prototype.Deserialize = function(gameState, data) | PETRA.Queue.prototype.Deserialize = function(gameState, data) | ||||
{ | { | ||||
this.paused = data.paused; | this.paused = data.paused; | ||||
this.switched = data.switched; | this.switched = data.switched; | ||||
this.plans = []; | this.plans = []; | ||||
for (let dataPlan of data.plans) | for (let dataPlan of data.plans) | ||||
{ | { | ||||
let plan; | let plan; | ||||
if (dataPlan.category == "unit") | if (dataPlan.category == "unit") | ||||
plan = new m.TrainingPlan(gameState, dataPlan.type); | plan = new PETRA.TrainingPlan(gameState, dataPlan.type); | ||||
else if (dataPlan.category == "building") | else if (dataPlan.category == "building") | ||||
plan = new m.ConstructionPlan(gameState, dataPlan.type); | plan = new PETRA.ConstructionPlan(gameState, dataPlan.type); | ||||
else if (dataPlan.category == "technology") | else if (dataPlan.category == "technology") | ||||
plan = new m.ResearchPlan(gameState, dataPlan.type); | plan = new PETRA.ResearchPlan(gameState, dataPlan.type); | ||||
else | else | ||||
{ | { | ||||
API3.warn("Petra deserialization error: plan unknown " + uneval(dataPlan)); | API3.warn("Petra deserialization error: plan unknown " + uneval(dataPlan)); | ||||
continue; | continue; | ||||
} | } | ||||
plan.Deserialize(gameState, dataPlan); | plan.Deserialize(gameState, dataPlan); | ||||
this.plans.push(plan); | this.plans.push(plan); | ||||
} | } | ||||
}; | }; | ||||
return m; | |||||
}(PETRA); |
Wildfire Games · Phabricator