use of org.openbw.bwapi4j.unit.MineralPatch in project BWAPI4J by OpenBW.
the class MainTest method testMineralMining.
private void testMineralMining() throws AssertionError {
boolean commandSuccessful = false;
Player self = this.bw.getInteractionHandler().self();
Collection<Unit> allUnits = this.bw.getAllUnits();
MineralPatch patch = null;
for (Unit unit : allUnits) {
if (unit instanceof MineralPatch) {
patch = (MineralPatch) unit;
}
}
List<PlayerUnit> units = this.bw.getUnits(self);
SCV scv = null;
for (PlayerUnit unit : units) {
if (unit instanceof SCV) {
scv = (SCV) unit;
}
}
if (patch != null && scv != null) {
commandSuccessful = scv.gather(patch);
} else {
logger.error("no scv and patch found.");
}
assertTrue("gather command failed.", commandSuccessful);
}
use of org.openbw.bwapi4j.unit.MineralPatch 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);
}
}
use of org.openbw.bwapi4j.unit.MineralPatch in project Ecgberht by Jabbo16.
the class ChooseScout method execute.
@Override
public State execute() {
try {
if (gameState.getStrat().name.equals("PlasmaWraithHell")) {
for (Squad s : gameState.sqManager.squads.values()) {
for (UnitInfo u : s.members) {
if (u.unit instanceof Wraith) {
gameState.chosenScout = (MobileUnit) u.unit;
s.members.remove(u);
return State.SUCCESS;
}
}
}
}
if (!gameState.workerIdle.isEmpty()) {
Worker chosen = gameState.workerIdle.iterator().next();
gameState.chosenScout = chosen;
gameState.workerIdle.remove(chosen);
}
if (gameState.chosenScout == null) {
for (Worker u : gameState.workerMining.keySet()) {
if (!u.isCarryingMinerals()) {
gameState.chosenScout = u;
MineralPatch mineral = gameState.workerMining.get(u);
if (gameState.mineralsAssigned.containsKey(mineral)) {
gameState.mining--;
gameState.mineralsAssigned.put(mineral, gameState.mineralsAssigned.get(mineral) - 1);
}
gameState.workerMining.remove(u);
break;
}
}
}
if (gameState.chosenScout != null)
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 WorkerWalkBuild method execute.
@Override
public State execute() {
try {
for (Entry<SCV, MutablePair<UnitType, TilePosition>> u : gameState.workerBuild.entrySet()) {
SCV chosen = u.getKey();
if (u.getValue().first != UnitType.Terran_Command_Center || gameState.getGame().getBWMap().isVisible(u.getValue().second) || !gameState.fortressSpecialBLsTiles.contains(u.getValue().second))
continue;
Base myBase = Util.getClosestBaseLocation(u.getValue().second.toPosition());
MutablePair<MineralPatch, MineralPatch> minerals = gameState.fortressSpecialBLs.get(myBase);
Area scvArea = gameState.bwem.getMap().getArea(chosen.getTilePosition());
if (!u.getValue().second.equals(new TilePosition(7, 118))) {
if (scvArea != null && scvArea.equals(myBase.getArea())) {
if (chosen.getDistance(minerals.first) > 3 * 32) {
chosen.move(u.getValue().second.toPosition());
continue;
}
if (minerals.second.isVisible()) {
chosen.gather(minerals.second);
continue;
}
}
if (minerals.second.isVisible()) {
chosen.gather(minerals.second);
continue;
}
if (minerals.first.isVisible()) {
chosen.gather(minerals.first);
}
} else {
// Weird logic :/
if (scvArea != null && scvArea.equals(myBase.getArea())) {
if (chosen.getDistance(minerals.first) > 3 * 32) {
chosen.move(u.getValue().second.toPosition());
continue;
} else {
chosen.gather(minerals.second);
continue;
}
}
chosen.gather(minerals.first);
}
}
return State.SUCCESS;
} 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 FinishBuilding method execute.
@Override
public State execute() {
try {
Worker chosen = gameState.chosenWorker;
if (chosen.rightClick(gameState.chosenBuildingLot, false)) {
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);
}
}
gameState.workerTask.put((SCV) chosen, gameState.chosenBuildingLot);
gameState.chosenWorker = null;
gameState.buildingLot.remove(gameState.chosenBuildingLot);
gameState.chosenBuildingLot = null;
return State.SUCCESS;
}
return State.FAILURE;
} catch (Exception e) {
System.err.println(this.getClass().getSimpleName());
e.printStackTrace();
return State.ERROR;
}
}
Aggregations