#quick maths
1 messages · Page 1 of 1 (latest)
fair
I think the code is working
but i think the whole bulk btns making it run the code is doing it twice
cutting it in half
in your BuyAutoClicker(), you do
upgradePastCost = upgradeBulkCost; which is the cost of buying many
its not the past cost of buying 1, so your sell function isnt calculating the cost of 1. Its calculating the cost based on your last bulk buy
in here u can see, I assign upgradePastCost while looping through CalculateBulkBuyPrice
Test your C# code online with .NET Fiddle code editor.
upgradebulkcost = the price the next one will cost
and i pick 1 bulk buy it will calculate the bulk for 1
shouldn't it work in reverse?
when selling bulk, you're calculating it based on upgradePastCost
upgradePastCost, at least to me, means the cost of the last 1 unit, not the last entire batch
i keep thinking and then getting confused lol let me quickly test the code you showed me
you can change the values in the top of the one i sent
I also think you dont need to loop, you can calculate this directly almost
yours works perfect
but when i try mine its saying no go away
idk what im doing wrong
because i assign upgradePastCost as i loop, so the value is correct
the upradePastCost is like 52 in the compiler, but in yours it wouldve been the 305
thats if you put upgradesBought back down to 0
public void CalculateBulkSellPrice()
{
fakeBulkSellPrice = 0f;
for (int i = 0; i < sellCorrect; i++)
{
fakeBulkSellPrice += upgradePastCost;
upgradePastCost = upgradePastCost / 1.15f;
}
upgradePastCost = Mathf.Round(upgradePastCost);
}
thats the code you did but in the script correct?
trying to mimic it
actually let me see if i can just get this done easier lol, this could all be done without a loop
okay i fixed 1 issue
aka the one where if i press the bulk btns it does the calculations
stay with me for a second, i got your equation to massively simplify the bulk buy at least
just gotta uh find a way to share this
alright
wow okay
ok idk why i did this on a graphing calculator but you can see the function if you care to
And you also can use the same formula for the selling
because its the same calculation right, just subtract from your money or whatever happens
for example, selling 10 when you have 10 is still f(10). Selling 10 when you have 13 is still just f(13) - f(3)
make that equation into a method in your code, itll act as a calculator. then id even add a method
float CalculatePrice(int amount, bool isBuying)
then just do the math from there, you plug in how many you want to buy or sell, then send true or false if you're buying or selling. You could even use an enum as the 2nd param to name it Buy or Sell but its gonna be the same thing
btw this formula only works for the geometric sum,
a = initial, r = multiplier
a + ar + ar^2 + ar^3 + ....
which becomes
15 + (15 * 1.15) + (15 * 1.15 * 1.15) + ...
And that last line is exactly what your for loop was already doing.
If this was a different equation, you would need a different summation
I really appreciate all this work you're putting in to help me but I don't know how to turn that into c#
baseprice + baseprice * 1.15f + baseprice * 1.15 * 1.15 on and on
well try writing the equation alone in c#, this quite literally is all you need
you already know how to do power, so this part should be easy
i swear thats what we're already doing in the buy is it not?
fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy);
you are, but theres no need to loop. we want to calculate it directly so we dont even need to keep track of a bunch more
this isnt the same formula as the screenshot, my formula is a summation of yours.
So if you loop from 0 to 1000, you are doing this 1000 times which gets costly.
My formula would just do 15 * (1-1.15^1000)/1-1.15 and be done
its just a summation, it doesnt care about if you are going from 3 to 13 items or 13 to 3. It still sums the same things
fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / 1 - 1.15f;
would that be how i do it?
shouuld be fine
wait also dont add
let me quickly explain in simple terms what this is doing
please do
Ill use simple numbers, Your algorithm was like
I plan to spend X = 0 dollars. First building is 1
X += 1, 2nd is now 2
X += 2, 3rd = 3
X += 3
I am now spending X=6 dollars
The summation algorithm takes your formula and directly adds 1 2 and 3 at the same time
So you can just do
X = (formula)
read the comments on desmos, i tried to directly explain what it was doing
Yeah I have
just trying to understand the math in c# as well
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = 0f;
int fakeBuildingsBuy = upgradesBought;
float currentUpgradeCost = upgradeBaseCost;
for (int i = 0; i < bulkAmount; i++)
{
fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
fakeBuildingsBuy++;
}
fakeBulkBuyPrice = Mathf.Round(fakeBulkBuyPrice);
upgradeBulkCost = fakeBulkBuyPrice;
}
function should look like this?
theres no need to loop anymore at all
okay
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = 0f;
int fakeBuildingsBuy = upgradesBought;
float currentUpgradeCost = upgradeBaseCost;
fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
fakeBuildingsBuy++;
fakeBulkBuyPrice = Mathf.Round(fakeBulkBuyPrice);
upgradeBulkCost = fakeBulkBuyPrice;
}
so this?
take the currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
and put this into a new method
float CalculatePriceOfN(int n)
return that formula here
now that the loop is gone, you dont need to assign it to 0 and then += it, because
0 += X is just X
but yea this is now how you calculate the price of buying the first, and 2nd, 3rd 4th ... Nth all at once
also in the desmos link, i list more about 1 flaw in this. You are now calculating buying the first to Nth everytime. Even if you've already bought stuff
i also list how to get around that
so like this?
float CalculatePriceOfN(int n)
{
fakeBulkBuyPrice = 0f;
int fakeBuildingsBuy = upgradesBought;
float currentUpgradeCost = upgradeBaseCost;
return fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
}
or legit just
return fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
sorry if im being a pain
alright
it has no knowledge of if you are buying, selling, whatever
now you'll want to add 1 more functionality though
where do i call it from?
from your buy or sell functions, to get the cost
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = 0f;
int fakeBuildingsBuy = upgradesBought;
float currentUpgradeCost = upgradeBaseCost;
fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
fakeBuildingsBuy++;
fakeBulkBuyPrice = Mathf.Round(fakeBulkBuyPrice);
upgradeBulkCost = fakeBulkBuyPrice;
}
is this reduntant now?
n = bulkamount right?
n = how many times we want it to loop to?
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = CalculatePriceOfN(upgradesBought);
upgradeBulkCost = Mathf.Round(fakeBulkBuyPrice);
}
although i really dont know what fakeBulkBuyPrice is for
also you'll still want to look at desmos for what to do when you already have upgrades bought
yeah i've fucked something up
🤦
bulk now cost - 100
im doing something wrong here
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = CalculatePriceOfN(upgradesBought);
upgradeBulkCost = Mathf.Round(fakeBulkBuyPrice);
}
float CalculatePriceOfN(int n)
{
fakeBulkBuyPrice = 0f;
int fakeBuildingsBuy = upgradesBought;
float currentUpgradeCost = upgradeBaseCost;
return fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / (1 - 1.15f);
}
public void CalculateBulkSellPrice()
{
fakeBulkSellPrice = 0f;
int fakeBuildingsSell = upgradesBought;
float currentUpgradeCost = upgradePastCost;
for (int i = 0; i < sellCorrect; i++)
{
fakeBulkSellPrice += currentUpgradeCost;
currentUpgradeCost = currentUpgradeCost / 1.15f;
}
fakeBulkSellPrice = Mathf.Round(fakeBulkSellPrice);
upgradeBulkCost = fakeBulkSellPrice;
}
public void BuyAutoClicker()
{
if (_GameManager.cookies >= upgradeBulkCost)
{
upgradePastCost = upgradeBulkCost;
_GameManager.cookies -= upgradeBulkCost;
upgradesBought += bulkAmount;
CalculateBulkBuyPrice();
// CalculateBulkSellPrice();
CalculateCookiesPerSecond();
}
}
oh i misread, CalculatePriceOfN is wrong
it is purely a calculator, it shouldnt adjust any variable
it is only returning this formula. dont add or adjust anything inside there
i feel like you have a lot of repeated variables tbh, like fakeBulkSellPrice fakeBulkSellPrice and upgradeBulkCost all seemingly do the same thing
quite possibly, i've been at this for ages now and changed this script so much idk what it even does at this point 😂
i've deleted public void CalculateBulkSellPrice()
ok wait you need that one lol
(╯°□°)╯︵ ┻━┻
Ok think of it like this, you have a calculator
float CalculatePriceOfN(int n)
{
return currentUpgradeCost * Mathf.Pow(1.15f, n) / (1 - 1.15f);
}
This only will give you the result of buying N items, assuming you have none to start
CalculateBulkBuyPrice will use this calculator and ask it what the price is to buy 10 items
CalculateBulkBuyPrice will use the value from the calculator and idk spend the money, i didnt really see what the code did
It still will do this, because you now have another case where you buy items while already having some. CalculateBulkBuyPrice will use the calculator with specific values
did you see what to do in the desmos link when i wrote you already have 3 items but wanna buy 10 more
yea so this is what your 2nd calculator will do
actually you could just do it from the first calculator and add a 2nd parameter, but id imagine you still want to split up logic so CalculateBulkSellPrice can subtract/add to your current money
Also for this, it works the same way with selling. If you have 13 and want to sell 10, you do the exact same formula
qustion what do i do with n
float CalculatePriceOfN(int n)
{
float currentUpgradeCost = upgradeBaseCost;
return currentUpgradeCost * Mathf.Pow(1.15f, upgradesBought) / (1 - 1.15f);
}
i wrote it here
n is the amount that you are buying essentially
so bulkbuy?
well not buying ah
ok let me word it like this. No matter what you do
item1 = 15, item2 = 16.something, item3 = 17. im just making up the numbers here
Regardless if you buy or sell, this is the price right
yeah
So the calculator is summing up item1, item2, item3, and just returning that result of
15 + 16 + 17. its up to you to add or subtract that value from your current amount
I do have to go soon, so I suggest really playing around with the algorithm and just rewriting some of your code to simplify
the desmos link also describes how to get the price of 1 item specifically, its just your old formula
alright thank you very much for your help
oh actually let me double check, the formula might be weird cause of thebrackets
or something 🤔
ah yes i missed it completely, u missed one thing 😅
what did i miss?
your formual missed a 1-
https://dotnetfiddle.net/Y3Cikt
Test your C# code online with .NET Fiddle code editor.
?
currentUpgradeCost * Mathf.Pow(1.15f, n) / (1 - 1.15f);
oh at the start
even though it was a hassle to get setup, the formula will be a massive improvement when you buy like 1000
honestly skimmed through your code, but i think its just better if i show with this link here how I would handle subtracting and adding from the money.
The displays can simply just use CalculatePriceOfN(int n, int m) to say how much it would cost
will that fix my issue with it doing -100
yes, that 1 - is crucial to the formula
that should still be fine
buying or selling 1 doesn't do anything
can you show the code for that part
it should still be fine, but also just remember that this n thing starts at 0
not the increased price
u want the base price
actually ignore this
but then how does it increase? say i bulkbuy 1, it goes to 17 thend i decide to buy another 1 and it goes to 20
it isn't increasing
public void CalculateBulkBuyPrice()
{
fakeBulkBuyPrice = CalculatePriceOfN(bulkAmount);
upgradeBulkCost = Mathf.Round(fakeBulkBuyPrice);
}
float CalculatePriceOfN(int n)
{
float currentUpgradeCost = upgradeBaseCost;
return currentUpgradeCost * (1 - Mathf.Pow(1.15f, n)) / (1 - 1.15f);
}
public void CalculateBulkSellPrice()
{
fakeBulkSellPrice = 0f;
int fakeBuildingsSell = upgradesBought;
float currentUpgradeCost = upgradePastCost;
for (int i = 0; i < sellCorrect; i++)
{
fakeBulkSellPrice += currentUpgradeCost;
currentUpgradeCost = currentUpgradeCost / 1.15f;
}
fakeBulkSellPrice = Mathf.Round(fakeBulkSellPrice);
upgradeBulkCost = fakeBulkSellPrice;
}
public void BuyAutoClicker()
{
if (_GameManager.cookies >= upgradeBulkCost)
{
upgradePastCost = upgradeBulkCost;
_GameManager.cookies -= upgradeBulkCost;
upgradesBought += bulkAmount;
CalculateBulkBuyPrice();
// CalculateBulkSellPrice();
CalculateCookiesPerSecond();
}
}```
look at this link, i added more calculators
CalculatePriceOfNthBuilding is what you can use if you want the price of the 2nd one only.
public float CalculatePriceOfN(int n, int m)
is what you can use when you want to buy 1, and you already have 1
yeah so the formula works but doesn't work according to adding 1 at a time
and doesn't go from the new price
if that makes sense
it always uses the base price
i get the feeling it should go from new price
anyway imma go thanks for the help
From the online compiler stuff actually, you would want to implement the 1st and 3rd calculator function
You should just experiment with it on the online compiler tbh
If you do need proper 1 on 1 tutoring for the math and code part, I do tutor (for a fee ofc). You can dm if that's something thatd interest you.