1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <ctype.h>
#include <stdint.h>
#include "str.h"
#include "ser.h"
#include "net.h"
#include "ticker.h"
#include "sig.h"
#include "content.h"
#include "chunk.h"
typedef struct {
str name;
entity_id id;
} player;
typedef struct {
peer conn;
struct termios oldtio;
str name;
str pass;
entity_id self_id;
array(player) players;
arraybuf(entity) entities;
str server_motd;
chunk map;
node *map_swap;
char *frame_buf;
size_t frame_size;
FILE *frame;
} client;
player *player_from_id(client *c, entity_id id)
{
ARR_ITER(c->players, p) if (p->id == id) return p;
return NULL;
}
entity *entity_from_id(client *c, entity_id id)
{
ARR_ITER(c->entities, e) if (e->id == id) return e;
return NULL;
}
void gfx_alt_buffer(bool enable)
{
str out = enable ? S("\e[?1049h\e[?25l") : S("\e[?1049l\e[?25h");
write(STDOUT_FILENO, out.data, out.len);
}
typedef struct {
bool bg;
bool has_old;
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->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->frame, "\e[2J");
}
#define C color_from_u32
#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;
} tile_display;
bool gfx_water_wave(vec2 pos)
{
return true;
}
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_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(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));
case N_PLANK: return DISPLAY_BG(C(0x5c3b12));
case N_BIG_TREE: return DISPLAY_BG(C(0x016300));
default: return DISPLAY_ERR;
}
}
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->frame);
gfx_clear(c);
color_layer fg = {0};
color_layer bg = {0};
bg.bg = true;
// 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));
}
// entities
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);
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 (dis.texture) {
gfx_set_color(c, &fg, dis.fg);
fprintf(c->frame, "%s", dis.texture);
} else {
fprintf(c->frame, " ");
}
}
gfx_clear_effects(c);
fg.has_old = false;
bg.has_old = false;
fprintf(c->frame, "\n");
}
// submit
fflush(c->frame);
write(STDOUT_FILENO, c->frame_buf, ftell(c->frame));
}
void free_players(client *c)
{
ARR_ITER(c->players, p) free(p->name.data);
free(c->players.data);
c->players.len = 0;
c->players.data = NULL;
}
void client_exit(client *c, int ret)
{
free(c->name.data);
free(c->pass.data);
free(c->server_motd.data);
free_players(c);
free(c->entities.data);
peer_free(&c->conn);
tcsetattr(STDIN_FILENO, TCSANOW, &c->oldtio);
if (c->frame) fclose(c->frame);
free(c->frame_buf);
gfx_alt_buffer(false);
exit(ret);
}
dir dir_from_key(char ch)
{
switch (ch) {
case 'w': return DIR_UP;
case 'a': return DIR_LEFT;
case 's': return DIR_DOWN;
case 'd': return DIR_RIGHT;
default: return 4;
}
}
void handle_input(client *c, char ch)
{
ch = tolower(ch);
dir move;
if ((move = dir_from_key(ch)) != 4)
SEND_PKT(c->conn, SPKT_MOVE, ser_dir(&pkt, move);)
}
void map_set_center(client *c, vec2 center)
{
vec2 pos = vec2_sub(center, CVEC2(uvec2_div(c->map.bounds.size, 2)));
if (vec2_eq(c->map.bounds.pos, pos))
return;
chunk old = c->map;
c->map.data = c->map_swap;
c->map.bounds.pos = pos;
c->map_swap = old.data;
chunk_clear(c->map);
chunk_copy(c->map, old);
ARR_ITER(c->entities, e) {
if (!box2_contains(c->map.bounds, e->pos)) {
ARR_REMOVE(c->entities, e);
e--;
}
}
}
bool handle_nodes(str *pkt, client *c)
{
chunk ch;
if (!deser_vec2(pkt, &ch.bounds.pos)) return false;
if (!deser_uvec2(pkt, &ch.bounds.size)) return false;
if (ch.bounds.size.x * ch.bounds.size.y > PKT_NODES_MAX) return false;
node nodes[ch.bounds.size.x * ch.bounds.size.y];
ch.pitch = ch.bounds.size.y;
ch.data = nodes;
for (uint32_t x = 0; x < ch.bounds.size.x; x++)
for (uint32_t y = 0; y < ch.bounds.size.y; y++)
if (!deser_node(pkt, chunk_index(ch, UVEC2(x, y)))) return false;
chunk_copy(c->map, ch);
return true;
}
bool handle_reset_map(str *pkt, client *c)
{
(void) pkt;
chunk_clear(c->map);
c->entities.len = 0;
return true;
}
bool handle_players(str *pkt, client *c)
{
free_players(c);
uint16_t len;
if (!deser_u16(pkt, &len)) return false;
c->players.data = malloc(sizeof *c->players.data * len);
for (c->players.len = 0; c->players.len < len; c->players.len++) {
player *p = &c->players.data[c->players.len];
if (!deser_str(pkt, &p->name)) return false;
if (!deser_entity_id(pkt, &p->id)) return false;
if (str_eq(p->name, c->name))
c->self_id = p->id;
p->name = str_clone(p->name);
}
return true;
}
bool handle_hi(str *pkt, client *c)
{
str motd;
if (!deser_str(pkt, &motd)) return false;
c->server_motd = str_clone(motd);
return true;
}
bool handle_entity(str *pkt, client *c)
{
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 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:
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 (e == NULL) continue;
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;
}
}
return true;
}
bool handle_pkt(client *c, str pkt)
{
pkt_type type;
if (!deser_pkt_type(&pkt, &type))
return false;
switch (type) {
case CPKT_HI: return handle_hi(&pkt, c);
case CPKT_PLAYERS: return handle_players(&pkt, c);
case CPKT_RESET_MAP: return handle_reset_map(&pkt, c);
case CPKT_NODES: return handle_nodes(&pkt, c);
case CPKT_ENTITY: return handle_entity(&pkt, c);
default: return false;
}
}
int main(int argc, char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: %s server port name [pass]\n", argv[0]);
return EXIT_FAILURE;
}
signal_setup();
client c = {0};
const char *server = argv[1];
const char *port = argv[2];
c.name = str_clone(str_intro(argv[3]));
c.pass = str_clone(argc < 5 ? S("") : str_intro(argv[4]));
c.conn.socket = -1;
tcgetattr(STDIN_FILENO, &c.oldtio); // TODO: handle error
struct termios newtio = c.oldtio;
newtio.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newtio); // TODO: handle error
SET_NONBLOCK(STDIN_FILENO); // TODO: handle error
int socket = socket_create(server, port, false);
if (socket < 0)
client_exit(&c, EXIT_FAILURE);
str server_name = S("server");
peer_init(&c.conn, socket, &server_name);
c.frame = open_memstream(&c.frame_buf, &c.frame_size);
gfx_alt_buffer(true);
SEND_PKT(c.conn, SPKT_HI,
ser_str(&pkt, c.name);
ser_str(&pkt, c.pass);
)
c.map = chunk_alloc(box2_around(VEC2(0, 0), SIGHT_RANGE));
c.map_swap = malloc(c.map.bounds.size.x * c.map.bounds.size.y * sizeof(node));
ticker t;
ticker_init(&t, NANOS/30);
for (;;) {
struct pollfd fds[2];
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
fds[1] = peer_prepare(&c.conn);
if (poll(fds, 2, ticker_timeout(&t)) < 0) {
switch (errno) {
case EINTR: break;
default: perror("poll"); continue;
}
}
if (signal_stop)
client_exit(&c, EXIT_SUCCESS);
if (fds[0].revents) {
// this is cursed
// maybe dont do this idk
char ch;
errno = 0;
while (read(STDIN_FILENO, &ch, 1) == 1)
handle_input(&c, ch);
switch (errno) {
case 0: case EWOULDBLOCK: case EINTR: break;
default: perror("read"); break;
}
}
str pkt = peer_recv(&c.conn, fds[1]);
if (c.conn.disco)
client_exit(&c, EXIT_SUCCESS);
if (pkt.len > 0 && !handle_pkt(&c, pkt))
invalid_pkt(&c.conn, pkt);
uint64_t dtime;
if (ticker_tick(&t, &dtime))
gfx_render(&c, dtime);
}
}
|