Page MenuHomeWildfire Games
Paste P146

Petra: Allow to upgrade entities
ActivePublic

Authored by lyv on Dec 28 2018, 6:30 AM.
Index: binaries/data/mods/public/simulation/ai/common-api/entity.js
===================================================================
--- binaries/data/mods/public/simulation/ai/common-api/entity.js (revision 21899)
+++ binaries/data/mods/public/simulation/ai/common-api/entity.js (working copy)
@@ -335,7 +335,28 @@
return undefined;
return templates.replace(/\{native\}/g, this.civ()).replace(/\{civ\}/g, civ).split(/\s+/);
},
+
+ "upgradableEntity": function(civ) {
+ // missing choices
+ let choices = this.get("Upgrade");
+ if (!choices)
+ return undefined;
+ return choices;
+ },
+
+ "upgradeCost": function(civ) {
+ if (!this.get("Upgrade/Cost"))
+ return;
+
+ let ret = {};
+ for (let type in this.get("Upgrade/Cost/Resources"))
+ ret[type] = +this.get("Upgrade/Cost/Resources/" + type);
+
+ warn(uneval(ret));
+ return ret;
+ },
+
"researchableTechs": function(gameState, civ) {
let templates = this.get("ProductionQueue/Technologies/_string");
if (!templates)
Index: binaries/data/mods/public/simulation/ai/petra/config.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/config.js (revision 21899)
+++ binaries/data/mods/public/simulation/ai/petra/config.js (working copy)
@@ -89,6 +90,7 @@
"majorTech": 700,
"minorTech": 40,
"wonder": 1000,
+ "entityUpgrading": 80,
"emergency": 1000 // used only in emergency situations, should be the highest one
};
Index: binaries/data/mods/public/simulation/ai/petra/headquarters.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/headquarters.js (revision 21899)
+++ binaries/data/mods/public/simulation/ai/petra/headquarters.js (working copy)
@@ -1891,6 +1891,31 @@
plan.queueToReset = "defenseBuilding";
queues.defenseBuilding.addPlan(plan);
}
+
+ // Check for possible upgrades and do them if not saving resources
+ if (!this.saveResource)
+ {
+ let upgradableDefenses = gameState.getOwnEntitiesByClass("SentryTower", true).toEntityArray();
+ //warn(uneval(upgradableDefenses));
+
+ for (let ent of upgradableDefenses)
+ {
+ let upgradleEnt = ent.upgradableEntity();
+ let chosenUpgrade;
+ for (let choice of Object.keys(upgradableEnt))
+ {
+ // TODO Some fancy decision making
+ chosenUpgrade = choice;
+ }
+
+ let template = gameState.applyCiv(upgradableEnt[chosenUpgrade].Entity);
+ let plan = new m.UpgradePlan(gameState, ent, template);
+ let queue = "entityUpgrading";
+ plan.queueToReset = queue;
+ queues.entityUpgrading.addPlan(plan);
+ warn("added a queue");
+ }
+ }
};
m.HQ.prototype.buildBlacksmith = function(gameState, queues)
Index: binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js (revision 21899)
+++ binaries/data/mods/public/simulation/ai/petra/queueplanTraining.js (working copy)
@@ -1,7 +1,7 @@
var PETRA = function(m)
{
-m.TrainingPlan = function(gameState, type, metadata, number = 1, maxMerge = 5)
+m.TrainingPlan = function(gameState, type/*template_name*/, metadata, number = 1, maxMerge = 5)
{
if (!m.QueuePlan.call(this, gameState, type, metadata))
{
Index: binaries/data/mods/public/simulation/ai/petra/queueplanUpgrading.js
===================================================================
--- binaries/data/mods/public/simulation/ai/petra/queueplanUpgrading.js (nonexistent)
+++ binaries/data/mods/public/simulation/ai/petra/queueplanUpgrading.js (working copy)
@@ -0,0 +1,67 @@
+var PETRA = function(m)
+{
+
+m.UpgradePlan = function(gameState, ent, template)
+{
+ this.entity = ent;
+ this.template = template;
+ this.cost = new API3.Resources(this.entity.upgradeCost());
+ this.category = "upgrading";
+ return true;
+};
+
+m.UpgradePlan.prototype = Object.create(m.QueuePlan.prototype);
+
+m.UpgradePlan.prototype.canStart = function(gameState)
+{
+ let needed = false;
+ /*if (this.entity.requiredTech() && !gameState.isResearched(this.entity.requiredTech()))
+ return false;*/
+ needed = true;
+ if (needed)
+ return true;
+};
+
+m.UpgradePlan.prototype.start = function(gameState)
+{
+ Engine.PostCommand(PlayerID, {
+ "type": "upgrade",
+ "entities": [this.entity.id()],
+ "template": this.template,
+ "queued": false
+ });
+
+ this.onStart(gameState);
+};
+
+m.UpgradePlan.prototype.getCost = function()
+{
+ return this.cost;
+};
+
+m.UpgradePlan.prototype.onStart = function(gameState)
+{
+ if (this.queueToReset)
+ gameState.ai.queueManager.changePriority(this.queueToReset, gameState.ai.Config.priorities[this.queueToReset]);
+};
+
+m.UpgradePlan.prototype.Serialize = function()
+{
+ return {
+ "entity": this.entity,
+ "cost": this.cost.Serialize(),
+ "queueToReset": this.queueToReset || undefined
+ };
+};
+
+m.UpgradePlan.prototype.Deserialize = function(gameState, data)
+{
+ for (let key in data)
+ this[key] = data[key];
+
+ this.cost = new API3.Resources();
+ this.cost.Deserialize(data.cost);
+};
+
+return m;
+}(PETRA);

Event Timeline

lyv created this paste.Dec 28 2018, 6:30 AM
lyv added a comment.Dec 28 2018, 6:36 AM

The more I look at it, the more I feel this is not quite the right way to do it. The IMO, better choice would be to follow the same way as how transporting units are done.

lyv edited the content of this paste. (Show Details)Dec 28 2018, 10:16 AM
lyv changed the visibility from "All Users" to "Public (No Login Required)".Dec 29 2018, 9:50 AM