Page 1 of 2

Noob help with lua ai

Posted: 28 Aug 2013, 13:42
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

Re: Noob help with lua ai

Posted: 28 Aug 2013, 14:59
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)

Re: Noob help with lua ai

Posted: 28 Aug 2013, 15:07
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()

Re: Noob help with lua ai

Posted: 02 Sep 2013, 10:36
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?

Re: Noob help with lua ai

Posted: 02 Sep 2013, 11:03
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.

Re: Noob help with lua ai

Posted: 02 Sep 2013, 11:12
by Petah
I want to debug a lua table, is there an easy way to do that?

Re: Noob help with lua ai

Posted: 02 Sep 2013, 11:41
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).

Re: Noob help with lua ai

Posted: 02 Sep 2013, 20:54
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

Re: Noob help with lua ai

Posted: 02 Sep 2013, 21:05
by gajop
negative, that will only print a table of simple types, it will not print a table that contains other tables

Re: Noob help with lua ai

Posted: 02 Sep 2013, 21:22
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

Re: Noob help with lua ai

Posted: 03 Sep 2013, 00:53
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!

Re: Noob help with lua ai

Posted: 03 Sep 2013, 12:11
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).

Re: Noob help with lua ai

Posted: 03 Sep 2013, 14:59
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.

Re: Noob help with lua ai

Posted: 03 Sep 2013, 15:02
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.

Re: Noob help with lua ai

Posted: 03 Sep 2013, 19:45
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?

Re: Noob help with lua ai

Posted: 03 Sep 2013, 19:53
by Kloot

Code: Select all

t = {}; t[t] = t -- don't do this

Re: Noob help with lua ai

Posted: 05 Sep 2013, 09:04
by Forboding Angel
Bonus brownies if you make it play evo ;p

Re: Noob help with lua ai

Posted: 05 Sep 2013, 11:55
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?

Re: Noob help with lua ai

Posted: 05 Sep 2013, 12:35
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.

Re: Noob help with lua ai

Posted: 06 Sep 2013, 03:41
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.";