Darthfett
Aerospace/Cybersecurity Software Engineer
- Reaction score
- 615
Why make a tutorial on making an AI?
Well, so many maps have AI's, yet not one of them uses the AI editor in game. Games like Castle Fight, Multi-Square Defense, and others have their own AI systems. Many people play games with AI's in them, and when they go to make their own map, they assume the game AI can already do everything. So, many of those people will come onto a forum like this one, asking how to get their "AI" working, when their hero isn't automatically collecting items and using his abilities. This tutorial teaches you about AI's, so that when you want to make your own, you'll know just where to begin!
Note: This tutorial is a Work-In-Progress.
An AI is not an actual person and thus, must be thought of as a collection of current conditions, objectives, and rules. For example, if a Melee AI has two thousand gold, its current condition might be 'rich'. Objectives of an AI can be scaled down to smaller and smaller decisions. For example, a large scale objective is to wipe out all other players. This can be scaled down to 'Assemble an army to attack the enemy', and further down to 'collect resources, research requirements, and build barracks in order to train an army'.
Just as your map started out somewhat basic, and got more complex as you learned what was good and bad, so your AI will have to start out basic. However, no one wants to have to recode things constantly. It's best to start out with a strong foundation.
A few things to know about writing an AI, is that you will be working with events and condition functions (in your code) much more than you probably did before. You will want to keep your code readable and easy to change, so you should use events to change variables and information about the game. Then you can use them to run evaluations which update units orders.
Well, so many maps have AI's, yet not one of them uses the AI editor in game. Games like Castle Fight, Multi-Square Defense, and others have their own AI systems. Many people play games with AI's in them, and when they go to make their own map, they assume the game AI can already do everything. So, many of those people will come onto a forum like this one, asking how to get their "AI" working, when their hero isn't automatically collecting items and using his abilities. This tutorial teaches you about AI's, so that when you want to make your own, you'll know just where to begin!
Note: This tutorial is a Work-In-Progress.
Non-Melee AI Tutorial
Ever wanted to create an AI, or bots for your game, but the AI editor was insufficient? This tutorial will teach you how AIs work, and how to program one.
The Basics
An AI is basically something that controls units. Whether it controls buildings, regular units, or heroes, it can all be programmed into one AI. An AI is not magical, and there is a very limited amount of what a computer can do without the aid of triggers, so understand that your AI system will take a lot of work. You will use variables and if-then-else conditions constantly in order for your AI to be able to make choices based on its environment.
How does an AI work?
An AI is not an actual person and thus, must be thought of as a collection of current conditions, objectives, and rules. For example, if a Melee AI has two thousand gold, its current condition might be 'rich'. Objectives of an AI can be scaled down to smaller and smaller decisions. For example, a large scale objective is to wipe out all other players. This can be scaled down to 'Assemble an army to attack the enemy', and further down to 'collect resources, research requirements, and build barracks in order to train an army'.
- These same concepts can be applied to any map's AI. For example, if you were making a bot hero for DotA, you might want to take in account a hero's Health, gold, available abilities, and current items as conditions. If you leave out one, you are crippling your bot. For example, a hero's health is necessary to know in order for the bot to know when to retreat to heal. Gold is necessary for the hero to know when he can go back to purchase items. Available abilities are necessary to know if its possible for a hero to 'sprint away' or severely cripple an enemy hero. Items are also necessary, so a bot knows if it has some of the necessary components for a recipe, or if it has any special abilities.
- The objectives of a DotA bot are relatively simple at first: Destroy the enemy base. However this can be broken up into many smaller objectives, such as destroy the tower, push one lane, retreat to fight later, or even as having a doomed hero using all of its abilities, since it will revive with full mana and cooldowns.
- The AI also has rules to follow. Going solo can be less beneficial to the team, compared with working with another hero to push one lane. Buy items cautiously, as you can only sell items for half of what they cost. These are basic rules that an AI should be 'aware' of. Rules are basically just evaluations that tell the AI what the best course of action is. Obviously, a bot shouldn't just pick any random item. He should choose the one that makes him strongest, or has the most potential. Another way to look at rules, is to make the AI not do what a person wouldn't. Obviously, it's not going to be sensible for a player to switch lanes in DotA, if he has to run all the way back to his base first. It would just be a waste of time. Make your AI able to understand the obstacles and mechanics of the game that players naturally understand. Your bot will have to understand the pathing of your map if it is going to be moving around within it.
Foundation Tips
Just as your map started out somewhat basic, and got more complex as you learned what was good and bad, so your AI will have to start out basic. However, no one wants to have to recode things constantly. It's best to start out with a strong foundation.
- Another tip for creating a good foundation, is to start small. No one writes a complicated AI script in a day. Start with the very basics, such as creating waypoints for units, and have units auto-attack. Scripting the actual combat decisions of a bot can be confusing, tiresome, and can easily result in a buggy AI.
- One good way of coding a good foundation is to write everything that could change in its own function. Try to reuse code as much as possible, so that if you happen to need to change something simple, such as knowing whether a unit is "threatened", you only have to change it in one place. Later on, you may discover that the condition is more complicated than simply checking if there are nearby enemies.
A few things to know about writing an AI, is that you will be working with events and condition functions (in your code) much more than you probably did before. You will want to keep your code readable and easy to change, so you should use events to change variables and information about the game. Then you can use them to run evaluations which update units orders.