AI Python API
This page provides (or will provide) an overview of FreeOrion AI Python scripting and a reference for the FreeOrion AI Python interface.
FreeOrion provides an embedded Python interface for writing AI scripts. Python can be easily modified to alter AI behaviour without needing to recompile the AI executable.
The FreeOrion AI executable looks for an AI script file:
This file must contain the following functions:
- initFreeOrionAI() - Called when the AI client is first started. Doesn't need to do anything.
- startNewGame() - Called when a new game is started (but not when a game is loaded). Should clear any pre-existing AI state and set up whatever the AI script needs to generate orders.
- resumeLoadedGame(savedStateString) - Called when a saved game is loaded. The savedStateString parameter will contain AI state information from when the game was saved, and this information should be extracted now so that the AI script can resume from where it was when the game was saved.
- prepareForSave() - Called when the game is about to be saved. The AI script should compact any state information it wishes to retain when a game is later loaded into a single string (This can be done using Python's pickle library). The setSaveStateString() function is used to set the save state string, and should be called within prepareForSave().
- handleChatMessage(senderID, messageText) - Called when this player receives a text message. The AI script can reply using the sendChatMessage() function, or can ignore the message. senderID is the player ID of the player who send the message, and messageText is the text of the sent message.
- generateOrders() - Called once per turn to tell the AI script that it should generate and issue orders for the current turn. Within this function the doneTurn() function should be called to indicate that the script is done issuing orders for this turn.
IntVec, StringVec, IntSet, StringSet