Ticket #3812: 3812.3.diff
File 3812.3.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 103 { 114 this.secondMarket = target; 115 this.gain = this.CalculateGain(this.firstMarket, this.secondMarket); 104 this.index = 0; 105 this.markets.push(target); 106 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 116 107 } 117 108 } 118 109 else 119 110 { 120 111 // Else we don't have target markets at all, 121 112 // set the target as first market 122 this.firstMarket = target; 113 this.index = 0; 114 this.markets = [target]; 123 115 } 124 116 if (marketsChanged) 125 117 { 126 118 // Drop carried goods 127 119 this.goods.amount = null; … … Trader.prototype.SetTargetMarket = funct 129 121 return marketsChanged; 130 122 }; 131 123 132 124 Trader.prototype.GetFirstMarket = function() 133 125 { 134 return this. firstMarket;126 return this.markets[0] || null; 135 127 }; 136 128 137 129 Trader.prototype.GetSecondMarket = function() 138 130 { 139 return this. secondMarket;131 return this.markets[1] || null; 140 132 }; 141 133 142 134 Trader.prototype.HasBothMarkets = function() 143 135 { 144 return this. firstMarket && this.secondMarket;136 return this.markets.length >= 2; 145 137 }; 146 138 147 139 Trader.prototype.GetRequiredGoods = function() 148 140 { 149 141 return this.requiredGoods; … … Trader.prototype.CanTrade = function(tar 184 176 return true; 185 177 }; 186 178 187 179 Trader.prototype.PerformTrade = function(currentMarket) 188 180 { 181 let previousMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; 182 183 this.index = ++this.index % this.markets.length; 184 185 let nextMarket = this.markets[(this.index+this.markets.length) % this.markets.length]; 186 187 if (previousMarket != currentMarket) 188 warn("Markets are not matching."); 189 189 190 if (this.goods.amount && this.goods.amount.traderGain) 190 191 { 191 192 var cmpPlayer = QueryOwnerInterface(this.entity); 192 193 if (cmpPlayer) 193 194 cmpPlayer.AddResource(this.goods.type, this.goods.amount.traderGain); … … Trader.prototype.PerformTrade = function 196 197 if (cmpStatisticsTracker) 197 198 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.traderGain); 198 199 199 200 if (this.goods.amount.market1Gain) 200 201 { 201 var cmpPlayer = QueryOwnerInterface(this.firstMarket);202 let cmpPlayer = QueryOwnerInterface(previousMarket); 202 203 if (cmpPlayer) 203 204 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market1Gain); 204 205 205 var cmpStatisticsTracker = QueryOwnerInterface(this.firstMarket, IID_StatisticsTracker);206 let cmpStatisticsTracker = QueryOwnerInterface(previousMarket, IID_StatisticsTracker); 206 207 if (cmpStatisticsTracker) 207 208 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market1Gain); 208 209 } 209 210 210 211 if (this.goods.amount.market2Gain) 211 212 { 212 var cmpPlayer = QueryOwnerInterface(this.secondMarket);213 let cmpPlayer = QueryOwnerInterface(nextMarket); 213 214 if (cmpPlayer) 214 215 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market2Gain); 215 216 216 var cmpStatisticsTracker = QueryOwnerInterface(this.secondMarket, IID_StatisticsTracker);217 let cmpStatisticsTracker = QueryOwnerInterface(nextMarket, IID_StatisticsTracker); 217 218 if (cmpStatisticsTracker) 218 219 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market2Gain); 219 220 } 220 221 } 221 222 … … Trader.prototype.PerformTrade = function 232 233 233 234 if (!nextGoods || RESOURCES.indexOf(nextGoods) == -1) 234 235 nextGoods = "metal"; 235 236 } 236 237 this.goods.type = nextGoods; 237 this.goods.amount = this.CalculateGain( this.firstMarket, this.secondMarket);238 this.goods.amount = this.CalculateGain(currentMarket, nextMarket); 238 239 this.goods.origin = currentMarket; 239 240 }; 240 241 241 242 Trader.prototype.GetGoods = function() 242 243 { 243 244 return this.goods; 244 245 }; 245 246 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 247 Trader.prototype.StopTrading = function() 257 248 { 249 this.index = -1; 250 this.markets = []; 258 251 // Drop carried goods 259 252 this.goods.amount = null; 260 253 // Reset markets 261 this.firstMarket = INVALID_ENTITY; 262 this.secondMarket = INVALID_ENTITY; 254 this.markets = []; 263 255 }; 264 256 265 257 // Get range in which deals with market are available, 266 258 // i.e. trader should be in no more than MaxDistance from market 267 259 // to be able to trade with it. … … Trader.prototype.GetRange = function() 275 267 }; 276 268 277 269 Trader.prototype.OnGarrisonedUnitsChanged = function() 278 270 { 279 271 if (this.HasBothMarkets()) 280 this.g ain = this.CalculateGain(this.firstMarket, this.secondMarket);272 this.goods.amount = this.CalculateGain(this.markets[0], this.markets[1]); 281 273 }; 282 274 283 275 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 "APPROACHING FIRSTMARKET": {2602 "APPROACHINGMARKET": { 2609 2603 "enter": function () { 2610 2604 this.SelectAnimation("move"); 2611 2605 }, 2612 2606 2613 2607 "MoveCompleted": function() { 2614 2608 if (this.waypoints && this.waypoints.length) 2615 2609 { 2616 if (!this.MoveToMarket(this.order.data. firstMarket))2610 if (!this.MoveToMarket(this.order.data.target)) 2617 2611 this.StopTrading(); 2618 2612 } 2619 2613 else 2620 this.PerformTradeAndMoveToNextMarket(this.order.data.firstMarket, this.order.data.secondMarket, "APPROACHINGSECONDMARKET"); 2621 }, 2622 }, 2623 2624 "APPROACHINGSECONDMARKET": { 2625 "enter": function () { 2626 this.SelectAnimation("move"); 2627 }, 2628 2629 "MoveCompleted": function() { 2630 if (this.waypoints && this.waypoints.length) 2631 { 2632 if (!this.MoveToMarket(this.order.data.secondMarket)) 2633 this.StopTrading(); 2634 } 2635 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 = { 5152 "target": cmpTrader.GetFirstMarket(), 5153 "firstMarket": cmpTrader.GetFirstMarket(), 5154 "secondMarket": cmpTrader.GetSecondMarket(), 5155 "route": route, "force": false }; 5174 5156 5175 5157 if (this.expectedRoute) 5176 5158 { 5177 5159 if (!route && this.expectedRoute.length) 5178 5160 data.route = this.expectedRoute.slice(); … … UnitAI.prototype.MoveToMarket = function 5228 5210 } 5229 5211 5230 5212 return ok; 5231 5213 }; 5232 5214 5233 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket , nextMarket, nextFsmStateName)5215 UnitAI.prototype.PerformTradeAndMoveToNextMarket = function(currentMarket) 5234 5216 { 5235 5217 if (!this.CanTrade(currentMarket)) 5236 5218 { 5237 5219 this.StopTrading(); 5238 5220 return; 5239 5221 } 5240 5222 5241 if ( this.CheckTargetRange(currentMarket, IID_Trader))5223 if (!this.CheckTargetRange(currentMarket, IID_Trader)) 5242 5224 { 5243 var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5244 cmpTrader.PerformTrade(currentMarket); 5245 if (!cmpTrader.GetGain().traderGain) 5246 { 5225 if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again 5247 5226 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 } 5227 return; 5228 } 5258 5229 5259 if (this.MoveToMarket(nextMarket)) // We've started walking to the next market 5260 this.SetNextState(nextFsmStateName); 5261 else 5262 this.StopTrading(); 5230 let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); 5231 cmpTrader.PerformTrade(currentMarket); 5232 let nextMarket = cmpTrader.markets[cmpTrader.index]; 5233 if (!cmpTrader.GetGoods().amount.traderGain) 5234 { 5235 this.StopTrading(); 5236 return; 5263 5237 } 5264 else 5238 5239 this.order.data.target = nextMarket; 5240 5241 if (this.order.data.route && this.order.data.route.length) 5265 5242 { 5266 if (!this.MoveToMarket(currentMarket)) // If the current market is not reached try again 5267 this.StopTrading(); 5243 this.waypoints = this.order.data.route.slice(); 5244 if (this.order.data.target == cmpTrader.GetSecondMarket()) 5245 this.waypoints.reverse(); 5246 this.waypoints.unshift(null); // additionnal dummy point for the market 5268 5247 } 5248 5249 if (this.MoveToMarket(nextMarket)) // We've started walking to the next market 5250 this.SetNextState("APPROACHINGMARKET"); 5251 else 5252 this.StopTrading(); 5269 5253 }; 5270 5254 5271 5255 UnitAI.prototype.StopTrading = function() 5272 5256 { 5273 5257 this.StopMoving();