diff options
author | June McEnroe <programble@gmail.com> | 2017-08-28 23:38:40 -0400 |
---|---|---|
committer | June McEnroe <programble@gmail.com> | 2017-08-28 23:38:40 -0400 |
commit | 66761cd30ee1da5b820c89c38fac6caf42fbe715 (patch) | |
tree | 2471ddbdfc0bc4dfc84998521cb175048d7282fe | |
parent | Move license above includes (diff) | |
download | torus-66761cd30ee1da5b820c89c38fac6caf42fbe715.tar.gz torus-66761cd30ee1da5b820c89c38fac6caf42fbe715.zip |
Add respawning
-rw-r--r-- | client.c | 6 | ||||
-rw-r--r-- | server.c | 79 | ||||
-rw-r--r-- | torus.h | 1 |
3 files changed, 52 insertions, 34 deletions
diff --git a/client.c b/client.c index 2a8c8c5..5e70b17 100644 --- a/client.c +++ b/client.c @@ -65,6 +65,11 @@ static void clientPut(uint8_t color, char cell) { clientMessage(&msg); } +static void clientSpawn(void) { + struct ClientMessage msg = { .type = CLIENT_SPAWN }; + clientMessage(&msg); +} + static uint8_t inputColor = COLOR_WHITE; static void colorFg(uint8_t fg) { @@ -180,6 +185,7 @@ static void readInput(void) { case ESC: mode = MODE_NORMAL; break; case 'q': endwin(); exit(EX_OK); + case 'Q': clientSpawn(); break; case 'a': clientMove(1, 0); // fallthrough case 'i': insertMode(1, 0); break; diff --git a/server.c b/server.c index c86f122..50a9e9c 100644 --- a/server.c +++ b/server.c @@ -97,10 +97,10 @@ static struct Client *clientAdd(int fd) { if (!client) err(EX_OSERR, "malloc"); client->fd = fd; - client->tileX = TILE_INIT_X; - client->tileY = TILE_INIT_Y; - client->cellX = CELL_INIT_X; - client->cellY = CELL_INIT_Y; + client->tileX = UINT32_MAX; + client->tileY = UINT32_MAX; + client->cellX = UINT8_MAX; + client->cellY = UINT8_MAX; client->prev = NULL; if (clientHead) { @@ -179,34 +179,14 @@ static bool clientCursors(const struct Client *client) { return true; } -static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { - struct Client old = *client; - - if (dx > CELL_COLS - client->cellX) dx = CELL_COLS - client->cellX; - if (dx < -client->cellX - 1) dx = -client->cellX - 1; - if (dy > CELL_ROWS - client->cellY) dy = CELL_ROWS - client->cellY; - if (dy < -client->cellY - 1) dy = -client->cellY - 1; - - client->cellX += dx; - client->cellY += dy; - - if (client->cellX == CELL_COLS) { client->tileX++; client->cellX = 0; } - if (client->cellX == UINT8_MAX) { client->tileX--; client->cellX = CELL_COLS - 1; } - if (client->cellY == CELL_ROWS) { client->tileY++; client->cellY = 0; } - if (client->cellY == UINT8_MAX) { client->tileY--; client->cellY = CELL_ROWS - 1; } - - if (client->tileX == TILE_COLS) client->tileX = 0; - if (client->tileX == UINT32_MAX) client->tileX = TILE_COLS - 1; - if (client->tileY == TILE_ROWS) client->tileY = 0; - if (client->tileY == UINT32_MAX) client->tileY = TILE_ROWS - 1; - +static bool clientUpdate(struct Client *client, struct Client *old) { struct ServerMessage msg = { .type = SERVER_MOVE, .data.m = { .cellX = client->cellX, .cellY = client->cellY }, }; if (!clientSend(client, &msg)) return false; - if (client->tileX != old.tileX || client->tileY != old.tileY) { + if (client->tileX != old->tileX || client->tileY != old->tileY) { msg.type = SERVER_TILE; if (!clientSend(client, &msg)) return false; @@ -215,11 +195,11 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { msg = (struct ServerMessage) { .type = SERVER_CURSOR, .data.c = { - .oldCellX = old.cellX, .oldCellY = old.cellY, + .oldCellX = old->cellX, .oldCellY = old->cellY, .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE, }, }; - clientCast(&old, &msg); + clientCast(old, &msg); msg = (struct ServerMessage) { .type = SERVER_CURSOR, @@ -234,7 +214,7 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { msg = (struct ServerMessage) { .type = SERVER_CURSOR, .data.c = { - .oldCellX = old.cellX, .oldCellY = old.cellY, + .oldCellX = old->cellX, .oldCellY = old->cellY, .newCellX = client->cellX, .newCellY = client->cellY, }, }; @@ -244,6 +224,39 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) { return true; } +static bool clientSpawn(struct Client *client) { + struct Client old = *client; + client->tileX = TILE_INIT_X; + client->tileY = TILE_INIT_Y; + 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; + + if (dx > CELL_COLS - client->cellX) dx = CELL_COLS - client->cellX; + if (dx < -client->cellX - 1) dx = -client->cellX - 1; + if (dy > CELL_ROWS - client->cellY) dy = CELL_ROWS - client->cellY; + if (dy < -client->cellY - 1) dy = -client->cellY - 1; + + client->cellX += dx; + client->cellY += dy; + + if (client->cellX == CELL_COLS) { client->tileX++; client->cellX = 0; } + if (client->cellX == UINT8_MAX) { client->tileX--; client->cellX = CELL_COLS - 1; } + if (client->cellY == CELL_ROWS) { client->tileY++; client->cellY = 0; } + if (client->cellY == UINT8_MAX) { client->tileY--; client->cellY = CELL_ROWS - 1; } + + if (client->tileX == TILE_COLS) client->tileX = 0; + if (client->tileX == UINT32_MAX) client->tileX = TILE_COLS - 1; + if (client->tileY == TILE_ROWS) client->tileY = 0; + if (client->tileY == UINT32_MAX) client->tileY = TILE_ROWS - 1; + + return clientUpdate(client, &old); +} + static bool clientPut(const struct Client *client, uint8_t color, char cell) { struct Tile *tile = tileModify(client->tileX, client->tileY); tile->colors[client->cellY][client->cellX] = color; @@ -319,6 +332,8 @@ int main() { success = clientMove(client, msg.data.m.dx, msg.data.m.dy); } else if (msg.type == CLIENT_PUT) { success = clientPut(client, msg.data.p.color, msg.data.p.cell); + } else if (msg.type == CLIENT_SPAWN) { + success = clientSpawn(client); } if (!success) clientRemove(client); @@ -344,10 +359,6 @@ int main() { nevents = kevent(kq, &event, 1, NULL, 0, NULL); if (nevents < 0) err(EX_OSERR, "kevent"); - struct ServerMessage msg = { .type = SERVER_TILE }; - bool success = clientSend(client, &msg) - && clientMove(client, 0, 0) - && clientCursors(client); - if (!success) clientRemove(client); + if (!clientSpawn(client)) clientRemove(client); } } diff --git a/torus.h b/torus.h index 4943afc..b03a1db 100644 --- a/torus.h +++ b/torus.h @@ -95,6 +95,7 @@ struct ServerMessage { enum ClientMessageType { CLIENT_MOVE, CLIENT_PUT, + CLIENT_SPAWN, }; struct ClientMessage { |