Game Development Reference
In-Depth Information
How it works...
At the beginning of the game, we add one green food resource, and one brown wood re-
source, some distance away from the HQ (at 0,0,0). The
AIAppState
starts by looking at
the current food storage, seeing it's low, it will assign an AI to go to the food resource and
bring back food.
The
AIAppState
evaluate method starts by establishing the need for food gathering. It
does this by dividing the food stores by the current requirement. By setting the food in the
algorithm to not be able to exceed the requirement, we make sure we get a figure between
0.0 and 1.0.
It then takes the amount of units available, and decides how many of those should be gath-
ering food, based on the
factorFood
figure, rounding it off to the nearest integer.
The result is compared to how many are currently on a food gathering mission, and adjusts
the number to suit the current need, assigning them to either food or wood gathering.
The worker AI is completely controlled by the state they're set to by the manager, and in
this recipe, all they can do is move to one resource or the other. They have no idle state,
and are expected to always have some task.
The two states we use in the recipe are actually the same class. Both resources are gathered
in the same way, and
GatherFoodState
and
GatherWoodState
are only used as
identifiers. In a real game, they might well behave differently from each other. If not, it
might be a good idea to use a parameterized version of
GatherResourceState
in-
stead.