Comp2132 3D Modelling and Simulation 2010-11.

militaryzoologistΤεχνίτη Νοημοσύνη και Ρομποτική

1 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

174 εμφανίσεις

Comp2132 3D Modelling and Simulation 2010
-
11.

Worksheet Scripting 4
a (UT2004).

Purpose


Here we are going to code

Cellular Automata (CA)

to represent the behaviour of

fire spreading through a forest. CA
s

model space by dividing it into a number of
cells

(think of a patch of space, could be a square metre) and within each cell there is a
computation, here we use a
finite state machine
. The computation within each cell is
based on the states of
four neighbouring cells

(North, South, East and West). In this

worksheet you will


(a) L
earn some
more
basic Unreal code, (b)
Practice

how to
code a Finite State
Machine (FSM)
(c)
Observe how a Cellular Automaton works.


Don’t worry we’re not going to write the entire code! We shall concentrate on the
FSM aspect.

Fil
es Required


Level
: ‘
aCBP14_
FireSpreading
.ut2

from the Resources page (“Maps”)


Code:

Additional files from
CBP14 from
the Resources page
(“Scripting 4
”)


Assets:

No new assets are required.


Tasks



0


Preparation before you begin!

Check that the static
mesh package
s and the
texture packages are

installed on your machine. If not, install
them into the
correct folders (see last week).
Then, o
pen up UnrealEd, open the static me
sh
browser and open each downloaded package and s
ave it to disk. Exit U
nrealEd.
I
nstall the folder CBP1
4

in your Unreal Runtime root directory

if it is not there
.

This has grown since last week

so you should update it
.

F
ire up ‘Wotgreal’
a湤⁣潭灩汥⁴桥⁣潤e.



1


Prepare a Level.
Open up
the leve
l aCBP14_
FireSpreading
.ut2. C
heck that

the
DefaultGameType

in the
LevelProperties

box (
right click on your map)
is set to

“CBP14.CBP1
㑇4浥f湦o
A

⸠o畮u瑨攠浡瀠瑨敮t c汯獥⸠
䅬瑥A湡瑩癥ly 畳e yo畲
潷渠汥癥氮



2


Writing

Code

(1)

Remember the order of states we need
combustible
-
>
igniting
-
> bu
rning
-
>smoking
-
> burned.

The state
combustible

is provided for
you. You should write your code in the class
CBP14_CA_CellFire.uc

which is
the code for each cell of the CA.


First write the code for state
igniting
. You should get the code to sleep for 2.5

second then go to the next state.

We do not need to set the spri
teEmitter here
since there is neither

smoke nor fire (“where there’s no smoke there’s no fire”
…bit of a problem in logic). Let’s move on


Now the state burning. First you must set the sprite
E
mitter to represent fire.
This is done by coding


Emitters[0] = ??


where ?? is a variable of class CBP14_SpriteEmitter. Find which variable to
use. Hint, look in t
he

function postBeginPlay().


Now code a sleep of let’s

say 5 second
s

and
add
code to move to the next state.



3


Writing Code (2
)
Now write the state smoking,
using a similar

sequence of
actions you did for burning.

Again you must find the appropriate spriteEmitter
variable.


Finally write the state burned.

The code execution stops here, so there is no
transition to a new state. But we
must also stop the spriteEmitter, in other words
we must remove the variable assigned to

Emitters[0]
. This can be done through
the statement



Emitters.Len
gth = 0;


which is saying “set the length of the Emitters[]

array is zero”, i.e., there are no
emitters, which is what we want.



4


Compile and debug your Code
, Build your Level
.
When all is well add a
CBP14_CA_Firefield actor into your level. Its locati
on will determine the
centre of the spreading fire. Open up its properties and set the x and y inter
-
cell
distances to dx = 148 and dy = 148. Start with a modest CA so set nrCellsX to
19 and nrCellsY to 19.
Note the code expects these numbers to be odd.




5


Play the Level

and watch the fire spreading.


You may with to experiment, changing the values of dx a
nd dy and nrCellsX
and nrCellsY to change the extent and density of the fire.



6


Writing Code (3
)

The problem with the CA so far is that all the ce
lls are
executing the same code. This is called
deterministic

computation since the
results are d
etermined and there is no variation
. To make things more exciting,
w
e can introduce some variation ie

randomness. Here’s one way how to do it.


You have used t
he sleep(2.5) or sleep(5) delays in your code. The numbers 2.5
and 5 are “hard
-
coded” making the behaviour
deterministic
. So we could
replace these numbers with a “random range” like this



sleep(randrange(4.0,6.0)
)
;


which means
each individual c
ell

will
sleep betwe
en

4 and 6 seconds.

Modify
your calls to
sleep()

as above and observe the change in behaviour of the
simulation.








Reflections

and Extensions
.


You should reflect on the tasks and on the code, and try to answer the following
(
per
haps
rather vague) questions in your own words.
Some ex
tra activities may be
suggested.

Feel free to collaborate!

Write

your work in your Journal.



1.

The current code starts off with a single cell in the
burning

state. This is set in
the
auto state start

state code of actor CBP14_CA_FireField. Modify this to
start off with several burning cells. You could comment out line 85 and enclose
line 86 in a for loop (lets say 4 or 5 iterations) setting the variable
seedcellIndex to some random number between 1 a
nd nrCellsX*nrCellsY for
example.



2.


Investigate the effects of a very long time for the smoke state.