Ticket #1691: trader-v3.diff
File trader-v3.diff, 13.1 KB (added by , 11 years ago) |
---|
-
binaries/data/mods/public/gui/session/selection_details.js
134 134 getGUIObjectByName("resourceCarryingIcon").tooltip = ""; 135 135 } 136 136 // Use the same indicators for traders 137 else if (entState.trader && entState.trader.goods.amount > 0)137 else if (entState.trader && entState.trader.goods.amount) 138 138 { 139 139 getGUIObjectByName("resourceCarryingIcon").hidden = false; 140 140 getGUIObjectByName("resourceCarryingText").hidden = false; 141 141 getGUIObjectByName("resourceCarryingIcon").sprite = "stretched:session/icons/resources/"+entState.trader.goods.type+".png"; 142 getGUIObjectByName("resourceCarryingText").caption = entState.trader.goods.amount; 143 getGUIObjectByName("resourceCarryingIcon").tooltip = ""; 142 var totalGain = entState.trader.goods.amount.traderGain; 143 if (entState.trader.goods.amount.market1Gain) 144 totalGain += entState.trader.goods.amount.market1Gain; 145 if (entState.trader.goods.amount.market2Gain) 146 totalGain += entState.trader.goods.amount.market2Gain; 147 getGUIObjectByName("resourceCarryingText").caption = totalGain; 148 getGUIObjectByName("resourceCarryingIcon").tooltip = "Gain:" + getTradingTooltip(entState.trader.goods.amount); 144 149 } 145 150 // And for number of workers 146 151 else if (entState.foundation) -
binaries/data/mods/public/gui/session/utility_functions.js
547 547 548 548 return ""; 549 549 } 550 551 /** 552 * Returns a message with the details of the trade gain. 553 */ 554 function getTradingTooltip(gain) 555 { 556 var tooltip = " " + gain.traderGain; 557 if (gain.market1Gain && gain.market1Owner == gain.traderOwner) 558 tooltip += "+" + gain.market1Gain; 559 if (gain.market2Gain && gain.market2Owner == gain.traderOwner) 560 tooltip += "+" + gain.market2Gain; 561 tooltip += " (you)"; 562 563 if (gain.market1Gain && gain.market1Owner != gain.traderOwner) 564 tooltip += ", " + gain.market1Gain + " (player " + gain.market1Owner + ")"; 565 if (gain.market2Gain && gain.market2Owner != gain.traderOwner) 566 tooltip += ", " + gain.market2Gain + " (player " + gain.market2Owner + ")"; 567 tooltip += "."; 568 569 return tooltip; 570 } -
binaries/data/mods/public/gui/session/input.js
271 271 272 272 var traderData = { "firstMarket": entState.id, "secondMarket": targetState.id, "template": trader }; 273 273 var gain = Engine.GuiInterfaceCall("GetTradingRouteGain", traderData); 274 if (gain !== null)274 if (gain && gain.traderGain) 275 275 { 276 276 data.command = "trade"; 277 277 data.target = traderData.secondMarket; 278 278 data.source = traderData.firstMarket; 279 279 cursor = "action-setup-trade-route"; 280 tooltip = " Click to establish a default route for new traders.";280 tooltip = "Right-click to establish a default route for new traders."; 281 281 if (trader) 282 tooltip += " Gain: " + gain + " metal.";282 tooltip += "\nGain (metal):" + getTradingTooltip(gain); 283 283 else // Foundation or cannot produce traders 284 tooltip += " Expected gain: " + gain + " metal.";284 tooltip += "\nExpected gain (metal):" + getTradingTooltip(gain); 285 285 } 286 286 } 287 287 … … 348 348 case "is first": 349 349 tooltip = "Origin trade market."; 350 350 if (tradingDetails.hasBothMarkets) 351 tooltip += " Gain: " + tradingDetails.gain + " " + tradingDetails.goods + ". Right-click to create a new trade route."351 tooltip += "\nGain (" + tradingDetails.goods + "):" + getTradingTooltip(tradingDetails.gain); 352 352 else 353 tooltip += " 353 tooltip += "\nRight-click on another market to set it as a destination trade market." 354 354 break; 355 355 case "is second": 356 tooltip = "Destination trade market. Gain: " + tradingDetails.gain + " " + tradingDetails.goods + "." + " Right-click to create a new trade route.";356 tooltip = "Destination trade market.\nGain (" + tradingDetails.goods + "):" + getTradingTooltip(tradingDetails.gain); 357 357 break; 358 358 case "set first": 359 tooltip = " Set as origin trade market";359 tooltip = "Right-click to set as origin trade market"; 360 360 break; 361 361 case "set second": 362 tooltip = " Set as destination trade market. Gain: " + tradingDetails.gain + " " + tradingDetails.goods + ".";362 tooltip = "Right-click to set as destination trade market.\nGain (" + tradingDetails.goods + "):" + getTradingTooltip(tradingDetails.gain); 363 363 break; 364 364 } 365 365 return {"possible": true, "tooltip": tooltip}; -
binaries/data/mods/public/simulation/helpers/TraderGain.js
1 1 // This constant used to adjust gain value depending on distance 2 2 const DISTANCE_FACTOR = 1 / 110; 3 3 4 // Additional gain for trading performed between markets of different players, in percents5 const INTERNATIONAL_TRADING_ADDITION = 50;4 // Additional gain (applying to each market) for trading performed between markets of different players, in percents 5 const INTERNATIONAL_TRADING_ADDITION = 25; 6 6 7 function CalculateTraderGain(firstMarket, secondMarket, template) 7 // If trader undefined, the trader owner is supposed to be the same as the first market 8 function CalculateTraderGain(firstMarket, secondMarket, template, trader) 8 9 { 10 var gain = {}; 11 9 12 var cmpFirstMarketPosition = Engine.QueryInterface(firstMarket, IID_Position); 10 13 var cmpSecondMarketPosition = Engine.QueryInterface(secondMarket, IID_Position); 11 14 if (!cmpFirstMarketPosition || !cmpFirstMarketPosition.IsInWorld() || !cmpSecondMarketPosition || !cmpSecondMarketPosition.IsInWorld()) … … 17 20 // We don't use pathfinder, because ordinary distance looks more fair. 18 21 var distance = Math.sqrt(Math.pow(firstMarketPosition.x - secondMarketPosition.x, 2) + Math.pow(firstMarketPosition.y - secondMarketPosition.y, 2)); 19 22 // We calculate gain as square of distance to encourage trading between remote markets 20 var gain = Math.pow(distance * DISTANCE_FACTOR, 2); 23 gain.traderGain = Math.pow(distance * DISTANCE_FACTOR, 2); 24 if (template && template.GainMultiplier) 25 gain.traderGain *= template.GainMultiplier; 26 gain.traderGain = Math.round(gain.traderGain); 27 // If trader undefined, the trader owner is supposed to be the same as the first market 28 if (trader) 29 var cmpOwnership = Engine.QueryInterface(trader, IID_Ownership); 30 else 31 var cmpOwnership = Engine.QueryInterface(firstMarket, IID_Ownership); 32 gain.traderOwner = cmpOwnership.GetOwner(); 21 33 22 // If markets belongs to different players, multiple gain to INTERNATIONAL_TRADING_MULTIPLIER 23 var cmpFirstMarketOwnership = Engine.QueryInterface(firstMarket, IID_Ownership); 24 var cmpSecondMarketOwnership = Engine.QueryInterface(secondMarket, IID_Ownership); 25 if (cmpFirstMarketOwnership.GetOwner() != cmpSecondMarketOwnership.GetOwner()) 26 gain *= 1 + INTERNATIONAL_TRADING_ADDITION / 100; 34 // If markets belong to different players, add gain from international trading 35 var ownerFirstMarket = Engine.QueryInterface(firstMarket, IID_Ownership).GetOwner(); 36 var ownerSecondMarket = Engine.QueryInterface(secondMarket, IID_Ownership).GetOwner(); 37 if (ownerFirstMarket != ownerSecondMarket) 38 { 39 var internationalGain1 = ApplyTechModificationsToEntity("Trade/International", INTERNATIONAL_TRADING_ADDITION, firstMarket); 40 gain.market1Gain = Math.round(gain.traderGain * internationalGain1 / 100); 41 gain.market1Owner = ownerFirstMarket; 42 var internationalGain2 = ApplyTechModificationsToEntity("Trade/International", INTERNATIONAL_TRADING_ADDITION, secondMarket); 43 gain.market2Gain = Math.round(gain.traderGain * internationalGain2 / 100); 44 gain.market2Owner = ownerSecondMarket; 27 45 28 if (template && template.GainMultiplier) 29 gain *= template.GainMultiplier; 30 gain = Math.round(gain); 46 } 47 31 48 return gain; 32 49 } 33 50 -
binaries/data/mods/public/simulation/components/Looter.js
27 27 if (cmpTrader) 28 28 { 29 29 var carriedGoods = cmpTrader.GetGoods(); 30 if (carriedGoods.amount > 0)30 if (carriedGoods.amount && carriedGoods.amount.traderGain) 31 31 { 32 32 // Convert from {type:<type>,amount:<amount>} to {<type>:<amount>} 33 33 var resourcesToAdd = {}; 34 resourcesToAdd[carriedGoods.type] = carriedGoods.amount; 34 resourcesToAdd[carriedGoods.type] = carriedGoods.amount.traderGain; 35 if (carriedGoods.amount.market1Gain) 36 resourcesToAdd[carriedGoods.type] += carriedGoods.amount.market1Gain; 37 if (carriedGoods.amount.market2Gain) 38 resourcesToAdd[carriedGoods.type] += carriedGoods.amount.market2Gain; 35 39 cmpPlayer.AddResources(resourcesToAdd); 36 40 } 37 41 } -
binaries/data/mods/public/simulation/components/Trader.js
31 31 // Selected resource for trading 32 32 this.preferredGoods = "metal"; 33 33 // Currently carried goods 34 this.goods = { "type": null, "amount": 0};35 } ;34 this.goods = { "type": null, "amount": null }; 35 } 36 36 37 37 Trader.prototype.CalculateGain = function(firstMarket, secondMarket) 38 38 { 39 var gain = CalculateTraderGain(firstMarket, secondMarket, this.template, this.entity); 40 39 41 // For ship increase gain for each garrisoned trader 40 // Calculate this here to save passing unnecessary stuff into the CalculatetraderGain function 41 var garrisonMultiplier = 1; 42 // Calculate this here to save passing unnecessary stuff into the CalculateTraderGain function 42 43 var cmpIdentity = Engine.QueryInterface(this.entity, IID_Identity); 43 44 if (cmpIdentity && cmpIdentity.HasClass("Ship")) 44 45 { 45 46 var cmpGarrisonHolder = Engine.QueryInterface(this.entity, IID_GarrisonHolder); 46 47 if (cmpGarrisonHolder) 47 48 { 49 var garrisonMultiplier = 1; 48 50 var garrisonedTradersCount = 0; 49 51 for each (var entity in cmpGarrisonHolder.GetEntities()) 50 52 { … … 53 55 garrisonedTradersCount++; 54 56 } 55 57 garrisonMultiplier *= 1 + GARRISONED_TRADER_ADDITION * garrisonedTradersCount / 100; 58 59 if (gain.traderGain) 60 gain.traderGain = Math.round(garrisonMultiplier * gain.traderGain); 61 if (gain.market1Gain) 62 gain.market1Gain = Math.round(garrisonMultiplier * gain.market1Gain); 63 if (gain.market2Gain) 64 gain.market2Gain = Math.round(garrisonMultiplier * gain.market2Gain); 56 65 } 57 66 } 58 67 59 return Math.round(garrisonMultiplier * CalculateTraderGain(firstMarket, secondMarket, this.template));60 } ;68 return gain; 69 } 61 70 62 71 Trader.prototype.GetGain = function() 63 72 { … … 116 125 if (marketsChanged) 117 126 { 118 127 // Drop carried goods 119 this.goods.amount = 0;128 this.goods.amount = null; 120 129 } 121 130 return marketsChanged; 122 131 }; … … 177 186 178 187 Trader.prototype.PerformTrade = function() 179 188 { 180 if (this.goods.amount > 0)189 if (this.goods.amount && this.goods.amount.traderGain) 181 190 { 182 191 var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); 183 cmpPlayer.AddResource(this.goods.type, this.goods.amount );192 cmpPlayer.AddResource(this.goods.type, this.goods.amount.traderGain); 184 193 185 194 var cmpStatisticsTracker = QueryOwnerInterface(this.entity, IID_StatisticsTracker); 186 195 if (cmpStatisticsTracker) 187 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount); 196 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.traderGain); 197 198 if (this.goods.amount.market1Gain) 199 { 200 var cmpPlayer = QueryOwnerInterface(this.firstMarket, IID_Player); 201 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market1Gain); 202 203 var cmpStatisticsTracker = QueryOwnerInterface(this.firstMarket, IID_StatisticsTracker); 204 if (cmpStatisticsTracker) 205 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market1Gain); 206 } 207 208 if (this.goods.amount.market2Gain) 209 { 210 var cmpPlayer = QueryOwnerInterface(this.secondMarket, IID_Player); 211 cmpPlayer.AddResource(this.goods.type, this.goods.amount.market2Gain); 212 213 var cmpStatisticsTracker = QueryOwnerInterface(this.secondMarket, IID_StatisticsTracker); 214 if (cmpStatisticsTracker) 215 cmpStatisticsTracker.IncreaseTradeIncomeCounter(this.goods.amount.market2Gain); 216 } 188 217 } 189 218 this.goods.type = this.preferredGoods; 190 219 this.goods.amount = this.gain; … … 198 227 Trader.prototype.StopTrading = function() 199 228 { 200 229 // Drop carried goods 201 this.goods.amount = 0;230 this.goods.amount = null; 202 231 // Reset markets 203 232 this.firstMarket = INVALID_ENTITY; 204 233 this.secondMarket = INVALID_ENTITY;