Skip to content

Commit

Permalink
Rewrite getTintedBlockColor to try to implement the same algorithm mo…
Browse files Browse the repository at this point in the history
…re efficiently
  • Loading branch information
magicus committed Nov 14, 2021
1 parent 3de0816 commit 20f1d62
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraft.block.MapColor;
import net.minecraft.block.Material;
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.state.property.Properties;
import net.minecraft.tag.FluidTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
Expand Down Expand Up @@ -145,7 +144,7 @@ private void updateDerivedData(World world, WorldChunk worldChunk, int xOffset,
derivedDeltaY[xOffset][zOffset] = (byte) my_delta;
}

private int getJustMapPixelColor(int xOffset, int zOffset) {
private int getJustMapPixelColor(int xOffset, int zOffset, BlockPos.Mutable blockPos) {
int solidY = scoutedSolidY[xOffset][zOffset];
int transparentY = scoutedTransparentY[xOffset][zOffset];
int waterY = scoutedWaterY[xOffset][zOffset];
Expand All @@ -154,15 +153,13 @@ private int getJustMapPixelColor(int xOffset, int zOffset) {
BlockState solidBlock = Block.getStateFromRawId(scoutedSolidBlocks[xOffset][zOffset]);
BlockState transparentBlock = Block.getStateFromRawId(scoutedTransparentBlocks[xOffset][zOffset]);

// FIXME: it is not corrrect to have "overBlock" as transparent block, but use it for now
BlockPos pos = new BlockPos(chunkPos.getStartX() + xOffset, solidY, chunkPos.getStartZ() + zOffset);
blockPos.setY(solidY);
// Get basic pixel color
int color = getTintedBlockColor(FastMapManager.MANAGER.getFastWorldMapper().getWorld(), pos, solidBlock, transparentBlock);
int color = getTintedBlockColor(FastMapManager.MANAGER.getFastWorldMapper().getWorld(), blockPos, transparentY, waterY, solidBlock, transparentBlock);
if (color != -1) {
// Topology processing
int heightDiff = deltaY;
float topoLevel = getTopoLevel(solidY);
color = ColorUtil.processColor(color, heightDiff, topoLevel);
color = ColorUtil.processColor(color, deltaY, topoLevel);
if (ClientSettings.showTopography) {
return MathUtil.isEven(solidY) ? color : ColorUtil.colorBrigtness(color, -0.6F);
}
Expand Down Expand Up @@ -190,43 +187,38 @@ private float getTopoLevel(int solidY) {
return topoLevel;
}

private static int getTintedBlockColor(World world, BlockPos pos, BlockState blockState, BlockState overState) {
if (BlockStateUtil.isAir(blockState)) {
return -1;
private static int getTintedBlockColor(World world, BlockPos.Mutable pos, int transparentY, int waterY, BlockState blockState, BlockState transparentBlockState) {
int topY = pos.getY();
if (!ClientSettings.hidePlants && transparentY > topY && isPlant(transparentBlockState)) {
// We'll use the plant block pointed to by "transparent" instead
topY = transparentY;
pos.setY(topY);
blockState = transparentBlockState;
}
Material overStateMaterial = overState.getMaterial();
if (!ClientSettings.hideWater && ClientSettings.hidePlants && isSeaweed(overState)) {
// We are showing water but not plants, and we have seaweed above us

if (topY <= waterY && !ClientSettings.hideWater) {
if (ClientSettings.waterTint) {
int color = ColorUtil.getBlockColorInner(world, blockState, pos);
return ColorUtil.applyTint(color, BiomeColors.getWaterColor(world, pos));
} else {
return ColorUtil.getBlockColorInner(world, Blocks.WATER.getDefaultState(), pos);
}
} else if (BlockStateUtil.isAir(overState)
|| ((ClientSettings.hideWater || ClientSettings.waterTint) && overStateMaterial.isLiquid() && overStateMaterial != Material.LAVA)
|| (ClientSettings.hidePlants && (overStateMaterial == Material.PLANT || overStateMaterial == Material.REPLACEABLE_PLANT ||
isSeaweed(overState)))) {
int color = ColorUtil.getBlockColorInner(world, blockState, pos);
if (ClientSettings.hideWater) {
return color;
} else {
if (topY >= 0) {
return ColorUtil.getBlockColorInner(world, blockState, pos);
} else {
if (ClientSettings.waterTint &&
(!isSeaweed(overState) && overState.getFluidState().isIn(FluidTags.WATER) ||
(blockState.contains(Properties.WATERLOGGED) && blockState.get(Properties.WATERLOGGED)) || isSeaweed(blockState))) {
return ColorUtil.applyTint(color, BiomeColors.getWaterColor(world, pos));
} else {
return color;
}
// Hole to the void
return -1;
}
} else {
return -1;
}
}

public static boolean isSeaweed(BlockState state) {
public static boolean isPlant(BlockState state) {
Material material = state.getMaterial();
return material == Material.UNDERWATER_PLANT || material == Material.REPLACEABLE_UNDERWATER_PLANT;
return material == Material.PLANT
|| material == Material.REPLACEABLE_PLANT
|| material == Material.UNDERWATER_PLANT
|| material == Material.REPLACEABLE_UNDERWATER_PLANT;
}


Expand Down Expand Up @@ -283,8 +275,8 @@ private int getVanillaPixelColor(int xOffset, int zOffset) {
}
}

private void updatePixelColor(int x, int z) {
int color = getJustMapPixelColor(x, z);
private void updatePixelColor(int x, int z, BlockPos.Mutable blockPos) {
int color = getJustMapPixelColor(x, z, blockPos);

int xOffset = x * 4;
pixelColors[z][xOffset + 0] = (byte) 0;
Expand All @@ -306,9 +298,11 @@ public void onChunkUpdate(WorldChunk worldChunk) {
}

for (int x = 0; x < MapRegionLayer.CHUNK_SIZE; x++) {
blockPos.setX(worldChunk.getPos().getStartX() + x);
for (int z = 0; z < MapRegionLayer.CHUNK_SIZE; z++) {
blockPos.setZ(worldChunk.getPos().getStartZ() + z);
updateDerivedData(world, worldChunk, x, z);
updatePixelColor(x, z);
updatePixelColor(x, z, blockPos);
}
}
}
Expand All @@ -326,7 +320,7 @@ public void onBlockUpdate(BlockPos blockPos) {
updateScoutedData(world, worldChunk, pos);
updateDerivedData(world, worldChunk, x, z);
// FIXME: We also need to calculate delta on it's neighbor!
updatePixelColor(x, z);
updatePixelColor(x, z, pos);
}
}

Expand Down

0 comments on commit 20f1d62

Please sign in to comment.