Most of AI coding since the last dev blog was about torpedo attacks of surface ships, and now I can tell about some of its interesting aspects.

### Torpedo triangle

Torpedo triangle is a triangle that has an attacker ship position, a target ship position and an expected hit position as vertices. Finding the point where a torpedo hits the target ship if it moves at the same speed and course is the goal of torpedo triangle calculation. You can read more about this here__,__ and an example of the torpedo triangle is in the image below:

In some cases, the torpedo triangle can’t be calculated due to a limited move distance of the torpedo or a greater speed of the target ship compared to the speed of the torpedo. But if the torpedo triangle is successfully calculated, we can expect a hit:

### Turn based reality

The described above can work in real life and in a real time game, but in a turn based game, the target ship does not move when the torpedo performs its turn. Thus, the torpedo can cross the move direction of the target in front of the target that is not moving, instead of hitting it. This situation is clearly visible in the animation below:

In some rare cases, when the torpedo can reach the target in one turn or the target is immobilized, this is not a problem, but in other cases, the game AI should correct a hit point to one of close ‘static’ positions of the target ship. The game AI also has to ensure that a number of turns required for the torpedo to reach this position corresponds to a number of turns required for the target to reach the same position. An example of hit position correction is shown in the scheme below:

### Difficulties of a real game environment

The above can only work in simple cases when the target does not change its speed and course. That is why the game AI takes the calculated hit point and calculates few azimuth sectors based on it:

- The first azimuth sector contains boundaries of the target when its position is at the hit point, which calculation is described in the previous section.
- The second azimuth sector contains boundaries of the target, if it attempts to perform a maximum rotation to the right and to the left,
**without changing its speed**. - The third azimuth sector deals with the target, if it attempts to
**accelerate as much as possible**, and contains expected boundaries of the target when moving straight and when rotating both sides. - The fourth azimuth sector is similar to the previous one, but deals with the
**‘maximum deceleration’**case.

A number of turns required to predict positions of the target when calculating the azimuth sectors above corresponds to the number of turns required for the torpedo to reach the corrected hit point in the torpedo triangle. As you can see in the next image, the calculated azimuth sectors often intersect and cover most of possibilities for the target ship to evade the torpedoes.

### Selecting a torpedoes count

When deciding how many torpedoes the game AI can spend on the target, it takes a value that is calculated based on the displacement of the target ship as a base value. At higher difficulty levels, this value is adjusted taking into account a visible buoyancy of the target, that is, the number of torpedoes decreased if the target ship looks more submerged in water than it should be in a normal state. At the higher difficulty levels, in order to calculate the final number of torpedoes, the game AI looks at a threat value posed by the target ship for the attacking ship. At lower difficulty levels, the game AI just randomly performs one of the following actions:

- Simply uses the base value calculated above. The game AI at the higher difficulty levels does this for targets with minimal threat values.
- Corrects the base value based on a mobility coefficient of the target. The mobility coefficient is calculated from a type of the target ship (i.e. destroyers are obviously much fast than armored cruisers, for example), and a visible state of its propulsion system and steering system.
- Makes corrections from the previous list item and doubles the resulting number of torpedoes.
- Assigns all available torpedoes to the target ship. The game AI at the higher difficulty levels does this for targets with very high threat values.

### Settings launch azimuths to the torpedoes

After calculating the number of torpedoes, they should be assigned to the azimuth sectors described above. To assign the torpedoes, the game AI looks at a speed of the target ship and tries to predict whether the target ship can accelerate from its current speed or not.

If the target ship can accelerate, then the torpedoes are divided into three equal groups. The first group is assigned to the first two azimuth sectors, where one torpedo is always assigned to the first sector, which is a result of a naive assumption that the target will move at the same speed and in the same direction. The remaining torpedoes of the first group are assigned to the second azimuth sector, which is intended to cover possible target maneuvers without changing its speed.

The second and the third groups of the torpedoes are assigned to the last two azimuth sectors, which are intended to cover the target maneuvers with increased or decreased speed.

If the target ship can only decelerate (that is, she is already moving at maximum speed), then the torpedoes are divided into only two equal groups instead of three, and the ‘maximum acceleration’ azimuth sector is not used.

Dividing the torpedoes into groups makes no sense if the torpedoes are expected to hit the target ship in one turn, because the target ship will not perform its turn before the torpedoes hit her. In this case, all torpedoes are assigned to the first azimuth sector.

A torpedo can be reassigned to another sector if the game AI unable to found a torpedo turret that can be aimed at the original azimuth sector. A final azimuth for each torpedo is randomly selected within the designated azimuth sector.

The method described above is not ideal, but it seems to provide a decent probability of hitting maneuvering targets and gives some flexibility for additional tuning for a better result in future. Of course, attacking the target with just a few torpedoes is unlikely to be successful, as it was in real life. To increase a chance of success, the attack should be performed simultaneously by several ships, and each of them should launch several torpedoes.

That’s all for today.

Good luck.

I am genuinely impressed on how much work and attention to detail you put in to this. Can't wait to play it.