# Bos Wars Scripting API: Artificial Intelligence (AI)

Bos Wars 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)

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", "building-1", ... "building-n"}`
    builder
      Name of the unit-type that can build.
    building-1
    building-n
      Names of the unit-types that can be builded by the builder.
    Note: If more units can build the same buildings you need the same list for all units.
  - `{"train", "trainer", "unit-1", ... "unit-n"}`
    trainer
      Name of the unit-type that can train the units.
    unit-1
    unit-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.
  - `{"upgrade", "unit", "upgraded-unit-1", ... "upgraded-unit-n"}`
    unit
      Name of the unit-type that can be upgraded.
    upgraded-unit-1
    upgraded-unit-n
      Names of the unit-types to that the unit can be upgraded.
  - `{"research", "researcher", "upgrade-1", ... "spell-n"}`
    researcher
      Name of the unit-type that can reasearch upgrades or spells.
    upgrade-1
    spell-n
      Names of the upgrades or spells that can be researched by the researcher.
  - `{"repair", "worker", "unit-1", ... "unit-n"}`
    worker
      Name of the unit-type that can repair other units.
    unit-1
    unit-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.
  - `{"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.
  - `{"unit-limit", "unit", "resource"}`
    unit
      Name of the unit-type that must be produced to reduce the unit limit.
    resource
      Name of the resource producing the shortage.
    I think we should swap the arguments: "resource" "unit".

#### Example

A minimal AI helper definition:

    DefineAiHelper(
      {"build", "unit-peasant", "unit-farm"},
      {"train", "unit-town-hall", "unit-peasant"},
      {"upgrade", "unit-town-hall", "unit-keep"},
      {"research", "unit-human-blacksmith", "upgrade-sword1"},
      {"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 town hall can be upgraded to a keep.

The blacksmith can reasearch the upgrade sword1.

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.

### 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
  Time in game cycles.
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
  Time in game cycles.
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.

All trademarks and copyrights on this page are owned by their respective owners.