Search in sources :

Example 11 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project Ecgberht by Jabbo16.

the class AreaInitializer method createBases.

void createBases(final TerrainData terrainData) {
    final TilePosition resourceDepotDimensions = UnitType.Terran_Command_Center.tileSize();
    final List<Resource> remainingResources = new ArrayList<>();
    for (final Mineral mineral : getMinerals()) {
        if ((mineral.getInitialAmount() >= 40) && !mineral.isBlocking()) {
            remainingResources.add(mineral);
        }
    }
    for (final Geyser geyser : getGeysers()) {
        if ((geyser.getInitialAmount() >= 300) && !geyser.isBlocking()) {
            remainingResources.add(geyser);
        }
    }
    while (!remainingResources.isEmpty()) {
        // 1) Calculate the SearchBoundingBox (needless to search too far from the
        // remainingResources):
        TilePosition topLeftResources = new TilePosition(Integer.MAX_VALUE, Integer.MAX_VALUE);
        TilePosition bottomRightResources = new TilePosition(Integer.MIN_VALUE, Integer.MIN_VALUE);
        for (final Resource r : remainingResources) {
            final Pair<TilePosition, TilePosition> pair1 = BwemExt.makeBoundingBoxIncludePoint(topLeftResources, bottomRightResources, r.getTopLeft());
            topLeftResources = pair1.getLeft();
            bottomRightResources = pair1.getRight();
            final Pair<TilePosition, TilePosition> pair2 = BwemExt.makeBoundingBoxIncludePoint(topLeftResources, bottomRightResources, r.getBottomRight());
            topLeftResources = pair2.getLeft();
            bottomRightResources = pair2.getRight();
        }
        final TilePosition dimensionsBetweenResourceDepotAndResources = new TilePosition(BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES, BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES);
        TilePosition topLeftSearchBoundingBox = topLeftResources.subtract(resourceDepotDimensions).subtract(dimensionsBetweenResourceDepotAndResources);
        TilePosition bottomRightSearchBoundingBox = bottomRightResources.add(new TilePosition(1, 1)).add(dimensionsBetweenResourceDepotAndResources);
        topLeftSearchBoundingBox = BwemExt.makePointFitToBoundingBox(topLeftSearchBoundingBox, getTopLeft(), getBottomRight().subtract(resourceDepotDimensions).add(new TilePosition(1, 1)));
        bottomRightSearchBoundingBox = BwemExt.makePointFitToBoundingBox(bottomRightSearchBoundingBox, getTopLeft(), getBottomRight().subtract(resourceDepotDimensions).add(new TilePosition(1, 1)));
        // 2) Mark the Tiles with their distances from each remaining Resource (Potential Fields >= 0)
        for (final Resource r : remainingResources) {
            for (int dy = -resourceDepotDimensions.getY() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dy < r.getSize().getY() + resourceDepotDimensions.getY() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dy) {
                for (int dx = -resourceDepotDimensions.getX() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dx < r.getSize().getX() + resourceDepotDimensions.getX() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dx) {
                    final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
                    if (terrainData.getMapData().isValid(deltaTilePosition)) {
                        final Tile tile = terrainData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                        int dist = (BwemExt.distToRectangle(BwemExt.center(deltaTilePosition), r.getTopLeft().toPosition(), r.getSize().toPosition()) + (TilePosition.SIZE_IN_PIXELS / 2)) / TilePosition.SIZE_IN_PIXELS;
                        int score = Math.max(BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES + 3 - dist, 0);
                        if (r instanceof Geyser) {
                            // somewhat compensates for Geyser alone vs the several minerals
                            score *= 3;
                        }
                        if (tile.getAreaId().equals(getId())) {
                            // note the additive effect (assume tile.InternalData() is 0 at the beginning)
                            tile.setInternalData(tile.getInternalData() + score);
                        }
                    }
                }
            }
        }
        // 3) Invalidate the 7 x 7 Tiles around each remaining Resource (Starcraft rule)
        for (final Resource r : remainingResources) {
            for (int dy = -3; dy < r.getSize().getY() + 3; ++dy) {
                for (int dx = -3; dx < r.getSize().getX() + 3; ++dx) {
                    final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
                    if (terrainData.getMapData().isValid(deltaTilePosition)) {
                        final Tile tileToUpdate = terrainData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                        tileToUpdate.setInternalData(-1);
                    }
                }
            }
        }
        // 4) Search the best location inside the SearchBoundingBox:
        TilePosition bestLocation = null;
        int bestScore = 0;
        final List<Mineral> blockingMinerals = new ArrayList<>();
        for (int y = topLeftSearchBoundingBox.getY(); y <= bottomRightSearchBoundingBox.getY(); ++y) {
            for (int x = topLeftSearchBoundingBox.getX(); x <= bottomRightSearchBoundingBox.getX(); ++x) {
                final int score = computeBaseLocationScore(terrainData, new TilePosition(x, y));
                if (score > bestScore && validateBaseLocation(terrainData, new TilePosition(x, y), blockingMinerals)) {
                    bestScore = score;
                    bestLocation = new TilePosition(x, y);
                }
            }
        }
        // 2))
        for (Resource r : remainingResources) {
            for (int dy = -resourceDepotDimensions.getY() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dy < r.getSize().getY() + resourceDepotDimensions.getY() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dy) {
                for (int dx = -resourceDepotDimensions.getX() - BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; dx < r.getSize().getX() + resourceDepotDimensions.getX() + BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES; ++dx) {
                    final TilePosition deltaTilePosition = r.getTopLeft().add(new TilePosition(dx, dy));
                    if (terrainData.getMapData().isValid(deltaTilePosition)) {
                        final Tile tileToUpdate = terrainData.getTile(deltaTilePosition, CheckMode.NO_CHECK);
                        tileToUpdate.setInternalData(0);
                    }
                }
            }
        }
        if (bestScore == 0) {
            break;
        }
        // 6) Create a new Base at bestLocation, assign to it the relevant resources and remove them
        // from RemainingResources:
        final List<Resource> assignedResources = new ArrayList<>();
        for (final Resource r : remainingResources) {
            if (BwemExt.distToRectangle(r.getCenter(), bestLocation.toPosition(), resourceDepotDimensions.toPosition()) + 2 <= BwemExt.MAX_TILES_BETWEEN_COMMAND_CENTER_AND_RESOURCES * TilePosition.SIZE_IN_PIXELS) {
                assignedResources.add(r);
            }
        }
        remainingResources.removeIf(assignedResources::contains);
        if (assignedResources.isEmpty()) {
            break;
        }
        super.bases.add(new Base(this, bestLocation, assignedResources, blockingMinerals, map.asserter));
    }
}
Also used : TilePosition(org.openbw.bwapi4j.TilePosition)

Example 12 with TilePosition

use of org.openbw.bwapi4j.TilePosition 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;
    }
}
Also used : MutablePair(ecgberht.Util.MutablePair) Area(bwem.Area) MineralPatch(org.openbw.bwapi4j.unit.MineralPatch) SCV(org.openbw.bwapi4j.unit.SCV) TilePosition(org.openbw.bwapi4j.TilePosition) Base(bwem.Base)

Example 13 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project Ecgberht by Jabbo16.

the class MoveIsland method execute.

@Override
public State execute() {
    try {
        Worker chosen = gameState.chosenWorkerDrop;
        UnitType chosenType = UnitType.Terran_Command_Center;
        TilePosition chosenTile = gameState.chosenIsland.getLocation();
        Position realEnd = Util.getUnitCenterPosition(chosenTile.toPosition(), chosenType);
        if (chosen.move(realEnd)) {
            gameState.workerBuild.put((SCV) chosen, new MutablePair<>(chosenType, chosenTile));
            gameState.deltaCash.first += chosenType.mineralPrice();
            gameState.deltaCash.second += chosenType.gasPrice();
            gameState.chosenWorkerDrop = null;
            gameState.chosenIsland = null;
            gameState.islandExpand = false;
            return State.SUCCESS;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) UnitType(org.openbw.bwapi4j.type.UnitType) TilePosition(org.openbw.bwapi4j.TilePosition) Worker(org.openbw.bwapi4j.unit.Worker)

Example 14 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project Ecgberht by Jabbo16.

the class BwemExt method centerOfBuilding.

public static Position centerOfBuilding(final TilePosition tilePosition, final TilePosition buildingSize) {
    final Position pixelSize = buildingSize.toPosition();
    final Position pixelOffset = pixelSize.divide(new Position(2, 2));
    return tilePosition.toPosition().add(pixelOffset);
}
Also used : WalkPosition(org.openbw.bwapi4j.WalkPosition) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position)

Example 15 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project Ecgberht by Jabbo16.

the class Build method execute.

@Override
public State execute() {
    try {
        List<SCV> toRemove = new ArrayList<>();
        for (Entry<SCV, MutablePair<UnitType, TilePosition>> u : gameState.workerBuild.entrySet()) {
            if (u.getKey().getOrder() != Order.PlaceBuilding && gameState.getGame().getBWMap().isVisible(u.getValue().second) && gameState.canAfford(u.getValue().first)) {
                SCV chosen = u.getKey();
                if (u.getValue().first == UnitType.Terran_Bunker) {
                    if (!chosen.build(u.getValue().second, u.getValue().first)) {
                        gameState.deltaCash.first -= u.getValue().first.mineralPrice();
                        gameState.deltaCash.second -= u.getValue().first.gasPrice();
                        toRemove.add(chosen);
                        chosen.stop(false);
                        gameState.workerIdle.add(chosen);
                    }
                } else if (u.getKey().getOrder() == Order.PlayerGuard) {
                    if (Math.random() < 0.8)
                        chosen.build(u.getValue().second, u.getValue().first);
                    else
                        chosen.move(u.getKey().getPosition().add(new Position(32, 0)));
                } else
                    chosen.build(u.getValue().second, u.getValue().first);
            }
        }
        for (SCV s : toRemove) gameState.workerBuild.remove(s);
        return State.SUCCESS;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : MutablePair(ecgberht.Util.MutablePair) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) SCV(org.openbw.bwapi4j.unit.SCV) ArrayList(java.util.ArrayList)

Aggregations

TilePosition (org.openbw.bwapi4j.TilePosition)46 ChokePoint (bwem.ChokePoint)13 Position (org.openbw.bwapi4j.Position)13 Area (bwem.Area)9 WalkPosition (org.openbw.bwapi4j.WalkPosition)8 Base (bwem.Base)7 MiniTile (bwem.tile.MiniTile)7 Tile (bwem.tile.Tile)7 ArrayList (java.util.ArrayList)7 UnitType (org.openbw.bwapi4j.type.UnitType)6 TileImpl (bwem.tile.TileImpl)4 Geyser (bwem.unit.Geyser)4 Mineral (bwem.unit.Mineral)4 Worker (org.openbw.bwapi4j.unit.Worker)4 MutablePair (ecgberht.Util.MutablePair)3 Building (org.openbw.bwapi4j.unit.Building)3 MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)3 SCV (org.openbw.bwapi4j.unit.SCV)3 Area (bwem.area.Area)2 StaticBuilding (bwem.unit.StaticBuilding)2