Ticket #3812: 3812.diff
File 3812.diff, 13.3 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/session/utility_functions.js
function getRankIconSprite(entState) 72 72 /** 73 73 * Returns a message with the details of the trade gain. 74 74 */ 75 75 function getTradingTooltip(gain) 76 76 { 77 if (!gain) 78 return ""; 77 79 78 80 var playerID = Engine.GetPlayerID(); 79 81 var simState = GetSimState(); 80 82 81 83 var gainString = gain.traderGain; -
binaries/data/mods/public/simulation/components/GuiInterface.js
GuiInterface.prototype.GetTradingDetails 1665 1665 result = { 1666 1666 "type": "is first", 1667 1667 "hasBothMarkets": cmpEntityTrader.HasBothMarkets() 1668 1668 }; 1669 1669 if (cmpEntityTrader.HasBothMarkets()) 1670 result.gain = cmpEntityTrader.GetG ain();1670 result.gain = cmpEntityTrader.GetGoods().amount; 1671 1671 } 1672 1672 else if (data.target === secondMarket) 1673 1673 { 1674 1674 result = { 1675 1675 "type": "is second", 1676 "gain": cmpEntityTrader.GetG ain(),1676 "gain": cmpEntityTrader.GetGoods().amount, 1677 1677 }; 1678 1678 } 1679 1679 else if (!firstMarket) 1680 1680 { 1681 1681 result = { "type": "set first" }; -
binaries/data/mods/public/simulation/components/Trader.js
Trader.prototype.Schema = 19 19 "<ref name='positiveDecimal'/>" + 20 20 "</element>"; 21 21 22 22 Trader.prototype.Init = function() 23 23 { 24 this.firstMarket = INVALID_ENTITY; 25 this.secondMarket = INVALID_ENTITY; 26 // Gain from one pass between markets 27 this.gain = null; 24 this.markets = []; 25 this.index = -1; 28 26 // Selected resource for trading 29 27 this.requiredGoods = undefined; 30 28 // Currently carried goods 31 29 this.goods = { "type": null, "amount": null, "origin": null }; 32 30 }; 33 31 34 Trader.prototype.CalculateGain = function( firstMarket, secondMarket)32 Trader.prototype.CalculateGain = function(currentMarket, nextMarket) 35 33 { 36 var gain = CalculateTraderGain(firstMarket, secondMarket, this.template, this.entity);34 let gain = CalculateTraderGain(currentMarket, nextMarket, this.template, this.entity); 37 35 if (!gain) // One of our markets must have been destroyed 38 36 return null; 39 37 40 38 // For ship increase gain for each garrisoned trader 41 39 // Calculate this here to save passing unnecessary stuff into the CalculateTraderGain function … … Trader.prototype.CalculateGain = functio 65 63 } 66 64 67 65 return gain; 68 66 }; 69 67 70 Trader.prototype.GetGain = function()71 {72 return this.gain;73 };74 75 68 // Set target as target market. 76 69 // Return true if at least one of markets was changed. 77 70 Trader.prototype.SetTargetMarket = function(target, source) 78 71 { 79 72 // Check that target is a market … … Trader.prototype.SetTargetMarket = funct 89 82 cmpTargetIdentity = Engine.QueryInterface(source, IID_Identity); 90 83 if (!cmpTargetIdentity) 91 84 return false; 92 85 if (!cmpTargetIdentity.HasClass("Market") && !cmpTargetIdentity.HasClass("NavalMarket")) 93 86 return false; 94 95 this.firstMarket = source; 96 this.secondMarket = INVALID_ENTITY; 87 this.markets = [source]; 97 88 } 98 99 if (this.secondMarket) 89 if (this.markets.length >= 2) 100 90 { 101 91 // If we already have both markets - drop them 102 92 // and use the target as first market 103 this. firstMarket = target;104 this. secondMarket = INVALID_ENTITY;93 this.index = 0; 94 this.markets = [target]; 105 95 } 106 else if (this. firstMarket)96 else if (this.markets.length == 1) 107 97 { 108 98 // If we have only one market and target is different from it, 109 99 // set the target as second one 110 if (target == this. firstMarket)100 if (target == this.markets[0]) 111 101 marketsChanged = false; 112 102 else 113 { 114 this. secondMarket = target;115 this.g ain = this.CalculateGain(this.firstMarket, this.secondMarket);103 { this.index = 0; 104 this.markets.push(target); 105 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 116 106 } 117 107 } 118 108 else 119 109 { 120 110 // Else we don't have target markets at all, 121 111 // set the target as first market 122 this.firstMarket = target; 112 this.index = 0; 113 this.markets = [target]; 123 114 } 124 115 if (marketsChanged) 125 116 { 126 117 // Drop carried goods 127 118 this.goods.amount = null; … … Trader.prototype.SetTargetMarket = funct 129 120 return marketsChanged; 130 121 }; 131 122 132 123 Trader.prototype.GetFirstMarket = function() 133 124 { 134 return this. firstMarket;125 return this.markets[0] || null; 135 126 }; 136 127 137 128 Trader.prototype.GetSecondMarket = function() 138 129 { 139 return this. secondMarket;130 return this.markets[1] || null; 140 131 }; 141 132 142 133 Trader.prototype.HasBothMarkets = function() 143 134 { 144 return this. firstMarket && this.secondMarket;135 return this.markets.length >= 2; 145 136 }; 146 137 147 138 Trader.prototype.GetRequiredGoods = function() 148 139 { 149 140 return this.requiredGoods; … … Trader.prototype.CanTrade = function(tar 184 175 return true; 185 176 }; 186 177 187 178 Trader.prototype.PerformTrade = function(currentMarket) 188 179 { 180 let previousMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; 181 182 this.index = ++this.index % this.markets.length; 183 184 let nextMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; 185 186 if (previousMarket != currentMarket) 187 warn("Markets are not matching."); 188 189 189 if (this.goods.amount && this.goods.amount.traderGain) 190 190 { 191 191 var cmpPlayer = QueryOwnerInterface(this.entity); 192 192 if (cmpPlayer) 193 193 cmpPlayer.AddResource(this.goods.type, this.goods.amount.traderGain); … … Trader.prototype.PerformTrade = function 196 196 if (cmpStatisticsTracker) 197 197 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.traderGain); 198 198 199 199 if (this.goods.amount.market1Gain) 200 200 { 201 var cmpPlayer = QueryOwnerInterface(this.firstMarket);201 let cmpPlayer = QueryOwnerInterface(previousMarket); 202 202 if (cmpPlayer) 203 203 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market1Gain); 204 204 205 var cmpStatisticsTracker = QueryOwnerInterface(this.firstMarket, IID_StatisticsTracker);205 let cmpStatisticsTracker = QueryOwnerInterface(previousMarket, IID_StatisticsTracker); 206 206 if (cmpStatisticsTracker) 207 207 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market1Gain); 208 208 } 209 209 210 210 if (this.goods.amount.market2Gain) 211 211 { 212 var cmpPlayer = QueryOwnerInterface(this.secondMarket);212 let cmpPlayer = QueryOwnerInterface(currentMarket); 213 213 if (cmpPlayer) 214 214 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market2Gain); 215 215 216 var cmpStatisticsTracker = QueryOwnerInterface(this.secondMarket, IID_StatisticsTracker);216 let cmpStatisticsTracker = QueryOwnerInterface(currentMarket, IID_StatisticsTracker); 217 217 if (cmpStatisticsTracker) 218 218 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market2Gain); 219 219 } 220 220 } 221 221 … … Trader.prototype.PerformTrade = function 232 232 233 233 if (!nextGoods || RESOURCES.indexOf(nextGoods) == -1) 234 234 nextGoods = "metal"; 235 235 } 236 236 this.goods.type = nextGoods; 237 this.goods.amount = this.CalculateGain( this.firstMarket, this.secondMarket);237 this.goods.amount = this.CalculateGain(currentMarket, nextMarket); 238 238 this.goods.origin = currentMarket; 239 239 }; 240 240 241 241 Trader.prototype.GetGoods = function() 242 242 { 243 243 return this.goods; 244 244 }; 245 245 246 Trader.prototype.GetNextMarket = function()247 {248 if (this.goods.amount && this.goods.origin == this.firstMarket)249 return this.secondMarket;250 251 if (this.goods.amount && this.goods.origin != this.secondMarket)252 this.goods.amount = null; // leftover from previous trading253 return this.firstMarket;254 };255 256 246 Trader.prototype.StopTrading = function() 257 247 { 248 this.index = -1; 249 this.markets = []; 258 250 // Drop carried goods 259 251 this.goods.amount = null; 260 252 // Reset markets 261 this.firstMarket = INVALID_ENTITY; 262 this.secondMarket = INVALID_ENTITY; 253 this.markets = []; 263 254 }; 264 255 265 256 // Get range in which deals with market are available, 266 257 // i.e. trader should be in no more than MaxDistance from market 267 258 // to be able to trade with it. … … Trader.prototype.GetRange = function() 275 266 }; 276 267 277 268 Trader.prototype.OnGarrisonedUnitsChanged = function() 278 269 { 279 270 if (this.HasBothMarkets()) 280 this.g ain = this.CalculateGain(this.firstMarket, this.secondMarket);271 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 281 272 }; 282 273 283 274 Engine.RegisterComponentType(IID_Trader, "Trader", Trader); -
binaries/data/mods/public/simulation/components/UnitAI.js
UnitAI.prototype.UnitFsmSpec = { 652 652 { 653 653 this.FinishOrder(); 654 654 return; 655 655 } 656 656 657 var nextMarket = cmpTrader.GetNextMarket();658 if (nextMarket == this.order.data.firstMarket)659 var state = "TRADE.APPROACHINGFIRSTMARKET";660 else661 var state = "TRADE.APPROACHINGSECONDMARKET";662 663 657 // TODO find the nearest way-point from our position, and start with it 664 658 this.waypoints = undefined; 665 if (this.MoveToMarket( nextMarket))659 if (this.MoveToMarket(this.order.data.target)) 666 660 // We've started walking to the next market 667 this.SetNextState( state);661 this.SetNextState("TRADE.APPROACHINGMARKET"); 668 662 else 669 663 this.FinishOrder(); 670 664 }, 671 665 672 666 "Order.Repair": function(msg) { … … UnitAI.prototype.UnitFsmSpec = { 2603 2597 "Attacked": function(msg) { 2604 2598 // Ignore attack 2605 2599 // TODO: Inform player 2606 2600 }, 2607 2601 2608 "APPROACHINGFIRSTMARKET": { 2609 "enter": function () { 2610 this.SelectAnimation("move"); 2611 }, 2612 2613 "MoveCompleted": function() { 2614 if (this.waypoints && this.waypoints.length) 2615 { 2616 if (!this.MoveToMarket(this.order.data.firstMarket)) 2617 this.StopTrading(); 2618 } 2619 else 2620 this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "APPROACHINGSECONDMARKET"); 2621 }, 2622 }, 2623 2624 "APPROACHINGSECONDMARKET": { 2602 "APPROACHINGMARKET": { 2625 2603 "enter": function () { 2626 2604 this.SelectAnimation("move"); 2627 2605 }, 2628 2606 2629 2607 "MoveCompleted": function() { 2630 2608 if (this.waypoints && this.waypoints.length) 2631 2609 { 2632 if (!this.MoveToMarket(this.order.data. secondMarket))2610 if (!this.MoveToMarket(this.order.data.target)) 2633 2611 this.StopTrading(); 2634 2612 } 2635 2613 else 2636 this.PerformTradeAndMoveToNextMarket(this.order.data. secondMarket, this.order.data.firstMarket, "APPROACHINGFIRSTMARKET");2614 this.PerformTradeAndMoveToNextMarket(this.order.data.target); 2637 2615 }, 2638 2616 }, 2639 2617 }, 2640 2618 2641 2619 "REPAIR": { … … UnitAI.prototype.SetupTradeRoute = funct 5168 5146 return; 5169 5147 5170 5148 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5171 5149 if (cmpTrader.HasBothMarkets()) 5172 5150 { 5173 var data = {"firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false };5151 let data = { "target": cmpTrader.GetFirstMarket(), "firstMarket": cmpTrader.GetFirstMarket(), "secondMarket": cmpTrader.GetSecondMarket(), "route": route, "force": false }; 5174 5152 5175 5153 if (this.expectedRoute) 5176 5154 { 5177 5155 if (!route && this.expectedRoute.length) 5178 5156 data.route = this.expectedRoute.slice(); … … UnitAI.prototype.MoveToMarket = function 5228 5206 } 5229 5207 5230 5208 return ok; 5231 5209 }; 5232 5210 5233 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket , nextMarket, nextFsmStateName)5211 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) 5234 5212 { 5235 5213 if (!this.CanTrade(currentMarket)) 5236 5214 { 5237 5215 this.StopTrading(); 5238 5216 return; 5239 5217 } 5240 5218 5241 if ( this.CheckTargetRange(currentMarket, IID_Trader))5219 if (!this.CheckTargetRange(currentMarket, IID_Trader)) 5242 5220 { 5243 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5244 cmpTrader.PerformTrade(currentMarket); 5245 if (!cmpTrader.GetGain().traderGain) 5246 { 5221 if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again 5247 5222 this.StopTrading(); 5248 return; 5249 } 5250 5251 if (this.order.data.route && this.order.data.route.length) 5252 { 5253 this.waypoints = this.order.data.route.slice(); 5254 if (nextFsmStateName == "APPROACHINGSECONDMARKET") 5255 this.waypoints.reverse(); 5256 this.waypoints.unshift(null); // additionnal dummy point for the market 5257 } 5223 return; 5224 } 5258 5225 5259 if (this.MoveToMarket(nextMarket)) // We've started walking to the next market 5260 this.SetNextState(nextFsmStateName); 5261 else 5262 this.StopTrading(); 5226 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5227 cmpTrader.PerformTrade(currentMarket); 5228 let nextMarket = cmpTrader.markets[cmpTrader.index]; 5229 if (!cmpTrader.GetGoods().amount.traderGain) 5230 { 5231 this.StopTrading(); 5232 return; 5263 5233 } 5264 else 5234 5235 if (this.order.data.route && this.order.data.route.length) 5265 5236 { 5266 if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again 5267 this.StopTrading(); 5237 this.waypoints = this.order.data.route.slice(); 5238 if (this.order.data.target == cmpTrader.GetSecondMarket()) 5239 this.waypoints.reverse(); 5240 this.waypoints.unshift(null); // additionnal dummy point for the market 5268 5241 } 5242 5243 this.order.data.target = nextMarket; 5244 if (this.MoveToMarket(nextMarket)) // We've started walking to the next market 5245 this.SetNextState("APPROACHINGMARKET"); 5246 else 5247 this.StopTrading(); 5269 5248 }; 5270 5249 5271 5250 UnitAI.prototype.StopTrading = function() 5272 5251 { 5273 5252 this.StopMoving();