LuaAI information

LuaAI information

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

LuaAI information

Post by SpliFF »

I'm looking to gather as much information as I can about LuaAI.

So far the only mod I know of that lets you choose a LuaAI opponent is Spring Tanks and currently that's crashing for me. Other than that I can find very little information. I'm hoping anyone who has any info on LuaAI can post it in this thread and i'll organise it for my Spring Lua Manual and the Wiki.

Starting with the "easy" stuff?
* What files are required in addition to /LuaAI.lua
* Is it synced or unsynced, widget or gadget?
* How do you choose a LuaAI opponent in Spring and in SpringLobby / TASClient?
* Is it shipped in the engine or do games supply their own copy?
* Does documentation exist anywhere?
* Is Tobi still around and supporting it?

I'll be able to answer some of these questions myself once I locate the relevant parts of code but right now anything is welcome.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: LuaAI information

Post by Forboding Angel »

SpliFF wrote: Starting with the "easy" stuff?
* What files are required in addition to /LuaAI.lua
* Is it synced or unsynced, widget or gadget?
* How do you choose a LuaAI opponent in Spring and in SpringLobby / TASClient?
* Is it shipped in the engine or do games supply their own copy?
* Does documentation exist anywhere?
* Is Tobi still around and supporting it?
S44
ZK
Evolution RTS (if you can call that an "AI", I mean it is, but it's very rudimentary)
CT (iirc)

1. Dunno
2. Unsynced (Primarily, can communicate with synced tho)
3. Add Bot button
4. Included in the game
5. Wiki but dunno exactly where
6. Yes
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: LuaAI information

Post by SpliFF »

Thanks. I found the wiki entry
http://springrts.com/wiki/AI:Development:Lang:Lua

I managed to duplicate C.R.A.I.G as well and have it appear in the Add Bot list as a seperate A.I. It requires:

Code: Select all

LuaRules/Gadgets/craig.lua
LuaRules/Gadgets/craig/
LuaRules/Configs/craig/
Note that changing the name of the AI and/or the mod can break this AI. For example it uses the mods shortName to find the config file LuaRules/Configs/craig/<modname>/buildorder.lua and if the file doesn't exist the AI will crash and the game will end immediately.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

CT (iirc)
That is just a Test that does nothing but fly the flagship around and build miners to harvest. ( http://www.youtube.com/watch?v=aOtnOEOtVTM )
Afaik it is currently broken by changes to the game (units got renamed)

Kernel Panic has a Lua AI too.
* Is it synced or unsynced, widget or gadget?
Gadget and I think at least the initialise part must be synced. I guess the rest must be synced too. (otherwise local player changing his team or quitting would influence the AI?)

What I found a bit problematic on first try, my AI wanted to order all units, not just the ones on his team. (power greedy bastard!)
So i added

Code: Select all

local uteam = Spring.GetUnitTeam (unit)
if (myTeam[uteam]==true) then
--do stuff
else
--not my unit
But supposedly a better way to do this is to use CallAsTeam, dont know how exactly it would be used though...
Is it shipped in the engine or do games supply their own copy?
Included with game. LuaAIs outside games do not seem to be possible.
Does documentation exist anywhere?
http://springrts.com/wiki/AI:Development:Lang:Lua
http://springrts.com/phpbb/viewtopic.ph ... 6&p=229532
Basically just make a gadget and use functions from
http://springrts.com/wiki/Lua_SyncedRead#GetUnits
ie Spring.GetUnitNearestEnemy to get information and then use
Spring.GiveOrderToUnit from
http://springrts.com/wiki/Lua_SyncedCtrl#Give_Order to give commands to your units. (attack, move, build etc)
Commands are here: http://springrts.com/wiki/Lua_CMDs

Moving a unit to x,y,z:
Spring.GiveOrderToUnit(unitID, CMD.MOVE , {x,y,z }, {})

Making a unit attack an enemy:
Spring.GiveOrderToUnit(unitID, CMD.ATTACK , { enemy }, {})

Making a factory build something:
Spring.GiveOrderToUnit(unitID, -UnitDefNames["name of unit to build"].id, {}, {})
Notice the minus sign.
I think if you want a mobile builder to build something, the coordinates would go in the first {}

Using custom commands
Spring.GiveOrderToUnit(unitID,31210,{UnitDefNames["bmetaltruckdepot"].id},{})
The number 31210 is the commandID of the custom command. No idea how this exactly works but it must match the number in the gadget. (in this case, 31210 was the number for morph/deploying units)

(typing this post just doubled the documentation on Lua AI)

The AI from Spring Tanks is really basic but it is just two small file so maybe good example:
LuaRules\Gadgets\tp_AIbork.lua
LuaAI.lua (in root folder)
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: LuaAI information

Post by hoijui »

yeah. LuaAI's are synced. they run on every machine, and have to create equal results everywhere. native AIs are unsynced.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: LuaAI information

Post by Kloot »

LuaAI's can run in synced (gadget) and/or unsynced (gadget/widget) code.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

While it is possible to make an "AI widget" (ie like some helper widgets who order units around) would you also be able to add these AIs in the lobby?
And it does not really make sense or does it?

Oh and I was wondering, is there docu on this CallAsTeam thing?
I imagine it probally works something like Spring.CallAsTeam (Spring.GetNearestEnemy())
but how would it look like for callins ie UnitDestroyed?
Or would I just have to put the callin in unsynced part to prevent the AI from accidently cheating?
Last edited by knorke on 19 Mar 2011, 12:46, edited 1 time in total.
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: LuaAI information

Post by SpliFF »

How would it run in a widget? That would imply it has to latch on to at least one real player to act as its host right? Are you suggesting it could load as a gadget stub and then be 'remote controlled' by a widget? I think the concept might have some merits but I suspect it would be tricky to implement.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: LuaAI information

Post by FLOZi »

Kloot wrote:LuaAI's can run in synced (gadget) and/or unsynced (gadget/widget) code.
Pretty sure CRAIG is indeed a mix of synced/unsynced gadget code.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: LuaAI information

Post by Kloot »

knorke wrote:While it is possible to make an "AI widget" (ie like some helper widgets who order units around) would you also be able to add these AIs in the lobby?
And it does not really make sense or does it?
No, that does not make sense. Lobbies only parse LuaAI.lua so players (teams) can select Lua AI's by name. They then pass that information to the engine (through script.txt), but any AI written as a gadget still has to check which teams actually want to run it (via Spring.GetTeamLuaAI), since gadgets are _always_ loaded for all players, unlike native AI's. Widgets can obviously just be (de)activated at will.
knorke wrote: Oh and I was wondering, is there docu on this CallAsTeam thing?
I imagine it probally works something like Spring.CallAsTeam (Spring.GetNearestEnemy())
but how would it look like for callins ie UnitDestroyed?
Or would I just have to put the callin in unsynced part to prevent the AI from accidently cheating?
http://springrts.com/wiki/Lua_System#LuaGadgets_only

Ordinary callouts in synced gadgets already have full access, so this is mostly useful on the unsynced side to get around per-allyteam LOS viewing restrictions. The engine itself takes care of restricting information to unsynced callins (by only executing them for in-the-know teams), so an AI can't accidentally cheat that way unless events are being forwarded with SendToUnsynced and friends.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: LuaAI information

Post by Tobi »

FLOZi wrote:
Kloot wrote:LuaAI's can run in synced (gadget) and/or unsynced (gadget/widget) code.
Pretty sure CRAIG is indeed a mix of synced/unsynced gadget code.
Correct.

Synced part is just a small proxy that actually gives commands to units. The unsynced part does the actual work (on a single client only) and then sends messages to the synced part (on all clients).
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: LuaAI information

Post by SpliFF »

How does CRAIG choose which client to run the unsynced part on?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

ok, thanks.
So to really prevent the AI from accidently cheating, every GiveOrder(), GetUnit() etc would have to be "wrapped" with CallAsTeam ()?
Unless there is a way to somehow use CallAsTeam on the whole gadget?
The engine itself takes care of restricting information to unsynced callins (by only executing them for in-the-know teams), so an AI can't accidentally cheat that way
My AI is all synced at the moment. So should I divide in sync/unsync and use callins only in unsynced?

It would be nice to have an option to have Lua AIs more "capsuled", at the moment it seems the AI maker has to pay attention to not to cheat (or even worse: given orders to enemy units) on his own?
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: LuaAI information

Post by Tobi »

SpliFF wrote:How does CRAIG choose which client to run the unsynced part on?
It's based on the teamleader field in the startscript.

https://github.com/tvo/craig/blob/maste ... k.lua#L208
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: LuaAI information

Post by SpliFF »

What exactly constitutes a 'team leader'? How would this handle FFA in the event of say.. 2 real PCs/players and 3 AI teams? It kind of sounds like if the AI is not on the same team as at least one human player it bails? That can't be right though or you wouldn't be able to vs it singleplayer.

For that matter I'm also a bit unsure how the gadget deals with more than 1 AI of the same type. Presumably then you'd have 1 gadget handling several teams and you would therefore have to segregate the data (like threatmaps) according (ie, teams[teamID].value) ?

If I call gl.Text() in the unsynced part of your gadget whose screen am I writing too? Everybodies? Just the 'team leader's ?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

SpliFF wrote:For that matter I'm also a bit unsure how the gadget deals with more than 1 AI of the same type. Presumably then you'd have 1 gadget handling several teams and you would therefore have to segregate the data (like threatmaps) according (ie, teams[teamID].value)
I was wondering that too and did a test by putting this into the synced part of a Lua AI:

Code: Select all

counter = 0
function gadget:GameFrame(frame)
	if (frame % 30 ==0) then
		counter=counter+1		
	end
	
	if ((frame-1) % 30 ==0) then
		Spring.Echo ("counter=" .. counter)
	end
With 3 AIs of the same type ingame it printed:

[f=0000001] counter=1
[f=0000031] counter=2
[f=0000061] counter=3
[f=0000091] counter=4
[f=0000121] counter=5
[f=0000151] counter=6
[f=0000181] counter=7
[f=0000211] counter=8
[f=0000241] counter=9
[f=0000271] counter=10

So to me it seems it is just a gadget that is run only once and there is only one "space" for variables. So seperating with something like teams[teamID].value is needed.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: LuaAI information

Post by Tobi »

Technically a LuaAI is nothing more than the functionality to mark a team as `running' a LuaAI, and a way to expose a list of LuaAIs to lobbies (unitsync, LuaAI.lua).

It is then up to an otherwise normal gadget to go through all teams, read this value and optionally do whatever it wants with it. (For example running an AI ;-))
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

Ok, what would be a good way to "capsle" AI data so that multiple Lua AIs of the same type do not influence each other?
Writing every variable as blub[teamID] instead of just blub seems not so nice.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: LuaAI information

Post by hoijui »

Use classes, and make one instance of a kind of context-class per AI team.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: LuaAI information

Post by knorke »

classes in mah lua? zomg. *googlegoog*
Post Reply

Return to “AI”