# Selecting Fire Power/Albert

Choosing the right fire power can make the difference. For example, Aspid has a tendency to survive less than his enemies because it always fires with power 3, so it wastes lots of energy (well, the good part is that he does lots of damage).

My personal opinion is: If you don't know which power to fire, fire power 3. It will hurt more if you are good hitting your enemy, or it will lower your energy as soon as possible if you are not good at it (thus reducing the maximum bullet damage the enemy can score). Of course, it is valid only for 1vs1 (maybe it is true also for melee, but I don't know).

When do I fire?

It seems clear that it makes no sense to fire if you are not going to hit. But where is this limit? Which is the minimum expected probability necessary to make a shot profitable?

The easier way is to model it is as a gain function:

```//p is the power of your bullet, and a the expected probability to hit
G = (4p + 2(p-1))*a + 3p*a - p
```

The first term is the energy the enemy loses (assuming you fire a bullet with power (1..3)), the second one the energy you gain by hitting, and the 3rd one is the energy you lose by firing. Not that if the gain is greater than 0, then you expect to gain some energy (relative to your enemy). If lower than 0, you expect to lose some energy. Now, you zero it to get the minimum probability.

```G = 0 = (4p + 2(p-1))*a + 3p*a - p ==> a = p / (9p-2)
```

The minimum probabilities are 12% (power 3), 12.5% (power 2) and 14.2% (power 1). Note that you expect to get more energy when the bullet is more powerful. For example, you will fire a power 3 bullet if the expected probability is 13%, but you will not fire a power 1 bullet if the probability is 13%.

Note that the previous function is only good for 1vs1. The previous strategy can lead to a situation where your energy level goes down (simply because it makes the enemy energy level to fall faster than yours), but you probably don't want it in meele. The following function is probably more appropiate for meele:

```G = 3p*a - p ==> a = 1/3 = 33% (note that it is independent from fire power).
```

Which fire power do I use?

Well, now things get more confusing. First of all, the probability to hit is related to the fire power. Second, you have to take into account that the enemy is firing at you, so may be the most energy-efficient strategy is not the best strategy...

Currently, the only bots of mine that use a fire power selection strategy are Mamba's. Depending on the version, they maximize the following functions.

• The energy gain per time unit.
• The energy gain per power unit fired.

Calculating energy gain per time unit

To calculate this gain, the approach is similar to the one used previously. You simply have to add a factor to take into account that a higher power means less shots (because you need more time to cool down your gun) and also that the probability to hit is a function of the power you use (you should also consider the distance, but of course, you are taking the decision for a given distance, so what you need is to keep statistics for different ranges).

```//a(p) is the probability to hit based on the power used
G = (9 * p * a(p) - 2 * a(p) - p) * (1/(10+2*p))
```

Note that this function is almost the same one used before, but multiplied by the inverse of the number of turns you will need to cool down your gun.

a(p) is unknown, so we have to estimate it. The easier way is to make a linear approach like this:

```a(p) = a - b * p
a is the probability to hit with a bullet of power 0 (ie. of velocity 20).
b is the decrease in the probability by increasing the fire power by 1.
Example: If you have a probability to hit of 30% with fire power 1 and
of 10% with fire power 3, then a = 0.4 and b = 0.1
```

You can collect data from your previous shots and easily calculate this parameters.

Now, the easiest way to calculate the best fire power is to iterate some values of fire power (ie. from 1 to 3, steps = 0.5) and to select the one with higher gains.

Another way, more accurate, is to calculate the máximum of this function by derivating and finding the zeros of the derivative. I'w not do it here (I posted a link in Math section that does it for you, but I'w post the final formula.

```Best Power = p = [(2b+9a-1) + Sqrt((1-9a-2b)^2 - 4*9b*2a)] / (2*9b)
Note that this function can have result higher than 3 (use 3 instead)
or lower than 1 (remember that G has a range for p = (1..3)).
```

As an example, for a = 0.4 and b = 0.1 the best firepower is 2.79.