Merge branch 'master' of github.com:cabaletta/baritone

This commit is contained in:
Nachiketa Gargi 2018-08-14 23:49:11 -07:00
commit d2ad15b79f
23 changed files with 160 additions and 89 deletions

View File

@ -22,6 +22,7 @@ import baritone.bot.behavior.impl.LookBehavior;
import baritone.bot.behavior.impl.MemoryBehavior;
import baritone.bot.behavior.impl.PathingBehavior;
import baritone.bot.event.GameEventHandler;
import baritone.bot.utils.InputOverrideHandler;
import java.util.ArrayList;
import java.util.List;

View File

@ -17,25 +17,103 @@
package baritone.bot;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import java.lang.reflect.Field;
import java.util.*;
/**
* Baritone's settings
*
* @author leijurv
*/
public class Settings {
public boolean allowBreak = true;
public boolean allowPlaceThrowaway = true;
public double costHeuristic = 4;
public boolean chuckCaching = false;
public boolean allowWaterBucketFall = true;
public int planningTickLookAhead = 150;
public boolean renderPath = true;
public boolean chatDebug = true;
public boolean chatControl = true;
public boolean fadePath = true;
public boolean slowPath = false;
public Setting<Boolean> allowBreak = new Setting<>(true);
public Setting<Boolean> allowPlaceThrowaway = new Setting<>(true);
public Setting<Double> costHeuristic = new <Double>Setting<Double>(4D);
public Setting<Boolean> chuckCaching = new Setting<>(false);
public Setting<Boolean> allowWaterBucketFall = new Setting<>(true);
public Setting<Integer> planningTickLookAhead = new Setting<>(150);
public Setting<Boolean> renderPath = new Setting<>(true);
public Setting<Boolean> chatDebug = new Setting<>(true);
public Setting<Boolean> chatControl = new Setting<>(true); // probably false in impact
public Setting<Boolean> fadePath = new Setting<>(false); // give this a better name in the UI, like "better path fps" idk
public Setting<Boolean> slowPath = new Setting<>(false);
public Setting<List<Item>> acceptableThrowAwayItems = new Setting<>(Arrays.asList(
Item.getItemFromBlock(Blocks.DIRT),
Item.getItemFromBlock(Blocks.COBBLESTONE),
Item.getItemFromBlock(Blocks.NETHERRACK)
));
Settings() {
public final Map<String, Setting<?>> byName;
public final List<Setting<?>> allSettings;
public class Setting<T> {
public T value;
private String name;
private final Class<T> klass;
private Setting(T value) {
if (value == null) {
throw new IllegalArgumentException("Cannot determine value type class from null");
}
this.value = value;
this.klass = (Class<T>) value.getClass();
}
public final T get() {
return value;
}
public final String getName() {
return name;
}
public Class<T> getValueClass() {
return klass;
}
public String toString() {
return name + ": " + value;
}
}
// here be dragons
{
Field[] temp = getClass().getFields();
HashMap<String, Setting<?>> tmpByName = new HashMap<>();
List<Setting<?>> tmpAll = new ArrayList<>();
try {
for (Field field : temp) {
if (field.getType().equals(Setting.class)) {
Setting<?> setting = (Setting<? extends Object>) field.get(this);
String name = field.getName();
setting.name = name;
if (tmpByName.containsKey(name)) {
throw new IllegalStateException("Duplicate setting name");
}
tmpByName.put(name, setting);
tmpAll.add(setting);
}
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
byName = Collections.unmodifiableMap(tmpByName);
allSettings = Collections.unmodifiableList(tmpAll);
}
public <T> List<Setting<T>> getByValueType(Class<T> klass) {
ArrayList<Setting<T>> result = new ArrayList<>();
for (Setting<?> setting : allSettings) {
if (setting.klass.equals(klass)) {
result.add((Setting<T>) setting);
}
}
return result;
}
Settings() { }
}

View File

@ -122,7 +122,7 @@ public class PathingBehavior extends Behavior {
// and this path dosen't get us all the way there
return;
}
if (current.getPath().ticksRemainingFrom(current.getPosition()) < Baritone.settings().planningTickLookAhead) {
if (current.getPath().ticksRemainingFrom(current.getPosition()) < Baritone.settings().planningTickLookAhead.get()) {
// and this path has 5 seconds or less left
displayChatMessageRaw("Path almost over. Planning ahead...");
findPathInNewThread(current.getPath().getDest(), false);
@ -135,10 +135,12 @@ public class PathingBehavior extends Behavior {
this.goal = goal;
}
public PathExecutor getExecutor() {
public PathExecutor getCurrent() {
return current;
}
public PathExecutor getNext() {return next;}
public Optional<IPath> getPath() {
return Optional.ofNullable(current).map(PathExecutor::getPath);
}
@ -231,7 +233,7 @@ public class PathingBehavior extends Behavior {
@Override
public void onRenderPass(RenderEvent event) {
if (!Baritone.settings().renderPath) {
if (!Baritone.settings().renderPath.get()) {
return;
}
// System.out.println("Render passing");
@ -246,10 +248,10 @@ public class PathingBehavior extends Behavior {
// Render the current path, if there is one
if (current != null && current.getPath() != null) {
int renderBegin = Math.max(current.getPosition() - 3, 0);
PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED, Baritone.settings().fadePath, 10, 20);
PathRenderer.drawPath(current.getPath(), renderBegin, player(), partialTicks, Color.RED, Baritone.settings().fadePath.get(), 10, 20);
}
if (next != null && next.getPath() != null) {
PathRenderer.drawPath(next.getPath(), 0, player(), partialTicks, Color.GREEN, Baritone.settings().fadePath, 10, 20);
PathRenderer.drawPath(next.getPath(), 0, player(), partialTicks, Color.GREEN, Baritone.settings().fadePath.get(), 10, 20);
}
long split = System.nanoTime();
@ -262,10 +264,10 @@ public class PathingBehavior extends Behavior {
// If there is a path calculation currently running, render the path calculation process
AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> {
currentlyRunning.bestPathSoFar().ifPresent(p -> {
PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE, Baritone.settings().fadePath, 10, 20);
PathRenderer.drawPath(p, 0, player(), partialTicks, Color.BLUE, Baritone.settings().fadePath.get(), 10, 20);
currentlyRunning.pathToMostRecentNodeConsidered().ifPresent(mr -> {
PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN, Baritone.settings().fadePath, 10, 20);
PathRenderer.drawPath(mr, 0, player(), partialTicks, Color.CYAN, Baritone.settings().fadePath.get(), 10, 20);
PathRenderer.drawManySelectionBoxes(player(), Collections.singletonList(mr.getDest()), partialTicks, Color.CYAN);
});
});

View File

@ -35,15 +35,15 @@
package baritone.bot.event;
import baritone.bot.Baritone;
import baritone.bot.InputOverrideHandler;
import baritone.bot.behavior.Behavior;
import baritone.bot.chunk.CachedWorld;
import baritone.bot.chunk.CachedWorldProvider;
import baritone.bot.chunk.ChunkPacker;
import baritone.bot.event.listener.IGameEventListener;
import baritone.bot.event.events.*;
import baritone.bot.event.events.type.EventState;
import baritone.bot.event.listener.IGameEventListener;
import baritone.bot.utils.Helper;
import baritone.bot.utils.InputOverrideHandler;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
@ -108,7 +108,7 @@ public final class GameEventHandler implements IGameEventListener, Helper {
&& type == ChunkEvent.Type.UNLOAD
&& mc.world.getChunkProvider().isChunkGeneratedAt(event.getX(), event.getZ());
if (Baritone.settings().chuckCaching) {
if (Baritone.settings().chuckCaching.get()) {
if (isPostPopulate || isPreUnload) {
CachedWorldProvider.INSTANCE.ifWorldLoaded(world ->
world.updateCachedChunk(event.getX(), event.getZ(),
@ -132,7 +132,7 @@ public final class GameEventHandler implements IGameEventListener, Helper {
@Override
public void onWorldEvent(WorldEvent event) {
if (Baritone.settings().chuckCaching) {
if (Baritone.settings().chuckCaching.get()) {
CachedWorldProvider cache = CachedWorldProvider.INSTANCE;
switch (event.getState()) {

View File

@ -81,14 +81,15 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
}
currentlyRunning = this;
long startTime = System.currentTimeMillis();
long timeoutTime = startTime + (Baritone.settings().slowPath ? 40000 : 4000);
boolean slowPath = Baritone.settings().slowPath.get();
long timeoutTime = startTime + (slowPath ? 40000 : 4000);
long lastPrintout = 0;
int numNodes = 0;
CalculationContext calcContext = new CalculationContext();
int numEmptyChunk = 0;
boolean cache = Baritone.settings().chuckCaching;
boolean cache = Baritone.settings().chuckCaching.get();
while (!openSet.isEmpty() && numEmptyChunk < 50 && System.currentTimeMillis() < timeoutTime) {
if (Baritone.settings().slowPath) {
if (slowPath) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {

View File

@ -99,7 +99,7 @@ public class GoalXZ implements Goal {
diagonal = z;
}
diagonal *= SQRT_2;
return (diagonal + straight) * Baritone.settings().costHeuristic; // big TODO tune
return (diagonal + straight) * Baritone.settings().costHeuristic.get(); // big TODO tune
}
public static GoalXZ fromDirection(Vec3d origin, float yaw, double distance) {

View File

@ -44,5 +44,9 @@ public interface ActionCosts extends ActionCostsButOnlyTheOnesThatMakeMickeyDieI
*/
double PLACE_ONE_BLOCK_COST = 20;
/**
* don't make this Double.MAX_VALUE because it's added to other things, maybe other COST_INFs,
* and that would make it overflow to negative
*/
double COST_INF = 1000000;
}

View File

@ -42,8 +42,8 @@ public class CalculationContext implements Helper {
public CalculationContext(ToolSet toolSet) {
this.toolSet = toolSet;
this.hasWaterBucket = Baritone.settings().allowWaterBucketFall && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether();
this.hasThrowaway = Baritone.settings().allowPlaceThrowaway && MovementHelper.throwaway(false);
this.hasWaterBucket = Baritone.settings().allowWaterBucketFall.get() && InventoryPlayer.isHotbar(player().inventory.getSlotFor(STACK_BUCKET_WATER)) && !world().provider.isNether();
this.hasThrowaway = Baritone.settings().allowPlaceThrowaway.get() && MovementHelper.throwaway(false);
}
public ToolSet getToolSet() {

View File

@ -37,7 +37,7 @@ import net.minecraft.util.math.Vec3d;
import java.util.ArrayList;
import java.util.Optional;
import static baritone.bot.InputOverrideHandler.Input;
import static baritone.bot.utils.InputOverrideHandler.Input;
public abstract class Movement implements Helper, MovementHelper {
@ -265,7 +265,7 @@ public abstract class Movement implements Helper, MovementHelper {
return result;
}
public ArrayList<BlockPos> toWalkInto() {
public ArrayList<BlockPos> toWalkInto() { // overridden by movementdiagonal
if (toWalkIntoCached == null) {
toWalkIntoCached = new ArrayList<>();
}

View File

@ -18,7 +18,6 @@
package baritone.bot.pathing.movement;
import baritone.bot.Baritone;
import baritone.bot.InputOverrideHandler;
import baritone.bot.behavior.impl.LookBehaviorUtils;
import baritone.bot.pathing.movement.MovementState.MovementTarget;
import baritone.bot.pathing.movement.movements.MovementDescend;
@ -30,14 +29,11 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
@ -47,12 +43,6 @@ import java.util.Optional;
*/
public interface MovementHelper extends ActionCosts, Helper {
List<Item> ACCEPTABLE_THROWAWAY_ITEMS = Arrays.asList(
Item.getItemFromBlock(Blocks.DIRT),
Item.getItemFromBlock(Blocks.COBBLESTONE),
Item.getItemFromBlock(Blocks.NETHERRACK)
);
static boolean avoidBreaking(BlockPos pos) {
Block b = BlockStateInterface.getBlock(pos);
BlockPos below = new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ());
@ -178,7 +168,7 @@ public interface MovementHelper extends ActionCosts, Helper {
IBlockState state = BlockStateInterface.get(position);
Block block = state.getBlock();
if (!block.equals(Blocks.AIR) && !canWalkThrough(position)) {
if (!Baritone.settings().allowBreak) {
if (!Baritone.settings().allowBreak.get()) {
return COST_INF;
}
if (avoidBreaking(position)) {
@ -242,7 +232,7 @@ public interface MovementHelper extends ActionCosts, Helper {
NonNullList<ItemStack> inv = p.inventory.mainInventory;
for (byte i = 0; i < 9; i++) {
ItemStack item = inv.get(i);
if (ACCEPTABLE_THROWAWAY_ITEMS.contains(item.getItem())) {
if (Baritone.settings().acceptableThrowAwayItems.get().contains(item.getItem())) {
if (select) {
p.inventory.currentItem = i;
}

View File

@ -17,7 +17,7 @@
package baritone.bot.pathing.movement;
import baritone.bot.InputOverrideHandler.Input;
import baritone.bot.utils.InputOverrideHandler.Input;
import baritone.bot.utils.Rotation;
import net.minecraft.util.math.Vec3d;

View File

@ -17,7 +17,6 @@
package baritone.bot.pathing.movement.movements;
import baritone.bot.InputOverrideHandler;
import baritone.bot.behavior.impl.LookBehaviorUtils;
import baritone.bot.pathing.movement.CalculationContext;
import baritone.bot.pathing.movement.Movement;
@ -25,6 +24,7 @@ import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState;
import baritone.bot.pathing.movement.MovementState.MovementStatus;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.InputOverrideHandler;
import baritone.bot.utils.Utils;
import net.minecraft.block.BlockFalling;
import net.minecraft.block.state.IBlockState;
@ -98,10 +98,6 @@ public class MovementAscend extends Movement {
// TODO incorporate some behavior from ActionClimb (specifically how it waited until it was at most 1.2 blocks away before starting to jump
// for efficiency in ascending minimal height staircases, which is just repeated MovementAscend, so that it doesn't bonk its head on the ceiling repeatedly)
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
case RUNNING:
break;

View File

@ -17,13 +17,13 @@
package baritone.bot.pathing.movement.movements;
import baritone.bot.InputOverrideHandler;
import baritone.bot.pathing.movement.CalculationContext;
import baritone.bot.pathing.movement.Movement;
import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState;
import baritone.bot.pathing.movement.MovementState.MovementStatus;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.InputOverrideHandler;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLadder;
import net.minecraft.block.BlockVine;
@ -59,10 +59,6 @@ public class MovementDescend extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
state.setStatus(MovementStatus.RUNNING);
case RUNNING:

View File

@ -51,10 +51,6 @@ public class MovementDiagonal extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
case RUNNING:
break;

View File

@ -53,10 +53,6 @@ public class MovementDownward extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
case RUNNING:
break;

View File

@ -17,12 +17,12 @@
package baritone.bot.pathing.movement.movements;
import baritone.bot.InputOverrideHandler;
import baritone.bot.behavior.impl.LookBehaviorUtils;
import baritone.bot.pathing.movement.*;
import baritone.bot.pathing.movement.MovementState.MovementStatus;
import baritone.bot.pathing.movement.MovementState.MovementTarget;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.InputOverrideHandler;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.Utils;
import net.minecraft.init.Items;
@ -67,10 +67,6 @@ public class MovementFall extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
state.setStatus(MovementStatus.RUNNING);
case RUNNING:

View File

@ -17,12 +17,12 @@
package baritone.bot.pathing.movement.movements;
import baritone.bot.InputOverrideHandler;
import baritone.bot.pathing.movement.CalculationContext;
import baritone.bot.pathing.movement.Movement;
import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.InputOverrideHandler;
import baritone.bot.utils.Rotation;
import baritone.bot.utils.Utils;
import net.minecraft.block.*;
@ -99,10 +99,6 @@ public class MovementPillar extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
case RUNNING:
break;

View File

@ -17,13 +17,13 @@
package baritone.bot.pathing.movement.movements;
import baritone.bot.InputOverrideHandler;
import baritone.bot.behavior.impl.LookBehaviorUtils;
import baritone.bot.pathing.movement.CalculationContext;
import baritone.bot.pathing.movement.Movement;
import baritone.bot.pathing.movement.MovementHelper;
import baritone.bot.pathing.movement.MovementState;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.InputOverrideHandler;
import baritone.bot.utils.Utils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDoor;
@ -120,10 +120,6 @@ public class MovementTraverse extends Movement {
public MovementState updateState(MovementState state) {
super.updateState(state);
switch (state.getStatus()) {
case PREPPING:
case UNREACHABLE:
case FAILED:
return state;
case WAITING:
case RUNNING:
break;

View File

@ -41,7 +41,7 @@ public class BlockStateInterface implements Helper {
if (chunk.isLoaded()) {
return chunk.getBlockState(pos);
}
if(Baritone.settings().chuckCaching) {
if (Baritone.settings().chuckCaching.get()) {
CachedWorld world = CachedWorldProvider.INSTANCE.getCurrentWorld();
if (world != null) {
PathingBlockType type = world.getBlockType(pos);

View File

@ -18,6 +18,7 @@
package baritone.bot.utils;
import baritone.bot.Baritone;
import baritone.bot.Settings;
import baritone.bot.behavior.Behavior;
import baritone.bot.behavior.impl.PathingBehavior;
import baritone.bot.event.events.ChatEvent;
@ -27,6 +28,8 @@ import baritone.bot.pathing.goals.GoalXZ;
import baritone.bot.pathing.goals.GoalYLevel;
import net.minecraft.util.math.BlockPos;
import java.util.List;
public class ExampleBaritoneControl extends Behavior {
public static ExampleBaritoneControl INSTANCE = new ExampleBaritoneControl();
@ -40,7 +43,7 @@ public class ExampleBaritoneControl extends Behavior {
@Override
public void onSendChatMessage(ChatEvent event) {
if (!Baritone.settings().chatControl) {
if (!Baritone.settings().chatControl.get()) {
return;
}
String msg = event.getMessage();
@ -82,11 +85,6 @@ public class ExampleBaritoneControl extends Behavior {
event.cancel();
return;
}
if (msg.toLowerCase().equals("slowpath")) {
Baritone.settings().slowPath ^= true;
event.cancel();
return;
}
if (msg.toLowerCase().equals("cancel")) {
PathingBehavior.INSTANCE.cancel();
event.cancel();
@ -100,5 +98,14 @@ public class ExampleBaritoneControl extends Behavior {
event.cancel();
return;
}
List<Settings.Setting<Boolean>> toggleable = Baritone.settings().getByValueType(Boolean.class);
for (Settings.Setting<Boolean> setting : toggleable) {
if (msg.toLowerCase().equals(setting.getName().toLowerCase())) {
setting.value ^= true;
event.cancel();
displayChatMessageRaw("Toggled " + setting.getName() + " to " + setting.value);
return;
}
}
}
}

View File

@ -68,7 +68,7 @@ public interface Helper {
}
default void displayChatMessageRaw(String message) {
if (!Baritone.settings().chatDebug) {
if (!Baritone.settings().chatDebug.get()) {
System.out.println("Suppressed debug message:");
System.out.println(message);
return;

View File

@ -15,9 +15,25 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot;
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot.utils;
import baritone.bot.utils.Helper;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
@ -34,7 +50,7 @@ import java.util.Map;
*/
public final class InputOverrideHandler implements Helper {
InputOverrideHandler() {}
public InputOverrideHandler() {}
/**
* Maps keybinds to whether or not we are forcing their state down.

View File

@ -1,7 +1,7 @@
package baritone.movement;
import baritone.Baritone;
import baritone.bot.InputOverrideHandler;
import baritone.bot.utils.InputOverrideHandler;
import baritone.ui.LookManager;
import net.minecraft.block.BlockLadder;
import net.minecraft.block.BlockVine;