Ticket #3277: trading_v3.patch
File trading_v3.patch, 10.5 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/GuiInterface.js
1660 1660 let firstMarket = cmpEntityTrader.GetFirstMarket(); 1661 1661 let secondMarket = cmpEntityTrader.GetSecondMarket(); 1662 1662 let result = null; 1663 if (data.target === firstMarket) 1663 if (!firstMarket) 1664 result = { "type": "set first" }; 1665 else if (!secondMarket) 1664 1666 { 1665 1667 result = { 1668 "type": "set second", 1669 "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target), 1670 }; 1671 } 1672 else if (data.target === firstMarket) 1673 { 1674 result = { 1666 1675 "type": "is first", 1667 1676 "hasBothMarkets": cmpEntityTrader.HasBothMarkets() 1668 1677 }; … … 1676 1685 "gain": cmpEntityTrader.GetGoods().amount, 1677 1686 }; 1678 1687 } 1679 else if (!firstMarket)1680 {1681 result = { "type": "set first" };1682 }1683 else if (!secondMarket)1684 {1685 result = {1686 "type": "set second",1687 "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target),1688 };1689 }1690 1688 else 1691 1689 { 1692 1690 // Else both markets are not null and target is different from them -
binaries/data/mods/public/simulation/components/Market.js
1 /** 2 @file Market.js 3 Component used for the management of market and trade units in the field 4 */ 5 function Market() {} 6 7 Market.prototype.Schema = "<a:component type='system'/><empty/>"; 8 9 Market.prototype.Init = function() 10 { 11 this.tradeEntities = []; // List of trade entities from which this market works as a target 12 }; 13 14 /** 15 Subscribes a new trader to this market. 16 @param {Object} trader - The trader object expected being subscribed inside the market. 17 */ 18 19 Market.prototype.SubscribeTrader = function(trader) 20 { 21 // First check that this trader has not been already registered inside the market. 22 // If that is the case then do nothing 23 if (!(trader in this.tradeEntities)) 24 // If the entity has not been registered for this market, 25 // add it to the list of traders 26 this.tradeEntities.push(trader); 27 }; 28 29 Market.prototype.OnDestroy = function() 30 { 31 this.NotifySubscribedTraders(); 32 }; 33 34 Market.prototype.OnOwnershipChanged = function(msg) 35 { 36 if (msg.to != -1 && msg.from != -1) 37 { 38 let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 39 let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.to), IID_Player); 40 41 // If the new owner is an not an ally the trader should display a behavior 42 // similar when the market is destroyed. 43 if(!cmpPlayer.IsAlly(msg.from) || !cmpPlayer.isNeutral(msg.from)) 44 this.NotifySubscribedTraders(); 45 } 46 }; 47 48 Market.prototype.OnDiplomacyChanged = function(msg) 49 { 50 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 51 let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 52 let cmpPlayer = undefined; 53 if(cmpPlayerManager) 54 cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.player), IID_Player); 55 56 if(!cmpOwnership || !cmpPlayer) 57 return; 58 59 // If the new diplomatic position of the player is not an ally return to 60 // respective ally markets 61 if (!cmpPlayer.IsAlly(cmpOwnership.GetOwner()) || !cmpPlayer.IsNeutral(cmpOwnership.GetOwner())) 62 this.NotifySubscribedTraders(); 63 } 64 65 /** 66 Notifies traders subscribed to this market to change their trading behavior 67 */ 68 Market.prototype.NotifySubscribedTraders = function() { 69 70 let cmpUnitAI = undefined; 71 for(let i=0, l=this.tradeEntities.length; i<l; ++i) 72 { 73 if (this.tradeEntities[i]) 74 { 75 cmpUnitAI = Engine.QueryInterface(this.tradeEntities[i], IID_UnitAI); 76 if (cmpUnitAI) 77 cmpUnitAI.InvalidateTradingMarket(this.entity); 78 } 79 } 80 } 81 82 Engine.RegisterComponentType(IID_Market, "Market", Market); -
binaries/data/mods/public/simulation/components/Trader.js
1 // See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many 2 // resources a trader gets 1 // See helpers/TraderGain.js for the CalculateTaderGain() function which works out how many 2 // resources a trader gets 3 3 4 4 // Additional gain for ships for each garrisoned trader, in percents 5 5 const GARRISONED_TRADER_ADDITION = 20; … … 61 61 gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain); 62 62 } 63 63 } 64 64 65 65 return gain; 66 66 }; 67 67 … … 273 273 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 274 274 }; 275 275 276 Trader.prototype.InvalidateFirstMarket = function() 277 { 278 this.firstMarket = INVALID_ENTITY; 279 } 280 281 Trader.prototype.InvalidateSecondMarket = function() 282 { 283 this.secondMarket = INVALID_ENTITY; 284 } 285 276 286 Engine.RegisterComponentType(IID_Trader, "Trader", Trader); -
binaries/data/mods/public/simulation/components/UnitAI.js
353 353 var needToMove = true; 354 354 var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); 355 355 if (this.lastShorelinePosition && cmpPosition && (this.lastShorelinePosition.x == cmpPosition.GetPosition().x) 356 356 && (this.lastShorelinePosition.z == cmpPosition.GetPosition().z)) 357 357 { 358 358 // we were already on the shoreline, and have not moved since 359 359 if (DistanceBetweenEntities(this.entity, this.order.data.target) < 50) … … 2724 2724 let dropsiteTypes = cmpResourceDropsite.GetTypes(); 2725 2725 cmpResourceGatherer.CommitResources(dropsiteTypes); 2726 2726 this.SetGathererAnimationOverride(); 2727 } 2727 } 2728 2728 2729 2729 // We finished building it. 2730 2730 // Switch to the next order (if any) … … 3436 3436 var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); 3437 3437 if (this.losRangeQuery) 3438 3438 this.SetupRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losRangeQuery)); 3439 3439 3440 3440 if (this.IsHealer() && this.losHealRangeQuery) 3441 3441 this.SetupHealRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losHealRangeQuery)); 3442 3442 }; … … 5141 5141 return; 5142 5142 } 5143 5143 5144 varmarketsChanged = this.SetTargetMarket(target, source);5144 let marketsChanged = this.SetTargetMarket(target, source); 5145 5145 if (!marketsChanged) 5146 5146 return; 5147 5147 5148 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5148 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5149 let cmpMarket = Engine.QueryInterface(target, IID_Market); 5150 if(!cmpTrader || !cmpMarket) 5151 return; 5152 5153 cmpMarket.SubscribeTrader(this.entity); 5154 5149 5155 if (cmpTrader.HasBothMarkets()) 5150 5156 { 5151 5157 let data = { … … 5164 5170 if (this.IsFormationController()) 5165 5171 { 5166 5172 this.CallMemberFunction("AddOrder", ["Trade", data, queued]); 5167 varcmpFormation = Engine.QueryInterface(this.entity, IID_Formation);5173 let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); 5168 5174 if (cmpFormation) 5169 5175 cmpFormation.Disband(); 5170 5176 } … … 5212 5218 return ok; 5213 5219 }; 5214 5220 5221 /** 5222 * Makes a routing trade invalid sending the trader unit to the next 5223 * possible market. 5224 */ 5225 UnitAI.prototype.InvalidateTradingMarket = function(market) 5226 { 5227 // Find the entity inside the one of the markets in the trader 5228 // 5229 // If the market is found as the first market then send the trader to the 5230 // second market. 5231 // 5232 // If the market is found in the second market then send the trader to the 5233 // first market 5234 // 5235 // If the source and target have been captured then stop 5236 // trading 5237 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5238 5239 if(cmpTrader) 5240 { 5241 if (!cmpTrader.GetFirstMarket() || !cmpTrader.GetSecondMarket()) 5242 this.StopTrading(); 5243 else if(cmpTrader.GetFirstMarket() === market) 5244 { 5245 cmpTrader.InvalidateFirstMarket(); 5246 this.MoveToMarket(cmpTrader.GetSecondMarket()); 5247 } 5248 else if(cmpTrader.GetSecondMarket() === market) 5249 { 5250 cmpTrader.InvalidateSecondMarket(); 5251 this.MoveToMarket(cmpTrader.GetFirstMarket()); 5252 } 5253 } 5254 }; 5255 5215 5256 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) 5216 5257 { 5217 5258 if (!this.CanTrade(currentMarket)) … … 5228 5269 } 5229 5270 5230 5271 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5272 if(!cmpTrader) 5273 { 5274 this.StopTrading(); 5275 return; 5276 } 5277 5231 5278 cmpTrader.PerformTrade(currentMarket); 5232 5279 if (!cmpTrader.GetGoods().amount.traderGain) 5233 5280 { … … 5255 5302 UnitAI.prototype.StopTrading = function() 5256 5303 { 5257 5304 this.StopMoving(); 5258 this.FinishOrder(); 5259 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5305 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5260 5306 cmpTrader.StopTrading(); 5261 5307 }; 5262 5308 -
binaries/data/mods/public/simulation/components/interfaces/Market.js
1 Engine.RegisterInterface("Market"); -
binaries/data/mods/public/simulation/templates/template_structure_economic_market.xml
63 63 <Vision> 64 64 <Range>32</Range> 65 65 </Vision> 66 <Market/> 66 67 <VisualActor> 67 68 <FoundationActor>structures/fndn_5x5.xml</FoundationActor> 68 69 </VisualActor> -
binaries/data/mods/public/simulation/templates/template_structure_military_dock.xml
74 74 <Vision> 75 75 <Range>40</Range> 76 76 </Vision> 77 <Market/> 77 78 <VisualActor> 78 79 <FoundationActor>structures/fndn_4x4_dock.xml</FoundationActor> 79 80 </VisualActor>