src/Network/Client.cc
changeset 387 294ce7ae8140
parent 381 9b35bc329d23
child 391 59c2499fe7bb
--- a/src/Network/Client.cc	Mon Dec 22 02:37:28 2008 +0000
+++ b/src/Network/Client.cc	Mon Dec 22 02:37:43 2008 +0000
@@ -6,6 +6,7 @@
 #include "../Logger.hh"
 
 #include <cassert>
+#include <zlib.h>
 
 NetworkClient::NetworkClient (Engine &engine, GameState &state, const NetworkEndpoint &connect_to) : 
     engine(engine), state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)),
@@ -29,14 +30,30 @@
     Terrain &terrain = state.world;
 
     // read map width/height
-    // XXX: over 2**31?
     PixelDimension map_w = pkt.read_uint32();
     PixelDimension map_h = pkt.read_uint32();
 
-    // read map data
+    // the terrain byte array
+    size_t terrain_size = map_w * map_h;
+    uint8_t terrain_buf[map_w][map_h];
+    
+    // compressed packet data
+    unsigned long inflate_size = terrain_size;
+    unsigned long deflate_size = pkt.tell_remaining();
+    const uint8_t *deflate_ptr = (const uint8_t *) pkt.read_ptr(deflate_size);
+    
+    // uncompress the rest of the packet data
+    if (uncompress((uint8_t *) terrain_buf, &inflate_size, deflate_ptr, deflate_size) != Z_OK)
+        throw Error("uncompress");
+
+    // invalid data?
+    if (inflate_size != terrain_size)
+        throw Error("Corrupt terrain data");
+
+    // translate map data to terrain vector
     for (PixelDimension x = 0; x < map_w; x++) {
         for (PixelDimension y = 0; y < map_h; y++) {
-            terrain.terrain[x][y] = (TerrainType) pkt.read_uint8();
+            terrain.terrain[x][y] = (TerrainType) terrain_buf[x][y];
         }
     }