Search in sources :

Example 1 with MapPrinterExample

use of bwem.example.MapPrinterExample 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

MapPrinterExample (bwem.example.MapPrinterExample)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Player (org.openbw.bwapi4j.Player)1 MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)1 PlayerUnit (org.openbw.bwapi4j.unit.PlayerUnit)1 Worker (org.openbw.bwapi4j.unit.Worker)1