toolset introduction

This commit is contained in:
Leijurv 2018-08-02 13:49:31 -04:00
parent ee5d382693
commit 0f74141d23
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
3 changed files with 82 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import baritone.bot.behavior.Behavior;
import baritone.bot.event.IGameEventListener;
import baritone.bot.event.events.ChatEvent;
import baritone.bot.event.events.ChunkEvent;
import baritone.bot.pathing.action.ActionWorldHelper;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
@ -22,9 +21,6 @@ public final class GameEventHandler implements IGameEventListener {
@Override
public final void onTick() {
dispatch(behavior -> onTick());
while (true) {
System.out.println(ActionWorldHelper.lavaFlowing);
}
}
@Override

View File

@ -1,9 +1,11 @@
package baritone.bot.pathing.action;
import baritone.bot.utils.BlockStateInterface;
import baritone.bot.utils.ToolSet;
import net.minecraft.block.*;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
/**
@ -11,7 +13,7 @@ import net.minecraft.util.math.BlockPos;
*
* @author leijurv
*/
public interface ActionWorldHelper {
public interface ActionWorldHelper extends ActionCosts {
Block waterFlowing = Block.getBlockById(8);
Block waterStill = Block.getBlockById(9);
Block lavaFlowing = Block.getBlockById(10);
@ -116,4 +118,22 @@ public interface ActionWorldHelper {
}
return state.isBlockNormalCube() && !isLava(block);
}
static boolean canFall(BlockPos pos) {
return BlockStateInterface.get(pos).getBlock() instanceof BlockFalling;
}
static double getHardness(ToolSet ts, IBlockState block, BlockPos position) {
if (!block.equals(Blocks.AIR) && !canWalkThrough(position)) {
if (avoidBreaking(position)) {
return COST_INF;
}
//if (!Baritone.allowBreakOrPlace) {
// return COST_INF;
//}
double m = Block.getBlockFromName("minecraft:crafting_table").equals(block) ? 10 : 1;
return m / ts.getStrVsBlock(block, position) + BREAK_ONE_BLOCK_ADD;
}
return 0;
}
}

View File

@ -1,7 +1,5 @@
package baritone.util;
package baritone.bot.utils;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
@ -13,37 +11,59 @@ import net.minecraft.item.ItemTool;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A cached list of the best tools on the hotbar for any block
*
* @author avecowa
*/
public class ToolSet {
public ArrayList<ItemTool> tools;
public ArrayList<Byte> slots;
public HashMap<Block, Byte> cache = new HashMap<Block, Byte>();
public ToolSet(ArrayList<ItemTool> tools, ArrayList<Byte> slots) {
this.tools = tools;
this.slots = slots;
}
/**
* A list of tools on the hotbar that should be considered.
* Note that if there are no tools on the hotbar this list will still have one (null) entry.
*/
List<ItemTool> tools;
/**
* A mapping from the tools array to what hotbar slots the tool is actually in.
* tools.get(i) will be on your hotbar in slot slots.get(i)
*/
List<Byte> slots;
/**
* A mapping from a block to which tool index is best for it.
* The values in this map are *not* hotbar slots indexes, they need to be looked up in slots
* in order to be converted into hotbar slots.
*/
Map<Block, Byte> cache = new HashMap<>();
/**
* Create a toolset from the current player's inventory (but don't calculate any hardness values just yet)
*/
public ToolSet() {
EntityPlayerSP p = Minecraft.getMinecraft().player;
NonNullList<ItemStack> inv = p.inventory.mainInventory;
tools = new ArrayList<ItemTool>();
slots = new ArrayList<Byte>();
//Out.log("inv: " + Arrays.toString(inv));
tools = new ArrayList<>();
slots = new ArrayList<>();
boolean fnull = false;
for (byte i = 0; i < 9; i++) {
if (!fnull || ((!(inv.get(i).getItem() instanceof ItemAir)) && inv.get(i).getItem() instanceof ItemTool)) {
tools.add((!(inv.get(i).getItem() instanceof ItemAir)) ? (ItemTool) inv.get(i).getItem() : null);
tools.add(inv.get(i).getItem() instanceof ItemTool ? (ItemTool) inv.get(i).getItem() : null);
slots.add(i);
fnull |= (inv.get(i).getItem() instanceof ItemAir) || (!inv.get(i).getItem().isDamageable());
}
}
}
/**
* A caching wrapper around getBestToolIndex
*
* @param b the blockstate to be mined
* @return get which tool on the hotbar is best for mining it
*/
public Item getBestTool(IBlockState b) {
if (cache.get(b.getBlock()) != null) {
return tools.get(cache.get(b.getBlock()));
@ -51,29 +71,36 @@ public class ToolSet {
return tools.get(getBestToolIndex(b));
}
/**
* Calculate which tool on the hotbar is best for mining
*
* @param b the blockstate to be mined
* @return a byte indicating the index in the tools array that worked best
*/
private byte getBestToolIndex(IBlockState b) {
byte best = 0;
//Out.log("best: " + best);
float value = -1;
for (byte i = 0; i < tools.size(); i++) {
Item item = tools.get(i);
if (item == null) {
item = Item.getByNameOrId("minecraft:apple");
}
//Out.log(inv[i]);
float v = item.getDestroySpeed(new ItemStack(item), b);
//Out.log("v: " + v);
if (v < value || value == -1) {
value = v;
best = i;
}
}
//Out.log("best: " + best);
cache.put(b.getBlock(), best);
return best;
}
/**
* Get which hotbar slot should be selected for fastest mining
*
* @param b the blockstate to be mined
* @return a byte indicating which hotbar slot worked best
*/
public byte getBestSlot(IBlockState b) {
if (cache.get(b.getBlock()) != null) {
return slots.get(cache.get(b.getBlock()));
@ -81,6 +108,13 @@ public class ToolSet {
return slots.get(getBestToolIndex(b));
}
/**
* Using the best tool on the hotbar, how long would it take to mine this block
*
* @param b the blockstate to be mined
* @param pos the blockpos to be mined
* @return how long it would take in ticks
*/
public double getStrVsBlock(IBlockState b, BlockPos pos) {
Item item = this.getBestTool(b);
if (item == null) {
@ -90,6 +124,13 @@ public class ToolSet {
return f < 0.0F ? 0.0F : (!canHarvest(b, item) ? item.getDestroySpeed(new ItemStack(item), b) / f / 100.0F : item.getDestroySpeed(new ItemStack(item), b) / f / 30.0F);
}
/**
* Whether a tool can be efficiently used against a block
*
* @param blockIn the blockstate to be mined
* @param item the tool to be used
* @return whether or not this tool would help
*/
public boolean canHarvest(IBlockState blockIn, Item item) {
if (blockIn.getMaterial().isToolNotRequired()) {
return true;