#quick maths

1 messages · Page 1 of 1 (latest)

ocean gate
#

Just gonna make a thread quick

rose cloak
#

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

ocean gate
#

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

rose cloak
#

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?

ocean gate
#

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

rose cloak
#

i keep thinking and then getting confused lol let me quickly test the code you showed me

ocean gate
#

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

rose cloak
#

yours works perfect

#

but when i try mine its saying no go away

#

idk what im doing wrong

ocean gate
#

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

rose cloak
#
 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

ocean gate
#

actually let me see if i can just get this done easier lol, this could all be done without a loop

rose cloak
#

okay i fixed 1 issue

#

aka the one where if i press the bulk btns it does the calculations

ocean gate
#

just gotta uh find a way to share this

rose cloak
#

alright

rose cloak
#

wow okay

ocean gate
#

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

rose cloak
#

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

ocean gate
#

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

rose cloak
#

i swear thats what we're already doing in the buy is it not?

#

fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy);

ocean gate
#

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

ocean gate
rose cloak
#

and that formula works both ways?

#

for doing increasing and decreasing correctly?

ocean gate
rose cloak
#

fakeBulkBuyPrice += currentUpgradeCost * Mathf.Pow(1.15f, fakeBuildingsBuy) / 1 - 1.15f;

#

would that be how i do it?

ocean gate
#

use the brackets

#

that / 1 - 1.15f is probably subtracting after the whole equation

rose cloak
#

/ (1 - 1.15f);

#

like that

ocean gate
#

shouuld be fine

#

wait also dont add

#

let me quickly explain in simple terms what this is doing

rose cloak
#

please do

ocean gate
#

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

rose cloak
#

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?

ocean gate
#

theres no need to loop anymore at all

rose cloak
#

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?

ocean gate
#

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
ocean gate
#

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

rose cloak
#

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

ocean gate
#

yea that last 1 liner looks fine

#

this function is just a calculator

rose cloak
#

alright

ocean gate
#

it has no knowledge of if you are buying, selling, whatever

#

now you'll want to add 1 more functionality though

rose cloak
#

where do i call it from?

ocean gate
#

from your buy or sell functions, to get the cost

rose cloak
#
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?

ocean gate
#
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

rose cloak
#

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();
        }
    }
ocean gate
#

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

rose cloak
#

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()

ocean gate
#

ok wait you need that one lol

rose cloak
#

(╯°□°)╯︵ ┻━┻

ocean gate
#

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

rose cloak
#

CalculateBulkBuyPrice was just to calculate the price

#

so now we got 2 calculators? 😂

ocean gate
#

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

rose cloak
#

yeah

ocean gate
#

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

ocean gate
# rose cloak

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

rose cloak
#

qustion what do i do with n

#

float CalculatePriceOfN(int n)
{
float currentUpgradeCost = upgradeBaseCost;
return currentUpgradeCost * Mathf.Pow(1.15f, upgradesBought) / (1 - 1.15f);
}

rose cloak
#

oh my bad

#

and n = buildings owned?

#

when i test it

#

it returns -100

ocean gate
#

n is the amount that you are buying essentially

rose cloak
#

so bulkbuy?

ocean gate
#

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

rose cloak
#

yeah

ocean gate
#

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

rose cloak
#

alright thank you very much for your help

ocean gate
#

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 😅

rose cloak
#

what did i miss?

ocean gate
rose cloak
#

?
currentUpgradeCost * Mathf.Pow(1.15f, n) / (1 - 1.15f);

ocean gate
rose cloak
#

oh at the start

ocean gate
#

even though it was a hassle to get setup, the formula will be a massive improvement when you buy like 1000

ocean gate
rose cloak
#

will that fix my issue with it doing -100

ocean gate
#

yes, that 1 - is crucial to the formula

rose cloak
#

yeah

#

it works now

#

however not when trying to sell 1 at a time

ocean gate
#

that should still be fine

rose cloak
#

buying or selling 1 doesn't do anything

ocean gate
#

can you show the code for that part

rose cloak
#

i think i know why

#

we are using the baseprice

ocean gate
#

it should still be fine, but also just remember that this n thing starts at 0

rose cloak
#

not the increased price

ocean gate
#

u want the base price

rose cloak
#

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();
        }
    }```
ocean gate
#

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

rose cloak
#

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

ocean gate
#

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.