diff --git a/src/main/java/com/jelly/MightyMiner/baritone/automine/AutoMineBaritone.java b/src/main/java/com/jelly/MightyMiner/baritone/automine/AutoMineBaritone.java index 51e8f66f..d5e4f9c3 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/automine/AutoMineBaritone.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/automine/AutoMineBaritone.java @@ -1,8 +1,6 @@ package com.jelly.MightyMiner.baritone.automine; -import com.jelly.MightyMiner.MightyMiner; -import com.jelly.MightyMiner.baritone.automine.logging.Logger; -import com.jelly.MightyMiner.baritone.automine.pathing.exceptions.NoBlockException; +import com.jelly.MightyMiner.baritone.logging.Logger; import com.jelly.MightyMiner.baritone.automine.pathing.AStarPathFinder; import com.jelly.MightyMiner.baritone.structures.BlockNode; import com.jelly.MightyMiner.baritone.structures.BlockType; @@ -10,7 +8,6 @@ import com.jelly.MightyMiner.player.Rotation; import com.jelly.MightyMiner.render.BlockRenderer; import com.jelly.MightyMiner.utils.*; -import lombok.SneakyThrows; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.util.BlockPos; @@ -19,13 +16,8 @@ import net.minecraftforge.fml.common.gameevent.TickEvent; import java.awt.*; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; public class AutoMineBaritone{ @@ -150,15 +142,12 @@ public void onTickEvent(TickEvent.Phase phase){ if(phase != TickEvent.Phase.START) return; - Logger.log(inAction + " " + blocksToMine.isEmpty()); if(!inAction) return; if(blocksToMine.isEmpty()) return; - - if ( (blocksToMine.getLast().getBlockType() == BlockType.MINE && BlockUtils.isPassable(blocksToMine.getLast().getBlockPos())) || (blocksToMine.getLast().getBlockType() == BlockType.WALK && (BlockUtils.onTheSameXZ(blocksToMine.getLast().getBlockPos(), BlockUtils.getPlayerLoc()) || !BlockUtils.fitsPlayer(blocksToMine.getLast().getBlockPos().down())) ) diff --git a/src/main/java/com/jelly/MightyMiner/baritone/automine/execute/PathExecutor.java b/src/main/java/com/jelly/MightyMiner/baritone/automine/execute/PathExecutor.java deleted file mode 100644 index 4c337d13..00000000 --- a/src/main/java/com/jelly/MightyMiner/baritone/automine/execute/PathExecutor.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.jelly.MightyMiner.baritone.automine.execute; - -public class PathExecutor { - - -} diff --git a/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/AStarPathFinder.java b/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/AStarPathFinder.java index 996ade2e..fea5d52e 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/AStarPathFinder.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/AStarPathFinder.java @@ -1,8 +1,8 @@ package com.jelly.MightyMiner.baritone.automine.pathing; -import com.jelly.MightyMiner.baritone.automine.AutoMineBaritone; -import com.jelly.MightyMiner.baritone.automine.logging.Logger; +import com.jelly.MightyMiner.baritone.logging.Logger; import com.jelly.MightyMiner.baritone.automine.pathing.exceptions.NoBlockException; +import com.jelly.MightyMiner.baritone.automine.pathing.exceptions.NoPathException; import com.jelly.MightyMiner.baritone.structures.BlockNode; import com.jelly.MightyMiner.baritone.structures.BlockType; import com.jelly.MightyMiner.baritone.structures.GridEnvironment; @@ -15,13 +15,10 @@ import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraft.util.BlockPos; -import net.minecraft.util.ChatComponentText; import java.awt.*; -import java.util.ArrayList; -import java.util.LinkedList; +import java.util.*; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; public class AStarPathFinder { Minecraft mc = Minecraft.getMinecraft(); @@ -33,7 +30,7 @@ public class AStarPathFinder { List allowedMiningBlocks; List checkedNodes = new ArrayList<>(); - List openNodes = new ArrayList<>(); + PriorityQueue openNodes = new PriorityQueue<>(Comparator.comparingDouble(n -> n.fValue)); public AStarPathFinder(List forbiddenMiningBlocks, List allowedMiningBlocks){ @@ -41,48 +38,38 @@ public AStarPathFinder(List forbiddenMiningBlocks, List allowedMin this.allowedMiningBlocks = allowedMiningBlocks; } - public LinkedList getPath(Block... blockType) throws Exception{ + public LinkedList getPath(Block... blockType) throws NoBlockException, NoPathException { List foundBlocks = BlockUtils.findBlock(30, blockType); - /* for(BlockPos blockPos : foundBlocks) { - BlockRenderer.renderMap.put(blockPos, Color.LIGHT_GRAY); - }*/ + Logger.playerLog("Found gemstones : " + foundBlocks.size()); - if(foundBlocks.isEmpty()){ - throw new NoBlockException(); - } + long pastTime = System.currentTimeMillis(); - LinkedList lowestCostPath = new LinkedList<>(); - int prevCost = 9999; - Logger.playerLog("Found blocks : " + foundBlocks.size()); - for(int i = 0; i < (Math.min(foundBlocks.size(), 15)); i++){ - LinkedList currentPath; - try { - currentPath = calculatePath(BlockUtils.getPlayerLoc(), foundBlocks.get(i)); - } catch (Exception ignored){ - continue; - } - Logger.playerLog("Possible path index : " + i); - if(!currentPath.isEmpty()) { - int currentCost = calculatePathCost(currentPath); - BlockRenderer.renderMap.put(currentPath.getFirst().getBlockPos(), Color.GREEN); - - if (currentCost < prevCost) { - Logger.playerLog("Better path detected : index : " + i); - lowestCostPath = currentPath; - prevCost = currentCost; - } - if (currentCost <= 3) { - return lowestCostPath; - } + if(foundBlocks.isEmpty()) + throw new NoBlockException(); + LinkedList> possiblePaths = new LinkedList<>(); + for(int i = 0; i < (Math.min(foundBlocks.size(), 20)); i++){ + LinkedList path = calculatePath(BlockUtils.getPlayerLoc(), foundBlocks.get(i)); + if(!path.isEmpty()){ + possiblePaths.add(path); + BlockRenderer.renderMap.put(possiblePaths.getLast().getFirst().getBlockPos(), Color.GREEN); + if(possiblePaths.getLast().size() == 1) + return possiblePaths.getLast(); } } - return lowestCostPath; + + if(possiblePaths.isEmpty()) + throw new NoPathException(); + + Logger.playerLog("Total time | Time per path : " + (System.currentTimeMillis() - pastTime) + " ms | " + (System.currentTimeMillis() - pastTime) * 1.0d / possiblePaths.size() + " ms"); + + possiblePaths.sort(Comparator.comparingInt(this::calculatePathCost)); + return possiblePaths.getFirst(); } - private LinkedList calculatePath(BlockPos startingPos, BlockPos endingBlock) throws Exception { + private LinkedList calculatePath(BlockPos startingPos, BlockPos endingBlock) { gridEnvironment.clear(); checkedNodes.clear(); @@ -92,115 +79,89 @@ private LinkedList calculatePath(BlockPos startingPos, BlockPos endin return new LinkedList(){{add(new BlockNode(endingBlock, getBlockType(endingBlock)));}}; } - boolean completedPathfind = false; Node startNode; Node currentNode; - Node goalNode = new Node(endingBlock); int currentGridX = 0; int currentGridY = (int) mc.thePlayer.posY; int currentGridZ = 0; instantiateAnyNode(currentGridX, currentGridY, currentGridZ, new Node(startingPos)); - currentNode = gridEnvironment.get(currentGridX, currentGridY, currentGridZ); - startNode = currentNode; + startNode = gridEnvironment.get(currentGridX, currentGridY, currentGridZ); step = 0; + openNodes.add(startNode); + while (!openNodes.isEmpty()) { + currentNode = openNodes.poll(); + if(currentNode.lastNode != null) { + currentGridX = currentNode.blockPos.getX() - startNode.blockPos.getX(); + currentGridY = currentNode.blockPos.getY(); + currentGridZ = currentNode.blockPos.getZ() - startNode.blockPos.getZ(); + } - while (!completedPathfind) { - step++; - currentNode.checked = true; checkedNodes.add(currentNode); - openNodes.remove(currentNode); - - instantiateNode(currentGridX - 1, currentGridY, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX - 1, currentGridY, currentGridZ), currentNode, endingBlock); + step++; - instantiateNode(currentGridX + 1, currentGridY, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX + 1, currentGridY, currentGridZ), currentNode, endingBlock); + if(step > 300) break; + instantiateNode(currentGridX - 1, currentGridY, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX - 1, currentGridY, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY, currentGridZ - 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY, currentGridZ - 1), currentNode, endingBlock); + instantiateNode(currentGridX + 1, currentGridY, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX + 1, currentGridY, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY, currentGridZ + 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY, currentGridZ + 1), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY, currentGridZ - 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY, currentGridZ - 1), currentNode, endingBlock); - instantiateNode(currentGridX - 1, currentGridY - 1, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX - 1, currentGridY - 1, currentGridZ), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY, currentGridZ + 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY, currentGridZ + 1), currentNode, endingBlock); - instantiateNode(currentGridX + 1, currentGridY - 1, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX + 1, currentGridY - 1, currentGridZ), currentNode, endingBlock); + instantiateNode(currentGridX - 1, currentGridY - 1, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX - 1, currentGridY - 1, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY - 1, currentGridZ - 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ - 1), currentNode, endingBlock); + instantiateNode(currentGridX + 1, currentGridY - 1, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX + 1, currentGridY - 1, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY - 1, currentGridZ + 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ + 1), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY - 1, currentGridZ - 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ - 1), currentNode, endingBlock); - instantiateNode(currentGridX - 1, currentGridY + 1, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX - 1, currentGridY + 1, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX + 1, currentGridY + 1, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX + 1, currentGridY + 1, currentGridZ), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY - 1, currentGridZ + 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ + 1), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY + 1, currentGridZ - 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY + 1, currentGridZ - 1), currentNode, endingBlock); + instantiateNode(currentGridX - 1, currentGridY + 1, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX - 1, currentGridY + 1, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY + 1, currentGridZ + 1, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY + 1, currentGridZ + 1), currentNode, endingBlock); + instantiateNode(currentGridX + 1, currentGridY + 1, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX + 1, currentGridY + 1, currentGridZ), currentNode, endingBlock); - instantiateNode(currentGridX, currentGridY - 1, currentGridZ, startNode); - openNodeAndCalculateCost(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY + 1, currentGridZ - 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY + 1, currentGridZ - 1), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY + 1, currentGridZ + 1, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY + 1, currentGridZ + 1), currentNode, endingBlock); + instantiateNode(currentGridX, currentGridY - 1, currentGridZ, startNode); + checkNode(gridEnvironment.get(currentGridX, currentGridY - 1, currentGridZ), currentNode, endingBlock); - int bestIndex = 0; - double minFcost = 9999; - for (int i = 0; i < openNodes.size(); i++) { - if(openNodes.get(i).hValue == 0){ - bestIndex = i; - break; - } - if (openNodes.get(i).fValue < minFcost) { - bestIndex = i; - minFcost = openNodes.get(i).fValue; - } - } - int tempX, tempY, tempZ; - tempX = currentGridX; - tempY = currentGridY; - tempZ = currentGridZ; - currentGridX += openNodes.get(bestIndex).blockPos.getX() - gridEnvironment.get(tempX, tempY, tempZ).blockPos.getX(); - currentGridY += openNodes.get(bestIndex).blockPos.getY() - gridEnvironment.get(tempX, tempY, tempZ).blockPos.getY(); - currentGridZ += openNodes.get(bestIndex).blockPos.getZ() - gridEnvironment.get(tempX, tempY, tempZ).blockPos.getZ(); - - currentNode = openNodes.get(bestIndex); - if (goalNode.blockPos.equals(currentNode.blockPos)) { - completedPathfind = true; + if(currentNode.blockPos.equals(endingBlock)) { + return trackBackPath(currentNode, startNode); } - if(step > 30000) - { - Logger.playerLog("Steps exceeded"); - throw new Exception(); - } - } - return trackBackPath(currentNode, startNode); - + } + return new LinkedList<>(); } - private void openNodeAndCalculateCost(Node searchNode, Node currentNode, BlockPos endingBlockPos){ + private void checkNode(Node searchNode, Node currentNode, BlockPos endingBlockPos){ - if ( (!searchNode.checked - && !searchNode.opened - && BlockUtils.canWalkOn(searchNode.blockPos.down()))){ + + if (!checkedNodes.contains(searchNode) && BlockUtils.canWalkOn(searchNode.blockPos.down())){ if(!searchNode.blockPos.equals(endingBlockPos)) { if (forbiddenMiningBlocks != null && forbiddenMiningBlocks.contains(BlockUtils.getBlock(searchNode.blockPos)) && !BlockUtils.getBlock(searchNode.blockPos).equals(Blocks.air)) @@ -209,12 +170,22 @@ private void openNodeAndCalculateCost(Node searchNode, Node currentNode, BlockPo if (allowedMiningBlocks != null && !allowedMiningBlocks.contains(BlockUtils.getBlock(searchNode.blockPos)) && !BlockUtils.getBlock(searchNode.blockPos).equals(Blocks.air)) return; } - BlockRenderer.renderMap.put(searchNode.blockPos, Color.cyan); + if(!openNodes.contains(searchNode)){ + calculateCost(searchNode, endingBlockPos); + searchNode.lastNode = currentNode; + openNodes.add(searchNode); + + } else { + if(currentNode.gValue + (Math.abs(searchNode.blockPos.getY() - currentNode.blockPos.getY()) > 0 ? 2 : 1) < searchNode.gValue){ + Logger.log("Found better path"); + searchNode.lastNode = currentNode; + calculateCost(searchNode, endingBlockPos); + openNodes.remove(searchNode); + openNodes.add(searchNode); + } + } + - searchNode.opened = true; - searchNode.lastNode = currentNode; - openNodes.add(searchNode); - calculateCost(searchNode, endingBlockPos); } } @@ -306,23 +277,16 @@ private LinkedList trackBackPath(Node goalNode, Node startNode){ private void calculateCost(Node node, BlockPos endingBlock){ node.hValue = getHeuristic(node.blockPos, endingBlock); - if(node.lastNode != null) { - if(node.lastNode.blockPos.getY() != node.blockPos.getY()) { - node.gValue = node.lastNode.gValue + 2; - } else { - node.gValue = node.lastNode.gValue + 1; - } - } + if(node.lastNode != null) + node.gValue = node.lastNode.gValue + ((node.lastNode.blockPos.getY() != node.blockPos.getY()) ? 2 : 1) * ((BlockUtils.isPassable(node.blockPos)) ? 0.5f : 2); else node.gValue = 1f; - - // node.gValue *= BlockUtils.isPassable(node.blockPos) ? 0.5f : 2f; node.fValue = node.gValue + node.hValue; } private int calculatePathCost(List nodes){ int cost = 0; for(BlockNode node : nodes){ - cost += (node.getBlockType() == BlockType.WALK) ? 2 : 1; + cost += (node.getBlockType() == BlockType.WALK) ? 2 : 1; //avoid open areas } return cost; } diff --git a/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/exceptions/NoPathException.java b/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/exceptions/NoPathException.java index 5045d7bd..2a7b2756 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/exceptions/NoPathException.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/automine/pathing/exceptions/NoPathException.java @@ -1,4 +1,8 @@ package com.jelly.MightyMiner.baritone.automine.pathing.exceptions; -public class NoPathException { +public class NoPathException extends Exception{ + public NoPathException(){} + public NoPathException(String msg){ + super(msg); + } } diff --git a/src/main/java/com/jelly/MightyMiner/baritone/autowalk/WalkBaritone.java b/src/main/java/com/jelly/MightyMiner/baritone/autowalk/WalkBaritone.java index 4246331d..87c2be27 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/autowalk/WalkBaritone.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/autowalk/WalkBaritone.java @@ -19,7 +19,7 @@ import java.util.List; public class WalkBaritone{ -//Custom grid example +/*//Custom grid example //Xxxxxxxxxxxxxx | //xxxxxxxxxxxxxx | @@ -290,6 +290,6 @@ private List trackBackPath(Node goalNode, Node startNode){ mc.thePlayer.addChatMessage(new ChatComponentText("Block count : " + blocksToWalk.size())); return blocksToWalk; } - +*/ } diff --git a/src/main/java/com/jelly/MightyMiner/baritone/automine/logging/Logger.java b/src/main/java/com/jelly/MightyMiner/baritone/logging/Logger.java similarity index 83% rename from src/main/java/com/jelly/MightyMiner/baritone/automine/logging/Logger.java rename to src/main/java/com/jelly/MightyMiner/baritone/logging/Logger.java index 992b5896..21b17e28 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/automine/logging/Logger.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/logging/Logger.java @@ -1,9 +1,8 @@ -package com.jelly.MightyMiner.baritone.automine.logging; +package com.jelly.MightyMiner.baritone.logging; import com.jelly.MightyMiner.MightyMiner; import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; -import net.minecraftforge.fml.common.Mod; import org.apache.logging.log4j.LogManager; public class Logger { diff --git a/src/main/java/com/jelly/MightyMiner/baritone/structures/Node.java b/src/main/java/com/jelly/MightyMiner/baritone/structures/Node.java index 15f6a184..4731b51c 100644 --- a/src/main/java/com/jelly/MightyMiner/baritone/structures/Node.java +++ b/src/main/java/com/jelly/MightyMiner/baritone/structures/Node.java @@ -3,27 +3,12 @@ import net.minecraft.util.BlockPos; public class Node { - - public Node(){ - } public Node(BlockPos blockPos){ this.blockPos = blockPos; } - public Node(BlockPos blockPos, boolean checked, boolean opened){ - this.blockPos = blockPos; - this.checked = checked; - this.opened = opened; - } - public double hValue; public double gValue; public double fValue; public Node lastNode; public BlockPos blockPos; - public boolean checked; - public boolean opened; - - - - }