summary refs log tree commit diff homepage
path: root/torus.h
blob: b5a02356f647bc9ede3cb0afec72e4cf7c04ed8d (plain) (blame)
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
/* Copyright (C) 2017  June McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

#define PACKED __attribute__((packed))
#define ALIGNED(x) __attribute__((aligned(x)))

#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))

#undef COLOR_BLACK
#undef COLOR_RED
#undef COLOR_GREEN
#undef COLOR_YELLOW
#undef COLOR_BLUE
#undef COLOR_MAGENTA
#undef COLOR_CYAN
#undef COLOR_WHITE

enum {
	COLOR_BLACK,
	COLOR_RED,
	COLOR_GREEN,
	COLOR_YELLOW,
	COLOR_BLUE,
	COLOR_MAGENTA,
	COLOR_CYAN,
	COLOR_WHITE,
	COLOR_BRIGHT,
};

enum {
	CELL_ROWS = 25,
	CELL_COLS = 80,
};
static const size_t CELLS_SIZE = sizeof(char[CELL_ROWS][CELL_COLS]);

static const uint8_t CELL_INIT_X = CELL_COLS / 2;
static const uint8_t CELL_INIT_Y = CELL_ROWS / 2;

struct ALIGNED(4096) Tile {
	time_t createTime;
	time_t modifyTime;
	char ALIGNED(16) cells[CELL_ROWS][CELL_COLS];
	uint8_t ALIGNED(16) colors[CELL_ROWS][CELL_COLS];
	uint32_t modifyCount;
	uint32_t accessCount;
	time_t accessTime;
};
static_assert(4096 == sizeof(struct Tile), "struct Tile is page-sized");
static_assert(16 == offsetof(struct Tile, cells), "stable cells offset");
static_assert(2016 == offsetof(struct Tile, colors), "stable colors offset");

enum {
	TILE_ROWS = 512,
	TILE_COLS = 512,
};
static const size_t TILES_SIZE = sizeof(struct Tile[TILE_ROWS][TILE_COLS]);

static const uint32_t TILE_VOID_X = UINT32_MAX;
static const uint32_t TILE_VOID_Y = UINT32_MAX;

static const struct {
	uint32_t tileX;
	uint32_t tileY;
} SPAWNS[] = {
	{ 0, 0 },
	{ TILE_COLS * 3 / 4, TILE_ROWS * 3 / 4 }, // NW
	{ TILE_COLS * 1 / 4, TILE_ROWS * 3 / 4 }, // NE
	{ TILE_COLS * 1 / 4, TILE_ROWS * 1 / 4 }, // SE
	{ TILE_COLS * 3 / 4, TILE_ROWS * 1 / 4 }, // SW
};

enum {
	MAP_ROWS = 11,
	MAP_COLS = 11,
};

struct Map {
	struct MapTile {
		time_t createTime;
		time_t modifyTime;
		time_t accessTime;
		uint32_t modifyCount;
		uint32_t accessCount;
	} tiles[MAP_ROWS][MAP_COLS];
};

struct ServerMessage {
	enum PACKED {
		SERVER_TILE,
		SERVER_MOVE,
		SERVER_PUT,
		SERVER_CURSOR,
		SERVER_MAP,
	} type;
	union {
		struct {
			uint8_t cellX;
			uint8_t cellY;
		} move;
		struct {
			uint8_t cellX;
			uint8_t cellY;
			uint8_t color;
			char cell;
		} put;
		struct {
			uint8_t oldCellX;
			uint8_t oldCellY;
			uint8_t newCellX;
			uint8_t newCellY;
		} cursor;
	};
};

static const uint8_t CURSOR_NONE = UINT8_MAX;

struct ClientMessage {
	enum PACKED {
		CLIENT_MOVE,
		CLIENT_PUT,
		CLIENT_SPAWN,
		CLIENT_MAP,
	} type;
	union {
		struct {
			int8_t dx;
			int8_t dy;
		} move;
		struct {
			uint8_t color;
			char cell;
		} put;
		uint8_t spawn;
	};
};
265d8cfdc5b312e0ea76da8c8a&follow=1'>html: check return value of writeJason A. Donenfeld This squelches a gcc warning. It's also correct that we check to see if there are any partial or failed writes. For now, we just print a warning to stderr. In the future, perhaps it will prove wise to exit(1) on partial writes. 2013-03-20ui-shared: squelch compiler warning.Jason A. Donenfeld Since tail is initialized to 0, we will never get a warning on the last if statement, but recent gcc complains anyway. So, we initialize len as well. Future gcc versions should be able to optimize this out anyway. 2013-03-20cgit.mk: Use SHELL_PATH_SQ to run gen-version.shJohn Keeping On some platforms (notably Solaris) /bin/sh doesn't support enough of POSIX for gen-version.sh to run. Git's Makefile provides SHELL_PATH_SQ to address this issue so we just have to use it. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20cgit.mk: don't rebuild everything if CGIT_VERSION changesJohn Keeping If CGIT_VERSION is in CGIT_CFLAGS then a change in version (for example because you have committed your changes) causes all of the CGit objects to be rebuilt. Avoid this by using EXTRA_CPPFLAGS to add the version for only those files that are affected and make them depend on VERSION. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20ui-patch: use cgit_version not CGIT_VERSIONJohn Keeping We already have a global cgit_version which is set from the #define'd CGIT_VERSION in cgit.c. Change ui-patch.c to use this so that we only need to rebuild cgit.o when the version changes. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20Makefile: re-use Git's Makefile where possibleJohn Keeping