Use the actual game's code in ToolSet#getStrVsBlock

This commit is contained in:
Brady 2018-08-10 00:08:29 -05:00
parent 326d979c0b
commit 22a6a4285f
No known key found for this signature in database
GPG Key ID: 73A788379A197567
3 changed files with 33 additions and 52 deletions

View File

@ -44,12 +44,5 @@ public interface ActionCosts extends ActionCostsButOnlyTheOnesThatMakeMickeyDieI
*/ */
double PLACE_ONE_BLOCK_COST = 20; double PLACE_ONE_BLOCK_COST = 20;
/**
* Add this to the cost of breaking any block. The cost of breaking any
* block is calculated as the number of ticks that block takes to break with
* the tools you have. You add this because there's always a little overhead
* (e.g. looking at the block)
*/
double BREAK_ONE_BLOCK_ADD = 4;
double COST_INF = 1000000; double COST_INF = 1000000;
} }

View File

@ -139,7 +139,7 @@ public interface MovementHelper extends ActionCosts, Helper {
// return COST_INF; // return COST_INF;
//} //}
double m = Blocks.CRAFTING_TABLE.equals(block) ? 10 : 1; double m = Blocks.CRAFTING_TABLE.equals(block) ? 10 : 1;
return m / ts.getStrVsBlock(state, position) + BREAK_ONE_BLOCK_ADD; return m / ts.getStrVsBlock(state, position);
} }
return 0; return 0;
} }

View File

@ -39,26 +39,26 @@ import java.util.Map;
* *
* @author avecowa * @author avecowa
*/ */
public class ToolSet { public class ToolSet implements Helper {
private static final Item FALLBACK_ITEM = Items.APPLE;
/** /**
* A list of tools on the hotbar that should be considered. * 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. * Note that if there are no tools on the hotbar this list will still have one (null) entry.
*/ */
List<ItemTool> tools; private List<ItemTool> tools;
/** /**
* A mapping from the tools array to what hotbar slots the tool is actually in. * 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) * tools.get(i) will be on your hotbar in slot slots.get(i)
*/ */
List<Byte> slots; private List<Byte> slots;
/** /**
* A mapping from a block to which tool index is best for it. * 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 * 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. * in order to be converted into hotbar slots.
*/ */
Map<Block, Byte> cache = new HashMap<>(); private Map<Block, Byte> cache = new HashMap<>();
/** /**
* Create a toolset from the current player's inventory (but don't calculate any hardness values just yet) * Create a toolset from the current player's inventory (but don't calculate any hardness values just yet)
@ -81,14 +81,11 @@ public class ToolSet {
/** /**
* A caching wrapper around getBestToolIndex * A caching wrapper around getBestToolIndex
* *
* @param b the blockstate to be mined * @param state the blockstate to be mined
* @return get which tool on the hotbar is best for mining it * @return get which tool on the hotbar is best for mining it
*/ */
public Item getBestTool(IBlockState b) { public Item getBestTool(IBlockState state) {
if (cache.get(b.getBlock()) != null) { return tools.get(cache.computeIfAbsent(state.getBlock(), block -> getBestToolIndex(state)));
return tools.get(cache.get(b.getBlock()));
}
return tools.get(getBestToolIndex(b));
} }
/** /**
@ -102,60 +99,51 @@ public class ToolSet {
float value = -1; float value = -1;
for (byte i = 0; i < tools.size(); i++) { for (byte i = 0; i < tools.size(); i++) {
Item item = tools.get(i); Item item = tools.get(i);
if (item == null) { if (item == null)
item = FALLBACK_ITEM; continue;
}
float v = item.getDestroySpeed(new ItemStack(item), b); float v = item.getDestroySpeed(new ItemStack(item), b);
if (v < value || value == -1) { if (v < value || value == -1) {
value = v; value = v;
best = i; best = i;
} }
} }
cache.put(b.getBlock(), best);
return best; return best;
} }
/** /**
* Get which hotbar slot should be selected for fastest mining * Get which hotbar slot should be selected for fastest mining
* *
* @param b the blockstate to be mined * @param state the blockstate to be mined
* @return a byte indicating which hotbar slot worked best * @return a byte indicating which hotbar slot worked best
*/ */
public byte getBestSlot(IBlockState b) { public byte getBestSlot(IBlockState state) {
if (cache.get(b.getBlock()) != null) { return slots.get(cache.computeIfAbsent(state.getBlock(), block -> getBestToolIndex(state)));
return slots.get(cache.get(b.getBlock()));
}
return slots.get(getBestToolIndex(b));
} }
/** /**
* Using the best tool on the hotbar, how long would it take to mine this block * Using the best tool on the hotbar, how long would it take to mine this block
* *
* @param b the blockstate to be mined * @param state the blockstate to be mined
* @param pos the blockpos to be mined * @param pos the blockpos to be mined
* @return how long it would take in ticks * @return how long it would take in ticks
*/ */
public double getStrVsBlock(IBlockState b, BlockPos pos) { public double getStrVsBlock(IBlockState state, BlockPos pos) {
Item item = this.getBestTool(b); // Calculate the slot with the best item
if (item == null) { byte slot = this.getBestSlot(state);
item = FALLBACK_ITEM;
}
float f = b.getBlockHardness(Minecraft.getMinecraft().world, pos);
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);
}
/** // Save the old current slot
* Whether a tool can be efficiently used against a block int oldSlot = player().inventory.currentItem;
*
* @param blockIn the blockstate to be mined // Set the best slot
* @param item the tool to be used player().inventory.currentItem = slot;
* @return whether or not this tool would help
*/ // Calculate the relative hardness of the block to the player
public boolean canHarvest(IBlockState blockIn, Item item) { float hardness = state.getPlayerRelativeBlockHardness(player(), world(), pos);
if (blockIn.getMaterial().isToolNotRequired()) {
return true; // Restore the old slot
} else { player().inventory.currentItem = oldSlot;
return new ItemStack(item).canHarvestBlock(blockIn);
} return hardness;
} }
} }