use of org.openbw.bwapi4j.unit.MineralPatch in project Ecgberht by Jabbo16.
the class CheckMineralWalk method getCloserMineral.
private MineralPatch getCloserMineral() {
double bestDist = Double.MAX_VALUE;
MineralPatch closerMineral = null;
for (MineralPatch mineralPatch : gameState.walkingMinerals) {
if (!mineralPatch.isVisible() || gameState.chosenScout.getDistance(mineralPatch) <= 32 * 4)
continue;
double dist = mineralPatch.getDistance(gameState.scoutSLs.iterator().next().getLocation().toPosition());
if (dist > gameState.chosenScout.getDistance(gameState.scoutSLs.iterator().next().getLocation().toPosition()))
continue;
Area mineralArea = gameState.bwem.getMap().getArea(mineralPatch.getTilePosition());
Area workerArea = gameState.bwem.getMap().getArea(gameState.chosenScout.getTilePosition());
if (mineralPatch.equals(movingMineral) && mineralArea != null && mineralArea.equals(workerArea))
continue;
if (dist < bestDist) {
bestDist = dist;
closerMineral = mineralPatch;
}
}
return closerMineral;
}
use of org.openbw.bwapi4j.unit.MineralPatch 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);
}
}
use of org.openbw.bwapi4j.unit.MineralPatch in project Ecgberht by Jabbo16.
the class Move method execute.
@Override
public State execute() {
try {
Worker chosen = gameState.chosenWorker;
Position realEnd = Util.getUnitCenterPosition(gameState.chosenPosition.toPosition(), gameState.chosenToBuild);
if (chosen.move(realEnd)) {
if (gameState.workerIdle.contains(chosen))
gameState.workerIdle.remove(chosen);
else if (gameState.workerMining.containsKey(chosen)) {
MineralPatch mineral = gameState.workerMining.get(chosen);
gameState.workerMining.remove(chosen);
if (gameState.mineralsAssigned.containsKey(mineral)) {
gameState.mining--;
gameState.mineralsAssigned.put(mineral, gameState.mineralsAssigned.get(mineral) - 1);
}
}
if (gameState.chosenToBuild == UnitType.Terran_Command_Center && gameState.bwem.getMap().getArea(gameState.chosenPosition).equals(gameState.naturalArea) && gameState.naturalChoke != null) {
gameState.defendPosition = gameState.naturalChoke.getCenter().toPosition();
}
gameState.workerBuild.put((SCV) chosen, new MutablePair<>(gameState.chosenToBuild, gameState.chosenPosition));
gameState.deltaCash.first += gameState.chosenToBuild.mineralPrice();
gameState.deltaCash.second += gameState.chosenToBuild.gasPrice();
gameState.chosenWorker = null;
gameState.chosenToBuild = UnitType.None;
return State.SUCCESS;
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
e.printStackTrace();
return State.ERROR;
}
}
use of org.openbw.bwapi4j.unit.MineralPatch in project Ecgberht by Jabbo16.
the class ChooseBlotWorker method execute.
@Override
public State execute() {
try {
Worker closestWorker = null;
Position chosen = gameState.chosenBuildingLot.getPosition();
if (!gameState.workerIdle.isEmpty()) {
for (Worker u : gameState.workerIdle) {
if ((closestWorker == null || u.getDistance(chosen) < closestWorker.getDistance(chosen))) {
closestWorker = u;
}
}
}
if (!gameState.workerMining.isEmpty()) {
for (Entry<Worker, MineralPatch> u : gameState.workerMining.entrySet()) {
if ((closestWorker == null || u.getKey().getDistance(chosen) < closestWorker.getDistance(chosen)) && !u.getKey().isCarryingMinerals()) {
closestWorker = u.getKey();
}
}
}
if (closestWorker != null) {
gameState.chosenWorker = closestWorker;
return State.SUCCESS;
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
e.printStackTrace();
return State.ERROR;
}
}
use of org.openbw.bwapi4j.unit.MineralPatch in project Ecgberht by Jabbo16.
the class CheckMineralWalkGoldRush method getCloserMineral.
private MineralPatch getCloserMineral(Map.Entry<SCV, MutablePair<UnitType, TilePosition>> entry) {
double bestDist = Double.MAX_VALUE;
MineralPatch closerMineral = null;
SCV scv = entry.getKey();
Position buildTarget = entry.getValue().second.toPosition();
for (MineralPatch mineralPatch : gameState.walkingMinerals) {
if (!mineralPatch.isVisible() || scv.getDistance(mineralPatch) <= 32 * 4)
continue;
double dist = mineralPatch.getDistance(buildTarget) * 1.2;
if (dist > scv.getDistance(buildTarget))
continue;
Area mineralArea = gameState.bwem.getMap().getArea(mineralPatch.getTilePosition());
Area workerArea = gameState.bwem.getMap().getArea(scv.getTilePosition());
if (mineralPatch.equals(scv.getTargetUnit()) && mineralArea != null && mineralArea.equals(workerArea))
continue;
if (dist < bestDist) {
bestDist = dist;
closerMineral = mineralPatch;
}
}
return closerMineral;
}
Aggregations