Search in sources :

Example 1 with Worker

use of org.openbw.bwapi4j.unit.Worker 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 Worker

use of org.openbw.bwapi4j.unit.Worker in project BWAPI4J by OpenBW.

the class TestListenerBwem method onStart.

@Override
public void onStart() {
    try {
        System.out.println("onStart");
        // Hello World!
        bw.getInteractionHandler().sendText("hello, world");
        // Print the map name.
        bw.getInteractionHandler().printf("The map is " + bw.getBWMap().mapName() + "! Size: " + bw.getBWMap().mapWidth() + "x" + bw.getBWMap().mapHeight());
        // Enable the UserInput flag, which allows us to manually control units and type messages.
        bw.getInteractionHandler().enableUserInput();
        // Uncomment the following line and the bot will know about everything through the fog of war (cheat).
        // bw.getInteractionHandler().enableCompleteMapInformation();
        self = bw.getInteractionHandler().self();
        // Initialize BWEM.
        System.out.println("BWEM initialization started.");
        // Instantiate the BWEM object.
        bwem = new BWEM(bw);
        // Initialize and pre-calculate internal data.
        bwem.initialize();
        // This option requires "bwem.getMap().onUnitDestroyed(unit);" in the "onUnitDestroy" callback.
        bwem.getMap().enableAutomaticPathAnalysis();
        try {
            // Throws an exception on failure.
            bwem.getMap().assignStartingLocationsToSuitableBases();
        } catch (final Exception e) {
            e.printStackTrace();
            if (bwem.getMap().getUnassignedStartingLocations().size() > 0) {
                throw new IllegalStateException("Failed to find suitable bases for the following starting locations: " + bwem.getMap().getUnassignedStartingLocations().toString());
            }
        }
        System.out.println("BWEM initialization completed.");
        // BWEM's map printer example. Generates a "map.bmp" image file.
        bwem.getMap().getMapPrinter().initialize(bw, bwem.getMap());
        final MapPrinterExample example = new MapPrinterExample(bwem.getMap().getMapPrinter());
        example.printMap(bwem.getMap());
        example.pathExample(bwem.getMap());
        /* Print player info to console. */
        {
            final StringBuilder sb = new StringBuilder("Players: ").append(System.lineSeparator());
            for (final Player player : bw.getAllPlayers()) {
                sb.append("  ").append(player.getName()).append(", ID=").append(player.getId()).append(", race=").append(player.getRace()).append(System.lineSeparator());
            }
            System.out.println(sb.toString());
        }
        // Compile list of workers.
        for (final PlayerUnit u : bw.getUnits(self)) {
            if (u instanceof Worker) {
                final Worker worker = (Worker) u;
                if (!workers.contains(worker)) {
                    workers.add(worker);
                }
            }
        }
        /* Basic gamestart worker auto-mine */
        {
            final List<MineralPatch> unassignedMineralPatches = bw.getMineralPatches();
            final List<Worker> unassignedWorkers = new ArrayList<>(workers);
            unassignedMineralPatches.sort(new UnitDistanceComparator(self.getStartLocation().toPosition()));
            while (!unassignedWorkers.isEmpty() && !unassignedMineralPatches.isEmpty()) {
                final Worker unassignedWorker = unassignedWorkers.remove(0);
                final MineralPatch unassignedMineralPatch = unassignedMineralPatches.remove(0);
                unassignedWorker.gather(unassignedMineralPatch);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Player(org.openbw.bwapi4j.Player) MapPrinterExample(bwem.example.MapPrinterExample) MineralPatch(org.openbw.bwapi4j.unit.MineralPatch) Worker(org.openbw.bwapi4j.unit.Worker) ArrayList(java.util.ArrayList) List(java.util.List) PlayerUnit(org.openbw.bwapi4j.unit.PlayerUnit)

Aggregations

MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)2 PlayerUnit (org.openbw.bwapi4j.unit.PlayerUnit)2 Worker (org.openbw.bwapi4j.unit.Worker)2 MapPrinterExample (bwem.example.MapPrinterExample)1 Geyser (bwem.unit.Geyser)1 Mineral (bwem.unit.Mineral)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Player (org.openbw.bwapi4j.Player)1 Position (org.openbw.bwapi4j.Position)1 TilePosition (org.openbw.bwapi4j.TilePosition)1 Color (org.openbw.bwapi4j.type.Color)1 CommandCenter (org.openbw.bwapi4j.unit.CommandCenter)1