Skip to content

Commit

Permalink
Calculate and store delta. Simplify updateBlock signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
magicus committed Nov 14, 2021
1 parent b43e6a0 commit 3e44ba5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
67 changes: 48 additions & 19 deletions src/main/java/ru/bulldog/justmap/map/data/fast/MapChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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]);
Expand All @@ -116,26 +135,23 @@ 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) {
shade = 0;
} 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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3e44ba5

Please sign in to comment.