Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/headquarters.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/headquarters.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/headquarters.js @@ -1319,7 +1319,7 @@ gameState.ai.queueManager.changePriority("economicBuilding", 3*this.Config.priorities.economicBuilding); let plan = new m.ConstructionPlan(gameState, "structures/{civ}_market"); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("economicBuilding", gameState.ai.Config.priorities.economicBuilding); }; + plan.queueToReset = "economicBuilding"; queues.economicBuilding.addPlan(plan); }; @@ -1399,23 +1399,7 @@ { let plan = new m.ConstructionPlan(gameState, "structures/{civ}_house"); // change the starting condition according to the situation. - plan.isGo = function (gameState) { - if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_house")) - return false; - if (gameState.getPopulationMax() <= gameState.getPopulationLimit()) - return false; - let freeSlots = gameState.getPopulationLimit() - gameState.getPopulation(); - for (let ent of gameState.getOwnFoundations().values()) - freeSlots += ent.getPopulationBonus(); - - if (gameState.ai.HQ.saveResources) - return freeSlots <= 10; - else if (gameState.getPopulation() > 55) - return freeSlots <= 21; - else if (gameState.getPopulation() > 30) - return freeSlots <= 15; - return freeSlots <= 10; - }; + plan.isGoRequirement = "houseNeeded"; queues.house.addPlan(plan); } @@ -1447,7 +1431,7 @@ --needed; else if (needed > 0) { - houseQueue[i].isGo = function () { return true; }; + houseQueue[i].isGoRequirement = undefined; --needed; } } @@ -1561,7 +1545,7 @@ if (!numFortresses) gameState.ai.queueManager.changePriority("defenseBuilding", 2*this.Config.priorities.defenseBuilding); let plan = new m.ConstructionPlan(gameState, "structures/{civ}_fortress"); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("defenseBuilding", gameState.ai.Config.priorities.defenseBuilding); }; + plan.queueToReset = "defenseBuilding"; queues.defenseBuilding.addPlan(plan); return; } @@ -1593,7 +1577,7 @@ if (numTowers > 2 * this.numActiveBase() + 3) gameState.ai.queueManager.changePriority("defenseBuilding", Math.round(0.7*this.Config.priorities.defenseBuilding)); let plan = new m.ConstructionPlan(gameState, "structures/{civ}_defense_tower"); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("defenseBuilding", gameState.ai.Config.priorities.defenseBuilding); }; + plan.queueToReset = "defenseBuilding"; queues.defenseBuilding.addPlan(plan); } }; @@ -1633,7 +1617,7 @@ gameState.ai.queueManager.changePriority("militaryBuilding", 2*this.Config.priorities.militaryBuilding); let preferredBase = this.findBestBaseForMilitary(gameState); let plan = new m.ConstructionPlan(gameState, "structures/{civ}_barracks", { "preferredBase": preferredBase }); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("militaryBuilding", gameState.ai.Config.priorities.militaryBuilding); }; + plan.queueToReset = "militaryBuilding"; queues.militaryBuilding.addPlan(plan); return; } Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/queue.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/queue.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/queue.js @@ -145,12 +145,12 @@ for (let dataPlan of data.plans) { let plan; - if (dataPlan.prop.category == "unit") - plan = new m.TrainingPlan(gameState, dataPlan.prop.type); - else if (dataPlan.prop.category == "building") - plan = new m.ConstructionPlan(gameState, dataPlan.prop.type); - else if (dataPlan.prop.category == "technology") - plan = new m.ResearchPlan(gameState, dataPlan.prop.type); + if (dataPlan.category == "unit") + plan = new m.TrainingPlan(gameState, dataPlan.type); + else if (dataPlan.category == "building") + plan = new m.ConstructionPlan(gameState, dataPlan.type); + else if (dataPlan.category == "technology") + plan = new m.ResearchPlan(gameState, dataPlan.type); else { API3.warn("Petra deserialization error: plan unknown " + uneval(dataPlan)); Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanBuilding.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanBuilding.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanBuilding.js @@ -764,37 +764,58 @@ return nbcell ? total / nbcell : 0; }; +m.ConstructionPlan.prototype.isGo = function(gameState) +{ + if (this.isGoRequirement && this.isGoRequirement === "houseNeeded") + { + if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_house")) + return false; + if (gameState.getPopulationMax() <= gameState.getPopulationLimit()) + return false; + let freeSlots = gameState.getPopulationLimit() - gameState.getPopulation(); + for (let ent of gameState.getOwnFoundations().values()) + freeSlots += ent.getPopulationBonus(); + + if (gameState.ai.HQ.saveResources) + return freeSlots <= 10; + else if (gameState.getPopulation() > 55) + return freeSlots <= 21; + else if (gameState.getPopulation() > 30) + return freeSlots <= 15; + return freeSlots <= 10; + } + return true; +}; + +m.ConstructionPlan.prototype.onStart = function(gameState) +{ + if (this.queueToReset) + gameState.ai.queueManager.changePriority(this.queueToReset, gameState.ai.Config.priorities[this.queueToReset]); +}; + m.ConstructionPlan.prototype.Serialize = function() { - let prop = { + return { "category": this.category, "type": this.type, "ID": this.ID, "metadata": this.metadata, "cost": this.cost.Serialize(), "number": this.number, - "position": this.position + "position": this.position, + "isGoRequirement": this.isGoRequirement || undefined, + "queueToReset": this.queueToReset || undefined }; - - 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.prop) - this[key] = data.prop[key]; + for (let key in data) + this[key] = data[key]; let cost = new API3.Resources(); - cost.Deserialize(data.prop.cost); + cost.Deserialize(data.cost); this.cost = cost; - - for (let fun in data.func) - this[fun] = eval(data.func[fun]); }; return m; Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanResearch.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanResearch.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanResearch.js @@ -71,37 +71,61 @@ this.onStart(gameState); }; +m.ResearchPlan.prototype.isGo = function(gameState) +{ + if (this.type === gameState.townPhase()) + { + let ret = gameState.getPopulation() >= gameState.ai.Config.Economy.popForTown; + if (ret && gameState.ai.HQ.econState !== "growth") + gameState.ai.HQ.econState = "growth"; + else if (!ret && gameState.ai.HQ.econState !== "townPhasing") + gameState.ai.HQ.econState = "townPhasing"; + return ret; + } + else if (this.type === gameState.cityPhase()) + gameState.ai.HQ.econState = "cityPhasing"; + return true; +}; + +m.ResearchPlan.prototype.onStart = function(gameState) +{ + if (this.queueToReset) + gameState.ai.queueManager.changePriority(this.queueToReset, gameState.ai.Config.priorities[this.queueToReset]); + + if (this.type == gameState.townPhase()) + { + gameState.ai.HQ.econState = "growth"; + gameState.ai.HQ.OnTownPhase(gameState); + } + else if (this.type == gameState.cityPhase()) + { + gameState.ai.HQ.econState = "growth"; + gameState.ai.HQ.OnCityPhase(gameState); + } +}; + m.ResearchPlan.prototype.Serialize = function() { - let prop = { + return { "category": this.category, "type": this.type, "ID": this.ID, "metadata": this.metadata, "cost": this.cost.Serialize(), "number": this.number, - "rush": this.rush + "rush": this.rush, + "queueToReset": this.queueToReset || undefined }; - - let func = { - "isGo": uneval(this.isGo), - "onStart": uneval(this.onStart) - }; - - return { "prop": prop, "func": func }; }; m.ResearchPlan.prototype.Deserialize = function(gameState, data) { - for (let key in data.prop) - this[key] = data.prop[key]; + for (let key in data) + this[key] = data[key]; let cost = new API3.Resources(); - cost.Deserialize(data.prop.cost); + cost.Deserialize(data.cost); this.cost = cost; - - for (let fun in data.func) - this[fun] = eval(data.func[fun]); }; return m; Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js @@ -169,7 +169,7 @@ m.TrainingPlan.prototype.Serialize = function() { - let prop = { + return { "category": this.category, "type": this.type, "ID": this.ID, @@ -178,17 +178,15 @@ "number": this.number, "maxMerge": this.maxMerge }; - - return { "prop": prop }; }; m.TrainingPlan.prototype.Deserialize = function(gameState, data) { - for (let key in data.prop) - this[key] = data.prop[key]; + for (let key in data) + this[key] = data[key]; let cost = new API3.Resources(); - cost.Deserialize(data.prop.cost); + cost.Deserialize(data.cost); this.cost = cost; }; Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/researchManager.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/researchManager.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/researchManager.js @@ -25,18 +25,6 @@ gameState.hasResearchers(townPhase, true)) { let plan = new m.ResearchPlan(gameState, townPhase, true); - plan.onStart = function (gameState) { - gameState.ai.HQ.econState = "growth"; - gameState.ai.HQ.OnTownPhase(gameState); - }; - plan.isGo = function (gameState) { - let ret = gameState.getPopulation() >= gameState.ai.Config.Economy.popForTown; - if (ret && gameState.ai.HQ.econState !== "growth") - gameState.ai.HQ.econState = "growth"; - else if (!ret && gameState.ai.HQ.econState !== "townPhasing") - gameState.ai.HQ.econState = "townPhasing"; - return ret; - }; queues.majorTech.addPlan(plan); } else if (gameState.canResearch(cityPhase,true) && gameState.ai.elapsedTime > this.Config.Economy.cityPhase && @@ -44,14 +32,6 @@ gameState.hasResearchers(cityPhase, true) && !queues.civilCentre.hasQueuedUnits()) { let plan = new m.ResearchPlan(gameState, cityPhase, true); - plan.onStart = function (gameState) { - gameState.ai.HQ.econState = "growth"; - gameState.ai.HQ.OnCityPhase(gameState); - }; - plan.isGo = function (gameState) { - gameState.ai.HQ.econState = "cityPhasing"; - return true; - }; queues.majorTech.addPlan(plan); } }; @@ -185,7 +165,7 @@ { gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); let plan = new m.ResearchPlan(gameState, techName.name); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("minorTech", gameState.ai.Config.priorities.minorTech); }; + plan.queueToReset = "minorTech"; queues.minorTech.addPlan(plan); } else @@ -203,7 +183,7 @@ { gameState.ai.queueManager.changePriority("minorTech", 2*this.Config.priorities.minorTech); let plan = new m.ResearchPlan(gameState, techName.name); - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("minorTech", gameState.ai.Config.priorities.minorTech); }; + plan.queueToReset = "minorTech"; queues.minorTech.addPlan(plan); } else Index: ps/trunk/binaries/data/mods/public/simulation/ai/petra/tradeManager.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/ai/petra/tradeManager.js +++ ps/trunk/binaries/data/mods/public/simulation/ai/petra/tradeManager.js @@ -582,7 +582,7 @@ gameState.ai.queueManager.changePriority("economicBuilding", 2*this.Config.priorities.economicBuilding); let plan = new m.ConstructionPlan(gameState, "structures/{civ}_market"); if (!this.tradeRoute) - plan.onStart = function(gameState) { gameState.ai.queueManager.changePriority("economicBuilding", gameState.ai.Config.priorities.economicBuilding); }; + plan.queueToReset = "economicBuilding"; queues.economicBuilding.addPlan(plan); };