Page 1 of 2

Shard Modules?

Posted: 16 Nov 2011, 21:28
by zoggop
I'm starting development of a Lua AI exclusively for BA. I have absolutely zero experience with Lua, so I'm stumbling around by first writing the backend functions the AI will need as widgets so that I can see what's going on. Thankfully there are a million examples on springfiles of working with Spring in Lua.

Anyway, this idea started because I was fiddling with Shard's BA task queue and attackers list. Perhaps rather than starting from scratch, I could extend Shard with the functionality I want my AI to have? It is modular, after all, though I don't fully understand how.

Some of the features I want my AI to have:

- goal-oriented global build list that is intelligently assigned to construction units. (by goal-oriented I mean say there is a goal of a list of attackers in response to the assessed enemy threat of a strategic location. the ai would extrapolate what is necessary to build said units and create a global build list.)

- threat matrix that is separated into ground, air, and submerged. this would be used to determine type of attacking forces, and where not to place an important building.

- threat matrix that only assesses what the ai has actually seen.

- economic importance matrix of both friendly and enemy buildings to determine where to place defensive structures and where to attack.

Because I've never done this before, I have a bad feeling I may be creating an AI with far too much processing overhead. :-P

AF: are these features that I could feasibly add to Shard, or would it be just as much work as starting from scratch? It seems a little silly to start from scratch when there already is a modular Lua AI.

Re: Shard Modules?

Posted: 16 Nov 2011, 21:43
by AF
Yes you could add these to Shard, think of the existing modules and behaviours as examples/starting points.

My only qualm is at the moment Shard for 0.83 under windows won't compile on my machine and I need to do a release

Re: Shard Modules?

Posted: 17 Nov 2011, 02:45
by SpliFF
I just posted a response to another topic with some information about a LuaAI i'm working on that has most of your requirements. It is still under heavy development but a lot of framework and support code already exists.

http://springrts.com/phpbb/viewtopic.ph ... 02#p505202

It started life as a widget (see the metalstorm ai) but I'm migrating it to a LuaAI framework caled Enigma. Parts of Enigma are based on C.R.A.I.G. which already deals with some low-level issues like grouping data into teams and running unsynced.

I already have code for:
* Complex threat/reward matrixes (ie, determine enemy AA strength in a region).
* Pathfinding (ie, find a path for planes to fly around that AA)
* Resource gathering (ie, find and cap metal and geo spots)
* Plans and Goals (ie, come up options and weight them against each other)
* Debugging (AI logging, dumping tables, drawing threatmaps and goal targets)
* Transporting (ie, units can call transports which respond by picking up units, dropping them off at a target and then flying home or to another job)

The main issue right now is the code on my website is very experimental, somewhat disorganised and not ready for 84.0. I have another version on my computer which is addressing some of these issues and I'll try to upload it within a few days.

Code license is GPL 2.0

Re: Shard Modules?

Posted: 17 Nov 2011, 02:48
by zoggop
I have begun with changing attackhandler.lua, and have encountered this:

Code: Select all

[f=0014232] <SkirmishAI: Shard dev-Isaac (team 0)>: ...ing/AI/Skirmish/Shard/dev-Isaac/ai/attackhandler.lua:173: attempt to index global 'Spring' (a nil value)
Line 173 is...

Code: Select all

local unitDefID= Spring.GetUnitDefID(unitID)
Which worked when I had the same line in a widget, so I must be missing something about how Shard (or an AI in general) interacts with Spring. How might I get the unit def ids (and defs)?

As for your compiling woes, I wish I could help :-(

SpliFF, that sounds very much like what I have in mind. Let me know when you upload it (I'd like to take a look).

Re: Shard Modules?

Posted: 17 Nov 2011, 03:02
by knorke
Which worked when I had the same line in a widget, so I must be missing something about how Shard (or an AI in general) interacts with Spring. How might I get the unit def ids (and defs)?
All the Spring.xxx function will not work in shard. So (some, not all) functions have counterparts in shard's Lua, so to speak.
ie Spring.GetUnitDefID might be engineunit:Type() or something like that.

Re: Shard Modules?

Posted: 17 Nov 2011, 03:42
by zoggop
Thanks, knorke. Well, now that I've had a look in Shard's api.lua, I'm not quite sure how to implement what I want to do. I need to get the range of all of a unit's weapons. Maybe I just need to make a table to convert shortnames to UnitDefIDs to use UnitDefs? Or if I can't access UnitDefs and WeaponDefs, I suppose I could just make a table (generate a text file with a widget) of unit weapon ranges.

Re: Shard Modules?

Posted: 17 Nov 2011, 04:49
by knorke
zoggop wrote:Well, now that I've had a look in Shard's api.lua, I'm not quite sure how to implement what I want to do. I need to get the range of all of a unit's weapons.
How to do it in Shard's api, I do not know.
With Spring Lua it goes like:

Code: Select all

function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)   
	local n = #UnitDefs[unitDefID].weapons  -- #bla returns number of entries in a table
	Spring.Echo ("number of weapons for this unit: "..n)
	for wi=1,n do
		local weaponDefID = UnitDefs[unitDefID].weapons[wi].weaponDef
		local wname = WeaponDefs[weaponDefID].name
		local wrange = WeaponDefs[weaponDefID].range
		Spring.Echo (wname .." - range=" .. wrange)
	end
end
Now whenever a unit is spawned that will print things like:
number of weapons for this unit: 2
dualcannon - range=600
singlecannon - range=400

Now the point is not that this is only possible with Spring gadget/widget Lua and that shard can not do it, I am pretty sure shard can get informations about a unit and its weapons.
But it was just so much more simple to go to the wiki and quickly look up the things I needed for this. And if that had not worked out I would have looked at some other examples that do things with weapons (eg there is a popular widget that draws the ranges for all turrets of the enemy players.)

Re: Shard Modules?

Posted: 17 Nov 2011, 16:24
by AF
At the moment all the weapons ranges cant be grabbed via Shard, only the maximum weapons range

Re: Shard Modules?

Posted: 18 Nov 2011, 09:47
by zoggop
But it was just so much more simple to go to the wiki and quickly look up the things I needed for this. And if that had not worked out I would have looked at some other examples that do things with weapons (eg there is a popular widget that draws the ranges for all turrets of the enemy players.)
Yup, this is the workaround I went for, using your bit of code as an example. I wrote a widget that just reads through the whole UnitDefs table (and some of the WeaponDefs) and spits out a definition of my own unitTable Lua table. (Perhaps it was simple and quick for you--for me it involved hours of research into which attributes I needed and how I needed to process them to get the right information. My god the UnitDefs table is sprawling. By the way, why is it that the .lua files in BA's units directory actually seem to have nothing to do with the UnitDefs that actually reside in-game? Am I missing something?) Which has led to much more intelligent attack targetting that ruthlessly destroys metal mines and power plants. Of course, I've now broken it by trying to add attack pathfinding that avoids threats... :-P

Re: Shard Modules?

Posted: 18 Nov 2011, 17:58
by knorke
zoggop wrote:By the way, why is it that the .lua files in BA's units directory actually seem to have nothing to do with the UnitDefs that actually reside in-game? Am I missing something?
The files in in the units\ folder define the ingame units,.. BUT:
One confusing thing is that the properties (sometimes called "tags") sometimes have different names in game files and ingame-UnitDef.
eg for hitpoints, in the unitdef file there is "maxDamage" but later in the ingame-UnitDef it is called "health."
Some tags might just be leftovers from TA that do nothing, though I think BA devs cleaned that up a bit. Still the "hitdensity"-tag in the wreckage defs is such a thing that does nothing.
Other ingame-UnitDef tags are not actually in the files but set by the engine on loading: eg a unit is given isTransport=true if its unitDef file contains all the tags needed for a transporter to work.
Same with isFactory, isBuilding etc.
Which has led to much more intelligent attack targetting that ruthlessly destroys metal mines and power plants.
That's cool. Maybe you want to document and post your experiments somewhere (here or in wiki) so others can learn from it. There are always people asking how/if Shard can do that and this but examples of working stuff seem rare. You might think "but I only want to publish it if everything is working!", but if people only posted 100% complete things then this forum would be very empty. ;)

Re: Shard Modules?

Posted: 20 Nov 2011, 09:48
by zoggop
Well, alright. Here's the only somewhat functional version thus far: https://github.com/zoggop/metalShard/downloads
Everything is the same as the Shard-dev that comes with Spring 0.83 except the attack targetting function has been changed to go for the least guarded metal mines and power plants, and what Core builds in BA. (Now if given the chance Core will even build an experimental gantry and use it.)

As for documentation, err...

Re: Shard Modules?

Posted: 21 Nov 2011, 03:37
by AF
zoggop wrote:Well, alright. Here's the only somewhat functional version thus far: https://github.com/zoggop/metalShard/downloads
Everything is the same as the Shard-dev that comes with Spring 0.83 except the attack targetting function has been changed to go for the least guarded metal mines and power plants, and what Core builds in BA. (Now if given the chance Core will even build an experimental gantry and use it.)

As for documentation, err...
Wait you got a Shard build with Spring v83? I've expressly forbidden bundling shard in the engine installer. Are you a Linux user?

Re: Shard Modules?

Posted: 21 Nov 2011, 18:34
by zoggop
Wait you got a Shard build with Spring v83? I've expressly forbidden bundling shard in the engine installer. Are you a Linux user?
At first I thought I had just copied it from one of the pre-0.83 test builds for Mac OS X, but I went and checked: it's actually bundled in the 0.83 OS X .app release. If it weren't for that I would have never known Shard existed. Just think, all my messy lua code could have been averted. :-P

Re: Shard Modules?

Posted: 22 Nov 2011, 13:35
by AF
haha well I suppose Ill let the OSX people off =p

Re: Shard Modules?

Posted: 25 Nov 2011, 21:58
by zoggop
Lol. Last I looked there were a total of 61 downloads of Spring 0.84 for OS X. I'm actually surprised there are even that many. :-P

Speaking of evolving task lists (http://springrts.com/phpbb/viewtopic.php?f=15&t=27155), that's exactly what I've been trying to do for the past few days. Right now the fitness criterion sucks (average economy over unit's lifetime minus economy before it was built) because I can't figure out how to get the data I want from within Shard's API. Is there a way to find out how much damage the AI's own units are dealing? There is UnitDamaged, but I assume that only gets called when friendly units are damaged. Also is there any way to issue guard or assist commands? Building everything without assistance in the mid and late game makes the AI lose rather quickly.

Re: Shard Modules?

Posted: 26 Nov 2011, 01:23
by AF
No, unit damaged and the other events do exactly what they say on the tin, else they'd be called friendlyUnitDamaged or friendlyUnitIdle. It's also why a lot fo functions have checks to make sure they don't attempt to do things like assign behaviours and task lists to enemy units and issue commands to enemies.

As for the damage statistic, Shard doesn't expose that data because Shard isn't given it either, those stats need to be collected, they're not handed to AIs on a silver platter ( which is how it should be, different people will have differing ideas of how to quantify these things )

Re: Shard Modules?

Posted: 26 Nov 2011, 04:31
by zoggop
Ah, thank you. I've just done some tests using game:SendToConsole(). I found that attacker (and engineattacker) is always nil unless both the attacker and the damaged unit are friendly (i.e. accidental splash damage). Also, UnitDamaged (whether it's in AI, UnitHandler, or Unit) only triggers if the AI's own units are damaged.

Re: Shard Modules?

Posted: 26 Nov 2011, 17:07
by AF
I can't investigate that any further at the moment ( Shard will not build with v84 locally so no working development environment at the moment ).

As such I cannot ascertain if it's the behaviour of the C++ AI interface or Shard itself.

If you're able to determine if it's Shard code or engine code, please post any bugs you find with the C++ API to mantis immediatley

Re: Shard Modules?

Posted: 26 Nov 2011, 23:45
by abma
please checkout https://github.com/Tarendai/Shard/commi ... 8824dd6e42 to make it compile with spring 84.0. had no time to fix it yet, sorry.

Re: Shard Modules?

Posted: 27 Nov 2011, 16:39
by AF
Are you sure thats the right link? Those are lua files ( and this isn't the main shard thread )