Determine if unit will make metal from UnitDef

Determine if unit will make metal from UnitDef

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

Moderators: hoijui, Moderators

byronwall
Posts: 3
Joined: 12 Nov 2014, 04:15

Determine if unit will make metal from UnitDef

Post by byronwall »

I am working on an AI side project using the Java interface. Nothing too serious yet. I am trying to do as much work as possible using the UnitDefs instead of hard coding. I know I will meet a limit on this, but I wanted to check about units that make metal. For the life of me, I cannot find a method in the UnitDef that will return this. When I go look at the units for BA, I also cannot find an obvious spot in there other than the description: "Converts...".

Is it possible to determine if a unit makes metal? I have checked getExtractsResource, getMakesResource, getResourceMake, and getUpkeep and find all 0 for the Metal resource.

Am I missing something?

Thanks!
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Determine if unit will make metal from UnitDef

Post by PicassoCT »

In the Lua Enviroment you have the UnitDef Table, which contains all the infos about a unit
there is also UnitDefNames, which is indexed by the Units name (instead of the defid).

you should find what you search there..

Example Usage Hitpoints of a Unit

Code: Select all

hp=UnitDefNames["cormex"].maxdamage
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Determine if unit will make metal from UnitDef

Post by knorke »

I never got to resources with Java-AI but seems getResourceMake should be correct.
Do you pass it valid resourceID?

Maybe this is helpful? (from abma's AI)
https://github.com/abma/AGAI/blob/maste ... .java#L168
https://github.com/abma/AGAI/blob/maste ... e.java#L58

Another thing is that some mods do not even use the engine-side-resource tags anymore and instead do that via Lua. So the unitDef might produce 0 metal, but the mod then does its own scripting which would be "unreadable" by AI.
For example in BA there is file LuaRules\Gadgets\energy_conversion.lua (or similiar name), not sure how much that replaces the metalmakers with its own logic. So maybe see the unit-files in the mod if they really have metalMake and similiar tags. ( http://springrts.com/wiki/Gamedev:UnitDefs#Resources )
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Determine if unit will make metal from UnitDef

Post by Silentwings »

@byronwall: To make things clear, the point is here that the amount of metal a unit makes/uses in a given gameframe can be controlled via lua, so it can depend on ingame factors and can vary. The (constant) values inside the unitdef are just one of the inputs into how much resources a unit actually makes/uses. The lua API gives access to the net values on the current gameframe with Spring.GetUnitResources; maybe there is an equivalent function for AIs.
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determine if unit will make metal from UnitDef

Post by Anarchid »

The lua API gives access to the net values on the current gameframe with Spring.GetUnitResources; maybe there is an equivalent function for AIs.
This, however, would only work on already instantiated units. It would be of no use for the AI to determine if it's worth building the unit in the first place.

The FieldBOT AI is supposedly capable of playing a rather different set of TA-like games on no-metal maps, so it must be able to discover metal makers prior to building them. The method for picking the best resource-producing unit is probably of most interest. Note that you might have to use google translate on the russian comments.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Determine if unit will make metal from UnitDef

Post by Silentwings »

It looks as though that method hardcodes information about the game. I suspect that this is the only viable option when trying to determine which eco-producing units are worth building.

edit: BA entirely reimplements metal making logic here http://imolarpg.dyndns.org/trac/balates ... ersion.lua, which is the reason you can't find it in those particular unitdefs. I guess other games do similar stuff too.
Last edited by Silentwings on 12 Nov 2014, 20:35, edited 1 time in total.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Determine if unit will make metal from UnitDef

Post by gajop »

Silentwings wrote:It looks as though that method hardcodes information about the game. I suspect that this is the only viable option when trying to determine which eco-producing units are worth building.
Could try training it.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determine if unit will make metal from UnitDef

Post by Jools »

You should know that there are many ways a unit can "make metal": it can be a metal extractor, it can convert metal from energy, it can be an abandoned building under construction of it can be a builder reclaiming a wreck. Also every unit makes a little bit of metal just by being alive.
byronwall
Posts: 3
Joined: 12 Nov 2014, 04:15

Re: Determine if unit will make metal from UnitDef

Post by byronwall »

Thanks for all the responses. I will dig into the BA files to see what I can find. I was hoping to be able to assess the unit capabilities as much from the API as possible. It was working fine for all the other units I was trying on the resource front.

@knorke: I believe I was using the correct resource. I was iterating through (callback).getResources. It worked fine for the extractors and solar collectors all the others.

@Anarchid: I will look into that AI and see how much was hard coded there. Thanks for the link.

If it is necessary to hard code some game info into a config file or somewhere else for my AI to work, that's fine. Better to have it working.

Since I am just at the start here, would it be better to build this in lua instead of Java? Or will I face the same issues with information being set in game files outside of API calls?
playerO1
Posts: 57
Joined: 25 Jun 2014, 15:22

Re: Determine if unit will make metal from UnitDef

Post by playerO1 »

I have this problem too. I make special module (class) for detect some mod specification and translate some unique information to my bot.
I know that:
  • Balanced Anihilation (BA) and NOTA metal maker use LUA for make resources. Ower 70-80% of energy store resource convert to metal.
  • Tech Anihilation (TA) metal maker use LUA too, but convert level can control by player ingame (widget), and same metal maker have different convertion effective.
  • Evolution RTS use native metal maker - can get info from UnitDef.getMakesResource(Resource)
  • Zero-K use LUA metal extractor - extractor extract and make metall by special math formul.
On my BOT I make function getUnitResoureProduct(UnitDef def, Resource res) and create special class ModSpecification with hardcoden (maybe move it to XML) unit information for some mods.
Some of mod developers contain info in "CustomParams" (it renamed on last SPring version, don't know new name), but until Spring 98 acess to this from java is breaking.
Last edited by playerO1 on 13 Nov 2014, 19:01, edited 1 time in total.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determine if unit will make metal from UnitDef

Post by Jools »

Silentwings wrote: edit:BA entirely reimplements metal making logic here http://imolarpg.dyndns.org/trac/balates ... ersion.lua, which is the reason you can't find it in those particular unitdefs. I guess other games do similar stuff too.
We have made such a marvelous gadget that we guess everybody else must be using it.
Instead of guessing it's better to ask each games developer how metal makers work. In principle the unitdef tags listed above control the resource production, but they can be broken overridden by lua gadgets. In case of metal makers it's easier to hardcode those per game anyway. There are not so many metal converting units.
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determine if unit will make metal from UnitDef

Post by Anarchid »

Since I am just at the start here, would it be better to build this in lua instead of Java? Or will I face the same issues with information being set in game files outside of API calls?
Lua AI cannot be developed separately from the game they are run in, they are essentially part of the game rules; glorified gadgets in form and function. This means you will be tied to the game's developement process and its release cycle, and your AI will be distributed with the game archive.
Lua AI's are implicitly cheating and sometimes it's a lot of work to make them not use unfair information.
Lua AI's do have a broader access to gadgetized game rules, but there is no guarantee (not all gadgets share information even to other gadgets).
There are probably less libraries available for the higher-end tasks for Lua than for Java, and you cannot add native bindings to a Lua AI.
Game rules specific data can be exposed to native (including Java) AI's using (Game,Unit,Team)RulesParams and CustomParams.
Native AIs can send "messages" to game rules via SendSkirmishAIMessage, but this is not implemented for the java interface, which in turn means it's available for C/C++ API only.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Determine if unit will make metal from UnitDef

Post by FLOZi »

Depends whether you want a research project or something that can actually play all aspects of the game at hand.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Determine if unit will make metal from UnitDef

Post by Silentwings »

Jools wrote:
Silentwings wrote: blahblahblah
We have made such a marvelous gadget that we guess everybody else must be using it.
Actually it was written long ago by someone I never met. But...

https://code.google.com/p/xta-springrts ... sion.lua#5 *coughcoughcough* It seems XTA, amongst others, does use an energy conversion gadget that is a modifed version the (old) BA gadget. Nonetheless, this is all just off-topic.
Since I am just at the start here, would it be better to build this in lua instead of Java? Or will I face the same issues with information being set in game files outside of API calls?
In Lua you will have an easier time with finding API calls; the Lua API is better documented and (as I understand it, no offence meant if I'm wrong :wink:) has been maintained more actively in recent years. But as Anarchid pointed out there are other differences too.

If you are interested in AI in its own right, and you have a decent amount of time, then I guess the C/C++/java AI wrappers would suit you best. If you are interested primarily in Spring games and are just making an AI for fun, then I think you'd find Lua AIs quicker and easier.
I will dig into the BA files to see what I can find.
The energy->metal conversion gadget is http://imolarpg.dyndns.org/trac/balates ... ersion.lua. It contains the conversion ratios & capacties as a hardcoded list. If you would like that list to be exposed to Lua in general (and therefore accessible to a Lua AI) then just let me know.

That gadget controls all energy->metal conversion; it feeds newly converted metal straight into the teams resources (so there are no real time per-unit conversion stats, sorry!). There is also a widget http://imolarpg.dyndns.org/trac/balates ... ersion.lua that players can use to tell the gadget what level of conversion they want.

Metal extractors work in the "normal" way, using just the values from their unitdefs, and the same for all energy production.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determine if unit will make metal from UnitDef

Post by Jools »

Silentwings wrote: It seems XTA, amongst others, does use an energy conversion gadget that is a modifed version the (old) BA gadget. Nonetheless, this is all just off-topic
Except it's not off-topic, because the xta one does not alter team resources via lua, it only turns on/off metal makers, so in xta you can use unitdef-tags. (That whole crap should be a widget anyway. Yes, this part is off-topic.)
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Determine if unit will make metal from UnitDef

Post by Silentwings »

@Jools:
Jools wrote:
Silentwings wrote: edit:BA entirely reimplements metal making logic here http://imolarpg.dyndns.org/trac/balates ... ersion.lua, which is the reason you can't find it in those particular unitdefs. I guess other games do similar stuff too.
We have made such a marvelous gadget that we guess everybody else must be using it.
Instead of guessing it's better to ask each games developer how metal makers work.
The OP did just that; he asked about BA, to which I (as a BA developer) replied, moreover making it clear what part of my reply was BA specific and indicating as best I knew how to cope with games general. I personally have no interest or time to provide info on specifics of projects I don't work on (especially when it was not asked for) & imho in general often your replies to what appears to me as constructive discussion - in this and very many other threads - come across as intentionally offensive or simply derailments. I don't mean to cause any offence to you, but enough is enough and I will likely not read or reply to your posts in future.
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determine if unit will make metal from UnitDef

Post by Jools »

Silentwings wrote:I guess other games do similar stuff too.
I was just correcting incorrect or vague information, which in my opinion also came across as quite arrogant, hence the offensive response. Of course I would not react like this if this was the first time you write like this.

I don't have any problems with any other part of your response.
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determine if unit will make metal from UnitDef

Post by Anarchid »

If you would like that list to be exposed to Lua in general (and therefore accessible to a Lua AI) then just let me know.
May i suggest GameRulesParams instead? Since 98.0, native AI's can read them, so it puts both gadget and native bots on equal footing at the same time-cost.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: Determine if unit will make metal from UnitDef

Post by Silentwings »

GameRulesParams instead?
Aha, I guess this means that non-lua AIs cannot read GG? (I have never written one). So, yes :-)
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determine if unit will make metal from UnitDef

Post by Anarchid »

non-lua AIs cannot read GG
Yes, native AIs have no direct access to the Lua states.

Communication is possible with SendSkirmishAIMessage / RecvSkirmishAIMessage, but for one-way
communication on modern engines, G/T/URP is the way.

(bonus point is that it also allows this data for widgets)
Post Reply

Return to “AI”