use of org.openbw.bwapi4j.unit.Weapon in project Ecgberht by Jabbo16.
the class WraithAgent method runAgent.
@Override
public boolean runAgent() {
// TODO improve
try {
if (!unit.exists() || unitInfo == null)
return true;
actualFrame = getGs().frameCount;
frameLastOrder = unit.getLastCommandFrame();
airAttackers.clear();
if (frameLastOrder == actualFrame)
return false;
Position attack = getBestBaseToHarass();
UnitInfo closestThreat = null;
double bestDist = Double.MAX_VALUE;
SimInfo airSim = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.AIR);
airAttackers = airSim.enemies;
for (UnitInfo u : airAttackers) {
double predictedDist = unitInfo.getPredictedDistance(u);
if (predictedDist < bestDist) {
closestThreat = u;
bestDist = predictedDist;
}
}
Set<UnitInfo> mainTargets = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.MIX).enemies;
UnitInfo harassed = chooseHarassTarget(mainTargets);
if (closestThreat != null) {
Weapon myWeapon = closestThreat.flying ? unit.getAirWeapon() : unit.getGroundWeapon();
double hisAirWeaponRange = closestThreat.airRange;
Position kitePos = UtilMicro.kiteAway(unit, new TreeSet<>(Collections.singleton(closestThreat)));
if (myWeapon.maxRange() > hisAirWeaponRange && bestDist > hisAirWeaponRange) {
if (myWeapon.cooldown() != 0) {
if (kitePos != null) {
UtilMicro.move(unit, kitePos);
return false;
}
} else if (harassed != null && unitInfo.getDistance(harassed) <= myWeapon.maxRange()) {
UtilMicro.attack(unitInfo, harassed);
} else
UtilMicro.attack(unitInfo, closestThreat);
return false;
}
if (kitePos != null) {
UtilMicro.move(unit, kitePos);
return false;
}
}
if (harassed != null) {
UtilMicro.attack(unitInfo, harassed);
return false;
}
UnitInfo target = Util.getRangedTarget(unitInfo, mainTargets);
if (target != null) {
UtilMicro.attack(unitInfo, target);
return false;
}
if (attack != null) {
if (attack.getDistance(myUnit.getPosition()) >= 32 * 5)
UtilMicro.move(unit, attack);
else
UtilMicro.attack(unit, attack);
return false;
}
return false;
} catch (Exception e) {
System.err.println("Exception WraithAgent");
e.printStackTrace();
}
return false;
}