From 3e44ba563a6dd3ed8d9bbfbde462d13dc993aecf Mon Sep 17 00:00:00 2001 From: Magnus Ihse Bursie Date: Sun, 14 Nov 2021 01:26:35 +0100 Subject: [PATCH] Calculate and store delta. Simplify updateBlock signature. --- .../map/data/fast/DrawableMapRegion.java | 5 +- .../justmap/map/data/fast/FastMapManager.java | 2 +- .../map/data/fast/FastWorldMapper.java | 5 +- .../justmap/map/data/fast/MapChunk.java | 67 +++++++++++++------ .../justmap/map/data/fast/MapRegionLayer.java | 5 +- 5 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/main/java/ru/bulldog/justmap/map/data/fast/DrawableMapRegion.java b/src/main/java/ru/bulldog/justmap/map/data/fast/DrawableMapRegion.java index 363f40f4..0fb764b9 100644 --- a/src/main/java/ru/bulldog/justmap/map/data/fast/DrawableMapRegion.java +++ b/src/main/java/ru/bulldog/justmap/map/data/fast/DrawableMapRegion.java @@ -1,6 +1,5 @@ package ru.bulldog.justmap.map.data.fast; -import net.minecraft.block.BlockState; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.math.BlockPos; import net.minecraft.world.chunk.WorldChunk; @@ -83,8 +82,8 @@ public void updateChunk(WorldChunk worldChunk) { // FIXME: only update nether if actually in the nether, how to check? } - public void updateBlock(BlockPos pos, BlockState state) { + public void updateBlock(BlockPos pos) { // FIXME: should probably check if the block is on the surface here - surfaceLayer.updateBlock(pos, state); + surfaceLayer.updateBlock(pos); } } diff --git a/src/main/java/ru/bulldog/justmap/map/data/fast/FastMapManager.java b/src/main/java/ru/bulldog/justmap/map/data/fast/FastMapManager.java index ff768166..a654febb 100644 --- a/src/main/java/ru/bulldog/justmap/map/data/fast/FastMapManager.java +++ b/src/main/java/ru/bulldog/justmap/map/data/fast/FastMapManager.java @@ -34,7 +34,7 @@ public void onChunkLoad(World world, WorldChunk worldChunk) { @Override public void onSetBlockState(BlockPos pos, BlockState state, World world) { assert(world == getFastWorldMapper().getWorld()); - getFastWorldMapper().updateBlock(pos, state); + getFastWorldMapper().updateBlock(pos); } @Override diff --git a/src/main/java/ru/bulldog/justmap/map/data/fast/FastWorldMapper.java b/src/main/java/ru/bulldog/justmap/map/data/fast/FastWorldMapper.java index b196c32e..48f46eb2 100644 --- a/src/main/java/ru/bulldog/justmap/map/data/fast/FastWorldMapper.java +++ b/src/main/java/ru/bulldog/justmap/map/data/fast/FastWorldMapper.java @@ -3,7 +3,6 @@ import java.util.HashMap; import java.util.Map; -import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.chunk.WorldChunk; @@ -59,9 +58,9 @@ public void updateChunk(WorldChunk worldChunk) { region.updateChunk(worldChunk); } - public void updateBlock(BlockPos pos, BlockState state) { + public void updateBlock(BlockPos pos) { RegionPos regionPos = new RegionPos(pos.getX(), pos.getZ()); DrawableMapRegion region = getOrCreateRegion(regionPos); - region.updateBlock(pos, state); + region.updateBlock(pos); } } diff --git a/src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java b/src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java index 0fa69835..d8603763 100644 --- a/src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java +++ b/src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java @@ -29,6 +29,7 @@ public class MapChunk { private final byte[][] solidY = new byte[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; private final byte[][] transparentY = new byte[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; private final byte[][] waterY = new byte[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; + private final byte[][] delta = new byte[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; private final int[][] solidBlock = new int[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; private final int[][] transparentBlock = new int[MapRegionLayer.CHUNK_SIZE][MapRegionLayer.CHUNK_SIZE]; @@ -88,12 +89,30 @@ private void examinePos(World world, WorldChunk worldChunk, BlockPos.Mutable thi transparentBlock[xOffset][zOffset] = Block.getRawIdFromState(transparent_block); } - private int blockColorChunkFromCache(World world, WorldChunk worldChunk, BlockPos pos) { - int xOffset = pos.getX() & 15; - int zOffset = pos.getZ() & 15; + private void calculateDelta(World world, WorldChunk worldChunk, int xOffset, int zOffset) { + int solid_y = solidY[xOffset][zOffset]; + + // For terrain, Vanilla calculate shading according to height difference + // with north (z-1) neighbor + + // FIXME: this breaks at chunk border! + int north_solid_y; + if (zOffset > 0) { + north_solid_y = solidY[xOffset][zOffset - 1]; + } else { + // FIXME: fake! use our own height + north_solid_y = solidY[xOffset][zOffset]; + } + + int my_delta = solid_y - north_solid_y; + delta[xOffset][zOffset] = (byte) my_delta; + } + + private int blockColorChunkFromCache(int xOffset, int zOffset) { int solid_y = solidY[xOffset][zOffset]; int transparent_y = transparentY[xOffset][zOffset]; int water_y = waterY[xOffset][zOffset]; + int y_delta = delta[xOffset][zOffset]; BlockState solid_block = Block.getStateFromRawId(solidBlock[xOffset][zOffset]); BlockState transparent_block = Block.getStateFromRawId(transparentBlock[xOffset][zOffset]); @@ -116,18 +135,8 @@ private int blockColorChunkFromCache(World world, WorldChunk worldChunk, BlockPo mapColor = MapColor.WATER_BLUE; } else { // For terrain, calculate shading according to height difference - // with north (z-1) neighbor - // FIXME: this breaks at chunk border! - int north_solid_y; - if (zOffset > 0) { - north_solid_y = solidY[xOffset][zOffset - 1]; - } else { - // FIXME: fake! use our own height - north_solid_y = solidY[xOffset][zOffset]; - } - - double shadeArg = (solid_y - north_solid_y) * 4/5.0d - + ((xOffset + zOffset & 1) - 0.5d) * 0.4d; + // with neighbor + double shadeArg = y_delta * 4/5.0d + ((xOffset + zOffset & 1) - 0.5d) * 0.4d; if (shadeArg > 0.6d) { shade = 2; } else if (shadeArg < -0.6d) { @@ -135,7 +144,14 @@ private int blockColorChunkFromCache(World world, WorldChunk worldChunk, BlockPo } else { shade = 1; } - mapColor = solid_block.getMapColor(world, pos); + try { + // We're taking a bit of a chance here. In practice, the + // implementation of getMapColor() ignores its arguments, but + // that might change in the future + mapColor = solid_block.getMapColor(null, null); + } catch (NullPointerException e) { + mapColor = MapColor.CLEAR; + } } if (mapColor == MapColor.CLEAR) { @@ -196,24 +212,37 @@ public void updateChunk(WorldChunk worldChunk) { for (int z = 0; z < MapRegionLayer.CHUNK_SIZE; z++) { blockPos.setZ(worldChunk.getPos().getStartZ() + z); examinePos(world, worldChunk, blockPos); + } + } + + for (int x = 0; x < MapRegionLayer.CHUNK_SIZE; x++) { + for (int z = 0; z < MapRegionLayer.CHUNK_SIZE; z++) { + calculateDelta(world, worldChunk, x, z); + } + } - int color = blockColorChunkFromCache(world, worldChunk, blockPos); + for (int x = 0; x < MapRegionLayer.CHUNK_SIZE; x++) { + for (int z = 0; z < MapRegionLayer.CHUNK_SIZE; z++) { + int color = blockColorChunkFromCache(x, z); setColor(x, z, color); } } } - public void updateBlock(BlockPos blockPos, BlockState blockState) { + public void updateBlock(BlockPos blockPos) { int x = blockPos.getX() & 15; int z = blockPos.getZ() & 15; int relevantY = solidY[x][z]; if (blockPos.getY() >= relevantY) { + // Only look at changes to blocks that could possibly affect the map BlockPos.Mutable pos = new BlockPos.Mutable(); pos.set(blockPos); World world = FastMapManager.MANAGER.getFastWorldMapper().getWorld(); WorldChunk worldChunk = world.getWorldChunk(pos); examinePos(world, worldChunk, pos); - int color = blockColorChunkFromCache(world, worldChunk, blockPos); + calculateDelta(world, worldChunk, x, z); + // FIXME: We also need to calculate delta on it's neighbor! + int color = blockColorChunkFromCache(x, z); setColor(x, z, color); } } diff --git a/src/main/java/ru/bulldog/justmap/map/data/fast/MapRegionLayer.java b/src/main/java/ru/bulldog/justmap/map/data/fast/MapRegionLayer.java index 83fd1945..90fb6c73 100644 --- a/src/main/java/ru/bulldog/justmap/map/data/fast/MapRegionLayer.java +++ b/src/main/java/ru/bulldog/justmap/map/data/fast/MapRegionLayer.java @@ -5,7 +5,6 @@ import com.mojang.blaze3d.platform.TextureUtil; import com.mojang.blaze3d.systems.RenderSystem; -import net.minecraft.block.BlockState; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; @@ -47,11 +46,11 @@ public void updateChunk(WorldChunk worldChunk) { isModified = true; } - public void updateBlock(BlockPos pos, BlockState state) { + public void updateBlock(BlockPos pos) { ChunkPos chunkPos = new ChunkPos(pos); MapChunk mapChunk = getMapChunk(chunkPos); - mapChunk.updateBlock(pos, state); + mapChunk.updateBlock(pos); mapChunk.writeToTextureBuffer(buffer); isModified = true; }