waypoints rough start

This commit is contained in:
Leijurv 2018-08-22 15:35:32 -07:00
parent 1251b515fa
commit 7996cfe0a5
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
7 changed files with 131 additions and 36 deletions

View File

@ -23,6 +23,9 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.Chunk;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.BitSet; import java.util.BitSet;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -50,8 +53,14 @@ public final class CachedWorld implements IBlockTypeAccess {
private final LinkedBlockingQueue<Chunk> toPack = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<Chunk> toPack = new LinkedBlockingQueue<>();
public CachedWorld(String directory) { CachedWorld(Path directory) {
this.directory = directory; if (!Files.exists(directory)) {
try {
Files.createDirectories(directory);
} catch (IOException ignored) {}
}
this.directory = directory.toString();
System.out.println("Cached world directory: " + directory);
// Insert an invalid region element // Insert an invalid region element
cachedRegions.put(0, null); cachedRegions.put(0, null);
new PackerThread().start(); new PackerThread().start();

View File

@ -0,0 +1,36 @@
/*
* 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.chunk;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Waypoints {
private final Path directory;
Waypoints(Path directory) {
this.directory = directory;
if (!Files.exists(directory)) {
try {
Files.createDirectories(directory);
} catch (IOException ignored) {}
}
System.out.println("Would save waypoints to " + directory);
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.chunk;
import java.nio.file.Path;
public class WorldData {
public final CachedWorld cache;
public final Waypoints waypoints;
//public final MapData map;
public final Path directory;
WorldData(Path directory) {
this.directory = directory;
this.cache = new CachedWorld(directory.resolve("cache"));
this.waypoints = new Waypoints(directory.resolve("waypoints"));
}
void onClose() {
new Thread() {
public void run() {
System.out.println("Started saving the world in a new thread");
cache.save();
}
}.start();
}
}

View File

@ -17,9 +17,10 @@
package baritone.chunk; package baritone.chunk;
import baritone.utils.Helper; import baritone.Baritone;
import baritone.launch.mixins.accessor.IAnvilChunkLoader; import baritone.launch.mixins.accessor.IAnvilChunkLoader;
import baritone.launch.mixins.accessor.IChunkProviderServer; import baritone.launch.mixins.accessor.IChunkProviderServer;
import baritone.utils.Helper;
import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.server.integrated.IntegratedServer; import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
@ -36,53 +37,60 @@ import java.util.function.Consumer;
* @author Brady * @author Brady
* @since 8/4/2018 11:06 AM * @since 8/4/2018 11:06 AM
*/ */
public enum CachedWorldProvider implements Helper { public enum WorldProvider implements Helper {
INSTANCE; INSTANCE;
private final Map<String, CachedWorld> singlePlayerWorldCache = new HashMap<>(); private final Map<Path, WorldData> worldCache = new HashMap<>();
private CachedWorld currentWorld; private WorldData currentWorld;
public final CachedWorld getCurrentWorld() { public final WorldData getCurrentWorld() {
return this.currentWorld; return this.currentWorld;
} }
public final void initWorld(WorldClient world) { public final void initWorld(WorldClient world) {
IntegratedServer integratedServer; int dimensionID = world.provider.getDimensionType().getId();
if ((integratedServer = mc.getIntegratedServer()) != null) { File directory;
IntegratedServer integratedServer = mc.getIntegratedServer();
WorldServer localServerWorld = integratedServer.getWorld(world.provider.getDimensionType().getId()); if (integratedServer != null) {
WorldServer localServerWorld = integratedServer.getWorld(dimensionID);
IChunkProviderServer provider = (IChunkProviderServer) localServerWorld.getChunkProvider(); IChunkProviderServer provider = (IChunkProviderServer) localServerWorld.getChunkProvider();
IAnvilChunkLoader loader = (IAnvilChunkLoader) provider.getChunkLoader(); IAnvilChunkLoader loader = (IAnvilChunkLoader) provider.getChunkLoader();
directory = loader.getChunkSaveLocation();
Path dir = new File(new File(loader.getChunkSaveLocation(), "region"), "cache").toPath(); if (!directory.getParentFile().getName().equals("saves")) {
if (!Files.exists(dir)) { // subdirectory of the main save directory for this world
try { directory = directory.getParentFile();
Files.createDirectories(dir);
} catch (IOException ignored) {}
} }
this.currentWorld = this.singlePlayerWorldCache.computeIfAbsent(dir.toString(), CachedWorld::new); directory = new File(directory, "baritone");
} else {
//remote
directory = new File(Baritone.INSTANCE.getDir(), mc.getCurrentServerData().serverIP);
} }
// TODO: Store server worlds directory = new File(directory, "DIM" + dimensionID);
Path dir = directory.toPath();
if (!Files.exists(dir)) {
try {
Files.createDirectories(dir);
} catch (IOException ignored) {}
}
System.out.println("Baritone world data dir: " + dir);
this.currentWorld = this.worldCache.computeIfAbsent(dir, WorldData::new);
} }
public final void closeWorld() { public final void closeWorld() {
CachedWorld world = this.currentWorld; WorldData world = this.currentWorld;
this.currentWorld = null; this.currentWorld = null;
if (world == null) { if (world == null) {
return; return;
} }
new Thread() { world.onClose();
public void run() {
System.out.println("Started saving the world in a new thread");
world.save();
}
}.start();
} }
public final void ifWorldLoaded(Consumer<CachedWorld> currentWorldConsumer) { public final void ifWorldLoaded(Consumer<WorldData> currentWorldConsumer) {
if (this.currentWorld != null) if (this.currentWorld != null)
currentWorldConsumer.accept(this.currentWorld); currentWorldConsumer.accept(this.currentWorld);
} }

View File

@ -35,7 +35,7 @@
package baritone.event; package baritone.event;
import baritone.Baritone; import baritone.Baritone;
import baritone.chunk.CachedWorldProvider; import baritone.chunk.WorldProvider;
import baritone.event.events.*; import baritone.event.events.*;
import baritone.event.events.type.EventState; import baritone.event.events.type.EventState;
import baritone.event.listener.IGameEventListener; import baritone.event.listener.IGameEventListener;
@ -113,9 +113,9 @@ public final class GameEventHandler implements IGameEventListener, Helper {
if (Baritone.settings().chunkCaching.get()) { if (Baritone.settings().chunkCaching.get()) {
if (isPostPopulate || isPreUnload) { if (isPostPopulate || isPreUnload) {
CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> { WorldProvider.INSTANCE.ifWorldLoaded(world -> {
Chunk chunk = mc.world.getChunk(event.getX(), event.getZ()); Chunk chunk = mc.world.getChunk(event.getX(), event.getZ());
world.queueForPacking(chunk); world.cache.queueForPacking(chunk);
}); });
} }
} }
@ -126,7 +126,7 @@ public final class GameEventHandler implements IGameEventListener, Helper {
@Override @Override
public final void onRenderPass(RenderEvent event) { public final void onRenderPass(RenderEvent event) {
/* /*
CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> world.forEachRegion(region -> region.forEachChunk(chunk -> { WorldProvider.INSTANCE.ifWorldLoaded(world -> world.forEachRegion(region -> region.forEachChunk(chunk -> {
drawChunkLine(region.getX() * 512 + chunk.getX() * 16, region.getZ() * 512 + chunk.getZ() * 16, event.getPartialTicks()); drawChunkLine(region.getX() * 512 + chunk.getX() * 16, region.getZ() * 512 + chunk.getZ() * 16, event.getPartialTicks());
}))); })));
*/ */
@ -137,7 +137,7 @@ public final class GameEventHandler implements IGameEventListener, Helper {
@Override @Override
public final void onWorldEvent(WorldEvent event) { public final void onWorldEvent(WorldEvent event) {
if (Baritone.settings().chunkCaching.get()) { if (Baritone.settings().chunkCaching.get()) {
CachedWorldProvider cache = CachedWorldProvider.INSTANCE; WorldProvider cache = WorldProvider.INSTANCE;
switch (event.getState()) { switch (event.getState()) {
case PRE: case PRE:

View File

@ -18,7 +18,7 @@
package baritone.pathing.calc; package baritone.pathing.calc;
import baritone.Baritone; import baritone.Baritone;
import baritone.chunk.CachedWorldProvider; import baritone.chunk.WorldProvider;
import baritone.pathing.calc.openset.BinaryHeapOpenSet; import baritone.pathing.calc.openset.BinaryHeapOpenSet;
import baritone.pathing.calc.openset.IOpenSet; import baritone.pathing.calc.openset.IOpenSet;
import baritone.pathing.goals.Goal; import baritone.pathing.goals.Goal;
@ -113,8 +113,8 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest(); BetterBlockPos dest = (BetterBlockPos) movementToGetToNeighbor.getDest();
boolean isPositionCached = false; boolean isPositionCached = false;
if (cache) { if (cache) {
if (CachedWorldProvider.INSTANCE.getCurrentWorld() != null) { if (WorldProvider.INSTANCE.getCurrentWorld() != null) {
if (CachedWorldProvider.INSTANCE.getCurrentWorld().getBlock(dest) != null) { if (WorldProvider.INSTANCE.getCurrentWorld().cache.getBlock(dest) != null) {
isPositionCached = true; isPositionCached = true;
} }
} }

View File

@ -19,7 +19,7 @@ package baritone.utils;
import baritone.Baritone; import baritone.Baritone;
import baritone.chunk.CachedWorld; import baritone.chunk.CachedWorld;
import baritone.chunk.CachedWorldProvider; import baritone.chunk.WorldProvider;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockFalling; import net.minecraft.block.BlockFalling;
import net.minecraft.block.BlockLiquid; import net.minecraft.block.BlockLiquid;
@ -43,7 +43,7 @@ public class BlockStateInterface implements Helper {
} }
} }
if (Baritone.settings().chunkCaching.get()) { if (Baritone.settings().chunkCaching.get()) {
CachedWorld world = CachedWorldProvider.INSTANCE.getCurrentWorld(); CachedWorld world = WorldProvider.INSTANCE.getCurrentWorld().cache;
if (world != null) { if (world != null) {
IBlockState type = world.getBlock(pos); IBlockState type = world.getBlock(pos);
if (type != null) { if (type != null) {