Game Development Reference
In-Depth Information
This function will return a new time that is within one hour in either direction of the
desired time. If you set your start time for 6 p.m. (18.0), then your lights will come
on sometime between 5 p.m. and 7 p.m. This is definitely much better, but it
'
s still
not perfect. Most people don
t arrive home at a random time like this, but rather they
tend toward a specific time. We could certainly set a smaller deviation, but a better
solution would be to apply a nonlinear curve to the deviation, such as a normal dis-
tribution (also known as a Gaussian distribution, which generates a bell curve). That
would make values closer to the desired number more probable than the ones farther
away. This will make the times the light comes on or turns off a bit more believable.
'
Weighted Randoms
Weighted randoms are a close cousin to the distribution curve. While a distribution
curve is essentially an analog device, weighted randoms are more
The idea
is that for some number of possible decisions, each of those decisions is given a
weight. The weights are all added up, and a random number is generated from zero
up to the sum of all weights. This determines which action is chosen. For example,
let
digital.
s say I have a creature that can attack, cast a fire spell, or run away. I decide that
60% of the time I want this creature to attack, 30% of the time it should cast the fire
spell, and 10% of the time it should run away. I can decide what to do by generating
a single number from 0
'
s
greater than or equal to 60 and less than 90, the creature casts the fire spell. Other-
wise, the creature runs. This is a very easy way to create potentially complex
decisions.
Games have been using this technique with great success for years. In fact, the origi-
nal Dragon Warrior for the NES used this exact method for deciding what its oppo-
nents would do. Each monster had a table with a number of behaviors, and a number
was generated to choose a slot randomly. Since multiple slots could contain the same
entry, this gave the weighted random.
-
99. If the number is less than 60, the creature attacks. If it
'
Finite State Machines
A finite state machine is a construct that can exist in any number of finite states. For
example, in the previous Dragon Warrior example, each action could actually be
thought of as a state within a state machine. The creature
s state machine has some
number of states that it can possibly exist in, with each state determining a specific
behavior. A video game itself is often managed as a state machine, where the title
screen is one state, playing the game is another state, the options menu may be a
third state, and so on.
'
 
 
 
Search WWH ::




Custom Search