diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-07-13 14:51:54 +0200 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-07-13 14:57:57 +0200 |
commit | 60e11c18b8ac11645c86eb80ae6a7e8555815f53 (patch) | |
tree | 26b172ef2708fe0efe8c3e2b3787947ecee6c5d1 /src/server.c | |
parent | 1c53c39feed656194daf2fc24ddc4d2ee6abf728 (diff) |
add ARR_ITER
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/server.c')
-rw-r--r-- | src/server.c | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/src/server.c b/src/server.c index bab8c03..db599c7 100644 --- a/src/server.c +++ b/src/server.c @@ -76,17 +76,19 @@ typedef struct { 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]; + ARR_ITER(g->players, p) if (p->id == id) return p; + return NULL; +} + +player *player_from_name(game *g, str name) +{ + ARR_ITER(g->players, p) if (p->auth && str_eq(p->name, name)) return p; return NULL; } srv_entity *entity_from_id(game *g, entity_id id) { - for (size_t i = 0; i < g->entities.len; i++) - if (g->entities.data[i].base.id == id) - return &g->entities.data[i]; + ARR_ITER(g->entities, e) if (e->base.id == id) return e; return NULL; } @@ -158,16 +160,12 @@ static entity_cmd moved_check_update(srv_entity *observer, srv_entity *target, s void moved_entity(game *g, srv_entity *moved, maybe_pos before, maybe_pos after) { - for (size_t i = 0; i < g->players.len; i++) { - player *p = &g->players.data[i]; - if (!p->auth) continue; - + ARR_ITER(g->players, p) if (p->auth) { srv_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++) { - srv_entity *target = &g->entities.data[j]; + ARR_ITER(g->entities, target) { entity_cmd cmd = moved_check_update(observer, target, moved, before, after); if (cmd != ENTITY_CMD_COUNT) ser_entity_update(&pkt, target, cmd); @@ -183,8 +181,7 @@ void moved_entity(game *g, srv_entity *moved, maybe_pos before, maybe_pos after) node *map_node(game *g, vec2 p, int8_t z) { - for (size_t i = 0; i < g->maps.len; i++) { - map *m = &g->maps.data[i]; + ARR_ITER(g->maps, m) { if (m->z != z) continue; node *n = chunk_index_abs(m->chunk, p); @@ -336,13 +333,10 @@ bool map_load(game *g, map *m) void send_players(game *g) { - for (size_t i = 0; i < g->players.len; i++) { - player *to = &g->players.data[i]; - if (to->auth) SEND_PKT(to->conn, CPKT_PLAYERS, + ARR_ITER(g->players, to) if (to->auth) { + SEND_PKT(to->conn, CPKT_PLAYERS, ser_u16(&pkt, g->players.len); - for (size_t i = 0; i < g->players.len; i++) { - player *p = &g->players.data[i]; - if (!p->auth) continue; + ARR_ITER(g->players, p) if (p->auth) { ser_str(&pkt, p->name); ser_entity_id(&pkt, p->id); } @@ -388,14 +382,13 @@ void game_exit(game *g, int ret) free(g->motd.data); free(g->passphrase.data); - for (size_t i = 0; i < g->maps.len; i++) { - free(g->maps.data[i].name.data); - free(g->maps.data[i].chunk.data); + ARR_ITER(g->maps, m) { + free(m->name.data); + free(m->chunk.data); } free(g->maps.data); - for (size_t i = 0; i < g->players.len; i++) - player_cleanup(&g->players.data[i]); + ARR_ITER(g->players, p) player_cleanup(p); free(g->players.data); exit(ret); @@ -413,12 +406,9 @@ bool handle_hi(str pkt, player *p, game *g) return true; // valid pkt, but invalid passphrase } - for (size_t i = 0; i < g->players.len; i++) { - player *p2 = &g->players.data[i]; - if (p2->auth && str_eq(p2->name, name)) { - SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);) - return true; - } + if (player_from_name(g, name)) { + SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_ALREADY_ONLINE);) + return true; } printf("player authenticated as %.*s\n", PSTR(name)); |