Game Development Reference
In-Depth Information
running all the way across the zone. It would be better to have the Armor on for
those 8 seconds than to not have it.
Of course, if we arranged the scenario so that our starting health was at or
below 25, we would not be able to make it to the Armor and all the way back to the
Health Kit before we died. In that case, every health point would matter signifi-
cantly until we increased it. Therefore, it would be better to get the Health Kit first.
But we shall explore those sorts of factors in the next chapter.
P UTTING I TIN C ODE
As we discussed above, the utility function we used multiple times in this example
is simply one of time and rate (of damage). Expressed in code, this function is as
simple as it sounds.
int MyGame::Damage( int Time, int Rate )
{
return ( Time * Rate );
}
The rest of the decision is based on how and when we use the utility function.
By using it multiple times with the relevant data, we can construct a total amount
of damage taken. All of this action is performed in the function GetBestAction() .
In this function, we have put a slightly different spin on how we arrive at the re-
sult. We start with our current health ( mMyHealth ) and then reduce that health
value by the damage that is taken on each leg of the trip. The result is stored in
HealthFirst or ArmorFirst , respectively. The reason we do this becomes clear at
the bottom of the function. If our starting health is greater than both of the results,
then it is clearly in our best interests to not leave the cover spot at all! (Note that I
did not check for the possibility of running out of health along the way. That logic
is not the point of the example and will actually be handled in a far more interest-
ing way in the next chapter.)
Once again, for visual clarity in returning a value from the function
GetBestAction() , we use an enumerated type, GOAL_TYPE . This is, of course,
entirely cosmetic for purposes of this example.
typedef enum {
GOAL_NONE, // Do not get either
GOAL_HEALTH_ARMOR, // Get Health Kit first
GOAL_ARMOR_HEALTH // Get Armor first
Search WWH ::




Custom Search