Skip to content

Commit

Permalink
feat: positional features added (#79)
Browse files Browse the repository at this point in the history
* feat: positional features added
-calculateExpansionLocations() is now gives accurate base positions using a new algorithm, and fast as it no longer makes placement queries
-Point now has both add() methods
-Point2d has new helpful methods
-deprecated ExpansionParameters as it is no longer used

* fix: fixed unit test for new cluster() method

* fix: clean up and debug fix for displaying expansion locations

* fix: increment version in poms

---------

Co-authored-by: ketroc <jwelsh1@gmail.com>
  • Loading branch information
Ketroc and ketroc authored Dec 8, 2024
1 parent 7e2b4e3 commit 95627ab
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 127 deletions.
2 changes: 1 addition & 1 deletion ocraft-s2client-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ocraft-s2client-benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ocraft-s2client-bot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

/**
* Some nice parameters that generally work but may require tuning for certain maps.
* @deprecated this class is no longer used in the calculateExpansionLocations() algorithm
*/
@Deprecated
public final class ExpansionParameters {

private final List<Double> radiuses = new ArrayList<>();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

import com.github.ocraft.s2client.protocol.spatial.Point;
import com.github.ocraft.s2client.protocol.spatial.Point2d;
import com.github.ocraft.s2client.protocol.unit.Tag;
import org.junit.jupiter.api.Test;

Expand All @@ -46,13 +47,13 @@ void clustersUnits() {
UnitInPool unit03 = new UnitInPool(Tag.of(3L)).update(mockUnit(Point.of(0.5f, 0.1f, 0.0f)), 0, true);
UnitInPool unit04 = new UnitInPool(Tag.of(4L)).update(mockUnit(Point.of(9.5f, 5.2f, 3.1f)), 0, true);

Map<Point, List<UnitInPool>> clusters = QueryInterface.cluster(List.of(unit01, unit02, unit03, unit04), 1.0f);
Map<Point2d, List<UnitInPool>> clusters = QueryInterface.cluster(List.of(unit01, unit02, unit03, unit04), 1.0f);

Map<Point, List<UnitInPool>> expectedClusters = Map.of(
Point.of(0.4f, 0.05f, 0.1f), List.of(unit02, unit03),
Point.of(9.75f, 5.1f, 3.05f), List.of(unit01, unit04));
Map<Point2d, List<UnitInPool>> expectedClusters = Map.of(
Point2d.of(0.4f, 0.05f), List.of(unit02, unit03),
Point2d.of(9.75f, 5.1f), List.of(unit01, unit04)
);

assertThat(clusters).containsAllEntriesOf(expectedClusters);

}
}
2 changes: 1 addition & 1 deletion ocraft-s2client-protocol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public Point add(Point pointToAdd) {
return new Point(x + pointToAdd.getX(), y + pointToAdd.getY(), z + pointToAdd.getZ());
}

public Point add(float addX, float addY, float addZ) {
return new Point(x + addX, y + addY, z + addZ);
}

public Point sub(Point pointToSubtract) {
return new Point(x - pointToSubtract.getX(), y - pointToSubtract.getY(), z - pointToSubtract.getZ());
}
Expand Down Expand Up @@ -151,9 +155,9 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
result = 31 * result + (z != +0.0f ? Float.floatToIntBits(z) : 0);
int result = (x != 0.0f ? Float.floatToIntBits(x) : 0);
result = 31 * result + (y != 0.0f ? Float.floatToIntBits(y) : 0);
result = 31 * result + (z != 0.0f ? Float.floatToIntBits(z) : 0);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import SC2APIProtocol.Common;
import com.github.ocraft.s2client.protocol.Sc2ApiSerializable;
import com.github.ocraft.s2client.protocol.Strings;
import com.github.ocraft.s2client.protocol.unit.Unit;

import static com.github.ocraft.s2client.protocol.DataExtractor.tryGet;
import static com.github.ocraft.s2client.protocol.Errors.required;
Expand Down Expand Up @@ -110,10 +111,7 @@ public Point2d mul(float mulBy) {
}

public double distance(Point2d b) {
float x1 = x - b.getX();
float y1 = y - b.getY();

return Math.sqrt(x1 * x1 + y1 * y1);
return Math.hypot(x - b.getX(), y - b.getY());
}

public float dot(Point2d b) {
Expand All @@ -124,6 +122,78 @@ public Point toPoint(float z) {
return Point.of(x, y, z);
}

public float getAngle(Unit b) {
return getAngle(b.getPosition().toPoint2d());
}
// 0 = right, pi/2 = up, pi = left, 3pi/2 = down
public float getAngle(Point2d b) {
return (float)((Math.atan2(b.getY() - y, b.getX() - x) + Math.PI*2) % (Math.PI*2));
}

public Point2d addDistanceByAngle(double angleInRads, float distance) {
return Point2d.of(
distance * (float)Math.cos(angleInRads) + x,
distance * (float)Math.sin(angleInRads) + y
);
}

public Point2d midPoint(Point2d b) {
return this.add(b).div(2);
}

public Point2d rotate(Point2d pivotPoint, double angleInRads) {
double sin = Math.sin(angleInRads);
double cos = Math.cos(angleInRads);

//subtract pivot point
Point2d origin = this.sub(pivotPoint);

//rotate point
float xNew = (float)(origin.getX() * cos - origin.getY() * sin);
float yNew = (float)(origin.getX() * sin + origin.getY() * cos);

//add back the pivot point
float x = xNew + pivotPoint.getX();
float y = yNew + pivotPoint.getY();

return Point2d.of(x, y);
}

public Point2d roundToHalfPointAccuracy() {
return Point2d.of(Math.round(x * 2) / 2f, Math.round(y * 2) / 2f);
}

//useful for 1x1, 3x3, and 5x5 structure placements
public Point2d toNearestHalfPoint() {
return Point2d.of((int)x + 0.5f, (int)y + 0.5f);
}

//useful for 2x2 structure placements
public Point2d toNearestWholePoint() {
return Point2d.of(Math.round(x), Math.round(y));
}

public Point2d towards(Unit b, float distance) {
return towards(b.getPosition().toPoint2d(), distance);
}

public Point2d towards(Point2d b, float distance) {
if (this.equals(b)) {
return b;
}
Point2d vector = unitVector(b);
return this.add(vector.mul(distance));
}

public Point2d unitVector(Point2d b) {
return b.sub(this).normalize();
}

public Point2d normalize() {
float length = (float)Math.hypot(x, y);
return this.div(length);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -137,8 +207,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
int result = (x != 0.0f ? Float.floatToIntBits(x) : 0);
result = 31 * result + (y != 0.0f ? Float.floatToIntBits(y) : 0);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion ocraft-s2client-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion ocraft-s2client-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>ocraft-s2client</artifactId>
<groupId>com.github.ocraft</groupId>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.github.ocraft</groupId>
<artifactId>ocraft-s2client</artifactId>
<packaging>pom</packaging>
<version>0.4.21-SNAPSHOT</version>
<version>0.4.22-SNAPSHOT</version>

<modules>
<module>ocraft-s2client-test</module>
Expand Down

0 comments on commit 95627ab

Please sign in to comment.