Refactored IOpenSet implementations

This commit is contained in:
Brady 2018-08-03 13:31:33 -07:00
parent 9afcdedf81
commit 3d0d73a5ca
No known key found for this signature in database
GPG Key ID: 73A788379A197567
12 changed files with 82 additions and 40 deletions

View File

@ -1,9 +1,12 @@
package baritone.bot; package baritone.bot;
import baritone.bot.behavior.Behavior; import baritone.bot.behavior.Behavior;
import baritone.bot.chunk.ChunkPacker;
import baritone.bot.event.IGameEventListener; import baritone.bot.event.IGameEventListener;
import baritone.bot.event.events.ChatEvent; import baritone.bot.event.events.ChatEvent;
import baritone.bot.event.events.ChunkEvent; import baritone.bot.event.events.ChunkEvent;
import baritone.bot.event.events.type.EventState;
import baritone.bot.utils.Helper;
import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -13,10 +16,9 @@ import java.util.function.Consumer;
* @author Brady * @author Brady
* @since 7/31/2018 11:04 PM * @since 7/31/2018 11:04 PM
*/ */
public final class GameEventHandler implements IGameEventListener { public final class GameEventHandler implements IGameEventListener, Helper {
GameEventHandler() { GameEventHandler() {}
}
@Override @Override
public final void onTick() { public final void onTick() {
@ -50,6 +52,10 @@ public final class GameEventHandler implements IGameEventListener {
@Override @Override
public void onChunkEvent(ChunkEvent event) { public void onChunkEvent(ChunkEvent event) {
dispatch(behavior -> onChunkEvent(event)); dispatch(behavior -> onChunkEvent(event));
if (event.getState() == EventState.POST && event.getType() == ChunkEvent.Type.POPULATE) {
ChunkPacker.createPackedChunk(mc.world.getChunk(event.getX(), event.getZ())).write();
}
} }
private void dispatch(Consumer<Behavior> dispatchFunction) { private void dispatch(Consumer<Behavior> dispatchFunction) {

View File

@ -3,6 +3,8 @@ package baritone.bot.pathing.calc;
//import baritone.Baritone; //import baritone.Baritone;
import baritone.bot.pathing.openset.BinaryHeapOpenSet;
import baritone.bot.pathing.openset.IOpenSet;
import baritone.bot.pathing.goals.Goal; import baritone.bot.pathing.goals.Goal;
import baritone.bot.pathing.movement.ActionCosts; import baritone.bot.pathing.movement.ActionCosts;
import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.Movement;

View File

@ -1,14 +0,0 @@
package baritone.bot.pathing.calc;
/**
* An open set for A* or similar graph search algorithm
*
* @author leijurv
*/
public interface IOpenSet {
boolean isEmpty();
void insert(PathNode node);
PathNode removeLowest();
}

View File

@ -11,7 +11,8 @@ import java.util.Objects;
* *
* @author leijurv * @author leijurv
*/ */
class PathNode { public class PathNode {
/** /**
* The position of this node * The position of this node
*/ */

View File

@ -99,15 +99,10 @@ public interface MovementHelper extends ActionCosts {
return block.isPassable(Minecraft.getMinecraft().world, pos); return block.isPassable(Minecraft.getMinecraft().world, pos);
} }
static boolean avoidWalkingInto(BlockPos pos) { static boolean avoidWalkingInto(Block block) {
Block block = BlockStateInterface.get(pos).getBlock(); return isLava(block)
if (isLava(block)) { || block instanceof BlockCactus
return true; || block instanceof BlockFire;
}
if (block instanceof BlockCactus) {
return true;
}
return block instanceof BlockFire;
} }
/** /**

View File

@ -29,5 +29,4 @@ public class MovementAscend extends Movement {
latestState.setStatus(MovementState.MovementStatus.SUCCESS); latestState.setStatus(MovementState.MovementStatus.SUCCESS);
return latestState; return latestState;
} }
} }

View File

@ -1,10 +1,24 @@
package baritone.bot.pathing.calc; package baritone.bot.pathing.openset;
import baritone.bot.pathing.calc.PathNode;
import java.util.Arrays; import java.util.Arrays;
public class BinaryHeapOpenSet implements IOpenSet { public class BinaryHeapOpenSet implements IOpenSet {
/**
* The initial capacity of the heap (2^10)
*/
private static final int INITIAL_CAPACITY = 1024; private static final int INITIAL_CAPACITY = 1024;
/**
* The array backing the heap
*/
private PathNode[] array; private PathNode[] array;
/**
* The size of the heap
*/
private int size; private int size;
public BinaryHeapOpenSet() { public BinaryHeapOpenSet() {
@ -16,6 +30,7 @@ public class BinaryHeapOpenSet implements IOpenSet {
this.array = new PathNode[size]; this.array = new PathNode[size];
} }
@Override
public void insert(PathNode value) { public void insert(PathNode value) {
if (size >= array.length - 1) { if (size >= array.length - 1) {
array = Arrays.copyOf(array, array.length * 2); array = Arrays.copyOf(array, array.length * 2);
@ -31,16 +46,12 @@ public class BinaryHeapOpenSet implements IOpenSet {
} }
} }
/** @Override
* Returns true if the heap has no elements; false otherwise.
*/
public boolean isEmpty() { public boolean isEmpty() {
return size == 0; return size == 0;
} }
/** @Override
* Removes and returns the minimum element in the heap.
*/
public PathNode removeLowest() { public PathNode removeLowest() {
if (size == 0) { if (size == 0) {
throw new IllegalStateException(); throw new IllegalStateException();
@ -67,6 +78,12 @@ public class BinaryHeapOpenSet implements IOpenSet {
return result; return result;
} }
/**
* Swaps the elements at the specified indices.
*
* @param index1 The first index
* @param index2 The second index
*/
protected void swap(int index1, int index2) { protected void swap(int index1, int index2) {
PathNode tmp = array[index1]; PathNode tmp = array[index1];
array[index1] = array[index2]; array[index1] = array[index2];

View File

@ -1,4 +1,7 @@
package baritone.bot.pathing.calc; package baritone.bot.pathing.openset;
import baritone.bot.pathing.util.FibonacciHeap;
import baritone.bot.pathing.calc.PathNode;
/** /**
* Wrapper adapter between FibonacciHeap and OpenSet * Wrapper adapter between FibonacciHeap and OpenSet
@ -6,7 +9,7 @@ package baritone.bot.pathing.calc;
* @author leijurv * @author leijurv
*/ */
public class FibonacciHeapOpenSet extends FibonacciHeap implements IOpenSet { public class FibonacciHeapOpenSet extends FibonacciHeap implements IOpenSet {
//isEmpty is already defined in FibonacciHeap
@Override @Override
public void insert(PathNode node) { public void insert(PathNode node) {
super.insert(node, node.combinedCost); super.insert(node, node.combinedCost);

View File

@ -0,0 +1,30 @@
package baritone.bot.pathing.openset;
import baritone.bot.pathing.calc.PathNode;
/**
* An open set for A* or similar graph search algorithm
*
* @author leijurv
*/
public interface IOpenSet {
/**
* Inserts the specified node into the heap
*
* @param node The node
*/
void insert(PathNode node);
/**
* @return {@code true} if the heap has no elements; {@code false} otherwise.
*/
boolean isEmpty();
/**
* Removes and returns the minimum element in the heap.
*
* @return The minimum element in the heap
*/
PathNode removeLowest();
}

View File

@ -1,4 +1,6 @@
package baritone.bot.pathing.calc; package baritone.bot.pathing.openset;
import baritone.bot.pathing.calc.PathNode;
/** /**
* A linked list implementation of an open set. This is the original implementation from MineBot. * A linked list implementation of an open set. This is the original implementation from MineBot.

View File

@ -1,5 +1,5 @@
//Source: https://github.com/nlfiedler/graphmaker/blob/master/core/src/com/bluemarsh/graphmaker/core/util/FibonacciHeap.java //Source: https://github.com/nlfiedler/graphmaker/blob/master/core/src/com/bluemarsh/graphmaker/core/util/FibonacciHeap.java
package baritone.bot.pathing.calc; package baritone.bot.pathing.util;
/* /*
* The contents of this file are subject to the terms of the Common Development * The contents of this file are subject to the terms of the Common Development
* and Distribution License (the License). You may not use this file except in * and Distribution License (the License). You may not use this file except in

View File

@ -1,5 +1,6 @@
package baritone.bot.pathing.calc; package baritone.bot.pathing.openset;
import baritone.bot.pathing.calc.PathNode;
import baritone.bot.pathing.goals.GoalBlock; import baritone.bot.pathing.goals.GoalBlock;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import org.junit.Test; import org.junit.Test;