use of org.openbw.bwapi4j.Position in project BWAPI4J by OpenBW.
the class TestListenerBwem method onFrame.
@Override
public void onFrame() {
try {
// Send idle workers to mine at the closest mineral patch.
for (final Worker worker : workers) {
if (worker.isIdle()) {
MineralPatch closestMineralPatch = null;
for (final MineralPatch mineralPatch : bw.getMineralPatches()) {
if (closestMineralPatch == null) {
closestMineralPatch = mineralPatch;
} else {
if (mineralPatch.getPosition().getDistance(self.getStartLocation().toPosition()) < closestMineralPatch.getPosition().getDistance(self.getStartLocation().toPosition())) {
closestMineralPatch = mineralPatch;
}
}
}
worker.gather(closestMineralPatch);
}
}
/* Train an SCV at every Command Center. */
{
if (self.getRace() == Race.Terran) {
for (final PlayerUnit u : bw.getUnits(self)) {
if (u instanceof CommandCenter) {
final CommandCenter commandCenter = (CommandCenter) u;
if (!commandCenter.isTraining()) {
commandCenter.trainWorker();
}
}
}
}
}
/* Highlight starting locations and possible base locations. */
{
for (final Base base : bwem.getMap().getBases()) {
final boolean isStartingLocation = base.isStartingLocation();
final Color highlightColor = isStartingLocation ? Color.GREEN : Color.YELLOW;
final Position baseLocation = base.getLocation().toPosition();
final Position resourceDepotSize = UnitType.Terran_Command_Center.tileSize().toPosition();
if (isOnScreen(baseLocation)) {
bw.getMapDrawer().drawBoxMap(baseLocation, baseLocation.add(resourceDepotSize), highlightColor);
}
/* Display minerals. */
for (final Mineral mineral : base.getMinerals()) {
if (isOnScreen(mineral.getCenter())) {
bw.getMapDrawer().drawLineMap(mineral.getCenter(), base.getCenter(), Color.CYAN);
}
}
/* Display geysers. */
for (final Geyser geyser : base.getGeysers()) {
if (isOnScreen(geyser.getCenter())) {
bw.getMapDrawer().drawLineMap(geyser.getCenter(), base.getCenter(), Color.GREEN);
}
}
}
}
/* Highlight choke points. */
{
final int chokePointRadius = 8;
final Color chokePointColor = Color.RED;
for (final ChokePoint chokePoint : bwem.getMap().getChokePoints()) {
final Position center = chokePoint.getCenter().toPosition();
if (isOnScreen(center)) {
bw.getMapDrawer().drawCircleMap(center, chokePointRadius, chokePointColor);
bw.getMapDrawer().drawLineMap(center.getX() - chokePointRadius, center.getY(), center.getX() + chokePointRadius, center.getY(), chokePointColor);
bw.getMapDrawer().drawLineMap(center.getX(), center.getY() - chokePointRadius, center.getX(), center.getY() + chokePointRadius, chokePointColor);
}
}
}
/* Highlight workers. */
{
for (final Worker worker : workers) {
final Position tileSize = new TilePosition(worker.tileWidth(), worker.tileHeight()).toPosition();
final Position topLeft = worker.getPosition().subtract(tileSize.divide(new Position(2, 2)));
final Position bottomRight = topLeft.add(tileSize);
if (isOnScreen(topLeft)) {
bw.getMapDrawer().drawBoxMap(topLeft, bottomRight, Color.BROWN);
}
}
}
/* Draw mouse position debug info. */
{
final Position screenPosition = bw.getInteractionHandler().getScreenPosition();
final Position mousePosition = screenPosition.add(bw.getInteractionHandler().getMousePosition());
final String mouseText = "T:" + mousePosition.toTilePosition().toString() + "\nW:" + mousePosition.toWalkPosition().toString() + "\nP:" + mousePosition.toString();
bw.getMapDrawer().drawBoxMap(mousePosition.toTilePosition().toPosition(), mousePosition.toTilePosition().toPosition().add(new TilePosition(1, 1).toPosition()), Color.WHITE);
bw.getMapDrawer().drawTextMap(mousePosition.add(new Position(20, -10)), mouseText);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
use of org.openbw.bwapi4j.Position in project BWAPI4J by OpenBW.
the class BwemExt method centerOfBuilding.
public static Position centerOfBuilding(final TilePosition tilePosition, final TilePosition buildingSize) {
final Position pixelSize = buildingSize.toPosition();
final Position pixelOffset = pixelSize.divide(new Position(2, 2));
return tilePosition.toPosition().add(pixelOffset);
}
use of org.openbw.bwapi4j.Position in project BWAPI4J by OpenBW.
the class BwemExt method drawDiagonalCrossMap.
public static void drawDiagonalCrossMap(final MapDrawer mapDrawer, final Position topLeft, final Position bottomRight, final Color col) {
mapDrawer.drawLineMap(topLeft, bottomRight, col);
mapDrawer.drawLineMap(new Position(bottomRight.getX(), topLeft.getY()), new Position(topLeft.getX(), bottomRight.getY()), col);
}
use of org.openbw.bwapi4j.Position in project BWAPI4J by OpenBW.
the class Building method getLasKnownDistance.
public double getLasKnownDistance(Unit target) {
if (this == target) {
return 0;
}
int xDist = (this.getLastKnownPosition().getX() - this.type.dimensionLeft()) - (target.getRight() + 1);
if (xDist < 0) {
xDist = target.getLeft() - ((this.getLastKnownPosition().getX() + this.type.dimensionRight()) + 1);
if (xDist < 0) {
xDist = 0;
}
}
int yDist = (this.getLastKnownPosition().getY() - this.type.dimensionUp()) - (target.getBottom() + 1);
if (yDist < 0) {
yDist = target.getTop() - ((this.getLastKnownPosition().getY() + this.type.dimensionDown()) + 1);
if (yDist < 0) {
yDist = 0;
}
}
logger.trace("dx, dy: {}, {}.", xDist, yDist);
return new Position(0, 0).getDistance(new Position(xDist, yDist));
}
use of org.openbw.bwapi4j.Position in project Ecgberht by Jabbo16.
the class ChooseDefensePosition method chooseDefensePosition.
private Position chooseDefensePosition() {
Position chosen = null;
double maxScore = 0;
for (Unit b : gameState.enemyInBase) {
double influence = getScore(b);
// double score = influence / (2 * getEuclideanDist(p, b.pos.toPosition()));
double score = influence / (2.5 * Util.getGroundDistance(getDefensePosition(), b.getPosition()));
if (score > maxScore) {
chosen = b.getPosition();
maxScore = score;
}
}
return chosen;
}
Aggregations