Noob help with lua ai

Noob help with lua ai

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

Moderators: hoijui, Moderators

User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Noob help with lua ai

Post by Petah »

So I'm looking at trying to reimplement BAI into lua.

Java I have no problem with, but lua is a bit of a learning curve for me.

A few questions to get me started:

In the lua ai tutorial/wiki it gives this snippet:

Code: Select all

function gadget:GameStart() 
    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
To clarify:
line 1 is a call in (event/function)?
line 2 is basically for each team as key (_) value (t)
line 3 is setting local variables based of an array (table?) return from Spring.GetTeamInfo

Is there any good lua intros? I had a look through the lua site docs, but is there anything better?

Is this complete/accurate: http://springrts.com/wiki/LuaCallinReturn

How do I reload a lua AI? Is it /luarules reload

Is there a callin for when an AI is reloaded?

Is there a callin for when Spring is finished loading but the game is not started?

Thanks
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Noob help with lua ai

Post by knorke »

line 1:
yes, it is a callin function.

line 2:
yes. the _ means that varible does not get used. (you could put i for index there)

line 3:
GetTeamInfo returns info about a team.
http://springrts.com/wiki/Lua_SyncedRea ... sts.2FInfo
Here only important thing is the isAI value, which is true if the engine/lobby has assigned an AI to controll this team.
The LuaAI has to keep track which teams unit is supposed to control and which ones belong to other AIs and human players.

There is "Hello World Lua AI" thread:
http://springrts.com/phpbb/viewtopic.php?f=15&t=29302

Not guaranteed to be 100% but good enough for most uses.
How do I reload a lua AI? Is it /luarules reload
/luarules reload reloads all gadgets and since Lua AI is a gadget, can use that. Not sure if there is a special "/luaai reload" it would sure be helpful.
Is there a callin for when an AI is reloaded?
Like for any gadget, gadget:Initialize() gets called.
Is there a callin for when Spring is finished loading but the game is not started?
Initialize() again

By the way imo gadgets or LuaAI are hardest to start with since you not only need to learn Lua but also the "spring things" too.
imo for learning this is nice:
http://www.lua.org/demo.html
c&p snippets from tutorials into it to see how loops etc work.
It is faster to test there than in spring.
Then make spring widget and use some spring specifique things. (like counting units etc)
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: Noob help with lua ai

Post by SirMaverick »

line 1 is a call in (event/function)?
Yes.
line 2 is basically for each team as key (_) value (t)
.
See http://springrts.com/wiki/Lua_SyncedRead#Teams
GetTeamList returns an array. The ipairs iterates through it. Each time you have an index and a value. Variables call "_" are ignored, you are declaring that you don't need that value. In this example you only care about the team number, not the index.
line 3 is setting local variables based of an array (table?) return from Spring.GetTeamInfo
It's not an array. It returns a list of variables.
Is there any good lua intros? I had a look through the lua site docs, but is there anything better?
Dunno. Look at the existing mods and their widgets/gadgets.
Mostly. I often use the engine source code as a reference. Most accurate.
How do I reload a lua AI? Is it /luarules reload
Depends on your implementation. If you implemented it as a gadget then /luarules reload will reload your gadget that implements the AI.
Is there a callin for when Spring is finished loading but the game is not started?
GamePreload()
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: Noob help with lua ai

Post by Petah »

Thanks for the help, a few more questions:

Is there a call in when /luarules reload is run?

And is it possible to load lua libraries (like pl.pretty.dump)? Or is there a good way to dump debug information?
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Noob help with lua ai

Post by Anarchid »

You can Spring.Echo for anything simple, and yes, you can load libraries, assuming they're written in pure lua though. I remember using a json library to cheat the "strings only" limitation on CustomParams.

I think you should be able to detect luarules reload by checking if gadget "initializes" at frames > 0. There might be better ways.
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: Noob help with lua ai

Post by Petah »

I want to debug a lua table, is there an easy way to do that?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Noob help with lua ai

Post by gajop »

If by debug you mean print, get this file: https://github.com/gajop/Toolbox/blob/m ... etable.lua, include it, and use table.echo(myTable).
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Noob help with lua ai

Post by FLOZi »

Or just:

Code: Select all

for k,v in pairs(mytable) do Spring.Echo(k,v) end

Metatables such as unitdefs should be handled slightly differently
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Noob help with lua ai

Post by gajop »

negative, that will only print a table of simple types, it will not print a table that contains other tables
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Noob help with lua ai

Post by FLOZi »

gajop wrote:negative, that will only print a table of simple types, it will not print a table that contains other tables
I'm well aware of that, but it's enough in most cases, and simpler than using an include.

Code: Select all

function PrintTable(input)
  for k,v in pairs(input) do
    Spring.Echo(k,v)
    if type(v) == "table" then
      PrintTable(v)
    end
end
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Noob help with lua ai

Post by zwzsg »

But if your tables of tables loop, that is, if an inner table has an outter table for element, then your code make the Lua hang!
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: Noob help with lua ai

Post by Petah »

Ok, so I tried using gajop's library, but I can't figure out how to include it.

I tried all of:

Code: Select all

VFS.Include("luarules/bai/savetable.lua");
VFS.Include("bai/savetable.lua");
VFS.Include("LuaRules/bai/savetable.lua");
include("luarules/bai/savetable.lua");
include("bai/savetable.lua");
All failed. Is the path case sensitive, and is it OS dependant for slashes? How do I include a library, and where do I put it?

I have basically extracted BA to ba.sdd and am putting my AI in luarules/gadgets/bai.lua and trying to put libraries in luarules/gadgets/bai/lib.lua

Also when I clear the infolog.txt and do a /luarules reload I get some weird output (see attached screenshot). Is there a way to follow specific output to infolog.txt? I tried tail -f infolog.txt | grep bai but that didnt work (im on windows).
Attachments
nulnulnul.png
(182.02 KiB) Not downloaded yet
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Noob help with lua ai

Post by gajop »

If you put the library in the root of the project like I did (not saying it's a good thing), then you can include it by typing VFS.Include("savetable.lua"), like I did here.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Noob help with lua ai

Post by gajop »

The tail and grep commands are something you can do only on Linux or other Unix-like environments (installing bash on Windows would get you there).
PS: Instead of a screenshot you should really attach the file instead.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Noob help with lua ai

Post by FLOZi »

zwzsg wrote:But if your tables of tables loop, that is, if an inner table has an outter table for element, then your code make the Lua hang!
Is this permissible in lua?
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: Noob help with lua ai

Post by Kloot »

Code: Select all

t = {}; t[t] = t -- don't do this
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Noob help with lua ai

Post by Forboding Angel »

Bonus brownies if you make it play evo ;p
User avatar
Petah
Posts: 426
Joined: 13 Jan 2008, 19:40

Re: Noob help with lua ai

Post by Petah »

gajop wrote:If you put the library in the root of the project...
Puting savetable.lua in the root worked. But I noticed adding files requires a restart of Spring, rather than just /luarules reload.

I ended up using the vardump method found here: http://www.lua.org/gems/vardump.lua

Works like a treat.
gajop wrote:The tail and grep commands are something you can do only on Linux or other Unix-like environments (installing bash on Windows would get you there).
I'm using the native Windows ports of grep/tail.
http://gnuwin32.sourceforge.net/packages/grep.htm
http://tailforwin32.sourceforge.net/
gajop wrote:PS: Instead of a screenshot you should really attach the file instead.
The start of the file is just filled with null bytes. I assume this is because I was clearing it while Spring was running. Spring would have just filled it up to the point it last wrote. Which was also the likely crash for tail/grep.
Forboding Angel wrote:Bonus brownies if you make it play evo ;p
How much like BA is EVO? BAI is primarily for BA (I know its a bit ambiguous by the name).

Anyway you can follow my progress here if you are so inclined:
https://github.com/Petah/bai-lua
And this is the (defunc) Java AI I'm rewriting:
https://github.com/Petah/bai

Quick question: How do I set the version of a Lua AI?

Also how do I tell if a unit is idle? Do I need to count Spring.GetUnitCommands?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Noob help with lua ai

Post by gajop »

Petah wrote: Puting savetable.lua in the root worked. But I noticed adding files requires a restart of Spring, rather than just /luarules reload.
Yep, known but seems no one minds, related to this: http://springrts.com/phpbb/viewtopic.php?f=23&t=28883.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Noob help with lua ai

Post by knorke »

Also how do I tell if a unit is idle?
There is callin:UnitIdle() --> http://springrts.com/wiki/LuaCallinReturn#Unit-Specific:
Guess that is supposed to find idle units.
Though when I tried to use it I was not sure what it considers "idle."
Do I need to count Spring.GetUnitCommands?
yes, to check if a unit is idle if #Spring.GetUnitCommands(unitID) >0.. should work.
Though if you have for example a mobile jammer jamming somewhere, that check will see it is as idle while it could be debatted if such unit is really idle.
How do I set the version of a Lua AI?
spring\rts\ExternalAI\LuaAIImplHandler.cpp says:
ii.valueTypeString = "<not-versioned>";
ii.desc = "Lua Skirmish AIs do not have a version, "
"because they are fully defined by the mods version already.";
Post Reply

Return to “AI”