-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPacketMapChunk.h
More file actions
122 lines (105 loc) · 2.84 KB
/
PacketMapChunk.h
File metadata and controls
122 lines (105 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
#include "Packet.h"
#include "protocol.h"
extern "C"
{
#include "zlib.h"
}
class Packet_MapChunk : public Packet
{
private:
int32_t x;
int16_t y;
int32_t z;
int8_t size_x;
int8_t size_y;
int8_t size_z;
int32_t compressedSize;
unsigned char *compressedData;
unsigned char *uncompressedData;
public:
Packet_MapChunk() : x(0),y(0),z(0),size_x(0),size_y(0),size_z(0),compressedSize(0),compressedData(NULL) {}
~Packet_MapChunk()
{
delete[] compressedData;
}
bool ReadPacket(SOCKET s)
{
x = ReadInt(s);
y = ReadShort(s);
z = ReadInt(s);
size_x = ReadByte(s);
size_y = ReadByte(s);
size_z = ReadByte(s);
compressedSize = ReadInt(s);
compressedData = new unsigned char[compressedSize];
RecvAll(s,(char*)compressedData,compressedSize,0);
int decompressedSize = (int)((float)((size_x+1) * (size_y+1) * (size_z+1)) * 2.5);
uncompressedData = new unsigned char[decompressedSize];
z_stream zInfo;
memset(&zInfo,0,sizeof(z_stream));
zInfo.zalloc = Z_NULL;
zInfo.zfree = Z_NULL;
zInfo.opaque = Z_NULL;
zInfo.total_in= 0; zInfo.avail_in= compressedSize;
zInfo.total_out= 0; zInfo.avail_out= decompressedSize;
zInfo.next_in= compressedData;
zInfo.next_out= uncompressedData;
if(!dumpingWorld)
return true;
if(inflateInit(&zInfo) == Z_OK)
{
int ret = inflate( &zInfo, Z_FINISH );
if ( ret != Z_STREAM_END || zInfo.total_out != ((size_x+1)*(size_y+1)*(size_z+1)*2.5))
{
printf("Decompressed size does not match packet\n");
}
}
else
{
printf("Failed to initalize deflate\n");
}
deflateEnd( &zInfo );
return true;
}
void WritePacket(SOCKET s)
{
WriteInt(s,x);
WriteShort(s,y);
WriteInt(s,z);
WriteByte(s,size_x);
WriteByte(s,size_y);
WriteByte(s,size_z);
WriteInt(s,compressedSize);
send(s,(char*)compressedData,compressedSize,0);
}
void Process()
{
if(!dumpingWorld)
return;
int arraysize = ((size_x+1)*(size_y+1)*(size_z+1));
unsigned char *typedata = uncompressedData;
unsigned char *metadata = uncompressedData + arraysize;
unsigned char *blocklight = uncompressedData + (int)((float)arraysize * 1.5);
unsigned char *skylight = uncompressedData + arraysize * 2;
Chunk *chunk = g_level.chunks.getChunk(x/16,z/16);
if(chunk == NULL)
{
printf("Unable to find chunk %d,%d in hashmap\n",x,z);
}
// not full chunk
if(size_x != 15 || size_y != 127 || size_z != 15)
return;
chunk->populated = true;
memcpy(chunk->blockTypes,typedata,arraysize);
memcpy(chunk->metadata,metadata,arraysize/2);
memcpy(chunk->blocklight,blocklight,arraysize/2);
memcpy(chunk->skylight,skylight,arraysize/2);
}
void Print(FILE *fp)
{
fprintf(fp, "MapChunk ( x = %d, y = %d, z = %d, size_x = %d, size_y = %d, size_z = %d, compressedSize = %d )",
x,(int)y,z,(int)size_x,(int)size_y,(int)size_z,(int)compressedSize);
}
};
REGISTER_PACKET(MapChunk);