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

  if (AiGetRace() == race1) then

    return "unit-elven-lumber-mill"

  else

    return "unit-troll-lumber-mill"

  end

end

 

--

--  Blacksmith of the current race.

--

function AiBlacksmith()

  if (AiGetRace() == race1) then

    return "unit-human-blacksmith"

  else

    return "unit-orc-blacksmith"

  end

end

 

--

--  Upgrade armor 1 of the current race.

--

function AiUpgradeArmor1()

  if (AiGetRace() == race1) then

    return "upgrade-human-shield1"

  else

    return "upgrade-orc-shield1"

  end

end

 

--

--  Upgrade armor 2 of the current race.

--

function AiUpgradeArmor2()

  if (AiGetRace() == race1) then

    return "upgrade-human-shield2"

  else

    return "upgrade-orc-shield2"

  end

end

 

--

--  Upgrade weapon 1 of the current race.

--

function AiUpgradeWeapon1()

  if (AiGetRace() == race1) then

    return "upgrade-sword1"

  else

    return "upgrade-battle-axe1"

  end

end

 

--

--  Upgrade weapon 2 of the current race.

--

function AiUpgradeWeapon2()

  if (AiGetRace() == race1) then

    return "upgrade-sword2"

  else

    return "upgrade-battle-axe2"

  end

end

 

--

--  Upgrade missile 1 of the current race.

--

function AiUpgradeMissile1()

  if (AiGetRace() == race1) then

    return "upgrade-arrow1"

  else

    return "upgrade-throwing-axe1"

  end

end

 

--

--  Upgrade missile 2 of the current race.

--

function AiUpgradeMissile2()

  if (AiGetRace() == race1) then

    return "upgrade-arrow2"

  else

    return "upgrade-throwing-axe2"

  end

end

 

--

--  Upgrade catapult 1 of the current race.

--

function AiUpgradeCatapult1()

  if (AiGetRace() == race1) then

    return "upgrade-ballista1"

  else

    return "upgrade-catapult1"

  end

end

 

--

--  Upgrade catapult 2 of the current race.

--

function AiUpgradeCatapult2()

  if (AiGetRace() == race1) then

    return "upgrade-ballista2"

  else

    return "upgrade-catapult2"

  end

end

 

--

--  Research of the current race.

--

function AiScientific()

  if (AiGetRace() == race1) then

    return "unit-inventor"

  else

    return "unit-alchemist"

  end

end

 

--

--  Stables of the current race.

--

function AiStables()

  if (AiGetRace() == race1) then

    return "unit-stables"

  else

    return "unit-ogre-mound"

  end

end

 

--

--  Temple of the current race.

--

function AiTemple()

  if (AiGetRace() == race1) then

    return "unit-church"

  else

    return "unit-altar-of-storms"

  end

end

 

--

--  Mage tower of the current race.

--

function AiMageTower()

  if (AiGetRace() == race1) then

    return "unit-mage-tower"

  else

    return "unit-temple-of-the-damned"

  end

end

 

--

--  Airport of the current race.

--

function AiAirport()

  if (AiGetRace() == race1) then

    return "unit-gryphon-aviary"

  else

    return "unit-dragon-roost"

  end

end

 

--

--  Barracks of the current race.

--

function AiBarracks()

  if (AiGetRace() == race1) then

    return "unit-human-barracks"

  else

    return "unit-orc-barracks"

  end

end

 

--

--  Soldier of the current race.

--

function AiSoldier()

  if (AiGetRace() == race1) then

    return "unit-footman"

  else

    return "unit-grunt"

  end

end

 

--

--  Shooter of the current race.

--

function AiShooter()

  if (AiGetRace() == race1) then

    return "unit-archer"

  else

    return "unit-axethrower"

  end

end

 

--

--  Elite Shooter of the current race.

--

function AiEliteShooter()

  if (AiGetRace() == race1) then

    return "unit-ranger"

  else

    return "unit-berserker"

  end

end

 

--

--  Cavalry of the current race.

--

function AiCavalry()

  if (AiGetRace() == race1) then

    return "unit-knight"

  else

    return "unit-ogre"

  end

end

 

--

--  Cavalry mages of the current race.

--

function AiCavalryMage()

  if (AiGetRace() == race1) then

    return "unit-paladin"

  else

    return "unit-ogre-mage"

  end

end

 

--

--  Mage of the current race.

--

function AiMage()

  if (AiGetRace() == race1) then

    return "unit-mage"

  else

    return "unit-death-knight"

  end

end

 

--

--  Catapult of the current race.

--

function AiCatapult()

  if (AiGetRace() == race1) then

    return "unit-ballista"

  else

    return "unit-catapult"

  end

end

 

--

--  Scout of the current race.

--

function AiScout()

  if (AiGetRace() == race1) then

    return "unit-balloon"

  else

    return "unit-zeppelin"

  end

end

 

--

--  Flyer of the current race.

--

function AiFlyer()

  if (AiGetRace() == race1) then

    return "unit-gryphon-rider"

  else

    return "unit-dragon"

  end

end

 

--

--  Tower of the current race.

--

function AiTower()

  if (AiGetRace() == race1) then

    return "unit-human-watch-tower"

  else

    return "unit-orc-watch-tower"

  end

end

 

--

--  Guard-Tower of the current race.

--

function AiGuardTower()

  if (AiGetRace() == race1) then

    return "unit-human-guard-tower"

  else

    return "unit-orc-guard-tower"

  end

end

 

--

--  Cannon-Tower of the current race.

--

function AiCannonTower()

  if (AiGetRace() == race1) then

    return "unit-human-cannon-tower"

  else

    return "unit-orc-cannon-tower"

  end

end

 

--

--  Harbor of the current race.

--

function AiHarbor()

  if (AiGetRace() == race1) then

    return "unit-human-shipyard"

  else

    return "unit-orc-shipyard"

  end

end

 

--

--  Refinery of the current race.

--

function AiRefinery()

  if (AiGetRace() == race1) then

    return "unit-human-refinery"

  else

    return "unit-orc-refinery"

  end

end

 

--

--  Foundry of the current race.

--

function AiFoundry()

  if (AiGetRace() == race1) then

    return "unit-human-foundry"

  else

    return "unit-orc-foundry"

  end

end

 

--

--  Ship armor 1 of the current race.

--

function AiUpgradeShipArmor1()

  if (AiGetRace() == race1) then

    return "upgrade-human-ship-armor1"

  else

    return "upgrade-orc-ship-armor1"

  end

end

 

--

--  Ship armor 2 of the current race.

--

function AiUpgradeShipArmor2()

  if (AiGetRace() == race1) then

    return "upgrade-human-ship-armor2"

  else

    return "upgrade-orc-ship-armor2"

  end

end

 

--

--  Ship weapon 1 of the current race.

--

function AiUpgradeShipCannon1()

  if (AiGetRace() == race1) then

    return "upgrade-human-ship-cannon1"

  else

    return "upgrade-orc-ship-cannon1"

  end

end

 

--

--  Ship weapon 2 of the current race.

--

function AiUpgradeShipCannon2()

  if (AiGetRace() == race1) then

    return "upgrade-human-ship-cannon2"

  else

    return "upgrade-orc-ship-cannon2"

  end

end

 

--

--  Platform of the current race.

--

function AiPlatform()

  if (AiGetRace() == race1) then

    return "unit-human-oil-platform"

  else

    return "unit-orc-oil-platform"

  end

end

 

--

--  Tanker of the current race.

--

function AiTanker()

  if (AiGetRace() == race1) then

    return "unit-human-oil-tanker"

  else

    return "unit-orc-oil-tanker"

  end

end

 

--

--  Submarine of the current race.

--

function AiSubmarine()

  if (AiGetRace() == race1) then

    return "unit-human-submarine"

  else

    return "unit-orc-submarine"

  end

end

 

--

--  Destroyer of the current race.

--

function AiDestroyer()

  if (AiGetRace() == race1) then

    return "unit-human-destroyer"

  else

    return "unit-orc-destroyer"

  end

end

 

--

--  Battleship of the current race.

--

function AiBattleship()

  if (AiGetRace() == race1) then

    return "unit-battleship"

  else

    return "unit-ogre-juggernaught"

  end

end

 

--

--  Transporter of the current race.

--

function AiTransporter()

  if (AiGetRace() == race1) then

    return "unit-human-transport"

  else

    return "unit-orc-transport"

  end

end

 

--

--  1st Elite Shooter of the current race.

--

function AiUpgradeEliteShooter()

  if (AiGetRace() == race1) then

    return "upgrade-ranger"

  else

    return "upgrade-berserker"

  end

end

 

--

--  1st Upgrade of elite Shooter of the current race.

--

function AiUpgradeEliteShooter1()

  if (AiGetRace() == race1) then

    return "upgrade-ranger-scouting"

  else

    return "upgrade-berserker-scouting"

  end

end

 

--

--  2nd Upgrade of elite Shooter of the current race.

--

function AiUpgradeEliteShooter2()

  if (AiGetRace() == race1) then

    return "upgrade-longbow"

  else

    return "upgrade-light-axes"

  end

end

 

--

--  3th Upgrade of elite Shooter of the current race.

--

function AiUpgradeEliteShooter3()

  if (AiGetRace() == race1) then

    return "upgrade-ranger-marksmanship"

  else

    return "upgrade-berserker-regeneration"

  end

end

 

--

--  Upgrade cavalry to cavalry mages of the current race.

--

function AiUpgradeCavalryMage()

  if (AiGetRace() == race1) then

    return "upgrade-paladin"

  else

    return "upgrade-ogre-mage"

  end

end

 

--

--  1st spell of the cavalry mages of the current race.

--

function AiCavalryMageSpell1()

  if (AiGetRace() == race1) then

    return "upgrade-healing"

  else

    return "upgrade-bloodlust"

  end

end

 

--

--  2nd spell of the cavalry mages of the current race.

--

function AiCavalryMageSpell2()

  if (AiGetRace() == race1) then

    return "upgrade-exorcism"

  else

    return "upgrade-runes"

  end

end

 

--

--  1st spell of the mages of the current race.

--

function AiMageSpell1()

  if (AiGetRace() == race1) then

    return "upgrade-slow"

  else

    return "upgrade-haste"

  end

end

 

--

--  2nd spell of the mages of the current race.

--

function AiMageSpell2()

  if (AiGetRace() == race1) then

    return "upgrade-flame-shield"

  else

    return "upgrade-raise-dead"

  end

end

 

--

--  3th spell of the mages of the current race.

--

function AiMageSpell3()

  if (AiGetRace() == race1) then

    return "upgrade-invisibility"

  else

    return "upgrade-whirlwind"

  end

end

 

--

--  4th spell of the mages of the current race.

--

function AiMageSpell4()

  if (AiGetRace() == race1) then

    return "upgrade-polymorph"

  else

    return "upgrade-unholy-armor"

  end

end

 

--

--  5th spell of the mages of the current race.

--

function AiMageSpell5()

  if (AiGetRace() == race1) then

    return "upgrade-blizzard"

  else

    return "upgrade-death-and-decay"

  end

end