summaryrefslogtreecommitdiff
path: root/include/content.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/content.h')
-rw-r--r--include/content.h106
1 files changed, 83 insertions, 23 deletions
diff --git a/include/content.h b/include/content.h
index 664c84e..9d35dc8 100644
--- a/include/content.h
+++ b/include/content.h
@@ -7,9 +7,13 @@
#include <stdint.h>
#include <stdbool.h>
+#include "ser.h"
#include "vec.h"
-typedef enum {
+#define SIGHT_RANGE 10
+#define PKT_NODES_MAX (50*50)
+
+typedef enum : uint16_t {
N_VALLEY_FLOWER,
N_MOUNTAIN_FLOWER,
N_BIG_TREE,
@@ -22,16 +26,31 @@ typedef enum {
N_SAND,
} node_type;
+#define ser_node_type ser_u16
+#define deser_node_type deser_u16
+
typedef struct {
uint8_t r, g, b;
} color;
-static inline uint32_t color_to_u32(color c)
+[[maybe_unused]] static void ser_color(strbuf *w, color c)
+{
+ ser_u8(w, c.r);
+ ser_u8(w, c.g);
+ ser_u8(w, c.b);
+}
+
+[[maybe_unused]] static bool deser_color(str *r, color *c)
+{
+ return deser_u8(r, &c->r) && deser_u8(r, &c->g) && deser_u8(r, &c->b);
+}
+
+[[maybe_unused]] static uint32_t color_to_u32(color c)
{
return ((uint32_t) c.b) | (((uint32_t) c.g) << 8) | (((uint32_t) c.r) << 16);
}
-static inline color color_from_u32(uint32_t u)
+[[maybe_unused]] static color color_from_u32(uint32_t u)
{
return (color) { (u >> 16) & 0xFF, (u >> 8) & 0xFF, u & 0xFF };
}
@@ -43,51 +62,92 @@ typedef struct {
color col;
} node;
-#define SIGHT_RANGE 10
-#define NODES_PKT_MAX (50*50)
+[[maybe_unused]] static void ser_node(strbuf *w, node *n)
+{
+ bool present = n != NULL && n->present;
+ ser_bool(w, present);
+ if (!present)
+ return;
+ ser_node_type(w, n->type);
+ ser_i8(w, n->z);
+ ser_color(w, n->col);
+}
+
+[[maybe_unused]] static bool deser_node(str *r, node *n)
+{
+ if (!deser_bool(r, &n->present)) return false;
+ if (!n->present) return true;
+
+ return deser_node_type(r, &n->type)
+ && deser_i8(r, &n->z)
+ && deser_color(r, &n->col);
+}
-typedef enum {
+typedef enum : uint8_t {
DIR_RIGHT = 0,
DIR_UP,
DIR_LEFT,
DIR_DOWN,
} dir;
-static inline vec2 dir_to_vec2(dir d)
+#define ser_dir ser_u8
+#define deser_dir deser_u8
+
+[[maybe_unused]] static vec2 dir_to_vec2(dir d)
{
switch (d) {
- case DIR_RIGHT: return VEC2(0, 1);
- case DIR_UP: return VEC2(-1, 0);
- case DIR_LEFT: return VEC2(0, -1);
- case DIR_DOWN: return VEC2(1, 0);
+ case DIR_RIGHT: return VEC2(1, 0);
+ case DIR_UP: return VEC2(0, -1);
+ case DIR_LEFT: return VEC2(-1, 0);
+ case DIR_DOWN: return VEC2(0, 1);
+ default: return VEC2(0, 0);
}
}
-typedef uint8_t fail_reason;
+typedef enum : uint8_t {
+ FAIL_WRONG_PASS = 0,
+ FAIL_ALREADY_ONLINE,
+} fail_reason;
+
#define ser_fail_reason ser_u8
#define deser_fail_reason deser_u8
-enum {
- FAIL_WRONG_PASS = 0,
- FAIL_ALREADY_ONLINE,
-};
+typedef uint64_t entity_id;
+#define ser_entity_id ser_u64
+#define deser_entity_id deser_u64
-typedef uint16_t pkt_type;
-#define ser_pkt_type ser_u16
-#define deser_pkt_type deser_u16
+typedef enum : uint16_t {
+ ENTITY_PLAYER
+} entity_type;
+
+#define ser_entity_type ser_u16
+#define deser_entity_type deser_u16
+
+typedef enum : uint8_t {
+ ENTITY_ADD, // type
+ ENTITY_REMOVE,
+ ENTITY_MOVE, // x y
+} entity_cmd;
+
+#define ser_entity_cmd ser_u8
+#define deser_entity_cmd deser_u8
-enum {
+enum : uint16_t {
CPKT_HI = 0, // len motd
CPKT_FAIL, // fail_reason
CPKT_PLAYERS, // len [len name id]
- CPKT_MOVE, // player_id remove x y
+ CPKT_ENTITY, // len [entity_id command]
CPKT_NODES, // box2 [node]
CPKT_RESET_MAP,
};
-enum {
+enum : uint16_t {
SPKT_HI = 0, // len name len password
- SPKT_MOVE, // move_dir
+ SPKT_MOVE, // dir
};
+typedef uint16_t pkt_type;
+#define ser_pkt_type ser_u16
+#define deser_pkt_type deser_u16
+
#endif