Game Development Reference
In-Depth Information
}
}
void TurnOffLights(void)
{
if (m_lightsOn)
{
SetLightStatus(false);
m_lightsOn = false;
}
}
};
This class is pretty simple. The update function checks to see if the time passed is
within the start and end times and turns on the lights if necessary. It also turns
them off when outside of that time zone. Since time is cyclical, the function takes
into account whether or not
the end time has wrapped around back to the
beginning.
This is a good example of hard-coded AI logic. The algorithm is 100% deterministic
and will do its job exactly as asked, but is it optimal? Probably not. If your house is
worth breaking into, the thief may case the place. If he notices that your lights are
always coming on and turning off at exactly the same times over the course of a cou-
ple of days, he can be reasonably sure that it
'
s just a timer program. How can we
make an AI that outsmarts the thief?
Randomization
The next step is randomization. The easiest implementation would be to instantiate
the LightTimer class with random start times and end times and then do it again
every 24 hours or so. This would certainly solve the problem of being deterministic,
but it falls on the exact opposite side of the spectrum. A thief casing your house will
realize something is very odd since most people do have a schedule when they are
home.
A better solution is to create a random deviation from the start and end times. This
is pretty trivial to implement and gives us what we want: a reasonable pattern with-
out the appearance of being run by a program.
To implement this, two new variables are added: m_desiredStartTime and
m_desiredEndTime . The constructor, TurnOnLights() , and TurnOffLights()
functions all need to change:
 
 
Search WWH ::




Custom Search