about summary refs log tree commit diff homepage
path: root/torus.h
blob: 076cff9dc314ac633a520f941a0a76e92a29c7d7 (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
/* 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 <time.h>

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

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

#define CELL_ROWS (25)
#define CELL_COLS (80)
#define CELLS_SIZE (sizeof(char[CELL_ROWS][CELL_COLS]))

#define CELL_INIT_X (CELL_COLS / 2)
#define CELL_INIT_Y (CELL_ROWS / 2)

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

#define TILE_ROWS (512)
#define TILE_COLS (512)
#define TILES_SIZE (sizeof(struct Tile[TILE_ROWS][TILE_COLS]))

static const struct {
    uint32_t tileX;
    uint32_t tileY;
} SPAWN[] = {
    { 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
};

#define SPAWN_COUNT (sizeof(SPAWN) / sizeof(SPAWN[0]))

enum ServerMessageType {
    SERVER_TILE,
    SERVER_MOVE,
    SERVER_PUT,
    SERVER_CURSOR,
};

struct ServerMessage {
    enum ServerMessageType type;
    union {
        struct {
            uint8_t cellX;
            uint8_t cellY;
        } m;
        struct {
            uint8_t cellX;
            uint8_t cellY;
            uint8_t color;
            char cell;
        } p;
        struct {
            uint8_t oldCellX;
            uint8_t oldCellY;
            uint8_t newCellX;
            uint8_t newCellY;
        } c;
    } data;
};

#define CURSOR_NONE UINT8_MAX

enum ClientMessageType {
    CLIENT_MOVE,
    CLIENT_PUT,
    CLIENT_SPAWN,
};

struct ClientMessage {
    enum ClientMessageType type;
    union {
        struct {
            int8_t dx;
            int8_t dy;
        } m;
        struct {
            uint8_t color;
            char cell;
        } p;
        struct {
            uint8_t spawn;
        } s;
    } data;
};