Bos Wars Scripting API: Artificial Intelligence (AI)
Bos Wars
FAQ
PREV
NEXT
LUA Index
AiTypes
AiState
DefineAiType
DefineAiHelper
AiCheckForce
AiDebug
AiDebugPlayer
AiAttackWithForce
AiForce
AiForceRole
AiNeed
AiPlayer
AiSet
AiSleep
AiWait
AiWaitForce
Intro - Introduction to AI functions and variables
Everything around the control of the Bos Wars AI.
Warning: The AI documentation is slightly outdated. This documentation hasn't
been fully converted to the lua API yet.
How does it work
AI in Bos Wars is script based. Each AI player keeps executing scripts.
There are two kinds of scripts :
- The main script. It starts buildings, ...
- The action/reaction scripts. They are started by the AI engine, under certain condition.
Scripts can arrange and control units using forces :
A script can ask for some type of unit in a force (using AiForce),
and then wait for them to be ready (using AiWaitForce).
The force 0 is a special case : it holds all the unassigned units, and is used to fill other forces.
( when needed, units are transfered from force 0 to others ). Attacker units in force 0 won't be used for attacks
Forces from 1 to 3 are also special : They are used as the attack reserve.
Attack forces will only be created using units available in those forces.
The main script is responsible for setting minimums for the forces 0 and 1. This will influence the balance
between defend and attack for an AI player.
Variables
AiTypes
This table lists all the AI types defined in Lua. It is used by
both the engine and the Lua code. In AiTypes, every AI type has an
entry that is a table with these keys:
- Ident = string
- Unique, language-independent identifier of this AI type.
By convention, each identifier should begin with "ai-".
The Ident string is also the key of the entry within AiTypes.
The map setup file sets e.g.
Players[1].AiName = "ai-blitz"
and the engine then finds the AI type whose identifier is that string;
or if no such AI type exists, the engine falls back to "ai-passive".
In saved games, these names are used in
the "ai-type" parameter of DefineAiPlayer
and the "ai-name" parameter of Player.
- Name = string
- Name of the AI the Player sees and which gets translated.
The engine does not use this; only the Lua code does.
- Init = function or nil
- Initializes
AiState[AiPlayer()]
to whatever the AI type requires.
When starting a new game, the engine calls the Init function
for each player that is going to use this AI type.
Typically, the Init function creates a table and writes it to
AiState[AiPlayer()], and the EachSecond
function will then maintain that table.
Even if the AI type does not use AiState for its own purposes,
the Init function must regardless write a non-nil value to
AiState[AiPlayer()], so that the engine will know the AI state
has been initialized and not call the Init function again
if the game is later saved and loaded.
(Of course, if the AI type has no Init function, then there
is no such requirement.)
- EachSecond = function
- Decides AI actions: primarily, what to build.
The engine calls the EachSecond function once per second
(at the default game speed).
- (other)
- Lua code can add other keys in the table.
The engine will ignore them.
(Perhaps there should be a naming convention so that
Lua-specific and engine-specific keys won't clash.
Or we can just document all keys here.)
Lua code is free to read the AiTypes table but should not
modify it or its elements directly. Instead, it should call
DefineAiType, so that the engine
knows to update its data structures accordingly.
AiState
The AI state of the map and each AI player.
The AI scripts should write all persistent state into AiState,
so that the engine will save and load it as part of the game,
and so that the engine can easily clear the state when
starting a new game.
- AiState[AiPlayer()] = table
- AI state of a single AI player.
The Init function of the AI type
creates this table, and the other functions of the AI type
can then use the fields of the table as they please.
If AiState[AiPlayer()] is nil, that means the engine has not
called the Init function yet.
- AiState.cache = table
- Cached information shared by all AI players.
Lua scripts of individual AI types should not access this directly.
Instead, they should use shared functions.
Not in engine:
The engine does not know about AiState.cache.
Only the Lua code does.
The engine does not currently preserve object identity when saving
and loading a game. That means, if the AI script does
local x = {}
AiState[AiPlayer()].first = x
AiState[AiPlayer()].second = x
then a comparison like
AiState[AiPlayer()].first == AiState[AiPlayer()].second
will initially return true, but if the game is saved and loaded, the
comparison will start returning false because first
and
second
now refer to different empty tables.
However, scripts should not depend on this behavior.
Functions
DefineAiType(table)
Defines an AI type and adds it to AiTypes.
- table
- Describes the AI type, in the same format
as an element of AiTypes.
Note: Individual maps should not call DefineAiType.
Call it during global initialization instead.
The engine does not provide any way to undefine an AI type
and reacts poorly to redefinitions.
Example
-- Defines the passive computer AI, which does nothing.
DefineAiType({
Ident = "ai-passive",
Name = _("Passive"),
Init = nil, -- which is the default anyway
EachSecond = function() end })
DefineAiHelper(list)
The AI is complete configurable, nothing is hardcoded. The AI knows nothing
about any units without this table. This table defines F.E. what unit can build
and what they can build and many other things that the AI must know.
- list
- A list of features:
{"build", builder-type, building-type-1, ... building-type-n}
-
Obsolete since r8101 (2006-06-21):
Ignored. The AI instead gets this information from
DefineButton calls
that have Action = "build".
- builder-type
- Name of the unit-type that can build.
- building-type-1
- building-type-n
- Names of the unit-types that can be built by the builder.
Note: If more units can build the same buildings
you need the same list for all units.
{"train", trainer-type, unit-type-1, ... unit-type-n}
-
Obsolete since r8101 (2006-06-21):
Ignored. The AI instead gets this information from
DefineButton calls
that have Action = "train".
- trainer-type
- Name of the unit-type that can train the units.
- unit-type-1
- unit-type-n
- Names of the unit-types that can be trained by the trainer.
Note: If more units can train the same unit
you need the same list for all units.
{"repair", worker-type, unit-type-1, ... unit-type-n}
-
Obsolete since r8101 (2006-06-21):
Ignored. The AI instead gets this information from
DefineButton calls
that have Action = "repair".
- worker-type
- Name of the unit-type that can repair other units.
- unit-type-1
- unit-type-n
- Names of the unit-types that can be repaired by the worker.
Note: If more units can repair the same units
you need the same list for all units.
{"unit-equiv", unit-type, unit-type-1, ... unit-type-n}
- Declares unit types as equivalent for AI purposes.
When AiForce is asked to train
any of them, it instead trains the best one it can;
"best" here is controlled by the Priority setting in
DefineUnitType.
- unit-type
- Name of the unit-type that have equivalent units.
- unit-type-1
- unit-type-n
- Names of the unit-types that can be used by the AI equivalent.
{"unit-limit", unit-type, resource}
-
Obsolete since r8101 (2006-06-21) and r8658 (2007-04-01):
Ignored. The engine no longer limits the number of units
according to resources.
- unit-type
- Name of the unit-type that must be produced to increase the unit limit.
- resource
- Name of the resource producing the shortage.
I think we should swap the arguments: "resource", "unit-type".
Example
A minimal AI helper definition:
DefineAiHelper(
{"build", "unit-peasant", "unit-farm"},
{"train", "unit-town-hall", "unit-peasant"},
{"repair", "unit-peasant", "unit-town-hall"},
{"unit-limit", "unit-farm", "food"},
{"unit-equiv", "unit-town-hall", "unit-keep"})
A peasant can build a farm.
The town hall can train a peasant.
A peasant can repair the town hall.
To fix the food shortage the AI must build farms.
For the AI is a keep equivalent to a town hall.
AiAttackWithForce(force)
Attack the opponent with the given force. The place is choosen by the AI. If there
are flyers, ships and land units in the force they could attack different
goals.
- force
- ID of the force to which the attacking units belong. The force ids 0 to 9
are currently supported.
- returns
- False.
The force isn't moved as a single unit: faster units attacks first, than later the
slower units will attack.
Example
-- Force 0 is built with one assault unit. The script continues processing, when
-- the assault unit is ready. When ready, the script attacks the opponent
-- with force 0.
{
function() return AiForce(0, "unit-assault", 1) end,
function() return AiWaitForce(0) end,
function() return AiAttackWithForce(0) end
}
AiCheckForce(force)
Check if a force is complete.
- force
- Number of the force to which the units should belong. 0 - 9 is currently
supported.
- returns
- True if the force is complete, false if not yet.
Example
-- Force 0 is build with one assault.
AiForce(0, "unit-assault", 1)
...
-- If the force is ready, use it to attack.
if (AiCheckForce(0)) then AiAttackWithForce(0) end
AiDebug(flag)
Obsolete since r5923 (2004-01-01):
The debug flag no longer affects the engine,
and scripts cannot read it.
Enable or disable the debug output of the AI script execution.
- flag
- If true enables printing of the AI commands, if false disables it.
Example
-- Prints now the commands of this computer player.
AiDebug(true)
AiDebugPlayer(1, 2, 3, ...)
Obsolete since r5923 (2004-01-01):
The debug flag no longer affects the engine,
and scripts cannot read it.
Activate dump of AI forces and strategy on stdout.
Parameters are player number, or "self", for thisplayer.
"none" will stop all AI debug output.
Example
AiDebugPlayer("self")
AiForce(force, unit-type-1, count-1, ... ,unit-type-N, count-N)
Define a force, what and how many units should belong to a force. Up to 10
forces are currently supported.
Force 0 is currently fixed to be the defence force. Send to a building or
unit under attack.
If there are any unassigned units of the requested unit-type, than they are
assigned to the force.
Note: The low-level didn't support the reduce of a force.
- force
- Number of the force to which the units should belong. 0 - 9 is currently
supported.
- unit-type-1
- unit-type-N
- Unit-type that should be in the force. Currently only mobile (trained) units
are allowed.
- count-1
- count-N
- How many of this units should be in the force.
- returns
- False.
The unit-types should be build in a fixed order. From left to right? or from
each unit-type one after the other?
Example
-- First the force 0 is filled up with 4 assaults and 5 bazoos, after this
-- force 1 is filled with 3 assaults and 2 bazoos.
AiForce(0, "unit-assault", 4, "unit-bazoo", 5)
AiForce(1, "unit-assault", 3, "unit-bazoo", 2)
AiForceRole(force, role)
Obsolete since r5923 (2004-01-01):
Roles of forces no longer affect the engine,
and scripts cannot read them.
Define the role of a force.
- force
- Number of the force to which the units belong. 0 - 9 is currently
supported.
- role
- The role of the force. Can be either "attack" or "defend".
Example
-- Sets the role of force 0 to attack.
AiForceRole(0, "attack")
AiNeed(unit-type)
Tells the AI that it should have a unit of this unit-type. The AI builds or
trains units in this order of the AiSet/AiNeed commands. If the unit or an
equivalent unit already exists, the AI does nothing. If the unit is lost, it is
automatically rebuilt. If the units are requested in wrong order, the AI could
hang up. Resources are collected automatic and farms are automatic build, but
additional could be requested.
- unit-type
- Name of the unit-type required.
- returns
- False.
Example
-- The magmapump must be build before a powerplant.
AiNeed("unit-magmapump")
AiNeed("unit-powerplant")
AiPlayer()
Return the player number of the running AI.
This is often used for indexing the AiState table.
Example
-- If the player of the running AI has insufficient magma
-- then build a magmapump:
player = AiPlayer()
if (Players[player].MagmaStored < 300) then
AiNeed("unit-magmapump")
end
AiSet(unit-type, count)
This AiNeed with a number. Tells the AI that it should have a specified number
of a unit of this unit-type. The AI builds or trains units in this order of
the AiSet/AiNeed commands. If the unit or an equivalent unit already exists,
the AI does nothing. If the units are lost, they are automatic rebuild. If the
units are requested in wrong order, the AI could hang up. Resources are
collected automatic and farms are automatic build, but additional could be
requested. In the opposite to AiNeed, which always inserts a request, AiSet
modifies the last request to the new number.
- unit-type
- Name of the unit-type(s) required.
- count
- Number of unit-types(s) required. Zero is not allowed.
- returns
- False.
Example
-- Request two assault units.
AiSet("unit-assault", 2)
AiSleep(frames)
Wait some frames, to let the opponent (you) recover.
The first time this function is called,
the frames value is stored, and "true" is returned. The next times,
also "true" is returned, until the timeout is over:
then "false" is returned.
The frames parameter is ignored except during the first call.
- frames
- How many frames (~1/30s) the AI shouldn't proceed with the script.
- returns
- True if we need to wait longer.
- False if the waiting period is over.
Example
{
-- Wait 500 frames ~16s.
function() return AiSleep(500) end,
}
AiWait(type)
Waits until the *first* request of this unit-type is completed.
Don't forget to request a unit-type, before you wait on it.
- type
- The unit type name string.
- returns
- True if this unit type is not yet available.
- False if at least one instance of this unit-type exists.
Example
{
-- Proceed only if an engineer is ready:
function() return AiNeed("unit-engineer") end,
function() return AiWait("unit-engineer") end,
-- Proceed only if two magmapumps are ready:
function() return AiSet("unit-magmapump", 2) end,
function() return AiWait("unit-magmapump") end,
}
AiWaitForce(force)
Check if a force is complete.
The forces are build in force number order. First 0, than 1, last 9.
- force
- Number of the force to which the units should belong. 0 - 9 is currently
supported.
- returns
- False if the force is complete, true if not yet.
Example
-- Force 0 is build with one assault unitn. The script continues processing, if the
-- unit is ready trained.
AiForce(0, "unit-assault", 1)
AiWaitForce(0)
Notes
The current AI script support is very limited, many new functions are needed.
If you want to write this you know who to contact.
All trademarks and copyrights on this page are owned by their respective owners.
(c) 2002-2010 by
The Bos Wars Project