From 5403c19cf051462ee70030bb4863d871be9b9351 Mon Sep 17 00:00:00 2001 From: mimo Date: Sun, 30 Nov 2014 19:05:39 +0000 Subject: [PATCH] petra: internal functions are now serialized This was SVN commit r16016. --- .../simulation/ai/petra/headquarters.js | 15 ++--- .../mods/public/simulation/ai/petra/queue.js | 14 ++--- .../simulation/ai/petra/queueplan-building.js | 63 ++++++------------- .../simulation/ai/petra/queueplan-research.js | 50 ++++++--------- .../simulation/ai/petra/queueplan-training.js | 16 ++--- 5 files changed, 59 insertions(+), 99 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/petra/headquarters.js b/binaries/data/mods/public/simulation/ai/petra/headquarters.js index 5a6d9ad3af..4b106b6662 100644 --- a/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ b/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -1259,10 +1259,9 @@ m.HQ.prototype.buildMarket = function(gameState, queues) return; if (!this.canBuild(gameState, "structures/{civ}_market")) return; - var priority = this.Config.priorities.economicBuilding; - gameState.ai.queueManager.changePriority("economicBuilding", 3*priority); + gameState.ai.queueManager.changePriority("economicBuilding", 3*this.Config.priorities.economicBuilding); var plan = new m.ConstructionPlan(gameState, "structures/{civ}_market"); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("economicBuilding", priority); }; + plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("economicBuilding", gameState.ai.Config.priorities.economicBuilding); }; queues.economicBuilding.addItem(plan); }; @@ -1296,10 +1295,9 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues) if (numPlanned < 3 || (numPlanned < 5 && gameState.getPopulation() > 80)) { var plan = new m.ConstructionPlan(gameState, "structures/{civ}_house"); - var self = this; // change the starting condition according to the situation. plan.isGo = function (gameState) { - if (!self.canBuild(gameState, "structures/{civ}_house")) + if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_house")) return false; if (gameState.getPopulationMax() <= gameState.getPopulationLimit()) return false; @@ -1309,7 +1307,7 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues) // TODO how to modify with tech var popBonus = gameState.getTemplate(gameState.applyCiv("structures/{civ}_house")).getPopulationBonus(); freeSlots = gameState.getPopulationLimit() + HouseNb*popBonus - gameState.getPopulation(); - if (self.saveResources) + if (gameState.ai.HQ.saveResources) return (freeSlots <= 10); else if (gameState.getPopulation() > 55) return (freeSlots <= 21); @@ -1524,11 +1522,10 @@ m.HQ.prototype.constructTrainingBuildings = function(gameState, queues) if (barrackNb == 0 && (gameState.getPopulation() > this.Config.Military.popForBarracks1 || (this.econState == "townPhasing" && gameState.getOwnStructures().filter(API3.Filters.byClass("Village")).length < 5))) { - var priority = this.Config.priorities.militaryBuilding; - gameState.ai.queueManager.changePriority("militaryBuilding", 2*priority); + gameState.ai.queueManager.changePriority("militaryBuilding", 2*this.Config.priorities.militaryBuilding); var preferredBase = this.findBestBaseForMilitary(gameState); var plan = new m.ConstructionPlan(gameState, "structures/{civ}_barracks", { "preferredBase": preferredBase }); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("militaryBuilding", priority); }; + plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("militaryBuilding", gameState.ai.Config.priorities.militaryBuilding); }; queues.militaryBuilding.addItem(plan); } // second barracks, then 3rd barrack, and optional 4th for some civs as they rely on barracks more. diff --git a/binaries/data/mods/public/simulation/ai/petra/queue.js b/binaries/data/mods/public/simulation/ai/petra/queue.js index 29d6eb4292..b448e69614 100644 --- a/binaries/data/mods/public/simulation/ai/petra/queue.js +++ b/binaries/data/mods/public/simulation/ai/petra/queue.js @@ -143,15 +143,15 @@ m.Queue.prototype.Deserialize = function(gameState, data) this.queue = []; for (let dataPlan of data.queue) { - if (dataPlan.category == "unit") - var plan = new m.TrainingPlan(gameState, dataPlan.type); - else if (dataPlan.category == "building") - var plan = new m.ConstructionPlan(gameState, dataPlan.type); - else if (dataPlan.category == "technology") - var plan = new m.ResearchPlan(gameState, dataPlan.type); + if (dataPlan.prop.category == "unit") + var plan = new m.TrainingPlan(gameState, dataPlan.prop.type); + else if (dataPlan.prop.category == "building") + var plan = new m.ConstructionPlan(gameState, dataPlan.prop.type); + else if (dataPlan.prop.category == "technology") + var plan = new m.ResearchPlan(gameState, dataPlan.prop.type); else { - API3.warn("Petra deserialization error: plan " + dataPlan.category + " unknown."); + API3.warn("Petra deserialization error: plan unknown " + uneval(dataPlan)); continue; } plan.Deserialize(gameState, dataPlan); diff --git a/binaries/data/mods/public/simulation/ai/petra/queueplan-building.js b/binaries/data/mods/public/simulation/ai/petra/queueplan-building.js index 675a8f5174..91e7e663dc 100644 --- a/binaries/data/mods/public/simulation/ai/petra/queueplan-building.js +++ b/binaries/data/mods/public/simulation/ai/petra/queueplan-building.js @@ -32,8 +32,7 @@ m.ConstructionPlan.prototype.canStart = function(gameState) if (this.template.requiredTech() && !gameState.isResearched(this.template.requiredTech())) return false; - var builders = gameState.findBuilders(this.type); - return (builders.length != 0); + return (gameState.findBuilders(this.type).length != 0); }; m.ConstructionPlan.prototype.start = function(gameState) @@ -454,62 +453,36 @@ m.ConstructionPlan.prototype.getDockAngle = function(gameState, x, z) m.ConstructionPlan.prototype.Serialize = function() { - return { - "type": this.type, - "metadata": this.metadata, - "ID": this.ID, + let prop = { "category": this.category, + "type": this.type, + "ID": this.ID, + "metadata": this.metadata, "cost": this.cost.Serialize(), "number": this.number, "position": this.position, - "lastIsGo": this.lastIsGo + "lastIsGo": this.lastIsGo, }; + + let func = { + "isGo": uneval(this.isGo), + "onStart": uneval(this.onStart) + }; + + return { "prop": prop, "func": func }; }; m.ConstructionPlan.prototype.Deserialize = function(gameState, data) { - for (let key in data) - this[key] = data[key]; + for (let key in data.prop) + this[key] = data.prop[key]; let cost = new API3.Resources(); - cost.Deserialize(data.cost); + cost.Deserialize(data.prop.cost); this.cost = cost; - // TODO find a way to properly serialize functions. For the time being, they are manually added - if (this.type == gameState.applyCiv("structures/{civ}_house")) - { - // change the starting condition according to the situation. - this.isGo = function (gameState) { - if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_house")) - return false; - if (gameState.getPopulationMax() <= gameState.getPopulationLimit()) - return false; - var HouseNb = gameState.countEntitiesByType(gameState.applyCiv("foundation|structures/{civ}_house"), true); - - var freeSlots = 0; - // TODO how to modify with tech - var popBonus = gameState.getTemplate(gameState.applyCiv("structures/{civ}_house")).getPopulationBonus(); - freeSlots = gameState.getPopulationLimit() + HouseNb*popBonus - gameState.getPopulation(); - if (gameState.ai.HQ.saveResources) - return (freeSlots <= 10); - else if (gameState.getPopulation() > 55) - return (freeSlots <= 21); - else if (gameState.getPopulation() > 30) - return (freeSlots <= 15); - else - return (freeSlots <= 10); - }; - } - else if (this.type == gameState.applyCiv("structures/{civ}_market")) - { - let priority = gameState.ai.Config.priorities.economicBuilding; - this.onStart = function(gameState) { gameState.ai.queueManager.changePriority("economicBuilding", priority); }; - } - else if (this.type == gameState.applyCiv("structures/{civ}_barracks")) - { - let priority = gameState.ai.Config.priorities.militaryBuilding; - this.onStart = function(gameState) { gameState.ai.queueManager.changePriority("militaryBuilding", priority); }; - } + for (let fun in data.func) + this[fun] = eval(data.func[fun]); }; return m; diff --git a/binaries/data/mods/public/simulation/ai/petra/queueplan-research.js b/binaries/data/mods/public/simulation/ai/petra/queueplan-research.js index 25e3153475..2bc30efac2 100644 --- a/binaries/data/mods/public/simulation/ai/petra/queueplan-research.js +++ b/binaries/data/mods/public/simulation/ai/petra/queueplan-research.js @@ -51,50 +51,38 @@ m.ResearchPlan.prototype.start = function(gameState) m.ResearchPlan.prototype.Serialize = function() { - return { - "type": this.type, - "metadata": this.metadata, - "ID": this.ID, + let prop = { "category": this.category, + "type": this.type, + "ID": this.ID, + "metadata": this.metadata, "cost": this.cost.Serialize(), "number": this.number, + "rush": this.rush, "lastIsGo": this.lastIsGo, - "rush": this.rush }; + + let func = { + "isGo": uneval(this.isGo), + "onGo": uneval(this.onGo), + "onNotGo": uneval(this.onNotGo), + "onStart": uneval(this.onStart) + }; + + return { "prop": prop, "func": func }; }; m.ResearchPlan.prototype.Deserialize = function(gameState, data) { - for (let key in data) - this[key] = data[key]; + for (let key in data.prop) + this[key] = data.prop[key]; let cost = new API3.Resources(); - cost.Deserialize(data.cost); + cost.Deserialize(data.prop.cost); this.cost = cost; - // TODO find a way to properly serialize functions. For the time being, they are manually added - if (this.type == gameState.townPhase()) - { - this.onStart = function (gameState) { - gameState.ai.HQ.econState = "growth"; - gameState.ai.HQ.OnTownPhase(gameState); - }; - this.isGo = function (gameState) { - var ret = gameState.getPopulation() >= gameState.ai.Config.Economy.popForTown; - if (ret && !this.lastIsGo) - this.onGo(gameState); - else if (!ret && this.lastIsGo) - this.onNotGo(gameState); - this.lastIsGo = ret; - return ret; - }; - this.onGo = function (gameState) { gameState.ai.HQ.econState = "townPhasing"; }; - this.onNotGo = function (gameState) { gameState.ai.HQ.econState = "growth"; }; - } - else if (this.type == gameState.cityPhase()) - { - this.onStart = function (gameState) { gameState.ai.HQ.OnCityPhase(gameState) }; - } + for (let fun in data.func) + this[fun] = eval(data.func[fun]); }; return m; diff --git a/binaries/data/mods/public/simulation/ai/petra/queueplan-training.js b/binaries/data/mods/public/simulation/ai/petra/queueplan-training.js index 2701122cb1..228b644a83 100644 --- a/binaries/data/mods/public/simulation/ai/petra/queueplan-training.js +++ b/binaries/data/mods/public/simulation/ai/petra/queueplan-training.js @@ -141,25 +141,27 @@ m.TrainingPlan.prototype.promotedTypes = function(gameState) m.TrainingPlan.prototype.Serialize = function() { - return { - "type": this.type, - "metadata": this.metadata, - "ID": this.ID, + let prop = { "category": this.category, + "type": this.type, + "ID": this.ID, + "metadata": this.metadata, "cost": this.cost.Serialize(), "number": this.number, "maxMerge": this.maxMerge, "lastIsGo": this.lastIsGo }; + + return { "prop": prop }; }; m.TrainingPlan.prototype.Deserialize = function(gameState, data) { - for (let key in data) - this[key] = data[key]; + for (let key in data.prop) + this[key] = data.prop[key]; let cost = new API3.Resources(); - cost.Deserialize(data.cost); + cost.Deserialize(data.prop.cost); this.cost = cost; };