📜 ⬆️ ⬇️

On the experience of writing AI to a simple 2D shooter

Greetings habrochiteli, I want to tell you about the experience of writing AI to a simple 2D toy in the distant already 2003-2004. I will say right away that I never succeeded in writing essays in my life, but I will try.
Most likely, gamedev specialists will find this post Nubian, but I hope that the rest will be interesting, so let's start from the beginning.

Game world

The game world consists of "squares" of the same size, teleports, jumpers and other elements suddenly changing the player's position are available.
The map contains useful items in the form of weapons, cartridges, first-aid kits, armor, etc. The player is available long jumps in space allowing enough free to move around the game world.

game world

The task of the future bot is to freely navigate on absolutely any new map in order to elegantly succumb to the player, and, if possible, kill as many opponents as possible.
')
Priority system

The essence of the AI ​​bot is built on a system of priorities. Each significant point on the map where you can stand is entered into an array and for each element the weight is calculated based on the current situation.
Significant points are the positions of first-aid kits, armor, weapons and the location of enemies, if they are currently on the ground. The weight of a position with a first-aid kit is inversely proportional to the amount of health and time needed to hit a point and directly proportional to a certain amount of conditional weight chosen for first-aid kits.
If the level of health is complete, then it is absolutely clear that the weight of this point is reset. The situation is similar with other significant points. The positions of the enemies are more priority if they have a low level of health or have no weapons. The positions of the weapon increase the weight in the absence of it and are equal to zero if present. Revision of the goal occurs every few seconds, or upon reaching the position of the current goal.
It would seem that he told everything, but there is one more thing without which the described AI could not work - calculating the time to reach a significant point.

Shortest way

In order for the AI ​​system not to slow down the game process, at the start of the map, the movements are calculated. Moreover, each point at which you can stand is assigned a sequence number, the distance between the points is equal to one player’s step. Before the start of the game, the full calculation of movements was made The bot was consistently placed at each of these points and performed possible actions for itself: a step to the left, a step to the right, a weak jump to the left, a strong jump to the left, etc.
After the action was completed, the gameplay continued until the bot came to rest, after which the result was saved to a two-dimensional array of the form "[starting point] [action] = {touchdown point, move time}". This mechanism allowed the bot to use teleports, jumpers and any other elements of the game world, since the bot just knew that if being at point X and stepping to the left, we appear at point Y after N seconds.
Having this array for the whole world, an array of movements of the form "[start point] [finish point] = {minimum time of movement}" was built. For this, a fairly common wave algorithm was used:
wave algorithm

Only with more actions:
wave algorithm

as a result, at any time, a bot can get an exact time to achieve the goal and a list of consecutive actions necessary for it.

Self study

For the selection of optimal weight parameters (which is more important than a first-aid kit, a weapon, or to kill a weak enemy), a mini-tournament was organized. In each, 10 bots participated in each of which the weight parameters differed by some random variable. After 20 minutes of sparing, the strongest stood out and moved on to the next battle level, where he killed even stronger opponents. The result was a fairly good opponent to a live player - you cannot of course call him perfectly smart, but he is able to retreat and run for the medicine cabinet or cartridges in time.
The remaining moments, such as aiming or choosing the optimal weapon, are quite boring and not worth your attention.
I hope that you have read to the end. Well, finally, a small video of this action:

Source: https://habr.com/ru/post/124906/


All Articles