Go to the first, previous, next, last section, table of contents.

## Writing New AIs

You can add new types of AIs to Xconq. You would do this to add different strategies as well as to add AIs that are programmed specifically for a single game or class of games. (This is useful because the generic AI does not always understand the appropriate strategy for each game.)

You have to design the object that is the AI's "mental state". If your AI need only react to the immediate situation, then this object can be very simple, but in general you will need to design a fairly elaborate structure with a number of substructures. Since there may be several AIs in a single game, you should be careful about using globals, and since Xconq games may often run for a long time, you should be careful not to consume memory recklessly.

- Name. This is a string, such as `"mplayer"`. It may be displayed to players, so it should not be too cryptic.
- Validity function. This runs after modules are loaded, and during player/side setup, and decides whether it can be in the given game on the given side. [have a chain of fallback AIs, or blow off the game?]
- Game init function. This runs before displays are set up, just in case a display needs to examine the AI's data (for instance to display whether the AI is friendly or unfriendly). The game init function should also look for and interpret the contents of the side's `aidata`, if appropriate. The generic game reading code fills the slot, but does not interpret the data further.
- Turn init function. This runs after all the units get their acp and mp for the turn, but before anybody actually gets to move.
- Unit order function. This gets run to decide what the unit should do. Usually it should be allowed to follow its plan. [do separate fns for before and after plan execution?]
- Event reaction functions. [how many?]
- State save function. This formats state that should be saved into a Lisp object. The game saving code then writes this state into the file so that it can be read in again. You may not do any allocation while saving a game however, so if you need space (and note that even a single cons allocates space), you must have the AI init routine or some such do the allocation early on, then reuse the space.

Note that these functions have very few constraints, so you can write them to work together in various ways. For instance, an AI can decide whether to resign once/turn, once/action, or once for each 4 units it moves, every other turn.

[describe default AI as illustrative example]

Go to the first, previous, next, last section, table of contents.