use of bwapi.Unit in project Ecgberht by Jabbo16.
the class SendDefenders method execute.
@Override
public State execute() {
try {
boolean air_only = true;
boolean cannon_rush = false;
for (Unit u : ((GameState) this.handler).enemyInBase) {
if (u.isFlying() || u.isCloaked()) {
continue;
}
if (!cannon_rush) {
if (u.getType() == UnitType.Protoss_Pylon || u.getType() == UnitType.Protoss_Photon_Cannon) {
cannon_rush = true;
}
}
air_only = false;
}
Set<Unit> friends = new HashSet<Unit>();
for (Squad s : ((GameState) this.handler).squads.values()) {
for (Unit u : s.members) {
friends.add(u);
}
}
boolean bunker = false;
if (!((GameState) this.handler).DBs.isEmpty()) {
for (Unit u : ((GameState) this.handler).DBs.keySet()) {
friends.add(u);
}
bunker = true;
}
int defenders = 6;
Iterator<Unit> it = ((GameState) this.handler).enemyInBase.iterator();
if (((GameState) this.handler).enemyInBase.size() == 1 && it.next().getType().isWorker()) {
defenders = 1;
}
Pair<Boolean, Boolean> battleWin = new Pair<>(true, false);
if (defenders != 1) {
if (((GameState) this.handler).enemyInBase.size() + friends.size() < 25) {
battleWin = ((GameState) this.handler).simulateDefenseBattle(friends, ((GameState) this.handler).enemyInBase, 150, bunker);
}
if (((GameState) this.handler).enemyInBase.size() >= 2 * friends.size()) {
battleWin.first = false;
}
}
if (cannon_rush) {
battleWin.first = false;
}
int frame = ((GameState) this.handler).frameCount;
if (!air_only && ((!battleWin.first || battleWin.second) || defenders == 1)) {
while (((GameState) this.handler).workerDefenders.size() < defenders && !((GameState) this.handler).workerIdle.isEmpty()) {
Unit closestWorker = null;
Position chosen = ((GameState) this.handler).attackPosition;
for (Unit u : ((GameState) this.handler).workerIdle) {
if (u.getLastCommandFrame() == frame) {
continue;
}
if ((closestWorker == null || ((GameState) this.handler).broodWarDistance(u.getPosition(), chosen) < ((GameState) this.handler).broodWarDistance(closestWorker.getPosition(), chosen))) {
closestWorker = u;
}
}
if (closestWorker != null) {
((GameState) this.handler).workerDefenders.add(closestWorker);
((GameState) this.handler).workerIdle.remove(closestWorker);
}
}
while (((GameState) this.handler).workerDefenders.size() < defenders && !((GameState) this.handler).workerMining.isEmpty()) {
Unit closestWorker = null;
Position chosen = ((GameState) this.handler).attackPosition;
for (Unit u : ((GameState) this.handler).workerMining.keySet()) {
if (u.getLastCommandFrame() == frame) {
continue;
}
if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
closestWorker = u;
}
}
if (closestWorker != null) {
if (((GameState) this.handler).workerMining.containsKey(closestWorker)) {
Unit mineral = ((GameState) this.handler).workerMining.get(closestWorker);
((GameState) this.handler).workerDefenders.add(closestWorker);
if (((GameState) this.handler).mineralsAssigned.containsKey(mineral)) {
((GameState) this.handler).mining--;
((GameState) this.handler).mineralsAssigned.put(mineral, ((GameState) this.handler).mineralsAssigned.get(mineral) - 1);
}
((GameState) this.handler).workerMining.remove(closestWorker);
}
}
}
for (Unit u : ((GameState) this.handler).workerDefenders) {
if (frame == u.getLastCommandFrame()) {
continue;
}
if (((GameState) this.handler).attackPosition != null) {
if (u.isIdle()) {
if (((GameState) this.handler).enemyInBase.size() == 1) {
u.attack(((GameState) this.handler).enemyInBase.iterator().next());
} else {
Unit toAttack = ((GameState) this.handler).getUnitToAttack(u, ((GameState) this.handler).enemyInBase);
if (toAttack != null) {
Unit lastTarget = u.getOrderTarget();
if (lastTarget != null && lastTarget.exists()) {
if (lastTarget.equals(toAttack)) {
continue;
}
}
UnitCommand lastUnitCommand = u.getLastCommand();
if (lastUnitCommand != null) {
if (lastUnitCommand.getTarget() != null && lastUnitCommand.getTarget().exists())
if (lastUnitCommand.getTarget().equals(toAttack)) {
continue;
}
}
u.attack(toAttack);
} else {
u.attack(((GameState) this.handler).attackPosition);
}
continue;
}
}
}
}
} else {
if (((GameState) this.handler).strat.name != "ProxyBBS") {
for (Entry<String, Squad> u : ((GameState) this.handler).squads.entrySet()) {
if (((GameState) this.handler).attackPosition != null) {
// if(u.getValue().estado == Status.IDLE || !((GameState)this.handler).attackPosition.equals(u.getValue().attack)) {
u.getValue().giveAttackOrder(((GameState) this.handler).attackPosition);
u.getValue().status = Status.DEFENSE;
continue;
// }
} else {
u.getValue().status = Status.IDLE;
u.getValue().attack = Position.None;
continue;
}
}
}
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
System.err.println(e);
return State.ERROR;
}
}
use of bwapi.Unit in project Ecgberht by Jabbo16.
the class CheckVisibleBL method execute.
@Override
public State execute() {
try {
for (Unit u : ((GameState) this.handler).enemyCombatUnitMemory) {
if (((GameState) this.handler).broodWarDistance(u.getPosition(), ((GameState) this.handler).chosenBaseLocation.toPosition()) < 300) {
((GameState) this.handler).chosenBaseLocation = null;
((GameState) this.handler).movingToExpand = false;
((GameState) this.handler).chosenBuilderBL.stop();
((GameState) this.handler).workerIdle.add(((GameState) this.handler).chosenBuilderBL);
((GameState) this.handler).chosenBuilderBL = null;
((GameState) this.handler).expanding = false;
((GameState) this.handler).deltaCash.first -= UnitType.Terran_Command_Center.mineralPrice();
((GameState) this.handler).deltaCash.second -= UnitType.Terran_Command_Center.gasPrice();
return State.FAILURE;
}
}
if (((GameState) this.handler).getGame().isExplored(((GameState) this.handler).chosenBaseLocation)) {
return State.SUCCESS;
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
System.err.println(e);
return State.ERROR;
}
}
use of bwapi.Unit in project Ecgberht by Jabbo16.
the class ChooseBaseLocation method execute.
@Override
public State execute() {
try {
if (((GameState) this.handler).chosenBaseLocation != null) {
return State.SUCCESS;
}
TilePosition main = null;
if (((GameState) this.handler).MainCC != null) {
main = ((GameState) this.handler).MainCC.getTilePosition();
} else {
main = ((GameState) this.handler).getPlayer().getStartLocation();
}
List<BaseLocation> valid = new ArrayList<>();
for (BaseLocation b : ((GameState) this.handler).BLs) {
if (!((GameState) this.handler).CCs.containsKey(b.getRegion().getCenter()) && BWTA.isConnected(b.getTilePosition(), main)) {
valid.add(b);
}
}
List<BaseLocation> remove = new ArrayList<>();
for (BaseLocation b : valid) {
for (Unit u : ((GameState) this.handler).enemyCombatUnitMemory) {
if (BWTA.getRegion(u.getPosition()) == null || !u.getType().canAttack() || u.getType().isWorker()) {
continue;
}
if (BWTA.getRegion(u.getPosition()).getCenter().equals(BWTA.getRegion(b.getPosition()).getCenter())) {
remove.add(b);
break;
}
}
for (EnemyBuilding u : ((GameState) this.handler).enemyBuildingMemory.values()) {
if (BWTA.getRegion(u.pos) == null) {
continue;
}
if (BWTA.getRegion(u.pos).getCenter().equals(BWTA.getRegion(b.getPosition()).getCenter())) {
remove.add(b);
break;
}
}
}
valid.removeAll(remove);
if (valid.isEmpty()) {
System.out.println("wut");
((GameState) this.handler).chosenBaseLocation = null;
((GameState) this.handler).movingToExpand = false;
((GameState) this.handler).chosenBuilderBL.stop();
((GameState) this.handler).workerIdle.add(((GameState) this.handler).chosenBuilderBL);
((GameState) this.handler).chosenBuilderBL = null;
((GameState) this.handler).expanding = false;
((GameState) this.handler).deltaCash.first -= UnitType.Terran_Command_Center.mineralPrice();
((GameState) this.handler).deltaCash.second -= UnitType.Terran_Command_Center.gasPrice();
return State.FAILURE;
}
((GameState) this.handler).chosenBaseLocation = valid.get(0).getTilePosition();
// System.out.println("----------------------------");
return State.SUCCESS;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
System.err.println(e);
return State.ERROR;
}
}
use of bwapi.Unit in project Ecgberht by Jabbo16.
the class Vulture method getNewStatus.
private void getNewStatus() {
Position myPos = unit.getPosition();
if (getGs().enemyCombatUnitMemory.isEmpty()) {
status = Status.ATTACK;
return;
}
for (Unit u : getGs().enemyCombatUnitMemory) {
if (u.getType().isWorker() && !u.isAttacking()) {
closeWorkers.add(u);
}
if (getGs().broodWarDistance(u.getPosition(), myPos) < 600) {
closeEnemies.add(u);
}
}
for (EnemyBuilding u : getGs().enemyBuildingMemory.values()) {
if ((u.type.canAttack() || u.type == UnitType.Terran_Bunker) && u.unit.isCompleted()) {
if (getGs().broodWarDistance(myPos, u.pos.toPosition()) <= 600) {
closeEnemies.add(u.unit);
}
}
}
if (closeEnemies.isEmpty()) {
status = Status.ATTACK;
return;
} else {
boolean meleeOnly = checkOnlyMelees();
int sim = 80;
if (meleeOnly) {
sim = 5;
}
if (!getGs().simulateHarass(unit, closeEnemies, sim)) {
status = Status.RETREAT;
return;
}
int cd = unit.getGroundWeaponCooldown();
if (status == Status.COMBAT || status == Status.ATTACK) {
if (attackUnit != null) {
if (attackUnit.getType().groundWeapon().maxRange() > type.groundWeapon().maxRange()) {
return;
}
}
if (cd > 0) {
status = Status.KITE;
return;
}
}
if (status == Status.KITE) {
if (attackUnit == null) {
Unit closest = getUnitToAttack(unit, closeEnemies);
if (closest != null) {
double dist = getGs().broodWarDistance(unit.getPosition(), closest.getPosition());
double speed = type.topSpeed();
double timeToEnter = 0.0;
if (speed > .00001) {
timeToEnter = Math.max(0.0, dist - type.groundWeapon().maxRange()) / speed;
}
if (timeToEnter >= cd) {
status = Status.COMBAT;
return;
}
}
} else {
double dist = getGs().broodWarDistance(unit.getPosition(), attackUnit.getPosition());
double speed = type.topSpeed();
double timeToEnter = 0.0;
if (speed > .00001) {
timeToEnter = Math.max(0.0, dist - type.groundWeapon().maxRange()) / speed;
}
if (timeToEnter >= cd) {
status = Status.COMBAT;
return;
}
}
if (cd == 0) {
status = Status.COMBAT;
return;
}
}
}
}
use of bwapi.Unit in project Ecgberht by Jabbo16.
the class ChooseBlotWorker method execute.
@Override
public State execute() {
try {
Unit closestWorker = null;
Position chosen = ((GameState) this.handler).chosenBuildingLot.getPosition();
if (!((GameState) this.handler).workerIdle.isEmpty()) {
for (Unit u : ((GameState) this.handler).workerIdle) {
if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
closestWorker = u;
}
}
}
if (!((GameState) this.handler).workerTask.isEmpty()) {
for (Pair<Unit, Unit> u : ((GameState) this.handler).workerTask) {
if ((closestWorker == null || u.first.getDistance(chosen) < closestWorker.getDistance(chosen)) && u.second.getType().isMineralField() && !u.first.isCarryingMinerals()) {
closestWorker = u.first;
}
}
}
if (closestWorker != null) {
((GameState) this.handler).chosenWorker = closestWorker;
return State.SUCCESS;
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
System.err.println(e);
return State.ERROR;
}
}
Aggregations