summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/content.h49
-rw-r--r--include/peer.h40
-rw-r--r--include/str.h57
3 files changed, 146 insertions, 0 deletions
diff --git a/include/content.h b/include/content.h
new file mode 100644
index 0000000..ac20e7e
--- /dev/null
+++ b/include/content.h
@@ -0,0 +1,49 @@
+// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+#ifndef CONTENT_H
+#define CONTENT_H
+
+typedef enum {
+ N_VALLEY_FLOWER,
+ N_MOUNTAIN_FLOWER,
+ N_BIG_TREE,
+ N_NEEDLE_TREE,
+ N_ROCK,
+ N_WATER,
+ N_PLANK,
+ N_PATH,
+} node_type;
+
+typedef struct {
+ uint8_t r, g, b;
+} color;
+
+typedef struct {
+ bool present;
+ node_type type;
+ int8_t z; // for rocks, indicates rock level
+ color col;
+} node;
+
+typedef enum {
+ MOVE_UP,
+ MOVE_DOWN,
+ MOVE_LEFT,
+ MOVE_RIGHT,
+} move_dir;
+
+typedef enum {
+ CPKT_HI, // len motd
+ CPKT_PLAYERS, // len
+ 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
+ SPKT_MOVE, // move_dir
+} server_pkt;
+
+#endif
diff --git a/include/peer.h b/include/peer.h
new file mode 100644
index 0000000..63de9e5
--- /dev/null
+++ b/include/peer.h
@@ -0,0 +1,40 @@
+// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+#ifndef PEER_H
+#define PEER_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stddef.h>
+
+#define PEER_INBUFFER_SIZE 0x100000 // 1MB
+#define PEER_OUTBUFFER_SIZE 0x200000 // 2MB
+
+typedef uint32_t pkt_header;
+
+typedef struct {
+ int socket;
+ bool disco;
+ struct {
+ bool header;
+ size_t len;
+ size_t promised;
+ uint8_t *buffer;
+ } in;
+ // TODO: ring buffer
+ struct {
+ size_t avail;
+ size_t cursor;
+ uint8_t *buffer;
+ } out;
+} peer;
+
+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);
+
+#endif
diff --git a/include/str.h b/include/str.h
new file mode 100644
index 0000000..f4054a1
--- /dev/null
+++ b/include/str.h
@@ -0,0 +1,57 @@
+// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+#ifndef STR_H
+#define STR_H
+
+#include <stddef.h>
+#include <stdbool.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 })
+
+// compares two strings by length and ASCII values. return value:
+// < 0 if s1 < s2
+// = 0 if s1 = s2
+// > 0 if s1 > s2
+int str_cmp(str s1, str s2);
+
+// returns index of first of occurrence in s of any of the chars in tokens
+// returns length of s if not found
+size_t str_find(str s, str tokens);
+
+// this is a splitting function
+// returns the next non-empty substring of *s that is delimited by the tokens in sep
+// the returned string does not contain any of the separators
+// returns an emtpy string when end is reached
+// advances s to after the substring plus first delimiting token
+str str_walk(str *s, str sep);
+
+// advances the string while its first token matches any of the chars in tokens
+// this can be used to consume whitespace, for example
+str str_eat(str s, str tokens);
+
+// advances the string s by x chars, increasing the data pointer and decreasing the length
+// note: this is not bounds checked
+str str_advance(str s, size_t x);
+
+// returns true if s starts with start
+bool str_start(str s, str start);
+
+// construct a str from a \0 terminated string at runtime
+// avoid this for literals: use the S macro instead without a runtime cost
+str str_intro(char *c);
+
+// copy a string to the heap
+str str_clone(str s);
+
+#endif