summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/array.h19
-rw-r--r--include/content.h45
-rw-r--r--include/peer.h7
-rw-r--r--include/ser.h6
-rw-r--r--include/str.h12
5 files changed, 69 insertions, 20 deletions
diff --git a/include/array.h b/include/array.h
new file mode 100644
index 0000000..8d52619
--- /dev/null
+++ b/include/array.h
@@ -0,0 +1,19 @@
+// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+#ifndef ARRAY_H
+#define ARRAY_H
+
+#include <stddef.h>
+
+#define len(X) (sizeof X / sizeof *X)
+#define array(T) struct { size_t len; T *data; }
+#define arraybuf(T) struct { size_t cap; size_t len; T *data; }
+
+#define ARR_REMOVE(A, P) memmove((P), (P)+1, --(A).len * sizeof *(P) - ((P) - (A).data));
+#define ARR_APPEND(A) (((A).cap == (A).len) \
+ ? (A).data = realloc((A).data, sizeof *(A).data * ((A).cap = (A).cap ? (A).cap * 2 : 1)) \
+ : NULL, &((A).data)[(A).len++])
+
+#endif
diff --git a/include/content.h b/include/content.h
index ac20e7e..ed8b822 100644
--- a/include/content.h
+++ b/include/content.h
@@ -5,6 +5,9 @@
#ifndef CONTENT_H
#define CONTENT_H
+#include <stdint.h>
+#include <stdbool.h>
+
typedef enum {
N_VALLEY_FLOWER,
N_MOUNTAIN_FLOWER,
@@ -14,12 +17,24 @@ typedef enum {
N_WATER,
N_PLANK,
N_PATH,
+ N_GRASS,
+ N_SAND,
} node_type;
typedef struct {
uint8_t r, g, b;
} color;
+static inline uint32_t color_u32(color c)
+{
+ return ((uint32_t) c.b) | (((uint32_t) c.g) << 8) | (((uint32_t) c.r) << 16);
+}
+
+static inline color u32_color(uint32_t u)
+{
+ return (color) { (u >> 16) & 0xFF, (u >> 8) & 0xFF, u & 0xFF };
+}
+
typedef struct {
bool present;
node_type type;
@@ -28,22 +43,36 @@ typedef struct {
} node;
typedef enum {
- MOVE_UP,
+ MOVE_UP = 0,
MOVE_DOWN,
MOVE_LEFT,
MOVE_RIGHT,
} move_dir;
-typedef enum {
- CPKT_HI, // len motd
- CPKT_PLAYERS, // len
+typedef uint8_t fail_reason;
+#define ser_fail_reason ser_u8
+#define deser_fail_reason deser_u8
+
+enum {
+ FAIL_WRONG_PASS = 0,
+ FAIL_ALREADY_ONLINE,
+};
+
+typedef uint16_t pkt_type;
+#define ser_pkt_type ser_u16
+#define deser_pkt_type deser_u16
+
+enum {
+ CPKT_HI = 0, // len motd
+ CPKT_FAIL, // fail_reason
+ CPKT_PLAYERS, // len [len name id]
CPKT_MOVE, // player_id remove x y z
CPKT_NODES, // z x y w h [node]
-} client_pkt;
+};
-typedef enum {
- SPKT_HI, // len name len password
+enum {
+ SPKT_HI = 0, // len name len password
SPKT_MOVE, // move_dir
-} server_pkt;
+};
#endif
diff --git a/include/peer.h b/include/peer.h
index 63de9e5..f6baa4b 100644
--- a/include/peer.h
+++ b/include/peer.h
@@ -8,6 +8,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
+#include <poll.h>
#define PEER_INBUFFER_SIZE 0x100000 // 1MB
#define PEER_OUTBUFFER_SIZE 0x200000 // 2MB
@@ -33,8 +34,8 @@ typedef struct {
void peer_init(peer *p, int socket);
void peer_free(peer *p);
-short peer_prepare(peer *p);
-bool peer_ready(peer *p, short revents);
-bool peer_send(peer *p, uint8_t *data, size_t len);
+struct pollfd peer_prepare(peer *p);
+bool peer_ready(peer *p, struct pollfd revents);
+bool peer_send(peer *p, void *data, size_t len);
#endif
diff --git a/include/ser.h b/include/ser.h
index c1f207d..374e5d9 100644
--- a/include/ser.h
+++ b/include/ser.h
@@ -12,8 +12,9 @@
// ser
-void ser_bytes(strbuf *w, size_t len, uint8_t *x);
+void ser_bytes(strbuf *w, void *x, size_t len);
+void ser_bool(strbuf *w, bool x);
void ser_str(strbuf *w, str x);
void ser_u8(strbuf *w, uint8_t x);
@@ -30,8 +31,9 @@ void ser_i64(strbuf *w, int64_t x);
// deser
-bool deser_bytes(str *r, size_t len, uint8_t *buf);
+bool deser_bytes(str *r, void *buf, size_t len);
+bool deser_bool(str *r, bool *buf);
bool deser_str(str *r, str *buf); // returns slice!
bool deser_u8(str *r, uint8_t *buf);
diff --git a/include/str.h b/include/str.h
index b208515..277a6f3 100644
--- a/include/str.h
+++ b/include/str.h
@@ -7,20 +7,18 @@
#include <stddef.h>
#include <stdbool.h>
+#include "array.h"
// string library taken from cuddlesOS:
// https://github.com/cuddlesOS/cuddles/blob/master/stage3/string.c
-// arr
-#define len(X) (sizeof X / sizeof *X)
-#define array(T) struct { size_t len; T *data; }
-
typedef array(char) str;
-#define S(X) ((str) { len(X)-1, X })
#define NILS ((str) { 0, NULL })
+#define S(X) ((str) { len(X)-1, X })
+#define PSTR(X) (int) (X).len, (X).data
-typedef struct { size_t cap; str buf; } strbuf;
-#define NILSBUF ((strbuf) { 0, NILS })
+typedef arraybuf(char) strbuf;
+#define NILSBUF ((strbuf) { 0, 0, NULL })
// compares two strings by length and ASCII values. return value:
// < 0 if s1 < s2