AI
From NetGore Wiki
| Difficulty | |
|---|---|
| Topics | Entities |
| (view all tutorials) | |
AI stands for artificial intelligence. This article explains how to add new AI objects and how to apply it to a character. It also explains some design considerations that should be adhered to for best performance as well as some methods for testing the performance.
Contents |
How the AI works in NetGore
When NetGore's server is initializing you may have noticed AI being 'Loaded'. At runtime NetGore's server loads all of the AI classes that are available when it starts up. Each of these classes are assigned AIID's by the developer. These classes that you see are then dynamically attached to the NPC at runtime. It's these AI classes that give the NPC their apparent intelligence in terms of how they move and interact with other players and NPC's. The AI does not control the chat, see the page on chat for more information.
AIID's
Each class that is defined as being used for handling AI must be declared with an AI Attribute. This attribute contains the specific ID that this AI class will be referenced in the database and in the program itself. The ID itself is specified by a constant integer that must be unique from any other AI class.
Your new classes attribute needs to be specified by using the following method:
using NetGore.AI; [AI(_id)] public class yourNewCustomAI { const int _id = 2; // Where '2' is your unique AI ID number. ... }
Database
AI rules are set to an NPC using the database. If you look in the character_template table you will notice a list of NPC's each with an ai_id either set to '(NULL)' or a number representative of the AI ID in the code.
This is where the server gets the information about which ai should be attached to each NPC. See the wiki page about the Editors for more information about editing NPC's and game content without having full access to the database.
Adding new AI rules
Before adding new AI rules, it is a good idea to write down exactly what you want your AI character to do. Like a kind of shortened design specification but specific to the AI. This helps to make sure that you achieve what you want, and helps in the algorithm design.
All AI classes should be put in the DemoGame.Server\World\Map\Entities\Characters\AI\ directory. This makes it easy to keep track of more than anything, although they don't have to be limited to this location, it is a lot easier to keep track of your AI classes, for example, you could create a directory structure within here to organise your AI rules.
All AI classes should inherit the DemoGame.Server.AIBase class. This class contains all the useful functions that you might need to interact with the world and to get certain information from the world. You might want to add functions here for use in your own AI rules, however NetGore is built with a basic set of functions that can get you started. It is advised that you look at the documentation for the DemoGame.Server.AIBase class.
The most important property that the DemoGame.Server.AIBase class provides is the Actor property.
The actor refers to the character that your AI controls. Through this property you can control every aspect of the character. From it's movement to whether the character is attacking or you could even set the AI to kill itself.
Adding AI to a Character
After creating your AI class. You need to add the ID of the AI to the character template in the database. This is the default method for adding AI rules to a character and should be used for characters.
You might want to create an entity however that is not a character but still displays AI rules. The best method of doing this is directly coding the rules into the entity itself. For example: you might want a security camera entity that sweeps the area, but as soon as it spots a player, follows the movement of the player and raises an alarm. See the Adding an entity example.
Considerations
The AI in NetGore is handled server side, as in most server/client games. This means that when creating your AI ensuring that computation is as little as possible is important. Some understanding of algorithm design and big O is useful but not entirely essential.
Take care not to use large loops. This can cause the server to hang and can cause your NPC's to appear to lag. If you can, use linq instead of loops.
Try not to do too much additional processing in the AI class and leave it purely for movement and attacking. Adding simple predicate conditions won't cause a noticeable performance difference and are effective in creating simple rules that give the illusion of artificial intelligence.
Testing Performance
|
This is a work in progress! |