Some odd behavior while trying to get and then delete campaign-level terrain. When I try to print all nearby terrain, I only ever get [TERRAIN] or [TERRAIN, DEBRIS] if there's debris. The worrisome part for me is that I delete magfields but not rings, asteroid fields, or solar shades somehow. RingBandAPI is a subclass of SecEnt.
final float searchRange =60000f;//arbitrary number, in the future have a check to avoid removing the terrain from unrelated planets
List<SectorEntityToken> terrainList=CampaignUtils.getNearbyEntitiesWithTag(planet, searchRange, "terrain");
terrainList.addAll(CampaignUtils.getNearbyEntitiesWithTag(planet, searchRange, "ACCRETION_DISK"));
terrainList.addAll(CampaignUtils.getNearbyEntitiesWithTag(planet, searchRange, "STELLAR_MIRROR"));
//note: no idea why terrain covers magfields but not rings. they're both under terrain tags
terrainList.addAll(CampaignUtils.getNearbyEntitiesWithTag(planet, searchRange, "RING"));
terrainList.addAll(CampaignUtils.getNearbyEntitiesWithTag(planet, searchRange, "ASTEROID_BELT"));
//note: adding things to terrainList seems to do precisely nothing in terms of increasing or decreasing the number of entities found
//note: holy *** it works
//note: wait ***, it wipes magfields but not rings that are in search range
for (SectorEntityToken terrain:terrainList)
{
log.info((terrain.getTags()));
system.removeEntity(terrain);
}
The function I'm calling with getNearbyEntitiesWithTag is a LazyLib function, which seems to have a completely sane implementation based on using locationAPI and distance^2 to check the distance to all relevant SectorEntities in system. The LazyLib function seems to function completely fine in itself in testing. Implementation below in case it matter.
public static <T extends SectorEntityToken> List<T> getNearbyEntitiesWithTag(
SectorEntityToken token, float range, String entityTag)
{
List<T> entities = new ArrayList<>();
// Find all tokens of the given type within range
for (SectorEntityToken tmp : token.getContainingLocation().getEntitiesWithTag(entityTag))
{
T entity = (T) tmp;
// Exclude passed in token
if (entity == token)
{
continue;
}
// Add any token within range
if (MathUtils.isWithinRange(token, entity, range))
{
entities.add(entity);
}
}
return entities;
}
@Nullable
public static <T extends SectorEntityToken> T getNearestEntityFromFaction(
SectorEntityToken token, String entityTag, FactionAPI faction)
{
T closest = null;
float distanceSquared, closestDistanceSquared = Float.MAX_VALUE;
for (SectorEntityToken tmp : token.getContainingLocation().getEntitiesWithTag(entityTag))
{
T entity = (T) tmp;
if (entity == token || faction != entity.getFaction())
{
continue;
}
distanceSquared = MathUtils.getDistanceSquared(token.getLocation(),
entity.getLocation());
if (distanceSquared < closestDistanceSquared)
{
closest = entity;
closestDistanceSquared = distanceSquared;
}
}
return closest;
}