petra: internal functions are now serialized

This was SVN commit r16016.
This commit is contained in:
mimo
2014-11-30 19:05:39 +00:00
parent 3c45599be5
commit 5403c19cf0
5 changed files with 59 additions and 99 deletions
@@ -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.
@@ -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);
@@ -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;
@@ -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;
@@ -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;
};