### Irrelevant thoughts on code and games

Most incrementals have a core mechanic where a base resource is increased as you play the game. This currency is then spent on upgrades or buildings that create a simple economic feedback loop which lets you gain resources faster and faster as you play the game.

Even the simplest incrementals have dozens of potential purchases and dealing with all of this math in your head can be rather daunting. The purpose of this post is to do all the dirty work for you and give you a formula that can be used to compare purchases in your favorite incremental.

The trivial example is a clicker that nets you 1 gold per click. There are two buildings available. Building A costs 25 and increases your gold per click by 1. Building B costs 50 and increases your gold per click by 4. Which building should you save up for and purchase?

The trick to this math is imagining what happens if we purchase A before B and vice versa. We know it will take 25 clicks before we can afford Building A and if we purchase it immediately we are now making 2 gold per click. At this rate, it will take 25 more clicks before we can afford Building B. Therefore, A then B costs us a total of 50 clicks.

$$25 / 1 + 50 / (1 + 1) = 50$$

If we switch the order we can use the same solution. It will take 50 clicks to get to Building B. After our gold per click boosts to 5, it will take us 5 clicks before we can buy Building A. Therefore, B then A costs us 55 clicks.

$$50 / 1 + 25 / (1 + 4) = 55$$

These two equations use the exact same formula:

$$clicks = costX / rate + costY / (rate + boostX)$$

The simple comparison is then to take whichever order scores best using the formula to compare each pair of buildings. I find it useful to use special variable names:

$$\begin{eqnarray} r & = & \text{current gold per click rate}\\ AC & = & \text{cost of Building A}\\ BC & = & \text{cost of Building B}\\ AV & = & \text{value of Building A (the increase in gold per click)}\\ BV & = & \text{value of Building B}\\ A & = & AC/r + BC/(r+AV)\\ B & = & BC/r + AC/(r+BV) \end{eqnarray}$$

$$A$$ and $$B$$ are the time taken for each path. The shortest path is best. The major annoyance caused by this comparison is that you end up needing to compare each building against each other building. I’d rather not do that for dozens of purchasing options so we need to find a better way.

What we want is a score for each building that is independent. Then we wouldn’t have to run the comparison for each pair — we could calculate the score for each building and simply pick the best.

To do this, we will begin by expressing the formulas as an inequality. We want to choose Building A when $$A$$ is less than $$B$$:

$$\begin{eqnarray} A & < & B\\ AC/r + BC/(r+AV) & < & BC/r + AC/(r+BV) \end{eqnarray}$$

If we can get all of the “A” variables on the left side and all of the “B” variables on the right side then we can reuse the same value for A when comparing against each other building. I’ve included all of the math here but you can skip to the end if you simply want the result.

$$\begin{eqnarray} AC/r + BC/(r+AV) & < & BC/r + AC/(r+BV)\\ AC/r + BC/(r+AV) & < & BC/r + AC/(r+BV)\\ (r+AV)*AC/r + BC & < & (r+AV)*BC/r + (r+AV)*AC/(r+BV)\\ (r+BV)*(r+AV)*AC/r + (r+BV)*BC & < & (r+AV)*(r+BV)*BC/r + (r+AV)*AC\\ (r+BV)*(AC+AV*AC/r) + (r+BV)*BC & < & (r+AV)*(r+BV)*BC/r + (r+AV)*AC\\ (r+BV)*(AC+AV*AC/r+BC) & < & (r+AV)*(r+BV)*BC/r + (r+AV)*AC\\ (r+BV)*(AC+AV*AC/r+BC) & < & (r+AV)*(BC+BV*BC/r) + (r+AV)*AC\\ (r+BV)*(AC+AV*AC/r+BC) & < & (r+AV)*(BC+BV*BC/r+AC)\\ (AC*r+AV*AC+BC*r + BV*AC+AV*BV*AC/r+BV*BC) & < & (r+AV)*(BC+BV*BC/r+AC)\\ (AC*r+AV*AC+BC*r + BV*AC+AV*BV*AC/r+BV*BC) & < & (BC*r+BV*BC+AC*r + AV*BC+AV*BV*BC/r+AV*AC)\\ AV*BV*AC/r + AV*AC + BV*AC + BV*BC + AC*r + BC*r & < & AV*BV*BC/r + AV*AC + AV*BC + BV*BC + AC*r + BC*r\\ AV*BV*AC/r + BV*AC + BV*BC + AC*r + BC*r & < & AV*BV*BC/r + AV*BC + BV*BC + AC*r + BC*r\\ AV*BV*AC/r + BV*AC + AC*r + BC*r & < & AV*BV*BC/r + AV*BC + AC*r + BC*r\\ AV*BV*AC/r + BV*AC + BC*r & < & AV*BV*BC/r + AV*BC + BC*r\\ AV*BV*AC/r + BV*AC & < & AV*BV*BC/r + AV*BC\\ AV*AC/r + AC & < & AV*BC/r + AV*BC/BV\\ AC/r + AC/AV & < & BC/r + BC/BV \end{eqnarray}$$

And there we have it! If we use $$AC/r + AC/AV$$ we can reuse each side which means we can just calculate the comparative value for each building and then find the smallest value.

There is a small gotcha, however. The above formula reduction assumes that the values and costs from each building are independent from the number of other buildings owned. This is frequently incorrect. If Building B reduces the cost of Building A by 10, then it would alter the values above.

But as long as the following things are true:

• Purchasing more of Building A does not alter the cost of Building B
• Purchasing more of Building B does not alter the cost of Building A

Then the above formula holds true.

If we return to our original example, the values for Building A and Building B are:

$$\begin{eqnarray} A & = & AC/r + AC/AV & 25/1 + 25/1 & = 50\\ B & = & BC/r + BC/BV & 50/1 + 50/4 & = 62.5\\ \end{eqnarray}$$

While $$A$$ and $$B$$ no longer represent the exact number of clicks for each, we still get to compare them and take the smallest. If we add a bunch more buildings:

• C costs 20 and increases our gold per click by 2
• D costs 100 and increases our gold per click by 100
• E costs 30 and increases our gold per click by 3

We can quickly generate their scores and determine the best building to save up for:

$$\begin{eqnarray} A & = & 25/1 + 25/1 & = 50\\ B & = & 50/1 + 50/4 & = 62.5\\ C & = & 20/1 + 20/2 & = 30\\ D & = & 100/1 + 100/100 & = 101\\ E & = & 30/1 + 30/3 & = 40\\ \end{eqnarray}$$

Our new winner is Building C!

As one last interesting note, because we have a direct comparison function we can actually find the point where various buildings are worth the same. What would it take for Building A to also score 30 if we changed its value?

$$\begin{eqnarray} A & = & 25/1 + 25/X & = 30\\ & & 25*X + 25 & = 30*X\\ & & 25 & = 5*X\\ & & 5 & = X\\ \end{eqnarray}$$

$$A = 25/1 + 25/5 = 25 + 5 = 30$$

Likewise, we can ask how much we should charge for a building with value 4:

$$\begin{eqnarray} B & = & X/1 + X/4 & = 30\\ & & 4*X + X & = 120\\ & & 5*X & = 120\\ & & X & = 24\\ \end{eqnarray}$$

$$B = 24/1 + 24/4 = 24 + 6 = 30$$

When choosing your initial building prices, this can come in quite handy.

1. Not sure if you’re still reading this, but this issue is not resolved. Others came to the same score like you, in particular here: http://math.stackexchange.com/q/478494
Note that AC/r+AC/AV is equivalent to AC*(1/r+1/AV), corresponding to cx*(1/p0+1/px) in the notation there. Someone managed to find a counterexample which proves that this solution is not necessarily correct for more than two upgrade options: http://math.stackexchange.com/a/478577
While nobody even properly understands why the formula is invalid, the example shows that it is.

This has spawned a follow-up discussion here: http://math.stackexchange.com/q/525371
But the poster took no effort to try and explain his formula and it’s obscure how he came to it, whether it’s valid (I can’t imagine it should be, since he still asks for another approach in the end of his post).

So basically, as it stands, there is still no general solution to this problem.

• MrHen

2016/04/04 at 4:48 pm

Hey, that’s cool. Thanks for the information! I’ll dig back into the problem again. :)