summaryrefslogtreecommitdiff
path: root/src/client.c
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:47:36 +0200
committerLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:47:36 +0200
commit95283d70eee749d984b2ca07c0b61d39f9bb350d (patch)
treee4d666a4c58f4877bf619e826f6697cd4914bf9a /src/client.c
parentf7b1199a46f4034a49b624afa0d0b74535fa9623 (diff)
implement entities
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/client.c')
-rw-r--r--src/client.c234
1 files changed, 143 insertions, 91 deletions
diff --git a/src/client.c b/src/client.c
index 78511c6..679fcd4 100644
--- a/src/client.c
+++ b/src/client.c
@@ -26,27 +26,35 @@ typedef struct {
str pass;
entity_id self_id;
array(player) players;
+ arraybuf(entity) entities;
str server_motd;
chunk map;
node *map_swap;
- vec2 player_pos;
- char *termbuf_buf;
- size_t termbuf_size;
- FILE *termbuf;
+ char *frame_buf;
+ size_t frame_size;
+ FILE *frame;
} client;
-void gfx_alt_buffer(bool enable)
+player *player_from_id(client *c, entity_id id)
{
- if (enable)
- printf("\e[?1049h\e[?25l");
- else
- printf("\e[?1049l\e[?25h");
- fflush(stdout);
+ for (size_t i = 0; i < c->players.len; i++)
+ if (c->players.data[i].id == id)
+ return &c->players.data[i];
+ return NULL;
}
-void gfx_clear_effects(client *c)
+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];
+ return NULL;
+}
+
+void gfx_alt_buffer(bool enable)
{
- fprintf(c->termbuf, "\e[0m");
+ str out = enable ? S("\e[?1049h\e[?25l") : S("\e[?1049l\e[?25h");
+ write(STDOUT_FILENO, out.data, out.len);
}
typedef struct {
@@ -55,83 +63,56 @@ typedef struct {
color old;
} color_layer;
+void gfx_clear_effects(client *c)
+{
+ fprintf(c->frame, "\e[0m");
+}
+
void gfx_set_color(client *c, color_layer *l, color col)
{
if (l->has_old && l->old.r == col.r && l->old.g == col.g && l->old.b == col.b)
return;
- fprintf(c->termbuf, "\e[%u;2;%u;%u;%um", l->bg ? 48 : 38, col.r, col.g, col.b);
+ fprintf(c->frame, "\e[%u;2;%u;%u;%um", l->bg ? 48 : 38, col.r, col.g, col.b);
l->has_old = true;
l->old = col;
}
void gfx_clear(client *c)
{
- fprintf(c->termbuf, "\e[2J");
+ fprintf(c->frame, "\e[2J");
}
#define C color_from_u32
-#define DISPLAY_BG(X) (node_display) { X, { 0 }, NULL }
-#define DISPLAY(...) (node_display) { __VA_ARGS__ }
+#define DISPLAY(...) (tile_display) { __VA_ARGS__ }
+#define DISPLAY_BG(X) (tile_display) { X, { 0 }, NULL }
#define DISPLAY_ERR DISPLAY(C(0xff0000), C(0x000000), " ?")
typedef struct {
color bg;
color fg;
char *texture;
-} node_display;
-
-bool gfx_rock_bg(node *n, color *c)
-{
- switch (n->z) {
- case 1: *c = C(0x595959); return true;
- case 2: *c = C(0x484848); return true;
- case 3: *c = C(0x373737); return true;
- default: return false;
- }
-}
-
-color gfx_grass_bg(vec2 pos)
-{
- return C(0x00880d);
-}
+} tile_display;
bool gfx_water_wave(vec2 pos)
{
return true;
}
-node_display gfx_render_node(client *c, vec2 pos, node *n)
+tile_display gfx_render_node(client *c, vec2 pos, node *n)
{
if (!n->present)
return DISPLAY(C(0x555555), C(0xffffff), " ?");
switch (n->type) {
- case N_VALLEY_FLOWER:
- case N_MOUNTAIN_FLOWER: {
- node_display d = {0};
- if (n->type == N_VALLEY_FLOWER)
- d.bg = gfx_grass_bg(pos);
- else if (!gfx_rock_bg(n, &d.bg))
- return DISPLAY_ERR;
- switch (n->variant) {
- case FLOWER_ROSE: d.texture = "🌹"; break;
- case FLOWER_HIBISCUS: d.texture = "🌺"; break;
- case FLOWER_SUNFLOWER: d.texture = "🌻"; break;
- case FLOWER_DANDELION: d.texture = "🌼"; break;
- case FLOWER_TULIP: d.texture = "🌷"; break;
- default: return DISPLAY_ERR;
- }
- return d;
- }
- case N_ROCK: {
- node_display d = {0};
- if (!gfx_rock_bg(n, &d.bg))
- return DISPLAY_ERR;
- return d;
+ case N_ROCK: switch (n->z) {
+ case 1: return DISPLAY_BG(C(0x595959));
+ case 2: return DISPLAY_BG(C(0x484848));
+ case 3: return DISPLAY_BG(C(0x373737));
+ default: return DISPLAY_ERR;
}
case N_WATER: return DISPLAY(C(0x00c6ff), C(0x0d79de),
n->variant && gfx_water_wave(pos) ? " ~" : NULL);
- case N_GRASS: return DISPLAY(gfx_grass_bg(pos), C(0x1c571e), n->variant ? " M" : NULL);
+ case N_GRASS: return DISPLAY(C(0x00880d), C(0x1c571e), n->variant ? " M" : NULL);
case N_SAND: return DISPLAY(C(0xded485), C(0xe3a112), n->variant ? " ~" : NULL);
case N_NEEDLE_TREE: return DISPLAY_BG(C(0x015100));
case N_PATH: return DISPLAY_BG(C(0xa09700));
@@ -141,49 +122,87 @@ node_display gfx_render_node(client *c, vec2 pos, node *n)
}
}
+char *gfx_render_entity(client *c, entity *e)
+{
+ switch (e->type) {
+ case ENTITY_PLAYER:
+ return e->id == c->self_id ? "🙂" : "😀";
+ case ENTITY_FLOWER: switch (e->flower) {
+ case FLOWER_ROSE: return "🌹";
+ case FLOWER_HIBISCUS: return "🌺";
+ case FLOWER_SUNFLOWER: return "🌻";
+ case FLOWER_DANDELION: return "🌼";
+ case FLOWER_TULIP: return "🌷";
+ default: return NULL;
+ }
+ default: return NULL;
+ }
+}
+
#undef C
#undef DISPLAY
#undef DISPLAY_ERR
void gfx_render(client *c, uint64_t dtime)
{
- rewind(c->termbuf);
+ rewind(c->frame);
gfx_clear(c);
- fprintf(c->termbuf, "\e[H");
- fprintf(c->termbuf, "%.*s\n", PSTR(c->server_motd));
- for (size_t i = 0; i < c->players.len; i++)
- fprintf(c->termbuf, "%.*s\n", PSTR(c->players.data[i].name));
color_layer fg = {0};
color_layer bg = {0};
bg.bg = true;
- for (size_t y = 0; y < c->map.bounds.size.y; y++) {
- for (size_t x = 0; x < c->map.bounds.size.y; x++) {
- uvec2 off = UVEC2(x, y);
- vec2 pos = vec2_add(c->map.bounds.pos, CVEC2(off));
+ // HUD
+ fprintf(c->frame, "\e[H");
+ fprintf(c->frame, "%.*s\n", PSTR(c->server_motd));
+ for (size_t i = 0; i < c->players.len; i++)
+ fprintf(c->frame, "%.*s\n", PSTR(c->players.data[i].name));
+
+ // map
+ tile_display tiles[c->map.bounds.size.x][c->map.bounds.size.y];
+ for (size_t x = 0; x < c->map.bounds.size.x; x++)
+ for (size_t y = 0; y < c->map.bounds.size.x; y++) {
+ uvec2 off = UVEC2(x, y);
+ vec2 pos = vec2_add(c->map.bounds.pos, CVEC2(off));
+ tiles[x][y] = gfx_render_node(c, pos, chunk_index(c->map, off));
+ }
- node_display dis = gfx_render_node(c, pos, chunk_index(c->map, off));
+ // entities
+ for (size_t i = 0; i < c->entities.len; i++) {
+ entity *e = &c->entities.data[i];
+ if (!box2_contains(c->map.bounds, e->pos)) // just to be safe...
+ continue;
+ vec2 pos = vec2_sub(e->pos, c->map.bounds.pos);
+ tile_display *t = &tiles[pos.x][pos.y];
+
+ char *tex = gfx_render_entity(c, e);
+ if (tex == NULL)
+ t->texture = "❓";
+ else
+ t->texture = tex;
+ }
+
+ // flush map & entities
+ for (size_t y = 0; y < c->map.bounds.size.y; y++) {
+ for (size_t x = 0; x < c->map.bounds.size.x; x++) {
+ tile_display dis = tiles[x][y];
gfx_set_color(c, &bg, dis.bg);
- if (vec2_eq(pos, c->player_pos)) {
- fprintf(c->termbuf, "🙂");
- } else if (dis.texture) {
+ if (dis.texture) {
gfx_set_color(c, &fg, dis.fg);
- fprintf(c->termbuf, "%s", dis.texture);
+ fprintf(c->frame, "%s", dis.texture);
} else {
- fprintf(c->termbuf, " ");
+ fprintf(c->frame, " ");
}
}
gfx_clear_effects(c);
- bg.has_old = false;
fg.has_old = false;
- fprintf(c->termbuf, "\n");
+ bg.has_old = false;
+ fprintf(c->frame, "\n");
}
- fflush(c->termbuf);
- fprintf(stderr, "size: %ld\n", ftell(c->termbuf));
- write(STDOUT_FILENO, c->termbuf_buf, ftell(c->termbuf));
- //fwrite(c->termbuf_buf, 1, ftell(c->termbuf), stdout);
- //fflush(stdout);
+
+ // submit
+ fflush(c->frame);
+ write(STDOUT_FILENO, c->frame_buf, ftell(c->frame));
}
void free_players(client *c)
@@ -204,12 +223,13 @@ void client_exit(client *c, int ret)
free(c->server_motd.data);
free_players(c);
+ free(c->entities.data);
peer_free(&c->conn);
tcsetattr(STDIN_FILENO, TCSANOW, &c->oldtio);
- if (c->termbuf) fclose(c->termbuf);
- free(c->termbuf_buf);
+ if (c->frame) fclose(c->frame);
+ free(c->frame_buf);
gfx_alt_buffer(false);
exit(ret);
@@ -250,6 +270,16 @@ 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_REMOVE(c->entities, e);
+ }
}
bool handle_nodes(str *pkt, client *c)
@@ -276,6 +306,7 @@ bool handle_reset_map(str *pkt, client *c)
{
(void) pkt;
chunk_clear(c->map);
+ c->entities.len = 0;
return true;
}
@@ -310,29 +341,50 @@ bool handle_hi(str *pkt, client *c)
bool handle_entity(str *pkt, client *c)
{
- uint16_t num;
- if (!deser_u16(pkt, &num)) return false;
- for (uint16_t i = 0; i < num; i++) {
+ while (pkt->len > 0) {
entity_id id;
entity_cmd cmd;
if (!deser_entity_id(pkt, &id)) return false;
if (!deser_entity_cmd(pkt, &cmd)) return false;
+ entity *e = entity_from_id(c, id);
switch (cmd) {
case ENTITY_ADD: {
- entity_type type;
- if (!deser_entity_type(pkt, &type)) return false;
+ entity ent = {0};
+ ent.id = id;
+ if (!deser_entity_type(pkt, &ent.type)) return false;
+ if (!deser_vec2(pkt, &ent.pos)) return false;
+ switch (ent.type) {
+ case ENTITY_PLAYER: break;
+ case ENTITY_FLOWER:
+ if (!deser_flower_type(pkt, &ent.flower)) return false;
+ break;
+ default: return false;
+ }
+ if (e == NULL)
+ *ARR_APPEND(c->entities) = ent;
+ else
+ *e = ent; // this... shouldn't happen
} break;
- case ENTITY_REMOVE: break;
- case ENTITY_MOVE:
+ case ENTITY_REMOVE:
+ if (e == NULL) continue; // consider returning false? shouldn't happen
+ ARR_REMOVE(c->entities, e);
+ break;
+ case ENTITY_MOVE: {
vec2 pos;
if (!deser_vec2(pkt, &pos)) return false;
- if (id == c->self_id)
- map_set_center(c, c->player_pos = pos);
+ if (e == NULL) continue;
- break;
+ e->pos = pos;
+
+ // TODO: remove entity if out of bounds?
+ // shouldn't happen tho, unless for moving the self player entity...
+
+ if (id == c->self_id)
+ map_set_center(c, pos);
+ } break;
default: return false;
}
}
@@ -388,7 +440,7 @@ int main(int argc, char **argv)
str server_name = S("server");
peer_init(&c.conn, socket, &server_name);
- c.termbuf = open_memstream(&c.termbuf_buf, &c.termbuf_size);
+ c.frame = open_memstream(&c.frame_buf, &c.frame_size);
gfx_alt_buffer(true);
SEND_PKT(c.conn, SPKT_HI,