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

Gotta go fast, fixes #179, fixes #168 #180

Merged
merged 6 commits into from
Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
s a n i k
  • Loading branch information
leijurv committed Sep 23, 2018
commit 0d0eefec9c82e1d0e4f00b71cccfe45342c15f04
65 changes: 9 additions & 56 deletions src/main/java/baritone/pathing/calc/AStarPathFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@
import baritone.pathing.goals.Goal;
import baritone.pathing.movement.ActionCosts;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement;
import baritone.pathing.movement.MovementHelper;
import baritone.pathing.movement.movements.*;
import baritone.pathing.path.IPath;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
import baritone.utils.pathing.BetterBlockPos;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ChunkProviderClient;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;

import java.util.Collection;
Expand All @@ -45,7 +41,7 @@
*
* @author leijurv
*/
public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper {

private final Optional<HashSet<BetterBlockPos>> favoredPositions;

Expand Down Expand Up @@ -104,12 +100,14 @@ protected Optional<IPath> calculate0(long timeout) {
logDebug("Took " + (System.nanoTime() / 1000000L - startTime) + "ms, " + numMovementsConsidered + " movements considered");
return Optional.of(new Path(startNode, currentNode, numNodes, goal));
}
Movement[] possibleMovements = getConnectedPositions(currentNodePos, calcContext);//movement that we could take that start at currentNodePos
for (Movement movementToGetToNeighbor : possibleMovements) {
if (movementToGetToNeighbor == null) {
for (Moves moves : Moves.values()) {
MoveResult res = moves.apply(calcContext, currentNodePos.x, currentNodePos.y, currentNodePos.z);
numMovementsConsidered++;
double actionCost = res.cost;
if (actionCost >= ActionCosts.COST_INF) {
continue;
}
BetterBlockPos dest = movementToGetToNeighbor.getDest();
BetterBlockPos dest = new BetterBlockPos(res.destX, res.destY, res.destZ);
int chunkX = currentNodePos.x >> 4;
int chunkZ = currentNodePos.z >> 4;
if (dest.x >> 4 != chunkX || dest.z >> 4 != chunkZ) {
Expand All @@ -122,14 +120,11 @@ protected Optional<IPath> calculate0(long timeout) {
}
}
}
// TODO cache cost
double actionCost = movementToGetToNeighbor.getCost(calcContext);
numMovementsConsidered++;
if (actionCost >= ActionCosts.COST_INF) {
continue;
}
if (actionCost <= 0) {
throw new IllegalStateException(movementToGetToNeighbor.getClass() + " " + movementToGetToNeighbor + " calculated implausible cost " + actionCost);
throw new IllegalStateException(moves + " calculated implausible cost " + actionCost);
}
if (favoring && favored.contains(dest)) {
// see issue #18
Expand All @@ -139,7 +134,7 @@ protected Optional<IPath> calculate0(long timeout) {
double tentativeCost = currentNode.cost + actionCost;
if (tentativeCost < neighbor.cost) {
if (tentativeCost < 0) {
throw new IllegalStateException(movementToGetToNeighbor.getClass() + " " + movementToGetToNeighbor + " overflowed into negative " + actionCost + " " + neighbor.cost + " " + tentativeCost);
throw new IllegalStateException(moves + " overflowed into negative " + actionCost + " " + neighbor.cost + " " + tentativeCost);
}
double improvementBy = neighbor.cost - tentativeCost;
// there are floating point errors caused by random combinations of traverse and diagonal over a flat area
Expand All @@ -150,7 +145,6 @@ protected Optional<IPath> calculate0(long timeout) {
continue;
}
neighbor.previous = currentNode;
neighbor.previousMovement = movementToGetToNeighbor;
neighbor.cost = tentativeCost;
neighbor.combinedCost = tentativeCost + neighbor.estimatedCostToGoal;
if (neighbor.isOpen) {
Expand Down Expand Up @@ -203,45 +197,4 @@ protected Optional<IPath> calculate0(long timeout) {
logDebug("No path found =(");
return Optional.empty();
}


public static Movement[] getConnectedPositions(BetterBlockPos pos, CalculationContext calcContext) {
int x = pos.x;
int y = pos.y;
int z = pos.z;
BetterBlockPos east = new BetterBlockPos(x + 1, y, z);
BetterBlockPos west = new BetterBlockPos(x - 1, y, z);
BetterBlockPos south = new BetterBlockPos(x, y, z + 1);
BetterBlockPos north = new BetterBlockPos(x, y, z - 1);
return new Movement[]{
new MovementDownward(pos, new BetterBlockPos(x, y - 1, z)),

new MovementPillar(pos, new BetterBlockPos(x, y + 1, z)),

new MovementTraverse(pos, east),
new MovementTraverse(pos, west),
new MovementTraverse(pos, north),
new MovementTraverse(pos, south),

new MovementAscend(pos, new BetterBlockPos(x + 1, y + 1, z)),
new MovementAscend(pos, new BetterBlockPos(x - 1, y + 1, z)),
new MovementAscend(pos, new BetterBlockPos(x, y + 1, z + 1)),
new MovementAscend(pos, new BetterBlockPos(x, y + 1, z - 1)),

MovementHelper.generateMovementFallOrDescend(pos, east, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, west, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, north, calcContext),
MovementHelper.generateMovementFallOrDescend(pos, south, calcContext),

new MovementDiagonal(pos, EnumFacing.NORTH, EnumFacing.EAST),
new MovementDiagonal(pos, EnumFacing.NORTH, EnumFacing.WEST),
new MovementDiagonal(pos, EnumFacing.SOUTH, EnumFacing.EAST),
new MovementDiagonal(pos, EnumFacing.SOUTH, EnumFacing.WEST),

MovementParkour.generate(pos, EnumFacing.EAST, calcContext),
MovementParkour.generate(pos, EnumFacing.WEST, calcContext),
MovementParkour.generate(pos, EnumFacing.NORTH, calcContext),
MovementParkour.generate(pos, EnumFacing.SOUTH, calcContext),
};
}
}
32 changes: 32 additions & 0 deletions src/main/java/baritone/pathing/calc/MoveResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/

package baritone.pathing.calc;

public final class MoveResult {
public final int destX;
public final int destY;
public final int destZ;
public final double cost;

public MoveResult(int x, int y, int z, double cost) {
this.destX = x;
this.destY = y;
this.destZ = z;
this.cost = cost;
}
}
Loading