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);
}
}
Aggregations