DarkAI

CONTAINER DEMO


This demo shows the use of containers to represent two floors, each occupying the same space but completely separated from each other inside the AI system. A player controlled object is created which is free to move between the containers by means of a ramp and can only be seen by the occupants of the container it is currently in. Obstacles are also created for a ground floor, which prevent the movement of ground floor entities but do not hinder the upper floor entities. Entities will not display any attacks in this demo but will move towards targets as if they were going to attack. This is to keep the focus on containers and using them to separate entities and floors.

The demo begins with the following setup code.

AI Start
AI Set Radius 2.5
AI Add Container 1


This starts the AI system and sets the radius as with most demos, and also creates a new container with id 1. The ground floor is represented using the default container 0, which is created automatically in AI Start, and the new container 1 is added to represent the upper floor. The container can be given any positive id, but it must not already exist. Obstacles are added as in the Path Finding demo except for the one boundary on the upper floor.

AI End New Obstacle 1,1

Which specifies a container number of 1 for the upper floor. By default obstacles are added to container 0 so we only need to change the container number parameter when we want to assign things to other containers.

for i=2 to 4

make object i,1,0
position object i,rnd(80)-40,2.5,rnd(80)-40
color object i,rgb(255,0,0)
AI Add Enemy i,1,0

next i

for i=10 to 12

make object i,1,0
position object i,rnd(80)-40,9,rnd(80)-40
color object i,rgb(255,0,0)
AI Add Enemy i,1,1

next i

This creates two sets of entities, the first for the ground floor and container 0, the second for the upper floor and container 1. The entity parameters are left as default since this demo focuses on containers and the separation of the floors. The default values will allow the entities to see all around themselves at a reasonable distance, in this case about half the level.

In the main loop we need to detect when the user moves the player over the ramp in order to control both its height and which container it is currently in.

if object position x(1)>40

position object 1,object position x(1),(object position z(1)+50)*(6.5/100.0)+2.5,object position z(1)
if object position y(1)>5.75 then playerLevel = 1 else playerLevel = 0

else

position object 1,object position x(1),playerLevel*6.5 + 2.5,object position z(1)

endif

The ramp is on the right side of the screen starting at X = 40 and ending at X = 50, and runs in the Z direction from -50 to 50. So if the player has an X co-ordinate greater than 40 it is on the ramp and we need to adjust its height relative to how far along the ramp in the Z direction it is. This is done by (PosZ+50)*(6.5/100.0)+2.5, which takes the Z position between -50 to 50, converts it into a value between 0 and 100, then divides by 100 to get it into the range 0.0 to 1.0. This represents a value of 0.0 when the entity is at the bottom of the ramp, and 1.0 when the entity is at the top of the ramp. Since the height difference between the floors is 6.5 we multiply this 0.0 to 1.0 value by 6.5 so that when at the top of the ramp the entity has moved up a whole floor height. Finally 2.5, the radius of the entity, is added to bring the entity up out of the floor to sit on top of it.

Alternatively this can be handled by a separate collision system that keeps the player and/or entities on the floor whilst they move about in their X and Z directions. The AI system does not mind objects being moved about in DarkBasic in the Y direction since from a top down view it makes no difference. You can also move the entity's object in the X and Z directions and the AI system will register the move but you may hinder entities from moving where they are trying to get to.

To check which floor the player is on we look at its object's Y position, if it is greater than the half-way point between the floors we set it as on floor 1, otherwise we set it as being on floor 0, we'll use this value next to set the player container. If the player is not on the ramp then its Y position is set using the playerLevel variable and multiplying it by the height difference between the floors.

AI Set Player Container playerLevel

Here we tell the AI system which container the player is in, 1 for the top floor, 0 for the ground floor. This cannot be calculated automatically since no height information is stored about the containers so the AI system would not be able to tell the difference between container 1 and container 0. You can continuously set the player container to the same value every frame as it simply sets a value in the AI system and does not do any calculation.

if showBounds=1

if currentShowBounds<>playerLevel

AI Debug Hide Obstacle Bounds currentShowBounds
AI Debug Show Obstacle Bounds playerLevel,playerLevel*6.5 + 2.5
currentShowBounds = playerLevel

endif

else

AI Debug Hide Obstacle Bounds 0
AI Debug Hide Obstacle Bounds 1

endif

This is an addition to the debug control that displays obstacle edges to detect when the player changes floors whilst the debug object is visible. If this happens it switches between showing the obstacles bounds for floor 0 or floor 1 and at the height of the floor they represent. This is done for clarity to only show the bounds for one floor at one time although you could show both at the same if this was desired.

AI Update

Finally the Update command handles all the details of moving and controlling entities in all containers. Entities will only register that they can see the player when the player is assigned to the same container as them. When the player leaves the container the entity will behave as if it lost sight of the player, and in some cases search for it. Although the entity cannot see it again until it re-enters the entity's container.