Basics of AI - Promethaes/Game-Programming-Compendium GitHub Wiki
The Basics of AI
Notes from this GDC talk As with all things in software development, we want to try our best to think abstractly about the problems we face at each different stage of development. To summarize briefly, AI like any other system, should be built off of single-purpose loosely-coupled modules intended for future reuse and refactoring.
There are four main sections to AI programming
- Blackboard
- Sensors
- Deciders
- Actors
Blackboard
This is essentially the component that holds all the state info. Every time a sensor reports an object being sensed, it is written to the blackboard for later use.
Sensors
Your eyes and ears, basically letting the blackboard know when the entity has seen the player for example.
Deciders
These are the components that will make decisions based on the events fired from the blackboard. Eg. enemy is alerted to the player, decider will choose a certain set of actions, otherwise if they don't see the player a different set of actions is chosen.
Actors
These are the components that will actually do something tangible or visible to the player. For example, moving would be considered an action, or shooting, or running away, etc.
Here are some data structures:
As a quick note
Since this is an abstract system, a lot of these types have default starter function prototypes such as:
- Init
- Select
- Deselect
Select gets fired when the action / decision / etc. gets selected, deselect gets fired when it is deselected. Events!
Reasoner
The thing that makes decisions.
The reasoner is essentially your finite state machine. It's the thing that's going to be telling the agent what to do.
Sequence Based
A reasoner that doesn't have to make decisions based on input. Example given was an agent that just aims and then fires repeatedly.
Utility Based
"The general concept of a utility-based system is that every possible action is scored at once and one of the top scoring actions is chosen. By itself, this is a very simple and straightforward approach." from this paper
Rule Based
Your typical if this then do that style of architecture.
Considerations
Evaluate a single aspect of the current situation.
Evaluate the situation and determine how good an option is using "weights", essentially working with the Utility Based Reasoner. Examples Include:
- Distance
- Execution History (How long have i been doing something?)
The Picker
Picks a decision based on several considerations, essentially a reasoner in combination with a picker. Also typically used to pick entities.
Actions
What to do when a particular option is selected.
Includes:
- Move
- Fire Weapon
- Activate a subreasoner
Targets
Represents a position and optionally an entity
Includes:
- fixed position
- named entity
- controlled entity
Weight Functions
Convert an input to weight values
Essentially, convert considerations into a floating point number (or some other type) that is then used to determine the "weight" of the particular option.
Region
A standard way to represent a region of space with an inside and outside.
- Circle
- Rectangle
- Polygon The region should know if you're within the region or not.
An example from military simulation: https://imgur.com/n2nu73t
Notes from this image:
- Veto, prevent all other considerations from operating
Implementation Details
- Polymorphism is king here, make things as abstracted as possible. Focus on decoupling and single responsibility.
- Factories handle reading XML (or some serialized data) and creates the object of the appropriate type. Can have constructors, which are just objects that know how to properly create eg. a consideration.
Code examples can be found at https://github.com/virtual-world-framework/mars-game
My Implementation
So, this part will probably need to be updated a lot considering that at the time of writing this, I'm about to start working with my AI system on a fully fledged project.
In summary, I created an event handler and custom events (all of which have unity events to directly plugin to) that uses polymorphism for the highest level of modularity I could muster.
- AI_Blackboard is the brain, it searches through all of the possible decisions it can make and tries to make those decisions (there is no weight factor for decisions yet).
- AI_Decision contains the criteria required to make an AI_Action, the criteria being AI_Sensors being triggered. It has a list of actions to perform of those conditions are met.
- AI_Sensor is your trigger volume or distance check, etc. Whatever you want to trigger the decision to happen
- AI_Action is the actual action that the AI will take. This can be walking, attacking, fleeing, etc.
I think I would change the decisions so that they actually have weights and do more research into behavior trees. Perhaps a behavior tree editor is next on the list, but this system is more than enough to achieve decent complexity levels for AI while not having to write that much code, so I'm proud!