O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Game AI Programming with Lua

Book Description

Leverage the power of Lua programming to create game AI that focuses on motion, animation, and tactics

In Detail

Game AI can be easily broken up into a number of components such as decision making, animation handling, and tactics, but the balance and interaction between each system strikes a balance between good AI and bad AI.

Beginning with an introduction to the AI sandbox, each new aspect of game AI is introduced, implemented, and then expanded upon. Going forward, you will learn to utilize open source libraries such as Ogre3D, Bullet Physics, OpenSteer, Recast, Detour, and Lua to create an AI sandbox with an entire codebase available to expand and step through.

This is done through a step-by-step approach, from learning to move basic shapes to fully animating your soldiers. By the end of the book, your AI will be able to navigate, pathfind, manage animation playback, communicate, and perceive their environment.

What You Will Learn

  • Create an animation state machine to drive AI animations within Lua
  • Build and find paths on navigation meshes
  • Write and debug Lua scripts within a full-scale Lua IDE
  • Develop decision logic with behavior trees, state machines, and decision trees to build modular, reusable AI
  • Manage short- and long-term knowledge representation with blackboard data structures
  • Add sensory perception to give AIs the ability to see and hear
  • Develop high-level tactics with multiple AIs based on influence maps

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Game AI Programming with Lua
    1. Table of Contents
    2. Learning Game AI Programming with Lua
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with AI Sandbox
      1. Introduction to AI sandbox
        1. Understanding the sandbox
        2. The project layout
        3. The Premake build
        4. Compiling the sandbox with Visual Studio 2008/2010/2012/2013
        5. Open source libraries
        6. Open source tools
        7. Lua IDE – Decoda
        8. Running AI sandbox inside Decoda
        9. Setting up a new Decoda project
        10. Debugging Lua scripts
        11. Decoda Watch window
        12. Decoda Call Stack window
        13. The Decoda Virtual Machines window
        14. Simultaneous Lua and C++ debugging
        15. Visual Studio – Attach to Process
        16. Decoda – Attach to Process
        17. Decoda – Attach System Debugger
        18. Associating Lua scripts from code with Decoda
        19. The Lua virtual machine
        20. The Lua stack
        21. Lua primitives
        22. Metatables
        23. Metamethods
        24. Userdata
        25. C/C++ calling Lua functions
        26. Lua calling C/C++ functions
          1. Function binding
        27. Creating custom userdata
          1. Looking at the vector data type
        28. The demo framework
          1. Ogre
          2. Object-Oriented Input System
          3. SandboxApplication
          4. Sandbox
          5. Agent
          6. Utility classes
          7. Lua binding
      2. Summary
    9. 2. Creating and Moving Agents
      1. Creating a new sandbox project
      2. Setting up the file structure
      3. Extending the SandboxApplication class
      4. Running your sandbox for the first time
      5. Creating a new Decoda project
      6. Configuring Decoda's run executable
      7. Creating a sandbox Lua script
        1. Creating a floor
        2. Adding a light
        3. Adding a skybox
        4. Adding meshes to the sandbox
        5. Creating sandbox objects
      8. Shooting blocks
      9. Creating an agent Lua script
        1. Creating a visual representation
        2. Updating an agent position
        3. Updating an agent orientation
      10. Agent properties
        1. Orientation
          1. The forward axis
          2. The left axis
          3. The up axis
        2. Location
          1. Position
        3. Size
          1. Height
          2. Radius
      11. Physics
        1. Mass
        2. The max force
        3. The max speed
        4. Speed
        5. Velocity
      12. Knowledge
        1. Target
        2. Target radius
        3. Path
      13. Agents' movement
        1. Mass
        2. Speed
        3. Velocity
        4. Acceleration
        5. Force
      14. Agent-steering forces
        1. Seeking
        2. Applying steering forces to an agent
        3. Clamping the horizontal speed of an agent
        4. Creating a seeking agent
        5. Pursuit
        6. Fleeing
        7. Evasion
        8. Wandering
        9. The target speed
        10. Path following
        11. Creating a path following agent
      15. Avoidance
        1. Collision avoidance
        2. Obstacle avoidance
      16. Avoiding blocks and agents
      17. Group steering
        1. Alignment
        2. Cohesion
        3. Separation
      18. Creating a group of followers
      19. Summing steering forces
        1. Weighted sums
        2. Priority-based forces
      20. Summary
    10. 3. Character Animations
      1. Skeletons and meshes
        1. Mesh skeletons
        2. Loading an animated mesh
        3. Showing a skeleton
      2. Attaching meshes to bones
        1. Attaching a weapon to our soldier
      3. Animation clips
        1. Playing an animation on our soldier
        2. Soldier animations
          1. Crouching animations
          2. Standing animations
          3. Weapon animations
      4. Soldier poses
        1. Weapon poses
      5. Manipulating animations
        1. Enabling and disabling animations
        2. Looping animations
        3. The animation length
        4. The animation time
        5. Normalized time
        6. Restarting an animation
        7. Playing a non-looping animation
        8. The animation rate
      6. Animation blending
        1. Animation weights
        2. Blend window
        3. Blend curves
        4. Linear blending
        5. Playing with blend weights
      7. Animation state machine (ASM)
        1. States
        2. Transitions
        3. Creating animation state machines
        4. Creating helper functions
        5. Adding states
        6. Adding transitions
        7. Adding external helper functions
        8. Forcefully setting states
        9. Requesting states
        10. Updating the animation state machine
        11. Handling state transitions and state requests
        12. Updating running animations
        13. Adding functions to animation state machine instances
      8. Building a weapon animation state machine
      9. Building a soldier animation state machine
      10. Updating animation state machines
      11. Playing with states
      12. Summary
    11. 4. Mind Body Control
      1. Creating a body
        1. Creating a soldier
        2. Attaching an animated mesh to an agent
        3. Creating an obstacle course
          1. Displaying the physics world
      2. Adding callbacks to the animation state machine
        1. Handling callbacks
        2. Adding callbacks to the ASM
        3. Updating the ASM to call callbacks
      3. Getting our soldier to shoot
        1. The bone position
        2. The bone rotation
        3. Creating particle effects
        4. The particle direction
        5. Object removal
        6. The collision impact callback
        7. Shooting a projectile
        8. Handling projectile collision impacts
        9. Shooting
      4. Getting our soldier to run
        1. Setting a path through the obstacle course
        2. Running the obstacle course
      5. Creating a brain
        1. Approaches for mind body control
      6. Direct animation control
        1. The death state
        2. The idle state
        3. The falling state
        4. The moving state
        5. The shooting state
      7. A simple, finite state machine
        1. Initializing the agent
        2. Agent FSM state handling
      8. Indirect animation control
        1. The animation controller
        2. Commands
        3. The command queue
        4. Manipulating commands
        5. The change stance command
        6. The die command
        7. The fall command
        8. The idle command
        9. The move command
        10. The shoot command
        11. Assigning member functions
        12. Initializing the controller
        13. Adding handlers for commands
        14. Updating the controller
      9. Running the obstacle course
        1. Creating a direct control agent
        2. Creating an indirect control agent
        3. Indirect control agent initialization
        4. Indirect control agent update
        5. Indirect control agent control
        6. Spawning an indirect control agent
      10. Action latency
      11. Summary
    12. 5. Navigation
      1. Pathfinding
      2. Creating a navigation mesh
        1. Configuring navigation meshes
        2. The walkable height
        3. The walkable radius
        4. The walkable climb height
        5. The walkable slope angle
        6. The minimum region area
        7. Building the navigation mesh
        8. Drawing the navigation mesh
      3. Pathfinding on a navigation mesh
        1. Path query
        2. Query results
        3. Random navigation points
      4. The path information
      5. Adding random pathfinding to our soldier
        1. Updating agent paths
        2. Drawing paths
        3. Initializing the navmesh
        4. Randomly running agents
      6. Creating additional navigation meshes
      7. Summary
    13. 6. Decision Making
      1. Creating userdata
      2. Agent actions
        1. Adding data members
        2. Initializing an action
        3. Updating an action
        4. Action cleanup
        5. Action member functions
      3. Creating actions
        1. The idle action
        2. The die action
        3. The reload action
        4. The shoot action
        5. The random move action
        6. The move action
        7. The flee action
        8. The pursue action
      4. Evaluators
      5. Creating evaluators
        1. Constant evaluators
        2. Has ammo evaluator
        3. Has critical health evaluator
        4. Has enemy evaluator
        5. Has move position evaluator
        6. Is alive evaluator
        7. Can shoot enemy evaluator
        8. 50/50 chance evaluator
      6. Decision structures
      7. Decision trees
        1. Branches
        2. Decision leaves
        3. Branch evaluation
      8. Building a decision tree
        1. Creating branches
      9. Creating a decision tree agent
        1. Strengths of decision trees
        2. Pitfalls of decision trees
      10. Finite state machines
        1. States
        2. Transitions
        3. Finite state machine structure
        4. Helper functions
        5. Adding states and transitions
        6. Updating the finite state machine
        7. Adding instance functions
      11. Building a finite state machine
        1. The idle state
        2. The movement state
        3. The random movement state
        4. The shoot state
        5. The flee state
        6. The die state
        7. The pursue state
        8. The reload state
      12. Creating a finite state machine agent
      13. Strengths of finite state machines
      14. Pitfalls of finite state machines
      15. Behavior trees
        1. The behavior tree node
        2. Helper functions
        3. Updating the behavior tree node
      16. Actions
      17. Conditions
      18. Selectors
      19. Sequences
      20. Creating a behavior tree object
        1. Behavior tree helper functions
        2. Selector evaluation
        3. Sequence evaluation
        4. Node evaluation
        5. Continue behavior tree evaluation
        6. The behavior tree update loop
        7. Updating the behavior tree
      21. Building a behavior tree
        1. The death behavior
        2. The flee behavior
        3. Combat behaviors
        4. The reload behavior
        5. The shoot behavior
        6. The pursue behavior
        7. The move behavior
        8. The random move behavior
        9. The idle behavior
      22. Creating a behavior tree agent
      23. Strengths of behavior trees
      24. Pitfalls of behavior trees
      25. Summary
    14. 7. Knowledge Representation
      1. Knowledge sources
        1. Creating a knowledge source
        2. Knowledge source evaluation
      2. Blackboards
        1. Creating a blackboard
        2. Adding and removing knowledge sources
        3. Evaluating knowledge sources
        4. Setting and retrieving blackboard attributes
        5. Blackboard member functions
      3. Creating soldier knowledge sources
        1. Enemy selection
        2. Flee position selection
      4. Constructing a soldier blackboard
      5. Updating decision evaluators
      6. Updating behavior actions
        1. The die action
        2. The flee action
        3. The idle action
        4. The move action
        5. The pursue action
        6. The reload action
        7. The shoot action
      7. Summary
    15. 8. Perception
      1. Events
        1. Attributes
        2. Sending events
        3. Receiving events
      2. Managing events
        1. Assigning agent teams
        2. Handling agent communications
        3. Event types
      3. Creating agent senses
        1. Initializing senses
        2. Updating senses
      4. Agent visibility
        1. Detecting other visible agents
      5. Agent sighting events
        1. New enemy sighted event
        2. New dead enemy body sighted event
        3. New dead teammate body sighted event
      6. Handling new agent sightings
        1. Intermittent agent sightings
        2. Throttling agent visibility updates
        3. Creating event handlers
        4. Adding event handlers
      7. Agent auditory senses
      8. Auditory events
        1. The BulletShot event
        2. The BulletImpact event
      9. Handling auditory events
      10. Decaying blackboard events
      11. Decaying auditory events
      12. Team communications
        1. The EnemySelection event
        2. The PositionUpdate event
        3. The RetreatPosition event
      13. Updating agent behaviors
        1. Enemy selection
        2. Scoring dangerous positions
          1. Score danger from bullet impacts
          2. Score danger from bullet shots
          3. Score danger from enemies
          4. Score danger from dead bodies
        3. Calculating the best flee position
      14. Summary
    16. 9. Tactics
      1. Influence maps
        1. The cell height
        2. The cell width
      2. Constructing an influence map
        1. Configuration
        2. Voxelizing a navigation mesh
      3. Drawing influence maps
      4. Accessing influences
        1. Setting influences
        2. Getting influences
      5. Clearing influences
      6. Spreading influences
        1. Cell inertia
        2. Cell falloff
      7. Influence map layers
      8. Updating the influence map
      9. Soldier tactics
        1. Initializing and updating tactics
      10. Scoring team influences
        1. Initializing team influences
        2. Updating team influences
        3. Configuring team influences
      11. Scoring dangerous areas
        1. Tapping into agent events
        2. Adding event handlers
        3. Initializing dangerous influences
        4. Updating dangerous influences
        5. Configuring team influences
      12. Summary
    17. Index