DarkAI

TEAMS DEMO


This demo combines several features with the built in teams to create a simple fight scene. It includes obstacle avoidance from the Path Finding demo, patrol paths from the Patrolling demo, the visual attack and sounds from the Sound demo and introduces friendly and neutral entities. Friendly entities support the player by attacking nearby enemies and following the player when ordered to. Neutral entities do not engage in combat and by default run away from sounds, their exact behaviour can be set use the Set Stance command or controlled manually as with other entities, but they will never pick targets and always return Can Fire as 0. Neutral entities will also not be shot at by other entities.

This document will not cover obstacle setup or patrolling since these are covered in detail in the Path Finding and Patrolling demos respectively. The visual attack is also the same as that found in the Zone or Sound demos the only difference being this demo deals with more entities. It does not matter which team the entity is on or who they are firing at since this method of visualising the attack is handled the same for all entities and targets.

First we will cover the creation and addition of the enemy entities.

for i = 2 to 4

make object i,1,0
position object i,i*5 – 30,2.5,40
color object i,rgb(255,0,0)

make object i+1000,2,1
set object light i+1000,0
hide object i+1000

AI Add Enemy i
AI Set Entity Speed i,10.0
AI Set Entity Aggressive i
AI Set Entity View Arc i,90,180
AI Set Entity View Range i,60
AI Set Entity Hearing Range i,100

next i

AI Entity Assign Patrol Path 2,1
AI Entity Assign Patrol Path 3,1
AI Entity Assign Patrol Path 4,1


The entity objects are created from a mesh and positioned where we want them to start along the top of the level. We create the attack object, one per entity, and then set the initial entity parameters. We use the Set Aggressive command to make the enemies approach and follow their targets so that they will continue to attack until either, one of them dies (not featured in this demo), a closer target is spotted, or it loses sight of all targets and cannot find them. We give the enemies a generous hearing range so they can hear and respond to any entity attacking on the level. Entities are also given a view angle of 180 so they cannot see behind themselves, and a view range of 60 so they cannot immediately spot the player and its allies on the other side of the level. The entities are then all assigned the same patrol route, to move back and forth across the top of the level.

Next we create and add the neutral entities.

for i = 11 to 13

make object i,1,0
position object i,rnd(50)-25,2.5,rnd(50)-25

AI Add Neutral i
AI Set Entity Speed i,10.0
AI Set Entity View Arc i,90,180
AI Set Entity Hearing Range i,100

next i

Since neutral entities cannot attack we do not give them an attack object, and we position them randomly around the level. Neutral entities will run away by default, and since we don't want to change this behaviour we will just set the range at which the entity will be able to hear and see. You could also use the Can Roam command here if you wanted neutral players to randomly move about instead of remaining fixed until an event occurs.

Finally we create and add the friendly entities.

for i = 21 to 22

make object i,1,0
position object i,i*5 – 120,2.5,-40
color object i,rgb(0,255,0)

make object i+1000,2,2
set object light i+1000,0
hide object i+1000

AI Add Friendly i
AI Set Entity Speed i,10.0
AI Set Entity View Arc i,90,180
AI Set Entity View Range i,60
AI Set Entity Hearing Range i,80

next i

There is little difference here to the enemy setup, except instead of calling Add Enemy we use Add Friendly to specify the team. It does not matter in which order the different teams are created, or which id's they are given as all entities are processed the same way. Friendly entities default to a cautious stance which means they will not approach and follow targets, if you wish this can be changed by setting the entity stance.

make object sphere 1,5
position object 1,20,2.5,-40
AI Add Player 1


This creates the player object and adds it to the AI system.
This is all we need to do for the player since the AI system will now read the player object's position automatically every frame and update its internal values since the default values link the player to the object.

Once the setup is complete the AI system now has enough information to move and control entities based on their team and current surroundings to produce a believable responses completely automatically by calling AI Update. The main loop provides a 'follow player' command for the friendly team to provide some control over the movement of allies.

if keystate(33)=1 and ftimer<timer()

ftimer = timer()+300
AI Team Follow Player 20

endif

if keystate(31)=1 and stimer<timer()

stimer = timer()+300
AI Team Separate

endif

The Follow Player command takes a distance as its parameter, specifying the maximum distance friendly entities should remain from the player. When the distance between the player and the entity exceeds this value the entity will move towards the player, otherwise it will be content with its current location. The Separate command tells all friendly entities to stop following the player and return to worrying about themselves.