Search in sources :

Example 1 with TilePosition

use of org.openbw.bwapi4j.TilePosition 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);
    }
}
Also used : Mineral(bwem.unit.Mineral) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) Color(org.openbw.bwapi4j.type.Color) CommandCenter(org.openbw.bwapi4j.unit.CommandCenter) MineralPatch(org.openbw.bwapi4j.unit.MineralPatch) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition) Worker(org.openbw.bwapi4j.unit.Worker) PlayerUnit(org.openbw.bwapi4j.unit.PlayerUnit)

Example 2 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class TestListenerYata method onStart.

@Override
public void onStart() {
    try {
        System.out.println("onStart");
        this.bw.getInteractionHandler().enableUserInput();
        System.out.println("YATA initialization started.");
        this.yata = new YATA(this.bw.getBWMap());
        System.out.println("YATA initialization complete.");
        final MapPrinter mapPrinter = new MapPrinter(new TilePosition(this.bw.getBWMap().mapWidth(), this.bw.getBWMap().mapHeight()));
        for (int y = 0; y < this.yata.getHeight(); ++y) {
            for (int x = 0; x < this.yata.getWidth(); ++x) {
                final Color color = this.yata.getNodes()[x][y].isWalkable() ? Color.GREEN : Color.BLACK;
                final TilePosition tilePosition = new TilePosition(x, y);
                mapPrinter.Point(tilePosition.toWalkPosition(), color);
            }
        }
        final TilePosition sourceTilePosition = this.bw.getBWMap().getStartPositions().get(0);
        final TilePosition targetTilePosition = this.bw.getBWMap().getStartPositions().get(1);
        // final TilePosition sourceTilePosition = new TilePosition(65, 104);
        // final TilePosition targetTilePosition = new TilePosition(89, 106);
        Timer timer = new Timer();
        final List<TilePosition> tilePath = this.yata.getPath(sourceTilePosition, targetTilePosition);
        System.out.println("getPath(): " + timer.elapsedMilliseconds() + " ms");
        timer.reset();
        for (final TilePosition tilePosition : tilePath) {
            mapPrinter.Point(tilePosition.toWalkPosition(), Color.WHITE);
        }
        mapPrinter.writeImageToFile(Paths.get("yata.png"));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}
Also used : Timer(bwem.util.Timer) TilePosition(org.openbw.bwapi4j.TilePosition)

Example 3 with TilePosition

use of org.openbw.bwapi4j.TilePosition 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);
}
Also used : WalkPosition(org.openbw.bwapi4j.WalkPosition) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position)

Example 4 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class BwemExt method makeBoundingBoxIncludePoint.

// Enlarges the bounding box [topLeft, bottomRight] so that it includes A.
public static Pair<TilePosition, TilePosition> makeBoundingBoxIncludePoint(final TilePosition topLeft, final TilePosition bottomRight, final TilePosition point) {
    int topLeftX = topLeft.getX();
    int topLeftY = topLeft.getY();
    int bottomRightX = bottomRight.getX();
    int bottomRightY = bottomRight.getY();
    if (point.getX() < topLeftX)
        topLeftX = point.getX();
    if (point.getX() > bottomRightX)
        bottomRightX = point.getX();
    if (point.getY() < topLeftY)
        topLeftY = point.getY();
    if (point.getY() > bottomRightY)
        bottomRightY = point.getY();
    return new Pair<>(new TilePosition(topLeftX, topLeftY), new TilePosition(bottomRightX, bottomRightY));
}
Also used : TilePosition(org.openbw.bwapi4j.TilePosition) Pair(org.openbw.bwapi4j.util.Pair)

Example 5 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class Graph method getNearestArea.

public Area getNearestArea(final TilePosition tilePosition) {
    final Area area = getArea(tilePosition);
    if (area != null) {
        return area;
    }
    final TilePosition t = getMap().breadthFirstSearch(tilePosition, // findCond
    args -> {
        Object ttile = args[0];
        if (ttile instanceof Tile) {
            Tile tile = (Tile) ttile;
            return (tile.getAreaId().intValue() > 0);
        } else {
            throw new IllegalArgumentException();
        }
    }, // visitCond
    args -> true);
    return getArea(t);
}
Also used : Area(bwem.area.Area) TilePosition(org.openbw.bwapi4j.TilePosition) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile)

Aggregations

TilePosition (org.openbw.bwapi4j.TilePosition)46 ChokePoint (bwem.ChokePoint)13 Position (org.openbw.bwapi4j.Position)13 Area (bwem.Area)9 WalkPosition (org.openbw.bwapi4j.WalkPosition)8 Base (bwem.Base)7 MiniTile (bwem.tile.MiniTile)7 Tile (bwem.tile.Tile)7 ArrayList (java.util.ArrayList)7 UnitType (org.openbw.bwapi4j.type.UnitType)6 TileImpl (bwem.tile.TileImpl)4 Geyser (bwem.unit.Geyser)4 Mineral (bwem.unit.Mineral)4 Worker (org.openbw.bwapi4j.unit.Worker)4 MutablePair (ecgberht.Util.MutablePair)3 Building (org.openbw.bwapi4j.unit.Building)3 MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)3 SCV (org.openbw.bwapi4j.unit.SCV)3 Area (bwem.area.Area)2 StaticBuilding (bwem.unit.StaticBuilding)2