From 74d7483b3c228ab464c16ff4b4005df2a568ffee Mon Sep 17 00:00:00 2001 From: Leijurv Date: Tue, 5 Mar 2019 12:26:52 -0800 Subject: [PATCH] explore --- src/api/java/baritone/api/Settings.java | 7 ++ src/main/java/baritone/Baritone.java | 11 +- src/main/java/baritone/cache/CachedWorld.java | 4 + .../java/baritone/process/ExploreProcess.java | 100 ++++++++++++++++++ .../utils/ExampleBaritoneControl.java | 15 +++ 5 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 src/main/java/baritone/process/ExploreProcess.java diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 0e31d942..b9a45960 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -625,6 +625,13 @@ public final class Settings { */ public final Setting followRadius = new Setting<>(3); + /** + * true = exploration uses pythagorean distance to choose closest uncached chunk + *

+ * false = exploration uses manhattan / taxicab distance to choose + */ + public final Setting exploreUsePythagorean = new Setting<>(false); + /** * Cached chunks (regardless of if they're in RAM or saved to disk) expire and are deleted after this number of seconds * -1 to disable diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 51a6ff69..ea8d40f2 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -25,10 +25,7 @@ import baritone.api.utils.IPlayerContext; import baritone.behavior.*; import baritone.cache.WorldProvider; import baritone.event.GameEventHandler; -import baritone.process.CustomGoalProcess; -import baritone.process.FollowProcess; -import baritone.process.GetToBlockProcess; -import baritone.process.MineProcess; +import baritone.process.*; import baritone.utils.*; import baritone.utils.player.PrimaryPlayerContext; import net.minecraft.client.Minecraft; @@ -80,6 +77,7 @@ public class Baritone implements IBaritone { private MineProcess mineProcess; private GetToBlockProcess getToBlockProcess; private CustomGoalProcess customGoalProcess; + private ExploreProcess exploreProcess; private PathingControlManager pathingControlManager; @@ -118,6 +116,7 @@ public class Baritone implements IBaritone { mineProcess = new MineProcess(this); customGoalProcess = new CustomGoalProcess(this); // very high iq getToBlockProcess = new GetToBlockProcess(this); + exploreProcess = new ExploreProcess(this); } this.worldProvider = new WorldProvider(); @@ -177,6 +176,10 @@ public class Baritone implements IBaritone { return this.lookBehavior; } + public ExploreProcess getExploreProcess() { + return this.exploreProcess; + } + @Override public MineProcess getMineProcess() { return this.mineProcess; diff --git a/src/main/java/baritone/cache/CachedWorld.java b/src/main/java/baritone/cache/CachedWorld.java index e8ea5cae..7e34fa08 100644 --- a/src/main/java/baritone/cache/CachedWorld.java +++ b/src/main/java/baritone/cache/CachedWorld.java @@ -107,6 +107,10 @@ public final class CachedWorld implements ICachedWorld, Helper { return region.isCached(blockX & 511, blockZ & 511); } + public final boolean regionLoaded(int blockX, int blockZ) { + return getRegion(blockX >> 9, blockZ >> 9) != null; + } + @Override public final ArrayList getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq) { ArrayList res = new ArrayList<>(); diff --git a/src/main/java/baritone/process/ExploreProcess.java b/src/main/java/baritone/process/ExploreProcess.java new file mode 100644 index 00000000..3f8d0f82 --- /dev/null +++ b/src/main/java/baritone/process/ExploreProcess.java @@ -0,0 +1,100 @@ +/* + * 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 + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.process; + +import baritone.Baritone; +import baritone.api.cache.ICachedWorld; +import baritone.api.pathing.goals.GoalXZ; +import baritone.api.process.PathingCommand; +import baritone.api.process.PathingCommandType; +import baritone.cache.CachedWorld; +import baritone.utils.BaritoneProcessHelper; +import net.minecraft.util.math.BlockPos; + +public class ExploreProcess extends BaritoneProcessHelper { + + private BlockPos explorationOrigin; + + public ExploreProcess(Baritone baritone) { + super(baritone, 0); + } + + @Override + public boolean isActive() { + return explorationOrigin != null; + } + + public void explore(int centerX, int centerZ) { + explorationOrigin = new BlockPos(centerX, 0, centerZ); + } + + @Override + public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) { + if (calcFailed) { + logDirect("Failed"); + onLostControl(); + return null; + } + BlockPos closestUncached = closestUncachedChunk(explorationOrigin); + if (closestUncached == null) { + logDebug("awaiting region load from disk"); + return new PathingCommand(null, PathingCommandType.REQUEST_PAUSE); + } + System.out.println("Closest uncached: " + closestUncached); + return new PathingCommand(new GoalXZ(closestUncached.getX(), closestUncached.getZ()), PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH); + } + + private BlockPos closestUncachedChunk(BlockPos pos) { + int chunkX = pos.getX() >> 4; + int chunkZ = pos.getZ() >> 4; + ICachedWorld cache = baritone.getWorldProvider().getCurrentWorld().getCachedWorld(); + for (int dist = 0; ; dist++) { + for (int dx = -dist; dx <= dist; dx++) { + for (int dz = -dist; dz <= dist; dz++) { + int trueDist = Baritone.settings().exploreUsePythagorean.value ? dx * dx + dz + dz : Math.abs(dx) + Math.abs(dz); + if (trueDist != dist) { + continue; // not considering this one just yet in our expanding search + } + int centerX = (chunkX + dx) * 16 + 8; + int centerZ = (chunkZ + dz) * 18 + 8; + + if (cache.isCached(centerX, centerZ)) { + continue; + } + if (!((CachedWorld) cache).regionLoaded(centerX, centerZ)) { + Baritone.getExecutor().execute(() -> { + ((CachedWorld) cache).tryLoadFromDisk(centerX >> 9, centerZ >> 9); + }); + return null; // we still need to load regions from disk in order to decide properly + } + return new BlockPos(centerX, 0, centerZ); + } + } + } + } + + @Override + public void onLostControl() { + explorationOrigin = null; + } + + @Override + public String displayName() { + return "Exploring around " + explorationOrigin + ", currently going to " + closestUncachedChunk(explorationOrigin); + } +} diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 7988d796..89aa0fb5 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -374,6 +374,21 @@ public class ExampleBaritoneControl extends Behavior implements Helper { logDirect("ok"); return true; } + if (msg.startsWith("explore")) { + String rest = msg.substring("explore".length()).trim(); + int centerX; + int centerZ; + try { + centerX = Integer.parseInt(rest.split(" ")[0]); + centerZ = Integer.parseInt(rest.split(" ")[1]); + } catch (Exception ex) { + centerX = ctx.playerFeet().x; + centerZ = ctx.playerFeet().z; + } + baritone.getExploreProcess().explore(centerX, centerZ); + logDirect("Exploring from " + centerX + "," + centerZ); + return true; + } if (msg.startsWith("find")) { String blockType = msg.substring(4).trim(); ArrayList locs = baritone.getWorldProvider().getCurrentWorld().getCachedWorld().getLocationsOf(blockType, 1, ctx.playerFeet().getX(), ctx.playerFeet().getZ(), 4);