Scripting and Actions Robin Burke GAM 376 Fall 2006.

35
Scripting and Actions Robin Burke GAM 376 Fall 2006

Transcript of Scripting and Actions Robin Burke GAM 376 Fall 2006.

Page 1: Scripting and Actions Robin Burke GAM 376 Fall 2006.

Scripting and Actions

Robin Burke

GAM 376

Fall 2006

Page 2: Scripting and Actions Robin Burke GAM 376 Fall 2006.

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.

Midterm solutions: Q1

DFS BFS ID

1

65

432

Page 4: Scripting and Actions Robin Burke GAM 376 Fall 2006.

Q2

Arrive Alignment Evade Obstacle Avoidance

Page 5: Scripting and Actions Robin Burke GAM 376 Fall 2006.

Q3

States Conditions Graph

Page 6: Scripting and Actions Robin Burke GAM 376 Fall 2006.

Q4

Preferred velocity Leading distance Following distance

Page 7: Scripting and Actions Robin Burke GAM 376 Fall 2006.

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.

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.

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.

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.

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.

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.

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.

Issues

Speed Execution model Integration Re-Entrancy

Page 15: Scripting and Actions Robin Burke GAM 376 Fall 2006.

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.

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.

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.

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.

Choices

Write your ownmore later

Lua Python Scheme

Page 20: Scripting and Actions Robin Burke GAM 376 Fall 2006.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Example

Finite state machinemachine itself written in C++states coded in Lua

Page 35: Scripting and Actions Robin Burke GAM 376 Fall 2006.

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