Page 1 of 2

[solved]Determining if unit is factory

Posted: 15 May 2014, 00:28
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?

Re: Determining if unit is factory

Posted: 15 May 2014, 01:35
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.

Re: Determining if unit is factory

Posted: 15 May 2014, 03:56
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

Re: Determining if unit is factory

Posted: 15 May 2014, 09:03
by gajop
Try:

Code: Select all

unitDef.isBuilder() && unitDef.getSpeed() == 0

Re: Determining if unit is factory

Posted: 15 May 2014, 09:19
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.

Re: Determining if unit is factory

Posted: 15 May 2014, 09:25
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"?

Re: Determining if unit is factory

Posted: 15 May 2014, 09:54
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.

Re: Determining if unit is factory

Posted: 15 May 2014, 10:03
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?

Re: Determining if unit is factory

Posted: 15 May 2014, 18:49
by FLOZi
Dunno if AI can access unitdef yardmap but its better than checking speed

Re: Determining if unit is factory

Posted: 16 May 2014, 08:20
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 :(

Re: Determining if unit is factory

Posted: 16 May 2014, 15:31
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

Re: Determining if unit is factory

Posted: 07 Jul 2014, 15:40
by Anarchid
Unit is a "factory" when it can build other units which are not buildings and have movespeed > 0.

Yeah yeah necro award.

Re: Determining if unit is factory

Posted: 07 Jul 2014, 20:56
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...

Re: Determining if unit is factory

Posted: 08 Jul 2014, 10:52
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.

Re: Determining if unit is factory

Posted: 08 Jul 2014, 13:46
by Anarchid
isFactory
... is not available for Java AI.

Re: Determining if unit is factory

Posted: 08 Jul 2014, 15:11
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();

Re: Determining if unit is factory

Posted: 09 Jul 2014, 15:46
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

Re: Determining if unit is factory

Posted: 09 Jul 2014, 15:57
by Anarchid
The algorithm above classifies e.g. reclaim-only workers as builders.

Re: Determining if unit is factory

Posted: 10 Jul 2014, 18:53
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

Re: Determining if unit is factory

Posted: 13 Jul 2014, 16:45
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 :-)