AI:Development:Lang:Lua: Difference between revisions
→References: adding Spring Tank AI |
|||
| (13 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
[[Engine_Development|Development]] < [[AI:Development|AI Development]] < Lua AI Development | [[Engine_Development|Development]] < [[AI:Development|AI Development]] < Lua AI Development | ||
<h1>Writing a Lua AI</h1> | |||
== Introduction == | |||
=== Lua AIs are different === | |||
Lua for AIs is quite different from all the other AI languages supported by spring. Lua AIs use a completely different interface, and they can not be shipped separately, but only bundled with a mod. | Lua for AIs is quite different from all the other AI languages supported by spring. Lua AIs use a completely different interface, and they can not be shipped separately, but only bundled with a mod. | ||
=== Things to know === | |||
* They have to be bundled with a mod. Therefore, you should consider contacting the mod | * They have to be bundled with a mod. Therefore, you should consider contacting the mod developer(s) in question early, to see if they are at all interested in an AI. | ||
* A Lua AI is actually a regular gadget. The difference is just that it is listed in the mods LuaAI.lua file. See [[Lua_Scripting|Lua Scripting]] for details. | * A Lua AI is actually a regular gadget. The difference is just that it is listed in the mods LuaAI.lua file. See [[Lua_Scripting|Lua Scripting]] for details. | ||
* This brings up the problem that AIs can "accidentally" cheat or even move units of the enemy! It is up to the AI writer to avoid that. All AI players of the same type also share the same variables and tables. One way to avoid that is to put everything into a teamdata[teamID] table. Or the use of classes. | |||
== How to start == | |||
For a start, choose a | For a start, choose a single mod you want your AI to work with. | ||
You will also want to contact the mods | You will also want to contact the mods developer(s) before you start to code, | ||
or soon thereafter. | or soon thereafter. | ||
| Line 26: | Line 27: | ||
Source code of these AIs can be found in the mod archives, which you may open with 7zip, or it may be available separately (see [[#References|References]]). | Source code of these AIs can be found in the mod archives, which you may open with 7zip, or it may be available separately (see [[#References|References]]). | ||
=== Integrating the AI Gadget === | |||
This section explains everything you have to do to integrate your Lua AI into a mod. | This section explains everything you have to do to integrate your Lua AI into a mod. Which at the same time is everything that separates a Lua AI form a gadget. | ||
In the root of the mods archive, you need the file ''LuaAI.lua'', | In the root of the mods archive, you need the file ''LuaAI.lua'', | ||
| Line 57: | Line 58: | ||
end | end | ||
function gadget:GameStart() | function gadget:GameStart() | ||
-- Initialise AI for all teams that are set to use it | -- Initialise AI for all teams that are set to use it | ||
for _,t in ipairs(Spring.GetTeamList()) do | for _,t in ipairs(Spring.GetTeamList()) do | ||
local _,_,_, | local _,_,_,isAI,side = Spring.GetTeamInfo(t) | ||
if Spring.GetTeamLuaAI(t) == gadget:GetInfo().name then | if Spring.GetTeamLuaAI(t) == gadget:GetInfo().name then | ||
Spring.Echo("Team "..t.." assigned to "..gadget:GetInfo().name) | |||
local pos = {} | local pos = {} | ||
local home_x,home_y,home_z = Spring.GetTeamStartPosition(t) | local home_x,home_y,home_z = Spring.GetTeamStartPosition(t) | ||
| Line 70: | Line 70: | ||
end | end | ||
</code> | </code> | ||
'''Note:''' | |||
The above example will not work if your AI controls multiple teams because ''home_x,home_y,home_z'' are not stored ''per team.'' | |||
That's it, the rest of the game is using regular gadget functions, and in particular [http://springrts.com/wiki/Lua_SyncedCtrl#Give_Order Spring.GiveOrderToUnit()]. Now you should be able to use this AI when starting the modified mod in the lobby, when you click on the ''Add Bot'' button. From Spring 95.0 onwards, lua AIs can be selected from the Spring.exe menu. | |||
Now you should be able to use this AI when starting the modified mod in the lobby, when you click on the ''Add Bot'' button. | |||
Enjoy! | Enjoy! | ||
=== | == References == | ||
* NOE AI: http://springrts.com/phpbb/viewtopic.php?f=55&t=28259 | |||
* ST AI source: http://pastebin.com/v1kNRBDj | |||
* KP AI source: http://pastebin.com/f771164a0 | * KP AI source: http://pastebin.com/f771164a0 | ||
* CRAIG source: | * CRAIG source: | ||
** to browse: http://github.com/tvo/craig/tree/master | ** to browse: http://github.com/tvo/craig/tree/master | ||
** for git: git://github.com/tvo/craig.git | ** for git: git://github.com/tvo/craig.git | ||
C.R.A.I.G. is inside S44 and SWIW | * C.R.A.I.G. is inside S44 and SWIW | ||
* forum thread: [http://springrts.com/phpbb/viewtopic.php?f=15&t=25639 LuaAI information] | |||
* forum threads about an AI for the game Conflict Terra: [http://springrts.com/phpbb/viewtopic.php?f=63&t=26148] [http://springrts.com/phpbb/viewtopic.php?f=63&t=26214] [http://springrts.com/phpbb/viewtopic.php?f=15&t=26207] (among the OT, some more or less common LuaAI problems are discussed) | |||
[[Category: AI | [[Category: AI Dev]] | ||
Latest revision as of 02:54, 25 August 2014
Development < AI Development < Lua AI Development
Writing a Lua AI
Introduction
Lua AIs are different
Lua for AIs is quite different from all the other AI languages supported by spring. Lua AIs use a completely different interface, and they can not be shipped separately, but only bundled with a mod.
Things to know
- They have to be bundled with a mod. Therefore, you should consider contacting the mod developer(s) in question early, to see if they are at all interested in an AI.
- A Lua AI is actually a regular gadget. The difference is just that it is listed in the mods LuaAI.lua file. See Lua Scripting for details.
- This brings up the problem that AIs can "accidentally" cheat or even move units of the enemy! It is up to the AI writer to avoid that. All AI players of the same type also share the same variables and tables. One way to avoid that is to put everything into a teamdata[teamID] table. Or the use of classes.
How to start
For a start, choose a single mod you want your AI to work with. You will also want to contact the mods developer(s) before you start to code, or soon thereafter.
Things you need to get comfortable with:
- the Lua scripting language
- Lua scripting for spring (gadgets)
You may combine learning Lua for spring with writing simplistic gadgets or widgets for spring, and publishing them in the forum, as this way you get practical training and feedback about your code from the pros, plus the community eventually gets a usefull addon. When you start feeling comfortable, you should inspect the code of Lua AIs that are already available. Source code of these AIs can be found in the mod archives, which you may open with 7zip, or it may be available separately (see References).
Integrating the AI Gadget
This section explains everything you have to do to integrate your Lua AI into a mod. Which at the same time is everything that separates a Lua AI form a gadget.
In the root of the mods archive, you need the file LuaAI.lua,
which should look about like this:
-- file: LuaAI.lua
local listOfLuaAIs = {
{
name = "MyFirstAI",
desc = "Beats even the best players with Core, most others with Arm too",
},
}
return listOfLuaAIs
Besides all the other stuff you need for a gadget (you should know this by now),
you will need something like this in your code (eg under {mod_root}/LuaRules/Gadgets/MyFirstAI_v01.lua):
function gadget:GetInfo()
return {
name = "MyFirstAI",
desc = "An AI that knows how to play Mod X",
author = "John Doe",
date = "2020-12-31",
license = "Public Domain",
layer = 82,
enabled = true
}
end
function gadget:GameStart()
-- Initialise AI for all teams that are set to use it
for _,t in ipairs(Spring.GetTeamList()) do
local _,_,_,isAI,side = Spring.GetTeamInfo(t)
if Spring.GetTeamLuaAI(t) == gadget:GetInfo().name then
Spring.Echo("Team "..t.." assigned to "..gadget:GetInfo().name)
local pos = {}
local home_x,home_y,home_z = Spring.GetTeamStartPosition(t)
end
end
end
Note:
The above example will not work if your AI controls multiple teams because home_x,home_y,home_z are not stored per team.
That's it, the rest of the game is using regular gadget functions, and in particular Spring.GiveOrderToUnit(). Now you should be able to use this AI when starting the modified mod in the lobby, when you click on the Add Bot button. From Spring 95.0 onwards, lua AIs can be selected from the Spring.exe menu.
Enjoy!
References
- NOE AI: http://springrts.com/phpbb/viewtopic.php?f=55&t=28259
- ST AI source: http://pastebin.com/v1kNRBDj
- KP AI source: http://pastebin.com/f771164a0
- CRAIG source:
- to browse: http://github.com/tvo/craig/tree/master
- for git: git://github.com/tvo/craig.git
- C.R.A.I.G. is inside S44 and SWIW
- forum thread: LuaAI information
- forum threads about an AI for the game Conflict Terra: [1] [2] [3] (among the OT, some more or less common LuaAI problems are discussed)