MHB Online game programming issues.

danielmchugh
Messages
3
Reaction score
0
Okay so i am building an incremental game and have a formula to work out how much it will cost to buy a set amount of buildings.

This is required as each time you buy a building the price for that building goes up.This formula is as follows:

((BaseCost * (1.07^b - 1.07^a)) / 0.07)BaseCost = Starting cost of buildings.

a = number of buildings i already own.

b = number of buildings i will own after buyingThis formula will give me a cost if i give it a set amount i want to buy.

But i need to work out how many i can buy with a finite amount of money, say 20000 if the base cost is 200 each and i already own 50.
 
Mathematics news on Phys.org
danielmchugh said:
Okay so i am building an incremental game and have a formula to work out how much it will cost to buy a set amount of buildings.

This is required as each time you buy a building the price for that building goes up.This formula is as follows:

((BaseCost * (1.07^b - 1.07^a)) / 0.07)BaseCost = Starting cost of buildings.

a = number of buildings i already own.

b = number of buildings i will own after buyingThis formula will give me a cost if i give it a set amount i want to buy.

But i need to work out how many i can buy with a finite amount of money, say 20000 if the base cost is 200 each and i already own 50.

Hi danielmchugh! Welcome to MHB! ;)

Filling in your numbers, we get:
\begin{aligned}\frac{200\times (1.07^b - 1.07^{50})}{0.07} &= 20000 \\
1.07^b - 1.07^{50} &= \frac{20000 \times 0.07}{200}\\
1.07^b &= \frac{20000 \times 0.07}{200} + 1.07^{50} \\
\ln\left(1.07^b\right) &= \ln \left(\frac{20000 \times 0.07}{200} + 1.07^{50}\right) \\
b \ln 1.07 &= \ln \left(\frac{20000 \times 0.07}{200} + 1.07^{50}\right) \\
b &= \frac{\ln \left(\frac{20000 \times 0.07}{200} + 1.07^{50}\right)}{\ln 1.07}
\end{aligned}
 
More generally, the formula is:
$$NrToOwn = \left\lfloor\frac{\ln \left(\frac{AvailableMoney \times 0.07}{BaseCost}+ 1.07^{NrOwned}\right)}{\ln 1.07}\right\rfloor$$

In your specific example, it's:
$$NrToOwn = \left\lfloor\frac{\ln \left(\frac{20000 \times 0.07}{200}+ 1.07^{50}\right)}{\ln 1.07}\right\rfloor
= \left\lfloor\frac{\ln 36.457}{\ln 1.07}\right\rfloor
= \left\lfloor 53.15\right\rfloor
= 53
$$
 
Thank you so much this is exactly what i needed! :)
 
danielmchugh said:
Thank you for replying again to the post it is exactly what i needed, after putting it in everything worked well but it has highlighted that my original formula is not working properly.

If each time you buy a building the price increases by baseCost * 1.07^N, what formula should i be using to find out the cost if i were to buy for instance 20 buildings in one go.

What do you mean exactly by "the price increases by baseCost * 1.07^N"?

Suppose you already own $N$ buildings and you've paid $Cost_N$ for the last building, what will you pay for the next building $N+1$?
And what will you pay for the one after if you buy both of them at the same time?
 
Sorry I ment to say:

Cost of building = BaseCost * 1.07 ^ Number already owned.

So if the base cost was 200 and i already owned 50 buildings.

Cost of one more building: $$$200 * 1.07 ^50 = $5891.4$$

So rather then running this multiple times over to find out how much it would cost to buy say 20 buildings at once, is there a formula i can use to get that answer directly?
 
Last edited:
danielmchugh said:
Sorry I ment to say:

Cost of building = BaseCost * 1.07 ^ Number already owned.

So if the base cost was 200 and i already owned 50 buildings.

Cost of one more building: $$$200 * 1.07 ^50 = $5891.4$$

So rather then running this multiple times over to find out how much it would cost to buy say 20 buildings at once, is there a formula i can use to get that answer directly?

Okay, so 3 more buildings would cost:
$$\$200 \cdot 1.07^{50} + \$200 \cdot 1.07^{51} + \$200 \cdot 1.07^{52}
= \$5,891.4 + \$6,303.8 + \$6,745.1 = \$18,940.3
$$
which is exactly what you can buy for $\$20,000$.
It brings you up to the $53$ buildings that the formula I gave earlier predicted.

The formula you gave yourself in the opening post tells us how much 20 more buildings will cost by substituting $a=50$ and $b=50+20=70$.
 

Similar threads

Back
Top