baritone/src/main/java/baritone/behavior/MineBehavior.java

190 lines
7.0 KiB
Java
Raw Normal View History

2018-08-28 18:43:28 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
2018-08-28 18:43:28 +00:00
* 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 Lesser General Public License for more details.
2018-08-28 18:43:28 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-28 18:43:28 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.behavior;
2018-08-28 18:28:36 +00:00
import baritone.Baritone;
2018-09-12 22:53:29 +00:00
import baritone.api.behavior.Behavior;
import baritone.api.event.events.PathEvent;
import baritone.api.event.events.TickEvent;
2018-09-11 17:28:03 +00:00
import baritone.cache.CachedChunk;
import baritone.cache.ChunkPacker;
import baritone.cache.WorldProvider;
import baritone.cache.WorldScanner;
2018-08-28 18:43:28 +00:00
import baritone.pathing.goals.Goal;
2018-09-16 20:00:44 +00:00
import baritone.pathing.goals.GoalBlock;
2018-08-28 18:43:28 +00:00
import baritone.pathing.goals.GoalComposite;
import baritone.pathing.goals.GoalTwoBlocks;
import baritone.utils.BlockStateInterface;
2018-09-12 22:53:29 +00:00
import baritone.utils.Helper;
import net.minecraft.block.Block;
2018-09-16 20:00:44 +00:00
import net.minecraft.init.Blocks;
2018-09-17 21:22:45 +00:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2018-08-28 18:43:28 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
2018-09-17 21:22:45 +00:00
import java.util.*;
2018-08-28 18:43:28 +00:00
import java.util.stream.Collectors;
/**
* Mine blocks of a certain type
*
* @author leijurv
*/
public final class MineBehavior extends Behavior implements Helper {
2018-09-12 22:53:29 +00:00
2018-08-28 18:43:28 +00:00
public static final MineBehavior INSTANCE = new MineBehavior();
private List<Block> mining;
2018-09-15 19:56:35 +00:00
private List<BlockPos> locationsCache;
2018-09-17 21:22:45 +00:00
private int quantity;
2018-08-28 18:43:28 +00:00
2018-09-17 03:16:05 +00:00
private MineBehavior() {}
@Override
public void onTick(TickEvent event) {
if (mining == null) {
return;
}
2018-09-17 21:22:45 +00:00
if (quantity > 0) {
Item item = mining.get(0).getItemDropped(mining.get(0).getDefaultState(), new Random(), 0);
int curr = player().inventory.mainInventory.stream().filter(stack -> item.equals(stack.getItem())).mapToInt(ItemStack::getCount).sum();
System.out.println("Currently have " + curr + " " + item);
if (curr >= quantity) {
logDirect("Have " + curr + " " + item.getItemStackDisplayName(new ItemStack(item, 1)));
cancel();
return;
}
}
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get();
if (mineGoalUpdateInterval != 0) {
if (event.getCount() % mineGoalUpdateInterval == 0) {
updateGoal();
}
}
2018-09-17 17:56:37 +00:00
PathingBehavior.INSTANCE.revalidateGoal();
}
2018-08-28 18:43:28 +00:00
@Override
2018-09-04 22:33:38 +00:00
public void onPathEvent(PathEvent event) {
updateGoal();
}
private void updateGoal() {
2018-08-28 18:43:28 +00:00
if (mining == null) {
return;
}
2018-09-15 19:56:35 +00:00
if (!locationsCache.isEmpty()) {
locationsCache = prune(new ArrayList<>(locationsCache), mining, 64);
2018-09-16 20:00:44 +00:00
PathingBehavior.INSTANCE.setGoal(coalesce(locationsCache));
2018-09-15 19:56:35 +00:00
PathingBehavior.INSTANCE.path();
}
List<BlockPos> locs = scanFor(mining, 64);
if (locs.isEmpty()) {
2018-09-11 20:45:43 +00:00
logDebug("No locations for " + mining + " known, cancelling");
cancel();
return;
}
2018-09-15 19:56:35 +00:00
locationsCache = locs;
2018-09-16 20:00:44 +00:00
PathingBehavior.INSTANCE.setGoal(coalesce(locs));
PathingBehavior.INSTANCE.path();
2018-09-17 21:22:45 +00:00
Blocks.DIAMOND_ORE.getItemDropped(Blocks.DIAMOND_ORE.getDefaultState(), new Random(), 0);
}
2018-09-16 20:00:44 +00:00
public static GoalComposite coalesce(List<BlockPos> locs) {
return new GoalComposite(locs.stream().map(loc -> {
if (!Baritone.settings().forceInternalMining.get()) {
return new GoalTwoBlocks(loc);
}
boolean noUp = locs.contains(loc.up()) && !(Baritone.settings().internalMiningAirException.get() && BlockStateInterface.getBlock(loc.up()) == Blocks.AIR);
boolean noDown = locs.contains(loc.down()) && !(Baritone.settings().internalMiningAirException.get() && BlockStateInterface.getBlock(loc.up()) == Blocks.AIR);
if (noUp) {
if (noDown) {
return new GoalTwoBlocks(loc);
} else {
return new GoalBlock(loc.down());
}
} else {
if (noDown) {
return new GoalBlock(loc);
} else {
return new GoalTwoBlocks(loc);
}
}
}).toArray(Goal[]::new));
}
public static List<BlockPos> scanFor(List<Block> mining, int max) {
2018-09-04 15:50:42 +00:00
List<BlockPos> locs = new ArrayList<>();
List<Block> uninteresting = new ArrayList<>();
2018-09-04 22:08:21 +00:00
//long b = System.currentTimeMillis();
for (Block m : mining) {
if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) {
locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(ChunkPacker.blockToString(m), 1, 1));
} else {
uninteresting.add(m);
}
2018-09-04 15:50:42 +00:00
}
2018-09-04 22:08:21 +00:00
//System.out.println("Scan of cached chunks took " + (System.currentTimeMillis() - b) + "ms");
2018-09-16 20:28:16 +00:00
if (locs.isEmpty()) {
uninteresting = mining;
}
2018-09-04 22:08:21 +00:00
if (!uninteresting.isEmpty()) {
//long before = System.currentTimeMillis();
locs.addAll(WorldScanner.INSTANCE.scanLoadedChunks(uninteresting, max));
//System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms");
}
2018-09-15 19:56:35 +00:00
return prune(locs, mining, max);
}
public static List<BlockPos> prune(List<BlockPos> locs, List<Block> mining, int max) {
BlockPos playerFeet = MineBehavior.INSTANCE.playerFeet();
2018-08-28 18:43:28 +00:00
locs.sort(Comparator.comparingDouble(playerFeet::distanceSq));
// remove any that are within loaded chunks that aren't actually what we want
locs.removeAll(locs.stream()
.filter(pos -> !(MineBehavior.INSTANCE.world().getChunk(pos) instanceof EmptyChunk))
.filter(pos -> !mining.contains(BlockStateInterface.get(pos).getBlock()))
2018-08-28 18:43:28 +00:00
.collect(Collectors.toList()));
if (locs.size() > max) {
2018-09-17 00:49:19 +00:00
return locs.subList(0, max);
2018-09-01 19:16:55 +00:00
}
return locs;
2018-08-28 18:43:28 +00:00
}
2018-09-17 21:22:45 +00:00
public void mine(int quantity, String... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList());
2018-09-17 21:22:45 +00:00
this.quantity = quantity;
2018-09-15 19:56:35 +00:00
this.locationsCache = new ArrayList<>();
updateGoal();
}
public void mine(Block... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.asList(blocks);
2018-09-15 19:56:35 +00:00
this.locationsCache = new ArrayList<>();
2018-09-04 22:33:38 +00:00
updateGoal();
2018-08-28 18:43:28 +00:00
}
public void cancel() {
2018-09-17 21:22:45 +00:00
mine(0, (String[]) null);
PathingBehavior.INSTANCE.cancel();
2018-08-28 18:43:28 +00:00
}
2018-08-28 18:28:36 +00:00
}