PATROLLING DEMO
This
demo shows the use of paths to create a patrol route for one or more
entities, in this case two paths are created and two entities are
assigned to each path. The two paths are created with the same points
but in opposite directions, making entities patrol both clockwise and
anti-clockwise around the level. This also demonstrates entities
avoiding each other when they meet and how they cope with overcrowded
destinations.
This document will describe the main AI commands
used to create and control this demo in the context they are used.
This demo starts very similar to the Path Finding demo so obstacle
creation will not be covered here. It is advised you familiarise
yourself with the path finding demo first to see how obstacles are
setup.
AI Make Path 1
AI Path Add Point 1,40,40
AI
Path Add Point 1,40,-40
AI Path Add Point 1,-40,-40
AI Path Add
Point 1,-40,40
AI Make Path 2
AI Path Add Point 2,-40,40
AI
Path Add Point 2,-40,-40
AI Path Add Point 2,40,-40
AI Path Add
Point 2,40,40
After the
obstacle setup we come to creating the two paths we want to use. The
first is the clockwise path (path 1), starting in the top right hand
corner, and the second (path 2) is the anti-clockwise path starting
in the top left hand corner. We will refer to these later when we
assign them to the entities.
for i = 2 to 5
make
object cone i,5
xrotate object i,90
fix object pivot i
position
object i,i*5 – 30,2.5,40
AI Add Enemy i,1
AI Set
Entity Speed i,10.0
next i
AI Entity Assign Patrol Path
2,2
AI Entity Assign Patrol Path 3,2
AI Entity Assign Patrol
Path 4,1
AI Entity Assign Patrol Path 5,1
This
creates 4 entities (id's 2-5 inclusive) and sets their speed. The
second parameter when adding the entities is to set link the entity
to its object, this would be set by default anyway but shows the
usage of the second parameter. We then get to assign the entities
their patrol paths, two to the clockwise path, two to the
anti-clockwise path and immediately changes the behaviour of the
entity to patrol whenever it would normally go into an idle state.
This allows it to perform all the normal actions of an entity such as
attacking and moving but be able to return to the patrol when it has
nothing else to do. Any changes to the path, for example adding extra
points, will take an immediate effect to any entity using it as a
path. To remove the patrol state from an entity and return its idle
state assign the entity a path with no points, or delete the path
that is assigned to the entity.
AI Update
sync
We
then call these two commands in the main loop and the entity movement
is automatically handled for us. Entities will start at the beginning
of their patrol paths and patrol forever, starting at the beginning
again when they reach the end.