about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-07-17 23:23:16 -0400
committerJune McEnroe <june@causal.agency>2018-07-17 23:35:21 -0400
commit8e4d15cf8fdf389a34666b5df47255c972478209 (patch)
treef9de04153bd6592461af82bc22ba817121cdfbc3
parentTabify source (diff)
downloadtorus-8e4d15cf8fdf389a34666b5df47255c972478209.tar.gz
torus-8e4d15cf8fdf389a34666b5df47255c972478209.zip
Add server map generation
-rw-r--r--server.c42
-rw-r--r--torus.h17
2 files changed, 53 insertions, 6 deletions
diff --git a/server.c b/server.c
index b7970bf..7312dba 100644
--- a/server.c
+++ b/server.c
@@ -287,6 +287,33 @@ static bool clientPut(const struct Client *client, uint8_t color, char cell) {
 	return success;
 }
 
+static bool clientMap(const struct Client *client) {
+	int32_t mapY = (int32_t)client->tileY - MAP_ROWS / 2;
+	int32_t mapX = (int32_t)client->tileX - MAP_COLS / 2;
+
+	struct Map map;
+	for (int32_t y = 0; y < MAP_ROWS; ++y) {
+		for (int32_t x = 0; x < MAP_COLS; ++x) {
+			uint32_t tileY = ((mapY + y) % TILE_ROWS + TILE_ROWS) % TILE_ROWS;
+			uint32_t tileX = ((mapX + x) % TILE_COLS + TILE_COLS) % TILE_COLS;
+
+			const struct Tile *tile = &tiles[tileY * TILE_ROWS + tileX];
+			map.tiles[y][x] = (struct MapTile) {
+				.createTime = tile->createTime,
+				.modifyTime = tile->modifyTime,
+				.accessTime = tile->accessTime,
+				.modifyCount = tile->modifyCount,
+				.accessCount = tile->accessCount,
+			};
+		}
+	}
+
+	struct ServerMessage msg = { .type = SERVER_MAP };
+	if (!clientSend(client, msg)) return false;
+	if (0 > send(client->fd, &map, sizeof(map), 0)) return false;
+	return true;
+}
+
 int main() {
 	int error;
 
@@ -362,12 +389,15 @@ int main() {
 		}
 
 		bool success = false;
-		if (msg.type == CLIENT_MOVE) {
-			success = clientMove(client, msg.move.dx, msg.move.dy);
-		} else if (msg.type == CLIENT_PUT) {
-			success = clientPut(client, msg.put.color, msg.put.cell);
-		} else if (msg.type == CLIENT_SPAWN) {
-			success = clientSpawn(client, msg.spawn);
+		switch (msg.type) {
+			break; case CLIENT_MOVE: {
+				success = clientMove(client, msg.move.dx, msg.move.dy);
+			}
+			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);
 		}
 		if (!success) clientRemove(client);
 	}
diff --git a/torus.h b/torus.h
index 3538213..c058a4d 100644
--- a/torus.h
+++ b/torus.h
@@ -88,12 +88,28 @@ static const struct {
 };
 static const size_t SPAWNS_LEN = sizeof(SPAWNS) / sizeof(SPAWNS[0]);
 
+enum {
+	MAP_ROWS = 11,
+	MAP_COLS = 11,
+};
+
+struct Map {
+	struct MapTile {
+		time_t createTime;
+		time_t modifyTime;
+		time_t accessTime;
+		uint32_t modifyCount;
+		uint32_t accessCount;
+	} tiles[MAP_ROWS][MAP_COLS];
+};
+
 struct ServerMessage {
 	enum PACKED {
 		SERVER_TILE,
 		SERVER_MOVE,
 		SERVER_PUT,
 		SERVER_CURSOR,
+		SERVER_MAP,
 	} type;
 	union {
 		struct {
@@ -122,6 +138,7 @@ struct ClientMessage {
 		CLIENT_MOVE,
 		CLIENT_PUT,
 		CLIENT_SPAWN,
+		CLIENT_MAP,
 	} type;
 	union {
 		struct {