From d0a5b55ae88a996284fea934f80150bb02fbeaf4 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Wed, 2 Oct 2024 17:35:28 +0200 Subject: [PATCH] try fixing extremely weird bug --- apps/amethyst/lib/apps/connection_handler.ex | 35 +++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/apps/amethyst/lib/apps/connection_handler.ex b/apps/amethyst/lib/apps/connection_handler.ex index d61b0e1..758f048 100644 --- a/apps/amethyst/lib/apps/connection_handler.ex +++ b/apps/amethyst/lib/apps/connection_handler.ex @@ -85,12 +85,16 @@ defmodule Amethyst.ConnectionHandler do end 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) - # Process these chunks, we can process all chunks in parallel - Task.Supervisor.async_stream(state |> Map.get(:game) |> Map.get(:refs) |> Map.get(:task_supervisor), - new_chunks, - fn chunk -> process_chunk(chunk, state) end, - [ordered: false] - ) |> Stream.run() + # We can process all chunks in parallel + # me = self() + # Task.Supervisor.async_stream(state |> Map.get(:game) |> Map.get(:refs) |> Map.get(:task_supervisor), + # new_chunks, + # fn chunk -> process_chunk(me, chunk, state) end, + # [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 loop(socket, connstate, version, state) {:send_packet, packet} -> @@ -119,7 +123,7 @@ defmodule Amethyst.ConnectionHandler do end) end - defp process_chunk(chunk, state) do + defp process_chunk(to, chunk, state) do import Amethyst.NBT.Write alias Amethyst.Minecraft.Write @@ -131,20 +135,17 @@ defmodule Amethyst.ConnectionHandler do heightmaps = compound(%{}) 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() - block_count = blocks - |> Enum.map(&(if &1 == 0, do: 0, else: 1)) - |> Enum.sum() + block_count = blocks |> Enum.filter(&(&1 != 0)) |> length # Put together the palette unique_blocks = MapSet.new(blocks) min_bpe = MapSet.size(unique_blocks) |> :math.log2() |> ceil() - paletted_container_data = case min_bpe do 0 -> <<0::8>> <> # 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 min_bpe when min_bpe in 1..8 -> # INDIRECT @@ -155,6 +156,7 @@ defmodule Amethyst.ConnectionHandler do Map.new(fn {i, v} -> {v, i} end) paletted_blocks = blocks |> Enum.map(&(Map.get(palette, &1))) + Logger.debug("got here") paletted_data = long_aligned_bit_string_reduce(paletted_blocks, bpe) Write.ubyte(bpe) <> @@ -166,6 +168,7 @@ defmodule Amethyst.ConnectionHandler do paletted_data _ -> # DIRECT + Logger.debug("got here with bpe #{min_bpe}") data = long_aligned_bit_string_reduce(blocks, 15) Write.ubyte(15) <> 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>> end) - send(self(), {:send_packet, %{ + Logger.debug("Asking to send chunk packet...") + send(to, {:send_packet, %{ packet_type: :chunk_data_and_update_light, chunk_x: cx, chunk_z: cz, heightmaps: heightmaps, @@ -190,11 +194,12 @@ defmodule Amethyst.ConnectionHandler do sky_light_arrays: [], block_light_arrays: [] }}) + :ok end defp long_aligned_bit_string_reduce(values, bpe) do values |> Enum.reduce("", fn value, acc -> - next = acc <> <> + next = acc <> <> # man i hope they dont suddenly change the size of a long if rem(bit_size(next), 64) + bpe < 64 do # gotta pad it