# Bos Wars Scripting API: Artificial Intelligence (AI)

Bos Wars FAQ PREV NEXT LUA Index

DefineAi DefineAiHelper DefineAiPlayer 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.

## Functions

### DefineAi(name, class, script)

This defines how a special AI works. Each level can use his own AI definition.

name
  Unique name of this AI definitions. The "ai-type" parameter of DefineAiPlayer refers to this.
class
  Class name of this definition. Used to choose the AI for computer players. The map setup file sets e.g. `Players[1].AiName = "ai-blitz"` and the engine then finds the AI type whose class name is that string. This is also used in the "ai-name" parameter of Player.
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.
  - `{"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.
  - `{"unit-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"},
      {"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.

### DefineAiPlayer(player-number, properties)

Define an AI player. Saved games use this function; map setup files should not.

player-number
  Number of the player, counting from 0.
properties
  Properties of the AI player. This list consists of tags and associated values. These tags are available:
  "ai-type", string
    Name of the AI type. Matches the name (not class) parameter of DefineAi.
  "script", string
    Name of the AI script function in the "_ai_scripts_" Lua table. DefineAi generates this name.
  "script-debug", boolean
    Obsolete since r5923 (2004-01-01): The debug flag no longer affects the engine, and scripts cannot read it.
    Whether to display the executed AI script commands. The AiDebug and AiDebugPlayer functions also control this flag.
  "sleep-cycles", integer
    The number of the game cycle on which the AI will wake up from AiSleep, or 0 if not sleeping.
  "force", {force, properties}
    Defines a group of units that should attack or defend together, to overwhelm the enemy. The value must be a list that contains two elements:
    force
      Number of the force. 0 - 9 is currently supported.
    properties
      Properties of the force. This list consists of subtags and possibly values. Some subtags do not allow a value and the others require a value. These subtags are available:
      "complete"
        The force does not need more units. This is the opposite of "recruit".
      "recruit"
        The force needs more units. This is the opposite of "complete".
      "attack"
        The force is attacking.
      "defend"
        The force is defending a unit that was attacked. After the enemies are dead, the force should return home.
      "role", string
        Obsolete since r5923 (2004-01-01): Roles of forces no longer affect the engine, and scripts cannot read them.
        The role of the force, as set with AiForceRole.
      "types", {[count, unit-type-name]...}
        How many of each unit type should be in the force. This is set with AiForce.
      "units", {[unit-number, unit-type-name]...}
        List of units currently in the force. The type names are ignored on load.
      "state", integer
        Attack state.
      "goalx", integer
        Attack point X tile map position.
      "goaly", integer
        Attack point Y tile map position.
  "needed", {[resource-type-name, amount]...}
    FIXME
  "need-mask", {resource-type-name...}
    FIXME
  "last-can-not-move-cycle", integer
    FIXME
  "unit-type", {[unit-type-name, count]...}
    FIXME
  "building", {[unit-type-name, made, want]...}
    FIXME
  "repair-building", integer
    FIXME
  "repair-workers", {[building-unit-number, count]...}
    Number of workers that failed trying to repair a building.

For an example of DefineAiPlayer, please look in any save file. No example is provided in this document because Lua scripts for maps should not call 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)

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.

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)

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.

#### 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()

### 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)

### 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,
        }

### 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-engineer")
        AiWait("unit-engineer")

### 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 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.

## Examples

    DefineAi("harsh-example", "land-attack",
     function()

This is the AI "harsh-example" usable for land-attack.

         AiNeed("unit-vault")

The first need unit is the great-hall.

         AiNeed("unit-engineer")

The next unit should be the peon.

         AiWait("unit-vault")

Now wait until the vault is available. Could hang if no great vault can be build.

         AiWait("unit-engineer")

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.