/* * Copyright (c) 2017, Curtis McEnroe * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #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; };