2018-08-07 22:16:53 -05:00
|
|
|
/*
|
|
|
|
|
* This file is part of Baritone.
|
|
|
|
|
*
|
|
|
|
|
* Baritone is free software: you can redistribute it and/or modify
|
2018-09-17 17:11:40 -05:00
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2018-08-07 22:16:53 -05:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2018-08-07 23:15:22 -05:00
|
|
|
* Baritone is distributed in the hope that it will be useful,
|
2018-08-07 22:16:53 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-09-17 17:11:40 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2018-08-07 22:16:53 -05:00
|
|
|
*
|
2018-09-17 17:11:40 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2018-08-07 22:16:53 -05:00
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-09-11 10:28:03 -07:00
|
|
|
package baritone.cache;
|
2018-08-04 23:36:59 -05:00
|
|
|
|
2019-04-17 18:10:47 -07:00
|
|
|
import baritone.api.utils.BlockUtils;
|
2018-08-22 13:15:56 -07:00
|
|
|
import baritone.pathing.movement.MovementHelper;
|
|
|
|
|
import baritone.utils.pathing.PathingBlockType;
|
2019-03-14 16:06:32 -07:00
|
|
|
import net.minecraft.block.*;
|
2018-08-23 12:54:12 -07:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-03-08 14:30:43 -06:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2020-01-09 17:15:05 -08:00
|
|
|
import net.minecraft.util.palette.PalettedContainer;
|
2018-08-04 23:36:59 -05:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2019-03-08 14:30:43 -06:00
|
|
|
import net.minecraft.world.chunk.ChunkSection;
|
2018-08-04 23:36:59 -05:00
|
|
|
|
2018-08-23 12:54:12 -07:00
|
|
|
import java.util.*;
|
2018-08-04 23:36:59 -05:00
|
|
|
|
2019-06-10 18:25:20 -07:00
|
|
|
import static baritone.utils.BlockStateInterface.getFromChunk;
|
|
|
|
|
|
2018-08-04 23:36:59 -05:00
|
|
|
/**
|
|
|
|
|
* @author Brady
|
2018-11-13 21:53:27 -06:00
|
|
|
* @since 8/3/2018
|
2018-08-04 23:36:59 -05:00
|
|
|
*/
|
2018-11-13 16:33:45 -06:00
|
|
|
public final class ChunkPacker {
|
2018-08-04 23:36:59 -05:00
|
|
|
|
|
|
|
|
private ChunkPacker() {}
|
|
|
|
|
|
2018-08-23 12:54:12 -07:00
|
|
|
public static CachedChunk pack(Chunk chunk) {
|
2018-09-16 17:15:33 -07:00
|
|
|
//long start = System.nanoTime() / 1000000L;
|
2018-08-23 12:54:12 -07:00
|
|
|
|
|
|
|
|
Map<String, List<BlockPos>> specialBlocks = new HashMap<>();
|
2018-08-04 23:36:59 -05:00
|
|
|
BitSet bitSet = new BitSet(CachedChunk.SIZE);
|
|
|
|
|
try {
|
2019-03-08 14:30:43 -06:00
|
|
|
ChunkSection[] chunkInternalStorageArray = chunk.getSections();
|
2018-09-04 15:18:51 -07:00
|
|
|
for (int y0 = 0; y0 < 16; y0++) {
|
2019-03-08 14:30:43 -06:00
|
|
|
ChunkSection extendedblockstorage = chunkInternalStorageArray[y0];
|
2018-09-04 15:18:51 -07:00
|
|
|
if (extendedblockstorage == null) {
|
|
|
|
|
// any 16x16x16 area that's all air will have null storage
|
|
|
|
|
// for example, in an ocean biome, with air from y=64 to y=256
|
|
|
|
|
// the first 4 extended blocks storages will be full
|
|
|
|
|
// and the remaining 12 will be null
|
|
|
|
|
|
|
|
|
|
// since the index into the bitset is calculated from the x y and z
|
|
|
|
|
// and doesn't function as an append, we can entirely skip the scanning
|
|
|
|
|
// since a bitset is initialized to all zero, and air is saved as zeros
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-01-09 17:15:05 -08:00
|
|
|
PalettedContainer<BlockState> bsc = extendedblockstorage.getData();
|
2018-09-04 15:18:51 -07:00
|
|
|
int yReal = y0 << 4;
|
2018-09-04 15:23:54 -07:00
|
|
|
// the mapping of BlockStateContainer.getIndex from xyz to index is y << 8 | z << 4 | x;
|
|
|
|
|
// for better cache locality, iterate in that order
|
|
|
|
|
for (int y1 = 0; y1 < 16; y1++) {
|
|
|
|
|
int y = y1 | yReal;
|
|
|
|
|
for (int z = 0; z < 16; z++) {
|
|
|
|
|
for (int x = 0; x < 16; x++) {
|
2018-09-04 15:18:51 -07:00
|
|
|
int index = CachedChunk.getPositionIndex(x, y, z);
|
2019-06-10 12:43:02 -07:00
|
|
|
BlockState state = bsc.get(x, y1, z);
|
2019-02-25 20:35:06 -08:00
|
|
|
boolean[] bits = getPathingBlockType(state, chunk, x, y, z).getBits();
|
2018-09-04 15:18:51 -07:00
|
|
|
bitSet.set(index, bits[0]);
|
|
|
|
|
bitSet.set(index + 1, bits[1]);
|
|
|
|
|
Block block = state.getBlock();
|
|
|
|
|
if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(block)) {
|
2019-04-17 18:10:47 -07:00
|
|
|
String name = BlockUtils.blockToString(block);
|
2018-09-04 15:18:51 -07:00
|
|
|
specialBlocks.computeIfAbsent(name, b -> new ArrayList<>()).add(new BlockPos(x, y, z));
|
|
|
|
|
}
|
2018-08-23 12:54:12 -07:00
|
|
|
}
|
2018-08-04 23:36:59 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2018-09-16 17:15:33 -07:00
|
|
|
//long end = System.nanoTime() / 1000000L;
|
2018-08-21 15:18:35 -07:00
|
|
|
//System.out.println("Chunk packing took " + (end - start) + "ms for " + chunk.x + "," + chunk.z);
|
2019-06-10 12:43:02 -07:00
|
|
|
BlockState[] blocks = new BlockState[256];
|
2018-09-24 15:45:12 -07:00
|
|
|
|
2019-09-19 17:36:06 -04:00
|
|
|
// @formatter:off
|
2018-08-22 11:41:31 -07:00
|
|
|
for (int z = 0; z < 16; z++) {
|
2019-03-13 19:23:28 -07:00
|
|
|
https://www.ibm.com/developerworks/library/j-perry-writing-good-java-code/index.html
|
2018-08-22 11:41:31 -07:00
|
|
|
for (int x = 0; x < 16; x++) {
|
2018-08-28 13:58:36 -07:00
|
|
|
for (int y = 255; y >= 0; y--) {
|
|
|
|
|
int index = CachedChunk.getPositionIndex(x, y, z);
|
2018-08-28 14:15:20 -07:00
|
|
|
if (bitSet.get(index) || bitSet.get(index + 1)) {
|
2019-06-10 18:25:20 -07:00
|
|
|
blocks[z << 4 | x] = getFromChunk(chunk, x, y, z);
|
2018-09-16 16:46:41 -05:00
|
|
|
continue https;
|
2018-08-22 10:03:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-09-24 15:45:12 -07:00
|
|
|
blocks[z << 4 | x] = Blocks.AIR.getDefaultState();
|
2018-08-22 10:03:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-19 17:36:06 -04:00
|
|
|
// @formatter:on
|
2019-06-10 18:25:20 -07:00
|
|
|
return new CachedChunk(chunk.getPos().x, chunk.getPos().z, bitSet, blocks, specialBlocks, System.currentTimeMillis());
|
2018-08-23 12:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-10 12:43:02 -07:00
|
|
|
private static PathingBlockType getPathingBlockType(BlockState state, Chunk chunk, int x, int y, int z) {
|
2018-09-03 09:15:18 -07:00
|
|
|
Block block = state.getBlock();
|
2019-03-08 14:30:43 -06:00
|
|
|
if (MovementHelper.isWater(state)) {
|
2018-08-28 12:23:54 -07:00
|
|
|
// only water source blocks are plausibly usable, flowing water should be avoid
|
2019-02-04 13:07:16 -08:00
|
|
|
// FLOWING_WATER is a waterfall, it doesn't really matter and caching it as AVOID just makes it look wrong
|
2019-03-04 19:44:36 -08:00
|
|
|
if (MovementHelper.possiblyFlowing(state)) {
|
|
|
|
|
return PathingBlockType.AVOID;
|
|
|
|
|
}
|
2019-04-17 20:02:02 -07:00
|
|
|
if (
|
2019-06-10 18:25:20 -07:00
|
|
|
(x != 15 && MovementHelper.possiblyFlowing(getFromChunk(chunk, x + 1, y, z)))
|
|
|
|
|
|| (x != 0 && MovementHelper.possiblyFlowing(getFromChunk(chunk, x - 1, y, z)))
|
|
|
|
|
|| (z != 15 && MovementHelper.possiblyFlowing(getFromChunk(chunk, x, y, z + 1)))
|
|
|
|
|
|| (z != 0 && MovementHelper.possiblyFlowing(getFromChunk(chunk, x, y, z - 1)))
|
2019-04-17 20:02:02 -07:00
|
|
|
) {
|
|
|
|
|
return PathingBlockType.AVOID;
|
|
|
|
|
}
|
2019-03-04 19:44:36 -08:00
|
|
|
if (x == 0 || x == 15 || z == 0 || z == 15) {
|
2019-06-10 18:25:20 -07:00
|
|
|
Vec3d flow = state.getFluidState().getFlow(chunk.getWorld(), new BlockPos(x + chunk.getPos().x << 4, y, z + chunk.getPos().z << 4));
|
2019-03-08 14:44:49 -06:00
|
|
|
if (flow.x != 0.0 || flow.z != 0.0) {
|
2019-03-04 19:44:36 -08:00
|
|
|
return PathingBlockType.WATER;
|
|
|
|
|
}
|
|
|
|
|
return PathingBlockType.AVOID;
|
2019-02-25 20:35:06 -08:00
|
|
|
}
|
2018-08-04 23:36:59 -05:00
|
|
|
return PathingBlockType.WATER;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 14:30:43 -06:00
|
|
|
if (MovementHelper.avoidWalkingInto(state) || MovementHelper.isBottomSlab(state)) {
|
2018-08-04 23:36:59 -05:00
|
|
|
return PathingBlockType.AVOID;
|
|
|
|
|
}
|
2018-08-22 11:41:31 -07:00
|
|
|
// We used to do an AABB check here
|
|
|
|
|
// however, this failed in the nether when you were near a nether fortress
|
|
|
|
|
// because fences check their adjacent blocks in the world for their fence connection status to determine AABB shape
|
|
|
|
|
// this caused a nullpointerexception when we saved chunks on unload, because they were unable to check their neighbors
|
2019-06-10 18:25:20 -07:00
|
|
|
if (block instanceof AirBlock || block instanceof TallGrassBlock || block instanceof DoublePlantBlock || block instanceof FlowerBlock) {
|
2018-08-04 23:36:59 -05:00
|
|
|
return PathingBlockType.AIR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PathingBlockType.SOLID;
|
|
|
|
|
}
|
2018-08-22 12:04:44 -07:00
|
|
|
|
2019-06-10 12:43:02 -07:00
|
|
|
public static BlockState pathingTypeToBlock(PathingBlockType type, int dimension) {
|
2018-09-16 20:14:51 -05:00
|
|
|
switch (type) {
|
|
|
|
|
case AIR:
|
|
|
|
|
return Blocks.AIR.getDefaultState();
|
|
|
|
|
case WATER:
|
|
|
|
|
return Blocks.WATER.getDefaultState();
|
|
|
|
|
case AVOID:
|
|
|
|
|
return Blocks.LAVA.getDefaultState();
|
|
|
|
|
case SOLID:
|
|
|
|
|
// Dimension solid types
|
2018-11-09 19:24:02 -08:00
|
|
|
switch (dimension) {
|
2018-09-16 20:14:51 -05:00
|
|
|
case -1:
|
|
|
|
|
return Blocks.NETHERRACK.getDefaultState();
|
|
|
|
|
case 0:
|
2018-09-16 19:43:48 -07:00
|
|
|
default: // The fallback solid type
|
2018-09-16 20:14:51 -05:00
|
|
|
return Blocks.STONE.getDefaultState();
|
|
|
|
|
case 1:
|
|
|
|
|
return Blocks.END_STONE.getDefaultState();
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
2018-08-22 12:04:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-04 23:36:59 -05:00
|
|
|
}
|