diff options
Diffstat (limited to '')
-rw-r--r-- | client.c | 6 | ||||
-rw-r--r-- | server.c | 96 | ||||
-rw-r--r-- | torus.h | 1 |
3 files changed, 62 insertions, 41 deletions
diff --git a/client.c b/client.c index 28ccc4e..e0286eb 100644 --- a/client.c +++ b/client.c @@ -179,6 +179,11 @@ static void clientMove(int8_t dx, int8_t dy) { clientMessage(msg); } +static void clientFlip(void) { + struct ClientMessage msg = { .type = CLIENT_FLIP }; + clientMessage(msg); +} + static void clientPut(uint8_t color, uint8_t cell) { struct ClientMessage msg = { .type = CLIENT_PUT, @@ -277,6 +282,7 @@ static void inputNormal(wchar_t ch) { break; case ESC: input.mode = MODE_NORMAL; input.shift = 0; break; case 'q': endwin(); exit(EX_OK); + break; case 'g': clientFlip(); break; case 'h': clientMove(-1, 0); break; case 'l': clientMove( 1, 0); break; case 'k': clientMove( 0, -1); diff --git a/server.c b/server.c index 9486fcd..73cd9f1 100644 --- a/server.c +++ b/server.c @@ -172,6 +172,51 @@ static bool clientCursors(const struct Client *client) { return true; } +static bool clientUpdate(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 clientMove(struct Client *client, int8_t dx, int8_t dy) { struct Client old = *client; @@ -210,48 +255,14 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { assert(client->tileX < TILE_COLS); assert(client->tileY < TILE_ROWS); - 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 clientUpdate(client, &old); +} - return true; +static bool clientFlip(struct Client *client) { + struct Client old = *client; + client->tileX = (client->tileX + TILE_COLS / 2) % TILE_COLS; + client->tileY = (client->tileY + TILE_ROWS / 2) % TILE_ROWS; + return clientUpdate(client, &old); } static bool clientPut(const struct Client *client, uint8_t color, uint8_t cell) { @@ -371,6 +382,9 @@ int main() { break; case CLIENT_MOVE: { success = clientMove(client, msg.move.dx, msg.move.dy); } + break; case CLIENT_FLIP: { + success = clientFlip(client); + } break; case CLIENT_PUT: { success = clientPut(client, msg.put.color, msg.put.cell); } diff --git a/torus.h b/torus.h index 53e469c..818f009 100644 --- a/torus.h +++ b/torus.h @@ -140,6 +140,7 @@ static const uint8_t CURSOR_NONE = UINT8_MAX; struct ClientMessage { enum { CLIENT_MOVE, + CLIENT_FLIP, CLIENT_PUT, CLIENT_MAP, } type; |