Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/_petrabot.js
Engine.IncludeModule("common-api"); | Engine.IncludeModule("common-api"); | ||||
var PETRA = (function() { | var PETRA = {}; | ||||
var m = {}; | |||||
m.PetraBot = function PetraBot(settings) | PETRA.PetraBot = function PetraBot(settings) | ||||
elexis: Shouldn't this be `PETRA.PetraBot = function (settings)`?
It's a bit misleading if changing the… | |||||
{ | { | ||||
API3.BaseAI.call(this, settings); | API3.BaseAI.call(this, settings); | ||||
this.playedTurn = 0; | this.playedTurn = 0; | ||||
this.elapsedTime = 0; | this.elapsedTime = 0; | ||||
this.uniqueIDs = { | this.uniqueIDs = { | ||||
"armies": 1, // starts at 1 to allow easier tests on armies ID existence | "armies": 1, // starts at 1 to allow easier tests on armies ID existence | ||||
"bases": 1, // base manager ID starts at one because "0" means "no base" on the map | "bases": 1, // base manager ID starts at one because "0" means "no base" on the map | ||||
"plans": 0, // training/building/research plans | "plans": 0, // training/building/research plans | ||||
"transports": 1 // transport plans start at 1 because 0 might be used as none | "transports": 1 // transport plans start at 1 because 0 might be used as none | ||||
}; | }; | ||||
this.Config = new m.Config(settings.difficulty, settings.behavior); | this.Config = new PETRA.Config(settings.difficulty, settings.behavior); | ||||
this.savedEvents = {}; | this.savedEvents = {}; | ||||
}; | }; | ||||
m.PetraBot.prototype = new API3.BaseAI(); | PETRA.PetraBot.prototype = new API3.BaseAI(); | ||||
m.PetraBot.prototype.CustomInit = function(gameState) | PETRA.PetraBot.prototype.CustomInit = function(gameState) | ||||
{ | { | ||||
if (this.isDeserialized) | if (this.isDeserialized) | ||||
{ | { | ||||
// WARNING: the deserializations should not modify the metadatas infos inside their init functions | // WARNING: the deserializations should not modify the metadatas infos inside their init functions | ||||
elexisUnsubmitted Not Done Inline Actions(Deserialization needs to be fixed over here.) elexis: (Deserialization needs to be fixed over here.) | |||||
this.turn = this.data.turn; | this.turn = this.data.turn; | ||||
this.playedTurn = this.data.playedTurn; | this.playedTurn = this.data.playedTurn; | ||||
this.elapsedTime = this.data.elapsedTime; | this.elapsedTime = this.data.elapsedTime; | ||||
this.savedEvents = this.data.savedEvents; | this.savedEvents = this.data.savedEvents; | ||||
for (let key in this.savedEvents) | for (let key in this.savedEvents) | ||||
{ | { | ||||
for (let i in this.savedEvents[key]) | for (let i in this.savedEvents[key]) | ||||
{ | { | ||||
if (!this.savedEvents[key][i].entityObj) | if (!this.savedEvents[key][i].entityObj) | ||||
continue; | continue; | ||||
let evt = this.savedEvents[key][i]; | let evt = this.savedEvents[key][i]; | ||||
let evtmod = {}; | let evtmod = {}; | ||||
for (let keyevt in evt) | for (let keyevt in evt) | ||||
{ | { | ||||
evtmod[keyevt] = evt[keyevt]; | evtmod[keyevt] = evt[keyevt]; | ||||
evtmod.entityObj = new API3.Entity(gameState.sharedScript, evt.entityObj); | evtmod.entityObj = new API3.Entity(gameState.sharedScript, evt.entityObj); | ||||
this.savedEvents[key][i] = evtmod; | this.savedEvents[key][i] = evtmod; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
this.Config.Deserialize(this.data.config); | this.Config.Deserialize(this.data.config); | ||||
this.queueManager = new m.QueueManager(this.Config, {}); | this.queueManager = new PETRA.QueueManager(this.Config, {}); | ||||
this.queueManager.Deserialize(gameState, this.data.queueManager); | this.queueManager.Deserialize(gameState, this.data.queueManager); | ||||
this.queues = this.queueManager.queues; | this.queues = this.queueManager.queues; | ||||
this.HQ = new m.HQ(this.Config); | this.HQ = new PETRA.HQ(this.Config); | ||||
this.HQ.init(gameState, this.queues); | this.HQ.init(gameState, this.queues); | ||||
this.HQ.Deserialize(gameState, this.data.HQ); | this.HQ.Deserialize(gameState, this.data.HQ); | ||||
this.uniqueIDs = this.data.uniqueIDs; | this.uniqueIDs = this.data.uniqueIDs; | ||||
this.isDeserialized = false; | this.isDeserialized = false; | ||||
this.data = undefined; | this.data = undefined; | ||||
// initialisation needed after the completion of the deserialization | // initialisation needed after the completion of the deserialization | ||||
this.HQ.postinit(gameState); | this.HQ.postinit(gameState); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
this.Config.setConfig(gameState); | this.Config.setConfig(gameState); | ||||
// this.queues can only be modified by the queue manager or things will go awry. | // this.queues can only be modified by the queue manager or things will go awry. | ||||
this.queues = {}; | this.queues = {}; | ||||
for (let i in this.Config.priorities) | for (let i in this.Config.priorities) | ||||
this.queues[i] = new m.Queue(); | this.queues[i] = new PETRA.Queue(); | ||||
this.queueManager = new m.QueueManager(this.Config, this.queues); | this.queueManager = new PETRA.QueueManager(this.Config, this.queues); | ||||
this.HQ = new m.HQ(this.Config); | this.HQ = new PETRA.HQ(this.Config); | ||||
this.HQ.init(gameState, this.queues); | this.HQ.init(gameState, this.queues); | ||||
// Analyze our starting position and set a strategy | // Analyze our starting position and set a strategy | ||||
this.HQ.gameAnalysis(gameState); | this.HQ.gameAnalysis(gameState); | ||||
} | } | ||||
}; | }; | ||||
m.PetraBot.prototype.OnUpdate = function(sharedScript) | PETRA.PetraBot.prototype.OnUpdate = function(sharedScript) | ||||
{ | { | ||||
if (this.gameFinished) | if (this.gameFinished) | ||||
return; | return; | ||||
for (let i in this.events) | for (let i in this.events) | ||||
{ | { | ||||
if (i == "AIMetadata") // not used inside petra | if (i == "AIMetadata") // not used inside petra | ||||
continue; | continue; | ||||
Show All 25 Lines | for (let i in this.savedEvents) | ||||
this.savedEvents[i] = []; | this.savedEvents[i] = []; | ||||
Engine.ProfileStop(); | Engine.ProfileStop(); | ||||
} | } | ||||
this.turn++; | this.turn++; | ||||
}; | }; | ||||
m.PetraBot.prototype.Serialize = function() | PETRA.PetraBot.prototype.Serialize = function() | ||||
{ | { | ||||
let savedEvents = {}; | let savedEvents = {}; | ||||
for (let key in this.savedEvents) | for (let key in this.savedEvents) | ||||
{ | { | ||||
savedEvents[key] = this.savedEvents[key].slice(); | savedEvents[key] = this.savedEvents[key].slice(); | ||||
for (let i in savedEvents[key]) | for (let i in savedEvents[key]) | ||||
{ | { | ||||
if (!savedEvents[key][i].entityObj) | if (!savedEvents[key][i].entityObj) | ||||
Show All 14 Lines | return { | ||||
"elapsedTime": this.elapsedTime, | "elapsedTime": this.elapsedTime, | ||||
"savedEvents": savedEvents, | "savedEvents": savedEvents, | ||||
"config": this.Config.Serialize(), | "config": this.Config.Serialize(), | ||||
"queueManager": this.queueManager.Serialize(), | "queueManager": this.queueManager.Serialize(), | ||||
"HQ": this.HQ.Serialize() | "HQ": this.HQ.Serialize() | ||||
}; | }; | ||||
}; | }; | ||||
m.PetraBot.prototype.Deserialize = function(data, sharedScript) | PETRA.PetraBot.prototype.Deserialize = function(data, sharedScript) | ||||
{ | { | ||||
this.isDeserialized = true; | this.isDeserialized = true; | ||||
this.data = data; | this.data = data; | ||||
}; | }; | ||||
return m; | |||||
}()); |
Wildfire Games · Phabricator
Shouldn't this be PETRA.PetraBot = function (settings)?
It's a bit misleading if changing the name won't change the function call.
(I wonder if PetraBot could receive a name more distinct from PETRA, but I don't find an obvious better one.)
Edit: Update: You updated the patch after I mentioned this on IRC. We came to the conclusion it's necessary to keep this name for the Map in API3, but here it is not necessary, thus misleading for the reader (as he may think its necessary to name it like that).
Its also the only two occurrences in the codebase. So away with this one.