1
0
forked from mirrors/0ad

petra: revisit the serialization of functions, to fix 5403c19cf0

Differential Revision: https://code.wildfiregames.com/D617
This was SVN commit r19794.
This commit is contained in:
mimo
2017-06-17 08:42:04 +00:00
parent 00b60e2132
commit 0e887a686a
7 changed files with 94 additions and 87 deletions
@@ -1319,7 +1319,7 @@ m.HQ.prototype.buildMarket = function(gameState, queues)
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 @@ m.HQ.prototype.buildMoreHouses = function(gameState, queues)
{
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 @@ m.HQ.prototype.buildMoreHouses = function(gameState, queues)
--needed;
else if (needed > 0)
{
houseQueue[i].isGo = function () { return true; };
houseQueue[i].isGoRequirement = undefined;
--needed;
}
}
@@ -1561,7 +1545,7 @@ m.HQ.prototype.buildDefenses = function(gameState, queues)
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 @@ m.HQ.prototype.buildDefenses = function(gameState, queues)
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 @@ m.HQ.prototype.constructTrainingBuildings = function(gameState, queues)
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;
}
@@ -145,12 +145,12 @@ m.Queue.prototype.Deserialize = function(gameState, data)
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));
@@ -764,37 +764,58 @@ m.ConstructionPlan.prototype.getResourcesAround = function(gameState, types, i,
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;
@@ -71,37 +71,61 @@ m.ResearchPlan.prototype.start = function(gameState)
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;
@@ -169,7 +169,7 @@ m.TrainingPlan.prototype.promotedTypes = function(gameState)
m.TrainingPlan.prototype.Serialize = function()
{
let prop = {
return {
"category": this.category,
"type": this.type,
"ID": this.ID,
@@ -178,17 +178,15 @@ m.TrainingPlan.prototype.Serialize = function()
"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;
};
@@ -25,18 +25,6 @@ m.ResearchManager.prototype.checkPhase = function(gameState, queues)
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 @@ m.ResearchManager.prototype.checkPhase = function(gameState, queues)
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 @@ m.ResearchManager.prototype.update = function(gameState, queues)
{
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 @@ m.ResearchManager.prototype.update = function(gameState, queues)
{
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
@@ -582,7 +582,7 @@ m.TradeManager.prototype.prospectForNewMarket = function(gameState, queues)
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);
};