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
|
#include "stdafx.h"
#include "MultiPlayerChunkCache.h"
#include "ServerChunkCache.h"
#include "..\Minecraft.World\net.minecraft.world.level.chunk.h"
#include "..\Minecraft.World\net.minecraft.world.level.dimension.h"
#include "..\Minecraft.World\Arrays.h"
#include "..\Minecraft.World\StringHelpers.h"
#include "MinecraftServer.h"
#include "ServerLevel.h"
#include "..\Minecraft.World\Tile.h"
#include "..\Minecraft.World\WaterLevelChunk.h"
MultiPlayerChunkCache::MultiPlayerChunkCache(Level *level)
{
XZSIZE = level->dimension->getXZSize(); // 4J Added
XZOFFSET = XZSIZE/2; // 4J Added
m_XZSize = XZSIZE;
hasData = new bool[XZSIZE * XZSIZE];
memset(hasData, 0, sizeof(bool) * XZSIZE * XZSIZE);
emptyChunk = new EmptyLevelChunk(level, byteArray(16 * 16 * Level::maxBuildHeight), 0, 0);
// For normal world dimension, create a chunk that can be used to create the illusion of infinite water at the edge of the world
if( level->dimension->id == 0 )
{
byteArray bytes = byteArray(16 * 16 * 128);
// Superflat.... make grass, not water...
if(level->getLevelData()->getGenerator() == LevelType::lvl_flat)
{
for( int x = 0; x < 16; x++ )
for( int y = 0; y < 128; y++ )
for( int z = 0; z < 16; z++ )
{
unsigned char tileId = 0;
if( y == 3 ) tileId = Tile::grass_Id;
else if( y <= 2 ) tileId = Tile::dirt_Id;
bytes[x << 11 | z << 7 | y] = tileId;
}
}
else
{
for( int x = 0; x < 16; x++ )
for( int y = 0; y < 128; y++ )
for( int z = 0; z < 16; z++ )
{
unsigned char tileId = 0;
if( y <= ( level->getSeaLevel() - 10 ) ) tileId = Tile::stone_Id;
else if( y < level->getSeaLevel() ) tileId = Tile::calmWater_Id;
bytes[x << 11 | z << 7 | y] = tileId;
}
}
waterChunk = new WaterLevelChunk(level, bytes, 0, 0);
delete[] bytes.data;
if(level->getLevelData()->getGenerator() == LevelType::lvl_flat)
{
for( int x = 0; x < 16; x++ )
for( int y = 0; y < 128; y++ )
for( int z = 0; z < 16; z++ )
{
if( y >= 3 )
{
static_cast<WaterLevelChunk *>(waterChunk)->setLevelChunkBrightness(LightLayer::Sky,x,y,z,15);
}
}
}
else
{
for( int x = 0; x < 16; x++ )
for( int y = 0; y < 128; y++ )
for( int z = 0; z < 16; z++ )
{
if( y >= ( level->getSeaLevel() - 1 ) )
{
static_cast<WaterLevelChunk *>(waterChunk)->setLevelChunkBrightness(LightLayer::Sky,x,y,z,15);
}
else
{
static_cast<WaterLevelChunk *>(waterChunk)->setLevelChunkBrightness(LightLayer::Sky,x,y,z,2);
}
}
}
}
else
{
waterChunk = nullptr;
}
this->level = level;
this->cache = new LevelChunk *[XZSIZE * XZSIZE];
memset(this->cache, 0, XZSIZE * XZSIZE * sizeof(LevelChunk *));
InitializeCriticalSectionAndSpinCount(&m_csLoadCreate,4000);
}
MultiPlayerChunkCache::~MultiPlayerChunkCache()
{
delete emptyChunk;
delete waterChunk;
delete cache;
delete hasData;
for (auto& it : loadedChunkList)
{
if ( it )
delete it;
}
DeleteCriticalSection(&m_csLoadCreate);
}
bool MultiPlayerChunkCache::hasChunk(int x, int z)
{
// This cache always claims to have chunks, although it might actually just return empty data if it doesn't have anything
return true;
}
// 4J added - find out if we actually really do have a chunk in our cache
bool MultiPlayerChunkCache::reallyHasChunk(int x, int z)
{
int ix = x + XZOFFSET;
int iz = z + XZOFFSET;
// Check we're in range of the stored level - if we aren't, then consider that we do have that chunk as we'll be able to use the water chunk there
if( ( ix < 0 ) || ( ix >= XZSIZE ) ) return true;
if( ( iz < 0 ) || ( iz >= XZSIZE ) ) return true;
int idx = ix * XZSIZE + iz;
LevelChunk *chunk = cache[idx];
if( chunk == nullptr )
{
return false;
}
return hasData[idx];
}
void MultiPlayerChunkCache::drop(int x, int z)
{
// 4J Stu - We do want to drop any entities in the chunks, especially for the case when a player is dead as they will
// not get the RemoveEntity packet if an entity is removed.
LevelChunk *chunk = getChunk(x, z);
if (!chunk->isEmpty())
{
// Added parameter here specifies that we don't want to delete tile entities, as they won't get recreated unless they've got update packets
// The tile entities are in general only created on the client by virtue of the chunk rebuild
chunk->unload(false);
// 4J - We just want to clear out the entities in the chunk, but everything else should be valid
chunk->loaded = true;
}
}
LevelChunk *MultiPlayerChunkCache::create(int x, int z)
{
int ix = x + XZOFFSET;
int iz = z + XZOFFSET;
// Check we're in range of the stored level
if( ( ix < 0 ) || ( ix >= XZSIZE ) ) return ( waterChunk ? waterChunk : emptyChunk );
if( ( iz < 0 ) || ( iz >= XZSIZE ) ) return ( waterChunk ? waterChunk : emptyChunk );
int idx = ix * XZSIZE + iz;
LevelChunk *chunk = cache[idx];
LevelChunk *lastChunk = chunk;
if( chunk == nullptr )
{
EnterCriticalSection(&m_csLoadCreate);
//LevelChunk *chunk;
if( g_NetworkManager.IsHost() ) // force here to disable sharing of data
{
// 4J-JEV: We are about to use shared data, abort if the server is stopped and the data is deleted.
if (MinecraftServer::getInstance()->serverHalted()) return nullptr;
// If we're the host, then don't create the chunk, share data from the server's copy
#ifdef _LARGE_WORLDS
LevelChunk *serverChunk = MinecraftServer::getInstance()->getLevel(level->dimension->id)->cache->getChunkLoadedOrUnloaded(x,z);
#else
LevelChunk *serverChunk = MinecraftServer::getInstance()->getLevel(level->dimension->id)->cache->getChunk(x,z);
#endif
chunk = new LevelChunk(level, x, z, serverChunk);
// Let renderer know that this chunk has been created - it might have made render data from the EmptyChunk if it got to a chunk before the server sent it
level->setTilesDirty( x * 16 , 0 , z * 16 , x * 16 + 15, 127, z * 16 + 15);
hasData[idx] = true;
}
else
{
// Passing an empty array into the LevelChunk ctor, which it now detects and sets up the chunk as compressed & empty
byteArray bytes;
chunk = new LevelChunk(level, bytes, x, z);
// 4J - changed to use new methods for lighting
chunk->setSkyLightDataAllBright();
// Arrays::fill(chunk->skyLight->data, (byte) 255);
}
chunk->loaded = true;
LeaveCriticalSection(&m_csLoadCreate);
#if ( defined _WIN64 || defined __LP64__ )
if( InterlockedCompareExchangeRelease64((LONG64 *)&cache[idx],(LONG64)chunk,(LONG64)lastChunk) == (LONG64)lastChunk )
#else
if( InterlockedCompareExchangeRelease((LONG *)&cache[idx],(LONG)chunk,(LONG)lastChunk) == (LONG)lastChunk )
#endif // _DURANGO
{
// If we're sharing with the server, we'll need to calculate our heightmap now, which isn't shared. If we aren't sharing with the server,
// then this will be calculated when the chunk data arrives.
if( g_NetworkManager.IsHost() )
{
chunk->recalcHeightmapOnly();
}
// Successfully updated the cache
EnterCriticalSection(&m_csLoadCreate);
loadedChunkList.push_back(chunk);
LeaveCriticalSection(&m_csLoadCreate);
}
else
{
// Something else must have updated the cache. Return that chunk and discard this one. This really shouldn't be happening
// in multiplayer
delete chunk;
return cache[idx];
}
}
else
{
chunk->load();
}
return chunk;
}
LevelChunk *MultiPlayerChunkCache::getChunk(int x, int z)
{
int ix = x + XZOFFSET;
int iz = z + XZOFFSET;
// Check we're in range of the stored level
if( ( ix < 0 ) || ( ix >= XZSIZE ) ) return ( waterChunk ? waterChunk : emptyChunk );
if( ( iz < 0 ) || ( iz >= XZSIZE ) ) return ( waterChunk ? waterChunk : emptyChunk );
int idx = ix * XZSIZE + iz;
LevelChunk *chunk = cache[idx];
if( chunk == nullptr )
{
return emptyChunk;
}
else
{
return chunk;
}
}
bool MultiPlayerChunkCache::save(bool force, ProgressListener *progressListener)
{
return true;
}
bool MultiPlayerChunkCache::tick()
{
return false;
}
bool MultiPlayerChunkCache::shouldSave()
{
return false;
}
void MultiPlayerChunkCache::postProcess(ChunkSource *parent, int x, int z)
{
}
vector<Biome::MobSpawnerData *> *MultiPlayerChunkCache::getMobsAt(MobCategory *mobCategory, int x, int y, int z)
{
return nullptr;
}
TilePos *MultiPlayerChunkCache::findNearestMapFeature(Level *level, const wstring &featureName, int x, int y, int z)
{
return nullptr;
}
void MultiPlayerChunkCache::recreateLogicStructuresForChunk(int chunkX, int chunkZ)
{
}
wstring MultiPlayerChunkCache::gatherStats()
{
EnterCriticalSection(&m_csLoadCreate);
int size = static_cast<int>(loadedChunkList.size());
LeaveCriticalSection(&m_csLoadCreate);
return L"MultiplayerChunkCache: " + std::to_wstring(size);
}
void MultiPlayerChunkCache::dataReceived(int x, int z)
{
int ix = x + XZOFFSET;
int iz = z + XZOFFSET;
// Check we're in range of the stored level
if( ( ix < 0 ) || ( ix >= XZSIZE ) ) return;
if( ( iz < 0 ) || ( iz >= XZSIZE ) ) return;
int idx = ix * XZSIZE + iz;
hasData[idx] = true;
}
|