Search in sources :

Example 26 with Position

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

the class UtilMicro method kiteAway.

// Credits to @Yegers for a better kite method
public static Position kiteAway(final Unit unit, final Set<UnitInfo> enemies) {
    try {
        if (enemies.isEmpty())
            return null;
        Position ownPosition = unit.getPosition();
        List<MutablePair<Double, Double>> vectors = new ArrayList<>();
        // double minDistance = Double.MAX_VALUE;
        for (final UnitInfo enemy : enemies) {
            final Position enemyPosition = enemy.position;
            Position sub = ownPosition.subtract(enemyPosition);
            MutablePair<Double, Double> unitV = new MutablePair<>((double) sub.getX(), (double) sub.getY());
            // final double distance = enemy.getDistance(unit);
            /*if (distance < minDistance) {
                    minDistance = distance;
                }*/
            /*unitV.first = unitV.first / distance;
                unitV.second = unitV.second / distance;*/
            vectors.add(unitV);
        }
        // minDistance *= 2;
        /*for (MutablePair<Double, Double> vector : vectors){
                vector.first *= minDistance;
                vector.second *= minDistance;
            }*/
        // return GenericMath.add(ownPosition, GenericMath.multiply(1. / vectors.size(), GenericMath.sumAll(vectors)));
        MutablePair<Double, Double> sumAll = Util.sumPosition(vectors);
        return Util.cropPosition(Util.sumPosition(ownPosition, new Position((int) (sumAll.first / vectors.size()), (int) (sumAll.second / vectors.size()))));
    } catch (Exception e) {
        System.err.println("KiteAway Exception");
        e.printStackTrace();
        return null;
    }
}
Also used : UnitInfo(ecgberht.UnitInfo) Position(org.openbw.bwapi4j.Position) ArrayList(java.util.ArrayList)

Example 27 with Position

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

the class Graph method getPath.

public CPPath getPath(final Position a, final Position b, final MutableInt pLength) {
    final Area areaA = getNearestArea(a.toWalkPosition());
    final Area areaB = getNearestArea(b.toWalkPosition());
    if (areaA.equals(areaB)) {
        if (pLength != null) {
            pLength.setValue(BwemExt.getApproxDistance(a, b));
        }
        return new CPPath();
    }
    if (!areaA.isAccessibleFrom(areaB)) {
        if (pLength != null) {
            pLength.setValue(-1);
        }
        return new CPPath();
    }
    int minDistAB = Integer.MAX_VALUE;
    ChokePoint pBestCpA = null;
    ChokePoint pBestCpB = null;
    for (final ChokePoint cpA : areaA.getChokePoints()) {
        if (!cpA.isBlocked()) {
            final int distACpA = BwemExt.getApproxDistance(a, cpA.getCenter().toPosition());
            for (final ChokePoint cpB : areaB.getChokePoints()) {
                if (!cpB.isBlocked()) {
                    final int distBToCPB = BwemExt.getApproxDistance(b, cpB.getCenter().toPosition());
                    final int distAToB = distACpA + distBToCPB + distance(cpA, cpB);
                    if (distAToB < minDistAB) {
                        minDistAB = distAToB;
                        pBestCpA = cpA;
                        pBestCpB = cpB;
                    }
                }
            }
        }
    }
    // bwem_assert(minDistAB != numeric_limits<int>::max());
    if (minDistAB == Integer.MAX_VALUE) {
        throw new IllegalStateException();
    }
    final CPPath path = getPath(pBestCpA, pBestCpB);
    if (pLength != null) {
        // bwem_assert(Path.size() >= 1);
        if (!(path.size() >= 1)) {
            throw new IllegalStateException();
        }
        pLength.setValue(minDistAB);
        if (path.size() == 1) {
            // bwem_assert(pBestCpA == pBestCpB);
            if (!pBestCpA.equals(pBestCpB)) {
                throw new IllegalStateException();
            }
            final Position cpEnd1 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END1));
            final Position cpEnd2 = BwemExt.center(pBestCpA.getNodePosition(ChokePoint.Node.END2));
            if (Utils.intersect(a.getX(), a.getY(), b.getX(), b.getY(), cpEnd1.getX(), cpEnd1.getY(), cpEnd2.getX(), cpEnd2.getY())) {
                pLength.setValue(BwemExt.getApproxDistance(a, b));
            } else {
                for (final ChokePoint.Node node : new ChokePoint.Node[] { ChokePoint.Node.END1, ChokePoint.Node.END2 }) {
                    final Position c = BwemExt.center(pBestCpA.getNodePosition(node));
                    final int distAToB = BwemExt.getApproxDistance(a, c) + BwemExt.getApproxDistance(b, c);
                    if (distAToB < pLength.intValue()) {
                        pLength.setValue(distAToB);
                    }
                }
            }
        }
    }
    return getPath(pBestCpA, pBestCpB);
}
Also used : Area(bwem.area.Area) CPPath(bwem.typedef.CPPath) TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position) WalkPosition(org.openbw.bwapi4j.WalkPosition)

Example 28 with Position

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

the class MobileUnit method update.

@Override
public void update(int[] unitData, int index, int frame) {
    this.isFollowing = unitData[index + Unit.IS_FOLLOWING_INDEX] == 1;
    this.isHoldingPosition = unitData[index + Unit.IS_HOLDING_POSITION_INDEX] == 1;
    this.isStuck = unitData[index + Unit.IS_STUCK_INDEX] == 1;
    this.isStasised = unitData[index + Unit.IS_STASISED_INDEX] == 1;
    this.isUnderDarkSwarm = unitData[index + Unit.IS_UNDER_DARK_SWARM_INDEX] == 1;
    this.isUnderDisruptionWeb = unitData[index + Unit.IS_UNDER_DISRUPTION_WEB_INDEX] == 1;
    this.isUnderStorm = unitData[index + Unit.IS_UNDER_STORM_INDEX] == 1;
    this.isMoving = unitData[index + Unit.IS_MOVING_INDEX] == 1;
    this.isParasited = unitData[index + Unit.IS_PARASITED_INDEX] == 1;
    this.isPatrolling = unitData[index + Unit.IS_PATROLLING_INDEX] == 1;
    this.isPlagued = unitData[index + Unit.IS_PLAGUED_INDEX] == 1;
    this.targetPosition = new Position(unitData[index + Unit.TARGET_POSITION_X_INDEX], unitData[index + Unit.TARGET_POSITION_Y_INDEX]);
    this.transportId = unitData[index + Unit.TRANSPORT_INDEX];
    this.acidSporeCount = unitData[index + Unit.ACID_SPORE_COUNT_INDEX];
    this.isHallucination = unitData[index + Unit.IS_HALLUCINATION_INDEX] == 1;
    this.isBlind = unitData[index + Unit.IS_BLIND_INDEX] == 1;
    this.isBraking = unitData[index + Unit.IS_BRAKING_INDEX] == 1;
    this.isDefenseMatrixed = unitData[index + Unit.IS_DEFENSE_MATRIXED_INDEX] == 1;
    this.isEnsnared = unitData[index + Unit.IS_ENSNARED_INDEX] == 1;
    super.update(unitData, index, frame);
}
Also used : Position(org.openbw.bwapi4j.Position)

Example 29 with Position

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

the class MapTest method Test_getPath_Lengths_Using_Original_Samples_LIVE.

@Ignore
@Test
public void Test_getPath_Lengths_Using_Original_Samples_LIVE() throws IOException, URISyntaxException {
    this.bw = new BW(this);
    this.bw.startGame();
    int pathErrorsCount = 0;
    int pathsCount = 0;
    int differenceSum = 0;
    final BWEM_CPPathSamples expectedPathSamples = new BWEM_CPPathSamples(this.bw.getBWMap().mapHash());
    for (final BWEM_CPPathSamples.CPPSample expectedSample : expectedPathSamples.samples) {
        final Position sampleStartPosition = expectedSample.startAndEnd.getLeft();
        final Position sampleEndPosition = expectedSample.startAndEnd.getRight();
        final int expectedPathLength = expectedSample.pathLength;
        logger.debug("Testing: startPosition=" + sampleStartPosition.toString() + ", endPosition=" + sampleEndPosition.toString() + ", expectedPathLength=" + expectedPathLength);
        ++pathsCount;
        try {
            final MutableInt actualPathLength = new MutableInt();
            this.bwemMap.getPath(sampleStartPosition, sampleEndPosition, actualPathLength);
            final int difference = actualPathLength.intValue() - expectedPathLength;
            if (difference != 0) {
                differenceSum += difference;
                logger.warn("Path lengths do not match: expectedPathLength=" + expectedPathLength + ", actualPathLength=" + actualPathLength + ", difference=" + difference);
            }
        } catch (final Exception e) {
            logger.warn("Failed to find a path: path error: startPosition=" + sampleStartPosition.toString() + ", endPosition=" + sampleEndPosition.toString() + ", expectedPathLength=" + expectedPathLength);
            ++pathErrorsCount;
            e.printStackTrace();
        }
    }
    logger.info("Total # of Paths: " + pathsCount + ", # of Path errors: " + pathErrorsCount);
    logger.info("Average difference: " + (differenceSum / (pathsCount - pathErrorsCount)));
}
Also used : BW(org.openbw.bwapi4j.BW) Position(org.openbw.bwapi4j.Position) WalkPosition(org.openbw.bwapi4j.WalkPosition) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ChokePoint(bwem.ChokePoint) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BWEM_CPPathSamples(mockdata.BWEM_CPPathSamples) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with Position

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

the class TestListenerBwem method isOnScreen.

/**
 * Tests if the specified position is within the current viewport area.
 * Useful for ignoring shape drawing calls if the specified position is off screen.
 */
private boolean isOnScreen(final Position position) {
    final Position topLeftScreenPosition = bw.getInteractionHandler().getScreenPosition();
    final Position bottomRightScreenPosition = topLeftScreenPosition.add(SCREEN_SIZE);
    return (position.getX() >= topLeftScreenPosition.getX() && position.getY() >= topLeftScreenPosition.getY() && position.getX() < bottomRightScreenPosition.getX() && position.getY() < bottomRightScreenPosition.getY());
}
Also used : TilePosition(org.openbw.bwapi4j.TilePosition) Position(org.openbw.bwapi4j.Position)

Aggregations

Position (org.openbw.bwapi4j.Position)55 TilePosition (org.openbw.bwapi4j.TilePosition)19 UnitInfo (ecgberht.UnitInfo)10 Worker (org.openbw.bwapi4j.unit.Worker)10 UnitType (org.openbw.bwapi4j.type.UnitType)9 SimInfo (ecgberht.Simulation.SimInfo)8 Util (ecgberht.Util.Util)8 WalkPosition (org.openbw.bwapi4j.WalkPosition)8 Ecgberht.getGs (ecgberht.Ecgberht.getGs)7 UtilMicro (ecgberht.Util.UtilMicro)7 org.openbw.bwapi4j.unit (org.openbw.bwapi4j.unit)7 Area (bwem.Area)6 Base (bwem.Base)6 Collectors (java.util.stream.Collectors)6 Order (org.openbw.bwapi4j.type.Order)6 java.util (java.util)5 WeaponType (org.openbw.bwapi4j.type.WeaponType)5 ArrayList (java.util.ArrayList)4 MineralPatch (org.openbw.bwapi4j.unit.MineralPatch)4 Squad (ecgberht.Squad)3