[solved]Determining if unit is factory

[solved]Determining if unit is factory

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

Moderators: hoijui, Moderators

Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

[solved]Determining if unit is factory

Post by Bla »

Given a UnitDef, what way is there to determine if it is a factory?

Also, if I have getGame().getResourceMapSpotsPositions(metal), how can I tell if there is already a metal extractor on the map?
Last edited by Bla on 13 Jul 2014, 08:21, edited 1 time in total.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Determining if unit is factory

Post by zwzsg »

  • isFactory is still a valid unitdef tag in Spring 96 : It's true for factory, false for non-factory.
  • Use a combination of Spring.GetUnitsInCylinder and of the unitdef tags extractRange, extractSquare extractsMetal (you're lucky, extractsMetal and extractRange are 0, not nil, for regular units).

The hard part is actually the getResourceMapSpotsPositions. You seem to imply you got it covered, but in case you haven't, or if you have trouble with the corner cases of metal maps, no metal maps, fuzzy cloud metal distributions, Lua'ed metal maps, speak up, it's common problem many game and widgeters have faced (with uneven results....).


Edit: Oops, just noticed you're in the AI, and not the Lua Scripts section. Though it should still holds.
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: Determining if unit is factory

Post by Bla »

zwzsg wrote:Oops, just noticed you're in the AI, and not the Lua Scripts section. Though it should still holds.
Yeah, because I'm making the AI as high high school computer science final project, I'm limited to Java, which is what we learned the the class, and there is no isFactory in the callback :(

http://spring.abma.de/doc-Interface-Jav ... /Unit.html
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Determining if unit is factory

Post by gajop »

Try:

Code: Select all

unitDef.isBuilder() && unitDef.getSpeed() == 0
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determining if unit is factory

Post by Jools »

gajop wrote:Try:

Code: Select all

unitDef.isBuilder() && unitDef.getSpeed() == 0
That would also return e.g NOTA:s stationary start unit, which builds units like a commander but there are no units coming out of it, so it's not a factory.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Determining if unit is factory

Post by gajop »

Jools wrote: That would also return e.g NOTA:s stationary start unit, which builds units like a commander but there are no units coming out of it, so it's not a factory.
If it builds units, then how are "no units coming out of it"?
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: Determining if unit is factory

Post by Jools »

Because it builds them like a builder, only it's not moving. The unit's are not started to be built inside it.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Determining if unit is factory

Post by gajop »

Ah, I understand what you meant now.
Well maybe add a

Code: Select all

unitDef.getBuildDistance()==0
check as well if you want to differentiate between the two?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Determining if unit is factory

Post by FLOZi »

Dunno if AI can access unitdef yardmap but its better than checking speed
Bla
Posts: 79
Joined: 25 Feb 2013, 14:44

Re: Determining if unit is factory

Post by Bla »

Hm, checking speed works! thanks gajop! FLOZi, I don't know what a yardmap is, nor do I think there is any mention of it in the java wrapper, so I don't think it is an option for a java AI :(
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Determining if unit is factory

Post by AF »

If a unit is a structure and it can build things, then yes it is a factory.

If a unit is not a structure and it has no speed, it may be a nanoturret or similar unit ( e.g. construction hubs in EEE ), limit the build search radius to its build radius or it will be stuck forever trying to reach the target location

If a unit is not a structure and its build speed is above zero, it is a normal builder
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determining if unit is factory

Post by Anarchid »

Unit is a "factory" when it can build other units which are not buildings and have movespeed > 0.

Yeah yeah necro award.
playerO1
Posts: 57
Joined: 25 Jun 2014, 15:22

Re: Determining if unit is factory

Post by playerO1 »

I make this function:

Code: Select all

    public static boolean isStationarFactory(UnitDef def) {
        return def.isBuilder() && (def.isAbleToMove() && (def.getSpeed()==0)) && !def.getBuildOptions().isEmpty();
    }
Constructor bot can select by def.isBuilder() && (def.isAbleToMove() && (def.getSpeed()>0)) && !def.getBuildOptions().isEmpty();
But same constructor cam make only defencive tower, then need check what he can do (getBuildOptions() and test ecoing coefficient), but it is long story...
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10452
Joined: 24 Jan 2006, 21:12

Re: Determining if unit is factory

Post by PicassoCT »

You could itterate over all building options of a builder and only return true for isFactory if for that unittype isMobileUnit is returning a true.

also thats the sort of operation you only want to do once, so better cache that gained knowledge in a ["unittype"]= isFactoryboolean dictionary.
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determining if unit is factory

Post by Anarchid »

isFactory
... is not available for Java AI.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10452
Joined: 24 Jan 2006, 21:12

Re: Determining if unit is factory

Post by PicassoCT »

Anarchid wrote:
isFactory
... is not available for Java AI.
which is why we discuss how to implement it by hand...


goto discussion above.
read(discussion);
reason();
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Determining if unit is factory

Post by AF »

isFactory wouldn't be reliable anyway.

Also, the type of unit that can be built isn't a reliable factor, e.g. the aliens in EEE are mobile units building mobile units, even TA Kingdoms had it.


here is a logic path to follow:

Code: Select all

if the unit can move
	It may be a construction unit, it may also be a construction hub or nano turret
	if the movement speed is 0
		It's a nano turret or a hub of some sort. Treat as a builder but locate a build position within the build radius centered on the units location
	else
		It's a construction unit
else
	it is a factory
I should remind people that:

- Factories have been known to build structures
- Just because the unit has a movement speed of zero, doesn't mean it's a factory/structure
- Mobile units can build other mobile units, entire games and factions have been built around this
- Hubs and Nano turrets can build other structures, including mobile units, and other nano turrets and hubs
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Determining if unit is factory

Post by Anarchid »

The algorithm above classifies e.g. reclaim-only workers as builders.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Determining if unit is factory

Post by AF »

Anarchid wrote:The algorithm above classifies e.g. reclaim-only workers as builders.
They are... check if they have any build items for that part
playerO1
Posts: 57
Joined: 25 Jun 2014, 15:22

Re: Determining if unit is factory

Post by playerO1 »

PicassoCT wrote:You could itterate over all building options of a builder and only return true for isFactory if for that unittype isMobileUnit is returning a true.
AF wrote:if they have any build items for that part
So isFactory by getBuildOptions().isEmpty() is bad becouse required CPU time, but check getBuildOptions().isEmpty() is the only way for check it true? he-he :-)
Post Reply

Return to “AI”