diff options
-rw-r--r-- | client.c | 15 | ||||
-rw-r--r-- | server.c | 113 | ||||
-rw-r--r-- | torus.h | 17 |
3 files changed, 54 insertions, 91 deletions
diff --git a/client.c b/client.c index 7af6950..1e3421b 100644 --- a/client.c +++ b/client.c @@ -58,14 +58,6 @@ static void clientPut(uint8_t color, char cell) { clientMessage(msg); } -static void clientSpawn(uint8_t spawn) { - struct ClientMessage msg = { - .type = CLIENT_SPAWN, - .spawn = spawn, - }; - clientMessage(msg); -} - static void clientMap(void) { struct ClientMessage msg = { .type = CLIENT_MAP }; clientMessage(msg); @@ -162,13 +154,6 @@ static void inputNormal(int c) { break; case ESC: input.mode = MODE_NORMAL; break; case 'q': endwin(); exit(EX_OK); - break; case 'Q': { - if ((input.color & 0x07) < ARRAY_LEN(SPAWNS)) { - clientSpawn(input.color & 0x07); - } else { - clientSpawn(0); - } - } break; case 'm': clientMap(); break; case 'i': insertMode(1, 0); diff --git a/server.c b/server.c index 9cae5dd..f59e9c4 100644 --- a/server.c +++ b/server.c @@ -97,8 +97,8 @@ static struct Client *clientAdd(int fd) { if (!client) err(EX_OSERR, "malloc"); client->fd = fd; - client->tileX = TILE_VOID_X; - client->tileY = TILE_VOID_Y; + client->tileX = TILE_INIT_X; + client->tileY = TILE_INIT_Y; client->cellX = CELL_INIT_X; client->cellY = CELL_INIT_Y; @@ -172,61 +172,6 @@ static bool clientCursors(const struct Client *client) { return true; } -static bool clientUpdate(const struct Client *client, const struct Client *old) { - struct ServerMessage msg = { - .type = SERVER_MOVE, - .move = { .cellX = client->cellX, .cellY = client->cellY }, - }; - if (!clientSend(client, msg)) return false; - - if (client->tileX != old->tileX || client->tileY != old->tileY) { - msg.type = SERVER_TILE; - if (!clientSend(client, msg)) return false; - - if (!clientCursors(client)) return false; - - msg = (struct ServerMessage) { - .type = SERVER_CURSOR, - .cursor = { - .oldCellX = old->cellX, .oldCellY = old->cellY, - .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE, - }, - }; - clientCast(old, msg); - - msg = (struct ServerMessage) { - .type = SERVER_CURSOR, - .cursor = { - .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE, - .newCellX = client->cellX, .newCellY = client->cellY, - }, - }; - clientCast(client, msg); - - } else { - msg = (struct ServerMessage) { - .type = SERVER_CURSOR, - .cursor = { - .oldCellX = old->cellX, .oldCellY = old->cellY, - .newCellX = client->cellX, .newCellY = client->cellY, - }, - }; - clientCast(client, msg); - } - - return true; -} - -static bool clientSpawn(struct Client *client, uint8_t spawn) { - if (spawn >= ARRAY_LEN(SPAWNS)) return false; - struct Client old = *client; - client->tileX = SPAWNS[spawn].tileX; - client->tileY = SPAWNS[spawn].tileY; - client->cellX = CELL_INIT_X; - client->cellY = CELL_INIT_Y; - return clientUpdate(client, &old); -} - static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { struct Client old = *client; @@ -265,7 +210,48 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { assert(client->tileX < TILE_COLS); assert(client->tileY < TILE_ROWS); - return clientUpdate(client, &old); + struct ServerMessage msg = { + .type = SERVER_MOVE, + .move = { .cellX = client->cellX, .cellY = client->cellY }, + }; + if (!clientSend(client, msg)) return false; + + if (client->tileX != old.tileX || client->tileY != old.tileY) { + msg.type = SERVER_TILE; + if (!clientSend(client, msg)) return false; + + if (!clientCursors(client)) return false; + + msg = (struct ServerMessage) { + .type = SERVER_CURSOR, + .cursor = { + .oldCellX = old.cellX, .oldCellY = old.cellY, + .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE, + }, + }; + clientCast(&old, msg); + + msg = (struct ServerMessage) { + .type = SERVER_CURSOR, + .cursor = { + .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE, + .newCellX = client->cellX, .newCellY = client->cellY, + }, + }; + clientCast(client, msg); + + } else { + msg = (struct ServerMessage) { + .type = SERVER_CURSOR, + .cursor = { + .oldCellX = old.cellX, .oldCellY = old.cellY, + .newCellX = client->cellX, .newCellY = client->cellY, + }, + }; + clientCast(client, msg); + } + + return true; } static bool clientPut(const struct Client *client, uint8_t color, char cell) { @@ -366,7 +352,11 @@ int main() { nevents = kevent(kq, &event, 1, NULL, 0, NULL); if (nevents < 0) err(EX_IOERR, "kevent"); - if (!clientSpawn(client, 0)) clientRemove(client); + struct ServerMessage msg = { .type = SERVER_TILE }; + bool success = clientSend(client, msg) + && clientMove(client, 0, 0) + && clientCursors(client); + if (!success) clientRemove(client); continue; } @@ -392,8 +382,9 @@ int main() { break; case CLIENT_PUT: { success = clientPut(client, msg.put.color, msg.put.cell); } - break; case CLIENT_SPAWN: success = clientSpawn(client, msg.spawn); - break; case CLIENT_MAP: success = clientMap(client); + break; case CLIENT_MAP: { + success = clientMap(client); + } } if (!success) clientRemove(client); } diff --git a/torus.h b/torus.h index b5a0235..93622ad 100644 --- a/torus.h +++ b/torus.h @@ -75,19 +75,8 @@ enum { }; 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 -}; +static const uint32_t TILE_INIT_X = TILE_COLS / 2; +static const uint32_t TILE_INIT_Y = TILE_ROWS / 2; enum { MAP_ROWS = 11, @@ -138,7 +127,6 @@ struct ClientMessage { enum PACKED { CLIENT_MOVE, CLIENT_PUT, - CLIENT_SPAWN, CLIENT_MAP, } type; union { @@ -150,6 +138,5 @@ struct ClientMessage { uint8_t color; char cell; } put; - uint8_t spawn; }; }; |