try fixing extremely weird bug
All checks were successful
Build & Test / nix-build (pull_request) Successful in 2m37s
Build & Test / nix-build (push) Successful in 2m38s

This commit is contained in:
Kodi Craft 2024-10-02 17:35:28 +02:00
parent af76cace76
commit d0a5b55ae8
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -85,12 +85,16 @@ defmodule Amethyst.ConnectionHandler do
end end
chunks = MapSet.new(visible_chunks_from(elem(cp, 0), elem(cp, 1), Map.get(state, :view_distance, 16))) chunks = MapSet.new(visible_chunks_from(elem(cp, 0), elem(cp, 1), Map.get(state, :view_distance, 16)))
new_chunks = MapSet.difference(chunks, prev_chunks) new_chunks = MapSet.difference(chunks, prev_chunks)
# Process these chunks, we can process all chunks in parallel # We can process all chunks in parallel
Task.Supervisor.async_stream(state |> Map.get(:game) |> Map.get(:refs) |> Map.get(:task_supervisor), # me = self()
new_chunks, # Task.Supervisor.async_stream(state |> Map.get(:game) |> Map.get(:refs) |> Map.get(:task_supervisor),
fn chunk -> process_chunk(chunk, state) end, # new_chunks,
[ordered: false] # fn chunk -> process_chunk(me, chunk, state) end,
) |> Stream.run() # [ordered: false]
# ) |> Stream.run()
if new_chunks |> Enum.map(&process_chunk(self(), &1, state)) |> Enum.any?(&(&1 != :ok)) do
Logger.error("something happened while processing chunks")
end
end end
loop(socket, connstate, version, state) loop(socket, connstate, version, state)
{:send_packet, packet} -> {:send_packet, packet} ->
@ -119,7 +123,7 @@ defmodule Amethyst.ConnectionHandler do
end) end)
end end
defp process_chunk(chunk, state) do defp process_chunk(to, chunk, state) do
import Amethyst.NBT.Write import Amethyst.NBT.Write
alias Amethyst.Minecraft.Write alias Amethyst.Minecraft.Write
@ -131,20 +135,17 @@ defmodule Amethyst.ConnectionHandler do
heightmaps = compound(%{}) heightmaps = compound(%{})
data = Enum.chunk_every(chunk_array, 16, 16, 0) # 0 -> air data = Enum.chunk_every(chunk_array, 16, 16, 0) # 0 -> air
|> Enum.reduce(fn chunk_section, acc -> |> Enum.reduce("", fn chunk_section, acc ->
blocks = chunk_section |> List.flatten() blocks = chunk_section |> List.flatten()
block_count = blocks block_count = blocks |> Enum.filter(&(&1 != 0)) |> length
|> Enum.map(&(if &1 == 0, do: 0, else: 1))
|> Enum.sum()
# Put together the palette # Put together the palette
unique_blocks = MapSet.new(blocks) unique_blocks = MapSet.new(blocks)
min_bpe = MapSet.size(unique_blocks) |> :math.log2() |> ceil() min_bpe = MapSet.size(unique_blocks) |> :math.log2() |> ceil()
paletted_container_data = case min_bpe do paletted_container_data = case min_bpe do
0 -> <<0::8>> <> 0 -> <<0::8>> <>
# SINGLE VALUED # SINGLE VALUED
Write.varint(MapSet.to_list(unique_blocks)[0]) <> Write.varint(MapSet.to_list(unique_blocks) |> List.first()) <>
Write.varint(0) # No data, empty pallette Write.varint(0) # No data, empty pallette
min_bpe when min_bpe in 1..8 -> min_bpe when min_bpe in 1..8 ->
# INDIRECT # INDIRECT
@ -155,6 +156,7 @@ defmodule Amethyst.ConnectionHandler do
Map.new(fn {i, v} -> {v, i} end) Map.new(fn {i, v} -> {v, i} end)
paletted_blocks = blocks |> paletted_blocks = blocks |>
Enum.map(&(Map.get(palette, &1))) Enum.map(&(Map.get(palette, &1)))
Logger.debug("got here")
paletted_data = long_aligned_bit_string_reduce(paletted_blocks, bpe) paletted_data = long_aligned_bit_string_reduce(paletted_blocks, bpe)
Write.ubyte(bpe) <> Write.ubyte(bpe) <>
@ -166,6 +168,7 @@ defmodule Amethyst.ConnectionHandler do
paletted_data paletted_data
_ -> _ ->
# DIRECT # DIRECT
Logger.debug("got here with bpe #{min_bpe}")
data = long_aligned_bit_string_reduce(blocks, 15) data = long_aligned_bit_string_reduce(blocks, 15)
Write.ubyte(15) <> Write.ubyte(15) <>
Write.varint(floor(bit_size(data) / 64)) <> Write.varint(floor(bit_size(data) / 64)) <>
@ -176,7 +179,8 @@ defmodule Amethyst.ConnectionHandler do
acc <> Write.short(block_count) <> paletted_container_data <> <<0::8, 0::8>> acc <> Write.short(block_count) <> paletted_container_data <> <<0::8, 0::8>>
end) end)
send(self(), {:send_packet, %{ Logger.debug("Asking to send chunk packet...")
send(to, {:send_packet, %{
packet_type: :chunk_data_and_update_light, packet_type: :chunk_data_and_update_light,
chunk_x: cx, chunk_z: cz, chunk_x: cx, chunk_z: cz,
heightmaps: heightmaps, heightmaps: heightmaps,
@ -190,11 +194,12 @@ defmodule Amethyst.ConnectionHandler do
sky_light_arrays: [], sky_light_arrays: [],
block_light_arrays: [] block_light_arrays: []
}}) }})
:ok
end end
defp long_aligned_bit_string_reduce(values, bpe) do defp long_aligned_bit_string_reduce(values, bpe) do
values |> Enum.reduce("", fn value, acc -> values |> Enum.reduce("", fn value, acc ->
next = acc <> <<value::big-size(bpe)>> next = acc <> <<value::size(bpe)-big>>
# man i hope they dont suddenly change the size of a long # man i hope they dont suddenly change the size of a long
if rem(bit_size(next), 64) + bpe < 64 do if rem(bit_size(next), 64) + bpe < 64 do
# gotta pad it # gotta pad it