Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/tradeManager.js
Show First 20 Lines • Show All 385 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
/** | /** | ||||
* 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) | m.TradeManager.prototype.checkRoutes = function(gameState, accessIndex) | ||||
{ | { | ||||
let market1 = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()).toEntityArray(); | let market1 = gameState.updatingCollection("OwnMarkets", API3.Filters.byClass("Market"), gameState.getOwnStructures()); | ||||
let market2 = gameState.updatingCollection("ExclusiveAllyMarkets", API3.Filters.byClass("Market"), gameState.getExclusiveAllyEntities()).toEntityArray(); | let market2 = gameState.updatingCollection("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; | ||||
} | } | ||||
if (!market2.length) | let onlyOurs = !market2.hasEntities(); | ||||
if (onlyOurs) | |||||
market2 = market1; | market2 = market1; | ||||
let candidate = { "gain": 0 }; | let candidate = { "gain": 0 }; | ||||
let potential = { "gain": 0 }; | let potential = { "gain": 0 }; | ||||
let bestIndex = { "gain": 0 }; | let bestIndex = { "gain": 0 }; | ||||
let bestLand = { "gain": 0 }; | let bestLand = { "gain": 0 }; | ||||
let traderTemplatesGains = gameState.getTraderTemplatesGains(); | let traderTemplatesGains = gameState.getTraderTemplatesGains(); | ||||
for (let m1 of market1) | for (let m1 of market1.values()) | ||||
{ | { | ||||
if (!m1.position()) | if (!m1.position()) | ||||
continue; | continue; | ||||
let access1 = m.getLandAccess(gameState, m1); | let access1 = m.getLandAccess(gameState, m1); | ||||
let sea1 = m1.hasClass("NavalMarket") ? m.getSeaAccess(gameState, m1) : undefined; | let sea1 = m1.hasClass("NavalMarket") ? m.getSeaAccess(gameState, m1) : undefined; | ||||
for (let m2 of market2) | for (let m2 of market2.values()) | ||||
{ | { | ||||
if (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 = m.getLandAccess(gameState, m2); | ||||
let sea2 = m2.hasClass("NavalMarket") ? m.getSeaAccess(gameState, m2) : undefined; | let sea2 = m2.hasClass("NavalMarket") ? m.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())) | |||||
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; | ||||
let gain = Math.round(API3.SquareVectorDistance(m1.position(), m2.position()) * gainMultiplier / 10000); | let gain = Math.round(API3.SquareVectorDistance(m1.position(), m2.position()) * gainMultiplier / 10000); | ||||
▲ Show 20 Lines • Show All 229 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator