ARTIFICIAL INTELLIGENCE FOR GAMES Second Edition IANMILLINGTONand JOHNFUNGE AMSTERDAM•BOSTON•HEIDELBERG•LONDON NEWYORK•OXFORD•PARIS•SANDIEGO SANFRANCISCO•SINGAPORE•SYDNEY•TOKYO MorganKaufmannPublishersisanimprintofElsevier MorganKaufmannPublishersisanimprintofElsevier. 30CorporateDrive,Suite400,Burlington,MA01803,USA Thisbookisprintedonacid-freepaper. Copyright©2009byElsevierInc.Allrightsreserved. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarksorregisteredtrademarks.Inall instancesinwhichMorganKaufmannPublishersisawareofaclaim,theproductnamesappearininitialcapitalorallcapital letters.Alltrademarksthatappearorareotherwisereferredtointhisworkbelongtotheirrespectiveowners.NeitherMorgan KaufmannPublishersnortheauthorsandothercontributorsofthisworkhaveanyrelationshiporaffiliationwithsuch trademarkownersnordosuchtrademarkownersconfirm,endorseorapprovethecontentsofthiswork.Readers,however, shouldcontacttheappropriatecompaniesformoreinformationregardingtrademarksandanyrelatedregistrations. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformorbyany means—electronic,mechanical,photocopying,scanning,orotherwise—withoutpriorwrittenpermissionofthepublisher. PermissionsmaybesoughtdirectlyfromElsevier’sScience&TechnologyRightsDepartmentinOxford,UK:phone:(+44)1865 843830,fax:(+44)1865853333,E-mail:permissions@elsevier.com.YoumayalsocompleteyourrequestonlineviatheElsevier homepage(http://elsevier.com),byselecting“Support&Contact”then“CopyrightandPermission”andthen“Obtaining Permissions.” LibraryofCongressCataloging-in-PublicationData Millington,Ian. Artificialintelligenceforgames/IanMillington,JohnFunge.–2nded. p.cm. Includesindex. ISBN978-0-12-374731-0(hardcover:alk.paper) 1.Computergames–Programming.2.Computeranimation.3.Artificialintelligence. I.Funge,JohnDavid,1968-II.Title. QA76.76.C672M5492009 006.3–dc22 2009016733 BritishLibraryCataloguing-in-PublicationData AcataloguerecordforthisbookisavailablefromtheBritishLibrary. ISBN:978-0-12-374731-0 ForinformationonallMorganKaufmannpublications visitourWebsiteatwww.mkp.comorwww.elsevierdirect.com Typesetby:diacriTech,India PrintedintheUnitedStatesofAmerica 09 10 11 12 13 5 4 3 2 1 For Conor – I.M. For Xiaoyuan – J.F. About the Authors IanMillington isapartnerof IcosagonLtd.(www.icosagon.com),aconsultingcompanydevel- opingnext-generationAItechnologiesforentertainment,modeling,andsimulation.Previously he founded Mindlathe Ltd.,the largest specialist AI middleware company in computer games, working on a huge range of game genres and technologies. He has a long background in AI, includingPhDresearchincomplexitytheoryandnaturalcomputing.Hehaspublishedacademic andprofessionalpapersandarticlesontopicsrangingfrompaleontologytohypertext. JohnFunge(www.jfunge.com)recentlyjoinedNetflixtostartandleadthenewGamePlatforms group.Previously,Johnco-foundedAiLiveandspentnearlytenyearshelpingtocreateasuccessful companythatisnowwellknownforitspioneeringmachinelearningtechnologyforgames.AiLive co-createdtheWiiMotionPlushardwareandhasestablisheditsLiveMoveproductsastheindustry standardforautomaticmotionrecognition.AtAiLiveJohnalsoworkedextensivelyonLiveAI,a real-timebehaviorcaptureproductthatisbeingusedbytheformerleadgamedesignerofGuitar HeroandRockBandtocreateanewgenreofgame.JohnisalsoanAssistantAdjunctProfessor attheUniversityof California,SantaCruz(UCSC)whereheteachesaGameAIcoursethathe proposed,designedanddeveloped.JohnhasaPhDfromtheUniversityofTorontoandanMSc from the University of Oxford. He holds several patents, is the author of numerous technical papers,andwrotetwopreviousbooksonGameAI. iv Contents About the Authors iv Acknowledgments xix Preface xxi About the Website xxiii Part I AI and Games 1 Chapter 1 Introduction 3 What Is AI? 1.1 4 1.1.1 AcademicAI 5 1.1.2 GameAI 7 Model of Game AI 1.2 8 1.2.1 Movement 9 1.2.2 DecisionMaking 10 1.2.3 Strategy 10 1.2.4 Infrastructure 11 1.2.5 Agent-BasedAI 11 1.2.6 IntheBook 12 Algorithms, Data Structures, and 1.3 Representations 12 1.3.1 Algorithms 12 1.3.2 Representations 15 v vi Contents On the Website 1.4 16 1.4.1 Programs 16 1.4.2 Libraries 17 Layout of the Book 1.5 18 Chapter 2 Game AI 19 The Complexity Fallacy 2.1 19 2.1.1 WhenSimpleThingsLookGood 19 2.1.2 WhenComplexThingsLookBad 20 2.1.3 ThePerceptionWindow 21 2.1.4 ChangesofBehavior 21 The Kind of AI in Games 2.2 22 2.2.1 Hacks 22 2.2.2 Heuristics 23 2.2.3 Algorithms 24 Speed and Memory 2.3 25 2.3.1 ProcessorIssues 25 2.3.2 MemoryConcerns 28 2.3.3 PCConstraints 29 2.3.4 ConsoleConstraints 29 The AI Engine 2.4 31 2.4.1 StructureofanAIEngine 32 2.4.2 ToolchainConcerns 33 2.4.3 PuttingItAllTogether 34 Part II Techniques 37 Chapter 3 Movement 39 The Basics of Movement Algorithms 3.1 40 3.1.1 Two-DimensionalMovement 41 3.1.2 Statics 42 3.1.3 Kinematics 45 Kinematic Movement Algorithms 3.2 49 3.2.1 Seek 49 vii Contents 3.2.2 Wandering 53 3.2.3 OntheWebsite 55 Steering Behaviors 3.3 55 3.3.1 SteeringBasics 55 3.3.2 VariableMatching 56 3.3.3 SeekandFlee 56 3.3.4 Arrive 59 3.3.5 Align 62 3.3.6 VelocityMatching 66 3.3.7 DelegatedBehaviors 67 3.3.8 PursueandEvade 68 3.3.9 Face 71 3.3.10 LookingWhereYou’reGoing 72 3.3.11 Wander 73 3.3.12 PathFollowing 76 3.3.13 Separation 82 3.3.14 CollisionAvoidance 84 3.3.15 ObstacleandWallAvoidance 90 3.3.16 Summary 95 Combining Steering Behaviors 3.4 95 3.4.1 BlendingandArbitration 96 3.4.2 WeightedBlending 96 3.4.3 Priorities 103 3.4.4 CooperativeArbitration 107 3.4.5 SteeringPipeline 108 Predicting Physics 3.5 120 3.5.1 AimingandShooting 121 3.5.2 ProjectileTrajectory 121 3.5.3 TheFiringSolution 123 3.5.4 ProjectileswithDrag 126 3.5.5 IterativeTargeting 128 Jumping 3.6 134 3.6.1 JumpPoints 135 3.6.2 LandingPads 138 3.6.3 HoleFillers 143 Coordinated Movement 3.7 144 3.7.1 FixedFormations 144 3.7.2 ScalableFormations 146 3.7.3 EmergentFormations 146 3.7.4 Two-LevelFormationSteering 147 3.7.5 Implementation 151 viii Contents 3.7.6 ExtendingtoMorethanTwoLevels 157 3.7.7 SlotRolesandBetterAssignment 159 3.7.8 SlotAssignment 162 3.7.9 DynamicSlotsandPlays 166 3.7.10 TacticalMovement 168 Motor Control 3.8 171 3.8.1 OutputFiltering 172 3.8.2 Capability-SensitiveSteering 174 3.8.3 CommonActuationProperties 175 Movement in the Third Dimension 3.9 178 3.9.1 RotationinThreeDimensions 178 3.9.2 ConvertingSteeringBehaviorstoThreeDimensions 180 3.9.3 Align 180 3.9.4 AligntoVector 181 3.9.5 Face 183 3.9.6 LookWhereYou’reGoing 186 3.9.7 Wander 186 3.9.8 FakingRotationAxes 188 Exercises 192 Chapter 4 Pathfinding 197 The Pathfinding Graph 4.1 198 4.1.1 Graphs 198 4.1.2 WeightedGraphs 199 4.1.3 DirectedWeightedGraphs 202 4.1.4 Terminology 203 4.1.5 Representation 203 Dijkstra 4.2 204 4.2.1 TheProblem 205 4.2.2 TheAlgorithm 206 4.2.3 Pseudo-Code 210 4.2.4 DataStructuresandInterfaces 212 4.2.5 PerformanceofDijkstra 214 4.2.6 Weaknesses 214 A* 4.3 215 4.3.1 TheProblem 216 4.3.2 TheAlgorithm 216 4.3.3 Pseudo-Code 220 4.3.4 DataStructuresandInterfaces 223 ix Contents 4.3.5 ImplementationNotes 228 4.3.6 AlgorithmPerformance 228 4.3.7 NodeArrayA* 229 4.3.8 ChoosingaHeuristic 231 World Representations 4.4 237 4.4.1 TileGraphs 239 4.4.2 DirichletDomains 241 4.4.3 PointsofVisibility 244 4.4.4 NavigationMeshes 246 4.4.5 Non-TranslationalProblems 251 4.4.6 CostFunctions 251 4.4.7 PathSmoothing 251 Improving on A* 4.5 255 Hierarchical Pathfinding 4.6 255 4.6.1 TheHierarchicalPathfindingGraph 256 4.6.2 PathfindingontheHierarchicalGraph 259 4.6.3 HierarchicalPathfindingonExclusions 262 4.6.4 StrangeEffectsofHierarchiesonPathfinding 263 4.6.5 InstancedGeometry 265 Other Ideas in Pathfinding 4.7 271 4.7.1 OpenGoalPathfinding 272 4.7.2 DynamicPathfinding 272 4.7.3 OtherKindsofInformationReuse 273 4.7.4 LowMemoryAlgorithms 273 4.7.5 InterruptiblePathfinding 274 4.7.6 PoolingPlanners 275 Continuous Time Pathfinding 4.8 276 4.8.1 TheProblem 276 4.8.2 TheAlgorithm 277 4.8.3 ImplementationNotes 281 4.8.4 Performance 281 4.8.5 Weaknesses 282 Movement Planning 4.9 282 4.9.1 Animations 282 4.9.2 MovementPlanning 283 4.9.3 Example 286 4.9.4 Footfalls 287 Exercises 288
Description: