Sách tham khảo trí tuệ nhân tạo
Trang 2Ian Millington crosses the boundary between academic and professional game AI with his book Artificial Intelligence for Games Most books either lack academic rigor or are rigorous with algorithms that won’t work under the CPU constraints imposed by modern games This book walks a line between the two and does it well It explains algorithms rigorously while also discussing appropriate implementation details such as scheduling
AI over time and using the right data structures I will be using this book for my Game
AI course.
—Jessica D Bayliss, Ph.D
Rochester Institute of Technology
This is the first serious attempt to create a comprehensive reference for all game AI tices, terminology, and know-how Works like this are badly needed by the maturing video games industry Systematic yet accessible, it is a must-have for any student or pro- fessional.
—Dr Ian Lane Davis
Mad Doc Software
Trang 3The game industry is a powerful and driving force in the evolution of computer nology As the capabilities of personal computers, peripheral hardware, and gameconsoles have grown, so has the demand for quality information about the algo-rithms, tools, and descriptions needed to take advantage of this new technology Tosatisfy this demand and establish a new level of professional reference for the game de-
tech-veloper, we created the Morgan Kaufmann Series in Interactive 3D Technology Books
in the series are written for developers by leading industry professionals and academicresearchers, and cover the state of the art in real-time 3D The series emphasizes prac-tical, working solutions and solid software-engineering principles The goal is for thedeveloper to be able to implement real systems from the fundamental ideas, whether
it be for games or for other applications
Artificial Intelligence for Games
3D Game Engine Architecture:
Engineering Real-Time Applications with
Matt Pharr and Greg Humphreys
Essential Mathematics for Games and Interactive Applications: A Programmer’s Guide
James M Van Verth and Lars M Bishop
Leonard Daly and Don Brutzman
Game Physics Engine Development
Ian Millington
3D Game Engine Design: A Practical Approach to Real-Time Computer Graphics, 2 nd Edition
David H Eberly
Real-Time Cameras
Mark Haigh-Hutchinson
Trang 4INTELLIGENCE FOR GAMES
IAN MILLINGTON
AMSTERDAM • BOSTON • HEIDELBERG
LONDON • NEW YORK • OXFORD
PARIS • SAN DIEGO • SAN FRANCISCO
SINGAPORE • SYDNEY • TOKYO
Trang 5Publishing Services Manager George Morrison
Senior Production Editor Paul Gottehrer
Cover Design Chen Design Associates
Composition VTEX Typesetting Services
Technical Illustration Dartmouth Publishing, Inc.
Copyeditor Andrea Raia
Proofreader Phyllis Coyne Proofreading
Indexer Distributech
Interior printer Maple-Vail Book Manufacturing Group
Cover printer Phoenix Color Corp.
Morgan Kaufmann Publishers is an imprint of Elsevier.
500 Sansome Street, Suite 400, San Francisco, CA 94111
This book is printed on acid-free paper.
© 2006 by Elsevier Inc All rights reserved.
Designations used by companies to distinguish their products are often claimed as trademarks or registered trademarks In all instances in which Morgan Kaufmann Publishers is aware of a claim, the product names appear in initial capital or all capital letters Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, mechanical, photocopying, scanning, or otherwise—without prior written permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44)
1865 843830, fax: (+44) 1865 853333, E-mail: permissions@elsevier.com You may also complete your request on-line via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact” then “Copyright and Permission” and then
For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com or www.books.elsevier.com
Printed in the United States of America
06 07 08 09 5 4 3 2 1
Trang 6To Conor
Trang 7Ian Millington is a partner of Icosagon Ltd (www.icosagon.com), a consulting
com-pany developing next-generation AI technologies for entertainment, modeling, andsimulation Previously he founded Mindlathe Ltd, the largest specialist AI middle-ware company in computer games, working with on a huge range of game genres andtechnologies He has a long background in AI, including PhD research in complexitytheory and natural computing He has published academic and professional papersand articles on topics ranging from paleontology to hypertext
vi
Trang 113.9.2 Converting Steering Behaviors to Three Dimensions 189
Trang 124.6.4 Strange Effects of Hierarchies on Pathfinding 272
Trang 135.2.3 Pseudo-Code 309
5.3.10 Combining Decision Trees and State Machines 341
Trang 146.1.3 Generating the Tactical Properties of a Waypoint 488
Trang 156.2.7 Convolution Filters 521
Trang 16xv
Trang 178.3.4 Replacement Strategies 676
Trang 223.6 Smoothing facing direction of motion over multiple frames 49
3.25 Grazing a wall with a single ray, and avoiding it with three 97
xxi
Trang 233.29 Steering family tree 99
3.38 Priority steering avoiding unstable equilibrium 111
3.44 Obstacle avoidance projected and at right angles 125
3.55 A defensive circle formation with different numbers of characters 153
3.60 An RPG formation, and two examples of the formation filled 168
3.66 An example of slot change in bounding overwatch 179
Trang 244.22 Two poor quantizations show that a path may not be viable 2484.23 Tile-based graph with partially blocked validity 250
4.33 Different node positions for different directions 259
4.36 Smoothed path with a better smoothing indicated 262
Trang 254.42 Pathological example of the maximin method 274
4.46 Different nodes with different times and the same position 288
5.15 An alarm mechanism in a standard state machine 329
5.17 A hierarchical state machine with a cross hierarchy transition 330
5.21 State machine without decision tree transitions 342
5.28 Exclusive mapping to states for fuzzy decision making 3565.29 Why to replace transposition entries lower down 396
Trang 265.33 A join node with variable clash, and two others without 426
6.2 Tactical points are not the best pathfinding graph 476
6.5 A character selecting a cover point in two different ways 482
6.16 Screenshot of a Gaussian blur on an influence map 5286.17 Screenshot of a sharpening filter on an influence map 532
6.20 Averaging the connection cost sometimes causes problems 5396.21 Screenshot of the planning system showing tactical pathfinding 5416.22 Adding waypoints that are not tactically sensible 543
6.26 A hierarchical scheduling system for multi-tier AI 5516.27 State machines for emergent fire team behavior 552
Trang 277.1 The energy landscape of a one-dimensional problem 568
7.3 Non-monotonic fitness landscape with sub-optimal hill climbing 573
7.5 Non-monotonic fitness landscape solved by momentum hill climbing 574
7.10 The decision tree constructed from a simple example 5977.11 Two sequential decisions on the same attribute 607
8.2 Abstract game tree showing terminal and players’ moves 651
Trang 289.7 The behaviors being run in the hierarchical LOD 723
10.3 Angled corridor and a sound transmission error 757
10.9 Line of sight in a sight-connected pair of nodes 762
11.2 A visibility-based graph and its post-processed form 775
13.1 Neural network architecture for creature-teaching games 820
Trang 30A C K N O W L E D G M E N T S
Although it is my name on the cover, this book contains relatively little that originatedwith me But on the other hand it contains relatively few references When I began thisproject Game AI wasn’t as hot as it is today: it had no textbooks, no canonical body
of papers, and few well-established citations for the origins of its wisdom
In my career Game AI has been a field where techniques, gotchas, traps, and spirations are shared more often on the job than in landmark papers I have drawnthe knowledge in this book from a whole web of developers, stretching out from here
in-to all corners of the gaming world Although they undoubtedly deserve it, I’m at a losshow better to acknowledge the contribution of these unacknowledged innovators.There are people with whom I have worked closely who have had a more directinfluence on my AI journey None more so than the excellent team of core AI pro-grammers at Mindlathe: Marcin Chady, who I’ve credited several times for inventions
in this book; Stuart Reynolds, Will Stones, and Ed Davis
Mindlathe in turn wouldn’t have happened without my PhD supervisor Prof.Aaron Sloman Aside from his considerable academic influence, and the game-raisingintellectual challenges he posed me, it is the influence of the Pop-11 programminglanguage he introduced me to (and the Sim-Agent package in particular) that can beseen most often in my work, and in this book
This book has been as epic an experience to write as its bulk might suggest It is
a mammoth task to write text, produce code, create illustrations, act on reviews, andcheck proofs It’s far too much for any one person Adding to the injustice of just myname on the cover, are the contributions of the review team: Toby Allen, Jessica D.Bayliss, Marcin Chady (again), David Eberly, John Laird, and Brian Peltonen: thankyou for your hard-work and incisive comments I have missed one name from the list:the late, and sorely missed, Eric Dybsand also worked on the reviewing of this book,and I’m proud to acknowledge that the benefit I gained from his comments are yetanother part of his extensive legacy to the field
I am particularly grateful for the patience of the editorial team led by Tim Cox
at Morgan Kauffman, aided and abetted by Paul Gottehrer and Jessie Evans, withadditional wisdom and series guidance from Dave Eberly
Late nights and long days aren’t a hardship when you love what you do So withoutdoubt the person who’s had the worst of the writing process was my wife Mel Thankyou for the encouragement to start this, and the support to see it through
xxix
Trang 31Finally, I’d like to dedicate the book to my late friend and colleague ConorBrennan For two years during the writing of this book he’d ask me each time if itwas out yet, and whether he could get a copy Despite his lack of all technical knowl-edge I continually promised him one on the book’s publication He sadly died just afew weeks before it went to press.
Conor enjoyed having his name in print He would proudly show off a mention
in Pete Slosberg’s book Beer for Pete’s Sake It would have appealed to his wry sense of
humor to receive the dedication of a book whose contents would have baffled him
Trang 32P R E FA C E
Two memories stand out in my career writing game AI
The first takes place in a dingy computer lab on the top floor of the computer ence building at Birmingham University in the UK Although I am half-way throughthe first year of my Artificial Intelligence degree, I’ve only been in the departmentfor a couple of weeks after transferring from a Mathematics major Catching up on
sci-a semester of work is, unexpectedly, gresci-at fun, sci-and there sci-are sci-a gresci-at bunch of fellowstudents eager to help me learn about Expert Systems, Natural Language Processing,Philosophy of Mind, and the Prolog programming language
One of my fellow students has written a simple text-based adventure game inProlog I’m not new to game programming—I was part of the 8-bit bedroom codingscene through my teenage years, and by this time had written more than ten gamesmyself But this simple game completely captivates my attention It is the first timeI’ve seen a finite state machine in action There is an Ogre, who can be asleep, dozing,distracted, or angry And you can control his emotions through hiding, playing aflute, or stealing his dinner
All thoughts of assignment deadlines are thrown to the wind, and a day later Ihave my own game in C written with this new technique It is a mind-altering expe-rience, taking me to an entirely new understanding of what is possible The enemiesI’d always coded were stuck following fixed paths, or waited until the player cameclose before homing right in In the FSM I saw the prospect of modeling complexemotional states, triggers, and behaviors And I knew Game AI is what I wanted todo
The second memory is more than ten years later Using some technology oped to simulate military tactics, I have founded a company called Mindlathe, ded-icated to providing artificial intelligence middleware to games and other real-timeapplications It is more than two years into development, and we are well into theprocess of converting prototypes and legacy code into a robust AI engine I am work-ing on the steering system; producing a formation motion plug-in
devel-On screen I have a team of eight robots wandering through a landscape of trees.Using techniques in this book, they are staying roughly in formation, while avoid-ing collisions and taking the easiest route through more difficult terrain The ideaoccurred to me to combine this with an existing demo we had of characters usingsafe-tactical locations to hide in With a few lines of code I had the formation locked
xxxi
Trang 33to tactical locations Rather than robots trying to stay in a V formation, they tried tostick to safe locations, moving forward only if they would otherwise get left behind.Immediately the result was striking: the robots dashed between cover points, movingone at a time, so the whole group made steady progress through the forest, but eachindividual stayed in cover as long as possible.
The memory stays with me, not because of that idea, but because it was the fastestand most striking example of something I had seen many times: that incredibly real-istic results can be gained from intelligently combining very simple algorithms.Both memories, along with many years of experience have taught me that, with agood toolbox of simple AI techniques, you can build stunningly realistic game charac-ters Characters with behaviors that would take far longer to code directly, and would
be far less flexible to changing needs and player tactics
This book is an outworking of that experience It doesn’t tell you how to build asophisticated AI from the ground up It gives you a huge range of simple (and not sosimple) AI techniques that can be endlessly combined, re-used, and parameterized togenerate almost any character behavior that you can conceive
This is the way I, and most of the developers I know, build game AI Those who do
it long-hand each time are a dying breed As development budgets soar, as companiesget more risk averse, and as technology development costs need to be spread overmore titles; having a reliable toolkit of tried-and-tested techniques is the only sanechoice
I hope you’ll find an inspiring palette of techniques in this book that will keep you
in realistic characters for decades to come
Trang 34Windows that allows you to connect to the www.ai4g.com website and download the
complete source code library, including additional content not found on the ROM You may connect in this way as often as you like to get the latest code Patches,errata, and upgrades will only be available in this way Macintosh and Linux users candownload updaters for their platforms from the website The updater only runs whenyou tell it to, it does not include any mal-ware of any kind, and it doesn’t broadcastpersonally identifying information to our site
CD-ELSEVIER CD-ROM LICENSE AGREEMENT
Please read the following agreement carefully before using this CD-ROM product.This CD-ROM product is licensed under the terms contained in this CD-ROM li-cense agreement (“Agreement”) By using this CD-ROM product, you, an individ-ual or entity including employees, agents, and representatives (“you” or “your”), ac-knowledge that you have read this agreement, that you understand it, and that youagree to be bound by the terms and conditions of this agreement Elsevier Inc (“El-sevier”) expressly does not agree to license this CD-ROM product to you unless youassent to this agreement If you do not agree with any of the following terms, you may,within thirty (30) days after your receipt of this CD-ROM product return the unusedCD-ROM product, the book, and a copy of the sales receipt to the customer servicedepartment at Elsevier for a full refund
LIMITED WARRANTY AND LIMITATION OF LIABILITY
NEITHER ELSEVIER NOR ITS LICENSORS REPRESENT OR WARRANT THATTHE CD-ROM PRODUCT WILL MEET YOUR REQUIREMENTS OR THAT ITSOPERATION WILL BE UNINTERRUPTED OR ERROR-FREE WE EXCLUDEAND EXPRESSLY DISCLAIM ALL EXPRESS AND IMPLIED WARRANTIES NOTSTATED HEREIN, INCLUDING THE IMPLIED WARRANTIES OF MERCHANT-ABILITY AND FITNESS FOR A PARTICULAR PURPOSE IN ADDITION, NEI-
xxxiii
Trang 35THER ELSEVIER NOR ITS LICENSORS MAKE ANY REPRESENTATIONS ORWARRANTIES, EITHER EXPRESS OR IMPLIED, REGARDING THE PERFOR-MANCE OF YOUR NETWORK OR COMPUTER SYSTEM WHEN USED IN CON-JUNCTION WITH THE CD-ROM PRODUCT WE SHALL NOT BE LIABLE FORANY DAMAGE OR LOSS OF ANY KIND ARISING OUT OF OR RESULTINGFROM YOUR POSSESSION OR USE OF THE SOFTWARE PRODUCT CAUSED
BY ERRORS OR OMISSIONS, DATA LOSS OR CORRUPTION, ERRORS OR SIONS IN THE PROPRIETARY MATERIAL, REGARDLESS OF WHETHER SUCHLIABILITY IS BASED IN TORT, CONTRACT OR OTHERWISE AND INCLUDING,BUT NOT LIMITED TO, ACTUAL, SPECIAL, INDIRECT, INCIDENTAL OR CON-SEQUENTIAL DAMAGES IF THE FOREGOING LIMITATION IS HELD TO BEUNENFORCEABLE, OUR MAXIMUM LIABILITY TO YOU SHALL NOT EXCEEDTHE AMOUNT OF THE PURCHASE PRICE PAID BY YOU FOR THE SOFTWAREPRODUCT THE REMEDIES AVAILABLE TO YOU AGAINST US AND THE LI-CENSORS OF MATERIALS INCLUDED IN THE SOFTWARE PRODUCT ARE EX-CLUSIVE
OMIS-If this CD-ROM product is defective, Elsevier will replace it at no charge if the tive CD-ROM product is returned to Elsevier within sixty (60) days (or the greatestperiod allowable by applicable law) from the date of shipment
defec-YOU UNDERSTAND THAT, EXCEPT FOR THE 60-DAY LIMITED WARRANTYRECITED ABOVE, ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS ANDAGENTS, MAKE NO WARRANTIES, EXPRESSED OR IMPLIED, WITH RESPECT
TO THE CD-ROM PRODUCT, INCLUDING, WITHOUT LIMITATION THE PRIETARY MATERIAL, AND SPECIFICALLY DISCLAIM ANY WARRANTY OFMERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
PRO-IN NO EVENT WILL ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS ORAGENTS, BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, WITHOUTLIMITATION, ANY LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTAL
OR CONSEQUENTIAL DAMAGES, ARISING OUT OF YOUR USE OR ITY TO USE THE CD-ROM PRODUCT REGARDLESS OF WHETHER SUCHDAMAGES ARE FORESEEABLE OR WHETHER SUCH DAMAGES ARE DEEMED
INABIL-TO RESULT FROM THE FAILURE OR INADEQUACY OF ANY EXCLUSIVE OROTHER REMEDY
SOFTWARE LICENSE
IMPORTANT: PLEASE READ THE FOLLOWING AGREEMENT CAREFULLY
BY COPYING, INSTALLING OR OTHERWISE USING THE SOURCE CODE ONTHE ACCOMPANYING CD-ROM, YOU ARE DEEMED TO HAVE AGREED TOTHE TERMS AND CONDITIONS OF THIS LICENSE AGREEMENT
1 This LICENSE AGREEMENT is between the IPR VENTURES, having an office at2(B) King Edward Road, Bromsgrove, B61 8SR, United Kingdom (“IPRV”), and the
Trang 36Individual or Organization (“Licensee”) accessing and otherwise using the software
on the accompanying CD-ROM (“AI CORE”) in source or binary form and its ciated documentation
asso-2 Subject to the terms and conditions of this License Agreement, IPRV hereby grantsLicensee a non-exclusive, royalty-free, world-wide license to reproduce, analyze, test,perform and/or display publicly, prepare derivative works, distribute, and otherwiseuse AI CORE alone or in any derivative version, provided, however, that IPRVs Li-cense Agreement is retained in AI CORE, alone or in any derivative version prepared
by Licensee
3 In the event Licensee prepares a derivative work that is based on or incorporates
AI CORE or any part thereof, and wants to make the derivative work available to thepublic as provided herein, then Licensee hereby agrees to indicate in any such workthe nature of the modifications made to AI CORE
4 IPRV is making AI CORE available to Licensee on an “AS IS” basis IPRV MAKES
NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED BY WAY OFEXAMPLE, BUT NOT LIMITATION, IPRV MAKES NO AND DISCLAIMS ANYREPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FORANY PARTICULAR PURPOSE OR THAT THE USE OF AI CORE WILL NOT IN-FRINGE ANY THIRD PARTY RIGHTS
5 IPRV SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THESOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAM-AGES OR LOSS AS A RESULT OF USING, MODIFYING OR DISTRIBUTING AICORE, OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBIL-ITY THEREOF
6 This License Agreement will automatically terminate upon a material breach of itsterms and conditions
7 This License Agreement shall be governed by and interpreted in all respects by thelaw of England, excluding conflict of law provisions Nothing in this License Agree-ment shall be deemed to create any relationship of agency, partnership, or joint ven-ture between IPRV and Licensee This License Agreement does not grant permission
to use IPRV trademarks or trade name in a trademark sense to endorse or promoteproducts or services of Licensee, or any third party
8 By copying, installing or otherwise using AI CORE, Licensee agrees to be bound bythe terms and conditions of this License Agreement
Trang 38ARTIFICIAL INTELLIGENCE FOR GAMES
Trang 40P A R T I
AI and Games