-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.cpp
273 lines (237 loc) · 9.46 KB
/
thread.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
#include <QTimer>
#include <QImage>
#include <QRgb>
#include <QColor>
#include <QFile>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <qcoreapplication.h>
#include "minecraft_object.h"
#include <math.h>
#include "thread.h"
#define THREAD_COUNT 8
#define xyz( x, y, z ) (x * Height + z * Square + y)
#define mxyz( x, y, z ) (z * 128 + x * 2048 + y)
#define dxyz( x, y, z ) (z * 64 + x * 1024 + y)
#define rgb( r, g, b ) (0xFF000000 | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF))
Thread::Thread( char* image, char* dir ) : Image_Filename( image ), Dir( dir )
{
}
void Thread::startThread()
{
QTimer::singleShot(0, this, SLOT(thread()));
}
void Thread::thread()
{
int x, y, z, yt;
int px, pz;
int t1, t2, t3;
QString fx, fz, fux, fuz;
int i;
bool done;
//Load up my image
QImage front( "Front_50.png" );
QImage side( "Side_50.png" );
QImage top( "Top_50.png" );
//Get my basic world info
Width = front.width();
Height = side.height();
Depth = top.width();
Square = Width * Height;
Size = Square * Depth;
//Alloc my world
qDebug("Alloc World: %d\n", Size );
Blocks = new unsigned char[ Size ];
Data = new unsigned char[ Size ];
//Zero all the memory
memset( Blocks, 0, Size );
memset( Data, 0, Size );
BlockComp** block_comp = new BlockComp*[THREAD_COUNT];
for ( i = 0; i < THREAD_COUNT; i++ )
{
block_comp[i] = new BlockComp(Blocks, Data,
Width, Depth, Height,
&front, &side, &top );
//Reset my y to start
if ( i == 0 ) block_comp[i]->reset();
block_comp[i]->start();
}
//Wait until we're done
do {
QThread::yieldCurrentThread();
//Check for being done
for ( done = true, i = 0; done && i < THREAD_COUNT; i++ )
done = (done & block_comp[i]->isFinished());
} while ( !done );
//Create my level file
qDebug("Building Level object\n" );
MinecraftObject level;
level.storeName( QString("") );
level.insert( QString("Data"), McVariable(McHash<McVariable>()) );
level["Data"].insert( QString("Time"), McVariable((qlonglong)0) );
level["Data"].insert( QString("LastPlayed"), McVariable((qlonglong)1106379604) );
level["Data"].insert( QString("Player"), McVariable(McHash<McVariable>()) );
level["Data"]["Player"].insert( QString("Motion"), McVariable(McList<McVariable>()));
level["Data"]["Player"]["Motion"].toList().setType( McVariable::TAG_DOUBLE );
level["Data"]["Player"]["Motion"].push( (double)0 );
level["Data"]["Player"]["Motion"].push( (double)-.078 );
level["Data"]["Player"]["Motion"].push( (double)0 );
level["Data"]["Player"].insert( QString("OnGround"), McVariable((char)0x01));
level["Data"]["Player"].insert( QString("HurtTime"), McVariable((short)0));
level["Data"]["Player"].insert( QString("Health"), McVariable((short)20));
level["Data"]["Player"].insert( QString("Dimension"), McVariable((int)0));
level["Data"]["Player"].insert( QString("Air"), McVariable((short)300));
level["Data"]["Player"].insert(QString("Inventory"),McVariable(McList<McVariable>()));
level["Data"]["Player"].insert(QString("Pos"),McVariable(McList<McVariable>()));
level["Data"]["Player"]["Pos"].toList().setType( McVariable::TAG_DOUBLE );
level["Data"]["Player"]["Pos"].push( (double)1 );
level["Data"]["Player"]["Pos"].push( (double)4.5 );
level["Data"]["Player"]["Pos"].push( (double)1 );
level["Data"]["Player"].insert( QString("AttackTime"),McVariable((short)0));
level["Data"]["Player"].insert( QString("Fire"), McVariable((short)-20));
level["Data"]["Player"].insert( QString("FallDistance"), McVariable((float)0));
level["Data"]["Player"].insert(QString("Rotation"),McVariable(McList<McVariable>()));
level["Data"]["Player"]["Rotation"].toList().setType( McVariable::TAG_FLOAT );
level["Data"]["Player"]["Rotation"].push( (float)-433.350037 );
level["Data"]["Player"]["Rotation"].push( (float)10.650003 );
level["Data"]["Player"].insert( QString("Score"), McVariable((int)0));
level["Data"]["Player"].insert( QString("DeathTime"), McVariable((short)0));
level["Data"].insert( QString("SpawnX"), McVariable((int)1) );
level["Data"].insert( QString("SpawnY"), McVariable((int)3) );
level["Data"].insert( QString("SpawnZ"), McVariable((int)1) );
level["Data"].insert( QString("SizeOnDisk"), McVariable((qlonglong)91700) );
level["Data"].insert( QString("RandomSeed"), McVariable((qlonglong)0x917) );
//Build a map object
MinecraftObject map;
map.storeName( QString("") );
map.insert( QString("Level"), McVariable(McHash<McVariable>()) );
map["Level"].insert( QString("Data"), McVariable(QByteArray()) );
map["Level"].insert( QString("Entities"), McVariable(McList<McVariable>()));
map["Player"]["Entities"].toList().setType( McVariable::TAG_COMPOUND );
map["Level"].insert( QString("LastUpdate"), McVariable((qlonglong)0));
map["Level"].insert( QString("xPos"), McVariable((int)0));
map["Level"].insert( QString("zPos"), McVariable((int)0));
map["Level"].insert(QString("TileEntities"),McVariable(McList<McVariable>()));
map["Player"]["TileEntities"].toList().setType( McVariable::TAG_COMPOUND);
map["Level"].insert( QString("TerrainPopulated"), McVariable((char)0x01));
map["Level"].insert( QString("SkyLight"), McVariable(QByteArray()));
map["Level"].insert( QString("HeightMap"), McVariable(QByteArray()));
map["Level"].insert( QString("BlockLight"), McVariable(QByteArray()));
map["Level"].insert( QString("Blocks"), McVariable(QByteArray()));
//Now that we've read all my data in, write the map out
qDebug("Writing Level\n" );
FILE* handle;
//start piping data
if ( (handle = popen( QString("gzip > %1/level.dat").arg(Dir).toAscii().data(), "w")) == NULL )
{
qDebug("Couldn't write level.dat\n");
QCoreApplication::exit(0);
return;
}
//Write out the data
QByteArray level_dat;
level.load( level_dat );
fwrite( level_dat.data(), 1, level_dat.size(), handle );
pclose( handle );
//Write out the maps
for ( pz = -6; pz < Depth / 16 + 7; pz++ )
for ( px = -6; px < Width / 16 + 7; px++ )
{
//Naming schema for the filesystem inside minecraft/saves
if ( px >= 0 )
{
fx = QString::number( px, 36 );
fux = QString::number( px & 0x3F, 36 );
}
else
{
fx = QString("-%1").arg(QString::number( -px, 36 ));
fux = QString::number( (((~(-px)) & 0xFF) + 1) & 0x3F, 36);
}
if ( pz >= 0 )
{
fz = QString::number( pz, 36 );
fuz = QString::number( pz & 0x3F, 36 );
}
else
{
fz = QString("-%1").arg(QString::number( -pz, 36 ));
fuz = QString::number( (((~(-pz)) & 0xFF) + 1) & 0x3F, 36);
}
QString filename = QString("%1/%2/%3/c.%4.%5.dat").
arg(Dir).arg(fux).arg(fuz).arg(fx).arg(fz);
//Make my directories
int ret;
ret = system(QString("mkdir %1/%2 2> /dev/null").arg(Dir).arg(fux).toAscii().data());
ret = system(QString("mkdir %1/%2/%3 2> /dev/null").arg(Dir).arg(fux).arg(fuz).toAscii().data());
//Start building the map
qDebug("Building Map %s... ", filename.toAscii().data() );
fflush( stdout );
//Reset my data buffers
QByteArray bData;
QByteArray bSkyLight;
QByteArray bHeightMap;
QByteArray bBlockLight;
QByteArray bBlocks;
//Size my stuff
bData.resize( 16 * 16 * 64 );
bSkyLight.resize( 16 * 16 * 64 );
bHeightMap.resize( 16 * 16 );
bBlockLight.resize( 16 * 16 * 64 );
bBlocks.resize( 16 * 16 * 128);
//Fill my arrays, turn all lighting to max
bSkyLight.fill(0xff);
bBlockLight.fill(0xff);
bData.fill(0);
bHeightMap.fill( Height / 2 + 5 );
bBlocks.fill(0);
//Load my object with data
for ( z = 0; z < 16; z++ )
for ( x = 0; x < 16; x++ )
for ( y = 0; y < Height; y++ )
{
//get my t's
yt = (Height - y) + 5;
t1 = mxyz( x, yt, z );
t2 = xyz( (x + px * 16), y, (z + pz * 16) );
t3 = dxyz( x, (yt / 2), z );
//Pull data from my coordinate system, and store it into minecraft
if ( px >= 0 && pz >= 0 &&
x + px * 16 < Width && z + pz * 16 < Depth && y < Height )
{
bBlocks[t1] = Blocks[t2];
bData[t3] = (bData[t3] | ((Data[t2] & 0x0F) << ((yt & 1) * 4)));
}
}
//Lay down a floor
for ( z = 0; z < 16; z++ )
for ( x = 0; x < 16; x++ )
bBlocks[ mxyz( x, 0, z ) ] = 0x07;
//Set the position of this block in minecraft filesystem coordinates
map["Level"]["xPos"] = px;
map["Level"]["zPos"] = pz;
//Store my data to a map object
map["Level"]["Data"] = bData;
map["Level"]["SkyLight"] = bSkyLight;
map["Level"]["HeightMap"] = bHeightMap;
map["Level"]["BlockLight"] = bBlockLight;
map["Level"]["Blocks"] = bBlocks;
//Write out the data
qDebug("Writing\n");
//start piping data
if ( (handle = popen(QString("gzip > %1").arg(filename).toAscii().data(),
"w")) == NULL )
{
qDebug("Couldn't write level.dat\n");
QCoreApplication::exit(0);
return;
}
//Write out the data
QByteArray map_dat;
map.load( map_dat );
fwrite( map_dat.data(), 1, map_dat.size(), handle );
pclose( handle );
}
QCoreApplication::exit(0);
}