diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/array.h | 2 | ||||
-rw-r--r-- | src/client.c | 26 | ||||
-rw-r--r-- | src/server.c | 52 |
3 files changed, 31 insertions, 49 deletions
diff --git a/src/array.h b/src/array.h index 9affdde..e50ecd4 100644 --- a/src/array.h +++ b/src/array.h @@ -17,5 +17,7 @@ #define ARR_APPEND(A) (((A).cap == (A).len) \ ? (A).data = realloc((A).data, sizeof *(A).data * ((A).cap = (A).cap ? (A).cap * 2 : 1)) \ : NULL, &((A).data)[(A).len++]) +#define ARR_ITER(A, I) \ + for (typeof((A).data) (I) = (A).data; (size_t) ((I) - (A).data) < (A).len; (I)++) #endif diff --git a/src/client.c b/src/client.c index 679fcd4..608eeaa 100644 --- a/src/client.c +++ b/src/client.c @@ -37,17 +37,13 @@ typedef struct { player *player_from_id(client *c, entity_id id) { - for (size_t i = 0; i < c->players.len; i++) - if (c->players.data[i].id == id) - return &c->players.data[i]; + ARR_ITER(c->players, p) if (p->id == id) return p; return NULL; } entity *entity_from_id(client *c, entity_id id) { - for (size_t i = 0; i < c->entities.len; i++) - if (c->entities.data[i].id == id) - return &c->entities.data[i]; + ARR_ITER(c->entities, e) if (e->id == id) return e; return NULL; } @@ -168,8 +164,7 @@ void gfx_render(client *c, uint64_t dtime) } // entities - for (size_t i = 0; i < c->entities.len; i++) { - entity *e = &c->entities.data[i]; + ARR_ITER(c->entities, e) { if (!box2_contains(c->map.bounds, e->pos)) // just to be safe... continue; vec2 pos = vec2_sub(e->pos, c->map.bounds.pos); @@ -207,9 +202,7 @@ void gfx_render(client *c, uint64_t dtime) void free_players(client *c) { - for (size_t i = 0; i < c->players.len; i++) - free(c->players.data[i].name.data); - + ARR_ITER(c->players, p) free(p->name.data); free(c->players.data); c->players.len = 0; @@ -271,14 +264,11 @@ void map_set_center(client *c, vec2 center) chunk_clear(c->map); chunk_copy(c->map, old); - for (size_t i = 0; i < c->entities.len;) { - entity *e = &c->entities.data[i]; - if (box2_contains(c->map.bounds, e->pos)) { - i++; - continue; + ARR_ITER(c->entities, e) { + if (!box2_contains(c->map.bounds, e->pos)) { + ARR_REMOVE(c->entities, e); + e--; } - - ARR_REMOVE(c->entities, e); } } 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)); |