scripting and actions robin burke gam 376 fall 2006
TRANSCRIPT
![Page 1: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/1.jpg)
Scripting and Actions
Robin Burke
GAM 376
Fall 2006
![Page 2: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/2.jpg)
Outline
Midterm solutions Scripting
Why How Lua
• language• binding to C++
Custom languages Action systems
![Page 3: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/3.jpg)
Midterm solutions: Q1
DFS BFS ID
1
65
432
![Page 4: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/4.jpg)
Q2
Arrive Alignment Evade Obstacle Avoidance
![Page 5: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/5.jpg)
Q3
States Conditions Graph
![Page 6: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/6.jpg)
Q4
Preferred velocity Leading distance Following distance
![Page 7: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/7.jpg)
Scripting
Two senses of the word "scripted behavior"
• having agents follow pre-set actions• rather than choose them dynamically
"scripting language"• using a dynamic language to make a game easier
to modify The senses are related
a dynamic language is an excellent tool for working with agent
behaviors
![Page 8: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/8.jpg)
Scripted behavior
Write a fixed action sequenceas opposed to something that "make
sense" in the game world
![Page 9: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/9.jpg)
Example
fixed trigger regions when an enemy unit enters this area send these waiting units to attack
player reaction build up a big force just outside the trigger
area then attack
Doesn't truly simulate scouting and preparedness
![Page 10: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/10.jpg)
A better simulated version
Assign a patrol unituse information to influence
production planningvary depth of patrol with stage of
game Use "expected cost of ignorance"
an idea from surveillance theory
![Page 11: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/11.jpg)
Advantages of scripting
Much, much fasterto apply a simple rule than to run a
physical simulation Easy to write, understand and modify
![Page 12: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/12.jpg)
Disadvantages of scripting
Limits player creativity Players will try things that "should" work
• based on extensive physical intuition Will be disappointed if they don't
Allow degenerate strategies players will learn the limits of the scripts and exploit them
Game will need many scripts predicting their interactions can be difficult complex debugging problem
![Page 13: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/13.jpg)
Scripting languages
Scripting languages came into being so that level designers could design simple
behaviors• triggers• level interaction
without having to mess with the game internals
Scripting languages also enable modding can be an important factor in the PC game
market• witness UnrealTournament, Neverwinter Nights
![Page 14: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/14.jpg)
Issues
Speed Execution model Integration Re-Entrancy
![Page 15: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/15.jpg)
Speed
Always important, esp. for game AI In most cases
AI scripts will be part of inner game loop
• character behavior• event handling
![Page 16: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/16.jpg)
Execution model
Interpreted Processed bottom-up each time
Compiled Almost always byte-compiled
• compiled into lower level instructions• but not machine instructions
Interpreting byte-code much faster than interpretation
Can remove compiler when distributing code• user can't mess with script internals
![Page 17: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/17.jpg)
Integration
Must be possible to integrate the scripting language with the gameExpose crucial methods and data
structures to scriptersManage script execution from within
the game If possible
avoid translation between data types and calling protocols
![Page 18: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/18.jpg)
Re-Entrant
Re-entrant code can be called safely from multiple processes Often scripts will be shared among multiple
instances of an AI A script may also need to be suspended
and reactivated to distributed the AI load over multiple
frames Some script sections may not be
interruptible for example, modifying velocity and
orientation
![Page 19: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/19.jpg)
Choices
Write your ownmore later
Lua Python Scheme
![Page 20: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/20.jpg)
Scheme
Lisp variant lots of parentheses
Pluses very small implementations exist very efficient AI-friendly no distinction between code and data
Minuses awkward for non-initiates less common
(Insert ad for CSC 358 here)
![Page 21: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/21.jpg)
Python
Full-featured language with C-like syntax used in Civilization IV
Pluses Large user base Good integration with C/C++ Re-entrancy primitives
Minuses Slow Interpreter is relatively large
![Page 22: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/22.jpg)
Lua
Relatively stripped-down C-like language used in many games
Pluses Excellent integration with C/C++ Very fast Small library
Minuses Weak debugging support No re-entrancy support
![Page 23: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/23.jpg)
Creating your own
Many developers create their own scripting languages
Pluses Can be tailored specifically to project needs Can be extended as necessary
Minuses Substantial effort No third-party user base / contributors / tools
(Insert ad for CSC 347, 348 here)
![Page 24: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/24.jpg)
Lua
Buckland uses Lua for the Raven game mostly to replace his "params.ini" files eliminates the need for a separate parsing of
parameter files How
simply define variables in a file load the file into a lua "state" then use accessors to get the values
![Page 25: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/25.jpg)
Before
params.ini fileTeamName = "MyTeam"
codeentire param loader class andGetNextTokenAsString(); // Skip RedTeam Label
RedTeamId = GetNextTokenAsString();
GetNextTokenAsString(); // Skip BlueTeam Label
BlueTeamId = GetNextTokenAsString();
![Page 26: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/26.jpg)
After
params.lua fileBot_MaxHealth = 100Bot_MaxSpeed = 1Bot_Mass = 1
in codescript->GetInt("Bot_Mass")plus minimal interface code importing
the lua libraries
![Page 27: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/27.jpg)
Result
Much more flexible systemparameters no longer have to be in
ordersimple calculations can be embedded
in the lua script
![Page 28: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/28.jpg)
How does it work
Two issuespassing data
• Lua -> C++• C++ -> Lua
invoking functions• Lua -> C++• C++ -> Lua
![Page 29: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/29.jpg)
Basic concept
Stack Lua and C++ share the "Lua stack"
In each case calling environment puts information on the stack transfers control called environment reads information from the stack
• does something then puts an answer back on the stack transfers control back calling environment reads answer from the stack
![Page 30: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/30.jpg)
Data
Lua -> C++ Assume
we have created a Lua state• pL points to it
we have loaded our parameters file C++ code
lua_settop(pL, 0); // reset the stacklua_getglobal(pL, "Bot_Mass");if (lua_isnumber(pL, 1))
double mass = (double)lua_tonumber(pL, 1);...
![Page 31: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/31.jpg)
Function calls
Lua -> C++ Idea
push some values on the stack then call the function get the result from the stack
Examplelua_getglobal(pL, "updatePosition"); // the function
we're callinglua_pushnumber(pL, posX); // some parameterslua_pushnumber(pL, posY);lua_call(pL, 2, 0); // the call itself
![Page 32: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/32.jpg)
Function calls
C++ -> Lua Idea
Lua requires a particular function signature• function(lua_state*)
Create a wrapper with this signature• it grabs the data from the state• then calls the relevant function
Register this function in the Lua environment Call as normal
Details in the book somewhat ugly and repetitive for lots of functions
![Page 33: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/33.jpg)
Luabind
To the rescue open source project version 0.7 now available in public beta
A set of facilities for automatically generating wrappers
Works with C++ classes Allows class-like object inside of Lua as well
![Page 34: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/34.jpg)
Example
Finite state machinemachine itself written in C++states coded in Lua
![Page 35: Scripting and Actions Robin Burke GAM 376 Fall 2006](https://reader036.vdocument.in/reader036/viewer/2022062517/56649ec05503460f94bcc59c/html5/thumbnails/35.jpg)
Wednesday
Soccer Tournament Must have working code by 5 pm
TuesdayI will compile and test the teamsYour code must be self-contained in a
uniquely-named folder inside the SoccerLab folder
You can bring updated code to classmeet in the lab