Ticket #3277: ticket3277.patch
File ticket3277.patch, 5.8 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Market.js
1 function Market() {} 2 3 Market.prototype.Schema = 4 "<empty/>"; 5 6 Market.prototype.Init = function() 7 { 8 this.traders = []; // list of traders with a route on this market 9 }; 10 11 Market.prototype.GetTraders = function() 12 { 13 return this.traders.slice(); 14 }; 15 16 Market.prototype.AddTrader = function(ent) 17 { 18 if (this.traders.indexOf(ent) != -1) 19 return; 20 this.traders.push(ent); 21 }; 22 23 Market.prototype.RemoveTrader = function(ent) 24 { 25 let index = this.traders.indexOf(ent); 26 if (index != -1) 27 this.traders.splice(index, 1); 28 }; 29 30 /* Check if all traders with a route on this market can still trade */ 31 Market.prototype.OnOwnershipChanged = function(msg) 32 { 33 for (let ent of this.traders) 34 { 35 let cmpTrader = Engine.QueryInterface(ent, IID_Trader); 36 if (!cmpTrader || (msg.to == -1 || !cmpTrader.CanTrade(this.entity))) 37 { 38 this.RemoveTrader(ent); 39 if (cmpTrader) 40 cmpTrader.RemoveMarket(this.entity); 41 } 42 } 43 }; 44 45 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 … … 69 69 // Return true if at least one of markets was changed. 70 70 Trader.prototype.SetTargetMarket = function(target, source) 71 71 { 72 // Check that target is a market 73 var cmpTargetIdentity = Engine.QueryInterface(target, IID_Identity); 74 if (!cmpTargetIdentity) 72 var cmpTargetMarket = Engine.QueryInterface(target, IID_Market); 73 if (!cmpTargetMarket) 75 74 return false; 76 if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))77 return false;78 75 79 76 if (source) 80 77 { 81 78 // Establish a trade route with both markets in one go. 82 cmpTargetIdentity = Engine.QueryInterface(source, IID_Identity);83 if (!cmp TargetIdentity)79 let cmpSourceMarket = Engine.QueryInterface(source, IID_Market); 80 if (!cmpSourceMarket) 84 81 return false; 85 if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket"))86 return false;87 82 this.markets = [source]; 83 cmpSourceMarket.AddTrader(this.entity); 88 84 } 89 85 if (this.markets.length >= 2) 90 86 { 91 87 // If we already have both markets - drop them 92 88 // and use the target as first market 89 for (let market of this.markets) 90 { 91 let cmpMarket = Engine.QueryInterface(market, IID_Market); 92 if (cmpMarket) 93 cmpMarket.RemoveTrader(this.entity); 94 } 93 95 this.index = 0; 94 96 this.markets = [target]; 97 cmpTargetMarket.AddTrader(this.entity); 95 98 } 96 99 else if (this.markets.length == 1) 97 100 { … … 103 106 { 104 107 this.index = 0; 105 108 this.markets.push(target); 109 cmpTargetMarket.AddTrader(this.entity); 106 110 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 107 111 } 108 112 } … … 112 116 // set the target as first market 113 117 this.index = 0; 114 118 this.markets = [target]; 119 cmpTargetMarket.AddTrader(this.entity); 115 120 } 116 121 // Drop carried goods if markets were changed 117 122 this.goods.amount = null; … … 242 247 return this.goods; 243 248 }; 244 249 250 /* Called when this trader can no more trade with the market */ 251 Trader.prototype.RemoveMarket = function(market) 252 { 253 let index = this.markets.indexOf(market); 254 if (index != -1) 255 this.markets.splice(index, 1); 256 }; 257 245 258 Trader.prototype.StopTrading = function() 246 259 { 260 for (let market of this.markets) 261 { 262 let cmpMarket = Engine.QueryInterface(market, IID_Market); 263 if (cmpMarket) 264 cmpMarket.RemoveTrader(this.entity); 265 } 247 266 this.index = -1; 248 267 this.markets = []; 249 268 // Drop carried goods -
binaries/data/mods/public/simulation/components/interfaces/Market.js
1 Engine.RegisterInterface("Market"); -
binaries/data/mods/public/simulation/templates/template_structure_economic_market.xml
32 32 <stone>25</stone> 33 33 <metal>25</metal> 34 34 </Loot> 35 <Market/> 35 36 <Obstruction> 36 37 <Static width="30.0" depth="26.0"/> 37 38 </Obstruction> -
binaries/data/mods/public/simulation/templates/template_structure_military_dock.xml
34 34 <stone>0</stone> 35 35 <metal>0</metal> 36 36 </Loot> 37 <Market/> 37 38 <Obstruction> 38 39 <Static width="18.0" depth="18.0"/> 39 40 </Obstruction>