From 0132dd4a202bb913df4556ef75003707f324761b Mon Sep 17 00:00:00 2001 From: Howard Stark Date: Tue, 21 Aug 2018 21:11:34 -0700 Subject: [PATCH] Add water shading --- src/main/java/baritone/map/Map.java | 8 +++-- src/main/java/baritone/map/MapChunk.java | 42 ++++++++++++++++++++---- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/main/java/baritone/map/Map.java b/src/main/java/baritone/map/Map.java index e362dcf4..98e4fd91 100644 --- a/src/main/java/baritone/map/Map.java +++ b/src/main/java/baritone/map/Map.java @@ -17,10 +17,14 @@ public class Map extends Behavior { public static final Map INSTANCE = new Map(); - private Map() {} - private BufferedImage fullImage = new BufferedImage(4080, 4080, BufferedImage.TYPE_INT_RGB); + private Map() { + try { + fullImage = ImageIO.read(getImagePath().toFile()); + } catch(IOException ignored) { } + } + @Override public void onChunkEvent(ChunkEvent event) { if(event.getType() != ChunkEvent.Type.POPULATE) diff --git a/src/main/java/baritone/map/MapChunk.java b/src/main/java/baritone/map/MapChunk.java index 4f8ad3c0..a30673cd 100644 --- a/src/main/java/baritone/map/MapChunk.java +++ b/src/main/java/baritone/map/MapChunk.java @@ -3,7 +3,12 @@ package baritone.map; import baritone.bot.Baritone; import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.pathing.BetterBlockPos; +import net.minecraft.block.BlockDynamicLiquid; +import net.minecraft.block.BlockLiquid; +import net.minecraft.block.BlockStaticLiquid; import net.minecraft.block.material.MapColor; +import net.minecraft.block.state.IBlockState; +import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.Chunk; @@ -76,14 +81,19 @@ public class MapChunk { private int getColor(int x, int z) { int chunkX = chunk.getPos().getXStart() + x; int chunkZ = chunk.getPos().getZStart() + z; - BetterBlockPos blockPos = new BetterBlockPos(chunkX, chunk.getHeight(new BetterBlockPos(chunkX, 0, chunkZ)), chunkZ); - MapColor mapColor = BlockStateInterface.get(blockPos).getMapColor(chunk.getWorld(), blockPos); + BlockPos blockPos = new BetterBlockPos(chunkX, chunk.getHeight(new BetterBlockPos(chunkX, 0, chunkZ)), chunkZ); + IBlockState blockState = BlockStateInterface.get(blockPos); + MapColor mapColor = blockState.getMapColor(chunk.getWorld(), blockPos); // The chunk heightMap returns the first non-full block above the surface, including bushes. // We have to check this and shift our block down by one if the color is "air" (as in, there's no real block there) - Color color = new Color(mapColor != MapColor.AIR ? - mapColor.colorValue : - BlockStateInterface.get(blockPos.down()).getMapColor(chunk.getWorld(), blockPos).colorValue); + if(mapColor == MapColor.AIR) { + blockPos = blockPos.down(); + blockState = BlockStateInterface.get(blockPos); + mapColor = blockState.getMapColor(chunk.getWorld(), blockPos); + } + + Color color = new Color(mapColor.colorValue); int red = color.getRed(); int green = color.getGreen(); int blue = color.getBlue(); @@ -100,8 +110,26 @@ public class MapChunk { if(BlockStateInterface.get(offset).getMapColor(chunk.getWorld(), offset) == MapColor.AIR) offset = offset.down(); - // We start the heightCoef at 0.8 to darken the colors similar to vanilla Minecraft's. - float heightCoef = 0.8f + (Math.signum(blockPos.getY() - offset.getY()) * 0.2f); + float heightCoef; + // Check if we need to handle water depth shading + if(blockState.getMaterial().isLiquid()) { + heightCoef = 1.0f; + int i = 1; + // Iterate over the 4 layers of water shading and color proportional to the depth + for(; i < 5; i++) { + if(BlockStateInterface.get(blockPos.down(i * 2)).getMaterial().isLiquid()) { + heightCoef -= 0.075f; + } else { + break; + } + } + // Add checkerboard shading to the odd layers to give texture + if(i % 2 == 0) + heightCoef += 0.075f * (((x + z) % 2 == 0) ? -1 : 1); + } else { + // We start the heightCoef at 0.8 to darken the colors similar to vanilla Minecraft's. + heightCoef = 0.8f + (Math.signum(blockPos.getY() - offset.getY()) * 0.2f); + } red = Math.min(255, (int) (red * heightCoef)); green = Math.min(255, (int) (green * heightCoef)); blue = Math.min(255, (int) (blue * heightCoef));