forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldsave.cpp
332 lines (303 loc) · 8.25 KB
/
worldsave.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright (c) 2013, Sean Kasun
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Saves the world to PNG. It doesn't use stock PNG code because
the resulting image might be too large to fit into RAM. Therefore,
it uses a custom PNG generator that will handle *huge* worlds, but
make less-than-optimal PNGs.
*/
#include "worldsave.h"
#include "mapview.h"
#include <zlib.h>
WorldSave::WorldSave(QString filename,MapView *map)
{
this->filename=filename;
this->map=map;
}
WorldSave::~WorldSave()
{
}
static inline void w32(char *p,quint32 v)
{
*p++=v>>24;
*p++=(v>>16)&0xff;
*p++=(v>>8)&0xff;
*p++=v&0xff;
}
static void writeChunk(QFile &f,const char *tag,const char *data,int len)
{
char dword[4];
w32(dword,len);
f.write(dword,4);
f.write(tag,4);
if (len!=0)
f.write(data,len);
quint32 crc=crc32(0,Z_NULL,0);
crc=crc32(crc,(const Bytef *)tag,4);
if (len!=0)
crc=crc32(crc,(const Bytef *)data,len);
w32(dword,crc);
f.write(dword,4);
}
void WorldSave::run()
{
emit progress(tr("Calculating world bounds"),0.0);
QString path=map->getWorldPath();
int top,left,right,bottom;
findBounds(path,&top,&left,&bottom,&right);
int width=(right+1-left)*16;
int height=(bottom+1-top)*16;
QFile png(filename);
png.open(QIODevice::WriteOnly);
//output PNG signature
const char *sig="\x89PNG\x0d\x0a\x1a\x0a";
png.write(sig,8);
//output PNG header
const char *ihdrdata="\x00\x00\x00\x00" //width
"\x00\x00\x00\x00" //height
"\x08" //bit depth
"\x06" //color type (rgba)
"\x00" //compresion method (deflate)
"\x00" //filter method (standard)
"\x00"; //interlace method (none)
char ihdr[13];
memcpy(ihdr,ihdrdata,13);
w32(ihdr,width);
w32(ihdr+4,height);
writeChunk(png,"IHDR",ihdr,13);
int insize=width*16*4+16;
int outsize=insize*2;
uchar *scanlines=new uchar[insize];
for (int i=0;i<16;i++) //set scanline filters to off
scanlines[i*(width*4+1)]=0;
uchar *compressed=new uchar[outsize];
z_stream strm;
strm.zalloc=Z_NULL;
strm.zfree=Z_NULL;
strm.opaque=Z_NULL;
deflateInit2(&strm,6,Z_DEFLATED,15,8,Z_DEFAULT_STRATEGY);
double maximum=(bottom+1-top)*(right+1-left);
double step=0.0;
for (int z=top;z<=bottom;z++)
{
for (int x=left;x<=right;x++,step+=1.0)
{
emit progress(tr("Rendering world"),step/maximum);
int rx=x>>5;
int rz=z>>5;
QFile f(path+"/region/r."+QString::number(rx)+"."+QString::number(rz)+".mca");
if (!f.open(QIODevice::ReadOnly))
{
blankChunk(scanlines,width*4+1,x-left);
continue;
}
uchar *header=f.map(0,4096);
int offset=4*((x&31)+(z&31)*32);
int coffset=(header[offset]<<16)|(header[offset+1]<<8)|header[offset+2];
int numSectors=header[offset+3];
f.unmap(header);
if (coffset==0) //no chunk here
blankChunk(scanlines,width*4+1,x-left);
else
{
uchar *raw=f.map(coffset*4096,numSectors*4096);
NBT nbt(raw);
Chunk *chunk=new Chunk();
chunk->load(nbt);
f.unmap(raw);
drawChunk(scanlines,width*4+1,x-left,chunk);
delete chunk;
}
f.close();
}
//write out scanlines to disk
strm.avail_in=insize;
strm.next_in=scanlines;
do {
strm.avail_out=outsize;
strm.next_out=compressed;
deflate(&strm,(z==bottom)?Z_FINISH:Z_NO_FLUSH);
writeChunk(png,"IDAT",(const char *)compressed,outsize-strm.avail_out);
} while (strm.avail_out==0);
}
deflateEnd(&strm);
delete [] scanlines;
delete [] compressed;
writeChunk(png,"IEND",NULL,0);
png.close();
emit finished();
}
typedef struct {
int x,z;
} ChunkPos;
//helper functions for the findBounds function
static bool outside(int side,ChunkPos &edge,ChunkPos &p)
{
switch (side)
{
case 0: //top
return edge.z>p.z;
case 1: //left
return edge.x>p.x;
case 2: //bottom
return edge.z<p.z;
default: //right
return edge.x<p.x;
}
}
static bool onside(int side,ChunkPos &edge,ChunkPos &p)
{
switch (side)
{
case 0: //top or bottom
case 2:
return edge.z==p.z;
default: //left or right
return edge.x==p.x;
}
}
/*
This routine loops through all the region filenames and constructs a list
of regions that lie along the edges of the map. Then it loops through
those region headers and finds the furthest chunks. The end result is
the boundary chunks for the entire world.
Because we only check at the chunk level, there could be up to 15 pixels
of padding around the edge of the final image. However, if we just
went by regions, there could be 511 pixels of padding.
*/
void WorldSave::findBounds(QString path, int *top, int *left, int *bottom, int *right)
{
QStringList filters;
filters<<"*.mca";
QDirIterator it(path+"/region",filters);
QList<ChunkPos> edges[4];
ChunkPos cur;
bool hasOne=false;
//loop through all region files and find the extremes
while (it.hasNext())
{
QString fn=it.next();
int pos=fn.length()-5;
ushort c;
//figure out the x/z of the region
for (int i=0;i<2;i++)
{
int p=0;
int scale=1;
while (pos>0 && (c=fn.at(pos).unicode())!='.')
{
if (c=='-')
p=-p;
else
p+=(c-'0')*scale;
pos--;
scale*=10;
}
pos--;
if (i==0) cur.z=p;
else cur.x=p;
}
if (!hasOne)
{
for (int e=0;e<4;e++)
edges[e].append(cur);
hasOne=true;
}
for (int e=0;e<4;e++)
if (outside(e,edges[e].front(),cur))
{
edges[e].clear();
edges[e].append(cur);
}
else if (onside(e,edges[e].front(),cur))
edges[e].append(cur);
}
//find image bounds
int minz=32,maxz=0,minx=32,maxx=0;
for (int e=0;e<4;e++)
{
for (int i=0;i<edges[e].length();i++)
{
QFile f(path+"/region/r."+
QString::number(edges[e].at(i).x)+
"."+QString::number(edges[e].at(i).z)+".mca");
f.open(QIODevice::ReadOnly);
uchar *header=f.map(0,4096);
//loop through all chunk headers.
for (int offset=0;offset<4096;offset+=4)
{
int coffset=(header[offset]<<16)|(header[offset+1]<<8)|header[offset+2];
if (coffset!=0)
{
switch (e)
{
case 0: //smallest Z
if (minz>offset/128) minz=offset/128;
break;
case 1: //smallest X
if (minx>(offset&127)/4) minx=(offset&127)/4;
break;
case 2: //largest Z
if (maxz<offset/128) maxz=offset/128;
break;
case 3: //largest X
if (maxx<(offset&127)/4) maxx=(offset&127)/4;
break;
}
}
}
f.unmap(header);
f.close();
}
}
*top=(edges[0].front().z*32)+minz;
*left=(edges[1].front().x*32)+minx;
*bottom=(edges[2].front().z*32)+maxz;
*right=(edges[3].front().x*32)+maxx;
}
// sets chunk to transparent
void WorldSave::blankChunk(uchar *scanlines, int stride, int x)
{
int offset=x*16*4+1;
for (int y=0;y<16;y++,offset+=stride)
memset(scanlines+offset,0,16*4);
}
void WorldSave::drawChunk(uchar *scanlines, int stride, int x, Chunk *chunk)
{
//render chunk with current settings
map->renderChunk(chunk);
//we can't memcpy each scanline because it's in BGRA format.
int offset=x*16*4+1;
int ioffset=0;
for (int y=0;y<16;y++,offset+=stride)
{
int xofs=offset;
for (int x=0;x<16;x++,xofs+=4)
{
scanlines[xofs+2]=chunk->image[ioffset++];
scanlines[xofs+1]=chunk->image[ioffset++];
scanlines[xofs+0]=chunk->image[ioffset++];
scanlines[xofs+3]=chunk->image[ioffset++];
}
}
}