CSE 380 – Computer Game Programming Artificial Intelligence Murloc, Blizzard Entertainment
Artificial Intelligence Can a computer “think”? Can it process info in a fashion similar to humans? How do we make creatures “appear” to think? –We only want creatures intelligent enough The ultimate AI game – massively multiplayer The ultimate stupid programmer: –Press Your Luck – beaten by contestant Michael Larson
Remember Our Infrastructure AnimatedSprite CollidableObject AnimatedSpriteTypeSpriteManager GameStateManager PhysicalProperties BoundingVolume * * * 1 1 Bot 1 1
Bot.h class Bot : public AnimatedSprite { public: Bot(){} ~Bot(){} // TO BE DEFINED BY BOT AI CLASSES virtual void think(Game *game)=0; virtual Bot* clone()=0; };
BotRecycler? class BotRecycler {private: map *> recyclableBots; map registeredBotTypes; public: BotRecycler(); ~BotRecycler(); void addMoreBots(list *botsNeededList, wstring botType, unsigned int numToAdd); void initRecyclableBots(wstring botType, unsigned int maxBotsNeeded); bool isRegisteredBotType(wstring botType); void recycleBot(wstring botType, Bot* botToRecycle); void registerBotType(wstring botType, Bot *sampleBot); Bot* retrieveBot(wstring botType); };
Deterministic AI Algorithms Behaviors that are predetermined or preprogrammed –Objects may follow a straight course Asteroids –Random Motion Good for modeling things like insects –Periodically (not every loop iteration), pick random direction & velocity for object(s) –Weight probability in a particular direction for the wind –Tracking Algorithms Takes into consideration the position of object being tracked –Change path directly toward object, OR –More realistically, gradually change path toward object
Direct Tracking Algorithm Direct Tracking – like the terminator Example: if (player.x > monster.x) monster.vX = MAX_V; if (player.x < monster.x) monster.vX = -MAX_V; if (player.y > monster.y) monster.vY = MAX_V; if (player.y < monster.y) monster.vY = -MAX_V; Not very realistic because tracking is so precise
Trajectory Vector Tracking Gradually change the path toward the target 1.Compute the vector from the tracker to the target TV = (target.x-tracker.x, target.y-tracker.y) = (tvx,tvy) Normalize: (tvx,tvy)/Vector_Length(tvx,tvy) Now max length is 1.0 (remember physics?) 2.Adjust the current velocity vector of the tracker by adding TV* scaled by a rate of your choice tracker.vx += rate*tvx; tracker.vy += rate*tvy; 3.If velocity has overflowed a maximum rate, slow it down
Patterns & Basic Control Scripting Creatures perform a series of actions in a particular order Motion Control Patterns –Used in Space Invaders, Galaxian, Phoenix, etc … –Create a pattern instruction set, like: #define GO_FORWARD 1 #define GO_BACKWARD 2 #define TURN_RIGHT_90 3 #define TURN_LEFT_90 4 #define SELECT_RANDOM_DIRECTION 5 #define STOP 6 –Use another operand to keep track of how long to do each action –Use an array to store the order for a pattern
Pattern Example Globals: int num_instructions = 6; int square_stop_spin[1, 30, 3, 1, 1, 30, 3, 1, 1, 30, 3, 1, 1, 30, 6, 60, 4, 8]; During Game Play: int instruction_ptr = 0; int cycles = spare_stop_spin[instruction_ptr+1]; switch(square_stop_spin[instruction_ptr]) { case GO_FORWARD: //move creature forward … } instruction_ptr += 2 if (instruction_ptr > num_instructions*2) // sequence over, perhaps do again
Improving on Patterns Make sure you provide feedback control to your pattern engine so your monster doesn’t do anything illegal –No collisions, going off screen, etc … Patterns with Conditional Logic Processing –You can choose from a number of patterns randomly –To make the AI smarter, choose a pattern based on the current game conditions Test for distance to player, when the player gets closer, change the monster pattern
Behavioral State Systems To create a Finite State Machine (FSM), you need: –A reasonable number of states, each which represents a different goal or motive Example Monster states: –Move forward, move backward, turn, stop, fire weapon, chase player, evade player –A single state may itself have multiple sub-states –Lots of input to the FSM, such as the state of the environment & the other objects within the environment Continually update the monster so they are behaving on the latest state –You can model aggression, curiosity, stupidity, etc …
Elementary State Machines Create a master brain that deals with high level states, which then get translated down to lower states Ex: High-level creature behavior states may be: –Attack, retreat, move randomly, stop, look for something, select a pattern & follow it For last state, a pattern processor selects a pattern of behavior for the creature based on environment
ESM Example switch(creature_state) { case STATE_ATTACK: // attack logic case STATE_RETREAT: // retreat logic … case STATE_PATTERN: Process_Pattern(); } creature_state = rand()%6; We could give more personality to monster by creating a distribution of behavior. Ex: StateGoomba p(x)Koopa p(x) ATTACK50%15% RETREAT20%40% STOP5%45% RANDOM25%5%
Modeling Memory & Learning So far our creatures think on-the-fly Creatures can learn from past experiences (if you want them to) –Don’t fall for the same player moves –Creatures searching for something may remember where they’ve been –Many game programmers like to use bit strings or vectors to memorize data
Planning & Decision Trees Planning is high-level AI –Using a high-level set of actions that are carried out to arrive at a goal –There may be conditionals that must be satisfied before any particular action can be carried out Production rules can be used in decision tree: if x1 op y1 false true if x2 op y2 false true if x3 op y3 false true if x4 op y4 false true if x5 op y5 false true
Decision Tree Example Conditions If the player is close and damage is low –Then fire at player If the player is far and fuel is high –Then search for player If damage is high and player is close –Then evade player If damage is high and ammo is 0 and player is close –Then self destruct You may want to define structures that specifically represent decisions to be made
Pathfinding In our earlier example, there were no obstacles for vectoring If there are obstacles, things get complicated Resolutions –Trial and error – if a collision occurs, back-up the monster, turn it 45 degrees and try again –Contour Tracing – trace the contour of the object that’s blocking the path –Collision Avoidance Tracks – create virtual tracks around objects that trace out a fairly intelligent path The path can be computed algorithmically using a shortest-path algorithm –Waypoint Pathfinding – for complicated world with many obstacles, setup a network of paths Creatures only need to find their way to nodes, which are then connected via paths All sorts of CSE 373 & AMS 301 algorithms could be used for finding shortest paths
Advanced AI Techniques Advanced AI Scripting –Examples: QUAKE C, UNREAL Script Allow you to program game code with a high-level English-like language that is processed by the engine –You may define your own scripting language A model of the human brain –Brain has billion brain cells, on which end are you? Genetic Algorithms –A method of computing that relies on biological models to evolve solutions Fuzzy Logic –Making deductions about fuzzy set theory –Analyzing sets of data such that the elements of the sets have partial inclusion (objects are partially in a set)
Basic Guidelines for AI Objects with simple behaviors (rocks, missiles, etc …) –Use Deterministic AI Objects that are supposed to be smart, but are part of the environment rather than the main action (birds that fly around, a space ship passing by, etc …) –Use deterministic AI coupled with patterns & randomness For important game characters that interact with the player –Use FSM coupled with other supporting techniques like personality distributions Main characters should be very smart –Should be state-driven –Use conditional logic, probability, & memory for controlling state transitions