diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-07-13 12:47:36 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-07-13 12:47:36 +0200 |
commit | 95283d70eee749d984b2ca07c0b61d39f9bb350d (patch) | |
tree | e4d666a4c58f4877bf619e826f6697cd4914bf9a /src/server.c | |
parent | f7b1199a46f4034a49b624afa0d0b74535fa9623 (diff) |
implement entities
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/server.c')
-rw-r--r-- | src/server.c | 187 |
1 files changed, 154 insertions, 33 deletions
diff --git a/src/server.c b/src/server.c index 974e396..108ae0d 100644 --- a/src/server.c +++ b/src/server.c @@ -44,8 +44,7 @@ typedef struct { bool auth; str name; entity_id id; - vec2 pos; - int8_t z; + int8_t z; // TODO: move to entity peer conn; } player; @@ -55,9 +54,109 @@ typedef struct { str passphrase; array(map) maps; arraybuf(player) players; + arraybuf(entity) entities; entity_id next_entity; } game; +player *player_from_id(game *g, entity_id id) +{ + for (size_t i = 0; i < g->players.len; i++) + if (g->players.data[i].id == id) + return &g->players.data[i]; + return NULL; +} + +entity *entity_from_id(game *g, entity_id id) +{ + for (size_t i = 0; i < g->entities.len; i++) + if (g->entities.data[i].id == id) + return &g->entities.data[i]; + return NULL; +} + +box2 player_sight(vec2 center) +{ + return box2_around(center, SIGHT_RANGE); +} + +void ser_entity_update(strbuf *w, entity *e, entity_cmd cmd) +{ + ser_entity_id(w, e->id); + ser_entity_cmd(w, cmd); + + switch (cmd) { + case ENTITY_ADD: + ser_entity_type(w, e->type); + ser_vec2(w, e->pos); + switch (e->type) { + case ENTITY_FLOWER: + ser_flower_type(w, e->flower); + break; + default: break; + } + break; + case ENTITY_MOVE: + ser_vec2(w, e->pos); + break; + case ENTITY_REMOVE: + break; + case ENTITY_CMD_COUNT: + break; // unreachable + } +} + +static vec2 *moved_get_pos(entity *e, entity *moved, vec2 *p_moved) +{ + return (e == moved) ? p_moved : &e->pos; +} + +static bool moved_can_see(entity *a, entity *b, entity *moved, vec2 *p_moved) +{ + vec2 *p_a = moved_get_pos(a, moved, p_moved); + vec2 *p_b = moved_get_pos(b, moved, p_moved); + return p_a && p_b && box2_contains(player_sight(*p_a), *p_b); +} + +static entity_cmd moved_check_update(entity *observer, entity *target, entity *moved, vec2 *before, vec2 *after) +{ + bool see_before = moved_can_see(observer, target, moved, before); + bool see_after = moved_can_see(observer, target, moved, after); + + if (see_before && !see_after) + return ENTITY_REMOVE; + else if (!see_before && see_after) + return ENTITY_ADD; + else if (see_before && see_after && target == moved) + return ENTITY_MOVE; + else + return ENTITY_CMD_COUNT; +} + +void moved_entity(game *g, entity *moved, vec2 *before, vec2 *after) +{ + for (size_t i = 0; i < g->players.len; i++) { + player *p = &g->players.data[i]; + if (!p->auth) continue; + + entity *observer = entity_from_id(g, p->id); + + if (observer == moved) { + SEND_PKT(p->conn, CPKT_ENTITY, + for (size_t j = 0; j < g->entities.len; j++) { + entity *target = &g->entities.data[j]; + entity_cmd cmd = moved_check_update(observer, target, moved, before, after); + if (cmd != ENTITY_CMD_COUNT) + ser_entity_update(&pkt, target, cmd); + } + ) + } else { + entity_cmd cmd = moved_check_update(observer, moved, moved, before, after); + if (cmd != ENTITY_CMD_COUNT) + SEND_PKT(p->conn, CPKT_ENTITY, ser_entity_update(&pkt, moved, cmd);) + } + } +} + node *map_node(game *g, vec2 p, int8_t z) { for (size_t i = 0; i < g->maps.len; i++) { @@ -84,10 +183,24 @@ bool silly_noise(vec2 pos, uint32_t grid, double sillyness, int seed) y += round(noise2d(y/grid, x/grid, 0, seed) * sillyness); return x % grid == 0 && y % grid == 0; -#undef SMOD } -void map_load_node(map *m, uvec2 v, color col) +entity *spawn_entity(game *g, entity_type type, vec2 pos) +{ + entity *e = ARR_APPEND(g->entities); + e->id = g->next_entity++; + e->type = type; + e->pos = pos; + return e; +} + +void remove_entity(game *g, entity *e) +{ + moved_entity(g, e, &e->pos, NULL); + ARR_REMOVE(g->entities, e); +} + +void map_load_node(game *g, map *m, uvec2 v, color col) { uint32_t ucol = color_to_u32(col); @@ -101,20 +214,21 @@ void map_load_node(map *m, uvec2 v, color col) return; switch (ucol) { + case 0xff9de2: + spawn_entity(g, ENTITY_FLOWER, pos)->flower = rand() % FLOWER_COUNT; + [[fallthrough]]; case 0x00880d: n->type = N_GRASS; n->variant = silly_noise(pos, 3, 1.0, 0); break; case 0x595959: n->type = N_ROCK; n->z = 1; break; + case 0x001b51: + spawn_entity(g, ENTITY_FLOWER, pos)->flower = FLOWER_DANDELION; + [[fallthrough]]; case 0x484848: n->type = N_ROCK; n->z = 2; break; + case 0x00ffe8: + spawn_entity(g, ENTITY_FLOWER, pos)->flower = FLOWER_DANDELION; + [[fallthrough]]; case 0x373737: n->type = N_ROCK; n->z = 3; break; - case 0x00ffe8: n->type = N_MOUNTAIN_FLOWER; - n->variant = FLOWER_DANDELION; - n->z = 3; - break; - case 0x001b51: n->type = N_MOUNTAIN_FLOWER; - n->variant = FLOWER_DANDELION; - n->z = 2; - break; case 0xa09700: n->type = N_PATH; break; case 0x015100: n->type = N_NEEDLE_TREE; break; case 0x00c6ff: n->type = N_WATER; @@ -122,9 +236,6 @@ void map_load_node(map *m, uvec2 v, color col) n->z = -1; break; case 0x5c3b12: n->type = N_PLANK; break; - case 0xff9de2: n->type = N_VALLEY_FLOWER; - n->variant = rand() % FLOWER_COUNT; - break; case 0x016300: n->type = N_BIG_TREE; break; case 0xfce84e: n->type = N_SAND; n->variant = silly_noise(pos, 4, 1.0, 1); @@ -137,7 +248,7 @@ void map_load_node(map *m, uvec2 v, color col) } } -bool map_load(map *m) +bool map_load(game *g, map *m) { #define TRY(expr, ...) if (!(expr)) { \ fprintf(stderr, __VA_ARGS__); \ @@ -188,7 +299,7 @@ bool map_load(map *m) } }*/ - map_load_node(m, UVEC2(x, y), (color) { p[0], p[1], p[2] }); + map_load_node(g, m, UVEC2(x, y), (color) { p[0], p[1], p[2] }); } } @@ -225,7 +336,7 @@ void send_nodes(player *p, game *g, box2 bounds, int8_t z) ) } -void player_free(player *p) +void player_cleanup(player *p) { peer_free(&p->conn); if (p->auth) @@ -234,11 +345,17 @@ void player_free(player *p) void player_remove(player *p, game *g) { - player_free(p); + player_cleanup(p); bool auth = p->auth; + entity_id id = p->id; + ARR_REMOVE(g->players, p); - if (auth) send_players(g); + + if (auth) { + remove_entity(g, entity_from_id(g, id)); + send_players(g); + } } void game_exit(game *g, int ret) @@ -253,7 +370,7 @@ void game_exit(game *g, int ret) free(g->maps.data); for (size_t i = 0; i < g->players.len; i++) - player_free(&g->players.data[i]); + player_cleanup(&g->players.data[i]); free(g->players.data); exit(ret); @@ -283,18 +400,23 @@ bool handle_hi(str pkt, player *p, game *g) p->auth = true; p->name = str_clone(name); - p->pos.x = p->pos.y = 0; + + entity *e = ARR_APPEND(g->entities); + e->id = p->id; + e->type = ENTITY_PLAYER; + e->pos.x = e->pos.y = 0; p->z = 0; SEND_PKT(p->conn, CPKT_HI, ser_str(&pkt, g->motd);) send_players(g); - send_nodes(p, g, box2_around(p->pos, SIGHT_RANGE), p->z); + moved_entity(g, e, NULL, &e->pos); + send_nodes(p, g, player_sight(e->pos), p->z); return true; } -bool handle_move(str pkt, player *p, [[maybe_unused]] game *g) +bool handle_move(str pkt, player *p, game *g) { dir d; if (!deser_dir(&pkt, &d)) return false; @@ -303,17 +425,16 @@ bool handle_move(str pkt, player *p, [[maybe_unused]] game *g) if (vec2_iszero(v)) return false; + entity *e = entity_from_id(g, p->id); + // 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); - ) + vec2 old_pos = e->pos; + e->pos = vec2_add(v, e->pos); + + moved_entity(g, e, &old_pos, &e->pos); // TODO: only send newly visible nodes - send_nodes(p, g, box2_around(p->pos, SIGHT_RANGE), p->z); + send_nodes(p, g, player_sight(e->pos), p->z); return true; } @@ -355,7 +476,7 @@ int main() .chunk = chunk_alloc((box2) { { -50, -50 }, { 100, 100 } }), }; - if (!map_load(&g.maps.data[0])) + if (!map_load(&g, &g.maps.data[0])) game_exit(&g, EXIT_FAILURE); if ((g.accept_fd = socket_create("0.0.0.0", "4560", true)) < 0) |