From 4c7156d0b20447e29f354679acdcd28480df91bc Mon Sep 17 00:00:00 2001
From: ubq323 <ubq323@ubq323.website>
Date: Mon, 22 Apr 2024 01:21:38 +0100
Subject: rlerle

---
 packet_reader/packet_reader.ha | 19 ++++++++++++-------
 packet_reader/rle.ha           | 19 ++++++++++++++++---
 2 files changed, 28 insertions(+), 10 deletions(-)

(limited to 'packet_reader')

diff --git a/packet_reader/packet_reader.ha b/packet_reader/packet_reader.ha
index a1b5b8f..d541a11 100644
--- a/packet_reader/packet_reader.ha
+++ b/packet_reader/packet_reader.ha
@@ -100,17 +100,20 @@ export fn next(pr: *packet_reader) (packet | done | error) = {
 		case drawing::deser_fail => return "deser fail": error;
 		};
 	case packet_type::SEND_CHUNK =>
-		// return value is BORROWED from the BUFFER
+		// return value muste be FREED by CALLER
+		// (we don't really need a copy here. todo)
+		// ((the copy isn't here, it's wherever uses here))
 		const pos_bytes = payload[0..8];
-		const chunk_data_bytes = payload[8..];
+		const compressed_data_bytes = payload[8..];
 		const pos = (
 			endian::legetu32(pos_bytes[0..4]): i32,
 			endian::legetu32(pos_bytes[4..8]): i32
 		): pos;
-		const d = cast_u8s_to_u32s(chunk_data_bytes);
-		if (len(d) != CHUNKSIZE*CHUNKSIZE)
+		const chunk_data_compressed = cast_u8s_to_u32s(compressed_data_bytes);
+		const chunk_data = rle_decode(chunk_data_compressed);
+		if (len(chunk_data) != CHUNKSIZE*CHUNKSIZE)
 			return "wrong chunk size??": error;
-		return packet_sendchunk { world_pos = pos, chunk_data = d };
+		return packet_sendchunk { world_pos = pos, chunk_data = chunk_data };
 	case packet_type::POSITION =>
 		if (len(payload) != 8)
 			return "position packet wrong size": error;
@@ -155,8 +158,10 @@ export fn send(sock: io::file, packet: packet) (void | io::error) = {
 		const pos_buf: [8]u8 = [0...];
 		endian::leputu32(pos_buf[0..4],packet.world_pos.0: u32);
 		endian::leputu32(pos_buf[4..8],packet.world_pos.1: u32);
-		const chunk_data_bytes = cast_u32s_to_u8s(packet.chunk_data);
-		send_raw(sock, packet_type::SEND_CHUNK, pos_buf, chunk_data_bytes)?;
+		const chunk_data_compressed = rle_encode(packet.chunk_data);
+		defer free(chunk_data_compressed);
+		const compressed_data_bytes = cast_u32s_to_u8s(chunk_data_compressed);
+		send_raw(sock, packet_type::SEND_CHUNK, pos_buf, compressed_data_bytes)?;
 	case let pos: packet_position =>
 		const pos_buf: [8]u8 = [0...];
 		endian::leputu32(pos_buf[0..4], pos.0: u32);
diff --git a/packet_reader/rle.ha b/packet_reader/rle.ha
index af9f080..36e0b29 100644
--- a/packet_reader/rle.ha
+++ b/packet_reader/rle.ha
@@ -16,11 +16,11 @@ export fn rle_encode(d: []u32) []u32 = {
 		for (ix < len(d) && d[ix] == this) ix += 1;
 		const val = this;
 		let count = (ix - start): int;
-		fmt::printfln("#{:_06x} x{}",val, count)!;
-		for (count > 0) {
-			append(out, item_encode(count%256, val));
+		for (count > 256) {
+			append(out, item_encode(256, val));
 			count -= 256;
 		};
+		append(out, item_encode(count, val));
 		start = ix;
 	};
 	return out;
@@ -63,3 +63,16 @@ fn dotest(eu: []u32) void = {
 @test fn oeuoeu2() void = dotest([1,1,1]);
 @test fn oeuoeu2() void = dotest([1,6]);
 @test fn oeuoeu2() void = dotest([1]);
+
+fn testm(n: size) void = {
+	let e: []u32 = [];
+	append(e, [17...], n);
+	defer free(e);
+	dotest(e);
+};
+
+@test fn t254() void = testm(254);
+@test fn t255() void = testm(255);
+@test fn t256() void = testm(256);
+@test fn t257() void = testm(257);
+@test fn t258() void = testm(258);
-- 
cgit v1.2.3