Introduction to Game AI
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Game AI Neil Kirby Course Technology PTR A part of Cengage Learning Australia . Brazil . Japan . Korea . Mexico . Singapore . Spain . United Kingdom . United States Introduction to Game AI © 2011 Course Technology, a part of Cengage Learning. Neil Kirby ALL RIGHTS RESERVED. No part of this work covered by the copyright Publisher and General Manager, herein may be reproduced, transmitted, stored, or used in any form or Course Technology PTR: Stacy L. Hiquet by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web Associate Director of Marketing: distribution, information networks, or information storage and retrieval Sarah Panella systems, except as permitted under Section 107 or 108 of the 1976 Manager of Editorial Services: United States Copyright Act, without the prior written permission of the Heather Talbot publisher. Marketing Manager: Jordan Castellani Acquisitions Editor: Heather Hurley For product information and technology assistance, contact us at 1-800-354-9706 Project Editor: Kate Shoup Cengage Learning Customer & Sales Support, Technical Reviewer: Kevin Dill For permission to use material from this text or product, Copy Editor: Kate Shoup submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to Interior Layout Tech: MPS Limited, A Macmillan [email protected] Company Cover Designer: Mike Tanamachi Cover Photograph: Henry Kempker All trademarks are the property of their respective owners. C CD-ROM Producer: Brandon Penticuff All images Cengage Learning unless otherwise noted. Indexer: Larry Sweazy Library of Congress Control Number: 2009942392 Proofreader: Gene Redding ISBN-13: 978-1-59863-998-8 ISBN-10: 1-59863-998-6 eISBN-10: 1-43545-597-5 Course Technology, a part of Cengage Learning 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in the United States of America 1234567121110 This book is dedicated to my spouse Theresa Kempker and our son Henry. Thanks to you both for giving me the time to do this. Acknowledgments This book would not exist without the support of the game AI crowd that gathers every year at the Game Developers Conference. The late Eric Dybsand inspired many of us by example. I would also like to thank every person who has ever dined with me at one of the AI Programmers Dinners. Thanks go to Steve Woodcock and Steve Rabin for helping me moderate the AI Roundtables at GDC. Dave Mark and Laurie Reynolds proved that you can write a book and stay happily married, something I needed to know before setting off on this adven- ture. Thanks also go to the AI Game Programmers Guild, a group of experts who were only an e-mail away if I got into trouble. Kevin Dill deserves special attention in that regard. Jenifer Niles and Heather Hurley have supported me and the rest of the game AI community over the years. Many of us would not be in print without them. About the Author Neil Kirby is a Member of Technical Staff at Bell Laboratories, the R&D arm of Alcatel-Lucent. He currently develops solutions used to support CMMI certifi- cation. He also provides software architecture consulting services and teaches the course ‘‘Avoiding the Software Performance Crisis.’’ His previous assignments have included building speech-recognition software and teaching at the uni- versity level. He has been a judge of the Ohio State University Fundamentals of Engineering Honors robot competition for many years on behalf of Alcatel- Lucent. Neil holds a master’s degree in computer science from Ohio State University. Neil started writing multiplayer tactical combat computer games in 1987. These included a computer version of ADB’s Star Fleet Battles board game and games of his own design, most notably the futuristic armored ground combat game Bots. These were publicly played at the Ohio State University CACON conventions from 1987 until 1992 but never published. The methodology used to develop the AI in Bots led to his 1991 Computer Game Developers Conference talk, ‘‘Artificial Intelligence Without AI: A Darwinistic Approach.’’ He was under NDA as a consultant to Quicksilver, Software, Inc., during the early phases of devel- opment of Star Fleet Command. Neil moderates the AI Roundtables and hosts the AI Programmers Dinners at GDC. He has contributed articles to AI Game Pro- gramming Wisdom volumes 1, 2, and 4 and is a member of the AI Game Pro- gramming Guild. Neil also serves on the board of the IGDA Foundation and was a driving force behind its creation. Contents Introduction . xii Chapter 1 What Is Game AI? . ............................ 1 An Introduction to Visual Basic . 3 Getting Visual Basic . 3 The Hit Point Calculator Project . 5 Chapter Summary . 17 Chapter Review . 17 References . 18 Chapter 2 Simple Hard-Coded AI .......................... 19 The Good, the Bad, and the Ugly . 19 The Good . 19 TheBad ......................................... 20 The Ugly . 21 Projects . 22 A Simple Thermostat . 23 Analysis . 29 A More Sophisticated Implementation . 32 State of the Art . 39 Chapter Summary . 40 Chapter Review . 41 Exercises . 41 vi Contents vii Chapter 3 Finite State Machines (FSMs) ..................... 43 What Are FSMs? . 43 Design and Analysis . 44 Single-Transition Review . 45 Multiple-Transition Review . 47 Complexity . 49 Failure Modes . 50 Projects . 52 A Brief Foray into Object-Oriented Programming . 52 FSM Objects . 53 Creating the MonsterAI Project . 55 Chapter Summary . 73 Chapter Review . 74 Exercises . 74 References . 74 Chapter 4 Rule-Based Systems . ........................... 75 What Is a Rule-Based AI? . 75 Design and Analysis . 77 Advantages . 78 Disadvantages . 78 The Minesweeper Project . 79 Implementing the Basic Game . 79 Implementing the AI . 99 Chapter Summary . 122 Chapter Review . 122 Exercises . 122 References . 123 Chapter 5 Random and Probabilistic Systems ................ 125 Can That Be AI? . 125 Computing the Odds . 126 Monte Carlo Methods . 126 Precomputing . 127 Faking It . 128 Using the Odds: Factors to Consider . 129 Design and Analysis . 130 Advantages . 130 Disadvantages . 130 viii Contents The Day in the Life Project . 131 The Simulation . 132 Occupations . 132 The Simulated People . 134 Complexity . 135 Implementing the Basic Game . 136 Implementing the AI . 141 Finishing the Code . 143 Results . 147 Chapter Summary . 148 Chapter Review . 148 Exercises . 148 References . 149 Chapter 6 Look-Ahead: The First Step of Planning ............ 151 Evaluation Functions . 152 Pruning . 153 Heuristics . 154 Complexity Without Heuristics . 157 Complexity with the Line Heuristics . 159 Complexity with Depth-Limit Heuristics . 160 Drawbacks to Heuristics . 160 Discrete Moves . ..