DEVELOPMENT OF THE SYSTEM OF A GAME ARTIFICIAL INTELLIGENCE BASED ON CONCEPTS OF FINITE STATE MACHINES
Article is devoted to the development of the system of an artificial intelligence intended for creation of the game project in a genre of role-playing game (RPG). The features of the main development stages and component necessary for interaction with the game environment are considered. Implementation of system for creation of games is described. Discussion of different development approaches of a game artificial intelligence is carried out
Keywords
software, software product, computer game, types of computer games, game artificial intelligence, finite state machines, C# language, Unity game engine, program architecture
AUTHORS
Elena N. Drozdova
The candidate of Technical Sciences, the associate professor of Informational and the controlling systems Department, St. Petersburg State University of industrial technologies and design,
St. Petersburg, Russia 18, Bolshaya Morskaya street, Saint-Petersburg, 191186, Russia.
E-mail: endrozdova2@list.ru
Alexander N. Kovalenko
The candidate of physical and mathematical sciences, the associate professor, the head of the department Informational and the controlling systems, St. Petersburg State University of industrial technologies and design,
St. Petersburg, Russia 18, Bolshaya Morskaya street, Saint-Petersburg, 191186, Russia.
E-mail: akovalenko@uprint.spb.ru
1. Introduction
In traditional researches in the field of the artificial intelligence (AI) the purpose consists to train the computer to elements of a human thought (Artificial intelligence, 2016), (Drozdova, 2016), (Drozdova, 2017). In the project Kismet (Massachusetts Institute of Technology) becomes attempt to create AI with such properties as, learning capability, social interaction, expression of emotions.
In computer games, the artificial intelligence is not used in the true sense of the word (Bachurina, 2016), (Ryabova, 2016). Unlike the full-fledged systems of an artificial intelligence, the game intelligence does not possess simulation of "consciousness" and its analytical skills are not so developed. The game artificial intelligence solves problems only in the game environment, and, as a rule, represents a set of behavioral algorithms. Further the artificial intelligence will be considered only in this sense.
The Game Artificial Intelligence (GAI) performs several functions.
1) Creation of illusion of a game against the living person — as a rule, in the majority of games GAI undertakes a role of control of "bots" — characters of a game whom the
player cannot control. Bots can behave hostilely, friendly, and, in certain cases, ignore the player. Depending on the purposes of function of bots vary from a simple pursuit of the player to simulation of life in game conditions.
It is important to mark that working according to the program, bots will not make tactical mistakes. For example, when firing from weapon, strike accuracy will be absolute. To avoid a similar superiority, bots often project with defects. It creates to the player more comfortable conditions.
2) Control of events. In addition to control of bots, GAI is responsible for the events, which are taking place in the game environment. Such as accidental events, changes of weather conditions, control of interaction between the player and objects, etc. In process of growth of complexity of the game project, more and more extend obligations of GAI.
For execution of the role, GAI shall interact with substance (entities) in a game. There are two basic approaches to this interaction:
• From GAI to substance — such approach means that GAI is permanently active, and calculates decisions for finding of an optimal solution in the given situation.
It allows GAI "to think" globally, and mostly is suitable for complex strategic games.
• From a substance to GAI— in this approach, GAI is active only when it is necessary "to consider" the decision. Such approach is effective in games with a large number of substances, which do not need to update the status permanently.
It is obvious that in order that GAI could make intelligent decisions, it needs to perceive the game environment and to have the structure corresponding to complexity of tasks. The game is more difficult, the GAI shall process bigger quantity of objects and events.
In simple games, check of location of the player can be the single function. In difficult projects tasks of intelligence can include search of a way in a dynamic surrounding, team operation, ability to foresee actions of the player. All this will be based on the put knowledge or the got experience.
The basic principle, which is the cornerstone of GAI operation, is decision-making. The game system shall influence objects by means of GAI. In this article, implementation of GAI on the basis of finite-state state machines is offered. It is used as a method of simulation and implementation of the object possessing different statuses during the life. Each "status" represents physical conditions in which there is an object, for example, a set of emotions. Emotional statuses have no relation to emotions of AI, they treat the behavioral models corresponding to a game context.
In Saint Petersburg State University of Industrial Technologies and Design at department of the information and controlling systems, the researches in the field of the game artificial intelligence (GAI) are conducted. These researches are based on the finite-element method. They are applied during creation of the game project in a genre of role-playing game. This game is written in the C# language in the Unity environment.
6 Modern European Researches No 2 / 2018 2. Methods of carrying out experiment
We will consider features of the main stages of operation over the project.
2.1. Development of a GAI method within a genre of three-dimensional role-playing
game
In a figure 1 the general diagram of the GAI system and the accompanying components is provided.
Relocation system
Game artificial intelligence
Attacks algorithm
Shelters system \7:
Characters description
Change of
the course
5 Information
classes
FIGURE 1 — THE GENERAL DIAGRAM OF THE GAI SYSTEM AND THE ACCOMPANYING COMPONENTS
Further we will consider features of the main development stages of GAI and components necessary for interaction with the game environment.
2.1.1. Fight process description
In the majority of games, fight is one of the most important components. Therefore careful premeditation of system of fight is necessary during creation of a computer game of this type. It was decided to use the step-by-step system of the description of events which is typical for tactical games. In this case, the player can control several characters at the same time. Such approach allows to use fully the tactical potential of a game, gives to the player the chance to evaluate a situation and to make the reasonable decision.
Fight is conditionally the infinite loop consisting of two phases: course of the player and course of the computer. Fight stops if characters of the player or the computer are destroyed. During fight the player carries control of group of characters. Each character can possess a unique set of characteristics (different types of arms, ability to conduct a battle, ability to survive, etc.). At the same time, the number of actions of each of characters is restricted to points, which are filled at the beginning of each course.
2.1.2. Change of the course
As it was mentioned above, fight takes place in a step mode. The class which is consisting of conditionally infinite loop and including two phases was developed. It is the course of the player and the opponent's (computer) course.
Alternation of phases is one of key factors, it directly influences a possibility of making by the character of any actions.
2.1.3. Characters description
Game characters are dynamic objects, that is, have a row of characteristics and properties which change during the game. For tracing and change of characteristics of each of characters, their interaction and interaction between characters and a game surrounding, the class Stats was developed.
This component stores important variables and changes them in compliance the set conditions, for example, recovers points of actions at the beginning of each course.
2.1.4. Information classes
In the course of the game, the player can find new equipment and also use different types of the attacks. Characteristics of equipment and the attacks need to be stored as they are used when calculating efficiency of the attack.
For a solution of the problem of storage, by means of the C# language, the constructions representing simple tables of databases were created. In total three information classes were realized: arms, armor, types of the attacks.
2.1.5. Relocation system
Process of relocation consists of two stages.
1. Search of a way — process of finding of a way in three-dimensional space.
2. Relocation on the constructed way — relocation of a three-dimensional object on the way which was constructed at the first stage.
Search of a way. Search of a way means process of finding of a route between two points of space, which allows to bypass hindrances. For implementation of search of a way the algorithm A* was selected. It is necessary for use of this algorithm that a field of a game it was broken into points. The ray with a step 1 virtual meter perpendicular to a game field is for this purpose built. With its help the array from a set of points in space is created. The set of these points form the undirected graph.
This method allows to collect the position information of each object in space and to set "impassable" places,
After creation of a graph, the algorithm A * allows to find the shortest way from one point to another by viewing of possible ways which can bring into an ending point. In the course of search, the algorithm appropriates to each adjacent point of weights which is used for determination of a priority of transition to the following point. Thus, creation of a way does not require search of all available points as the algorithm will select only the best options.
Relocation on the constructed way. When using an algorithm A*, the required option of a way will be presented in the form of an array of points in space. Before relocation, the system graphically displays a way of relocation in order that the user could see the direction of movement of objects. Along with it relocation cost is calculated. For implementation of movement the normalized vector towards the following point is
created. Further the model of the character moves until the distance between a point and the character does not become zero.
2.1.6. Shelters system
For introduction of realness into game process and also for extension of tactical opportunities, it was developed the system of shelters.
Shelters were designed in such a way that each of them is a separate independent substance with the simple principle of operation. Shelters were designed so that each of them was a separate independent entity, and the principle of operation with them was simple. Depending on whether the current course is the course of the player or the computer, the algorithm of operation of the shelter uses an array of either characters of the player group, or characters of the computer group. When checking the shelter on safety, from the central point of the shelter the rays directed to each character in turn are issued. If at least one of rays gets to any of characters, (that designates absence of hindrances on the way of a ray) that shelter is considered dangerous.
Two types of shelters are now realized: complete and incomplete. If the character is behind the complete shelter, then such character cannot be attacked. The incomplete shelter, in turn, reduces probability of hit in the character by 50%.
2.1.7. Attack algorithm
For completion of a game, it is necessary that one of the sides destroyed characters of other side. For extermination of the character, it is necessary that his index of health as a result of the attack became equal to zero. Process of the attack can be divided into three main stages.
Obtaining information on characters. For attack making, it is necessary to obtain data on characteristics of the attacking character, and the characteristic of the purpose. Existence of the purpose is a necessary condition. The character and the purpose are selected by the player by means of the system of a choice of the purposes, or they are selected by GAI (depending on the one who controls the character).
Check of conditions of the attack (PreAttack method);
Calculation and plotting of a loss (AttackEnemy method).
PreAttack method. In a computer game there is a set of situations in which it is impossible to make the attack. For example, if the attacking character does not have enough points of actions for attack making. The algorithm in the Figure 2 checks characteristics of characters and existence of hindrances (shelters) between characters and the purpose. If at least one of conditions is not observed, the attack is cancelled.
Playet Weapon =Wea p<Hiln Ida Im-i Weapon FmOiDptaua(s^>s kt=^Se k-rKlPloyn Weapon Id); mMM> SUmntelireiafcMsi^OrOehiu^ss-sJd =Selec1edf>tayH._SkM); EnemyAimoi=MnokaMnAnmniRniOctiiils =>sJ4=SckcMPkyn. MiikiUI:
*<! Enemy AIM || Enemy = nuM || iSefecfedPtoye». Akve || h*xo«atef.tag h= cunentTsq)
icbin Cake;
* <!PlayetWeapon Ranged) attac» Range=4.11;
ete
flttecfc Range = PtoyetWeapon J>nlanu-+Pt>ye<S*il.Distance.
V (_d Is lance «attack Range) return hist;
FIGURE 2 - AN ATTACK ALGORITHM IN THE PREATTACK METHOD
AttackEnemy method. This method is executed if attack conditions in the PreAttack method were successfully checked. In an algorithm in the Figure 3 there is a calculation of chance of hit, calculation of a loss and also calculation of the remained health of the character by which the attack was carried.
V
if (Enemy IslnPartjaICover)
I
cover Modifier = 49;
GameManager.B«icGUI.NeiiaAc<iirity4Ene<ny.nane+~ Haxowncn m HBCnrMUH yipr>riiwM, ncHanbTKHa non«neHHe* + (_coverModifwrM)+~%~);
)
else J covorliodiher = 0; J
hitChancc = ((Random Range(0l. 99t>*f,laycrWoapon JVccuracytPlaye(SkillAccuracy)-coverModitier+Enofny Armor Dodge Penalty);
lf( hitChance <100) {
Allac k Damage = 0;
GafneHanager.Bas>oGLII.Ne«vAc(xvity(Sel«c(DdPfayer.nane + ~ npouaiMaaeTC» cnoco6Hocn>io~ ♦ PlayerSkillSkilMame);
J
FIGURE 3 - AN ATTACK ALGORITHM:IN ATTACKENEMY METHOD
2.2. Implementation of system of a Game Artificial Intelligence
2.2.1. General algorithm of operation of GAI
One of the main objectives of GAI is simulation of behavior of the person. Implementation of system that in details recreates behavior of the person w'll be too expensive, and its development will occupy a large number of time. However, in many games the high level of realism is not required. In this regard, finite state machines are quite often used to creation of GAI.
Structure of the finite state machine such is that transitions depend on the changing environment (inputs) and a current status of the automatic machine. Thus, the finite-state state machine will transform a chain of characters of an input to a chain of characters of an output. Structure of the finite state machine is such that transitions depend on the changing environment (inputs) and of a current status of the automatic machine. Thus, the finite state machine will transform a chain of symbols of an input to a chain of symbols of an output.
During the GAI-operations serial control of each of characters available to it is executed. The main method of the GAI system is the StateSelectionAndExecution procedure (the Figure 4). It is possible to select two main stages:
• check and choice of a status (SelectState method);
• execution of instructions of this status.
foreach (GameObjectaiUnit in AlUNit) (
GetUnitJnto();
CurrentVictifn = null;
CurrentAIUnit = ailinit;
Control IcdUniL KActive = true;
if (¡Control ledllniL_Alive) continue;
SelectStatefConbolledUnitCharactEfStatB);
Condition LislJContTolledUnitChanrctefStalB);
yield return StartCoroutirie(_condibonCoroutineName};
ControlledUniLlsActive = false;
}
FIGURE 4 - THE GENERAL ALGORITHM OF GAI OPERATIONS
SelectState method. This method is responsible for a choice of a certain states. In total in a game six states are developed: readiness, aggression, approach, rage, retreat. An output of states is the algorithm of behavior of the character, and the inputs leading to change of a state are its characteristics. They are specified in the table in the Figure 5
Описание
FIGURE 5 - THE STATE DIAGRAM
2.2.2. States for characters controlled by the computer
Readiness. Readiness is start state for all characters. The character who is in a ready condition has enough points of health for fight guiding. If the opponent is found, transition to a status "aggression" or "approach" is made. It becomes depending on the current weapon. Otherwise, the character will remain in this state and will move in the accidental direction to accidental distance, imitating terrain patrol.
Restoration. According to the name, being in this state the character recovers a part of points of health. However, an indispensable condition for transition to this state from aggression, approach, retreat and rage is absence of direct threat. In other words, the character shall be in the complete shelter, or in a zone of its visibility there shall not be hostile fighting units.
Aggression. This state is the main fighting state for characters, who are run by the computer. Being in this state, the character will aim to approach on distance sufficient for defeat of the purpose. Characters only with a close combat weapon can pass into this state. But it is possible to expand game conditions, and then this algorithm can be executed also in case of weapon of distant fight.
In a figure 6 the situation is shown when the character controlled by the computer is in the attack radius, but as on its way there is a building (the complete shelter), he cannot make the attack. In this case gAi reduces the radius of the attack to a certain value that the character controlled by it approached closer the purpose. As the algorithm A* guarantees finding of a way which carries to the purpose (if such way exists), then, gradually reducing the radius of the attack, GAI brings the character controlled by it in a zone of direct visibility to the purpose.
FIGURE 6 - THE ALGORITHM OF A STATE "AGGRESSION"
Approach. This state is the main for characters who use weapon of distant fight. In general, the approach algorithm (the Figure 7) can be described as follows:
1. To run for search of the next incomplete shelter which will allow the character to attack the opponent. If such shelter is not found, then to run for search of the next incomplete shelter within one course;
2. To make attack attempt if attack conditions are met.
CZ^ED
FIGURE 7 - THE ALGORITHM OF A STATE "APPROACH"
Rage. The rage is a status analog "aggression", however the character who fell into rage loses over himself monitoring, and will attack any character who followed at it ways whether regardless of that this character is an ally or the opponent. Transition to a state "rage" is carried out from a state "aggression". A condition of transition is the given probability, which that is higher, than health of characters is lower.
The player and two characters controlled by the computer (1, 2) are figured in the Figure 8. The character 2 fell into rage, and the character 1 will become his purpose as he is the next purpose. Other differences of a state of rage from a state of aggression are gain of the attacks and weakening of the armor of the character. Also, the character who fell into rage becomes fearless therefore transition to a state "retreat" is impossible.
Игрок
Певссжлж 1
ЗеШ&В
Персонаж ?
^BHj
.a
FIGURE 8 - TO ALGORITHM OF A STATE "RAGE"
Retreat. The character who passed into this state will try to disappear from the opponent (the Figure 9). Transition to this state comes from states "aggression" or "approach" if the quantity of points of health of the character are two and a half times less, than at his opponent. Also, the character will pass into a state "retreat" from a state "restoration" if the shelter in which he is stopped being safe.
m ■jftf's,
Finding TangetCPlayer");
GatUnitinfoO;
il (!CoverFound(Covere, 30, "FulfCover*", CurrentAlUnit)) {
CoverFound(Covers, 50, TullCaver", CurrentAlUnit);
}
actualDistance = 0;
distance =Vectof3.[>stanc»(transform_pos4bon, NewWaypoint);
if („dislance > „actualDistance) {
yield return StartCoroubnefPureuitl");
}
begin
data acquisition about the character and the purpose
search of the complete shelter - CoverFound
r
relocation call - Pursuit
i r
^ end ^
FIGURE 9 - TO ALGORITHM OF A STATE "RETREAT"
2.3. Other GAI methods
Also in the GAI system the methods realizing the following support functions are used.
FindingTarget method. The FindingTarget method is called every time when check of a status of the character is necessary for finding of the purpose. Around the controlled character the invisible sphere with the given radius is created (visibility range), and all characters of the player who got to this sphere are added to a special array. Further, there is a sorting of an array according to two criteria: on distance from the character to the purpose and by amount of health of characters who by means of coefficients are brought to a general meaning. As a result of sorting, the character with the highest general meaning becomes a main objective.
Pursuit method. This method is the intermediate between GAI and a component of relocation. The method transfers coordinates to a relocation component, the link to the moved character and also comprises basic checks on a distance. These data are necessary for calculation of relocation, count of quantity of points of action, etc.
AttackCycle method and Attacking method. Similar to the Pursuit method, these two methods serve as an interlayer between GAI and a component of the attack.
CoverFound method. This method is called when the character needs the shelter. The method defines: the maximum distance from the desirable shelter, shelter type, and the purpose from which the distance will be calculated. The method by comparing selects shelters in the given parameters. When finding the shelter, the method appropriates shelter coordinates to the intermediate variables
3. Results
As a result of this research the system of a game artificial intelligence (GAI) on the basis of finite state machines is developed. It includes the following components:
- the component which is responsible for change of the courses;
- information classes (databases);
- the classes which are responsible for basic mechanics of game characters: relocation, attack, etc.;
- system of a choice of the purposes;
- the component of an artificial intelligence capable to perform the following operations:
• change of statuses;
• a purpose choice on the basis of priorities;
• search and creation of a way;
• relocation on the constructed way;
• choice and attack of the character of the opponent.
The developed system of a game artificial intelligence is intended for creation of the game project in a genre of classical RPG.
4. Discussion
We will discuss different development approaches of a game artificial intelligence.
a) GAI founded on a rule set. Such approach is used when from an artificial intelligence nothing is required except a set of rather simple actions (Kehoe, 2009). In this approach of the substances (characters) work according to the ordered behavior, in fact it is just a set of triggers and scripts. Despite simplicity, the correct distribution of rules and substances creates illusion of intelligent behavior of characters.
b) Approach based on finite state machines. In this case statuses are peaks of directed graph. Each status has the rule set and also conditions for transition to other status. Finite state machines are development of system of "rule set".
c) GAI based on a behavioral tree. This method is modification of a method of finite state machines. Instructions, which are described in statuses are responsible for all executed actions (Decision Tree vs Behavior Tree, 2013), (Lim, 2009). Unlike finite state machines, transition conditions between statuses are carried out in a separate element of architecture of GAI.
d) GAI based on the scheduler. This approach also uses statuses, but approach to their change changes. Achievement of a specific goal is result of operation of the scheduler. It is reached, serial change of statuses. At the beginning of a cycle of operation of the scheduler some purpose is created - statuses which will allow to reach it shall be described. After receiving the purpose, the scheduler builds a circuit from the actions necessary for its achievement. Important feature of a method is that process goes upside-down. First, those statuses which allow to achieve an ultimate goal are defined. If the subsistence cannot reach these statuses, then the way for transition to these statuses is defined. As a result of this process the scheduler builds a circuit from the statuses carrying to the end result. In some situations to the purpose can carry several circuits. Shorter circuit or a circuit, which is more adequate to a request, is in that case selected.
e) GAI based on the usefulness evaluation. In this approach the status of the character is defined according to his usefulness at the moment. It is the best for situations when GAI on hand has a large number of possible statuses, which the character can enter. When using this method there is no need to build the full action plan. It can accelerate
process. However, such algorithm can take away GAI from the scheduled purpose, or to be not the best.
In table 1 results of the comparative analysis of the GAI design approaches are provided.
TABLE 1 - ANALYSIS OF THE GAI DESIGN APPROACHES
Design approaches Design complexity Speed development Change of statuses Architecture complexity
Rule set Low High Is absent Low
Finite state machines Average High Is present Low
Behavioral tree Average High Is present Average
Scheduler High Average Is present High
Usefulness evaluation High Low Is present High
On the basis of the carried-out analysis it is possible to formulate the following requirements to a design method of games in RPG genre
1. Simplicity of design — for implementation of a goal of operation is not required the artificial intelligence highly imitating behavior of the person. Respectively, according to the principle of design of KISS ("do not complicate"), it is necessary to select the simplest of available decisions.
2. High speed of development — in connection with existence of periods of delivery.
3. The possibility of change of statuses of characters — this point is key as he directly is responsible for the interest of the player in the events on the screen. GAI shall assess accurately a game situation and transfer characters to a necessary status.
4. Absence of need to develop additional elements of architecture. Such elements complicate and decelerate development and also considerably decelerate testing of programs.
To the listed criteria most there corresponds GAI designed on architecture of finite state machines. The specified architecture gives an opportunity to create the systems of the average level of complexity, without additional expenses of the temporal or other resources connected to project development. The method of finite state machines gives sufficient opportunities for development of a game in a genre of "RPG" the small number of developers.
5. Conclusion
The system of a game artificial intelligence on the basis of finite state machines is developed and realized in the form of a programs set.
This system is capable to execute change of statuses, a purpose choice on the basis of priorities, search and creation of a way, relocation in the constructed way, a choice and the attack of the character of the opponent.
The system can be used during creation of the game project. If necessary, the system can be modified or added for the solution of specific objectives
6. Recommendations
The provided research will be useful for developers of systems of a game artificial intelligence. Also this development can be interesting to a wide range of the experts who are interested in problems in the field of intellectual systems.
REFERENCES
Artificial intelligence (2016). In Wikipedia, the free encyclopedia. https://ru.wikipedia.org/wiki7McKyccrBeHHbiti_MHTe.n.neKT
Bachurina L.R. & Drozdov E.N. Features of development 3D - the character for a computer game (2016). Messenger of young scientists of SPGUTD, 3 (14-18), 2312-2048.
Decision Tree vs Behavior Tree (2013). Game Development. http://gamedev.stackexchange.com/questions/51693/decision-tree-vs-behavior-tree
Drozdova, E. N. & Shavkunov, V. S. (2016). Development of the voice intellectual assistant for control of OS Windows and interaction with Internet services. Messenger of young scientists of SPGUTD, 2 (40-43), 2312-2048.
Drozdova E.N. & Kovalenko A.N. & Shavkunov V.S. (2017). Development of the intellectual voice assistant with extensibility support. Modern European Researches, 2 (32-43), 2311-8806.
Drozdova E. N. & Shavkunov V. S. (2017). Development of a mobile software agent with voice actuation for PC compatible computers. The Scientific and methodical online magazine "Kontsept", 31 (821 -825), 2304-120X
Drozdova E.N. & Kovalenko A.N. & Shavkunov V.S. (2017). Program implementation of the Russian-speaking personalized intellectual assistant with extensibility support. The SPGUPTD Bulletin. Series 1 Natural and technical science, 2 (8-13), 2079-8199.
Kehoe D. Designing Artificial Intelligence for Games. Part 1 (2009). Intel® Developer Zone. http://software.intel.com/en-us/articles/designing-artificial-intelligence-for-games-part-1.
Lim, Chong-U. An Evolutionary Approach Using Behavior Trees (2009). Imperial College London. http://www.doc.ic.ac.uk/teaching/distinguished-projects/2009/c.lim.pdf.
Ryabova M.V. & Drozdova E.N. Features of development mobile quest games with puzzle elements (2016). Messenger of young scientists of SPGUTD, 3 (93-97), 2312-2048.