use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class WorkerScoutAgent method updateBorders.
private void updateBorders() {
final Area enemyRegion = enemyBase.getArea();
if (enemyRegion == null)
return;
final Position enemyCenter = enemyBase.getLocation().toPosition().add(new Position(64, 48));
final List<TilePosition> closestTobase = new ArrayList<>(BuildingMap.tilesArea.get(enemyRegion));
List<Position> unsortedVertices = new ArrayList<>();
for (TilePosition tp : closestTobase) {
if (getGs().bwem.getMap().getArea(tp) != enemyRegion)
continue;
TilePosition right = new TilePosition(tp.getX() + 1, tp.getY());
TilePosition bottom = new TilePosition(tp.getX(), tp.getY() + 1);
TilePosition left = new TilePosition(tp.getX() - 1, tp.getY());
TilePosition up = new TilePosition(tp.getX(), tp.getY() - 1);
final boolean edge = (!getGs().getGame().getBWMap().isValidPosition(right) || getGs().bwem.getMap().getArea(right) != enemyRegion || !getGs().getGame().getBWMap().isBuildable(right)) || (!getGs().getGame().getBWMap().isValidPosition(bottom) || getGs().bwem.getMap().getArea(bottom) != enemyRegion || !getGs().getGame().getBWMap().isBuildable(bottom)) || (!getGs().getGame().getBWMap().isValidPosition(left) || getGs().bwem.getMap().getArea(left) != enemyRegion || !getGs().getGame().getBWMap().isBuildable(left)) || (!getGs().getGame().getBWMap().isValidPosition(up) || getGs().bwem.getMap().getArea(up) != enemyRegion || !getGs().getGame().getBWMap().isBuildable(up));
if (edge && getGs().getGame().getBWMap().isBuildable(tp)) {
Position vertex = tp.toPosition().add(new Position(16, 16));
double dist = enemyCenter.getDistance(vertex);
if (dist > 368.0) {
double pullBy = Math.min(dist - 368.0, 120.0);
if (vertex.getX() == enemyCenter.getX()) {
vertex = vertex.add(new Position(0, vertex.getY() > enemyCenter.getY() ? (int) (-pullBy) : (int) pullBy));
} else {
double m = (double) (enemyCenter.getY() - vertex.getY()) / (double) (enemyCenter.getX() - vertex.getX());
double x = vertex.getX() + (vertex.getX() > enemyCenter.getX() ? -1.0 : 1.0) * pullBy / (Math.sqrt(1 + m * m));
double y = m * (x - vertex.getX()) + vertex.getY();
vertex = new Position((int) x, (int) y);
}
}
if (getGs().getGame().getBWMap().isValidPosition(vertex) && getGs().getGame().getBWMap().isWalkable(vertex.toWalkPosition()))
unsortedVertices.add(vertex);
}
}
List<Position> sortedVertices = new ArrayList<>();
Position current = unsortedVertices.get(0);
enemyBaseBorders.add(current);
unsortedVertices.remove(current);
while (!unsortedVertices.isEmpty()) {
double bestDist = 1000000;
Position bestPos = null;
for (final Position pos : unsortedVertices) {
double dist = pos.getDistance(current);
if (dist < bestDist) {
bestDist = dist;
bestPos = pos;
}
}
current = bestPos;
sortedVertices.add(sortedVertices.size(), bestPos);
unsortedVertices.remove(bestPos);
}
int distanceThreshold = 100;
while (true) {
int maxFarthest = 0;
int maxFarthestStart = 0;
int maxFarthestEnd = 0;
for (int i = 0; i < sortedVertices.size(); ++i) {
int farthest = 0;
int farthestIndex = 0;
for (int j = 1; j < sortedVertices.size() / 2; ++j) {
int jindex = (i + j) % sortedVertices.size();
if (sortedVertices.get(i).getDistance(sortedVertices.get(jindex)) < distanceThreshold) {
farthest = j;
farthestIndex = jindex;
}
}
if (farthest > maxFarthest) {
maxFarthest = farthest;
maxFarthestStart = i;
maxFarthestEnd = farthestIndex;
}
}
if (maxFarthest < 4)
break;
List<Position> temp = new ArrayList<>();
for (int s = maxFarthestEnd; s != maxFarthestStart; s = (s + 1) % sortedVertices.size()) {
temp.add(temp.size(), sortedVertices.get(s));
}
sortedVertices = temp;
}
enemyBaseBorders = sortedVertices;
currentVertex = 0;
if (!getGs().learningManager.isNaughty()) {
Base enemyNatural = getGs().enemyNaturalBase;
if (enemyNatural != null) {
Position enemyNaturalPos = enemyNatural.getLocation().toPosition();
int index = -1;
double distMax = Double.MAX_VALUE;
for (int ii = 0; ii < enemyBaseBorders.size(); ii++) {
double dist = Util.getGroundDistance(enemyBaseBorders.get(ii), enemyNaturalPos);
if (index == -1 || dist < distMax) {
index = ii;
distMax = dist;
}
}
enemyBaseBorders.add(index, enemyNaturalPos);
this.enemyNaturalIndex = index;
}
} else {
enemyNaturalIndex = -1;
removedIndex = true;
}
}
use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class BuildingMap method findBunkerPosition.
public TilePosition findBunkerPosition(ChokePoint choke) {
TilePosition buildingSize = UnitType.Terran_Bunker.tileSize();
int size = Math.max(buildingSize.getY(), buildingSize.getX());
double chokeWidth = Util.getChokeWidth(choke);
Position starting = choke.getCenter().toPosition();
int x = starting.toTilePosition().getY();
int y = starting.toTilePosition().getX();
int i = 10;
int j = 10;
boolean expandBunker = choke.equals(getGs().naturalChoke);
// Finds the first valid tileposition starting around the given tileposition
TilePosition position = null;
double dist = Double.MAX_VALUE;
for (int ii = (x - i); ii <= (x + i); ii++) {
for (int jj = (y - j); jj <= (y + j); jj++) {
if ((ii >= 0 && ii < height) && (jj >= 0 && jj < width)) {
if ((!map[ii][jj].equals("M") && !map[ii][jj].equals("V") && !map[ii][jj].equals("E") && !map[ii][jj].equals("B")) && Integer.parseInt(map[ii][jj]) >= size) {
Area area = bwem.getMap().getArea(new TilePosition(jj, ii));
if (area != null && area.equals(getGs().naturalArea) && !expandBunker)
continue;
if (area != null && !area.equals(getGs().naturalArea) && expandBunker)
continue;
if (checkUnitsChosenBuildingGrid(new TilePosition(jj, ii), UnitType.Terran_Bunker)) {
TilePosition newPosition = new TilePosition(jj, ii);
Position centerBunker = Util.getUnitCenterPosition(newPosition.toPosition(), UnitType.Terran_Bunker);
if (chokeWidth <= 64.0 && Util.broodWarDistance(choke.getCenter().toPosition(), centerBunker) <= 64)
continue;
double newDist = Util.broodWarDistance(centerBunker, starting);
if (position == null || newDist < dist) {
position = newPosition;
dist = newDist;
}
}
}
}
}
}
return position;
}
use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class BuildingMap method checkUnitsChosenBuildingGrid.
private boolean checkUnitsChosenBuildingGrid(TilePosition BL, UnitType type) {
try {
Position topLeft = new Position(BL.getX() * TilePosition.SIZE_IN_PIXELS, BL.getY() * TilePosition.SIZE_IN_PIXELS);
Position bottomRight = new Position(topLeft.getX() + type.tileWidth() * TilePosition.SIZE_IN_PIXELS, topLeft.getY() + type.tileHeight() * TilePosition.SIZE_IN_PIXELS);
// Test
List<Unit> blockers = Util.getUnitsInRectangle(topLeft, bottomRight);
if (blockers.isEmpty() && !getGs().getGame().canBuildHere(BL, type))
return false;
if (blockers.isEmpty())
return true;
if (blockers.size() > 1)
return false;
else {
Unit blocker = blockers.get(0);
return blocker instanceof PlayerUnit && ((PlayerUnit) blocker).getPlayer().getId() == self.getId() && blocker instanceof Worker && ((Worker) blocker).getBuildType() == type;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class UnitInfo method update.
// TODO completion frames
void update() {
player = unit.getPlayer();
unitType = unit.getType();
visible = unit.isVisible();
position = visible ? unit.getPosition() : position;
currentOrder = unit.getOrder();
completed = !completed && visible ? unit.isCompleted() : completed;
tileposition = visible ? unit.getTilePosition() : tileposition;
if (!unitType.isBuilding())
walkposition = new Position(unit.getLeft(), unit.getTop()).toWalkPosition();
else
walkposition = tileposition.toWalkPosition();
if (visible) {
lastPosition = position;
lastTileposition = tileposition;
lastWalkposition = walkposition;
}
lastVisibleFrame = visible ? getGs().frameCount : lastVisibleFrame;
lastAttackFrame = unit.isStartingAttack() ? getGs().frameCount : lastVisibleFrame;
if (unit instanceof GroundAttacker)
groundRange = player.getUnitStatCalculator().weaponMaxRange(unitType.groundWeapon());
if (unit instanceof AirAttacker)
airRange = player.getUnitStatCalculator().weaponMaxRange(unitType.airWeapon());
if (unit instanceof Bunker) {
airRange = 5 * 32;
groundRange = 5 * 32;
}
health = visible ? unit.getHitPoints() : expectedHealth();
shields = visible ? unit.getShields() : expectedShields();
if (unit instanceof SpellCaster)
energy = ((SpellCaster) unit).getEnergy();
percentHealth = unitType.maxHitPoints() > 0 ? (double) health / (double) unitType.maxHitPoints() : 1.0;
percentShield = unitType.maxShields() > 0 ? (double) shields / (double) unitType.maxShields() : 1.0;
if (unit instanceof Burrowable && visible)
burrowed = ((Burrowable) unit).isBurrowed();
if (visible)
flying = unit.isFlying();
speed = Util.getSpeed(this);
target = unit instanceof Attacker ? ((Attacker) unit).getTargetUnit() : unit.getOrderTarget();
attackers.clear();
}
use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class BaseLocationComparator method compare.
@Override
public int compare(Base a, Base b) {
try {
if (base == null)
throw new Exception("No Base");
Position start = this.base.getLocation().toPosition();
if (getGs().blockedBLs.contains(a))
return 1;
if (getGs().blockedBLs.contains(b))
return -1;
if (a.getArea().getAccessibleNeighbors().isEmpty())
return 1;
if (b.getArea().getAccessibleNeighbors().isEmpty())
return -1;
if (a.equals(base))
return -1;
if (b.equals(base))
return 1;
double distA = Util.getGroundDistance(a.getLocation().toPosition(), start);
double distB = Util.getGroundDistance(b.getLocation().toPosition(), start);
if (Double.compare(distA, 0.0) == 0 && Double.compare(distB, 0.0) > 0)
return 1;
if (Double.compare(distB, 0.0) == 0 && Double.compare(distA, 0.0) > 0)
return -1;
if (!getGs().getStrat().name.equals("FullBio") && !getGs().getStrat().name.equals("FullBioFE") && !getGs().getStrat().name.equals("BioGreedyFE")) {
if ((a.getGeysers().isEmpty() && !a.getMinerals().isEmpty()) && (!b.getGeysers().isEmpty() && !b.getMinerals().isEmpty())) {
return 1;
}
if ((!a.getGeysers().isEmpty() && !a.getMinerals().isEmpty()) && (b.getGeysers().isEmpty() && !b.getMinerals().isEmpty())) {
return -1;
}
}
if (Double.compare(distA, distB) < 0 && Double.compare(distA, 0.0) > 0)
return -1;
else if (Double.compare(distA, distB) > 0 && Double.compare(distB, 0.0) > 0)
return 1;
return 1;
} catch (Exception e) {
System.err.println("Sorter");
e.printStackTrace();
}
return 0;
}
Aggregations