Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked groundwar pathfinding #1102

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hu/openig/core/AStarSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public int compare(T o1, T o2) {

// if we get here, there was no direct path available
// find a target location which minimizes initial-L-destination
if (closedSet.isEmpty()) {
if (closedSet.size() <= 1 || initial.equals(nearest)) {
return Pair.of(false, Collections.<T>emptyList());
}
return Pair.of(true, reconstructPath(cameFrom, nearest));
Expand Down
27 changes: 12 additions & 15 deletions src/hu/openig/core/Pathfinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
public class Pathfinding extends AStarSearch<Location> {
/** Test for passability. */
public Func1<Location, Boolean> isPassable;
/** Test for permanent obstacles */
public Func1<Location, Boolean> isBlocked;
/** Setup the A*. */
public Pathfinding() {
neighbors = new Func1<Location, List<Location>>() {
Expand All @@ -35,8 +37,8 @@ public List<Location> invoke(Location value) {
* @param destination the destination
* @return the path, empty if the target is completely unreachable
*/
public List<Location> searchApproximate(final Location initial, final Location destination) {
return search(initial, destination).second;
public Pair<Boolean, List<Location>> searchApproximate(final Location initial, final Location destination) {
return search(initial, destination);
}
/**
* Computes the sum of the distance squares between (target and source1) and (target and source2).
Expand Down Expand Up @@ -97,42 +99,37 @@ private List<Location> neighbors(Location current) {
Location bottom = current.delta(0, 1);
Location top = current.delta(0, -1);

boolean pleft = isPassable.invoke(left);
boolean pright = isPassable.invoke(right);
boolean pbottom = isPassable.invoke(bottom);
boolean ptop = isPassable.invoke(top);

if (pleft) {
if (isPassable.invoke(left)) {
result.add(left);
}
if (pright) {
if (isPassable.invoke(right)) {
result.add(right);
}
if (pbottom) {
if (isPassable.invoke(bottom)) {
result.add(bottom);
}
if (ptop) {
if (isPassable.invoke(top)) {
result.add(top);
}
if (pleft && ptop) {
if (!isBlocked.invoke(left) && !isBlocked.invoke(top)) {
Location c = current.delta(-1, -1);
if (isPassable.invoke(c)) {
result.add(c);
}
}
if (pleft && pbottom) {
if (!isBlocked.invoke(left) && !isBlocked.invoke(bottom)) {
Location c = current.delta(-1, 1);
if (isPassable.invoke(c)) {
result.add(c);
}
}
if (pright && ptop) {
if (!isBlocked.invoke(right) && !isBlocked.invoke(top)) {
Location c = current.delta(1, -1);
if (isPassable.invoke(c)) {
result.add(c);
}
}
if (pright && pbottom) {
if (!isBlocked.invoke(right) && !isBlocked.invoke(bottom)) {
Location c = current.delta(1, 1);
if (isPassable.invoke(c)) {
result.add(c);
Expand Down
2 changes: 1 addition & 1 deletion src/hu/openig/model/GroundwarManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public PathPlanning(

@Override
public PathPlanning call() {
path.addAll(getPathfinding(ignore).searchApproximate(current, goal));
//path.addAll(getPathfinding(ignore).searchApproximate(current, goal));
return this;
}
/**
Expand Down
52 changes: 8 additions & 44 deletions src/hu/openig/model/GroundwarUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,7 @@ public GroundwarUnit(BufferedImage[][] matrix) {
}
@Override
public Location location() {
return Location.of((int)x, (int)y);
}
/**
* Determines the location which should be used for path calculation.
* @return the location
*/
public Location pathFindingLocation() {
if ((Math.abs(x - (int)x) < 1E-9 && Math.abs(y - (int)y) < 1E-9) || path.isEmpty()) {
return Location.of((int)x, (int)y);
}
return path.get(0);
return Location.of((int)Math.round(x), (int)Math.round(y));
}
@Override
public Double exactLocation() {
Expand Down Expand Up @@ -148,6 +138,10 @@ public double distance(Location loc) {
public boolean isMoving() {
return nextMove != null || !path.isEmpty();
}
/** @return true if the unit is in between cells. */
public boolean inMotion() {
return (x % 1 != 0) || (y % 1 != 0);
}
/**
* @return the target cell of movement or null if not moving
*/
Expand Down Expand Up @@ -248,41 +242,11 @@ public Point center() {
return new Point(px + model.width / 2, py + model.height / 2);
}
/**
* Merges the current path with the new skipping
* overlapping parts to avoid unnecessary rotations.

* Merges the new path.
* @param newPath the new path to follow
*/
public void mergePath(List<Location> newPath) {
// path.addAll(newPath);
if (!newPath.isEmpty()) {

if (newPath.size() >= 2) {
Location p0 = newPath.get(0);

double vx = p0.x - x;
double vy = p0.y - y;
double v = Math.hypot(vx, vy);

if (v >= 1E-9) {
Location p1 = newPath.get(1);

double ax = p1.x - p0.x;
double ay = p1.y - p0.y;
double a = Math.hypot(ax, ay);

double angle = Math.acos((vx * ax + vy * ay) / v / a);
if (angle >= Math.PI - 1E-6) {
path.clear();
path.addAll(newPath.subList(1, newPath.size()));
nextMove = p1;
nextRotate = p1;
return;
}
}

}
path.addAll(newPath);
}
path.clear();
path.addAll(newPath);
}
}
4 changes: 2 additions & 2 deletions src/hu/openig/model/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class Planet implements Named, Owned, HasInventory, HasPosition {
/** The persistent deployed ground units and turrets. */
public final PlanetGround ground;
/** The ground war manager for this planet. */
public final GroundwarManager war;
//public final GroundwarManager war;
/**
* Compares the total value of the planet.
*/
Expand Down Expand Up @@ -135,7 +135,7 @@ public Planet(String id, World world) {
this.id = id;
this.world = world;
this.ground = new PlanetGround();
this.war = new GroundwarManager(this);
//this.war = new GroundwarManager(this);
}
/**
* Return the morale label for the given level.
Expand Down
Loading