summaryrefslogtreecommitdiff
path: root/server.c
blob: 651fccd3f9e010fefcd3d3182b52f06fb05dca2f (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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdint.h>
#include <png.h>
#include <endian.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "content.h"
#include "peer.h"
#include "str.h"

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

typedef struct {
	const char *filename;
	str name;
	map_type type;
	uint32_t width;
	uint32_t height;
	int32_t off_x;
	int32_t off_y;
	int8_t z; // caves: -1
	node *nodes;
} map;

typedef struct {
	str motd;
	array(map) maps;
} game;

typedef struct {
	str name;
	int32_t x, y;
	peer net;
} player;

node *map_local_node(map *m, int32_t x, int32_t y)
{
	if (x < 0 || y < 0 || (uint32_t) x >= m->width || (uint32_t) y >= m->height)
		return NULL;

	node *n = &m->nodes[x*m->width+y];
	if (!n->present)
		return NULL;

	return n;
}

node *map_node(game *g, int32_t x, int32_t y, int8_t z)
{
	for (size_t i = 0; i < g->maps.len; i++) {
		map *m = &g->maps.data[i];
		if (m->z != z)
			continue;
		node *n = map_local_node(m, x - m->off_x, y - m->off_y);
		if (n != NULL)
			return n;
	}
	return NULL;
}

/*
void map_load_node(map *m, uint32_t x, uint32_t y, uint32_t color)
{
	
}*/

bool map_load(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->width, "%s: width mismatch\n", m->filename);
	TRY(height == m->height, "%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];

			uint32_t color = ((uint32_t) p[2]) | (((uint32_t) p[1]) << 8) | (((uint32_t) p[0]) << 16);
			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;
				}
			}
		}
	}

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

int net_listen(const char *host, const char *port)
{
	struct addrinfo *info = NULL, hints = {0};
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	int err;
	if ((err = getaddrinfo(host, port, &hints, &info))) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
		return -1;
	}

#define TRY(op, val) if ((val) < 0) { perror(op); freeaddrinfo(info); return -1; }

	int fd;
	TRY("socket", fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol))

	int flag = 1;
	TRY("setsockopt", setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &flag, sizeof flag));

	TRY("bind", bind(fd, info->ai_addr, info->ai_addrlen))
	TRY("listen", listen(fd, 3))

#undef TRY

	freeaddrinfo(info);
	return fd;

}

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

	if (g->maps.data)
		for (size_t i = 0; i < g->maps.len; i++) {
			free(g->maps.data[i].name.data);
			free(g->maps.data[i].nodes);
		}
		
	free(g->maps.data);

	exit(ret);
}

int main()
{
	game g = {0};

	g.motd = str_clone(S("Welcome to test server"));

	g.maps.len = 1;
	g.maps.data = malloc(g.maps.len * sizeof *g.maps.data);
	g.maps.data[0] = (map) {
		.filename = "map_valley.png",
		.name = str_clone(S("Valley")),
		.type = MAP_VALLEY,
		.off_x = -50,
		.off_y = -50,
		.z = 0,
		.width = 100,
		.height = 100,
		.nodes = NULL,
	};

	if (!map_load(&g.maps.data[0]))
		game_exit(&g, EXIT_FAILURE);

	int accept_fd = net_listen("0.0.0.0", "4560");
	if (accept_fd < 0)
		game_exit(&g, EXIT_FAILURE);

	for (;;) {
		int clt_sock = accept(accept_fd, NULL, NULL);
		if (clt_sock < 0) {
			perror("accept");
			continue;
		}

		if (fcntl(clt_sock, F_SETFL, fcntl(clt_sock, F_GETFL, 0) | O_NONBLOCK) < 0) {
			perror("fcntl");
			continue;
		}		
	}
	
}