[solved]Determining if unit is factory
Moderators: hoijui, Moderators
[solved]Determining if unit is factory
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?
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.
Re: Determining if unit is factory
- 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
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 callbackzwzsg wrote:Oops, just noticed you're in the AI, and not the Lua Scripts section. Though it should still holds.

http://spring.abma.de/doc-Interface-Jav ... /Unit.html
Re: Determining if unit is factory
Try:
Code: Select all
unitDef.isBuilder() && unitDef.getSpeed() == 0
Re: Determining if unit is factory
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 wrote:Try:Code: Select all
unitDef.isBuilder() && unitDef.getSpeed() == 0
Re: Determining if unit is factory
If it builds units, then how are "no units coming out of it"?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.
Re: Determining if unit is factory
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
Ah, I understand what you meant now.
Well maybe add a check as well if you want to differentiate between the two?
Well maybe add a
Code: Select all
unitDef.getBuildDistance()==0
Re: Determining if unit is factory
Dunno if AI can access unitdef yardmap but its better than checking speed
Re: Determining if unit is factory
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
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
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
Unit is a "factory" when it can build other units which are not buildings and have movespeed > 0.
Yeah yeah necro award.
Yeah yeah necro award.
Re: Determining if unit is factory
I make this function:
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...
Code: Select all
public static boolean isStationarFactory(UnitDef def) {
return 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
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.
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
... is not available for Java AI.isFactory
Re: Determining if unit is factory
which is why we discuss how to implement it by hand...Anarchid wrote:... is not available for Java AI.isFactory
goto discussion above.
read(discussion);
reason();
Re: Determining if unit is factory
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:
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
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
- 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
The algorithm above classifies e.g. reclaim-only workers as builders.
Re: Determining if unit is factory
They are... check if they have any build items for that partAnarchid wrote:The algorithm above classifies e.g. reclaim-only workers as builders.
Re: Determining if unit is factory
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.
So isFactory by getBuildOptions().isEmpty() is bad becouse required CPU time, but check getBuildOptions().isEmpty() is the only way for check it true? he-heAF wrote:if they have any build items for that part
