From b062fbef175be6d7505ef2a71224e8e2bb289820 Mon Sep 17 00:00:00 2001 From: leper Date: Fri, 24 Feb 2017 21:59:23 +0000 Subject: [PATCH] Stop relying on the internal structure of the current Trader component. Get the required information by returning it in the proper places. Also clean up some pointless modulo operations. This code is pulled out of the Silk Road code (refs #4314). Reviewed By: mimo Differential Revision: https://code.wildfiregames.com/D161 This was SVN commit r19243. --- .../data/mods/public/simulation/components/Trader.js | 10 ++++++---- .../data/mods/public/simulation/components/UnitAI.js | 5 ++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/Trader.js b/binaries/data/mods/public/simulation/components/Trader.js index 84ce02b399..2fccd9f5b2 100644 --- a/binaries/data/mods/public/simulation/components/Trader.js +++ b/binaries/data/mods/public/simulation/components/Trader.js @@ -164,15 +164,15 @@ Trader.prototype.CanTrade = function(target) Trader.prototype.PerformTrade = function(currentMarket) { - let previousMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; + let previousMarket = this.markets[this.index]; if (previousMarket != currentMarket) // Inconsistent markets { this.goods.amount = null; - return; + return INVALID_ENTITY; } this.index = ++this.index % this.markets.length; - let nextMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; + let nextMarket = this.markets[this.index]; if (this.goods.amount && this.goods.amount.traderGain) { @@ -209,11 +209,13 @@ Trader.prototype.PerformTrade = function(currentMarket) let cmpPlayer = QueryOwnerInterface(this.entity); if (!cmpPlayer) - return; + return INVALID_ENTITY; this.goods.type = cmpPlayer.GetNextTradingGoods(); this.goods.amount = this.CalculateGain(currentMarket, nextMarket); this.goods.origin = currentMarket; + + return nextMarket; }; Trader.prototype.GetGoods = function() diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index ade2bddb5f..fa21dd4f78 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -5357,15 +5357,14 @@ UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) } let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); - cmpTrader.PerformTrade(currentMarket); + let nextMarket = cmpTrader.PerformTrade(currentMarket); let amount = cmpTrader.GetGoods().amount; - if (!amount || !amount.traderGain) + if (!nextMarket || !amount || !amount.traderGain) { this.StopTrading(); return; } - let nextMarket = cmpTrader.markets[cmpTrader.index]; this.order.data.target = nextMarket; if (this.order.data.route && this.order.data.route.length)