Parberry: Chapter 8 AI - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Parberry: Chapter 8 AI

Description:

randomness makes crows less predictable, more chaotic. the crows react to the plane change speed & height to escape plane, drop ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 34
Provided by: brianm46
Category:

less

Transcript and Presenter's Notes

Title: Parberry: Chapter 8 AI


1
Parberry Chapter 8AI
  • Brian Malloy

2
Whats AI
  • Building a piece of hardware or writing a
    computer software that allows the machine to
    think or process information in a fashion similar
    to humans!
  • AI has been slow to develop
  • It has come on fast recently
  • neural nets
  • genetic algorithms

LaMothe
3
Future Gaming
  • 3D Graphics has peaked, LaMothe
  • the future in game programming is to construct
    creatures that not only lookreal, but
  • act real
  • are cunning and devious
  • are really smart
  • AI related technologies will have the same
    impact on gaming as the Doom graphics

4
Whats the right way?
  • There aint no right way!
  • need to create a computer jet that can kick
    butt
  • Regardless of how primitive the AI is, the player
    will always imagine more detail and project
    personalities of their own onto the virtual
    opponents

5
LaMothes book
  • Four AI techniques
  • deterministic algorithms predetermined
    behaviors, random or otherwise
  • patterns scripts actions determined by inputs
  • state machines behaviors based on conditions
    and results of game play
  • neural networks models of computation based on
    biological brain functions

6
Parberry Whats new in Ch 8?
  • Rule-based system augmented with randomness
  • randomness makes crows less predictable, more
    chaotic
  • the crows react to the plane change speed
    height to escape plane, drop behind the plane if
    they can
  • The player can shoot spacebar
  • when hit, crows explode into falling carcasses
  • exploding crow different from falling crow

7
Whats new
  • plane crow are animated by playing frame
    sequence forward backward
  • exploding crow animation played only once
  • bullets falling crows have single frame of
    animation fixed lifetime
  • if speed of plane is minimum, crows form two
    flocks one ahead, one behind

8
Intelligent objects
  • Some objects are intelligent, some are not
  • Intelligence costs time storage
  • compute actions
  • store state
  • Many AI designs Parberry uses rule-based
  • the rules
  • determine which state the object is in
  • what action to take, given the state
  • Advantage easy to program, efficient
  • drawback can lead to predictable behavior

9
CViewPoint
System Overview
Main

CObjectManager
Cobjcect m_pObjectList
1
1
g_pSpriteNUM_SPRITES
1
CBaseSprite
CObject
CIntelligentObject
CClippedSprite
10
View.h
Main
Objman.h
Why not make file name same as class name? The os
is dictating nomenclature!!
Objects.h
Bsprite.h
Ai.h
Csprite.h
11
Overview of CIntelligentObject
  • enumeration for the states of the object
  • enum StateType CRUISING_STATE,
    AVOIDING_STATE
  • Record objects current state
  • StateType m_eState
  • Variables for height changes
  • int m_nDesiredHeight
  • int m_nHeightTime (last time changed height)
  • int m_nHeightDelayTime (time to next height
    change)

12
CIntelligentObject (cont)
  • Intelligent objects are not intelligent all of
    the time they zone out!
  • int m_nLastAiTime (last time AI used)
  • int m_nAiDelayTime (time until next AI)
  • Distances from Plane Object (in pixels)
  • int m_nDistance (distance from plane)
  • int m_nVerticalDistance
  • int m_nHorizontalDistance

13
CIntelligentObject (cont)
  • member function ai()
  • main ai function
  • has a switch statement that tests objects current
    state, which determines next action

void CIntelligentObjectai()
if(Timer.elapsed(m_nLastAiTime,m_nAiDelayTime))
switch(m_eState) //behavior depends on
state case CRUISING_STATE cruising_ai()
break case AVOIDING_STATE avoiding_ai()
break default break
14
enum StateTypeCRUISING_STATE,AVOIDING_STATE cla
ss CIntelligentObject public CObject
private StateType m_eState //state int
m_nDesiredHeight //desired altitude int
m_nHeightTime //time between height changes
int m_nHeightDelayTime //time to next height
change int m_nSpeedVariationTime //last time
speed varied int m_nSpeedVariationDuration
//time to next speed vrn int m_nLastAiTime
//last time AI was used int m_nAiDelayTime
//time until AI next used int m_nDistance
//distance to plane int m_nVerticalDistance
//vertical distance from plane int
m_nHorizontalDistance //hor. distance from
plane void ai() //artificial intelligence
void cruising_ai() //ai for cruising along
void avoiding_ai() //ai for avoiding plane
void set_state(StateType state) //change state

15
CIntelligentObject (cont)
enum StateTypeCRUISING_STATE,AVOIDING_STATE cla
ss CIntelligentObject public CObject
private public CIntelligentObject(O
bjectType object,int x,int y,
int xspeed,int yspeed) virtual
void move() //move depending on time and speed
void plane(int x,int y,int d) //relationship
w/plane
class CObjectManager private CObject
m_pObjectList //list of objects in game
16
How is move called?
Main calls ObjectManager to animate, and animate
moves each of the objects!
17
Ai.cpp
include "ai.h" include "timer.h" //game
timer include "random.h" //for random number
generator include "view.h" //for viewpoint
manager extern CTimer Timer //game
timer extern CRandom Random //random number
generator const int CLOSE_DISTANCE200 //close
to plane const int FAR_DISTANCE300 //far from
plane //fall back at this vertical distance from
plane const int FALLBACK_DISTANCE150
//horizontal distance considered to be behind
plane const int BEHIND_DISTANCE-5
18
CIntelligentObjectCIntelligentObject(ObjectType
object, int x,int y,int xspeed,int yspeed)
CObject(object,x,y,xspeed,
yspeed) m_bIntelligentTRUE
m_nDesiredHeight240 m_nHeightTime0
m_nHeightDelayTime0 m_nSpeedVariationTimem_nS
peedVariationDuration0 m_nDistancem_nHorizont
alDistancem_nVerticalDistance0
m_eStateCRUISING_STATE m_nLastAiTime0
m_nAiDelayTime0 void CIntelligentObjectmove
() //move object CObjectmove() //move like
a dumb object ai() //act intelligently
19
void CObjectmove() //move object const int
XSCALE16 //to scale back horizontal motion
const int YSCALE32 //to scale back vertical
motion const int YMARGIN20 //vertical
margin int xdelta,ydelta //change in
position int timeTimer.time() //current time
int tfactor //time since last move
switch(m_eLocomotion) case FLYING_MOTION
break case FALLING_MOTION
default break
20
switch(m_eLocomotion) //this switch in
CObjectmove() case FLYING_MOTION
//horizontal motion tfactortime-m_nLastXMov
eTime //time since last move
xdelta(m_nXspeedtfactor)/XSCALE //x distance
moved m_nXxdelta //x motion
Viewpoint.normalize(m_nX) //normalize to world
width if(xdeltam_nXspeed0) //record
time of move m_nLastXMoveTimetime
//vertical motion tfactortime-m_nLastYMoveT
ime //time since last move
ydelta(m_nYspeedtfactor)/YSCALE //y distance
moved m_nYydelta //y motion
if(m_nYltYMARGIN)m_nYYMARGIN
if(m_nYgtSCREEN_HEIGHT)m_nYSCREEN_HEIGHT-1
if(ydeltam_nYspeed0) //record time of move
m_nLastYMoveTimetime break
21
void CIntelligentObjectcruising_ai() //just
cruising along //height variation
if(m_nDesiredHeightltm_nY-20)m_nYspeed-1 else
if(m_nDesiredHeightgtm_nY20)m_nYspeed1 else
m_nYspeed0 if(Timer.elapsed(m_nHeightTime,m_nH
eightDelayTime)) m_nDesiredHeightRandom.numb
er(150,400) m_nHeightDelayTime15000Random.n
umber(0,5000) //look for plane
if(m_nDistanceltCLOSE_DISTANCE
m_nHorizontalDistanceltBEHIND_DISTANCE)
set_state(AVOIDING_STATE)
22
void CIntelligentObjectavoiding_ai()
//avoiding plane //height variation
if(Timer.elapsed(m_nHeightTime,m_nHeightDelayTime)
) m_nDesiredHeightRandom.number(100,450)
if(m_nDesiredHeightltm_nY)m_nYspeed-2
if(m_nDesiredHeightgtm_nY)m_nYspeed2
m_nHeightDelayTime3000Random.number(0,2000)
and lots more see code!!
23
void CIntelligentObjectset_state(StateType
state) m_eStatestate //change state
switch(m_eState) //change behavior settings
case CRUISING_STATE m_nAiDelayTime300Rand
om.number(0,300) m_nXspeed-1
m_nHeightDelayTime8000Random.number(0,5000)
break case AVOIDING_STATE
m_nAiDelayTime200Random.number(0,200)
m_nXspeed-3 m_nDesiredHeightRandom.number
(100,400) m_nHeightDelayTime3000Random.nu
mber(0,2000) m_nSpeedVariationDuration5000
Random.number(0,2000) break default
break
24
Collision Detection andthe Object Manager
  • The magic number 15!

25
class CObjectManager private CObject
m_pObjectList //list of objects in game
int m_nCount //how many objects in list int
m_nMaxCount //maximum number of objects int
m_nCurrentObject //index of the current object
int m_nLastGunFireTime //time gun was last
fired //distance functions int
distance(int x1,int y1,int x2,int y2) //in
universe int distance(int first,int second)
//between objects //collision detection
void collision_detection() //all collisions
void collision_detection(int index) //with this
object //managing dead objects void
cull() //cull dead objects void kill(int
index) //remove object from list void
replace(int index) //replace by next in
series .continued on next page
26
public CObjectManager(int max)
//constructor CObjectManager()
//destructor int create(ObjectType object,int
x,int y, int xspeed,int yspeed) //create
new object void animate(LPDIRECTDRAWSURFACE
surface) //the following functions operate
on the current object void accelerate(int
xdelta,int ydelta0) //change speed void
set_current(int index) //set current object
int speed() //return magnitude of speed void
fire_gun() //fire the gun
27
Constructor
CObjectManagerCObjectManager(int max)
m_nMaxCountmax m_nCount0
m_nCurrentObject0 m_nLastGunFireTime0
m_pObjectListnew CObjectmax //object list
for(int i0 iltm_nMaxCount i) //create
objects m_pObjectListiNULL
28
Destructor
CObjectManagerCObjectManager() //destructor
for(int i0 iltm_nMaxCount i) //for each
object delete m_pObjectListi //delete it
deletem_pObjectList //delete object list
29
Creation of objects
int CObjectManagercreate(ObjectType object,int
x,int y, int
xspeed,int yspeed) if(m_nCountltm_nMaxCount)
//if room, create object int i0 //index
into object list while(m_pObjectListi!NULL)
i //find first free slot
if(objectCROW_OBJECT) //intelligent object
m_pObjectListi new CIntelligentObject(
object,x,y,xspeed,yspeed) else //dumb
object m_pObjectListinew
CObject(object,x,y,xspeed,yspeed)
m_nCount return i else return -1
30
void CObjectManageranimate(LPDIRECTDRAWSURFACE
surface) //move objects for(int i0
iltm_nMaxCount i) //for each object slot
if(m_pObjectListi!NULL) //if there's an
object there m_pObjectListi-gtmove()
//move it if(m_pObjectListi-gtm_bIntelligen
t) //if intelligent //tell object about
plane current position ((CIntelligentObjec
t)m_pObjectListi)-gtplane(
m_pObjectListm_nCurrentObject-gtm_nX,
m_pObjectListm_nCurrentObject-gtm_nY,
distance(i,m_nCurrentObject)) //move
viewpoint with plane Viewpoint.set_position(
m_pObjectListm_nCurrentObject-gtm_nX)
collision_detection() cull() for(i0
iltm_nMaxCount i) //for each object slot
if(m_pObjectListi!NULL) //if there's an object
there m_pObjectListi-gtdraw(surface)
//draw it
31
void CObjectManagercull() //cull old objects
CObject object for(int i0 iltm_nMaxCount
i) //for each object objectm_pObjectListi
//current object if(object!NULL)
//died of old age if(object-gtm_eMortalityM
ORTAL //if mortal and ... //...lived long
enough... (Timer.time()-object-gtm_nBirthTime
gtobject-gtm_nLifeTime)) kill(i) //...then
kill it else //one-shot animation
//if object played one time only...
if(object-gtm_eMortalityONESHOT_MORTAL
//...and played once already...
object-gtm_nCurrentFramegtobject-gtm_nFrameCount)
replace(i) //...then replace the object

32
Exhaustively search!
void CObjectManagercollision_detection() //chec
k for all collisions for(int i0
iltm_nMaxCount i) //for each object slot
if(m_pObjectListi!NULL) //if object exists
if(m_pObjectListi-gtm_eObjectBULLET_OBJECT)
collision_detection(i) //check for
collisions
What's the running time?
33
void CObjectManagercollision_detection(int
index) //check whether object with this index
collides int i0 //index of object collided
with BOOL finishedFALSE //finished when
collision detected while(iltm_nMaxCount!finishe
d) if(m_pObjectListi!NULL) //if i is a
valid object index if(m_pObjectListi-gtm_bV
ulnerable //if vulnerable
distance(index,i)lt15) //and close enough, then
finishedTRUE //hit found, so exit loop
replace(i) //replace object that is hit
kill(index) //kill object doing the hitting
i //next object
That magic number
Write a Comment
User Comments (0)
About PowerShow.com