Search in sources :

Example 21 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class WraithAgent method runAgent.

@Override
public boolean runAgent() {
    // TODO improve
    try {
        if (!unit.exists() || unitInfo == null)
            return true;
        actualFrame = getGs().frameCount;
        frameLastOrder = unit.getLastCommandFrame();
        airAttackers.clear();
        if (frameLastOrder == actualFrame)
            return false;
        Position attack = getBestBaseToHarass();
        UnitInfo closestThreat = null;
        double bestDist = Double.MAX_VALUE;
        SimInfo airSim = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.AIR);
        airAttackers = airSim.enemies;
        for (UnitInfo u : airAttackers) {
            double predictedDist = unitInfo.getPredictedDistance(u);
            if (predictedDist < bestDist) {
                closestThreat = u;
                bestDist = predictedDist;
            }
        }
        Set<UnitInfo> mainTargets = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.MIX).enemies;
        UnitInfo harassed = chooseHarassTarget(mainTargets);
        if (closestThreat != null) {
            Weapon myWeapon = closestThreat.flying ? unit.getAirWeapon() : unit.getGroundWeapon();
            double hisAirWeaponRange = closestThreat.airRange;
            Position kitePos = UtilMicro.kiteAway(unit, new TreeSet<>(Collections.singleton(closestThreat)));
            if (myWeapon.maxRange() > hisAirWeaponRange && bestDist > hisAirWeaponRange) {
                if (myWeapon.cooldown() != 0) {
                    if (kitePos != null) {
                        UtilMicro.move(unit, kitePos);
                        return false;
                    }
                } else if (harassed != null && unitInfo.getDistance(harassed) <= myWeapon.maxRange()) {
                    UtilMicro.attack(unitInfo, harassed);
                } else
                    UtilMicro.attack(unitInfo, closestThreat);
                return false;
            }
            if (kitePos != null) {
                UtilMicro.move(unit, kitePos);
                return false;
            }
        }
        if (harassed != null) {
            UtilMicro.attack(unitInfo, harassed);
            return false;
        }
        UnitInfo target = Util.getRangedTarget(unitInfo, mainTargets);
        if (target != null) {
            UtilMicro.attack(unitInfo, target);
            return false;
        }
        if (attack != null) {
            if (attack.getDistance(myUnit.getPosition()) >= 32 * 5)
                UtilMicro.move(unit, attack);
            else
                UtilMicro.attack(unit, attack);
            return false;
        }
        return false;
    } catch (Exception e) {
        System.err.println("Exception WraithAgent");
        e.printStackTrace();
    }
    return false;
}
Also used : UnitInfo(ecgberht.UnitInfo) SimInfo(ecgberht.Simulation.SimInfo) Position(org.openbw.bwapi4j.Position) Weapon(org.openbw.bwapi4j.unit.Weapon)

Example 22 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class CheckPerimeter method execute.

@Override
public State execute() {
    try {
        gameState.enemyInBase.clear();
        gameState.defense = false;
        Set<Unit> enemyInvaders = new TreeSet<>(gameState.enemyCombatUnitMemory);
        for (UnitInfo u : gameState.unitStorage.getEnemyUnits().values().stream().filter(u -> u.unitType.isBuilding()).collect(Collectors.toSet())) {
            if (u.unitType.canAttack() || u.unitType == UnitType.Protoss_Pylon || u.unitType.canProduce() || u.unitType.isRefinery()) {
                enemyInvaders.add(u.unit);
            }
        }
        for (Unit u : enemyInvaders) {
            UnitType uType = u.getType();
            if (u instanceof Building || ((uType.canAttack() || uType.isSpellcaster() || u instanceof Loadable) && uType != UnitType.Zerg_Scourge && uType != UnitType.Terran_Valkyrie && uType != UnitType.Protoss_Corsair && !(u instanceof Overlord))) {
                Area enemyArea = gameState.bwem.getMap().getArea(u.getTilePosition());
                if (enemyArea != null) {
                    Area myMainArea = gameState.mainCC != null ? gameState.mainCC.first.getArea() : null;
                    Area myNatArea = gameState.naturalArea;
                    for (Base b : gameState.CCs.keySet()) {
                        if (!b.getArea().equals(enemyArea))
                            continue;
                        if ((myMainArea != null && !b.getArea().equals(myMainArea) && (myNatArea != null && !b.getArea().equals(myNatArea))))
                            continue;
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Map.Entry<SCV, Building> c : gameState.workerTask.entrySet()) {
                    int dist = c.getValue() instanceof CommandCenter ? 500 : 200;
                    if (Util.broodWarDistance(u.getPosition(), c.getValue().getPosition()) <= dist) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.CCs.values()) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 500) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.DBs.keySet()) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (Unit c : gameState.SBs) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                for (ResearchingFacility c : gameState.UBs) {
                    if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                        gameState.enemyInBase.add(u);
                        break;
                    }
                }
                if (!gameState.getStrat().name.equals("ProxyBBS") && !gameState.getStrat().name.equals("ProxyEightRax")) {
                    for (Unit c : gameState.MBs) {
                        if (Util.broodWarDistance(u.getPosition(), c.getPosition()) <= 200) {
                            gameState.enemyInBase.add(u);
                            break;
                        }
                    }
                }
            }
        }
        if (!gameState.enemyInBase.isEmpty()) {
            /*if ((((GameState) gameState).getArmySize() >= 50 && ((GameState) gameState).getArmySize() / ((GameState) gameState).enemyInBase.size() > 10)) {
                    return State.FAILURE;
                }*/
            gameState.defense = true;
            return State.SUCCESS;
        }
        int cFrame = gameState.frameCount;
        List<Worker> toDelete = new ArrayList<>();
        for (Worker u : gameState.workerDefenders.keySet()) {
            if (u.getLastCommandFrame() == cFrame)
                continue;
            Position closestDefense;
            if (gameState.learningManager.isNaughty()) {
                if (!gameState.DBs.isEmpty()) {
                    closestDefense = gameState.DBs.keySet().iterator().next().getPosition();
                    u.move(closestDefense);
                    toDelete.add(u);
                    continue;
                }
            }
            closestDefense = gameState.getNearestCC(u.getPosition(), false);
            if (closestDefense != null) {
                u.move(closestDefense);
                toDelete.add(u);
            }
        }
        for (Worker u : toDelete) {
            u.stop(false);
            gameState.workerDefenders.remove(u);
            gameState.workerIdle.add(u);
        }
        for (Squad u : gameState.sqManager.squads.values()) {
            if (u.status == Status.DEFENSE) {
                Position closestCC = gameState.getNearestCC(u.getSquadCenter(), false);
                if (closestCC != null) {
                    Area squad = gameState.bwem.getMap().getArea(u.getSquadCenter().toTilePosition());
                    Area regCC = gameState.bwem.getMap().getArea(closestCC.toTilePosition());
                    if (squad != null && regCC != null) {
                        if (!squad.equals(regCC)) {
                            if (!gameState.DBs.isEmpty() && gameState.CCs.size() == 1) {
                                u.giveMoveOrder(gameState.DBs.keySet().iterator().next().getPosition());
                            } else {
                                u.giveMoveOrder(Util.getClosestChokepoint(u.getSquadCenter()).getCenter().toPosition());
                            }
                            u.status = Status.IDLE;
                            u.attack = null;
                            continue;
                        }
                    }
                    u.status = Status.IDLE;
                    u.attack = null;
                    continue;
                }
                u.status = Status.IDLE;
                u.attack = null;
            }
        }
        gameState.defense = false;
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : java.util(java.util) Util(ecgberht.Util.Util) Area(bwem.Area) UnitInfo(ecgberht.UnitInfo) Squad(ecgberht.Squad) Collectors(java.util.stream.Collectors) Status(ecgberht.Squad.Status) Conditional(org.iaie.btree.task.leaf.Conditional) UnitType(org.openbw.bwapi4j.type.UnitType) org.openbw.bwapi4j.unit(org.openbw.bwapi4j.unit) GameState(ecgberht.GameState) Base(bwem.Base) State(org.iaie.btree.BehavioralTree.State) Position(org.openbw.bwapi4j.Position) Position(org.openbw.bwapi4j.Position) Base(bwem.Base) UnitInfo(ecgberht.UnitInfo) Area(bwem.Area) UnitType(org.openbw.bwapi4j.type.UnitType) Squad(ecgberht.Squad)

Example 23 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class VesselAgent method getNewStatus.

private void getNewStatus() {
    SimInfo mySimAir = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.AIR);
    SimInfo mySimMix = getGs().sim.getSimulation(unitInfo, SimInfo.SimType.MIX);
    boolean chasenByScourge = false;
    boolean sporeColony = false;
    double maxScore = 0;
    PlayerUnit chosen = null;
    if (getGs().enemyRace == Race.Zerg && !mySimAir.enemies.isEmpty()) {
        for (UnitInfo u : mySimAir.enemies) {
            if (u.unit instanceof Scourge && u.target.equals(unit))
                chasenByScourge = true;
            else if (u.unit instanceof SporeColony && unitInfo.getDistance(u) < u.airRange * 1.2)
                sporeColony = true;
            if (chasenByScourge && sporeColony)
                break;
        }
    }
    if (!mySimMix.enemies.isEmpty()) {
        // Irradiate
        Set<UnitInfo> irradiateTargets = new TreeSet<>(mySimMix.enemies);
        for (UnitInfo t : mySimMix.allies) {
            if (t.unit instanceof SiegeTank)
                irradiateTargets.add(t);
        }
        if (follow != null && !irradiateTargets.isEmpty() && getGs().getPlayer().hasResearched(TechType.Irradiate) && unit.getEnergy() >= TechType.Irradiate.energyCost() && follow.status != Squad.Status.IDLE) {
            for (UnitInfo u : irradiateTargets) {
                if (!u.visible)
                    continue;
                if (u.unit instanceof Building || u.unit instanceof Egg || (!(u.unit instanceof Organic) && !(u.unit instanceof SiegeTank)))
                    continue;
                if (u.unit instanceof MobileUnit && (u.unit.isIrradiated() || ((MobileUnit) u.unit).isStasised()))
                    continue;
                if (getGs().wizard.isUnitIrradiated(u.unit))
                    continue;
                double score = 1;
                int closeUnits = 0;
                for (UnitInfo close : irradiateTargets) {
                    if (u.equals(close) || !(close.unit instanceof Organic) || close.burrowed)
                        continue;
                    if (u.getDistance(close) <= 32)
                        closeUnits++;
                }
                if (// Kill it with fire!!
                u.unitType == UnitType.Zerg_Lurker)
                    // Kill it with fire!!
                    score = u.burrowed ? 20 : 18;
                else if (u.unitType == UnitType.Zerg_Mutalisk)
                    score = 8;
                else if (u.unitType == UnitType.Zerg_Hydralisk)
                    score = 5;
                else if (u.unitType == UnitType.Zerg_Zergling)
                    score = 2;
                // Prefer healthy units
                score *= u.percentHealth;
                double multiplier = u.unit instanceof SiegeTank ? 3.75 : u.unitType == UnitType.Zerg_Lurker ? 2.5 : u.unitType == UnitType.Zerg_Mutalisk ? 2 : 1;
                score += multiplier * closeUnits;
                if (chosen == null || score > maxScore) {
                    chosen = u.unit;
                    maxScore = score;
                }
            }
            if (maxScore >= 5) {
                status = Status.IRRADIATE;
                target = chosen;
                return;
            }
        }
        chosen = null;
        maxScore = 0;
        // EMP
        Set<UnitInfo> empTargets = new TreeSet<>(mySimMix.enemies);
        if (follow != null && !empTargets.isEmpty() && getGs().getPlayer().hasResearched(TechType.EMP_Shockwave) && unit.getEnergy() >= TechType.EMP_Shockwave.energyCost() && follow.status != Squad.Status.IDLE) {
            for (UnitInfo u : empTargets) {
                // TODO Change to rectangle to choose best Position and track emped positions
                if (!u.visible || u.unit instanceof Building || u.unit instanceof Worker || u.unit instanceof MobileUnit && (u.unit.isIrradiated() || ((MobileUnit) u.unit).isStasised()))
                    continue;
                if (getGs().wizard.isUnitEMPed(u.unit))
                    continue;
                double score = 1;
                double closeUnits = 0;
                for (UnitInfo close : empTargets) {
                    if (u.equals(close))
                        continue;
                    if (close.lastPosition.getDistance(u.lastPosition) <= WeaponType.EMP_Shockwave.innerSplashRadius())
                        closeUnits += close.shields * 0.6;
                }
                if (u.unitType == UnitType.Protoss_High_Templar)
                    score = 10;
                else if (u.unitType == UnitType.Protoss_Arbiter)
                    score = 7;
                else if (u.unitType == UnitType.Protoss_Archon || u.unitType == UnitType.Protoss_Dark_Archon)
                    score = 5;
                // Prefer healthy units(shield)
                score *= u.percentShield;
                double multiplier = u.unitType == UnitType.Protoss_High_Templar ? 6 : 1;
                score += multiplier * closeUnits;
                if (chosen == null || score > maxScore) {
                    chosen = u.unit;
                    maxScore = score;
                }
            }
            if (maxScore >= 6) {
                status = Status.EMP;
                target = chosen;
                return;
            }
        }
        chosen = null;
        maxScore = 0;
    }
    if (!mySimMix.allies.isEmpty()) {
        // Defense Matrix
        Set<UnitInfo> matrixTargets = new TreeSet<>(mySimMix.allies);
        if (follow != null && !matrixTargets.isEmpty() && unit.getEnergy() >= TechType.Defensive_Matrix.energyCost() && follow.status != Squad.Status.IDLE) {
            for (UnitInfo u : matrixTargets) {
                if (!(u.unit instanceof MobileUnit))
                    continue;
                if (getGs().wizard.isDefenseMatrixed((MobileUnit) u.unit))
                    continue;
                double score = 1;
                if (!u.unit.isUnderAttack() || ((MobileUnit) u.unit).isDefenseMatrixed())
                    continue;
                if (u.unitType.isMechanical())
                    score = 8;
                if (u.unitType == UnitType.Terran_Marine || u.unitType == UnitType.Terran_Firebat)
                    score = 3;
                if (u.unitType == UnitType.Terran_SCV || u.unitType == UnitType.Terran_Medic)
                    score = 1;
                score *= (double) u.unitType.maxHitPoints() / (double) u.health;
                if (chosen == null || score > maxScore) {
                    chosen = u.unit;
                    maxScore = score;
                }
            }
            if (maxScore >= 2) {
                status = Status.DMATRIX;
                target = chosen;
                return;
            }
        }
    }
    if ((status == Status.IRRADIATE || status == Status.DMATRIX || status == Status.EMP) && target != null)
        return;
    if (!mySimAir.enemies.isEmpty()) {
        if (unit.isUnderAttack() || chasenByScourge || sporeColony)
            status = Status.KITE;
        else if (Util.broodWarDistance(unit.getPosition(), center) >= 100)
            status = Status.FOLLOW;
        else if (mySimAir.lose)
            status = Status.KITE;
    } else if (mySimMix.lose)
        status = Status.RETREAT;
    else if (Util.broodWarDistance(unit.getPosition(), center) >= 200)
        status = Status.FOLLOW;
    else
        status = Status.HOVER;
}
Also used : SimInfo(ecgberht.Simulation.SimInfo) UnitInfo(ecgberht.UnitInfo) TreeSet(java.util.TreeSet)

Example 24 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class CheckHarasserAttacked method execute.

@Override
public State execute() {
    try {
        if (gameState.enemyMainBase == null) {
            gameState.chosenUnitToHarass = null;
            gameState.chosenHarasser = null;
            return State.FAILURE;
        }
        if (gameState.chosenUnitToHarass != null) {
            if (!gameState.bw.getBWMap().isValidPosition(gameState.chosenUnitToHarass.getPosition())) {
                gameState.chosenUnitToHarass = null;
            }
        }
        UnitInfo attacker = null;
        int workers = 0;
        Set<UnitInfo> attackers = new TreeSet<>();
        // Thanks to @N00byEdge for cleaner code
        for (UnitInfo u : gameState.unitStorage.getAllyUnits().get(gameState.chosenHarasser).attackers) {
            if (!(u.unit instanceof Building) && u.unit instanceof Attacker && u.unit.exists()) {
                if (u.unit instanceof Worker) {
                    workers++;
                    attacker = u;
                }
                attackers.add(u);
            }
        }
        if (workers > 1) {
            gameState.learningManager.setHarass(true);
            gameState.chosenUnitToHarass = null;
            return State.FAILURE;
        }
        if (attackers.isEmpty()) {
            if (!gameState.getGame().getBWMap().isVisible(gameState.enemyMainBase.getLocation()) && gameState.chosenUnitToHarass == null) {
                gameState.chosenHarasser.move(gameState.enemyMainBase.getLocation().toPosition());
            }
            return State.SUCCESS;
        } else {
            boolean winHarass = gameState.sim.simulateHarass(gameState.chosenHarasser, attackers, 70);
            if (winHarass) {
                if (workers == 1 && !attacker.unit.equals(gameState.chosenUnitToHarass)) {
                    UtilMicro.attack(gameState.chosenHarasser, attacker);
                    gameState.chosenUnitToHarass = attacker.unit;
                    return State.SUCCESS;
                }
            } else {
                if (IntelligenceAgency.getEnemyStrat() == IntelligenceAgency.EnemyStrats.Unknown) {
                    gameState.explore = true;
                    gameState.chosenUnitToHarass = null;
                    gameState.chosenHarasser.stop(false);
                    return State.FAILURE;
                } else if (gameState.chosenHarasser.getHitPoints() <= 15) {
                    gameState.workerIdle.add(gameState.chosenHarasser);
                    gameState.chosenHarasser.stop(false);
                    gameState.chosenHarasser = null;
                    gameState.chosenUnitToHarass = null;
                } else {
                    // Position kite = UtilMicro.kiteAway(gameState.chosenHarasser, attackers);
                    Optional<UnitInfo> closestUnit = attackers.stream().min(Comparator.comparing(u -> u.getDistance(gameState.chosenHarasser)));
                    Position kite = closestUnit.map(unit1 -> UtilMicro.kiteAwayAlt(gameState.chosenHarasser.getPosition(), unit1.position)).orElse(null);
                    if (kite != null && gameState.bw.getBWMap().isValidPosition(kite)) {
                        UtilMicro.move(gameState.chosenHarasser, kite);
                        gameState.chosenUnitToHarass = null;
                    } else {
                        kite = UtilMicro.kiteAway(gameState.chosenHarasser, attackers);
                        if (kite != null && gameState.bw.getBWMap().isValidPosition(kite)) {
                            UtilMicro.move(gameState.chosenHarasser, kite);
                            gameState.chosenUnitToHarass = null;
                        }
                    }
                }
                return State.FAILURE;
            }
        }
        return State.SUCCESS;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : Building(org.openbw.bwapi4j.unit.Building) UnitInfo(ecgberht.UnitInfo) Attacker(org.openbw.bwapi4j.unit.Attacker) Optional(java.util.Optional) Position(org.openbw.bwapi4j.Position) TreeSet(java.util.TreeSet) Worker(org.openbw.bwapi4j.unit.Worker)

Example 25 with UnitInfo

use of ecgberht.UnitInfo in project Ecgberht by Jabbo16.

the class ChooseBuildingToHarass method execute.

@Override
public State execute() {
    try {
        if (gameState.chosenUnitToHarass != null)
            return State.FAILURE;
        for (UnitInfo u : gameState.unitStorage.getEnemyUnits().values().stream().filter(u -> u.unitType.isBuilding()).collect(Collectors.toSet())) {
            if (gameState.enemyMainBase != null && gameState.bwem.getMap().getArea(u.tileposition).equals(gameState.bwem.getMap().getArea(gameState.enemyMainBase.getLocation()))) {
                gameState.chosenUnitToHarass = u.unit;
                return State.SUCCESS;
            }
        }
        if (gameState.chosenHarasser.isIdle()) {
            gameState.workerIdle.add(gameState.chosenHarasser);
            gameState.chosenHarasser.stop(false);
            gameState.chosenHarasser = null;
            gameState.chosenUnitToHarass = null;
        }
        return State.FAILURE;
    } catch (Exception e) {
        System.err.println(this.getClass().getSimpleName());
        e.printStackTrace();
        return State.ERROR;
    }
}
Also used : UnitInfo(ecgberht.UnitInfo) GameState(ecgberht.GameState) State(org.iaie.btree.BehavioralTree.State) Action(org.iaie.btree.task.leaf.Action) Collectors(java.util.stream.Collectors) UnitInfo(ecgberht.UnitInfo)

Aggregations

UnitInfo (ecgberht.UnitInfo)32 Position (org.openbw.bwapi4j.Position)12 ArrayList (java.util.ArrayList)9 Base (bwem.Base)7 Util (ecgberht.Util.Util)7 Collectors (java.util.stream.Collectors)7 Ecgberht.getGs (ecgberht.Ecgberht.getGs)6 MutablePair (ecgberht.Util.MutablePair)6 List (java.util.List)6 Set (java.util.Set)6 org.openbw.bwapi4j.unit (org.openbw.bwapi4j.unit)6 Area (bwem.Area)5 SimInfo (ecgberht.Simulation.SimInfo)5 Squad (ecgberht.Squad)5 ChokePoint (bwem.ChokePoint)4 TreeSet (java.util.TreeSet)4 org.openbw.bwapi4j.type (org.openbw.bwapi4j.type)4 Cluster (ecgberht.Clustering.Cluster)3 ConfigManager (ecgberht.ConfigManager)3 GameState (ecgberht.GameState)3