Inverse mode and some old things

This commit is contained in:
Casey 2024-10-03 15:49:08 +03:00
parent c081ad93d7
commit b8c71209f7
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
8 changed files with 296 additions and 6 deletions

3
.gitignore vendored
View File

@ -4,3 +4,6 @@ obcb-nbd.so
live live
bot bot
live-1chunk live-1chunk
*.data
obcb-write
listen-fullmap

View File

@ -2,7 +2,7 @@ CFLAGS +=
LDFLAGS := -lm LDFLAGS := -lm
OBJECTS := obj/obcb.o obj/mongoose.o OBJECTS := obj/obcb.o obj/mongoose.o
all: obcb obcb-nbd.so live all: obcb obcb-nbd.so live live-1chunk listen-fullmap obcb-write
test: obcb test: obcb
./obcb ./obcb
@ -16,9 +16,18 @@ live-1chunk-run: live-1chunk
bot-run: bot bot-run: bot
./bot ./bot
listen-fullmap-run: listen-fullmap
./listen-fullmap
obcb-write: ./src/obcb-write.c $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) src/obcb-write.c $(LDFLAGS) -o obcb-write -lraylib -lm
bot: ./src/bot.c $(OBJECTS) bot: ./src/bot.c $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) src/bot.c $(LDFLAGS) -o bot -lraylib -lm $(CC) $(CFLAGS) $(OBJECTS) src/bot.c $(LDFLAGS) -o bot -lraylib -lm
listen-fullmap: ./src/listen-fullmap.c $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) src/listen-fullmap.c $(LDFLAGS) -o listen-fullmap -lraylib -lm
live: ./src/live.c $(OBJECTS) live: ./src/live.c $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) src/live.c $(LDFLAGS) -o live -lraylib -lm $(CC) $(CFLAGS) $(OBJECTS) src/live.c $(LDFLAGS) -o live -lraylib -lm

1
img421.cpi Normal file

File diff suppressed because one or more lines are too long

View File

@ -125,7 +125,7 @@ int render_thread(void *_conn) {
int failure = 0; int failure = 0;
for (int i = (rand() % OBCB_CHUNK_SIZE); true; i = (i + 1) % OBCB_CHUNK_SIZE) { for (int i = (rand() % OBCB_CHUNK_SIZE); true; i = (i + 1) % OBCB_CHUNK_SIZE) {
#if 0 #if 1
int blk_index = i / 256; int blk_index = i / 256;
int blk_pos = shuffle[i % 256]; int blk_pos = shuffle[i % 256];
int ox = blk_pos & 15, oy = blk_pos >> 4; int ox = blk_pos & 15, oy = blk_pos >> 4;
@ -149,7 +149,7 @@ int render_thread(void *_conn) {
if ((chunk_current[byte] & mask) != (chunk_target[byte] & mask)) { if ((chunk_current[byte] & mask) != (chunk_target[byte] & mask)) {
obcb_send_toggle_bit(ws, index + dst_chunk_id * OBCB_CHUNK_SIZE); obcb_send_toggle_bit(ws, index + dst_chunk_id * OBCB_CHUNK_SIZE);
usleep(2500); usleep(5000);
} else if (failure++ > 1024) { } else if (failure++ > 1024) {
usleep(1000); usleep(1000);
failure = 0; failure = 0;

86
src/listen-fullmap.c Normal file
View File

@ -0,0 +1,86 @@
// x-run: make -C.. listen-fullmap-run
#include <math.h>
#include <raylib.h>
#include "mongoose.h"
#include <string.h>
#include <threads.h>
#include "obcb.h"
#include "packets.h"
#define OBCB_URL "ws://bitmap-ws.alula.me/"
static uint8_t buffer_current[OBCB_BITMAP_SIZE / 8];
bool party_should_be_partying = true;
static int mongoose_thread(void *mgr) {
mg_mgr_init(mgr);
while (party_should_be_partying) mg_mgr_poll(mgr, 1000);
mg_mgr_free(mgr);
return 0;
}
static void mg_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
c->is_hexdumping = 0;
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wsm = (struct mg_ws_message*)ev_data;
if (wsm->data.buf[0] == OBCB_MSG_HELLO) {
struct obcb_msg_hello hello; memcpy(&hello, &wsm->data.buf[1], sizeof(hello));
printf("HELLO: v%d.%d. Why hello there, honey~\n", hello.version_major, hello.version_minor);
} else if (wsm->data.buf[0] == OBCB_MSG_STATS) {
struct obcb_msg_stats stats; memcpy(&stats, &wsm->data.buf[1], sizeof(stats));
printf("STATS: %d clients connected\n", stats.current_clients);
} else if (wsm->data.buf[0] == OBCB_MSG_FULL_STATE_RESPONSE) {
struct obcb_msg_full_state_response state; memcpy(&state, &wsm->data.buf[1], sizeof(state));
printf("Got full state\n");
memcpy(&buffer_current[state.chunk_index * OBCB_CHUNK_SIZE_BYTES], state.bitmap, OBCB_CHUNK_SIZE_BYTES);
} else {
printf("UNHANDLED MESSAGE 0x%02x\n", wsm->data.buf[0]);
}
}
}
struct chunk {
double last_updated;
bool requested;
} chunks[OBCB_CHUNK_COUNT];
int main(void) {
thrd_t mg_mgr_thread;
struct mg_mgr mg_manager;
struct mg_connection *mg_connections[OBCB_CHUNK_COUNT];
thrd_create(&mg_mgr_thread, mongoose_thread, &mg_manager);
for (int i = 0; i < 64; i++) {
mg_connections[i] = mg_ws_connect(
&mg_manager,
OBCB_URL,
mg_handler, 0,
"User-Agent: maidcore/4.2.0 (kc.is.being.pet), mongoose.ws/" MG_VERSION "\r\n");
}
InitWindow(512, 512, "listener");
SetTargetFPS(60);
while (!WindowShouldClose()) {
double current_time = GetTime();
BeginDrawing();
ClearBackground(BLACK);
for (int i = 0; i < OBCB_CHUNK_COUNT; i++) {
int x = i % 64, y = i / 64;
double since_update = current_time - chunks[i].last_updated;
float alpha = 2.0 - sqrtf((since_update + 3.0) / 3.0);
DrawRectangle(x * 8, y * 8, 8, 8, Fade(RED, alpha));
}
EndDrawing();
}
party_should_be_partying = false;
thrd_join(mg_mgr_thread, 0);
return 0;
}

View File

@ -1,8 +1,10 @@
// x-run: make -C.. CFLAGS+=-ggdb live-1chunk-run // x-run: make -C.. CFLAGS+=-ggdb live-1chunk-run
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "mongoose.h" #include "mongoose.h"
#include <raylib.h> #include <raylib.h>
#include <raymath.h> #include <raymath.h>
#include <string.h>
#include <threads.h> #include <threads.h>
#include "obcb.h" #include "obcb.h"
#include "packets.h" #include "packets.h"
@ -15,6 +17,8 @@ int connected_clients = 0;
double last_msg_time = 0; double last_msg_time = 0;
uint8_t chunk_data[OBCB_CHUNK_SIZE_BYTES];
static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) { static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) { if (ev == MG_EV_OPEN) {
printf("Connected to WebSocket\n"); printf("Connected to WebSocket\n");
@ -38,6 +42,7 @@ static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
view[i] = state.bitmap[i >> 3] & (1 << (i & 7)) ? 0xff : 0x00; view[i] = state.bitmap[i >> 3] & (1 << (i & 7)) ? 0xff : 0x00;
} }
last_msg_time = GetTime(); last_msg_time = GetTime();
memcpy(chunk_data, state.bitmap, OBCB_CHUNK_SIZE_BYTES);
} else if (wsm->data.buf[0] == OBCB_MSG_PARTIAL_STATE_UPDATE) { } else if (wsm->data.buf[0] == OBCB_MSG_PARTIAL_STATE_UPDATE) {
struct obcb_msg_partial_state_update update; memcpy(&update, &wsm->data.buf[1], sizeof(update)); struct obcb_msg_partial_state_update update; memcpy(&update, &wsm->data.buf[1], sizeof(update));
@ -46,10 +51,13 @@ static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
if (chunk_index != chunk_id) return; if (chunk_index != chunk_id) return;
printf("Partial update\n");
unsigned char *view = c->fn_data; unsigned char *view = c->fn_data;
for (int i = 0; i < OBCB_UPDATE_CHUNK_SIZE * 8; i++) { for (int i = 0; i < OBCB_UPDATE_CHUNK_SIZE * 8; i++) {
view[i + byte_offset * 8] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00; view[i + byte_offset * 8] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00;
} }
memcpy(&chunk_data[byte_offset], update.chunk, OBCB_UPDATE_CHUNK_SIZE);
last_msg_time = GetTime(); last_msg_time = GetTime();
} else { } else {
printf("UNHANDLED MESSAGE 0x%02x\n", wsm->data.buf[0]); printf("UNHANDLED MESSAGE 0x%02x\n", wsm->data.buf[0]);
@ -67,10 +75,14 @@ static int mongoose_thread(void *_mgr) {
void DrawTextShadow(Font font, const char *txt, int x, int y, int size, Color color); void DrawTextShadow(Font font, const char *txt, int x, int y, int size, Color color);
int main(void) { int main(int argc, char **argv) {
InitWindow(512, 512, "obcb-1chunk"); InitWindow(512, 512, "obcb-1chunk");
SetTargetFPS(60); SetTargetFPS(60);
if (argc >= 2) {
chunk_id = atoi(argv[1]);
}
thrd_t mongoose_manager_thrd; thrd_t mongoose_manager_thrd;
thrd_create(&mongoose_manager_thrd, mongoose_thread, 0); thrd_create(&mongoose_manager_thrd, mongoose_thread, 0);
@ -95,9 +107,17 @@ int main(void) {
for (int frame = 0; !WindowShouldClose(); frame++) { for (int frame = 0; !WindowShouldClose(); frame++) {
if (IsKeyPressed(KEY_R)) { if (IsKeyPressed(KEY_R)) {
printf("Requesting state again\n"); printf("Requesting state again\n");
obcb_send_partial_subscription(ws, chunk_id);
obcb_send_full_state_request(ws, chunk_id); obcb_send_full_state_request(ws, chunk_id);
} }
if (IsKeyPressed(KEY_S)) {
FILE *fp = fopen(TextFormat("save.%d.%d.data", time(0), chunk_id), "wb");
printf("Saving...\n");
fwrite(chunk_data, OBCB_CHUNK_SIZE_BYTES, 1, fp);
fclose(fp);
}
BeginDrawing(); BeginDrawing();
UpdateTexture(tex, img.data); UpdateTexture(tex, img.data);
DrawTexture(tex, 0, 0, WHITE); DrawTexture(tex, 0, 0, WHITE);

View File

@ -8,6 +8,7 @@
#include "obcb.h" #include "obcb.h"
#define WAITING_QUEUE_SIZE 48 #define WAITING_QUEUE_SIZE 48
#define INVERSE_COLORS 1
static uint32_t connected_clients = 0; static uint32_t connected_clients = 0;
static bool running = true; static bool running = true;
@ -59,7 +60,7 @@ struct img_and_tex {
Texture2D tex; Texture2D tex;
} blocks[32 * 32], subscribed; } blocks[32 * 32], subscribed;
int subscribed_chunk_id = 420; int subscribed_chunk_id = -1;
static struct waiting_queue_elem { static struct waiting_queue_elem {
uint16_t index; uint16_t index;
@ -99,10 +100,18 @@ static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
int in_chunk_x = x % 1024, in_chunk_y = y % 1024; int in_chunk_x = x % 1024, in_chunk_y = y % 1024;
uint8_t *img = blocks[chunk_x + chunk_y * 32].img.data; uint8_t *img = blocks[chunk_x + chunk_y * 32].img.data;
bool bit = state.bitmap[i >> 3] & (1 << (i & 7)); bool bit = state.bitmap[i >> 3] & (1 << (i & 7));
#if INVERSE_COLORS
img[in_chunk_x + in_chunk_y * 1024] = bit ? 0x00 : 0xFF;
#else
img[in_chunk_x + in_chunk_y * 1024] = bit ? 0xFF : 0x00; img[in_chunk_x + in_chunk_y * 1024] = bit ? 0xFF : 0x00;
#endif
if (state.chunk_index == subscribed_chunk_id) { if (state.chunk_index == subscribed_chunk_id) {
#if INVERSE_COLORS
view[i] = bit ? 0x00 : 0xFF;
#else
view[i] = bit ? 0xFF : 0x00; view[i] = bit ? 0xFF : 0x00;
#endif
} }
} }
@ -115,7 +124,11 @@ static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
int chunk_x = x / 1024, chunk_y = y / 1024; int chunk_x = x / 1024, chunk_y = y / 1024;
int in_chunk_x = x % 1024, in_chunk_y = y % 1024; int in_chunk_x = x % 1024, in_chunk_y = y % 1024;
uint8_t *img = blocks[chunk_x + chunk_y * 32].img.data; uint8_t *img = blocks[chunk_x + chunk_y * 32].img.data;
#if INVERSE_COLORS
img[in_chunk_x + in_chunk_y * 1024] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0x00 : 0xFF;
#else
img[in_chunk_x + in_chunk_y * 1024] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00; img[in_chunk_x + in_chunk_y * 1024] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00;
#endif
} }
uint32_t chunk_index = update.offset / OBCB_CHUNK_SIZE_BYTES; uint32_t chunk_index = update.offset / OBCB_CHUNK_SIZE_BYTES;
@ -124,7 +137,11 @@ static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
printf("That's our chunk!\n"); printf("That's our chunk!\n");
unsigned char *view = subscribed.img.data; unsigned char *view = subscribed.img.data;
for (int i = 0; i < OBCB_UPDATE_CHUNK_SIZE; i++) { for (int i = 0; i < OBCB_UPDATE_CHUNK_SIZE; i++) {
#if INVERSE_COLORS
view[i + byte_offset * 8] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0x00 : 0xFF;
#else
view[i + byte_offset * 8] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00; view[i + byte_offset * 8] = update.chunk[i >> 3] & (1 << (i & 7)) ? 0xFF : 0x00;
#endif
} }
} }
} else { } else {
@ -189,7 +206,7 @@ int main(void) {
.zoom = 0.125 .zoom = 0.125
}; };
double zoom = 1.0; double zoom = 0.5;
int req_chunk = 0; int req_chunk = 0;
for (unsigned long frame = 0; !WindowShouldClose(); frame++) { for (unsigned long frame = 0; !WindowShouldClose(); frame++) {

154
src/obcb-write.c Normal file
View File

@ -0,0 +1,154 @@
// x-run: make -C.. obcb-write && ../obcb-write 421 ../img421.cpi
#include "mongoose.h"
#include "obcb.h"
#include "packets.h"
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include <time.h>
#include <unistd.h>
void usage(const char *progname) {
printf("Usage: %s [page:int] [infile:path]\n", progname);
}
int chunk_id = 0;
bool has_data = false;
size_t buffer_size = 0;
unsigned char buffer[OBCB_CHUNK_SIZE_BYTES], current[OBCB_CHUNK_SIZE_BYTES];
const uint8_t shuffle[256] = {
0, 128, 64, 192, 32, 160, 96, 224, 16,
144, 80, 208, 48, 176, 112, 240, 8,
136, 72, 200, 40, 168, 104, 232, 24,
152, 88, 216, 56, 184, 120, 248, 4,
132, 68, 196, 36, 164, 100, 228, 20,
148, 84, 212, 52, 180, 116, 244, 12,
140, 76, 204, 44, 172, 108, 236, 28,
156, 92, 220, 60, 188, 124, 252, 2,
130, 66, 194, 34, 162, 98, 226, 18,
146, 82, 210, 50, 178, 114, 242, 10,
138, 74, 202, 42, 170, 106, 234, 26,
154, 90, 218, 58, 186, 122, 250, 6,
134, 70, 198, 38, 166, 102, 230, 22,
150, 86, 214, 54, 182, 118, 246, 14,
142, 78, 206, 46, 174, 110, 238, 30,
158, 94, 222, 62, 190, 126, 254, 1,
129, 65, 193, 33, 161, 97, 225, 17,
145, 81, 209, 49, 177, 113, 241, 9,
137, 73, 201, 41, 169, 105, 233, 25,
153, 89, 217, 57, 185, 121, 249, 5,
133, 69, 197, 37, 165, 101, 229, 21,
149, 85, 213, 53, 181, 117, 245, 13,
141, 77, 205, 45, 173, 109, 237, 29,
157, 93, 221, 61, 189, 125, 253, 3,
131, 67, 195, 35, 163, 99, 227, 19,
147, 83, 211, 51, 179, 115, 243, 11,
139, 75, 203, 43, 171, 107, 235, 27,
155, 91, 219, 59, 187, 123, 251, 7,
135, 71, 199, 39, 167, 103, 231, 23,
151, 87, 215, 55, 183, 119, 247, 15,
143, 79, 207, 47, 175, 111, 239, 31,
159, 95, 223, 63, 191, 127, 255
};
static void obcb_mg_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
printf("Connected to WebSocket\n");
} else if (ev == MG_EV_WS_OPEN) {
obcb_send_full_state_request(c, 0);
} else if (ev == MG_EV_WS_MSG) {
struct mg_ws_message *wsm = (struct mg_ws_message*)ev_data;
if (wsm->data.buf[0] == OBCB_MSG_HELLO) {
struct obcb_msg_hello hello; memcpy(&hello, &wsm->data.buf[1], sizeof(hello));
printf("HELLO: v%d.%d. Why hello there, honey~\n", hello.version_major, hello.version_minor);
} else if (wsm->data.buf[0] == OBCB_MSG_STATS) {
struct obcb_msg_stats stats; memcpy(&stats, &wsm->data.buf[1], sizeof(stats));
} else if (wsm->data.buf[0] == OBCB_MSG_FULL_STATE_RESPONSE) {
struct obcb_msg_full_state_response state; memcpy(&state, &wsm->data.buf[1], sizeof(state));
if (state.chunk_index == chunk_id) {
memcpy(current, state.bitmap, OBCB_CHUNK_SIZE_BYTES);
}
has_data = true;
} else if (wsm->data.buf[0] == OBCB_MSG_PARTIAL_STATE_UPDATE) {
struct obcb_msg_partial_state_update update; memcpy(&update, &wsm->data.buf[1], sizeof(update));
uint32_t chunk_index = update.offset / OBCB_CHUNK_SIZE_BYTES;
uint32_t byte_index = update.offset % OBCB_CHUNK_SIZE_BYTES;
if (chunk_index == chunk_id) {
memcpy(&current[byte_index], update.chunk, OBCB_UPDATE_CHUNK_SIZE);
}
} else {
printf("UNHANDLED MESSAGE 0x%02x\n", wsm->data.buf[0]);
}
}
}
int render_thread(void *_conn);
int main(int argc, char **argv) {
if (argc < 3) {
usage(argv[0]);
return 1;
}
chunk_id = atoi(argv[1]);
FILE *fp = fopen(argv[2], "rb");
buffer_size = fread(buffer, 1, OBCB_CHUNK_SIZE_BYTES, fp);
fclose(fp);
printf("Writing %zu bytes at the chunk %d\n", buffer_size, chunk_id);
struct mg_mgr manager;
mg_mgr_init(&manager);
struct mg_connection *ws = mg_ws_connect(&manager, "ws://bitmap-ws.alula.me/", obcb_mg_handler, 0, "%s", "User-Agent: maidcore/4.2.0 (kc.is.being.pet)\r\n");
obcb_send_full_state_request(ws, chunk_id);
obcb_send_partial_subscription(ws, chunk_id);
time_t last_request = time(0);
int t = 0;
while (true) {
time_t now = time(0);
if ((now - last_request) > 10) {
obcb_send_full_state_request(ws, chunk_id);
last_request = now;
}
if (has_data) {
for (int j = 0; j < 32; j++){
size_t i = ((t & ~0xFFL) | shuffle[t & 0xFF]) % buffer_size;
if (buffer[i] != current[i]) {
for (int bit = 0, mask = 1; bit < 8; bit++, mask <<= 1) {
if ((buffer[i] ^ current[i]) & mask) {
obcb_send_toggle_bit(ws, chunk_id * OBCB_CHUNK_SIZE + i * 8 + bit);
usleep(5000);
}
}
}
t++;
}
}
mg_mgr_poll(&manager, 100);
}
mg_mgr_free(&manager);
return 0;
}
int render_thread(void *_conn) {
struct mg_connection *ws = _conn;
while (!has_data) {
usleep(1000);
}
printf("Got initial state!\n");
for (int t = 0; true; t++) {
if ((t % 100) == 0) {
usleep(100);
}
}
}