Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/tradeManager.js
var PETRA = function(m) | |||||
{ | |||||
/** | /** | ||||
* Manage the trade | * Manage the trade | ||||
*/ | */ | ||||
PETRA.TradeManager = function(Config) | |||||
m.TradeManager = function(Config) | |||||
{ | { | ||||
this.Config = Config; | this.Config = Config; | ||||
this.tradeRoute = undefined; | this.tradeRoute = undefined; | ||||
this.potentialTradeRoute = undefined; | this.potentialTradeRoute = undefined; | ||||
this.routeProspection = false; | this.routeProspection = false; | ||||
this.targetNumTraders = this.Config.Economy.targetNumTraders; | this.targetNumTraders = this.Config.Economy.targetNumTraders; | ||||
this.warnedAllies = {}; | this.warnedAllies = {}; | ||||
}; | }; | ||||
m.TradeManager.prototype.init = function(gameState) | PETRA.TradeManager.prototype.init = function(gameState) | ||||
{ | { | ||||
this.traders = gameState.getOwnUnits().filter(API3.Filters.byMetadata(PlayerID, "role", "trader")); | this.traders = gameState.getOwnUnits().filter(API3.Filters.byMetadata(PlayerID, "role", "trader")); | ||||
this.traders.registerUpdates(); | this.traders.registerUpdates(); | ||||
this.minimalGain = gameState.ai.HQ.navalMap ? 3 : 5; | this.minimalGain = gameState.ai.HQ.navalMap ? 3 : 5; | ||||
}; | }; | ||||
m.TradeManager.prototype.hasTradeRoute = function() | PETRA.TradeManager.prototype.hasTradeRoute = function() | ||||
{ | { | ||||
return this.tradeRoute !== undefined; | return this.tradeRoute !== undefined; | ||||
}; | }; | ||||
m.TradeManager.prototype.assignTrader = function(ent) | PETRA.TradeManager.prototype.assignTrader = function(ent) | ||||
{ | { | ||||
ent.setMetadata(PlayerID, "role", "trader"); | ent.setMetadata(PlayerID, "role", "trader"); | ||||
this.traders.updateEnt(ent); | this.traders.updateEnt(ent); | ||||
}; | }; | ||||
m.TradeManager.prototype.trainMoreTraders = function(gameState, queues) | PETRA.TradeManager.prototype.trainMoreTraders = function(gameState, queues) | ||||
{ | { | ||||
if (!this.hasTradeRoute() || queues.trader.hasQueuedUnits()) | if (!this.hasTradeRoute() || queues.trader.hasQueuedUnits()) | ||||
return; | return; | ||||
let numTraders = this.traders.length; | let numTraders = this.traders.length; | ||||
let numSeaTraders = this.traders.filter(API3.Filters.byClass("Ship")).length; | let numSeaTraders = this.traders.filter(API3.Filters.byClass("Ship")).length; | ||||
let numLandTraders = numTraders - numSeaTraders; | let numLandTraders = numTraders - numSeaTraders; | ||||
// add traders already in training | // add traders already in training | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | PETRA.TradeManager.prototype.trainMoreTraders = function(gameState, queues) | ||||
if (!gameState.getTemplate(template)) | if (!gameState.getTemplate(template)) | ||||
{ | { | ||||
if (this.Config.debug > 0) | if (this.Config.debug > 0) | ||||
API3.warn("Petra error: trying to train " + template + " for civ " + | API3.warn("Petra error: trying to train " + template + " for civ " + | ||||
gameState.getPlayerCiv() + " but no template found."); | gameState.getPlayerCiv() + " but no template found."); | ||||
return; | return; | ||||
} | } | ||||
queues.trader.addPlan(new m.TrainingPlan(gameState, template, metadata, 1, 1)); | queues.trader.addPlan(new PETRA.TrainingPlan(gameState, template, metadata, 1, 1)); | ||||
}; | }; | ||||
m.TradeManager.prototype.updateTrader = function(gameState, ent) | PETRA.TradeManager.prototype.updateTrader = function(gameState, ent) | ||||
{ | { | ||||
if (ent.hasClass("Ship") && gameState.ai.playedTurn % 5 == 0 && | if (ent.hasClass("Ship") && gameState.ai.playedTurn % 5 == 0 && | ||||
!ent.unitAIState().startsWith("INDIVIDUAL.GATHER") && | !ent.unitAIState().startsWith("INDIVIDUAL.GATHER") && | ||||
m.gatherTreasure(gameState, ent, true)) | PETRA.gatherTreasure(gameState, ent, true)) | ||||
return; | return; | ||||
if (!this.hasTradeRoute() || !ent.isIdle() || !ent.position()) | if (!this.hasTradeRoute() || !ent.isIdle() || !ent.position()) | ||||
return; | return; | ||||
if (ent.getMetadata(PlayerID, "transport") !== undefined) | if (ent.getMetadata(PlayerID, "transport") !== undefined) | ||||
return; | return; | ||||
// TODO if the trader is idle and has workOrders, restore them to avoid losing the current gain | // TODO if the trader is idle and has workOrders, restore them to avoid losing the current gain | ||||
Engine.ProfileStart("Trade Manager"); | Engine.ProfileStart("Trade Manager"); | ||||
let access = ent.hasClass("Ship") ? m.getSeaAccess(gameState, ent) : m.getLandAccess(gameState, ent); | let access = ent.hasClass("Ship") ? PETRA.getSeaAccess(gameState, ent) : PETRA.getLandAccess(gameState, ent); | ||||
let route = this.checkRoutes(gameState, access); | let route = this.checkRoutes(gameState, access); | ||||
if (!route) | if (!route) | ||||
{ | { | ||||
// TODO try to garrison land trader inside merchant ship when only sea routes available | // TODO try to garrison land trader inside merchant ship when only sea routes available | ||||
if (this.Config.debug > 0) | if (this.Config.debug > 0) | ||||
API3.warn(" no available route for " + ent.genericName() + " " + ent.id()); | API3.warn(" no available route for " + ent.genericName() + " " + ent.id()); | ||||
Engine.ProfileStop(); | Engine.ProfileStop(); | ||||
return; | return; | ||||
Show All 16 Lines | PETRA.TradeManager.prototype.updateTrader = function(gameState, ent) | ||||
if (nearerSource) | if (nearerSource) | ||||
ent.tradeRoute(route.target, route.source); | ent.tradeRoute(route.target, route.source); | ||||
else | else | ||||
ent.tradeRoute(route.source, route.target); | ent.tradeRoute(route.source, route.target); | ||||
ent.setMetadata(PlayerID, "route", this.routeEntToId(route)); | ent.setMetadata(PlayerID, "route", this.routeEntToId(route)); | ||||
Engine.ProfileStop(); | Engine.ProfileStop(); | ||||
}; | }; | ||||
m.TradeManager.prototype.setTradingGoods = function(gameState) | PETRA.TradeManager.prototype.setTradingGoods = function(gameState) | ||||
{ | { | ||||
let tradingGoods = {}; | let tradingGoods = {}; | ||||
for (let res of Resources.GetCodes()) | for (let res of Resources.GetCodes()) | ||||
tradingGoods[res] = 0; | tradingGoods[res] = 0; | ||||
// first, try to anticipate future needs | // first, try to anticipate future needs | ||||
let stocks = gameState.ai.HQ.getTotalResourceLevel(gameState); | let stocks = gameState.ai.HQ.getTotalResourceLevel(gameState); | ||||
let mostNeeded = gameState.ai.HQ.pickMostNeededResources(gameState); | let mostNeeded = gameState.ai.HQ.pickMostNeededResources(gameState); | ||||
let wantedRates = gameState.ai.HQ.GetWantedGatherRates(gameState); | let wantedRates = gameState.ai.HQ.GetWantedGatherRates(gameState); | ||||
Show All 37 Lines | PETRA.TradeManager.prototype.setTradingGoods = function(gameState) | ||||
if (this.Config.debug > 2) | if (this.Config.debug > 2) | ||||
API3.warn(" trading goods set to " + uneval(tradingGoods)); | API3.warn(" trading goods set to " + uneval(tradingGoods)); | ||||
}; | }; | ||||
/** | /** | ||||
* Try to barter unneeded resources for needed resources. | * Try to barter unneeded resources for needed resources. | ||||
* only once per turn because the info is not updated within a turn | * only once per turn because the info is not updated within a turn | ||||
*/ | */ | ||||
m.TradeManager.prototype.performBarter = function(gameState) | PETRA.TradeManager.prototype.performBarter = function(gameState) | ||||
{ | { | ||||
let barterers = gameState.getOwnEntitiesByClass("BarterMarket", true).filter(API3.Filters.isBuilt()).toEntityArray(); | let barterers = gameState.getOwnEntitiesByClass("BarterMarket", true).filter(API3.Filters.isBuilt()).toEntityArray(); | ||||
if (barterers.length == 0) | if (barterers.length == 0) | ||||
return false; | return false; | ||||
// Available resources after account substraction | // Available resources after account substraction | ||||
let available = gameState.ai.queueManager.getAvailableResources(gameState); | let available = gameState.ai.queueManager.getAvailableResources(gameState); | ||||
let needs = gameState.ai.queueManager.currentNeeds(gameState); | let needs = gameState.ai.queueManager.currentNeeds(gameState); | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | if (this.Config.debug > 2) | ||||
" barterRate " + getBarterRate(barterPrices, bestToBuy, "food") + | " barterRate " + getBarterRate(barterPrices, bestToBuy, "food") + | ||||
" amount " + amount); | " amount " + amount); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}; | }; | ||||
m.TradeManager.prototype.checkEvents = function(gameState, events) | PETRA.TradeManager.prototype.checkEvents = function(gameState, events) | ||||
{ | { | ||||
// check if one market from a traderoute is renamed, change the route accordingly | // check if one market from a traderoute is renamed, change the route accordingly | ||||
for (let evt of events.EntityRenamed) | for (let evt of events.EntityRenamed) | ||||
{ | { | ||||
let ent = gameState.getEntityById(evt.newentity); | let ent = gameState.getEntityById(evt.newentity); | ||||
if (!ent || !ent.hasClass("Market")) | if (!ent || !ent.hasClass("Market")) | ||||
continue; | continue; | ||||
for (let trader of this.traders.values()) | for (let trader of this.traders.values()) | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | PETRA.TradeManager.prototype.checkEvents = function(gameState, events) | ||||
{ | { | ||||
this.activateProspection(gameState); | this.activateProspection(gameState); | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
}; | }; | ||||
m.TradeManager.prototype.activateProspection = function(gameState) | PETRA.TradeManager.prototype.activateProspection = function(gameState) | ||||
{ | { | ||||
this.routeProspection = true; | this.routeProspection = true; | ||||
gameState.ai.HQ.buildManager.setBuildable(gameState.applyCiv("structures/{civ}_market")); | gameState.ai.HQ.buildManager.setBuildable(gameState.applyCiv("structures/{civ}_market")); | ||||
gameState.ai.HQ.buildManager.setBuildable(gameState.applyCiv("structures/{civ}_dock")); | gameState.ai.HQ.buildManager.setBuildable(gameState.applyCiv("structures/{civ}_dock")); | ||||
}; | }; | ||||
/** | /** | ||||
* fills the best trade route in this.tradeRoute and the best potential route in this.potentialTradeRoute | * fills the best trade route in this.tradeRoute and the best potential route in this.potentialTradeRoute | ||||
* If an index is given, it returns the best route with this index or the best land route if index is a land index | * If an index is given, it returns the best route with this index or the best land route if index is a land index | ||||
*/ | */ | ||||
m.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) | PETRA.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) | ||||
{ | { | ||||
let market1 = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()); | let market1 = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()); | ||||
let market2 = gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()); | let market2 = gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()); | ||||
if (market1.length + market2.length < 2) // We have to wait ... markets will be built soon | if (market1.length + market2.length < 2) // We have to wait ... markets will be built soon | ||||
{ | { | ||||
this.tradeRoute = undefined; | this.tradeRoute = undefined; | ||||
this.potentialTradeRoute = undefined; | this.potentialTradeRoute = undefined; | ||||
return false; | return false; | ||||
Show All 9 Lines | PETRA.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) | ||||
let mapSize = gameState.sharedScript.mapSize; | let mapSize = gameState.sharedScript.mapSize; | ||||
let traderTemplatesGains = gameState.getTraderTemplatesGains(); | let traderTemplatesGains = gameState.getTraderTemplatesGains(); | ||||
for (let m1 of market1.values()) | for (let m1 of market1.values()) | ||||
{ | { | ||||
if (!m1.position()) | if (!m1.position()) | ||||
continue; | continue; | ||||
let access1 = m.getLandAccess(gameState, m1); | let access1 = PETRA.getLandAccess(gameState, m1); | ||||
let sea1 = m1.hasClass("NavalMarket") ? m.getSeaAccess(gameState, m1) : undefined; | let sea1 = m1.hasClass("NavalMarket") ? PETRA.getSeaAccess(gameState, m1) : undefined; | ||||
for (let m2 of market2.values()) | for (let m2 of market2.values()) | ||||
{ | { | ||||
if (onlyOurs && m1.id() >= m2.id()) | if (onlyOurs && m1.id() >= m2.id()) | ||||
continue; | continue; | ||||
if (!m2.position()) | if (!m2.position()) | ||||
continue; | continue; | ||||
let access2 = m.getLandAccess(gameState, m2); | let access2 = PETRA.getLandAccess(gameState, m2); | ||||
let sea2 = m2.hasClass("NavalMarket") ? m.getSeaAccess(gameState, m2) : undefined; | let sea2 = m2.hasClass("NavalMarket") ? PETRA.getSeaAccess(gameState, m2) : undefined; | ||||
let land = access1 == access2 ? access1 : undefined; | let land = access1 == access2 ? access1 : undefined; | ||||
let sea = sea1 && sea1 == sea2 ? sea1 : undefined; | let sea = sea1 && sea1 == sea2 ? sea1 : undefined; | ||||
if (!land && !sea) | if (!land && !sea) | ||||
continue; | continue; | ||||
if (land && m.isLineInsideEnemyTerritory(gameState, m1.position(), m2.position())) | if (land && PETRA.isLineInsideEnemyTerritory(gameState, m1.position(), m2.position())) | ||||
continue; | continue; | ||||
let gainMultiplier; | let gainMultiplier; | ||||
if (land && traderTemplatesGains.landGainMultiplier) | if (land && traderTemplatesGains.landGainMultiplier) | ||||
gainMultiplier = traderTemplatesGains.landGainMultiplier; | gainMultiplier = traderTemplatesGains.landGainMultiplier; | ||||
else if (sea && traderTemplatesGains.navalGainMultiplier) | else if (sea && traderTemplatesGains.navalGainMultiplier) | ||||
gainMultiplier = traderTemplatesGains.navalGainMultiplier; | gainMultiplier = traderTemplatesGains.navalGainMultiplier; | ||||
else | else | ||||
continue; | continue; | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | PETRA.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) | ||||
if (this.Config.chat) | if (this.Config.chat) | ||||
{ | { | ||||
let owner = this.tradeRoute.source.owner(); | let owner = this.tradeRoute.source.owner(); | ||||
if (owner == PlayerID) | if (owner == PlayerID) | ||||
owner = this.tradeRoute.target.owner(); | owner = this.tradeRoute.target.owner(); | ||||
if (owner != PlayerID && !this.warnedAllies[owner]) | if (owner != PlayerID && !this.warnedAllies[owner]) | ||||
{ // Warn an ally that we have a trade route with him | { // Warn an ally that we have a trade route with him | ||||
m.chatNewTradeRoute(gameState, owner); | PETRA.chatNewTradeRoute(gameState, owner); | ||||
this.warnedAllies[owner] = true; | this.warnedAllies[owner] = true; | ||||
} | } | ||||
} | } | ||||
if (accessIndex) | if (accessIndex) | ||||
{ | { | ||||
if (bestIndex.gain > 0) | if (bestIndex.gain > 0) | ||||
return bestIndex; | return bestIndex; | ||||
else if (gameState.ai.accessibility.regionType[accessIndex] == "land" && bestLand.gain > 0) | else if (gameState.ai.accessibility.regionType[accessIndex] == "land" && bestLand.gain > 0) | ||||
return bestLand; | return bestLand; | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
}; | }; | ||||
/** Called when a market was built or destroyed, and checks if trader orders should be changed */ | /** Called when a market was built or destroyed, and checks if trader orders should be changed */ | ||||
m.TradeManager.prototype.checkTrader = function(gameState, ent) | PETRA.TradeManager.prototype.checkTrader = function(gameState, ent) | ||||
{ | { | ||||
let presentRoute = ent.getMetadata(PlayerID, "route"); | let presentRoute = ent.getMetadata(PlayerID, "route"); | ||||
if (!presentRoute) | if (!presentRoute) | ||||
return; | return; | ||||
if (!ent.position()) | if (!ent.position()) | ||||
{ | { | ||||
// This trader is garrisoned, we will decide later (when ungarrisoning) what to do | // This trader is garrisoned, we will decide later (when ungarrisoning) what to do | ||||
ent.setMetadata(PlayerID, "route", undefined); | ent.setMetadata(PlayerID, "route", undefined); | ||||
return; | return; | ||||
} | } | ||||
let access = ent.hasClass("Ship") ? m.getSeaAccess(gameState, ent) : m.getLandAccess(gameState, ent); | let access = ent.hasClass("Ship") ? PETRA.getSeaAccess(gameState, ent) : PETRA.getLandAccess(gameState, ent); | ||||
let possibleRoute = this.checkRoutes(gameState, access); | let possibleRoute = this.checkRoutes(gameState, access); | ||||
// Warning: presentRoute is from metadata, so contains entity ids | // Warning: presentRoute is from metadata, so contains entity ids | ||||
if (!possibleRoute || | if (!possibleRoute || | ||||
possibleRoute.source.id() != presentRoute.source && possibleRoute.source.id() != presentRoute.target || | possibleRoute.source.id() != presentRoute.source && possibleRoute.source.id() != presentRoute.target || | ||||
possibleRoute.target.id() != presentRoute.source && possibleRoute.target.id() != presentRoute.target) | possibleRoute.target.id() != presentRoute.source && possibleRoute.target.id() != presentRoute.target) | ||||
{ | { | ||||
// Trader will be assigned in updateTrader | // Trader will be assigned in updateTrader | ||||
ent.setMetadata(PlayerID, "route", undefined); | ent.setMetadata(PlayerID, "route", undefined); | ||||
if (!possibleRoute && !ent.hasClass("Ship")) | if (!possibleRoute && !ent.hasClass("Ship")) | ||||
{ | { | ||||
let closestBase = m.getBestBase(gameState, ent, true); | let closestBase = PETRA.getBestBase(gameState, ent, true); | ||||
if (closestBase.accessIndex == access) | if (closestBase.accessIndex == access) | ||||
{ | { | ||||
let closestBasePos = closestBase.anchor.position(); | let closestBasePos = closestBase.anchor.position(); | ||||
ent.moveToRange(closestBasePos[0], closestBasePos[1], 0, 15); | ent.moveToRange(closestBasePos[0], closestBasePos[1], 0, 15); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
ent.stopMoving(); | ent.stopMoving(); | ||||
} | } | ||||
}; | }; | ||||
m.TradeManager.prototype.prospectForNewMarket = function(gameState, queues) | PETRA.TradeManager.prototype.prospectForNewMarket = function(gameState, queues) | ||||
{ | { | ||||
if (queues.economicBuilding.hasQueuedUnitsWithClass("Market") || queues.dock.hasQueuedUnitsWithClass("Market")) | if (queues.economicBuilding.hasQueuedUnitsWithClass("Market") || queues.dock.hasQueuedUnitsWithClass("Market")) | ||||
return; | return; | ||||
if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_market")) | if (!gameState.ai.HQ.canBuild(gameState, "structures/{civ}_market")) | ||||
return; | return; | ||||
if (!gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).hasEntities() && | if (!gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).hasEntities() && | ||||
!gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).hasEntities()) | !gameState.updatingCollection("diplo-ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).hasEntities()) | ||||
return; | return; | ||||
Show All 21 Lines | if (this.potentialTradeRoute) | ||||
marketPos[3] + " instead of the present " + this.potentialTradeRoute.gain); | marketPos[3] + " instead of the present " + this.potentialTradeRoute.gain); | ||||
else | else | ||||
API3.warn("turn " + gameState.ai.playedTurn + "we could have a first route with gain " + | API3.warn("turn " + gameState.ai.playedTurn + "we could have a first route with gain " + | ||||
marketPos[3]); | marketPos[3]); | ||||
} | } | ||||
if (!this.tradeRoute) | if (!this.tradeRoute) | ||||
gameState.ai.queueManager.changePriority("economicBuilding", 2*this.Config.priorities.economicBuilding); | gameState.ai.queueManager.changePriority("economicBuilding", 2*this.Config.priorities.economicBuilding); | ||||
let plan = new m.ConstructionPlan(gameState, "structures/{civ}_market"); | let plan = new PETRA.ConstructionPlan(gameState, "structures/{civ}_market"); | ||||
if (!this.tradeRoute) | if (!this.tradeRoute) | ||||
plan.queueToReset = "economicBuilding"; | plan.queueToReset = "economicBuilding"; | ||||
queues.economicBuilding.addPlan(plan); | queues.economicBuilding.addPlan(plan); | ||||
}; | }; | ||||
m.TradeManager.prototype.isNewMarketWorth = function(expectedGain) | PETRA.TradeManager.prototype.isNewMarketWorth = function(expectedGain) | ||||
{ | { | ||||
if (expectedGain < this.minimalGain) | if (expectedGain < this.minimalGain) | ||||
return false; | return false; | ||||
if (this.potentialTradeRoute && expectedGain < 2*this.potentialTradeRoute.gain && | if (this.potentialTradeRoute && expectedGain < 2*this.potentialTradeRoute.gain && | ||||
expectedGain < this.potentialTradeRoute.gain + 20) | expectedGain < this.potentialTradeRoute.gain + 20) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
}; | }; | ||||
m.TradeManager.prototype.update = function(gameState, events, queues) | PETRA.TradeManager.prototype.update = function(gameState, events, queues) | ||||
{ | { | ||||
if (gameState.ai.HQ.canBarter) | if (gameState.ai.HQ.canBarter) | ||||
this.performBarter(gameState); | this.performBarter(gameState); | ||||
if (this.Config.difficulty <= 1) | if (this.Config.difficulty <= 1) | ||||
return; | return; | ||||
if (this.checkEvents(gameState, events)) // true if one market was built or destroyed | if (this.checkEvents(gameState, events)) // true if one market was built or destroyed | ||||
Show All 12 Lines | if (this.tradeRoute) | ||||
if (gameState.ai.playedTurn % 60 == 0) | if (gameState.ai.playedTurn % 60 == 0) | ||||
this.setTradingGoods(gameState); | this.setTradingGoods(gameState); | ||||
} | } | ||||
if (this.routeProspection) | if (this.routeProspection) | ||||
this.prospectForNewMarket(gameState, queues); | this.prospectForNewMarket(gameState, queues); | ||||
}; | }; | ||||
m.TradeManager.prototype.routeEntToId = function(route) | PETRA.TradeManager.prototype.routeEntToId = function(route) | ||||
{ | { | ||||
if (!route) | if (!route) | ||||
return undefined; | return undefined; | ||||
let ret = {}; | let ret = {}; | ||||
for (let key in route) | for (let key in route) | ||||
{ | { | ||||
if (key == "source" || key == "target") | if (key == "source" || key == "target") | ||||
{ | { | ||||
if (!route[key]) | if (!route[key]) | ||||
return undefined; | return undefined; | ||||
ret[key] = route[key].id(); | ret[key] = route[key].id(); | ||||
} | } | ||||
else | else | ||||
ret[key] = route[key]; | ret[key] = route[key]; | ||||
} | } | ||||
return ret; | return ret; | ||||
}; | }; | ||||
m.TradeManager.prototype.routeIdToEnt = function(gameState, route) | PETRA.TradeManager.prototype.routeIdToEnt = function(gameState, route) | ||||
{ | { | ||||
if (!route) | if (!route) | ||||
return undefined; | return undefined; | ||||
let ret = {}; | let ret = {}; | ||||
for (let key in route) | for (let key in route) | ||||
{ | { | ||||
if (key == "source" || key == "target") | if (key == "source" || key == "target") | ||||
{ | { | ||||
ret[key] = gameState.getEntityById(route[key]); | ret[key] = gameState.getEntityById(route[key]); | ||||
if (!ret[key]) | if (!ret[key]) | ||||
return undefined; | return undefined; | ||||
} | } | ||||
else | else | ||||
ret[key] = route[key]; | ret[key] = route[key]; | ||||
} | } | ||||
return ret; | return ret; | ||||
}; | }; | ||||
m.TradeManager.prototype.Serialize = function() | PETRA.TradeManager.prototype.Serialize = function() | ||||
{ | { | ||||
return { | return { | ||||
"tradeRoute": this.routeEntToId(this.tradeRoute), | "tradeRoute": this.routeEntToId(this.tradeRoute), | ||||
"potentialTradeRoute": this.routeEntToId(this.potentialTradeRoute), | "potentialTradeRoute": this.routeEntToId(this.potentialTradeRoute), | ||||
"routeProspection": this.routeProspection, | "routeProspection": this.routeProspection, | ||||
"targetNumTraders": this.targetNumTraders, | "targetNumTraders": this.targetNumTraders, | ||||
"warnedAllies": this.warnedAllies | "warnedAllies": this.warnedAllies | ||||
}; | }; | ||||
}; | }; | ||||
m.TradeManager.prototype.Deserialize = function(gameState, data) | PETRA.TradeManager.prototype.Deserialize = function(gameState, data) | ||||
{ | { | ||||
for (let key in data) | for (let key in data) | ||||
{ | { | ||||
if (key == "tradeRoute" || key == "potentialTradeRoute") | if (key == "tradeRoute" || key == "potentialTradeRoute") | ||||
this[key] = this.routeIdToEnt(gameState, data[key]); | this[key] = this.routeIdToEnt(gameState, data[key]); | ||||
else | else | ||||
this[key] = data[key]; | this[key] = data[key]; | ||||
} | } | ||||
}; | }; | ||||
return m; | |||||
}(PETRA); |
Wildfire Games · Phabricator