An example of creating a complete game AI: Part 1 (Author: Geoff Howland)

Source: Internet
Author: User
Tags fsm

Artificial intelligence (AI) has always been a background development technology, but the proportion of AI development in future games will continue to increase. If the AI of your game cannot keep up with the standard, the players' comments on your game will be outdated and you will feel uncomfortable.
Game AI is not just a neural network and learning system, but also a complex mathematical algorithm. Although this can be used, the AI of the game is mainly to establish an environment and external performance through the objects in the game. Gaming AI must be behavioral rather than scientific.
The key to building a game AI is to identify the final result of the game. This will all be visible to gamers. If this cannot be expressed, we 'd better stop talking about it.
One of the following examples is about instant strategy games. Many of these concepts can be applied to other types of games. All the code is in C language:

State Machine and finite state machine:
A Finite State Machine (FSM) is a system that contains a certain number of operation states. In a real world, a switch may be in either the ON or OFF state, or an alarm will then sound. There are three statuses: Playing, recording, and timing. Any system that contains limited capabilities that can be defined as a state can be defined as a finite state machine.
Finite state machines are used in many programs and it is as difficult as creating a game world to effectively build game AI. Therefore, it is necessary to give them as detailed and concise as possible.
How to Use finite state machine
Using FSM in a game has many purposes, but one of the hard tasks is to standardize a behavior model to simulate human behavior, which is quite difficult. It is often wrong to simulate human behavior. Many games have discovered and pointed out this situation by game players. It is still in a very detailed FSM system.
When you design systems that are very accurate about human learning and thinking, you can use a simple way to determine how you choose to think as a person. After learning more about AI decisions and learning systems, we still need to use this method, which is often very simple to design rather than that scientific accuracy.
Instead of misunderstanding and conflict with Neural Network Systems, genetic algorithms, and other artificial intelligence algorithms, you just need to choose some wise solutions and interesting algorithms as a better solution, and achieve the best results. Use the final goal you want to achieve to measure and make choices, and do not think it is unique.

Game state machine:
Creating a satisfactory game environment means that you should consider the details that many players may notice as much as possible. In more cases, you need to plan and test it in advance, so that players will be immersed when moving continuously in the game environment.
In all your game states, there must be at least two state machines to make the game smooth. The first state machine processes the game interface, including whether or not the game is paused. If there are many modes for players to visit the world, you can list them. Some players can or cannot see them, some other labels can be made into special interfaces.
Struct gamelevelstate {
Int alert; // alert status of enemies //
Struct positionshot; // position of last shot fired //
Int shottime; // game cycle last shot was fired //
Int hostage; // hostage rescued //
Int explosives; // explosives set or not //
Int tank; // tank destroyed //
Int dialogue; // dialogue variable //
Int complete; // mission completed //
};

Flexibility:
Maintaining the flexibility of game AI is very important. If you set a standard rule, it is easier to further expand it. Understanding how to design a game AI is very similar to designing an internal thread, and you need to try to build them on it.
The goal of building a good AI is to see that all game units interact with each other in the environment. Without this, it is very difficult to adapt to the Game objects and the game interface will be very bad. If the player does not control the feeling of the game unit and obtains the returned information of the game unit, the player will only point to a point that has no purpose on the game interface, all immersion in the game will be lost. This means players will be bored and have no fun.
The following is a structure of game information that you may provide to players:
Struct character {
Struct positionpos; // map position //
Int screenx, screeny; // screen position //
Int animdir, animaction, animnum; // animation information, action and animation frame number //
Int rank; // rank //
Int health; // health //
Int num; // number of unit //
Int group; // group number //
Int style; // style of unit (ELF, human )//
Struct animationobject animobj; // animation object for complex animations //
};

Some variables are defined as follows:
The POS variable defines the location coordinates of the game unit. screenx and screeny are used to display related information of the game unit on the screen, such as the health value and whether the game unit is selected.
Animdir, animaction, and animnum are used to define the animation status of the game unit, which will be displayed on the screen.
Rank and health variables are the attributes of the game unit.
The num variable is the serial number of the game unit in the game unit array. When the game unit information is called, the game unit is used to select the corresponding game unit, and the structure address does not need to be provided directly.
The group variable determines which group the game unit belongs to. As a game unit, it will belong to a group. Only when the unit dies will it not belong to a group.
Style and animobj are more game unit display information.
Create a more advanced part:
When your game unit follows a certain route, it is time to build more information so that they seem to live a real life.
You need to think about what makes the game unit do something repeatedly. Do you want them to have emotions? Do you want them to fear, run, and shop like a woman?
You need to add variables to determine the emotional state. The best way to understand the emotional components of a game unit is to first make some ideas on yourself. To establish a human reflection mechanism, you need to understand how a real person reflects the situation.
On the other hand, it is almost relative to human emotions. Here a suitable experience is provided not based on reality, but can be used to challenge players. In addition to making the game unit human, you also need to think about whether there is reason for your game unit to make these reflections. The problem is that you have to make a conclusion, whether to add this, or simply end it, using a monotonous reflection mechanism, although it is easy to do, or simply using a random solution. All of these may have ruined a good game, so we need to do more functions, but it is very important to let players play for a lifetime and do not know how to make it!

Create group:
Set up a group or not?
If you make a first-person shooting game, you certainly won't be able to use it. However, in real-time strategic games or games where a player controls more than one unit, you need to know the following:
Do you need your organization to act in a harmonious way?
If your answer is yes, there is a good idea here, that is, building a group. If you don't answer the question, the advantage is that you don't have to pass the information to every game unit in the group, especially when a target point changes.
Coordinated action by multiple game units, such as patrol buildings, can be controlled near a central target point. Instead, you don't have to let each game unit act alone, and check whether it hits another game unit and then return to a new location.
Groups can maintain their common structure, so releasing a command takes the same time as releasing the command to a game unit. More importantly, you will establish some easy-to-understand and read methods. It's easy to manipulate 25 game units and make them communicate with each other.
Group-based actions are easier for Obstacle Avoidance and detection, and the time to find paths is also reduced, which is effective in the presence of a large number of game units.
Big scene:
After organizing your group, you just want to build game AI, just as you want to mobilize all game units for the final attack. The idea here is to build a region as a monitoring center so that game units can be quickly discovered. The idea is that you don't need to copy any data. You only need to search for data in a resource. This region will be based on the most reasonable location, and other logic expansion functions should also be in a reasonable location.
Based on my experience, I decided to differentiate in this way that any object that must be associated with the game unit will be put into the game object data structure as an additional object.
This means that game objects do not have any information, such as animation sequences and world coordinates, when they perform certain actions, or when they are physically coordinates. This means that a game organization must belong to a group to be able to move or change its behavior. If they are independent, they are treated as a group as a whole.
One of a large number of units:
We finally found a group for unified operations. A coordinate system is to build many small pieces, which is especially important when game units seek routes. So when we need the group to end the formation, and act independently. The following structure needs to be established:
Struct groupunit {
Int unitnum; // character number //
Struct Unit * Unit; // unit character data //
Struct positionwaypoint [50]; // path in waypoints for units //
Int action [50]; // actions by waypoints //
Int stepx, stepy; // step for individual units, when in cover mode //
Int run, walk, sneak, fire, hurt, sprint, crawl; // actions //
Int target; // targets for units //
Struct position targetpos; // target position //
};

Variable description:
Unitnum is the number of game units in the group. If the group contains up to 10 units, there will be 10 positions.
Unit is a pointer that indicates the unit structure. It stores the current coordinates, health values, and other information of the game unit. The life mark of a game unit indicates whether the unit is injured or is communicating with another game unit, or some other information.
The waypoint array is the path sequence of game unit movement. All actions and waypoints are uniquely specified in the groupunit structure, depending on whether the group is in formation.
The action array contains the moving behavior. Allow the creation of more command sequences, control the game unit's hidden action or rapid movement, and you can imagine other strategic actions.
Stepx and stepy are a speed information. Each shard moves all game objects. Proper use can be suitable for the occurrence of all thing rational events. In a common system, the processing time is usually reduced.
Run, walk, and sneak are different game unit statuses. It is not an animation, but the action status can be marked, and there are many States that can affect each other.
Target and targetpos are used to specify the attack target and its own coordinates. The enemy coordinates are the same as the healthy values and other attributes. You can use the enemy's game unit number to search for them, but for convenience, I decided to keep a copy of the local enemy coordinates.
Group Intelligence:
Our ultimate goal is to have a central position to limit the number of units and threads as much as possible for as much data as possible and to make the problem simple. Let's look at the following data structure:
Struct Group {
Int numunits; // units in group //
Struct groupunit Unit [4]; // unit info //
Int formation; // formation information for units and group //
Struct position destpos; // destination (for DeST. Circle )//
Int destpx, destpy; // destination screen coords //
Struct position wayx [50]; // path in waypoints for group //
Float formstepx, formstepy; // formation step values for group movements //
Int formed; // if true, then find cover and act as individuals, otherwise move in formation //
Int action, plan; // group action and plans //
Int run, walk, sneak, sprint, crawl, sniper; // actions //
Struct position spotpos; // sniper coords //
Int strategymode; // Group Strategy Mode //
Int orders [5]; // orders for group //
Int goals [5]; // goals for group //
Int leader; // leader of the group //
Struct sentryinfo sentry; // sentry list //
Struct aistateaistate; // AI state //
};

The number of game units in the numunits submission group, and the groupunit information is stored in the game unit array. This special group only allows a maximum of four game units
Formation is a formation flag that determines the type of the formation. It can be formed into columns, wedge-shaped, and diamond. You only need to define the corresponding number.
Destpos, destpx, and destpy provide the information to reach the destination to quickly reflect the player's operations. Waypoints and steps are independent unit control methods, and all game units in the formation will have the same speed to maintain the correct type. You do not need to change the speed for each unit.
Formed determines whether the game unit has a formation or independent action. If there is a pair, all game objects will perform the same operation. If there is a reason why the game cannot be moved in one way, such as being attacked or the necessary information is interrupted, the game unit will move itself in the face of obstacles.
Actions is an independent action designation. For example, if a sniper is in a group, you cannot allow him to act in a unified manner. This is the logic group that separates game units and controls sniper actions at the group level. This design is made under the best design structure.
Strategymode is a flexible variable that determines how to respond to enemies. Is it brave, strategized, defensive, and surrender? Using a simple form of access to control basic responses is a good way to subtract the state calculations of many independent game units and groups. Better player control is provided. Players can set different groups for different actions.
The orders and goals arrays indicate the command and purpose. The behavior defined in the command and target database requires multiple groups to easily reflect the behavior.
Sentry and aistate include the AI Status values of whether the website is on duty and the response is received.
Combined:
Now we have some group structures. The following describes how to use them:

Be sure to carefully write AI programs, and be standardized and flexible so that you can add them at will to facilitate calling each part. The idea is to have a data organizational structure, and make some special functions in a function. And then call it elsewhere. Finally, if your AI has problems, you need to debug it. You do not need to check each statement, because there is a special function that requires at least this habit.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.