use of org.openbw.bwapi4j.unit.Attacker in project Ecgberht by Jabbo16.
the class CheckHarasserAttacked method execute.
@Override
public State execute() {
try {
if (gameState.enemyMainBase == null) {
gameState.chosenUnitToHarass = null;
gameState.chosenHarasser = null;
return State.FAILURE;
}
if (gameState.chosenUnitToHarass != null) {
if (!gameState.bw.getBWMap().isValidPosition(gameState.chosenUnitToHarass.getPosition())) {
gameState.chosenUnitToHarass = null;
}
}
UnitInfo attacker = null;
int workers = 0;
Set<UnitInfo> attackers = new TreeSet<>();
// Thanks to @N00byEdge for cleaner code
for (UnitInfo u : gameState.unitStorage.getAllyUnits().get(gameState.chosenHarasser).attackers) {
if (!(u.unit instanceof Building) && u.unit instanceof Attacker && u.unit.exists()) {
if (u.unit instanceof Worker) {
workers++;
attacker = u;
}
attackers.add(u);
}
}
if (workers > 1) {
gameState.learningManager.setHarass(true);
gameState.chosenUnitToHarass = null;
return State.FAILURE;
}
if (attackers.isEmpty()) {
if (!gameState.getGame().getBWMap().isVisible(gameState.enemyMainBase.getLocation()) && gameState.chosenUnitToHarass == null) {
gameState.chosenHarasser.move(gameState.enemyMainBase.getLocation().toPosition());
}
return State.SUCCESS;
} else {
boolean winHarass = gameState.sim.simulateHarass(gameState.chosenHarasser, attackers, 70);
if (winHarass) {
if (workers == 1 && !attacker.unit.equals(gameState.chosenUnitToHarass)) {
UtilMicro.attack(gameState.chosenHarasser, attacker);
gameState.chosenUnitToHarass = attacker.unit;
return State.SUCCESS;
}
} else {
if (IntelligenceAgency.getEnemyStrat() == IntelligenceAgency.EnemyStrats.Unknown) {
gameState.explore = true;
gameState.chosenUnitToHarass = null;
gameState.chosenHarasser.stop(false);
return State.FAILURE;
} else if (gameState.chosenHarasser.getHitPoints() <= 15) {
gameState.workerIdle.add(gameState.chosenHarasser);
gameState.chosenHarasser.stop(false);
gameState.chosenHarasser = null;
gameState.chosenUnitToHarass = null;
} else {
// Position kite = UtilMicro.kiteAway(gameState.chosenHarasser, attackers);
Optional<UnitInfo> closestUnit = attackers.stream().min(Comparator.comparing(u -> u.getDistance(gameState.chosenHarasser)));
Position kite = closestUnit.map(unit1 -> UtilMicro.kiteAwayAlt(gameState.chosenHarasser.getPosition(), unit1.position)).orElse(null);
if (kite != null && gameState.bw.getBWMap().isValidPosition(kite)) {
UtilMicro.move(gameState.chosenHarasser, kite);
gameState.chosenUnitToHarass = null;
} else {
kite = UtilMicro.kiteAway(gameState.chosenHarasser, attackers);
if (kite != null && gameState.bw.getBWMap().isValidPosition(kite)) {
UtilMicro.move(gameState.chosenHarasser, kite);
gameState.chosenUnitToHarass = null;
}
}
}
return State.FAILURE;
}
}
return State.SUCCESS;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
e.printStackTrace();
return State.ERROR;
}
}
use of org.openbw.bwapi4j.unit.Attacker in project Ecgberht by Jabbo16.
the class CheckScan method execute.
@Override
public State execute() {
try {
if (gameState.CSs.isEmpty())
return State.FAILURE;
if (gameState.frameCount - gameState.startCount > 40 + gameState.getIH().getLatency()) {
for (ComsatStation u : gameState.CSs) {
if (u.getEnergy() < 50)
continue;
for (UnitInfo e : gameState.unitStorage.getEnemyUnits().values()) {
if ((e.unit.isCloaked() || e.burrowed) && !e.unit.isDetected() && e.unit instanceof Attacker) {
if (gameState.sim.getSimulation(e, true).allies.stream().noneMatch(a -> a.unitType.canAttack()))
continue;
gameState.checkScan = new MutablePair<>(u, e.lastPosition);
return State.SUCCESS;
}
}
}
}
List<Base> valid = new ArrayList<>();
for (Base b : gameState.enemyBLs) {
if (gameState.getGame().getBWMap().isVisible(b.getLocation()) || b.getArea().getAccessibleNeighbors().isEmpty()) {
continue;
}
if (gameState.enemyMainBase != null && gameState.enemyMainBase.getLocation().equals(b.getLocation())) {
continue;
}
valid.add(b);
}
if (valid.isEmpty())
return State.FAILURE;
for (ComsatStation u : gameState.CSs) {
if (u.getEnergy() == 200) {
Random random = new Random();
gameState.checkScan = new MutablePair<>(u, Util.getUnitCenterPosition(valid.get(random.nextInt(valid.size())).getLocation().toPosition(), gameState.enemyRace.getCenter()));
return State.SUCCESS;
}
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
e.printStackTrace();
return State.ERROR;
}
}
Aggregations