Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle chunk data and block changes #13

Merged
merged 5 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/minecraft/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ module Minecraft::IO
read_byte != 0
end

def read_int32 : Int32
read_bytes Int32, ::IO::ByteFormat::BigEndian
end

def read_long : Int64
read_bytes Int64, ::IO::ByteFormat::BigEndian
end

def read_float : Float32
read_bytes Float32, ::IO::ByteFormat::BigEndian
end
Expand All @@ -71,6 +63,18 @@ module Minecraft::IO
read_bytes Float64, ::IO::ByteFormat::BigEndian
end

def read_short : Int16
read_bytes Int16, ::IO::ByteFormat::BigEndian
end

def read_int : Int32
read_bytes Int32, ::IO::ByteFormat::BigEndian
end

def read_long : Int64
read_bytes Int64, ::IO::ByteFormat::BigEndian
end

def read_var_int : UInt32
result = 0_u32
shift = 0
Expand All @@ -79,6 +83,19 @@ module Minecraft::IO
result |= ((0x7F & b).to_u32) << shift
return result if b & 0x80 == 0
shift += 7
raise "VarInt is too big: #{shift}" if shift >= 32
end
end

def read_var_long : UInt64
result = 0_u64
shift = 0
loop do
b = read_byte
result |= ((0x7F & b).to_u64) << shift
return result if b & 0x80 == 0
shift += 7
raise "VarLong is too big: #{shift}" if shift >= 64
end
end

Expand Down Expand Up @@ -108,6 +125,15 @@ module Minecraft::IO
def read_nbt : NBT::Tag
NBT::Reader.new(self).read_named[:tag]
end

def read_position : Tuple(Int32, Int32, Int32)
value = read_long
# here ordered LSB to MSB; use arithmetic shift to preserve sign
y = ((value << 52) >> 52).to_i32 # 12 bits
z = ((value << 26) >> 38).to_i32 # 26 bits
x = (value >> 38).to_i32 # 26 bits
{x, y, z}
end
end

class Minecraft::IO::Memory < IO::Memory
Expand Down
20 changes: 20 additions & 0 deletions src/rosegold/packets/clientbound/block_change.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Rosegold::Clientbound::BlockChange < Rosegold::Clientbound::Packet
property \
x : Int32,
y : Int32,
z : Int32,
block_state : UInt16

def initialize(@x, @y, @z, @block_state); end

def self.read(packet)
self.new(
*packet.read_position,
packet.read_var_int.to_u16
)
end

def callback(client)
client.dimension.set_block_state x, y, z, block_state
end
end
29 changes: 29 additions & 0 deletions src/rosegold/packets/clientbound/chunk_data.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Rosegold::Clientbound::ChunkData < Rosegold::Clientbound::Packet
property \
chunk_x : Int32,
chunk_z : Int32,
heightmaps : NBT::Tag,
data : Bytes

def initialize(@chunk_x, @chunk_z, @heightmaps, @data)
end

def self.read(packet)
self.new(
packet.read_int,
packet.read_int,
packet.read_nbt,
packet.read_var_bytes
)
end

def callback(client)
source = Minecraft::IO::Memory.new data
chunk = World::Chunk.new source
client.dimension.load_chunk ({chunk_x, chunk_z}), chunk
end

def inspect(io)
io << "#<Clientbound::ChunkData " << chunk_x << "," << chunk_z << ">"
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/join_game.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Rosegold::Clientbound::JoinGame < Rosegold::Clientbound::Packet

def self.read(packet)
self.new(
packet.read_int32,
packet.read_int,
packet.read_bool,
packet.read_byte
)
Expand Down
1 change: 0 additions & 1 deletion src/rosegold/packets/clientbound/keep_alive.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Rosegold::Clientbound::KeepAlive < Rosegold::Clientbound::Packet
end

def callback(client)
Log.debug { "rx <- KeepAlive: #{@keep_alive_id}" }
client.queue_packet Serverbound::KeepAlive.new keep_alive_id
end
end
41 changes: 41 additions & 0 deletions src/rosegold/packets/clientbound/multi_block_change.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Rosegold::Clientbound::MultiBlockChange < Rosegold::Clientbound::Packet
property \
section_x : Int32,
section_y : Int32,
section_z : Int32,
block_states : Array(Tuple(Int32, Int32, Int32, UInt16))

def initialize(@section_x, @section_y, @section_z, @block_states); end

def self.read(packet)
section_pos_bits = packet.read_long
# here ordered LSB to MSB; use arithmetic shift to preserve sign
section_y = (section_pos_bits << 44 >> 44).to_i32 # 20 bits
section_z = (section_pos_bits << 22 >> 42).to_i32 # 22 bits
section_x = (section_pos_bits >> 42).to_i32 # 22 bits

packet.read_bool # ignored

block_states = Array(Tuple(Int32, Int32, Int32, UInt16)).new packet.read_var_int do
long = packet.read_var_long
y = section_y * 16 + (long & 0xf).to_u8
z = section_z * 16 + ((long >> 4) & 0xf).to_u8
x = section_x * 16 + ((long >> 8) & 0xf).to_u8
block_state = (long >> 12).to_u16
{x, y, z, block_state}
end

self.new(
section_x,
section_y,
section_z,
block_states
)
end

def callback(client)
block_states.each do |x, y, z, block_state|
client.dimension.set_block_state x, y, z, block_state
end
end
end
2 changes: 1 addition & 1 deletion src/rosegold/packets/clientbound/ping.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Rosegold::Clientbound::Ping < Rosegold::Clientbound::Packet

def self.read(packet)
self.new(
packet.read_int32
packet.read_int
)
end

Expand Down
18 changes: 18 additions & 0 deletions src/rosegold/packets/clientbound/unload_chunk.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Rosegold::Clientbound::UnloadChunk < Rosegold::Clientbound::Packet
property \
chunk_x : Int32,
chunk_z : Int32

def initialize(@chunk_x, @chunk_z); end

def self.read(packet)
self.new(
packet.read_int,
packet.read_int
)
end

def callback(client)
client.dimension.unload_chunk({chunk_x, chunk_z})
end
end
10 changes: 5 additions & 5 deletions src/rosegold/states.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class Rosegold::State::Play
when 0x3d; nil # TODO: Clientbound::Respawn
when 0x52; Clientbound::UpdateHealth
# physics
when 0x22; nil # TODO: Clientbound::ChunkDataAndUpdateLight
when 0x1d; nil # TODO: Clientbound::UnloadChunk
when 0x0c; nil # TODO: Clientbound::BlockChange
when 0x3f; nil # TODO: Clientbound::MultiBlockChange
# inventory
when 0x22; Clientbound::ChunkData
when 0x1d; Clientbound::UnloadChunk
when 0x0c; Clientbound::BlockChange
when 0x3f; Clientbound::MultiBlockChange
# inventory
when 0x48; nil # TODO: Clientbound::HeldItemChange
when 0x2e; nil # TODO: Clientbound::OpenWindow
when 0x13; nil # TODO: Clientbound::CloseWindow
Expand Down
46 changes: 46 additions & 0 deletions src/rosegold/world/chunk.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Rosegold::World::Chunk
alias BlockStateNr = UInt16

getter sections : Array(Section)
getter min_y : Int32
getter world_height : Int32

def initialize(io : Minecraft::IO)
@min_y = -64
@world_height = 64 + 256 + 64
section_count = world_height >> 4
@sections = Array(Section).new(section_count) { Section.new io }
end

def block_state(x : Int32, y : Int32, z : Int32) : BlockStateNr | Nil
x, z = x & 15, z & 15
section = sections[(y - min_y) >> 4]? || return nil
index = (((y - min_y) & 15) << 8) | (z << 4) | x
section.block_state index.to_u32
end

def set_block_state(x : Int32, y : Int32, z : Int32, block_state : BlockStateNr)
x, z = x & 15, z & 15
section = sections[(y - min_y) >> 4]
index = (((y - min_y) & 15) << 8) | (z << 4) | x
section.set_block_state index.to_u32, block_state
end

# Chunk Section (16x16x16 blocks), data format 1.16-1.18
class Section
def initialize(io : Minecraft::IO)
# Number of non-air blocks present in the chunk section. If the block count reaches 0, the whole chunk section is not rendered.
@block_count = io.read_short
@blocks = PalettedContainer.new io, 9, 4096
@biomes = PalettedContainer.new io, 4, 64
end

def block_state(index : UInt32) : BlockStateNr
@blocks[index]
end

def set_block_state(index : UInt32, block_state : BlockStateNr)
@blocks[index] = block_state
end
end
end
23 changes: 23 additions & 0 deletions src/rosegold/world/dimension.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
require "./chunk"

class Rosegold::World::Dimension
alias ChunkPos = {Int32, Int32}

@chunks = Hash(ChunkPos, Chunk).new

def load_chunk(chunk_pos : ChunkPos, chunk : Chunk)
@chunks[chunk_pos] = chunk
end

def unload_chunk(chunk_pos : ChunkPos)
@chunks.delete chunk_pos
end

def block_state(x : Int32, y : Int32, z : Int32) : UInt16 | Nil
chunk_pos = {x >> 4, z >> 4}
@chunks[chunk_pos]?.try &.block_state(x, y, z)
end

def set_block_state(x : Int32, y : Int32, z : Int32, block_state : UInt16)
chunk_pos = {x >> 4, z >> 4}
@chunks[chunk_pos]?.try &.set_block_state(x, y, z, block_state)
end
end
4 changes: 4 additions & 0 deletions src/rosegold/world/look.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ abstract struct Rosegold::Look(T)
def +(look : self)
self.new(x + look.x, y + look.y, z + look.z)
end

def inspect(io)
io << "#<Look yaw=" << yaw << " pitch=" << pitch << ">"
end
end

struct Rosegold::LookRad < Rosegold::Look(Float32)
Expand Down
84 changes: 84 additions & 0 deletions src/rosegold/world/paletted_container.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
class Rosegold::PalettedContainer
private alias Entry = UInt16
private alias Index = UInt32
private alias Long = Int64

private getter bits_per_entry : UInt8
private getter entries_per_long : UInt8
private getter entry_mask : Long
private getter long_array : Array(Long)?
private getter palette : Array(Entry)?

# TODO read+write lock

def initialize(io : Minecraft::IO, num_bits_direct, size = nil)
@bits_per_entry = io.read_byte
if bits_per_entry == 0
@palette = [io.read_var_int.to_u16]
@entries_per_long = 0
@entry_mask = 0
num_longs = io.read_var_int
raise "Unexpected num_longs=#{num_longs} should be 0" if num_longs > 0
@long_array = nil
return
end

if bits_per_entry >= num_bits_direct
@palette = nil # long_array stores the values directly

else
@palette = Array(Entry).new(io.read_var_int) { io.read_var_int.to_u16 }
end

@entries_per_long = 64_u8 // bits_per_entry
@entry_mask = (1_i64 << bits_per_entry) - 1

num_longs = io.read_var_int
raise "Data too short! #{num_longs} * #{entries_per_long} < #{size}" if size && num_longs * entries_per_long < size

@long_array = Array(Long).new(num_longs) { io.read_long }
end

def [](index : Index) : Entry
long_array = self.long_array
return palette.not_nil![0] if !long_array
long_index = index // entries_per_long
bit_offset_in_long = (index % entries_per_long) * bits_per_entry
value = long_array[long_index] >> bit_offset_in_long
value = (value & entry_mask).to_u16
palette.try(&.[] value) || value
end

def []=(index : Index, value : Entry) : Nil
# type checker assumes these may be changed concurrently, changing whether they are nil
palette = self.palette
long_array = self.long_array
if !long_array # we're storing a single value
if palette.not_nil![0] == value
return # nothing to do, value is already set
else
raise "Growing PalettedContainer from single-state is not implemented" # TODO
end
end
entry = value
if palette
entry = palette.index(value) || begin
max_palette_size = 1_u64 << bits_per_entry
if palette.size < max_palette_size
entry = palette.size
palette << value
entry
else
# the palette indices do not fit into bits_per_entry anymore
raise "Growing PalettedContainer palette is not implemented" # TODO
end
end
end
long_index = index // entries_per_long
bit_offset_in_long = (index % entries_per_long) * bits_per_entry
long = long_array[long_index]
long &= ~(entry_mask << bit_offset_in_long) # clear previous value
long |= (entry & entry_mask) << bit_offset_in_long
long_array[long_index] = long
end
end
Loading