From 94e02d677ceed3e0e1a0932ae8942b7ce475e152 Mon Sep 17 00:00:00 2001 From: olebeck <31539311+olebeck@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:11:40 +0200 Subject: [PATCH] checkerboard chunk render --- subcommands/world/chunk.go | 58 +++++++++++++----------- subcommands/world/chunk_render.go | 74 +++++++++++++++---------------- subcommands/world/entity.go | 13 +++--- subcommands/world/world.go | 19 ++++---- 4 files changed, 82 insertions(+), 82 deletions(-) diff --git a/subcommands/world/chunk.go b/subcommands/world/chunk.go index 536c697..ed027a6 100644 --- a/subcommands/world/chunk.go +++ b/subcommands/world/chunk.go @@ -135,40 +135,46 @@ func (w *WorldState) ProcessChunkPackets(pk packet.Packet) packet.Packet { case *packet.SubChunk: w.processSubChunk(pk) case *packet.BlockActorData: - sp := protocol.SubChunkPos{pk.Position.X() << 4, 0, pk.Position.Z() << 4} - b, ok := w.blockNBT[sp] - if !ok { - w.blockNBT[sp] = []map[string]any{pk.NBTData} - } else { - for i, v := range b { - x, y, z := v["x"].(int32), v["y"].(int32), v["z"].(int32) - if x == pk.Position.X() && y == pk.Position.Y() && z == pk.Position.Z() { - b[i] = pk.NBTData - break + if w.blockUpdates { + sp := protocol.SubChunkPos{pk.Position.X() << 4, 0, pk.Position.Z() << 4} + b, ok := w.blockNBT[sp] + if !ok { + w.blockNBT[sp] = []map[string]any{pk.NBTData} + } else { + for i, v := range b { + x, y, z := v["x"].(int32), v["y"].(int32), v["z"].(int32) + if x == pk.Position.X() && y == pk.Position.Y() && z == pk.Position.Z() { + b[i] = pk.NBTData + break + } } } } case *packet.UpdateBlock: - cp := protocol.ChunkPos{pk.Position.X() >> 4, pk.Position.Z() >> 4} - c, ok := w.chunks[cp] - if ok { - x, y, z := blockPosInChunk(pk.Position) - c.SetBlock(x, y, z, uint8(pk.Layer), pk.NewBlockRuntimeID) - w.mapUI.SetChunk(cp, w.chunks[cp], true) + if w.blockUpdates { + cp := protocol.ChunkPos{pk.Position.X() >> 4, pk.Position.Z() >> 4} + c, ok := w.chunks[cp] + if ok { + x, y, z := blockPosInChunk(pk.Position) + c.SetBlock(x, y, z, uint8(pk.Layer), pk.NewBlockRuntimeID) + w.mapUI.SetChunk(cp, w.chunks[cp], true) + } } case *packet.UpdateSubChunkBlocks: - cp := protocol.ChunkPos{pk.Position.X(), pk.Position.Z()} - c, ok := w.chunks[cp] - if ok { - for _, bce := range pk.Blocks { - x, y, z := blockPosInChunk(bce.BlockPos) - if bce.SyncedUpdateType == packet.EntityToBlockTransition { - c.SetBlock(x, y, z, 0, world.AirRID()) - } else { - c.SetBlock(x, y, z, 0, bce.BlockRuntimeID) + if w.blockUpdates { + cp := protocol.ChunkPos{pk.Position.X(), pk.Position.Z()} + c, ok := w.chunks[cp] + if ok { + for _, bce := range pk.Blocks { + x, y, z := blockPosInChunk(bce.BlockPos) + if bce.SyncedUpdateType == packet.BlockToEntityTransition { + c.SetBlock(x, y, z, 0, world.AirRID()) + } else { + c.SetBlock(x, y, z, 0, bce.BlockRuntimeID) + } } + w.mapUI.SetChunk(cp, w.chunks[cp], true) } - w.mapUI.SetChunk(cp, w.chunks[cp], true) } } return pk diff --git a/subcommands/world/chunk_render.go b/subcommands/world/chunk_render.go index c68e23e..28fced8 100644 --- a/subcommands/world/chunk_render.go +++ b/subcommands/world/chunk_render.go @@ -22,46 +22,44 @@ func blockColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) (blockColor color.R if y <= int16(c.Range().Min()) { return color.RGBA{0, 0, 0, 0} } - blockColor = color.RGBA{255, 0, 255, 255} rid := c.Block(x, y, z, 0) if rid == 0 && y == int16(c.Range().Min()) { // void - blockColor = color.RGBA{0, 0, 0, 255} - } else { - b, found := world.BlockByRuntimeID(rid) - if found { - if _, isWater := b.(block.Water); isWater { - // get the first non water block at the position - heightBlock := c.HeightMap().At(x, z) - depth := y - heightBlock - if depth > 0 { - blockColor = blockColorAt(c, x, heightBlock, z) - } - - // blend that blocks color with water depending on depth - waterColor := (&block.Water{}).Color() - waterColor.A = uint8(utils.Clamp(int(150+depth*7), 255)) - blockColor = utils.BlendColors(blockColor, waterColor) - blockColor.R -= uint8(depth * 2) - blockColor.G -= uint8(depth * 2) - blockColor.B -= uint8(depth * 2) - return blockColor - } else { - col := b.Color() - if col.A != 255 { - col = utils.BlendColors(blockColorAt(c, x, y-1, z), col) - } - return col - } - } - /* - if blockColor.R == 0 || blockColor.R == 255 && blockColor.B == 255 { - name, nbt := b.EncodeBlock() - fmt.Printf("unknown color %d %s %s %s\n", rid, reflect.TypeOf(b), name, nbt) - b.Color() - } - */ + return color.RGBA{0, 0, 0, 255} + } + + blockColor = color.RGBA{255, 0, 255, 255} + b, found := world.BlockByRuntimeID(rid) + if found { + if _, isWater := b.(block.Water); isWater { + // get the first non water block at the position + heightBlock := c.HeightMap().At(x, z) + depth := y - heightBlock + if depth > 0 { + blockColor = blockColorAt(c, x, heightBlock, z) + } + + // blend that blocks color with water depending on depth + waterColor := (&block.Water{}).Color() + waterColor.A = uint8(utils.Clamp(int(150+depth*7), 255)) + blockColor = utils.BlendColors(blockColor, waterColor) + blockColor.R -= uint8(depth * 2) + blockColor.G -= uint8(depth * 2) + blockColor.B -= uint8(depth * 2) + return blockColor + } else { + col := b.Color() + if col.A != 255 { + col = utils.BlendColors(blockColorAt(c, x, y-1, z), col) + } + return col + } + } else { + /* + name, nbt := b.EncodeBlock() + fmt.Printf("unknown color %d %s %s %s\n", rid, reflect.TypeOf(b), name, nbt) + */ + return blockColor } - return blockColor } func chunkGetColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) color.RGBA { @@ -84,7 +82,7 @@ func chunkGetColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) color.RGBA { }, cube.Range{int(y + 1), int(y + 1)}) blockColor := blockColorAt(c, x, y, z) - if haveUp { + if haveUp && (x+z)%2 == 0 { if blockColor.R > 10 { blockColor.R -= 10 } diff --git a/subcommands/world/entity.go b/subcommands/world/entity.go index 4dc9ba3..c7fb442 100644 --- a/subcommands/world/entity.go +++ b/subcommands/world/entity.go @@ -201,14 +201,13 @@ func (s *entityState) ToServerEntity() serverEntity { entityMetadataToNBT(s.Metadata, e.EntityType.NBT) if s.Helmet != nil || s.Chestplate != nil || s.Leggings != nil || s.Boots != nil { - armor := make([]map[string]any, 0, 4) - armor = append(armor, nbtconv.WriteItem(stackToItem(s.Helmet.Stack), true)) - armor = append(armor, nbtconv.WriteItem(stackToItem(s.Chestplate.Stack), true)) - armor = append(armor, nbtconv.WriteItem(stackToItem(s.Leggings.Stack), true)) - armor = append(armor, nbtconv.WriteItem(stackToItem(s.Boots.Stack), true)) - e.EntityType.NBT["Armor"] = armor + e.EntityType.NBT["Armor"] = []map[string]any{ + nbtconv.WriteItem(stackToItem(s.Helmet.Stack), true), + nbtconv.WriteItem(stackToItem(s.Chestplate.Stack), true), + nbtconv.WriteItem(stackToItem(s.Leggings.Stack), true), + nbtconv.WriteItem(stackToItem(s.Boots.Stack), true), + } } - return e } diff --git a/subcommands/world/world.go b/subcommands/world/world.go index 8531805..824e189 100644 --- a/subcommands/world/world.go +++ b/subcommands/world/world.go @@ -67,6 +67,7 @@ type WorldState struct { withPacks bool saveImage bool experimentInventory bool + blockUpdates bool } func NewWorldState(ctx context.Context, proxy *utils.ProxyContext, ServerName string, ui utils.UI) *WorldState { @@ -596,17 +597,13 @@ func (w *WorldState) OnConnect(err error) bool { Cmd: protocol.Command{ Name: "setname", Description: locale.Loc("setname_desc", nil), - Overloads: []protocol.CommandOverload{ - { - Parameters: []protocol.CommandParameter{ - { - Name: "name", - Type: protocol.CommandArgTypeString, - Optional: false, - }, - }, - }, - }, + Overloads: []protocol.CommandOverload{{ + Parameters: []protocol.CommandParameter{{ + Name: "name", + Type: protocol.CommandArgTypeString, + Optional: false, + }}, + }}, }, })