diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/chunk.h | 24 | ||||
-rw-r--r-- | include/content.h | 33 | ||||
-rw-r--r-- | include/vec.h | 64 |
3 files changed, 112 insertions, 9 deletions
diff --git a/include/chunk.h b/include/chunk.h new file mode 100644 index 0000000..420211f --- /dev/null +++ b/include/chunk.h @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef CHUNK_H +#define CHUNK_H + +#include <stdbool.h> +#include "vec.h" +#include "content.h" + +typedef struct { + uint32_t pitch; + box2 bounds; + node *data; +} chunk; + +chunk chunk_alloc(box2 b); +node *chunk_index(chunk c, uvec2 v); +node *chunk_index_abs(chunk c, vec2 v); +void chunk_copy(chunk dst, chunk src); +void chunk_clear(chunk c); + +#endif diff --git a/include/content.h b/include/content.h index ed8b822..664c84e 100644 --- a/include/content.h +++ b/include/content.h @@ -7,6 +7,7 @@ #include <stdint.h> #include <stdbool.h> +#include "vec.h" typedef enum { N_VALLEY_FLOWER, @@ -25,12 +26,12 @@ typedef struct { uint8_t r, g, b; } color; -static inline uint32_t color_u32(color c) +static inline 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 u32_color(uint32_t u) +static inline color color_from_u32(uint32_t u) { return (color) { (u >> 16) & 0xFF, (u >> 8) & 0xFF, u & 0xFF }; } @@ -42,12 +43,25 @@ typedef struct { color col; } node; +#define SIGHT_RANGE 10 +#define NODES_PKT_MAX (50*50) + typedef enum { - MOVE_UP = 0, - MOVE_DOWN, - MOVE_LEFT, - MOVE_RIGHT, -} move_dir; + DIR_RIGHT = 0, + DIR_UP, + DIR_LEFT, + DIR_DOWN, +} dir; + +static inline 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); + } +} typedef uint8_t fail_reason; #define ser_fail_reason ser_u8 @@ -66,8 +80,9 @@ 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] + CPKT_MOVE, // player_id remove x y + CPKT_NODES, // box2 [node] + CPKT_RESET_MAP, }; enum { diff --git a/include/vec.h b/include/vec.h new file mode 100644 index 0000000..ce1b921 --- /dev/null +++ b/include/vec.h @@ -0,0 +1,64 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef VEC_H +#define VEC_H + +#include <stdint.h> +#include <stdbool.h> +#include "ser.h" + +#define VECFN static __attribute__((unused)) + +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define CHKSUB(A, B) ((A) > (B) ? (A) - (B) : 0) + +#define CAST(T, V) ((T) { (V).x, (V).y }) + +#define MKVEC(V, B, S, SER) \ +typedef struct { S x; S y; } V; \ +typedef struct { V pos; uvec2 size; } B; \ +VECFN V V##_add(V a, V b) { return (V) { a.x+b.x, a.y+b.y }; } \ +VECFN V V##_sub(V a, V b) { return (V) { a.x-b.x, a.y-b.y }; } \ +VECFN uvec2 V##_chksub(V a, V b) { return (uvec2) { CHKSUB(a.x, b.x), CHKSUB(a.y, b.y) }; } \ +VECFN V V##_min(V a, V b) { return (V) { MIN(a.x, b.x), MIN(a.y, b.y) }; } \ +VECFN V V##_max(V a, V b) { return (V) { MAX(a.x, b.x), MAX(a.y, b.y) }; } \ +VECFN bool V##_eq(V a, V b) { return a.x == b.x && a.y == b.y; } \ +VECFN bool V##_le(V a, V b) { return a.x <= b.x && a.y <= b.y; } \ +VECFN bool V##_lt(V a, V b) { return a.x < b.x && a.y < b.y; } \ +VECFN void ser_##V(strbuf *w, V v) { ser_##SER(w, v.x); ser_##SER(w, v.y); } \ +VECFN bool deser_##V(str *r, V *v) { return deser_##SER(r, &v->x) && deser_##SER(r, &v->y); } \ +VECFN void ser_##B(strbuf *w, B b) { ser_##V(w, b.pos); ser_uvec2(w, b.size); } \ +VECFN bool deser_##B(str *r, B *b) { return deser_##V(r, &b->pos) && deser_uvec2(r, &b->size); } \ +VECFN V B##_upper(B b) { return V##_add(b.pos, CAST(V, b.size)); } \ +VECFN B B##_overlap(B a, B b) { V base = V##_max(a.pos, b.pos); \ + return (B) { base, V##_chksub(V##_min(B##_upper(a), B##_upper(b)), base) }; } \ +VECFN bool B##_empty(B b) { return b.size.x == 0 || b.size.y == 0; } \ +VECFN bool B##_contains(B b, V v) { return V##_le(b.pos, v) && V##_lt(v, B##_upper(b)); } \ +VECFN B B##_around(V center, uint32_t radius) { uint32_t diam = radius*2+1; \ + return (B) { V##_sub(center, (V) { radius, radius }), (uvec2) { diam, diam } }; } + +MKVEC(uvec2, ubox2, uint32_t, u32) +MKVEC(vec2, box2, int32_t, i32) + +#define VEC2(X, Y) ((vec2) { (X), (Y) }) +#define UVEC2(X, Y) ((uvec2) { (X), (Y) }) + +#define CUVEC2(V) ((uvec2) { (V).x, (V).y }) +#define CVEC2(V) ((vec2) { (V).x, (V).y }) + +VECFN vec2 vec2_neg(vec2 v) +{ + return VEC2(-v.x, -v.y); +} + +#undef MKVEC +#undef VECFN +#undef MIN +#undef MAX +#undef CHKSUB +#undef CAST + +#endif |