Artificial intelligence in the fighting game Shadow Fight 3





Game designers regularly face the challenge of creating an interesting AI. This process can be either relatively simple or extremely complex, depending on the needs of the project and the goals that you pursue. Banzai Games senior game designer Mikhail Dragovalovskiy spoke about his experience working on AI for the popular mobile fighting game Shadow Fight 3.



Difference between SF3 and most fighting games



The Shadow Fight series was immediately created with a number of features that distinguish it from many fighting games:





image






AI in previous games in the series



We already worked on AI in our previous fighting game - Shadow Fight 2, but due to the transition to 3D, the appearance of collisions between players and the addition of a shadow shape, we were able to transfer only part of the AI ​​logic, and the rest had to be recreated.



Where to start working on AI?



If you do not make a bot using machine learning, I would single out two captain approaches:



  1. Make a weak bot, then strengthen to the desired level with new features;
  2. Make an invincible bot and then weaken.


The second option sounds very romantic: it seems that you are smarter than everyone and make a cool AI that bends the players, and then mercifully reduces the complexity of the bot. From a development point of view, this may be more convenient, because you can pre-think all the features of the bot’s behavior that you will need and fully imagine its final behavior.



Of course, we chose this option, although we later realized that it wasn’t so simple: with our balance of animations, automatic blocks and a variety of skills, you can almost always find the necessary tactics and abuses that allow you to defeat AI with a high skill. We did not understand this right away, but only when we correctly measured the winrate of the players at different stages of the game. This is despite the fact that for many people, judging by their feedback on the game, the bot really was invincible.



image






How does our AI work?



Triggers or rules of conduct





If you release such a bot against a beginner, with an equal level of pumping, the outcome will depend on the weapons with which the opponents are armed. Who is faster than animation, he will win. Because, in fact, both press all the buttons, well, except that the bot does it a little more meaningfully.



image






Making decisions



The logical step to improve AI is to reduce randomness and add a decision-making system that will move the bot to victory. To do this, you need to understand which decisions are right in which situations. For fighting there are two situations that happen all the time:



  1. The enemy is waiting;
  2. The enemy is attacking.


There are two solutions too:



  1. Need to beat when guaranteed to hit;
  2. You need to block when they hit you and you can’t counterattack.


It turns out that for victory, the AI ​​must interrupt enemy attacks with its counterattacks, or block if it cannot interrupt them.



image






We made two groups of decisions that the bot makes: one - if the enemy is standing, the other - if the enemy is attacking. In order for AI to know exactly what to counter, we came up with collision tables.



Collision Tables



Remember how Doctor Strange looked through all the possible outcomes of the battle with Thanos? Similarly, the collision tables work: the computer calculates the outcomes in advance for each attacking animation in the game at every possible distance.



If in more detail: we launch a blow with a sword and at the same time a blow with a katana, and then we look who will hit whom. We do this procedure for all strokes in the game, at all distances and with delays of a different number of frames (I launched a blow with a sword, and the enemy through N frames - a katana strike). All results are recorded in tables. We do a similar procedure before each update and put the resulting files after a difficult compression algorithm in the finished form in the build so as not to eat up the device’s resources in runtime. The player in the client already has a list of all outcomes, which means the AI ​​knows which hit can counter any hit of the player at a specific distance, or understand that right now the best way is to block the attack.



image






As a result, AI effectively responds to player actions. That is, they countered the counterattacks. Now you need to teach the bot to attack in the optimal way, rather than random hits.



The choice of distance and the right strike



An attentive and intelligent reader will say: “Stop !!! Why even attack yourself if the enemy is always in the autoblock? We must wait for the enemy’s blow and punish him with a quick blow that will interrupt his attack! ” Yes, it is, but a bot that does not move and does not attack without player actions looks at least strange. The AI ​​must be able to choose effective attacks to launch an attack, even if the optimal strategy is to play from counterattacks. To do this, we have developed a system for choosing the best distance and strikes.



As we thought: the player most often uses several favorite punches, which means that the bot will specifically make those attacks that at a selected distance cannot be interrupted by typical player punches. To do this, we:





In theory, it sounds cool, but in practice it turned out not so good: players rarely stand idle - they almost always press buttons and press the bot, so the bot is often guided by a decision-making system based on player attacks, instead of taking the initiative. So choosing the right strike only works against passive players, but choosing the best distance has improved counterattacks - they have become more diverse, because the bot has more choices.



At this stage, the bot is already quite effective: it knows how best to start its attack, it knows when to counterattack and when to block. Next, we will talk about systems that allow you to set the required level of complexity and weaken or strengthen the bot.



Resentment and forgiveness



This is an interesting concept that underlies our balance sheet system. Through it, we create a sufficient level of complexity for beginners and professionals, as well as motivate the player to use different strokes, which increases the interest and entertainment of the game.



The essence of the concept lies in the name: the bot takes offense at the player’s blows, which the player repeats from time to time. Resentment is a counter that reduces the chance of making a suboptimal decision and increases the chance that the AI ​​will conduct a perfect counterattack.



On the other side of the scale lies forgiveness - the bot begins to increase the chance of choosing non-optimal solutions and reduce the chance of a counterattack if the player stops spamming and switches to using other hits.



image






If we want to make a complex bot, we reduce the speed of forgiveness, and also set a high level of starting resentment, if we want to make a weak bot, the opposite is true.



Delay



Every bot in SF3 has a delay in making a decision. This is a parameter that causes the bot to pause N frames before choosing an action option. A useful thing - allows you to create the appearance that your opponent thinks. The simpler the bot we want to make, the more delay we set for it.



Condition check



To make some parts of the game more emotional, we use a check on the states and relationships of various game variables.



We can make some bots not use throwing weapons at certain distances or reduce the speed of forgiveness if a player has more hit points than a bot, or make AI more aggressive if it has few hit points.



image






In general, this is an add-on that we use to generate situations in which the bot dramatically changes its behavior and effectiveness. If necessary, we restrict some actions of the bot in various situations, for example, it is more difficult for a bot to make a throw than for a player, otherwise the player would never be able to throw a bot, because the distances at which the throw can be applied would be the same, and the bot would just have time to start its animation earlier.



Based on all of the above mechanisms, we have created 9 basic AI presets that we use in different situations and sections of the game. This approach allows you to change the balance point by point and measure changes.



Result



The process of creating bot tactics was a long one. We introduced these features gradually, so sometimes our players suffered, and after them we suffered, reading their reviews and curses. When we downloaded the latest iterations of edits, the flow of negative reviews regarding the behavior of opponents stopped, which we are very proud of.



image






Our experience shows that further bot improvements with the current approach do not give a noticeable increase in quality. Therefore, we started working on a neural network that will simulate the actions of a player.



There are several things that push us to this decision:



  1. We are developing a new PvP fighting game - Shadow Fight: Arena. Its peculiarity is heroes with very different tactics with different abilities and mechanics. To make a realistic bot under these conditions is many times more difficult than in the case of SF3;
  2. We already have positive experience using neural networks to solve animation problems in our Cascadeur product. This is a program for creating realistic animations that we use for all our games;
  3. We want to assemble a team of competent ML specialists in order to further increase our expertise in this area and use it to improve Cascadeur and our games.


The Banzai Games team requires a Deep learning researcher. Read more about the vacancy here .



All Articles