summaryrefslogtreecommitdiff
path: root/src/content.h
blob: ef27e22bf799eeb20498fa7dd50dfe371be8a706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

#ifndef CONTENT_H
#define CONTENT_H

#include <stdint.h>
#include <stdbool.h>
#include "ser.h"
#include "vec.h"

#define SIGHT_RANGE 15
#define PKT_NODES_MAX (50*50)

typedef enum : uint16_t {
	N_BIG_TREE = 0,
	N_NEEDLE_TREE,
	N_ROCK,
	N_WATER,
	N_PLANK,
	N_PATH,
	N_GRASS,
	N_SAND,
} node_type;

#define ser_node_type ser_u16
#define deser_node_type deser_u16

typedef struct {
	uint8_t r, g, b;
} color;

[[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);
}

[[maybe_unused]] static color color_from_u32(uint32_t u)
{
	return (color) { (u >> 16) & 0xFF, (u >> 8) & 0xFF, u & 0xFF };
}

typedef enum : uint8_t {
	FLOWER_ROSE = 0,
	FLOWER_HIBISCUS,
	FLOWER_SUNFLOWER,
	FLOWER_TULIP,
	FLOWER_DANDELION, // im in a field of dandelions...
	FLOWER_COUNT,
} flower_type;

#define ser_flower_type ser_u8
#define deser_flower_type deser_u8

typedef struct {
	bool present;
	node_type type;
	int8_t z; // for rocks, indicates rock level
	color col;
	uint8_t variant;
} node;

[[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);
	ser_u8(w, n->variant);
}

[[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)
		&& deser_u8(r, &n->variant);
}

typedef enum : uint8_t {
	DIR_RIGHT = 0,
	DIR_UP,
	DIR_LEFT,
	DIR_DOWN,
} dir;

#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(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 enum : uint8_t {
	FAIL_WRONG_PASS = 0,
	FAIL_ALREADY_ONLINE,
} fail_reason;

#define ser_fail_reason ser_u8
#define deser_fail_reason deser_u8

typedef uint64_t entity_id;
#define ser_entity_id ser_u64
#define deser_entity_id deser_u64

typedef enum : uint16_t {
	ENTITY_PLAYER = 0,
	ENTITY_FLOWER,
} entity_type;

#define ser_entity_type ser_u16
#define deser_entity_type deser_u16

typedef enum : uint8_t {
	ENTITY_ADD = 0, // type
	ENTITY_REMOVE,
	ENTITY_MOVE, // x y
	ENTITY_CMD_COUNT,
} entity_cmd;

#define ser_entity_cmd ser_u8
#define deser_entity_cmd deser_u8

typedef struct {
	entity_id id;
	entity_type type;
	vec2 pos;
	union {
		flower_type flower;
	};
} entity;

enum : uint16_t {
	CPKT_HI = 0, // len motd
	CPKT_FAIL, // fail_reason
	CPKT_PLAYERS, // len [len name id]
	CPKT_ENTITY, // len [entity_id command]
	CPKT_NODES, // box2 [node]
	CPKT_RESET_MAP,
};

enum : uint16_t {
	SPKT_HI = 0, // len name len password
	SPKT_MOVE, // dir
};

typedef uint16_t pkt_type;
#define ser_pkt_type ser_u16
#define deser_pkt_type deser_u16

#endif