Stratagus Configuration Language Description: Artificial Intelligence (AI)


 
         _________ __                 __                               
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \ 
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/ 
    ______________________                           ______________________
                         T H E   W A R   B E G I N S
           Stratagus - A free fantasy real time strategy game engine

(C) Copyright 2002-2004 by The Stratagus Project. Distributed under the "GNU General Public License"



AiCheckForce AiComputeGauges AiDebug AiDebugPlayer AiDump 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.

How does it work

AI in Stratagus is script based. Each AI player keeps executing scheme-like scripts. There are two kinds of scripts :

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

 (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) (AiSleep 500))

(AiDebug flag)

Enable or disable the debug output of the AI script execution.

flag

If #t enables printing of the AI commands, if #f disables it.

Example

 
    ;; Prints now the commands of this computer player.
    (AiDebug #t)

(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 should 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 an 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 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.

unit-type

Name of the unit-type required.

Example

 
    ;; The great hall must be build before a barrack.
  (AiNeed (unit-type 'unit-great-hall))
  (AiNeed (unit-type 'unit-barrack))

(AiPlayer)

Return the player of the running AI.

Example

 
    ;; Returns the player of the running AI.
    (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 an 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 peons.
    (AiSet 'unit-peon 2)

(AiSetCollect #(time gold wood oil ore stone coal))

Set AI player resource collect percent.

time

Time in game cycles.

gold

Gold resource.

wood

Wood resource.

oil

Oil resource.

ore

Ore resource.

stone

Stone resource.

coal

Coal resource.

Example

 
    ;; Set the collect percent to 50% gold and 50% wood.
    (AiSetCollect #(0  50 50 0  0 0 0))

(AiSetReserve #( time gold wood oil ore stone coal))

Set AI player resource reserve.

time

Time in game cycles.

gold

Gold resource.

wood

Wood resource.

oil

Oil resource.

ore

Ore resource.

stone

Stone resource.

coal

Coal 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.
    (AiSleep 500)

(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 an 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 an 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

This is the AI "wc2-example" usable for human race and land-attack.

 
  '( (AiNeed (unit-type '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 built.

 
     (AiWait 'unit-peon) ))

Now wait unit we have a worker to build the next things


Last changed: $Id: ai.html,v 1.25 2004/07/30 18:29:47 feber Exp $
All trademarks and copyrights on this page are owned by their respective owners.

(c) 2002-2003 by The Stratagus Project
 
Here you will find a listing of all the unit and building types. This can also be found in ai.lua in the scripts folder
 

-- Unit can build which buildings.

  --

  {"build", "unit-peasant",

  "unit-farm", "unit-human-barracks", "unit-town-hall", "unit-elven-lumber-mill",

  "unit-human-blacksmith", "unit-human-watch-tower", "unit-human-wall",

  "unit-human-shipyard", "unit-human-foundry", "unit-human-refinery",

  "unit-inventor", "unit-stables", "unit-mage-tower", "unit-church",

  "unit-gryphon-aviary"},

  {"build", "unit-human-oil-tanker", "unit-human-oil-platform"},

  --

  -- Building can train which units.

  --

  {"train", "unit-farm", "unit-critter"},

  {"train", "unit-town-hall", "unit-peasant"},

  {"train", "unit-keep", "unit-peasant"},

  {"train", "unit-castle", "unit-peasant"},

  {"train", "unit-human-barracks",

  "unit-footman", "unit-archer", "unit-ranger", "unit-ballista", "unit-knight",

  "unit-paladin"},

  {"train", "unit-inventor",

  "unit-balloon", "unit-dwarves"},

  {"train", "unit-mage-tower", "unit-mage"},

  {"train", "unit-gryphon-aviary", "unit-gryphon-rider"},

  {"train", "unit-human-shipyard",

  "unit-human-oil-tanker", "unit-human-destroyer", "unit-human-transport",

  "unit-human-submarine", "unit-battleship"},

  --

  -- Building can upgrade which upgrades.

  --

  {"upgrade", "unit-town-hall", "unit-keep"},

  {"upgrade", "unit-keep", "unit-castle"},

  {"upgrade", "unit-human-watch-tower",

  "unit-human-guard-tower", "unit-human-cannon-tower"},

  --

  -- Building can research which spells or upgrades.

  --

  {"research", "unit-human-blacksmith",

  "upgrade-sword1", "upgrade-sword2",

  "upgrade-human-shield1", "upgrade-human-shield2",

  "upgrade-ballista1", "upgrade-ballista2"},

  {"research", "unit-elven-lumber-mill",

  "upgrade-arrow1", "upgrade-arrow2", "upgrade-ranger",

  "upgrade-ranger-scouting", "upgrade-longbow", "upgrade-ranger-marksmanship"},

  {"research", "unit-church",

  "upgrade-paladin", "upgrade-healing", "upgrade-exorcism"},

  {"research", "unit-mage-tower",

  "upgrade-slow", "upgrade-flame-shield", "upgrade-invisibility",

  "upgrade-polymorph", "upgrade-blizzard"},

  {"research", "unit-human-foundry",

  "upgrade-human-ship-cannon1", "upgrade-human-ship-cannon2",

  "upgrade-human-ship-armor1", "upgrade-human-ship-armor2"},

  --

  -- Unit can repair which units.

  --

  {"repair", "unit-peasant",

  "unit-farm", "unit-human-barracks", "unit-town-hall", "unit-keep", "unit-castle",

  "unit-elven-lumber-mill", "unit-human-blacksmith", "unit-human-watch-tower",

  "unit-human-guard-tower", "unit-human-cannon-tower", "unit-human-wall",

  "unit-human-shipyard", "unit-human-foundry", "unit-human-refinery",

  "unit-inventor", "unit-stables", "unit-mage-tower", "unit-church",

  "unit-gryphon-aviary", "unit-human-transport"},

 

Listing of all the selection functions for use with Ai need and AI research

--

--  City-center of the current race.

--

function AiCityCenter()

  if (AiGetRace() == race1) then

    return "unit-town-hall"

  else

    return "unit-great-hall"

  end

end

 

--

--  Better city-center of the current race.

--

function AiBetterCityCenter()

  if (AiGetRace() == race1) then

    return "unit-keep"

  else

    return "unit-stronghold"

  end

end

 

--

--  Best city-center of the current race.

--

function AiBestCityCenter()

  if (AiGetRace() == race1) then

    return "unit-castle"

  else

    return "unit-fortress"

  end

end

 

--

--  Worker of the current race.

--

function AiWorker()

  if (AiGetRace() == race1) then

    return "unit-peasant"

  else

    return "unit-peon"

  end

end

 

--

--  Lumber mill of the current race.

--

function