Games Programming
Games Programming
PROCESS
Game Programming
OUTLINE
o Select Languages
o Debugging
o Waterfall
o Iterative
o Agile
o Reactive, no proactive
o End with bugs. If bugs faster than can fix, “death spiral” and may be cancelled
o Plan ahead
o Proceed through various planning steps before
implementation
o requirements analysis, design, implementation, testing (validation),
integration, and maintenance
o The waterfall loops back as fixes required
o Can be brittle to changing functionality,
unexpected problems in implementation
o Going back to beginning
METHODOLOGIES - ITERATIVE
+ High-level
o Classes (objects), polymorphism, templates, exceptions
o Especially important as code-bases enlarge
o Strongly-typed (helps reduce errors)
o ex: declare before use, and const
+ Libraries
o C++ middleware readily available
o OpenGL, DirectX, Standard Template Library (containers,
like “vectors”, and algorithms, like “sort”)
C++ (3 OF 3)
- Too Low-level
o Still force programmer to deal with low-level issues
o ex: memory management, pointers
- Too complicated
o Years of expertise required to master (other languages seek to
overcome, like Java and C#)
- Lacking features
o No built-in way to look at object instances
o No built-in way to serialize
o Forces programmer to build such functionality (or learn custom or
3rd party library)
- Slow iteration
o Brittle, hard to try new things
o Code change can take a looong time as can compile
C++ (SUMMARY)
o When to use?
o Any code where performance is crucial
o Used to be all, now game engine such as graphics and AI
o Game-specific code often not C++
o Legacy code base, expertise
o When also use middle-ware libraries in C++
o When not to use?
o Tool building (GUI’s tough)
o High-level game tasks (technical designers)
JAVA (1 OF 3)
- Performance
o Interpreted, garbage collection, security
o So take 4x to 10x hit
+ Can overcome with JIT compiler, Java Native Interface (not
interpreted)
- Platforms
o JVM, yeah, but not all games (most PC games not, nor consoles)
+ Strong for browser-games, mobile
JAVA (3 OF 3)
o Used in:
o Downloadable/Casual games
o PopCap games
o Mummy Maze, Seven Seas, Diamond Mine
o Yahoo online games (WorldWinner)
o Poker, Blackjack
o PC
o Star Wars Galaxies uses Java (and simplified Java for scripting
language)
o You Don’t Know Jack and Who Wants to be a Millionaire all Java
SCRIPTING LANGUAGES (1 OF 3)
o Not compiled, rather specify (script) sequence of actions
+ Ease of development
o Low-level things taken care of
o Fewer errors by programmer
- But script errors tougher, often debuggers worse
o Less technical programming required
o Still, most scripting done by programmers
o Iteration time faster (don’t need to re-compile all code)
o Can be customized for game (ex: just AI tasks)
SCRIPTING LANGUAGES (2 OF 3)
+ Code as an asset
o Ex: consider Peon in C++, with behavior in C++, maybe art as an asset.
Script would allow for behavior to be an asset also
o Can be easily modified, even by end-user in “mod”
- Performance
o Parsed and executed “on the fly”
o Hit could be 10x or more over C++
o Less efficient use of instructions, memory management
-Tool support
o Not as many debuggers, IDEs
o Errors harder to catch
- Interface with rest of game
o Core in C++, must “export” interface
o Can be limiting way interact
o (Hey, draw picture)
o Timeline Based
o Frames and Frame rate (like animations)
o Programmers indicate when (time) event occurs (can occur
across many frames)
o Vector Engine
o Lines, vertices, circles
o Can be scaled to any size, still looks crisp
o Scripting
o ActionScript similar to JavaScript
o Classes (as of Flash v2.0)
o Backend connectivity (load other Movies, URLs)
o Take a break – too close, can’t see it. Remove to provide fresh
prospective
o Explain bug to someone else – helps retrace steps, and others
provide alternate hypotheses
o Debug with partner – provides new techniques
o Introduction (done)
o Agents (next)
o Finite State Machines
o Common AI Techniques
o Promising AI Techniques
GAME AGENTS (1 OF 2)
o Thinking
o Evaluate information and make decision
o As simple or elaborate as required
o Two ways:
o Precoded expert knowledge, typically hand-crafted if-then rules
+ randomness to make unpredictable
o Search algorithm for best (optimal) solution
GAME AGENTS – THINKING (1 OF 3)
o Expert Knowledge
o finite state machines, decision trees, … (FSM most popular, details
next)
o Appealing since simple, natural, embodies common sense
o Ex: if you see enemy weaker than you, attack. If you see enemy
stronger, then go get help
o Often quite adequate for many AI tasks
o Trouble is, often does not scale
o Complex situations have many factors
o Add more rules, becomes brittle
GAME AGENTS – THINKING (2 OF 3)
o Search
o Look ahead and see what move to do next
o Ex: piece on game board, pathfinding (ch 5.4)
o Machine learning
o Evaluate past actions, use for future
o Techniques show promise, but typically too slow
o Need to learn and remember
GAME AGENTS – THINKING (3 OF 3)
o Agent cheating
o Ideally, don’t have unfair advantage (such as more attributes
or more knowledge)
o But sometimes might to make a challenge
o Remember, that’s the goal, AI lose in challenging way
o Best to let player know
AI FOR GAMES – MINI OUTLINE
o Introduction (done)
o Agents (done)
o Common AI Techniques
o Promising AI Techniques
FINITE STATE MACHINES (1 OF 2)
o Problems
o Explosion of states
o Often created with ad hoc structure
FINITE-STATE MACHINE:
APPROACHES
o Three approaches
o Hardcoded (switch statement)
o Scripted
o Hybrid Approach
FINITE-STATE MACHINE:
HARDCODED FSM
void RunLogic( int * state ) {
switch( state )
{
case 0: //Wander
Wander();
if( SeeEnemy() ) { *state = 1; }
break;
case 1: //Attack
Attack();
if( LowOnHealth() ) { *state = 2; }
if( NoEnemy() ) { *state = 0; }
break;
case 2: //Flee
Flee();
if( NoEnemy() ) { *state = 0; }
break;
}
}
FINITE-STATE MACHINE:
PROBLEMS WITH SWITCH FSM
1. Code is ad hoc
o Language doesn’t enforce structure
2. Transitions result from polling
o Inefficient – event-driven sometimes better
3. Can’t determine 1st time state is entered
4. Can’t be edited or specified by game designers or players
FINITE-STATE MACHINE:
SCRIPTED WITH ALTERNATIVE LANGUAGE
AgentFSM
{
State( STATE_Wander )
OnUpdate
Execute( Wander )
if( SeeEnemy ) SetState( STATE_Attack )
OnEvent( AttackedByEnemy )
SetState( Attack )
State( STATE_Attack )
OnEnter
Execute( PrepareWeapon )
OnUpdate
Execute( Attack )
if( LowOnHealth ) SetState( STATE_Flee )
if( NoEnemy ) SetState( STATE_Wander )
OnExit
Execute( StoreWeapon )
State( STATE_Flee )
OnUpdate
Execute( Flee )
if( NoEnemy ) SetState( STATE_Wander )
}
FINITE-STATE MACHINE:
SCRIPTING ADVANTAGES
1. Structure enforced
o Introduction (done)
o Agents (done)
o Finite State Machines (done)
o Common AI Techniques (next)
o Promising AI Techniques
COMMON GAME AI TECHNIQUES