-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.cpp
314 lines (280 loc) · 9.04 KB
/
world.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
/**
* Copyright 2011 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of the CppCraft.
*
* CppCraft is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CppCraft is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CppCraft. If not, see <http://www.gnu.org/licenses/>.
*/
#include "world.h"
#include <iostream>
#include <cmath>
#include "zlib.h"
#include "render.h"
//decorations and specials must be translucent as well
int Block::opacity() {
switch (type) {
case 0: return O_AIR;
case 31: case 32: case 37: case 38:
case 9: case 18: case 20: case 83: return O_TRANSLUCENT;
default: return O_OPAQUE;
}
}
int Block::style() {
switch (type) {
case 31: case 32: case 37: case 38: case 83: return S_DECORATION;
default: return S_BLOCK;
}
}
Chunk::Chunk() : dirty(true),boundarydirty(false), renderdata(NULL), destroy(NULL) {
}
Chunk::~Chunk() {
if (destroy) destroy(this);
}
void Chunk::markDirty() {
dirty = true;
}
void Chunk::markBoundaryDirty() {
boundarydirty = true;
}
bool Chunk::update(int lx, int ly, int lz, int sx, int sy, int sz, int size, char *cdata) {
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
int ret = inflateInit(&strm);
if (ret != Z_OK) return false;
strm.avail_in = size;
strm.next_in = (unsigned char*) cdata;
sx++; sy++; sz++;
int nibblelen = sx*sy*sz/2;
int len = nibblelen*5;
unsigned char *data = new unsigned char[len];
strm.avail_out = len;
strm.next_out = data;
do {
ret = inflate(&strm, Z_NO_FLUSH);
} while (strm.avail_out != 0 && ret == Z_OK);
inflateEnd(&strm);
if (ret != Z_OK && ret != Z_STREAM_END) return false;
//yea, they sub-byte-packed shit, cause THAT compresses well -.-
unsigned char *types = data;
unsigned char *metas = &data[nibblelen*2];
unsigned char *lights = &data[nibblelen*3];
unsigned char *skys = &data[nibblelen*4];
int i = 0;
for (int x = lx; x < sx+lx; x++) {
Block *slice = &blocks[x*16*128];
for (int z = lz; z < sz+lz; z++) {
Block *col = &slice[z*128];
for (int y = ly; y < sy+ly; y++) {
col[y].type = *(types++);
if (i&1) {
col[y].meta = (*(metas++) >> 4) & 0xF;
col[y].light = (*(lights++) >> 4) & 0xF;
col[y].sky = (*(skys++) >> 4) & 0xF;
} else {
col[y].meta = (*(metas)) & 0xF;
col[y].light = (*(lights)) & 0xF;
col[y].sky = (*(skys)) & 0xF;
}
i++;
}
}
}
dirty = true;
delete data;
return true;
}
bool Chunk::update(int size, short *locs, char *types, char *metas) {
for (int i = 0; i < size; i++) {
int x = (*locs >> 12) & 0xF;
int z = (*locs >> 8) & 0xF;
int y = *(locs++) & 0xFF;
blocks[(x*16+z)*128+y].type = *(types++);
blocks[(x*16+z)*128+y].meta = *(metas++);
}
dirty = true;
return true;
}
World::World() {
chunklock = SDL_CreateMutex();
}
World::~World() {
SDL_DestroyMutex(chunklock);
}
void World::lockChunks() {
SDL_mutexP(chunklock);
}
void World::unlockChunks() {
SDL_mutexV(chunklock);
}
void World::facingNormal(double pitch, double yaw, double &fx, double &fy, double &fz) {
fx = cos(-pitch/180.0*3.14159)*cos(yaw/180.0*3.14159);
fz = cos(-pitch/180.0*3.14159)*sin(yaw/180.0*3.14159);
fy = sin(-pitch/180.0*3.14159);
}
Block* World::projectToBlock(double px, double py, double pz, double pitch, double yaw, int &x, int &y, int &z, int &face) {
double fx,fy,fz; facingNormal(pitch,yaw,fx,fy,fz);
int sx = (int)px; int ix = fx < 0 ? -1 : 1, ex = sx + 4*ix;
int sy = (int)py; int iy = fy < 0 ? -1 : 1, ey = sy + 4*iy;
int sz = (int)pz; int iz = fz < 0 ? -1 : 1, ez = sz + 4*iz;
double tmin = 6.0; //max distance to consider
Block *res = NULL;
for (int cx = sx; cx != ex; cx += ix) {
double t = (cx-px)/fx;
if (t > 0 && t <= tmin) {
int tx = fx > 0 ? cx : cx-1;
int ty = (int)floor(py+fy*t);
int tz = (int)floor(pz+fz*t);
Block *b = getBlock(tx,ty,tz);
if (b && b->type) {
tmin = t;
x = tx;
y = ty;
z = tz;
face = fx > 0 ? F_MINUS_X : F_PLUS_X;
res = b;
}
}
}
for (int cy = sy; cy != ey; cy += iy) {
double t = (cy-py)/fy;
if (t > 0 && t <= tmin) {
int tx = (int)floor(px+fx*t);
int ty = fy > 0 ? cy : cy-1;
int tz = (int)floor(pz+fz*t);
Block *b = getBlock(tx,ty,tz);
if (b && b->type) {
tmin = t;
x = tx;
y = ty;
z = tz;
face = fy > 0 ? F_MINUS_Y : F_PLUS_Y;
res = b;
}
}
}
for (int cz = sz; cz != ez; cz += iz) {
double t = (cz-pz)/fz;
if (t > 0 && t <= tmin) {
int tx = (int)floor(px+fx*t);
int ty = (int)floor(py+fy*t);
int tz = fz > 0 ? cz : cz-1;
Block *b = getBlock(tx,ty,tz);
if (b && b->type) {
tmin = t;
x = tx;
y = ty;
z = tz;
face = fz > 0 ? F_MINUS_Z : F_PLUS_Z;
res = b;
}
}
}
return res;
}
bool World::containsSolid(int sx,int sy,int sz,int ex,int ey,int ez) {
int cx, cy, cz, tcx, tcy, tcz,lx,ly,lz;
chunkPos(sx,sy,sz,cx,cy,cz);
Chunk *c = getChunk(sx,sy,sz);
for (int x = sx; x <= ex; x++) {
for (int z = sz; z <= ez; z++) {
for (int y = sy; y <= ey; y++) {
chunkPos(x,y,z,tcx,tcy,tcz);
if (tcx != cx || tcy != cy || tcz != cz) {
cx = tcx; cy = tcy; cz = tcz;
c = getChunk(x,y,z);
}
if (!c) continue;
localPos(x,y,z,lx,ly,lz);
if (!c->getBlock(lx,ly,lz)->passable()) return true;
}
}
}
return false;
}
void World::updateLighting(int x, int y, int z) {
//TODO make the lighting update... will be tedious and the fomula is sort of unknown
Chunk *c = getChunk(x,y,z); if (c) c->markDirty();
}
Block* World::getBlock(int x, int y, int z) {
int lx,ly,lz;
Chunk *c = getChunk(x,y,z);
localPos(x,y,z,lx,ly,lz);
return c ? c->getBlock(lx,ly,lz) : NULL;
}
Chunk* World::getChunk(int x, int y, int z) {
int cx,cy,cz;
chunkPos(x,y,z,cx,cy,cz);
return getChunkIdx(cx,cy,cz);
}
Chunk* World::getChunkIdx(int cx, int cy, int cz) {
std::map<Pos3D,Chunk*>::iterator ci = chunks.find(Pos3D(cx,cy,cz));
Chunk *c = NULL;
if (ci != chunks.end()) {
c = ci->second;
}
return c;
}
bool World::initChunk(int cx, int cy, int cz) {
//does nothing because this is aparantly not always sent by the server
return true;
}
bool World::updateChunk(int x, int y, int z, int sx, int sy, int sz, int size, char *cdata) {
Chunk *c = getChunk(x,y,z);
if (!c) {
lockChunks();
int cx,cy,cz;
chunkPos(x,y,z,cx,cy,cz);
//std::cout << "Creating Chunk (" << std::dec << cx << ',' << cy << ',' << cz << ") " << chunks.size() << '\n';
c = new Chunk();
chunks[Pos3D(cx,cy,cz)] = c;
unlockChunks();
}
int lx,ly,lz;
localPos(x,y,z,lx,ly,lz);
return c->update(lx,ly,lz,sx,sy,sz,size,cdata);
}
bool World::updateChunk(int cx, int cy, int cz, int size, short *locs, char *types, char *metas) {
int x,y,z; worldPos(cx,cy,cz,x,y,z);
Chunk *c = getChunk(x,y,z);
if (!c) return false; //this cannot create a new chunk
return c->update(size,locs,types,metas);
}
bool World::deleteChunk(int cx, int cy, int cz) {
lockChunks();
std::map<Pos3D,Chunk*>::iterator ci = chunks.find(Pos3D(cx,cy,cz));
if (ci == chunks.end()) {
unlockChunks();
return false;
}
//std::cout << "Deleting Chunk (" << std::dec << cx << ',' << cy << ',' << cz << ")\n";
delete ci->second;
chunks.erase(ci);
unlockChunks();
return true;
}
void World::clearChunks() {
lockChunks();
std::map<Pos3D,Chunk*>::iterator ci = chunks.begin();
std::map<Pos3D,Chunk*>::iterator end = chunks.end();
int i = 0;
for ( ; ci != end; ci++, i++) {
delete ci->second;
}
chunks.clear();
unlockChunks();
}