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
commitd60d2573ad7ab869d040a438b328d5abc3bcebae (patch)
tree3d31cdeadae3739faebf103eafe53e5e1fc3df39
parentTabify source (diff)
downloadtorus-d60d2573ad7ab869d040a438b328d5abc3bcebae.tar.gz
torus-d60d2573ad7ab869d040a438b328d5abc3bcebae.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 a5b8855..3a27ad4 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 d3c0d81..918a131 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 {