Ticket #3277: trading_v2.patch
File trading_v2.patch, 10.6 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 1664 { 1665 result = { "type": "set first" }; 1666 } 1667 else if (!secondMarket) 1668 { 1665 1669 result = { 1670 "type": "set second", 1671 "gain": cmpEntityTrader.CalculateGain(firstMarket, data.target), 1672 }; 1673 } 1674 else if (data.target === firstMarket) 1675 { 1676 result = { 1666 1677 "type": "is first", 1667 1678 "hasBothMarkets": cmpEntityTrader.HasBothMarkets() 1668 1679 }; … … 1676 1687 "gain": cmpEntityTrader.GetGain(), 1677 1688 }; 1678 1689 } 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 1690 else 1691 1691 { 1692 1692 // 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 and return true 23 if (trader in this.tradeEntities) 24 return true; 25 26 // If the entity has not been registered for this market, 27 // add it to the list of traders 28 this.tradeEntities.push(trader); 29 30 // If everything was correct return true 31 return true; 32 }; 33 34 Market.prototype.OnDestroy = function() 35 { 36 this.NotifySubscribedTraders(); 37 }; 38 39 Market.prototype.OnOwnershipChanged = function(msg) 40 { 41 if (msg.to != -1 && msg.from != -1) 42 { 43 let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 44 let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.to), IID_Player); 45 46 // If the new owner is an not an ally the trader should display a behavior 47 // similar when the market is destroyed. 48 if(!cmpPlayer.IsAlly(msg.from)) 49 { 50 this.NotifySubscribedTraders(); 51 } 52 } 53 }; 54 55 Market.prototype.OnDiplomacyChanged = function(msg) 56 { 57 let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); 58 let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager); 59 let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.player), IID_Player); 60 61 // If the new diplomatic position of the player is not an ally return to 62 // respective ally markets 63 if (!cmpPlayer.IsAlly(cmpOwnership.GetOwner())) 64 { 65 this.NotifySubscribedTraders(); 66 } 67 } 68 69 /** 70 Notifies traders subscribed to this market to change their trading behavior 71 */ 72 Market.prototype.NotifySubscribedTraders = function() { 73 74 let cmpTraderUnitAI = undefined; 75 for(let i=0, l=this.tradeEntities.length; i<l; ++i) 76 { 77 if (this.tradeEntities[i]) 78 { 79 cmpTraderUnitAI = Engine.QueryInterface(this.tradeEntities[i], IID_UnitAI); 80 if (cmpTraderUnitAI) 81 cmpTraderUnitAI.InvalidateTradingMarket(this.entity); 82 } 83 } 84 } 85 86 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; … … 63 63 gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain); 64 64 } 65 65 } 66 66 67 67 return gain; 68 68 }; 69 69 … … 280 280 this.gain = this.CalculateGain(this.firstMarket, this.secondMarket); 281 281 }; 282 282 283 Trader.prototype.InvalidateFirstMarket = function() 284 { 285 this.firstMarket = INVALID_ENTITY; 286 } 287 288 Trader.prototype.InvalidateSecondMarket = function() 289 { 290 this.secondMarket = INVALID_ENTITY; 291 } 292 283 293 Engine.RegisterComponentType(IID_Trader, "Trader", Trader); -
binaries/data/mods/public/simulation/components/UnitAI.js
2746 2746 let dropsiteTypes = cmpResourceDropsite.GetTypes(); 2747 2747 cmpResourceGatherer.CommitResources(dropsiteTypes); 2748 2748 this.SetGathererAnimationOverride(); 2749 } 2749 } 2750 2750 2751 2751 // We finished building it. 2752 2752 // Switch to the next order (if any) … … 3458 3458 var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); 3459 3459 if (this.losRangeQuery) 3460 3460 this.SetupRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losRangeQuery)); 3461 3461 3462 3462 if (this.IsHealer() && this.losHealRangeQuery) 3463 3463 this.SetupHealRangeQuery(cmpRangeManager.IsActiveQueryEnabled(this.losHealRangeQuery)); 3464 3464 }; … … 5163 5163 return; 5164 5164 } 5165 5165 5166 varmarketsChanged = this.SetTargetMarket(target, source);5166 let marketsChanged = this.SetTargetMarket(target, source); 5167 5167 if (!marketsChanged) 5168 5168 return; 5169 5169 5170 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5171 if (cmpTrader.HasBothMarkets()) 5170 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5171 let cmpMarket = Engine.QueryInterface(target, IID_Market); 5172 5173 if (cmpMarket.SubscribeTrader(this.entity) && cmpTrader.HasBothMarkets()) 5172 5174 { 5173 vardata = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };5175 let data = { "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false }; 5174 5176 5175 5177 if (this.expectedRoute) 5176 5178 { … … 5182 5184 if (this.IsFormationController()) 5183 5185 { 5184 5186 this.CallMemberFunction("AddOrder", ["Trade", data, queued]); 5185 varcmpFormation = Engine.QueryInterface(this.entity, IID_Formation);5187 let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); 5186 5188 if (cmpFormation) 5187 5189 cmpFormation.Disband(); 5188 5190 } … … 5230 5232 return ok; 5231 5233 }; 5232 5234 5235 /** 5236 * Makes a routing trade invalid sending the trader unit to the next 5237 * possible market. 5238 */ 5239 UnitAI.prototype.InvalidateTradingMarket = function(market) 5240 { 5241 // Find the entity inside the one of the list used in the market 5242 // 5243 // If the trader is found in the SourceMarket list then 5244 // send him to his target market. 5245 // 5246 // If the trader is found in the TargetMarket list then 5247 // send him to his source market 5248 // 5249 // If the source and target have been captured then stop 5250 // trading 5251 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5252 5253 if(!cmpTrader) 5254 return false; 5255 5256 if (!cmpTrader.GetFirstMarket() || !cmpTrader.GetSecondMarket()) 5257 { 5258 // At the moment of writing this this.StopTrading generate errors 5259 // because it calls this.FinishOrder but the order queue is empty in 5260 // certain conditions. If that error is fixed this two lines 5261 // should be changed to this.StopTrading() 5262 // 5263 // The reason of why the queue is empty is because MoveCompleted is 5264 // called when the two markets are destroyed, captured or change diplomacy stattus 5265 // and inside the method, this.FinishOrder is called cleaning the order queue. 5266 // This generates the problem that the trader finish its order but it doesn't stop 5267 // moving or trading. 5268 // 5269 // Here we stop moving and trading 5270 this.StopMoving(); 5271 cmpTrader.StopTrading(); 5272 } 5273 else if(cmpTrader.GetFirstMarket() === market) 5274 { 5275 cmpTrader.InvalidateFirstMarket(); 5276 this.MoveToMarket(cmpTrader.GetSecondMarket()); 5277 } 5278 else if(cmpTrader.GetSecondMarket() === market) 5279 { 5280 cmpTrader.InvalidateSecondMarket(); 5281 this.MoveToMarket(cmpTrader.GetFirstMarket()); 5282 } 5283 5284 return true; 5285 }; 5286 5233 5287 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket, nextMarket, nextFsmStateName) 5234 5288 { 5235 5289 if (!this.CanTrade(currentMarket)) … … 5272 5326 { 5273 5327 this.StopMoving(); 5274 5328 this.FinishOrder(); 5275 varcmpTrader = Engine.QueryInterface(this.entity, IID_Trader);5329 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5276 5330 cmpTrader.StopTrading(); 5277 5331 }; 5278 5332 -
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>