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;
import baritone.bot.behavior.Behavior;
import baritone.bot.chunk.ChunkPacker;
import baritone.bot.event.IGameEventListener;
import baritone.bot.event.events.ChatEvent;
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 org.lwjgl.input.Keyboard;
@ -13,10 +16,9 @@ import java.util.function.Consumer;
* @author Brady
* @since 7/31/2018 11:04 PM
*/
public final class GameEventHandler implements IGameEventListener {
public final class GameEventHandler implements IGameEventListener, Helper {
GameEventHandler() {
}
GameEventHandler() {}
@Override
public final void onTick() {
@ -50,6 +52,10 @@ public final class GameEventHandler implements IGameEventListener {
@Override
public void onChunkEvent(ChunkEvent 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) {

View File

@ -3,6 +3,8 @@ package baritone.bot.pathing.calc;
//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.movement.ActionCosts;
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
*/
class PathNode {
public class PathNode {
/**
* The position of this node
*/

View File

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

View File

@ -29,5 +29,4 @@ public class MovementAscend extends Movement {
latestState.setStatus(MovementState.MovementStatus.SUCCESS);
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;
public class BinaryHeapOpenSet implements IOpenSet {
/**
* The initial capacity of the heap (2^10)
*/
private static final int INITIAL_CAPACITY = 1024;
/**
* The array backing the heap
*/
private PathNode[] array;
/**
* The size of the heap
*/
private int size;
public BinaryHeapOpenSet() {
@ -16,6 +30,7 @@ public class BinaryHeapOpenSet implements IOpenSet {
this.array = new PathNode[size];
}
@Override
public void insert(PathNode value) {
if (size >= array.length - 1) {
array = Arrays.copyOf(array, array.length * 2);
@ -31,16 +46,12 @@ public class BinaryHeapOpenSet implements IOpenSet {
}
}
/**
* Returns true if the heap has no elements; false otherwise.
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/**
* Removes and returns the minimum element in the heap.
*/
@Override
public PathNode removeLowest() {
if (size == 0) {
throw new IllegalStateException();
@ -67,6 +78,12 @@ public class BinaryHeapOpenSet implements IOpenSet {
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) {
PathNode tmp = array[index1];
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
@ -6,7 +9,7 @@ package baritone.bot.pathing.calc;
* @author leijurv
*/
public class FibonacciHeapOpenSet extends FibonacciHeap implements IOpenSet {
//isEmpty is already defined in FibonacciHeap
@Override
public void insert(PathNode node) {
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.

View File

@ -1,5 +1,5 @@
//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
* 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 net.minecraft.util.math.BlockPos;
import org.junit.Test;