_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
T H E W A R B E G I N S
Stratagus - A free fantasy real time strategy game engine
Stratagus Configuration Language Description: Artificial Intelligence (AI)
Stratagus
FAQ
PREV
NEXT
LUA Index
DefineAi
DefineAiHelper
DefineAiPlayer
AiCheckForce
AiComputeGauges
AiDebug
AiDebugPlayer
AiDump
AiAttackWithForce
AiForce
AiForceRole
AiGetRace
AiGetSleepCycles
AiNeed
AiPlayer
AiResearch
AiSet
AiSetCollect
AiSetReserve
AiSleep
AiUpgradeTo
AiWait
AiWaitForce
Intro - Introduction to AI functions and variables
Everything around the control of the Stratagus 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 Stratagus is script based. Each AI player keeps executing scripts.
There are two kinds of scripts :
- The main script. It starts buildings, upgrades, ...
- 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.
Functions
DefineAi(name, race, class, script)
This defines how a special AI works. Each level can use his own AI definition.
- name
- Unique name of this AI definitions.
- race
- Name of the race for what this definition can be used, if the definition
can be used for any race use "*".
- class
- Class name of this definition. Used to choose the AI for computer players.
The puds contains numbers which are mapped by the C table AiTypeWcNames into
internal names. Some common used names are "passive", "land-attack",
"sea-attack", "air-attack".
- script
- A lua function.
Example
-- Defines the passive computer AI, which does nothing.
DefineAi("passive-ai", "*", "passive", function() AiSleep(10000) end)
DefineAiHelper(list)
Define the 'equivalent' unit type for the AI.
- list
- A list of features:
{"equiv", "unit", "unit-1", ... "unit-n"}
- unit
- Name of the unit-type that have equilvalent units.
- unit-1
- unit-n
- Names of the unit-types that can be uses by the AI equivalent.
Example
A minimal AI helper definition:
DefineAiHelper({"unit-equiv", "unit-town-hall", "unit-keep"})
For the AI, a keep is equivalent to a town hall.
DefineAiPlayer(list, ...)
Define an AI player. (NOT SUPPORTED)
Example
-- FIXME:
FIXME:
DefineAiPlayer()
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.
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 ready.
- force
- Number of the force to which the units should belong. 0 - 9 is currently supported.
Example
-- Force 0 is build with one footman. When the force is ready, sleep for
-- 500 frames.
AiForce(0, "unit-footman", 1)
if (AiCheckForce(0)) then AiSleep(500) end
AiDebug(flag)
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, ...)
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")
AiDump()
Dump some AI debug information.
Example
-- Prints out some debug information.
AiDump()
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.
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 footmans and 5 bowmans, after this
-- force 1 is filled with 3 footmans and 2 bowmans.
AiForce(0, "unit-footman", 4, "unit-bowman", 5)
AiForce(1, "unit-footman", 3, "unit-bowman", 2)
AiForceRole(force, role)
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")
AiGetRace()
Get the race of the current AI player.
Example
-- Returns the race name of the current AI player.
AiGetRace()
AiGetSleepCycles()
Get the number of cycles to sleep.
Example
-- Returns the number of cycles to sleep.
AiGetSleepCycles()
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.
Example
-- The great hall must be build before a barrack.
AiNeed("unit-great-hall")
AiNeed("unit-barrack")
AiPlayer()
Return the player of the running AI.
Example
-- Returns the player of the running AI.
player = AiPlayer()
AiResearch(upgrade)
Let the AI research an upgrade, upgrades are reseached in command order.
And automatic researched if lost. Building orders have a higher priority.
The script writer is responsible for the correct order. If wrong the
AI could hang up.
- upgrade
- Upgrade name to be researched.
Example
-- Research a better armor for land units.
AiResearch("upgrade-shield1")
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.
Example
-- Request two assault units.
AiSet("unit-assault", 2)
AiSetCollect({time, resource1, resource2, ...})
Set AI player resource collect percent.
- time
- unused parameter.
- resource1
- Percentage needed for the first resource.
Example
-- Set the collect percent to 50% gold and 50% wood.
AiSetCollect({0, 50, 50, 0, 0, 0, 0})
AiSetReserve({time, resource1, ...})
Set AI player resource reserve.
- time
- unused parameter.
- reource1
- first resource.
Example
-- Set all of the reserves to 0.
AiSetReserve({0, 0, 0, 0, 0, 0, 0})
AiSleep(frames)
Wait some frames, to let the opponent (you) recover.
- frames
- How many frames (~1/30s) the AI shouldn't proceed with the script.
Example
{
-- Wait 500 frames ~16s.
function() return AiSleep(500) end,
}
AiUpgradeTo(unit-type)
Upgrade units to an improved type. You must give for each unit you want to
upgrade an upgrade command. The computer automatic searches which unit it
upgrades.
- unit-type
- Unit type to that a unit should be upgraded.
Example
-- Upgrade a town-hall to keep.
AiUpgradeTo("unit-keep")
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
- Wait for this unit type.
Example
-- Proceed only if a peasant is ready.
AiNeed("unit-peasant")
AiWait("unit-peasant")
AiWaitForce(force)
Wait until 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.
Example
-- Force 0 is build with one footman. The script continues processing, if the
-- footman is ready trained.
AiForce(0, "unit-footman", 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.
Examples
DefineAi("wc2-example", "orc", "land-attack",
function()
This is the AI "wc2-example" usable for orc race and land-attack.
AiNeed("unit-great-hall")
The first need unit is the great-hall.
AiNeed("unit-peon")
The next unit should be the peon.
AiWait("unit-great-hall")
Now wait until the great hall is available. Could hang if no great hall can be
build.
AiWait("unit-peon")
Now wait unit we have a worker to build the next things.
FIXME: need some complex examples.
(C) Copyright 2002-2015 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.