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;
}
}
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);
}
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);
}
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)));
}
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());
}
Aggregations