Building a Sports AI Architecture Ushhan D. Gundevia November 8, 2004 Terry Wellmann He has been programming since 1983 Responsible for architecting and writing AI for Microsoft’s NBA Inside Drive franchise He was also the lead programmer on AllStar Baseball for N64 Has a Computer Science Degree from Purdue terry.wellmann@high-voltage.com If you would like to know more http://xbox.ign.com/articles/372/372418p1.html http://www.mobygames.com/developer/sheet/vi ew/by_genre/developerId,126714/ Challenges Real teams spend countless hours practicing together – Why? To improve an individuals skills and abilities To train a group of independently thinking individuals to instinctively react to a situation in a manner predictable by other teammates To stimulate cohesive group decision making in a game is difficult Things to keep in mind Keep it simple Break down decisions into various levels Spend time on planning Don’t be afraid to make mistakes Do not underestimate the power of Randomness Agent Plans Examples of Offensive, Defensive and Shared plans Pass Shoot Drive to basket Run the play Rescue a trapped ball handler … Pseudo code for Agent Plan Class AgentPlan { … float EvaluateInitiation(); float EvaluateContinuation(); void Initiate(); void Update(); … } Why float? The EvaluateInitiation and EvaluateContinuation functions return float values Always building complex systems, with several plans compared to determine which one is used Each plan is a building block in a complex system Each plan individually evaluates the current situation to determine its feasibility Returns a value between -1.0 and 1.0 Execute plans if value > 0 To strongly encourage a plan, return a value > 1 Team Management Its implemented using a FSM Each state encapsulates a different goal and a set of responsibilities Basketball can be assumed to have 3 kinds of states Offensive States Defensive States Common States Offensive and Defensive States They mirror each other Responsible for all aspects of the game play that the user can interact with. They are ? Inbound Transition Frontcourt Rebound Recover Loose Ball Free Throw Common States Neutral situations when the ball is not in play and neither team is in offense or defense They are ? Pre-game Tip-off Time-out Quarter Break Substitution Post Game State Transition and Flows Pseudo Code - Frontcourt Offense State Class FrontCourtOffense : public TeamStateBase { protected: enum TBallHandlerPlans { eBallHandlerShot, eBallHandlerPass, eBallHandlerDrive, eBallHandlerPlayMovement, … }; enum TNonBallHandlerPlans { eNonBallHandlerRescueBallHandler, eNonBallHandlerGetOpenForPass, eNonBallHandlerSetPick eNonBallHandlerPlayMovement … } public: … int Update(int); int ReInit(void); … }; What’s that? The key elements are the two enumerated types that define priority order We might also have 2 different update functions for a BallHandler and a NonBallHandler for simplicity In some situations priorities might not be enough, me may need some explicit ifthen logic Any Idea where? Pseudo Code for Update Fn for (each plan to evaluate) { //get the proper evaluation value if (planToCheck == currentPlan) evalValue = EvaluateContinuation(); else evalValue = EvaluateInitiation(); //is the current plan the best option //simple priority order plan selection if (evalValue > bestEvalValue) { bestEvalValue = evalValue; bestPlan = currentPlan; } //handle situations the priority method can’t else if (bestValue > 0.0f && bestPlan == shot) //is the drive plan a valid option if (currentPlan == drive && evalValue > 0.0f) //set the best plan to drive, but leave the bestValue as it is – Why? bestPlan = drive; //if the best plan is the current plan, update it else initiate it if (bestPlan == currentPlan) currentPlan->Update(); else currentPlan->Initiate(); } Agent AI A well thought-out collections of Utility Functions and data used by the plans to evaluate and execute the things they represent Shot Plan Taking a closer look at the shot plan Three Potential success rate Players skills and abilities – denoted by rating values Penalties – e.g. defensive pressure applied to the player Type of shot things to keep in mind Is Dunk possible? How far is he from the basket? Does he have a clear path to the basket? Can he dunk? Is Lay-up possible? Same questions as above, but every professional NBA player can lay-up Last option is Jump shot Tell the player how to perform that shot High-Level Agent AI Command issuing functions Decisions that require more logic then a simple random no. Whether to do a flashy 360° dunk or a simple one Wrapper for Agent Mechanics Agent Mechanics Low-Level AI Manage and select the animations Simple decisions that require no more than Random nos. Like commands given to people who have no knowledge about basketball, but can follow simple directions. e.g. walk, jog, run Conclusion Sports games present a unique set of challenges The rules are well defined and allow little room for creativity Professional athletes have personalities and styles that are recognized by millions of fans User should be able to capture the abilities, personalities and emotions of the athletes being stimulated References Building a Sports AI Architecture – AI Game Programming Wisdom 2 Learning Goals in Sports Games – Jack van Rijswijck www.gdconf.com/archives/2003/Van_Ryswyck_Jack.doc A very nice paper dealing with learning in Sports Games, using Soccer as the base. Thank You