
From: Ismael Orenstein <perdig@linuxbr.com.br>
Subject: [xrally] AI stuff
Date: Tue, 21 Mar 2000 17:37:39 -0300
To: XRally List <xrally@middleearth.telefragged.com>

Hello all!

As with the new 0.9 version the game engine is almost complete, I think it
is now time to improve some parts of the game that were left behind at the
start.

And the most important of them is, in my opinion, the AI. So I wrote a small
text explaining how the AI currently works and how it is organized. Those
interested in changing it's code should have now a better base to work on
(although this is not imutable! You can change everything I made if you think
of a better way of implementing it). Those who don't want to write AI code, at
least will know how the AI thinks and it will be a lot easier to beat the game!
:)

-> How the AI works (the pratical guide)

Currently all the AI is based on two definitions in the ai.h file:

ONE_SEEK
ENEMY_STOP

Each game loop the game searchs through all the enemy cars, and see who is
nearer the player. It discovers it in a very simple way: 

distance = |Ai.X - Player.X| + |Ai.Y - Player.Y|

As you can see, it doesn't pay attention if there is any kind of wall between
the player and the AI car. It only see who has the lower absolute distance to
the player.

If ONE_SEEK is defined, then the nearest enemy car moves towards the player
car. If it is not, all the enemy cars move towards the player.

About the "move towards" stuff... it see which is the bigger distance... to
move in a vertical or horizontal direction, and them go to the right way (up or
down, left or right). If ENEMY_STOP is defined, when there is a wall the enemy
just stops. If it isn't, the car uses the carAI() function, which makes it
automatically choose a free direction (the same way the player car does)

-> How the AI works (technical guide)

The Ai is a struct of the following variables:

        carStruct *bot;
        int number;
        int *order;
        int *data;   

This is going to be changed to support a linked list, but the basic idea should
remain. The *bot variable is the array of the enemy cars (holding their
position, speed, etc). The 'number' variable is the number of enemy cars.

The two more important variables there are *order and *data. Both are a array
of 'number' elements that hold what the AI will do next. The *order one carry
if the enemy car should chase the player, should be confused, should wander
around, etc. The *data one carry some arguments to the first (How many seconds
to be confused, what direction to go, etc)

Each game loop, the orderAI() function searchs through all the enemy cars and
order each one to do something (as explained earlier). Then, the moveAI() call
just make everyone executes his orders. There also some auxiliary calls, goTo()
and getDistance(), that help the AI to see how to get to the player and how far
he is (these are the ones I explained above).

So, if you want to implement a new order to the AI, there are 3 things you must
do:

- Define it in the ai.h file
- Edit the orderAI() call so that the enemy know what to do
- Edit the moveAI() call so that he knows how to do that

If you just want to change the way that the orders are currently being done,
just edit the orderAI(), moveAI() or a auxiliary call. For a example, if you
want to improve the AI by a great part, you could change the goTo() and
getDistance() calls and add a pathfinding algorithm to them... this way, the AI
would know who is really nearer the player and how to get there in the shortest
way.

Well, I think that is enogh... any idea, suggest, doubt or flame, just reply to
this message!

[]'s
-- 
Ismael Orenstein (UIN 1293061)
perdig@linuxbr.com.br
http://www.esquadro.com.br/~ismore
http://www.linuxgames.com/xrally


-------------------------------------------------------
 This list provided by LinuxGames (www.linuxgames.com)
 and TeleFragged (www.telefragged.com)
-------------------------------------------------------


