From AI Product Manuals
Revision as of 16:55, 4 October 2021 by Davemark (talk | contribs) (→‎Export)
Jump to navigationJump to search
This page is part of the IAUS Manual.BrainBehaviorBehavior TypeDecisionAI Entity


IAUS Structure

A Brain is the top level component that is assigned to an agent in the game. From a data standpoint, a Brain is simply a collection of Behavior Packs and Ability Packs. However, the Brain class contains most of the data for the agent's thinking and does most of the work of scoring Behaviors and keeping track of the history of the Behavior usage.

It is the Brain class that is ticked on a regular basis by the game code. However, the Think Cycle is controlled by the brain itself. That means that while the Brain might be ticked every frame of game loop, the Brain may only think perhaps every 250ms.

During this process, the Brain assembles a list of potential neighbors and combines them as necessary with Behaviors to arrive at a combined list of Decisions to be scored and selected from. It then proceeds to score all those Decisions to arrive at the one with the greatest utility at the time.

If a new Decision is selected, the related Behavior Type is executed. Additionally, data is adjusted in the Brain to make note of what the Brain is currently doing, the history of behaviors, etc.



The code for the Brain is held in Brain.cpp in the class of the same name.

There will be one instance of the Brain object for each character that has a brain. This is because it holds the information about the current state and history of this character's "thought process".


For readability, there are many typedefs at the top of the Brain file. Most of these are lists, sparse data structures, or defined pairs. In the case of the pairs, they are most often used for passing around the key/value combinations of the sparse structures but are also used as the key itself.

// Convenience types
typedef std::unordered_map<unsigned int, BrainSeed*> BrainMap;
typedef std::pair<unsigned int, BrainSeed*> BrainPair;

typedef std::unordered_map<unsigned int, Behavior*> BehaviorMap;
typedef std::unordered_map<unsigned int, BehaviorAbilityPair*> AbilityMap;
typedef std::pair<unsigned int, Behavior*> BehaviorPair;

typedef std::list<Behavior*> BehaviorList;
typedef std::list<Decision> DecisionList;
typedef std::list<BehaviorAbilityPair*> AbilityList;

typedef std::list<BehaviorPackage*> BehaviorPackageList;
typedef std::unordered_map<unsigned int, BehaviorPackage*> BehaviorPackageMap;
typedef std::pair<unsigned int, BehaviorPackage*> BehaviorPackagePair;

typedef std::list<AbilityPackage*> AbilityPackageList;
typedef std::unordered_map<unsigned int, AbilityPackage*> AbilityPackageMap;

typedef std::unordered_map<unsigned int, AbilityPackage*> AbilityToAPMap;
typedef std::pair<unsigned int, AbilityPackage*> AbilityToAPPair;
typedef std::unordered_map<unsigned int, BehaviorPackage*> AbilityToBPMap;
typedef std::pair<unsigned int, BehaviorPackage*> AbilityToBPPair;

typedef std::pair<InputParameter, float> ParameterPair;

typedef std::unordered_map<unsigned int, float> BehaviorHistoryMap;
typedef std::pair<unsigned int, float> BehaviorHistoryIdAndTime;

typedef std::unordered_map<ContextEntry, float> ContextHistoryMap;
typedef std::pair<ContextEntry, float> ContextHistoryIdAndTime;

typedef std::unordered_map<BehaviorType, float> BehaviorTypeHistoryMap;
typedef std::pair<BehaviorType, float> BehaviorTypeIdAndTime;

// Entity for keeping track of relevant neighbors while making decisions 
// so that they don't need to be reacquired by the engine

typedef AiEntityWP NeighborEntity;
typedef std::list<NeighborEntity> NeighborList;

Static Members

However, there are a number of static members in Brain.cpp that hold all the original versions of Brains, Behaviors, Abilities, Behavior Packs and Ability Packs, and Animation Mappings. These are all held in sparse containers (unordered maps in C++ and dictionaries in C#). Additionally, for readability, these are all typedef'd at the top of the Brain file.

	// Brain Data Initialization
	// This is not part of individual brains -- only one copy of each is necessary

	static BrainMap Brains;								// All brain definitions
	static BehaviorMap Behaviors;						// All behavior definitions
	static AbilityMap Abilities;						// All ability definitions
	static BehaviorPackageMap BehaviorPackages;			// All behavior package definitions
	static AbilityPackageMap AbilityPackages;			// All ability package definitions
	static ObjectAnimationMappings ObjectAnimations;	// All animation names for objects

In the case of the package lists, the ones in the static objects may not match up with the one that is contained in an active Brain itself. This is because packages can be pushed on and off the brain dynamically through events in gameplay. Therefore, the static versions are indicative of what the brain is "born with".

Brain Seed

Additionally, this file holds the class BrainSeed which is a way of storing the information for raw brain data that comes from the database. There will be one BrainSeed object for each brain. It merely holds the name, description, a list of the Behavior Packs and a list of the Ability Packs that are associated with this brain. On load, it is unpacked into the static members of the Brain class.


From a data standpoint, other than a unique ID, name, and a description, a Brain is simply a collection of Behavior Packs and Ability Packs.


Exporting a Brain is merely exporting the Brain's ID and the Behavior Packs and Ability Packs it contains. These end up creating Brain Seeds.