AI Python API

From FreeOrionWiki

Jump to: navigation, search

FreeOrion provides an embedded Python interface for writing AI scripts to determine AI behaviours. Python scripts can be easily modified and used without need to recompile the AI executable. This page provides (or will provide) an overview of FreeOrion AI Python scripting and a reference for the FreeOrion AI Python interface.

FreeOrion AI scripts work by acting like a player: examining the gamestate, deciding on strategies, and issuing orders... essentially playing the game just like a player, except without a GUI. The AI script can't directly modify the game universe by creating buildings or planets or ships from nothing, as these are not powers available to any player. If the AI wants a new ship, it needs to order its production and wait for it to complete, just like a player. Similarly, the AI client is given the same information that any player, human or AI, receives about the gamestate, based on what ships, planets, system, etc. that the player's empire knows about. AIs play by the same rules as human players.

The FreeOrion AI interface provides functions and classes to access the known game Universe and its contents, Empires and player information, as well as functions to issue orders and for logging or debug purposes. (Essentially) all these functions and classes are exposed to Python in the freeOrionAIInterface module, which can be imported into Python scripts executed by the FreeOrion AI executable.

Contents

Scripting Structure

The FreeOrion AI executable looks for an AI script file:

/default/AI/FreeOrionAI.py

This main AI script file must contain the following Python functions for the FreeOrion AI client to execute properly:

freeOrionAIInterface

Within FreeOrionAI.py and within any other script files called by it, the freeOrionAIInterface module should be imported:

import freeOrionAIInterface as fo

This module provides all the functions and classes used to access the gamestate that the AI player knows about and all the functions used to issue orders, interact with other players, save game state or to end the AI player's turn. The following functions and classes are all contained within freeOrionAIInterface.

Logging

FreeOrion AI Python scripts are run by the AI executable, which does not have any display or user interface. In order to see error messages or debug output from scripts, the AI executable redirects standard output and error to the AI log file. Any error output that is produced during execution of a script will appear in the FreeOrion AI log file automatically. To generate debug output, simply use the built-in python print command:

print "this will generate debug output"

The printed text will appear in the FreeOrion AI log file. The log files are located in the directory discussed here. The AI log files are name AI_#.log where # is a number such as 1, 2, 3, etc. Each AI player in a game generates a separate log file.

Free Functions

The following functions are exposed to Python in the freeOrionAIInterface module:

Classes

Universe

SubClasses:
SubClass Of:

The Universe class contains the majority of FreeOrion gamestate: All the UniverseObjects in the game, and all ShipDesigns in the game. (Other gamestate is contained in the Empire class.) The Universe class provides functions with which to access objects in it and information about connections and paths between systems.

Properties

Functions

UniverseObject

SubClasses: Fleet, Ship, Building, Planet, System
SubClass Of:

UniverseObject is the base class for all in-game objects, and has all the common properties and functions shared by such objects.

Properties

Functions

Fleet

SubClasses:
SubClass Of: UniverseObject

Properties

Ship

SubClasses:
SubClass Of: UniverseObject

Properties

ShipDesign

SubClasses:
SubClass Of:

Properties

Functions

Note that there are Free Functions available related to ShipDesign as well: validShipDesign and getShipDesign.

PartType

SubClasses:
SubClass Of:

Properties

Functions

HullType

SubClasses:
SubClass Of:

Properties

Functions

Building

SubClasses:
SubClass Of: UniverseObject

Properties

BuildingType

SubClasses:
SubClass Of:

Properties

Functions

ResourceCenter

SubClasses: Planet
SubClass Of:

Properties

PopCenter

SubClasses: Planet
SubClass Of:

Properties

Planet

SubClasses:
SubClass Of: UniverseObject, ResourceCenter, PopCenter

Properties

System

SubClasses:
SubClass Of: UniverseObject

Properties

Functions

Note that Universe also has the SystemHasStarlane function, which indicates whether a particular system has any starlanes visible to a particular empire.

Special

SubClasses:
SubClass Of:

Properties

Species

SubClasses:
SubClass Of:

Properties

Empire

Subclasses:
Subclass Of:

Properties

Functions

ResearchQueue

Subclasses:
Subclass Of:

ResearchQueueElement

Subclasses:
Subclass Of:

ProductionQueue

Subclasses:
Subclass Of:

ProductionQueueElement

Subclasses:
Subclass Of:

Properties

Tech

Subclasses:
Subclass Of:

Enumerations

starType

planetSize

planetType

planetEnvironment

techType

techStatus

buildType

resourceType

meterType

captureResult

shipSlotType

shipPartClass

STL Containers

Two types of C++ Standard Template Library (STL) containers are returned by FreeOrion Python AI interface functions: set and vector. Both set and vector may be returned containing integers or strings. All four combinations (integer set, integer vector, string set, string vector) are treated as separate classes in Python. All have been exposed to Python, and may be iterated over and treated like normal Python containers: C++ sets act similarly to Python frozenset and C++ vectors act similarly to Python lists.

IntSet and StringSet

Both specializations of C++ set act similarly to a Python frozenset containing a single type of object (int or string, respectively). The following functions are defined:

IntVec and StringVec

Both specializations of C++ vector act similarly to a Python list containing a single type of object (int or string, respectively). The following functions are defined:

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox