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

164 lines
5.8 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 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.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;
import baritone.pathing.goals.GoalComposite;
import baritone.pathing.goals.GoalTwoBlocks;
import baritone.pathing.path.IPath;
2018-08-28 18:43:28 +00:00
import baritone.utils.BlockStateInterface;
2018-09-12 22:53:29 +00:00
import baritone.utils.Helper;
import net.minecraft.block.Block;
2018-08-28 18:43:28 +00:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
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 MineBehavior() {
}
private List<Block> mining;
2018-09-15 19:56:35 +00:00
private List<BlockPos> locationsCache;
2018-08-28 18:43:28 +00:00
@Override
public void onTick(TickEvent event) {
if (mining == null) {
return;
}
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get();
if (mineGoalUpdateInterval != 0) {
if (event.getCount() % mineGoalUpdateInterval == 0) {
updateGoal();
}
}
if (!Baritone.settings().cancelOnGoalInvalidation.get()) {
return;
}
Optional<IPath> path = PathingBehavior.INSTANCE.getPath();
if (!path.isPresent()) {
return;
}
Goal currentGoal = PathingBehavior.INSTANCE.getGoal();
if (currentGoal == null) {
return;
}
Goal intended = path.get().getGoal();
BlockPos end = path.get().getDest();
if (intended.isInGoal(end) && !currentGoal.isInGoal(end)) {
// this path used to end in the goal
// but the goal has changed, so there's no reason to continue...
PathingBehavior.INSTANCE.cancel();
}
}
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);
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locationsCache.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
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;
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path();
}
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");
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) {
locs = locs.subList(0, max);
2018-09-01 19:16:55 +00:00
}
return locs;
2018-08-28 18:43:28 +00:00
}
public void mine(String... blocks) {
this.mining = blocks == null || blocks.length == 0 ? null : Arrays.stream(blocks).map(ChunkPacker::stringToBlock).collect(Collectors.toList());
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() {
mine((String[]) null);
PathingBehavior.INSTANCE.cancel();
2018-08-28 18:43:28 +00:00
}
2018-08-28 18:28:36 +00:00
}