Differential D2103 Diff 9534 ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js
var PETRA = function(m) | PETRA.TrainingPlan = function(gameState, type, metadata, number = 1, maxMerge = 5) | ||||
{ | { | ||||
if (!PETRA.QueuePlan.call(this, gameState, type, metadata)) | |||||
m.TrainingPlan = function(gameState, type, metadata, number = 1, maxMerge = 5) | |||||
{ | |||||
if (!m.QueuePlan.call(this, gameState, type, metadata)) | |||||
{ | { | ||||
API3.warn(" Plan training " + type + " canceled"); | API3.warn(" Plan training " + type + " canceled"); | ||||
return false; | return false; | ||||
} | } | ||||
// Refine the estimated cost and add pop cost | // Refine the estimated cost and add pop cost | ||||
let trainers = this.getBestTrainers(gameState); | let trainers = this.getBestTrainers(gameState); | ||||
let trainer = trainers ? trainers[0] : undefined; | let trainer = trainers ? trainers[0] : undefined; | ||||
this.cost = new API3.Resources(this.template.cost(trainer), +this.template._template.Cost.Population); | this.cost = new API3.Resources(this.template.cost(trainer), +this.template._template.Cost.Population); | ||||
this.category = "unit"; | this.category = "unit"; | ||||
this.number = number; | this.number = number; | ||||
this.maxMerge = maxMerge; | this.maxMerge = maxMerge; | ||||
return true; | return true; | ||||
}; | }; | ||||
m.TrainingPlan.prototype = Object.create(m.QueuePlan.prototype); | PETRA.TrainingPlan.prototype = Object.create(PETRA.QueuePlan.prototype); | ||||
m.TrainingPlan.prototype.canStart = function(gameState) | PETRA.TrainingPlan.prototype.canStart = function(gameState) | ||||
{ | { | ||||
this.trainers = this.getBestTrainers(gameState); | this.trainers = this.getBestTrainers(gameState); | ||||
if (!this.trainers) | if (!this.trainers) | ||||
return false; | return false; | ||||
this.cost = new API3.Resources(this.template.cost(this.trainers[0]), +this.template._template.Cost.Population); | this.cost = new API3.Resources(this.template.cost(this.trainers[0]), +this.template._template.Cost.Population); | ||||
return true; | return true; | ||||
}; | }; | ||||
m.TrainingPlan.prototype.getBestTrainers = function(gameState) | PETRA.TrainingPlan.prototype.getBestTrainers = function(gameState) | ||||
{ | { | ||||
if (this.metadata && this.metadata.trainer) | if (this.metadata && this.metadata.trainer) | ||||
{ | { | ||||
let trainer = gameState.getEntityById(this.metadata.trainer); | let trainer = gameState.getEntityById(this.metadata.trainer); | ||||
if (trainer) | if (trainer) | ||||
return [trainer]; | return [trainer]; | ||||
} | } | ||||
Show All 17 Lines | for (let ent of allTrainers.values()) | ||||
{ | { | ||||
costMin = cost; | costMin = cost; | ||||
trainers = [ent]; | trainers = [ent]; | ||||
} | } | ||||
} | } | ||||
return trainers; | return trainers; | ||||
}; | }; | ||||
m.TrainingPlan.prototype.start = function(gameState) | PETRA.TrainingPlan.prototype.start = function(gameState) | ||||
{ | { | ||||
if (this.metadata && this.metadata.trainer) | if (this.metadata && this.metadata.trainer) | ||||
{ | { | ||||
let metadata = {}; | let metadata = {}; | ||||
for (let key in this.metadata) | for (let key in this.metadata) | ||||
if (key !== "trainer") | if (key !== "trainer") | ||||
metadata[key] = this.metadata[key]; | metadata[key] = this.metadata[key]; | ||||
this.metadata = metadata; | this.metadata = metadata; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | PETRA.TrainingPlan.prototype.start = function(gameState) | ||||
if (this.metadata && this.metadata.base !== undefined && this.metadata.base === 0) | if (this.metadata && this.metadata.base !== undefined && this.metadata.base === 0) | ||||
this.metadata.base = this.trainers[0].getMetadata(PlayerID, "base"); | this.metadata.base = this.trainers[0].getMetadata(PlayerID, "base"); | ||||
this.trainers[0].train(gameState.getPlayerCiv(), this.type, this.number, this.metadata, this.promotedTypes(gameState)); | this.trainers[0].train(gameState.getPlayerCiv(), this.type, this.number, this.metadata, this.promotedTypes(gameState)); | ||||
this.onStart(gameState); | this.onStart(gameState); | ||||
}; | }; | ||||
m.TrainingPlan.prototype.addItem = function(amount = 1) | PETRA.TrainingPlan.prototype.addItem = function(amount = 1) | ||||
{ | { | ||||
this.number += amount; | this.number += amount; | ||||
}; | }; | ||||
/** Find the promoted types corresponding to this.type */ | /** Find the promoted types corresponding to this.type */ | ||||
m.TrainingPlan.prototype.promotedTypes = function(gameState) | PETRA.TrainingPlan.prototype.promotedTypes = function(gameState) | ||||
{ | { | ||||
let types = []; | let types = []; | ||||
let promotion = this.template.promotion(); | let promotion = this.template.promotion(); | ||||
let previous; | let previous; | ||||
let template; | let template; | ||||
while (promotion) | while (promotion) | ||||
{ | { | ||||
types.push(promotion); | types.push(promotion); | ||||
Show All 12 Lines | if (previous === promotion) | ||||
if (gameState.ai.Config.debug > 0) | if (gameState.ai.Config.debug > 0) | ||||
API3.warn(" unit " + promotion + " is its own promoted unit"); | API3.warn(" unit " + promotion + " is its own promoted unit"); | ||||
promotion = undefined; | promotion = undefined; | ||||
} | } | ||||
} | } | ||||
return types; | return types; | ||||
}; | }; | ||||
m.TrainingPlan.prototype.Serialize = function() | PETRA.TrainingPlan.prototype.Serialize = function() | ||||
{ | { | ||||
return { | return { | ||||
"category": this.category, | "category": this.category, | ||||
"type": this.type, | "type": this.type, | ||||
"ID": this.ID, | "ID": this.ID, | ||||
"metadata": this.metadata, | "metadata": this.metadata, | ||||
"cost": this.cost.Serialize(), | "cost": this.cost.Serialize(), | ||||
"number": this.number, | "number": this.number, | ||||
"maxMerge": this.maxMerge | "maxMerge": this.maxMerge | ||||
}; | }; | ||||
}; | }; | ||||
m.TrainingPlan.prototype.Deserialize = function(gameState, data) | PETRA.TrainingPlan.prototype.Deserialize = function(gameState, data) | ||||
{ | { | ||||
for (let key in data) | for (let key in data) | ||||
this[key] = data[key]; | this[key] = data[key]; | ||||
this.cost = new API3.Resources(); | this.cost = new API3.Resources(); | ||||
this.cost.Deserialize(data.cost); | this.cost.Deserialize(data.cost); | ||||
}; | }; | ||||
return m; | |||||
}(PETRA); |
Wildfire Games · Phabricator