mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-06-24 14:47:12 +00:00
petra: internal functions are now serialized
This was SVN commit r16016.
This commit is contained in:
@@ -1259,10 +1259,9 @@ m.HQ.prototype.buildMarket = function(gameState, queues)
|
|||||||
return;
|
return;
|
||||||
if (!this.canBuild(gameState, "structures/{civ}_market"))
|
if (!this.canBuild(gameState, "structures/{civ}_market"))
|
||||||
return;
|
return;
|
||||||
var priority = this.Config.priorities.economicBuilding;
|
gameState.ai.queueManager.changePriority("economicBuilding", 3*this.Config.priorities.economicBuilding);
|
||||||
gameState.ai.queueManager.changePriority("economicBuilding", 3*priority);
|
|
||||||
var plan = new m.ConstructionPlan(gameState, "structures/{civ}_market");
|
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);
|
queues.economicBuilding.addItem(plan);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1296,10 +1295,9 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues)
|
|||||||
if (numPlanned < 3 || (numPlanned < 5 && gameState.getPopulation() > 80))
|
if (numPlanned < 3 || (numPlanned < 5 && gameState.getPopulation() > 80))
|
||||||
{
|
{
|
||||||
var plan = new m.ConstructionPlan(gameState, "structures/{civ}_house");
|
var plan = new m.ConstructionPlan(gameState, "structures/{civ}_house");
|
||||||
var self = this;
|
|
||||||
// change the starting condition according to the situation.
|
// change the starting condition according to the situation.
|
||||||
plan.isGo = function (gameState) {
|
plan.isGo = function (gameState) {
|
||||||
if (!self.canBuild(gameState, "structures/{civ}_house"))
|
if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_house"))
|
||||||
return false;
|
return false;
|
||||||
if (gameState.getPopulationMax() <= gameState.getPopulationLimit())
|
if (gameState.getPopulationMax() <= gameState.getPopulationLimit())
|
||||||
return false;
|
return false;
|
||||||
@@ -1309,7 +1307,7 @@ m.HQ.prototype.buildMoreHouses = function(gameState,queues)
|
|||||||
// TODO how to modify with tech
|
// TODO how to modify with tech
|
||||||
var popBonus = gameState.getTemplate(gameState.applyCiv("structures/{civ}_house")).getPopulationBonus();
|
var popBonus = gameState.getTemplate(gameState.applyCiv("structures/{civ}_house")).getPopulationBonus();
|
||||||
freeSlots = gameState.getPopulationLimit() + HouseNb*popBonus - gameState.getPopulation();
|
freeSlots = gameState.getPopulationLimit() + HouseNb*popBonus - gameState.getPopulation();
|
||||||
if (self.saveResources)
|
if (gameState.ai.HQ.saveResources)
|
||||||
return (freeSlots <= 10);
|
return (freeSlots <= 10);
|
||||||
else if (gameState.getPopulation() > 55)
|
else if (gameState.getPopulation() > 55)
|
||||||
return (freeSlots <= 21);
|
return (freeSlots <= 21);
|
||||||
@@ -1524,11 +1522,10 @@ m.HQ.prototype.constructTrainingBuildings = function(gameState, queues)
|
|||||||
if (barrackNb == 0 && (gameState.getPopulation() > this.Config.Military.popForBarracks1 ||
|
if (barrackNb == 0 && (gameState.getPopulation() > this.Config.Military.popForBarracks1 ||
|
||||||
(this.econState == "townPhasing" && gameState.getOwnStructures().filter(API3.Filters.byClass("Village")).length < 5)))
|
(this.econState == "townPhasing" && gameState.getOwnStructures().filter(API3.Filters.byClass("Village")).length < 5)))
|
||||||
{
|
{
|
||||||
var priority = this.Config.priorities.militaryBuilding;
|
gameState.ai.queueManager.changePriority("militaryBuilding", 2*this.Config.priorities.militaryBuilding);
|
||||||
gameState.ai.queueManager.changePriority("militaryBuilding", 2*priority);
|
|
||||||
var preferredBase = this.findBestBaseForMilitary(gameState);
|
var preferredBase = this.findBestBaseForMilitary(gameState);
|
||||||
var plan = new m.ConstructionPlan(gameState, "structures/{civ}_barracks", { "preferredBase": preferredBase });
|
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);
|
queues.militaryBuilding.addItem(plan);
|
||||||
}
|
}
|
||||||
// second barracks, then 3rd barrack, and optional 4th for some civs as they rely on barracks more.
|
// 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 = [];
|
this.queue = [];
|
||||||
for (let dataPlan of data.queue)
|
for (let dataPlan of data.queue)
|
||||||
{
|
{
|
||||||
if (dataPlan.category == "unit")
|
if (dataPlan.prop.category == "unit")
|
||||||
var plan = new m.TrainingPlan(gameState, dataPlan.type);
|
var plan = new m.TrainingPlan(gameState, dataPlan.prop.type);
|
||||||
else if (dataPlan.category == "building")
|
else if (dataPlan.prop.category == "building")
|
||||||
var plan = new m.ConstructionPlan(gameState, dataPlan.type);
|
var plan = new m.ConstructionPlan(gameState, dataPlan.prop.type);
|
||||||
else if (dataPlan.category == "technology")
|
else if (dataPlan.prop.category == "technology")
|
||||||
var plan = new m.ResearchPlan(gameState, dataPlan.type);
|
var plan = new m.ResearchPlan(gameState, dataPlan.prop.type);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
API3.warn("Petra deserialization error: plan " + dataPlan.category + " unknown.");
|
API3.warn("Petra deserialization error: plan unknown " + uneval(dataPlan));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
plan.Deserialize(gameState, dataPlan);
|
plan.Deserialize(gameState, dataPlan);
|
||||||
|
|||||||
@@ -32,8 +32,7 @@ m.ConstructionPlan.prototype.canStart = function(gameState)
|
|||||||
if (this.template.requiredTech() && !gameState.isResearched(this.template.requiredTech()))
|
if (this.template.requiredTech() && !gameState.isResearched(this.template.requiredTech()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var builders = gameState.findBuilders(this.type);
|
return (gameState.findBuilders(this.type).length != 0);
|
||||||
return (builders.length != 0);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
m.ConstructionPlan.prototype.start = function(gameState)
|
m.ConstructionPlan.prototype.start = function(gameState)
|
||||||
@@ -454,62 +453,36 @@ m.ConstructionPlan.prototype.getDockAngle = function(gameState, x, z)
|
|||||||
|
|
||||||
m.ConstructionPlan.prototype.Serialize = function()
|
m.ConstructionPlan.prototype.Serialize = function()
|
||||||
{
|
{
|
||||||
return {
|
let prop = {
|
||||||
"type": this.type,
|
|
||||||
"metadata": this.metadata,
|
|
||||||
"ID": this.ID,
|
|
||||||
"category": this.category,
|
"category": this.category,
|
||||||
|
"type": this.type,
|
||||||
|
"ID": this.ID,
|
||||||
|
"metadata": this.metadata,
|
||||||
"cost": this.cost.Serialize(),
|
"cost": this.cost.Serialize(),
|
||||||
"number": this.number,
|
"number": this.number,
|
||||||
"position": this.position,
|
"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)
|
m.ConstructionPlan.prototype.Deserialize = function(gameState, data)
|
||||||
{
|
{
|
||||||
for (let key in data)
|
for (let key in data.prop)
|
||||||
this[key] = data[key];
|
this[key] = data.prop[key];
|
||||||
|
|
||||||
let cost = new API3.Resources();
|
let cost = new API3.Resources();
|
||||||
cost.Deserialize(data.cost);
|
cost.Deserialize(data.prop.cost);
|
||||||
this.cost = cost;
|
this.cost = cost;
|
||||||
|
|
||||||
// TODO find a way to properly serialize functions. For the time being, they are manually added
|
for (let fun in data.func)
|
||||||
if (this.type == gameState.applyCiv("structures/{civ}_house"))
|
this[fun] = eval(data.func[fun]);
|
||||||
{
|
|
||||||
// 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); };
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
|
|||||||
@@ -51,50 +51,38 @@ m.ResearchPlan.prototype.start = function(gameState)
|
|||||||
|
|
||||||
m.ResearchPlan.prototype.Serialize = function()
|
m.ResearchPlan.prototype.Serialize = function()
|
||||||
{
|
{
|
||||||
return {
|
let prop = {
|
||||||
"type": this.type,
|
|
||||||
"metadata": this.metadata,
|
|
||||||
"ID": this.ID,
|
|
||||||
"category": this.category,
|
"category": this.category,
|
||||||
|
"type": this.type,
|
||||||
|
"ID": this.ID,
|
||||||
|
"metadata": this.metadata,
|
||||||
"cost": this.cost.Serialize(),
|
"cost": this.cost.Serialize(),
|
||||||
"number": this.number,
|
"number": this.number,
|
||||||
|
"rush": this.rush,
|
||||||
"lastIsGo": this.lastIsGo,
|
"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)
|
m.ResearchPlan.prototype.Deserialize = function(gameState, data)
|
||||||
{
|
{
|
||||||
for (let key in data)
|
for (let key in data.prop)
|
||||||
this[key] = data[key];
|
this[key] = data.prop[key];
|
||||||
|
|
||||||
let cost = new API3.Resources();
|
let cost = new API3.Resources();
|
||||||
cost.Deserialize(data.cost);
|
cost.Deserialize(data.prop.cost);
|
||||||
this.cost = cost;
|
this.cost = cost;
|
||||||
|
|
||||||
// TODO find a way to properly serialize functions. For the time being, they are manually added
|
for (let fun in data.func)
|
||||||
if (this.type == gameState.townPhase())
|
this[fun] = eval(data.func[fun]);
|
||||||
{
|
|
||||||
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) };
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
|
|||||||
@@ -141,25 +141,27 @@ m.TrainingPlan.prototype.promotedTypes = function(gameState)
|
|||||||
|
|
||||||
m.TrainingPlan.prototype.Serialize = function()
|
m.TrainingPlan.prototype.Serialize = function()
|
||||||
{
|
{
|
||||||
return {
|
let prop = {
|
||||||
"type": this.type,
|
|
||||||
"metadata": this.metadata,
|
|
||||||
"ID": this.ID,
|
|
||||||
"category": this.category,
|
"category": this.category,
|
||||||
|
"type": this.type,
|
||||||
|
"ID": this.ID,
|
||||||
|
"metadata": this.metadata,
|
||||||
"cost": this.cost.Serialize(),
|
"cost": this.cost.Serialize(),
|
||||||
"number": this.number,
|
"number": this.number,
|
||||||
"maxMerge": this.maxMerge,
|
"maxMerge": this.maxMerge,
|
||||||
"lastIsGo": this.lastIsGo
|
"lastIsGo": this.lastIsGo
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return { "prop": prop };
|
||||||
};
|
};
|
||||||
|
|
||||||
m.TrainingPlan.prototype.Deserialize = function(gameState, data)
|
m.TrainingPlan.prototype.Deserialize = function(gameState, data)
|
||||||
{
|
{
|
||||||
for (let key in data)
|
for (let key in data.prop)
|
||||||
this[key] = data[key];
|
this[key] = data.prop[key];
|
||||||
|
|
||||||
let cost = new API3.Resources();
|
let cost = new API3.Resources();
|
||||||
cost.Deserialize(data.cost);
|
cost.Deserialize(data.prop.cost);
|
||||||
this.cost = cost;
|
this.cost = cost;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user