Search in sources :

Example 26 with TilePosition

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

the class TrainUnit method execute.

@Override
public State execute() {
    try {
        if (gameState.chosenUnit == UnitType.None)
            return State.FAILURE;
        TrainingFacility chosen = gameState.chosenTrainingFacility;
        if (gameState.getStrat().name.equals("ProxyBBS")) {
            if (Util.countBuildingAll(UnitType.Terran_Barracks) == 2 && Util.countBuildingAll(UnitType.Terran_Supply_Depot) == 0) {
                gameState.chosenTrainingFacility = null;
                gameState.chosenToBuild = UnitType.None;
                return State.FAILURE;
            }
            if (gameState.getSupply() > 0) {
                chosen.train(gameState.chosenUnit);
                return State.SUCCESS;
            }
        }
        if (gameState.getStrat().name.equals("ProxyEightRax")) {
            if (Util.countBuildingAll(UnitType.Terran_Barracks) == 0 && gameState.supplyMan.getSupplyUsed() >= 16) {
                gameState.chosenTrainingFacility = null;
                gameState.chosenToBuild = UnitType.None;
                return State.FAILURE;
            }
            if (gameState.getSupply() > 0) {
                chosen.train(gameState.chosenUnit);
                return State.SUCCESS;
            }
        }
        if (gameState.getSupply() > 4 || gameState.checkSupply() || gameState.getPlayer().supplyTotal() >= 400) {
            if (!gameState.defense && gameState.chosenToBuild == UnitType.Terran_Command_Center) {
                boolean found = false;
                for (MutablePair<UnitType, TilePosition> w : gameState.workerBuild.values()) {
                    if (w.first == UnitType.Terran_Command_Center) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    gameState.chosenTrainingFacility = null;
                    gameState.chosenUnit = UnitType.None;
                    return State.FAILURE;
                }
            }
            chosen.train(gameState.chosenUnit);
            return State.SUCCESS;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : TrainingFacility(org.openbw.bwapi4j.unit.TrainingFacility) UnitType(org.openbw.bwapi4j.type.UnitType) TilePosition(org.openbw.bwapi4j.TilePosition)

Example 27 with TilePosition

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

the class UnitInfo method update.

// TODO completion frames
void update() {
    player = unit.getPlayer();
    unitType = unit.getType();
    visible = unit.isVisible();
    position = visible ? unit.getPosition() : position;
    currentOrder = unit.getOrder();
    completed = !completed && visible ? unit.isCompleted() : completed;
    tileposition = visible ? unit.getTilePosition() : tileposition;
    if (!unitType.isBuilding())
        walkposition = new Position(unit.getLeft(), unit.getTop()).toWalkPosition();
    else
        walkposition = tileposition.toWalkPosition();
    if (visible) {
        lastPosition = position;
        lastTileposition = tileposition;
        lastWalkposition = walkposition;
    }
    lastVisibleFrame = visible ? getGs().frameCount : lastVisibleFrame;
    lastAttackFrame = unit.isStartingAttack() ? getGs().frameCount : lastVisibleFrame;
    if (unit instanceof GroundAttacker)
        groundRange = player.getUnitStatCalculator().weaponMaxRange(unitType.groundWeapon());
    if (unit instanceof AirAttacker)
        airRange = player.getUnitStatCalculator().weaponMaxRange(unitType.airWeapon());
    if (unit instanceof Bunker) {
        airRange = 5 * 32;
        groundRange = 5 * 32;
    }
    health = visible ? unit.getHitPoints() : expectedHealth();
    shields = visible ? unit.getShields() : expectedShields();
    if (unit instanceof SpellCaster)
        energy = ((SpellCaster) unit).getEnergy();
    percentHealth = unitType.maxHitPoints() > 0 ? (double) health / (double) unitType.maxHitPoints() : 1.0;
    percentShield = unitType.maxShields() > 0 ? (double) shields / (double) unitType.maxShields() : 1.0;
    if (unit instanceof Burrowable && visible)
        burrowed = ((Burrowable) unit).isBurrowed();
    if (visible)
        flying = unit.isFlying();
    speed = Util.getSpeed(this);
    target = unit instanceof Attacker ? ((Attacker) unit).getTargetUnit() : unit.getOrderTarget();
    attackers.clear();
}
Also used : WalkPosition(org.openbw.bwapi4j.WalkPosition) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position)

Example 28 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class AreaInitializerImpl method computeDistances.

@Override
public int[] computeDistances(final ChokePoint startCP, final List<ChokePoint> targetCPs) {
    // bwem_assert(!contains(targetCPs, startCP));
    if (targetCPs.contains(startCP)) {
        throw new IllegalStateException();
    }
    final TilePosition start = getMap().breadthFirstSearch(startCP.getNodePositionInArea(ChokePoint.Node.MIDDLE, this).toTilePosition(), // findCond
    args -> {
        final Object ttile = args[0];
        if (ttile instanceof Tile) {
            final Tile tile = (Tile) ttile;
            return tile.getAreaId().equals(getId());
        } else {
            throw new IllegalArgumentException();
        }
    }, // visitCond
    args -> true);
    final List<TilePosition> targets = new ArrayList<>();
    for (final ChokePoint cp : targetCPs) {
        final TilePosition t = getMap().breadthFirstSearch(cp.getNodePositionInArea(ChokePoint.Node.MIDDLE, this).toTilePosition(), // findCond
        args -> {
            final Object ttile = args[0];
            if (ttile instanceof Tile) {
                final Tile tile = (Tile) ttile;
                return (tile.getAreaId().equals(getId()));
            } else {
                throw new IllegalArgumentException();
            }
        }, // visitCond
        args -> true);
        targets.add(t);
    }
    return computeDistances(start, targets);
}
Also used : TilePosition(org.openbw.bwapi4j.TilePosition) ArrayList(java.util.ArrayList) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) ChokePoint(bwem.ChokePoint)

Example 29 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class AreaInitializerImpl method computeBaseLocationScore.

@Override
public int computeBaseLocationScore(final AdvancedData mapAdvancedData, final TilePosition location) {
    final TilePosition dimCC = UnitType.Terran_Command_Center.tileSize();
    int sumScore = 0;
    for (int dy = 0; dy < dimCC.getY(); ++dy) for (int dx = 0; dx < dimCC.getX(); ++dx) {
        final Tile tile = mapAdvancedData.getTile(location.add(new TilePosition(dx, dy)), CheckMode.NO_CHECK);
        if (!tile.isBuildable()) {
            return -1;
        }
        if (((TileImpl) tile).getInternalData() == -1) {
            // Unfortunately, this is guaranteed only for the resources in this Area, which is the very reason of validateBaseLocation
            return -1;
        }
        if (!tile.getAreaId().equals(getId())) {
            return -1;
        }
        if (tile.getNeutral() != null && (tile.getNeutral() instanceof StaticBuilding)) {
            return -1;
        }
        sumScore += ((TileImpl) tile).getInternalData();
    }
    return sumScore;
}
Also used : TileImpl(bwem.tile.TileImpl) StaticBuilding(bwem.unit.StaticBuilding) TilePosition(org.openbw.bwapi4j.TilePosition) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) ChokePoint(bwem.ChokePoint)

Example 30 with TilePosition

use of org.openbw.bwapi4j.TilePosition in project BWAPI4J by OpenBW.

the class MapPrinterExample method printMap.

public void printMap(Map theMap) {
    java.util.Map<Integer, Color> mapZoneColor = new HashMap<>();
    for (int y = 0; y < theMap.getData().getMapData().getWalkSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getWalkSize().getX(); ++x) {
        WalkPosition p = new WalkPosition(x, y);
        MiniTile miniTile = theMap.getData().getMiniTile(p, CheckMode.NO_CHECK);
        Color col;
        if (miniTile.isSea()) {
            if (mapPrinter.showSeaSide && theMap.getData().isSeaWithNonSeaNeighbors(p))
                col = MapPrinter.CustomColor.SEA_SIDE.color();
            else
                col = MapPrinter.CustomColor.SEA.color();
        } else {
            if (mapPrinter.showLakes && miniTile.isLake()) {
                col = MapPrinter.CustomColor.LAKE.color();
            } else {
                if (mapPrinter.showAltitude) {
                    int c = 255 - ((miniTile.getAltitude().intValue() * 255) / theMap.getHighestAltitude().intValue());
                    col = new Color(c, c, c);
                } else {
                    col = MapPrinter.CustomColor.TERRAIN.color();
                }
                if (mapPrinter.showAreas || mapPrinter.showContinents)
                    if (miniTile.getAreaId().intValue() > 0) {
                        Area area = theMap.getArea(miniTile.getAreaId());
                        Color zoneColor = getZoneColor(area, mapZoneColor);
                        int red = zoneColor.getRed() * col.getRed() / 255;
                        int green = zoneColor.getGreen() * col.getGreen() / 255;
                        col = new Color(red, green, 0);
                    } else {
                        col = MapPrinter.CustomColor.TINY_AREA.color();
                    }
            }
        }
        mapPrinter.point(p, col);
    }
    if (mapPrinter.showData)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            int data = ((TileImpl) theMap.getData().getTile(new TilePosition(x, y))).getInternalData();
            int c = (((data / 1) * 1) % 256);
            Color col = new Color(c, c, c);
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(new WalkPosition(3, 3)), col, MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showUnbuildable)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) if (!theMap.getData().getTile(new TilePosition(x, y)).isBuildable()) {
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin.add(new WalkPosition(1, 1)), origin.add(new WalkPosition(2, 2)), MapPrinter.CustomColor.UNBUILDABLE.color());
        }
    if (mapPrinter.showGroundHeight)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            Tile.GroundHeight groundHeight = theMap.getData().getTile(new TilePosition(x, y)).getGroundHeight();
            if (groundHeight.intValue() >= Tile.GroundHeight.HIGH_GROUND.intValue())
                for (int dy = 0; dy < 4; ++dy) for (int dx = 0; dx < 4; ++dx) {
                    WalkPosition p = (new TilePosition(x, y).toWalkPosition()).add(new WalkPosition(dx, dy));
                    if (// groundHeight is usefull only for walkable miniTiles
                    theMap.getData().getMiniTile(p, CheckMode.NO_CHECK).isWalkable())
                        if (((dx + dy) & (groundHeight == Tile.GroundHeight.HIGH_GROUND ? 1 : 3)) != 0)
                            mapPrinter.point(p, MapPrinter.CustomColor.HIGHER_GROUND.color());
                }
        }
    if (mapPrinter.showAssignedResources)
        for (Area area : theMap.getAreas()) for (Base base : area.getBases()) {
            for (Mineral m : base.getMinerals()) mapPrinter.line(base.getCenter().toWalkPosition(), m.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
            for (Geyser g : base.getGeysers()) mapPrinter.line(base.getCenter().toWalkPosition(), g.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
        }
    if (mapPrinter.showGeysers)
        for (Geyser g : theMap.getNeutralData().getGeysers()) printNeutral(theMap, g, MapPrinter.CustomColor.GEYSERS.color());
    if (mapPrinter.showMinerals)
        for (Mineral m : theMap.getNeutralData().getMinerals()) printNeutral(theMap, m, MapPrinter.CustomColor.MINERALS.color());
    if (mapPrinter.showStaticBuildings)
        for (StaticBuilding s : theMap.getNeutralData().getStaticBuildings()) printNeutral(theMap, s, MapPrinter.CustomColor.STATIC_BUILDINGS.color());
    if (mapPrinter.showStartingLocations)
        for (TilePosition t : theMap.getData().getMapData().getStartingLocations()) {
            WalkPosition origin = t.toWalkPosition();
            // same size for other races
            WalkPosition size = UnitType.Terran_Command_Center.tileSize().toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.STARTING_LOCATIONS.color(), MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showBases)
        for (Area area : theMap.getAreas()) {
            for (Base base : area.getBases()) {
                WalkPosition origin = base.getLocation().toWalkPosition();
                // same size for other races
                WalkPosition size = UnitType.Terran_Command_Center.tileSize().toWalkPosition();
                MapPrinter.dashed_t dashMode = base.getBlockingMinerals().isEmpty() ? MapPrinter.dashed_t.not_dashed : MapPrinter.dashed_t.dashed;
                mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.BASES.color(), MapPrinter.fill_t.do_not_fill, dashMode);
            }
        }
    if (mapPrinter.showChokePoints) {
        for (MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> f : theMap.getRawFrontier()) mapPrinter.point(f.getRight(), mapPrinter.showAreas ? MapPrinter.CustomColor.CHOKE_POINTS_SHOW_AREAS.color() : MapPrinter.CustomColor.CHOKE_POINTS_SHOW_CONTINENTS.color());
        for (Area area : theMap.getAreas()) for (ChokePoint cp : area.getChokePoints()) {
            ChokePoint.Node[] nodes = { ChokePoint.Node.END1, ChokePoint.Node.END2 };
            for (ChokePoint.Node n : nodes) mapPrinter.square(cp.getNodePosition(n), 1, new Color(255, 0, 255), MapPrinter.fill_t.fill);
            mapPrinter.square(cp.getCenter(), 1, new Color(0, 0, 255), MapPrinter.fill_t.fill);
        }
    }
    try {
        mapPrinter.writeImageToFile(Paths.get("map.png"), "png");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : TileImpl(bwem.tile.TileImpl) Mineral(bwem.unit.Mineral) StaticBuilding(bwem.unit.StaticBuilding) HashMap(java.util.HashMap) MiniTile(bwem.tile.MiniTile) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) IOException(java.io.IOException) ChokePoint(bwem.ChokePoint) Base(bwem.Base) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Area(bwem.area.Area) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition) WalkPosition(org.openbw.bwapi4j.WalkPosition) ChokePoint(bwem.ChokePoint)

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