From 3be565f4e8ee1610aae5b0945185b4012f026c87 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Sun, 23 Jun 2024 20:07:44 +0200 Subject: implement movement Signed-off-by: Lizzy Fleckenstein --- src/server.c | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'src/server.c') diff --git a/src/server.c b/src/server.c index 45d8384..30b5676 100644 --- a/src/server.c +++ b/src/server.c @@ -41,7 +41,7 @@ typedef struct { typedef struct { bool auth; str name; - uint64_t id; + entity_id id; vec2 pos; int8_t z; peer conn; @@ -53,7 +53,7 @@ typedef struct { str passphrase; array(map) maps; arraybuf(player) players; - uint64_t entity_id; + entity_id next_entity; } game; node *map_node(game *g, vec2 p, int8_t z) @@ -163,23 +163,6 @@ bool map_load(map *m) #undef TRY } -void ser_color(strbuf *w, color c) -{ - ser_u8(w, c.r); - ser_u8(w, c.g); - ser_u8(w, c.b); -} - -void ser_node(strbuf *w, node *n) -{ - bool present = n != NULL && n->present; - ser_bool(w, present); - if (!present) - return; - ser_i8(w, n->z); - ser_color(w, n->col); -} - void send_players(game *g) { for (size_t i = 0; i < g->players.len; i++) { @@ -190,7 +173,7 @@ void send_players(game *g) player *p = &g->players.data[i]; if (!p->auth) continue; ser_str(&pkt, p->name); - ser_u64(&pkt, p->id); + ser_entity_id(&pkt, p->id); } ) } @@ -276,6 +259,30 @@ bool handle_hi(str pkt, player *p, game *g) return true; } +bool handle_move(str pkt, player *p, [[maybe_unused]] game *g) +{ + dir d; + if (!deser_dir(&pkt, &d)) return false; + + vec2 v = dir_to_vec2(d); + if (vec2_iszero(v)) + return false; + + // yolo + p->pos = vec2_add(v, p->pos); + SEND_PKT(p->conn, CPKT_ENTITY, + ser_u16(&pkt, 1); + ser_entity_id(&pkt, p->id); + ser_entity_cmd(&pkt, ENTITY_MOVE); + ser_vec2(&pkt, p->pos); + ) + + // TODO: only send newly visible nodes + send_nodes(p, g, box2_around(p->pos, SIGHT_RANGE), p->z); + + return true; +} + bool handle_pkt(str pkt, player *p, game *g) { pkt_type type; @@ -287,6 +294,7 @@ bool handle_pkt(str pkt, player *p, game *g) switch (type) { case SPKT_HI: return handle_hi(pkt, p, g); + case SPKT_MOVE: return handle_move(pkt, p, g); default: return false; } } @@ -298,7 +306,7 @@ int main() game g = {0}; g.accept_fd = -1; - g.entity_id = 1; + g.next_entity = 1; g.motd = str_clone(S("Welcome to test server")); g.passphrase = str_clone(S("")); @@ -359,7 +367,7 @@ int main() printf("new player\n"); player *p = ARR_APPEND(g.players); p->auth = false; - p->id = g.entity_id++; + p->id = g.next_entity++; p->name = S("(unauthenticated)"); peer_init(&p->conn, socket, &p->name); } -- cgit v1.2.3