summaryrefslogtreecommitdiff
path: root/src/server.c
blob: db599c70ea2afe47904162ea14288d3337bb11a3 (plain)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <endian.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <poll.h>
#include <png.h>
#include <perlin.h>
#include <math.h>
#include "array.h"
#include "content.h"
#include "net.h"
#include "str.h"
#include "ser.h"
#include "sig.h"
#include "chunk.h"

#define MAYBE(T) \
	typedef struct { bool present; T value; } maybe_##T; \
	maybe_##T some_##T(T value) { return (maybe_##T) { true, value }; } \
	maybe_##T none_##T = { 0 };

typedef enum {
	MAP_VALLEY,
	MAP_ISLAND,
	MAP_MOUNTAIN,
	MAP_CAVE,
	MAP_NUM,
} map_type;

typedef struct {
	entity base;
	int8_t z;
} srv_entity;

typedef struct {
	vec2 xy;
	int8_t z;
} pos;

MAYBE(pos)

typedef struct {
	const char *filename;
	str name;
	map_type type;
	int8_t z; // caves: -1
	chunk chunk;
} map;

typedef struct {
	bool auth;
	str name;
	entity_id id;
	peer conn;
} player;

typedef struct {
	int accept_fd;
	str motd;
	str passphrase;
	array(map) maps;
	arraybuf(player) players;
	arraybuf(srv_entity) entities;
	entity_id next_entity;
} game;

player *player_from_id(game *g, entity_id id)
{
	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)
{
	ARR_ITER(g->entities, e) if (e->base.id == id) return e;
	return NULL;
}

pos entity_pos(srv_entity *e)
{
	return (pos) { e->base.pos, e->z };
}

box2 player_sight(vec2 center)
{
	return box2_around(center, SIGHT_RANGE);
}

void ser_entity_update(strbuf *w, srv_entity *e, entity_cmd cmd)
{
	ser_entity_id(w, e->base.id);
	ser_entity_cmd(w, cmd);

	switch (cmd) {
		case ENTITY_ADD:
			ser_entity_type(w, e->base.type);
			ser_vec2(w, e->base.pos);
			switch (e->base.type) {
				case ENTITY_FLOWER:
					ser_flower_type(w, e->base.flower);
					break;
				default: break;
			}
			break;
		case ENTITY_MOVE:
			ser_vec2(w, e->base.pos);
			break;
		case ENTITY_REMOVE:
			break;
		case ENTITY_CMD_COUNT:
			break; // unreachable
	}
}

static maybe_pos moved_get_pos(srv_entity *e, srv_entity *moved, maybe_pos p_moved)
{
	return (e == moved) ? p_moved : some_pos(entity_pos(e));
}

static bool moved_can_see(srv_entity *a, srv_entity *b, srv_entity *moved, maybe_pos p_moved)
{
	maybe_pos p_a = moved_get_pos(a, moved, p_moved);
	maybe_pos p_b = moved_get_pos(b, moved, p_moved);
	return p_a.present
		&& p_b.present
		&& p_a.value.z == p_b.value.z
		&& box2_contains(player_sight(p_a.value.xy), p_b.value.xy);
}

static entity_cmd moved_check_update(srv_entity *observer, srv_entity *target, srv_entity *moved, maybe_pos before, maybe_pos 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, srv_entity *moved, maybe_pos before, maybe_pos after)
{
	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,
				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);
				}
			)
		} 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)
{
	ARR_ITER(g->maps, m) {
		if (m->z != z)
			continue;
		node *n = chunk_index_abs(m->chunk, p);
		if (n == NULL || !n->present)
			continue;
		return n;
	}
	return NULL;
}

bool silly_noise(vec2 pos, uint32_t grid, double sillyness, int seed)
{
	int64_t x = (int64_t) pos.x+INT_MAX+1;
	int64_t y = (int64_t) pos.y+INT_MAX+1;

	if ((y/grid) % 2 == 0)
		x += grid/2;

	x += round(noise2d(x/grid, y/grid, 0, seed) * sillyness);
	y += round(noise2d(y/grid, x/grid, 0, seed) * sillyness);

	return x % grid == 0 && y % grid == 0;
}

srv_entity *spawn_entity(game *g, entity_type type, pos p)
{
	srv_entity *e = ARR_APPEND(g->entities);
	e->base.id = g->next_entity++;
	e->base.type = type;
	e->base.pos = p.xy;
	e->z = p.z;
	return e;
}

void remove_entity(game *g, srv_entity *e)
{
	moved_entity(g, e, some_pos(entity_pos(e)), none_pos);
	ARR_REMOVE(g->entities, e);
}

void map_load_node(game *g, map *m, uvec2 v, color col)
{
	uint32_t ucol = color_to_u32(col);

	pos p = { vec2_add(m->chunk.bounds.pos, CVEC2(v)), m->z };

	node *n = chunk_index(m->chunk, v);
	*n = (node) { 0 };

	n->present = ucol != 0xffffff;
	if (!n->present)
		return;

	switch (ucol) {
		case 0xff9de2:
			spawn_entity(g, ENTITY_FLOWER, p)->base.flower = rand() % FLOWER_COUNT;
			[[fallthrough]];
		case 0x00880d: n->type = N_GRASS;
			n->variant = silly_noise(p.xy, 3, 1.0, 0);
			break;
		case 0x595959: n->type = N_ROCK; n->z = 1; break;
		case 0x001b51:
			spawn_entity(g, ENTITY_FLOWER, p)->base.flower = FLOWER_DANDELION;
			[[fallthrough]];
		case 0x484848: n->type = N_ROCK; n->z = 2; break;
		case 0x00ffe8:
			spawn_entity(g, ENTITY_FLOWER, p)->base.flower = FLOWER_DANDELION;
			[[fallthrough]];
		case 0x373737: n->type = N_ROCK; n->z = 3; break;
		case 0xa09700: n->type = N_PATH; break;
		case 0x015100: n->type = N_NEEDLE_TREE; break;
		case 0x00c6ff: n->type = N_WATER;
			n->variant = silly_noise(p.xy, 3, 0.7, 1);
			n->z = -1;
			break;
		case 0x5c3b12: n->type = N_PLANK; break;
		case 0x016300: n->type = N_BIG_TREE; break;
		case 0xfce84e: n->type = N_SAND;
			n->variant = silly_noise(p.xy, 4, 1.0, 1);
			break;
		default:
			fprintf(stderr, "invalid color in map %.*s at %"PRIu32" %"PRIu32": %06x\n",
				PSTR(m->name), v.x, v.y, ucol);
			n->present = false;
			break;
	}
}

bool map_load(game *g, map *m)
{
#define TRY(expr, ...) if (!(expr)) { \
	fprintf(stderr, __VA_ARGS__); \
	if (file != NULL) fclose(file); \
	png_destroy_read_struct(&png, &info, NULL); \
	return false; }

	FILE *file = NULL;
	png_structp png = NULL;
	png_infop info = NULL;

	TRY((file = fopen(m->filename, "r")) != NULL, "failed to open %s\n", m->filename)

	TRY(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL),
		"png_create_read_struct failed\n")

	TRY(info = png_create_info_struct(png), "png_create_info_struct failed\n")

	png_init_io(png, file);
	png_read_info(png, info);

	png_uint_32 width = png_get_image_width(png, info);
	png_uint_32 height = png_get_image_height(png, info);
	png_byte color_type = png_get_color_type(png, info);

	TRY(width == m->chunk.bounds.size.x, "%s: width mismatch\n", m->filename)
	TRY(height == m->chunk.bounds.size.y, "%s: height mismatch\n", m->filename)
	TRY(color_type == PNG_COLOR_TYPE_RGB, "%s: color type is not RGB\n", m->filename)

	// uint32_t colors[100] = {0};

	png_uint_32 pitch = png_get_rowbytes(png, info);
	png_byte row[pitch];
	for (png_uint_32 y = 0; y < height; y++) {
		png_read_row(png, row, NULL);

		for (png_uint_32 x = 0; x < width; x++) {
			png_bytep p = &row[x*3];

			/*
			for (size_t i = 0; i < 100; i++) {
				if (colors[i] == color) {
					break;
				} else if (colors[i] == 0) {
					colors[i] = color;
					printf("#%06x\n", color);
					break;
				}
			}*/

			map_load_node(g, m, UVEC2(x, y), (color) { p[0], p[1], p[2] });
		}
	}

	fclose(file);
	png_destroy_read_struct(&png, &info, NULL);
	return true;
#undef TRY
}

void send_players(game *g)
{
	ARR_ITER(g->players, to) if (to->auth) {
		SEND_PKT(to->conn, CPKT_PLAYERS,
			ser_u16(&pkt, g->players.len);
			ARR_ITER(g->players, p) if (p->auth) {
				ser_str(&pkt, p->name);
				ser_entity_id(&pkt, p->id);
			}
		)
	}
}

// send_nudes when
void send_nodes(player *p, game *g, box2 bounds, int8_t z)
{
	SEND_PKT(p->conn, CPKT_NODES,
		ser_box2(&pkt, bounds);
		for (uint32_t x = 0; x < bounds.size.x; x++)
		for (uint32_t y = 0; y < bounds.size.y; y++)
			ser_node(&pkt, map_node(g, vec2_add(bounds.pos, VEC2(x, y)), z));
	)
}

void player_cleanup(player *p)
{
	peer_free(&p->conn);
	if (p->auth)
		free(p->name.data);
}

void player_remove(player *p, game *g)
{
	player_cleanup(p);

	bool auth = p->auth;
	entity_id id = p->id;

	ARR_REMOVE(g->players, p);

	if (auth) {
		remove_entity(g, entity_from_id(g, id));
		send_players(g);
	}
}

void game_exit(game *g, int ret)
{
	free(g->motd.data);
	free(g->passphrase.data);

	ARR_ITER(g->maps, m) {
		free(m->name.data);
		free(m->chunk.data);
	}
	free(g->maps.data);

	ARR_ITER(g->players, p) player_cleanup(p);
	free(g->players.data);

	exit(ret);
}

bool handle_hi(str pkt, player *p, game *g)
{
	str name, pass;
	if (! (deser_str(&pkt, &name) && deser_str(&pkt, &pass)))
		return false;

	if (!str_eq(g->passphrase, pass)) {
		printf("wrong passphrase from %.*s\n", PSTR(name)); // TODO: log ip ?
		SEND_PKT(p->conn, CPKT_FAIL, ser_fail_reason(&pkt, FAIL_WRONG_PASS);)
		return true; // valid pkt, but invalid passphrase
	}

	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));

	p->auth = true;
	p->name = str_clone(name);

	srv_entity *e = ARR_APPEND(g->entities);
	e->base.id = p->id;
	e->base.type = ENTITY_PLAYER;
	e->base.pos.x = e->base.pos.y = 0;
	e->z = 0;

	SEND_PKT(p->conn, CPKT_HI, ser_str(&pkt, g->motd);)
	send_players(g);

	pos ent_p = entity_pos(e);

	moved_entity(g, e, none_pos, some_pos(ent_p));
	send_nodes(p, g, player_sight(ent_p.xy), ent_p.z);

	return true;
}

bool handle_move(str pkt, player *p, game *g)
{
	dir d;
	if (!deser_dir(&pkt, &d)) return false;

	vec2 v = dir_to_vec2(d);
	if (vec2_iszero(v))
		return false;

	srv_entity *e = entity_from_id(g, p->id);

	pos old_pos = entity_pos(e);

	// yolo
	e->base.pos = vec2_add(v, e->base.pos);

	pos new_pos = entity_pos(e);

	moved_entity(g, e, some_pos(old_pos), some_pos(new_pos));

	// TODO: only send newly visible nodes
	send_nodes(p, g, player_sight(new_pos.xy), new_pos.z);

	return true;
}

bool handle_pkt(str pkt, player *p, game *g)
{
	pkt_type type;
	if (!deser_pkt_type(&pkt, &type))
		return false;

	if ((type == SPKT_HI) == p->auth)
		return false;

	switch (type) {
		case SPKT_HI: return handle_hi(pkt, p, g);
		case SPKT_MOVE: return handle_move(pkt, p, g);
		default: return false;
	}
}

int main()
{
	signal_setup();

	game g = {0};

	g.accept_fd = -1;
	g.next_entity = 1;
	g.motd = str_clone(S("Welcome to test server"));
	g.passphrase = str_clone(S(""));

	g.maps.len = 1;
	g.maps.data = malloc(g.maps.len * sizeof *g.maps.data);
	g.maps.data[0] = (map) {
		.filename = "assets/map_valley.png",
		.name = str_clone(S("Valley")),
		.type = MAP_VALLEY,
		.z = 0,
		.chunk = chunk_alloc((box2) { { -50, -50 }, { 100, 100 } }),
	};

	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)
		game_exit(&g, EXIT_FAILURE);

	for (;;) {
		struct pollfd fds[g.players.len + 1];

		for (size_t i = 0; i < g.players.len; i++)
			fds[i] = peer_prepare(&g.players.data[i].conn);

		fds[g.players.len].fd = g.accept_fd;
		fds[g.players.len].events = POLLIN;

		if (poll(fds, g.players.len + 1, -1) < 0) {
			switch (errno) {
				case EINTR: break;
				default: perror("poll"); continue;
			}
		}

		if (signal_stop)
			game_exit(&g, EXIT_SUCCESS);

		for (size_t i = 0; i < g.players.len; i++) {
			player *p = &g.players.data[i];

			str pkt = peer_recv(&p->conn, fds[i]);
			if (p->conn.disco) {
				printf("player %.*s disconnected\n", PSTR(p->name));
				player_remove(p, &g);
				goto cont;
			}

			if (pkt.len > 0 && !handle_pkt(pkt, p, &g))
				invalid_pkt(&p->conn, pkt);
		}

		if (fds[g.players.len].revents) {
			int socket = socket_accept(g.accept_fd);
			if (socket < 0)
				continue;

			printf("new player\n");
			player *p = ARR_APPEND(g.players);
			p->auth = false;
			p->id = g.next_entity++;
			p->name = S("(unauthenticated)");
			peer_init(&p->conn, socket, &p->name);
		}

		cont: continue;
	}
}