Stories

May 28, 2014

## 5. Scaling

So, we have revised the main function types used for the balance engineering and chosen one. In our case the most suitable for us is a power function, where a lies in the range from 0 to 1 (in other words, it is some root). What’s our next step?

The blue line shows our selection of points (which we’d like to approximate) and the black one represents a graph of the square root of x. You can see that the shapes of these two graphs are more or less the same, but there’s a long way before our function can coincide with the table values.

Here you should understand one very important thing: adding to the equation different numbers (summands, multipliers) we can modify our graph keeping its basic shape the same. Below I’ll give a short list of possible operations meant to modify the function graph. In this list letter A will denote some new number which will be introduced into the equation y = f(x).

• y = f(x) + A. If you add a number as a summand you will shift the whole graph up (if A is positive) or down (if A is negative).
• y = f(x) * A. If you add a number as a multiplier you will squeeze (if A is bigger than 1) or stretch (if A is smaller than 1) the graph along the y-axis and if A is negative you will flip the graph about the x-axis.
• y = f(x+A). If you add a number as a summand to the argument (every time you meet it in the equation) you will shift the graph to the left (if A is positive) or to the right (if A is negative) along the x-axis.
• y = f(-x) will give you a flipped graph about the y-axis.

In our example in order to stretch our graph along y-axis we should introduce a positive coefficient bigger than 1 to the equation.

On the left graph the blue curve represents our table set of points and the black one fits equation y = 2*√x. We can see now that our multiplier (so-called scaling coefficient) has helped us to get in the home stretch of the table values approximation. Hurray!

## 6. Deviation and criterion of accuracy

Our equation y = 2*√x approximates the number sequence we got from the table rather accurately. But if you look at the graph from above however you might notice some divergence (it’s seen especially clearly in the intervals (x = 30, x = 250). We have already excluded from consideration the points that were obviously tuned manually by the project developer, so now this divergence can be explained by round off. In fact, with x = 80, for instance, our formula result is 17.889 (even now we had to round it off to the third decimal place), while in the table we see that 80 minutes correspond to 18 coins. In this situation such deviation is of little consequence to us, but quite often the situation may be different.

Suppose you’ve got two different equations that both seem to approximate your function rather accurately. This is possible if you try to approximate your number sequence with two different power functions (with the 2nd and the 3rd powers) and which you scale so that in the given interval they lie closely to each other. How can one make out which one to choose?

To answer this question we have to introduce some criterion according to which you can estimate how accurately your function repeats the table values. Such a criterion can be represented by an expression which has function deviations from the table in each of the points described in this table serving as arguments.

Let’s try to estimate accuracy of one function from our example. For this purpose we will introduce one more column to our table – this column will contain price values our function gives for each plant (or rather for each time of ripening). As we can see not in a single line the function value matches the table price value!

In the right column we have calculated (by means of simple subtraction) the difference between the actual price value from the table and the values calculated by the formula. Note that we’ve calculated only moduli of these differences (i.e. only their significant part, without signs). That’s because we are interested not in the way the formula is wrong, but how much it is wrong.

The calculated differences are the arguments for the expression which will estimate the formula accuracy. There’re many ways to form such expression. The simplest expressions – are arithmetical average and sum.

The fundamental distinctive feature is – the arithmetical average can both increase and decrease. Here the formula’s accurate behaviour in one place compensates its inaccuracy in others. In the case of sum these deviations will be stockpiled and the expression will show the function total deviation from the original values. Moreover there’re square (or mean-square) deviations which are also used very often. In these expressions all differences are first of all raised to the second power, then are summed up and divided by the quantity of elements and after that the square root is taken of the final number.

In our case we simply sum up the moduli of differences and get a number approximately equal to only 3.12, which is not bad taking into consideration the fact that the table average price value amounts 15.4.

So, if you have to choose from several approximating functions take the one with the least deviation. If you have to choose between the two functions with similar deviations take the simplest one. Once we’ve decided on a formula (i.e. it’s rather accurate), we can take it to our project, scale it, watch its interrelation with other laws of our game and etc.

## 7. Function analysis

In conclusion let’s talk a little bit about what you can find out about the function from its equation. The function analysis can help us to understand how it behaves in different parts of the coordinate space, what you should expect from it and what you should not.

We are not going to delve into the mathematics of so-called function singularities (points where the function behaviour is completely different from its usual one – these are for instance both coordinate axes for a hyperbola or the origin point for a parabola). We’d better focus on the most important things – what we can tell about the function increase and decrease in different parts of the space.

We can study the function increase and decrease with the help of its derivative. A derivative of function f(x) – is another function denoted f’ which tells us how fast the value of f(x) would change with x changing too. It is evident, that f’ is another function of x, so we can put it down like f’(x).

For each function type it’s better to use its own technique of a derivative calculation. We’ll consider power functions as the most frequently used ones. Have a look at the standard form of a power function:

f(x) = a*xb + c

Its derivative will look like this:

f’(x) = a*b*xb-1

As a matter of fact in order to find a derivative we take the power of x and place it before x as a multiplier and we reduce the power of x by 1. The derivative of constant c is equal to 0 because constants never change.

Example: derivative of f(x) = x3 is equal to f’(x) = 3*x2.

It’s a little more complicated with a root, for instance. The algorithm is the same, but the power of x becomes negative in the end.

Example: derivative of f(x) = √x is equal to f’(x) = 0.5 * x-0.5 = 0.5 / x0.5 = 1/(2*√x).

Now that we know how to derive, let’s talk about why we need it. A derivative allows to predict whether the function increases or decreases and how fast it does it.

Suppose we have two functions:

• f(x) = 10*x2, derivative f’(x) = 20*x. Suppose this function is responsible for some overall progress of a player (i.e. amounts his income in one form or another).
• g(x) = x3, derivative g’(x) = 3*x2. Suppose this function is responsible for the game complexity increase (i.e. amounts player’s expenditures in one form or another).

In this example in both cases let a player’s level be the argument. In order to compare the rates of the two functions increase at a certain point let’s find their derivatives at this point.

• For point x = 2: f’(2) = 40, g’(2) = 12.
• For point x = 10: f’(10) = 200, g’(10) = 300.

First of all, both derivatives are positive, that means both functions are increasing. Besides, you can see that function f increases faster at first but then it yields to g in rate. As the bigger x is, the faster g increases, there’s no doubt that sooner or later g will outrun f. If we want to know the exact point, we’ll have to solve equation f(x) = g(x). The solution of this equation is two points: x = 0 and x = 10. The first one doesn’t suit us (levels start with 1), but the second is the exact point where the player’s expenditures will exceed his income. Here’s the picture of the game economics – up to the 10th level the player has proficit, after that this proficit keeps decreasing to zero until it becomes deficit.

These are the basic rules to study a function increase or decrease with the help of its derivative:

• If the derivative is positive, the function increases;
• If the derivative is negative, the function decreases;
• If the derivative is equal to zero or doesn’t exist, the function can switch from increasing to decreasing and v.v.

## Conclusion

As you have seen, reverse balance engineering – is not only difficult, but also very dreary. Your task here is – to consider a lot of game variables in reference to each other, to plot a lot of graphs and to approximate a lot of table values in order to obtain a lot of functions. But the result is worth it! Having carried out the correct analysis of a game balance you’ll be able to gain an insight into its inner mathematical arrangement, to see threads that integrate the whole project. You can use these threads in your own project – perfecting them or leaving them the way they were designed in the most successful projects of the industry.

And finally, if you please, one last piece of advice. Keep in mind that a mathematical game model is only a foundation for the perfect balance. Your own game experience, recommendations of testers, players, friends and colleagues are no less important.