4 #include "Config.hh" |
4 #include "Config.hh" |
5 #include "../Engine.hh" |
5 #include "../Engine.hh" |
6 #include "../Logger.hh" |
6 #include "../Logger.hh" |
7 |
7 |
8 #include <cassert> |
8 #include <cassert> |
|
9 #include <zlib.h> |
9 |
10 |
10 NetworkClient::NetworkClient (Engine &engine, GameState &state, const NetworkEndpoint &connect_to) : |
11 NetworkClient::NetworkClient (Engine &engine, GameState &state, const NetworkEndpoint &connect_to) : |
11 engine(engine), state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), |
12 engine(engine), state(state), netsession(NETWORK_MAGIC_ID), server(netsession.connect(connect_to)), |
12 controller(*this) |
13 controller(*this) |
13 { |
14 { |
27 return; |
28 return; |
28 |
29 |
29 Terrain &terrain = state.world; |
30 Terrain &terrain = state.world; |
30 |
31 |
31 // read map width/height |
32 // read map width/height |
32 // XXX: over 2**31? |
|
33 PixelDimension map_w = pkt.read_uint32(); |
33 PixelDimension map_w = pkt.read_uint32(); |
34 PixelDimension map_h = pkt.read_uint32(); |
34 PixelDimension map_h = pkt.read_uint32(); |
35 |
35 |
36 // read map data |
36 // the terrain byte array |
|
37 size_t terrain_size = map_w * map_h; |
|
38 uint8_t terrain_buf[map_w][map_h]; |
|
39 |
|
40 // compressed packet data |
|
41 unsigned long inflate_size = terrain_size; |
|
42 unsigned long deflate_size = pkt.tell_remaining(); |
|
43 const uint8_t *deflate_ptr = (const uint8_t *) pkt.read_ptr(deflate_size); |
|
44 |
|
45 // uncompress the rest of the packet data |
|
46 if (uncompress((uint8_t *) terrain_buf, &inflate_size, deflate_ptr, deflate_size) != Z_OK) |
|
47 throw Error("uncompress"); |
|
48 |
|
49 // invalid data? |
|
50 if (inflate_size != terrain_size) |
|
51 throw Error("Corrupt terrain data"); |
|
52 |
|
53 // translate map data to terrain vector |
37 for (PixelDimension x = 0; x < map_w; x++) { |
54 for (PixelDimension x = 0; x < map_w; x++) { |
38 for (PixelDimension y = 0; y < map_h; y++) { |
55 for (PixelDimension y = 0; y < map_h; y++) { |
39 terrain.terrain[x][y] = (TerrainType) pkt.read_uint8(); |
56 terrain.terrain[x][y] = (TerrainType) terrain_buf[x][y]; |
40 } |
57 } |
41 } |
58 } |
42 |
59 |
43 // update the pixbuf |
60 // update the pixbuf |
44 terrain.generatePixelBuffer(); |
61 terrain.generatePixelBuffer(); |