summary refs log tree commit diff homepage
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--server.c79
1 files changed, 45 insertions, 34 deletions
diff --git a/server.c b/server.c
index d2797c9..a22046f 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);
     }
 }