Changeset View
Standalone View
binaries/data/mods/public/simulation/components/Barter.js
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | if (availResources.indexOf(resourceToBuy) == -1) | ||||
warn("ExchangeResources: incorrect resource to buy: " + uneval(resourceToBuy)); | warn("ExchangeResources: incorrect resource to buy: " + uneval(resourceToBuy)); | ||||
return; | return; | ||||
} | } | ||||
if (!this.PlayerHasMarket(playerEntity)) | if (!this.PlayerHasMarket(playerEntity)) | ||||
{ | { | ||||
warn("ExchangeResources: player has no markets"); | warn("ExchangeResources: player has no markets"); | ||||
return; | return; | ||||
} | } | ||||
if (amount != 100 && amount != 500) | |||||
return; | |||||
var cmpPlayer = Engine.QueryInterface(playerEntity, IID_Player); | var cmpPlayer = Engine.QueryInterface(playerEntity, IID_Player); | ||||
var prices = this.GetPrices(playerEntity); | var prices = this.GetPrices(playerEntity); | ||||
var amountsToSubtract = {}; | var amountsToSubtract = {}; | ||||
amountsToSubtract[resourceToSell] = amount; | amountsToSubtract[resourceToSell] = amount; | ||||
if (cmpPlayer.TrySubtractResources(amountsToSubtract)) | if (cmpPlayer.TrySubtractResources(amountsToSubtract)) | ||||
{ | { | ||||
var amountToAdd = Math.round(prices["sell"][resourceToSell] / prices["buy"][resourceToBuy] * amount); | var amountToAdd = Math.round(prices["sell"][resourceToSell] / prices["buy"][resourceToBuy] * amount); | ||||
cmpPlayer.AddResource(resourceToBuy, amountToAdd); | cmpPlayer.AddResource(resourceToBuy, amountToAdd); | ||||
var numberOfDeals = Math.round(amount / 100); | |||||
mimo: Wouldn't it be better to only remove the Math.round here? so that the formula will always work… | |||||
Not Done Inline Actions(In fact with the check above I should have remove but that's not what you point out). My first patch just changed that Math.round to a Math.ceil But we still have some side effects. Indeed it allowed to have a really finest subdivision wich doesn't really fit to the current code. For example, with an amount of 1, we will get 1 ress (even if the price will fall, we won't get a 0 until some barters). So basically we will get almost the same thing for 100 barter of 1 than for 1 barter of 100 (but there will be some difference). Well I have no real idea of what is the best and your input is really welcome. (In both cases moving it is to do) fatherbushido: (In fact with the check above I should have remove but that's not what you point out).
My… | |||||
Not Done Inline ActionsFrom what you describe, i've the impression the limitation could be a minimum (i.e. amount > 50 or whatever seems reasonnable for gameplay) if we remove the rounding (without using a ceil). And also, whatever the limitations, a comment could be added in menu.js as otherwise a modder can have the impression he can change g_BarterResourceSellQuantity freely. mimo: From what you describe, i've the impression the limitation could be a minimum (i.e. amount > 50… | |||||
// Display chat message to observers | // Display chat message to observers | ||||
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); | var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface); | ||||
if (cmpGUIInterface) | if (cmpGUIInterface) | ||||
cmpGUIInterface.PushNotification({ | cmpGUIInterface.PushNotification({ | ||||
"type": "barter", | "type": "barter", | ||||
"players": [cmpPlayer.GetPlayerID()], | "players": [cmpPlayer.GetPlayerID()], | ||||
"amountsSold": amount, | "amountsSold": amount, | ||||
"amountsBought": amountToAdd, | "amountsBought": amountToAdd, | ||||
"resourceSold": resourceToSell, | "resourceSold": resourceToSell, | ||||
"resourceBought": resourceToBuy | "resourceBought": resourceToBuy | ||||
}); | }); | ||||
var cmpStatisticsTracker = Engine.QueryInterface(playerEntity, IID_StatisticsTracker); | var cmpStatisticsTracker = Engine.QueryInterface(playerEntity, IID_StatisticsTracker); | ||||
if (cmpStatisticsTracker) | if (cmpStatisticsTracker) | ||||
{ | { | ||||
cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount); | cmpStatisticsTracker.IncreaseResourcesSoldCounter(resourceToSell, amount); | ||||
cmpStatisticsTracker.IncreaseResourcesBoughtCounter(resourceToBuy, amountToAdd); | cmpStatisticsTracker.IncreaseResourcesBoughtCounter(resourceToBuy, amountToAdd); | ||||
} | } | ||||
let difference = this.DIFFERENCE_PER_DEAL * amount / 100; | |||||
// Increase price difference for both exchange resources. | // Increase price difference for both exchange resources. | ||||
// Overall price difference (dynamic +/- constant) can't exceed +-99%. | // Overall price difference (dynamic +/- constant) can't exceed +-99%. | ||||
Not Done Inline ActionsOverall if you want elexis: Overall if you want | |||||
this.priceDifferences[resourceToSell] -= this.DIFFERENCE_PER_DEAL * numberOfDeals; | this.priceDifferences[resourceToSell] -= difference; | ||||
this.priceDifferences[resourceToSell] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToSell])); | this.priceDifferences[resourceToSell] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToSell])); | ||||
this.priceDifferences[resourceToBuy] += this.DIFFERENCE_PER_DEAL * numberOfDeals; | this.priceDifferences[resourceToBuy] += difference; | ||||
this.priceDifferences[resourceToBuy] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToBuy])); | this.priceDifferences[resourceToBuy] = Math.min(99 - this.CONSTANT_DIFFERENCE, Math.max(this.CONSTANT_DIFFERENCE - 99, this.priceDifferences[resourceToBuy])); | ||||
} | } | ||||
if (this.restoreTimer === undefined) | if (this.restoreTimer === undefined) | ||||
{ | { | ||||
var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
this.restoreTimer = cmpTimer.SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {}); | this.restoreTimer = cmpTimer.SetInterval(this.entity, IID_Barter, "ProgressTimeout", this.RESTORE_TIMER_INTERVAL, this.RESTORE_TIMER_INTERVAL, {}); | ||||
} | } | ||||
Show All 26 Lines |
Wouldn't it be better to only remove the Math.round here? so that the formula will always work without such arbitrary limitations of amount%100=0.
and if doing that, move that definition of numberOfDeals where it is needed (i.e. just before l124)