summaryrefslogblamecommitdiffstats
path: root/source/cChunk.cpp
blob: 8e03ff6c733a70bd3ab73987ffe855216c0e8776 (plain) (tree)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391























                                     
                              














                                           



                                                                             







































                                                                                                                                        
                                                  






















                                                            
                                                            


























































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                     


                                                                                                                                    
 
                                                                     
 
                                                                                                           






















































































































































                                                                                                                                                                                      

                                                                                             













                                                                                                                              

                                                                                                           




















































































































































































































                                                                                                                                              
                                           



                              
                                             



























































































































































































































































































































                                                                                                                                                                    
#ifndef _WIN32
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <sys/stat.h>   // for mkdir
#include <sys/types.h>
#endif
#include "cChunk.h"
#include "cWorld.h"
#include "cClientHandle.h"
#include "cServer.h"
#include "zlib.h"
#include "Defines.h"
#include <string> // memset
#include "cChestEntity.h"
#include "cFurnaceEntity.h"
#include "cSignEntity.h"
#include "cMCLogger.h"
#include "cTorch.h"
#include "cLadder.h"
#include "cPickup.h"
#include "cItem.h"
#include "cNoise.h"
#include "cRoot.h"
#include "cCriticalSection.h"

#include "cGenSettings.h"

#include "packets/cPacket_DestroyEntity.h"
#include "packets/cPacket_PreChunk.h"
#include "packets/cPacket_BlockChange.h"
#include "packets/cPacket_MapChunk.h"
#include "packets/cPacket_MultiBlock.h"

#include <json/json.h>

#include <list>
#include <vector>
#include <map>

#ifndef _WIN32
#define sprintf_s(dst, size, format, ...) sprintf(dst, format, __VA_ARGS__ )
#endif

extern bool g_bWaterPhysics;


typedef std::list< cFurnaceEntity* > FurnaceEntityList;
typedef std::list< cClientHandle* > ClientHandleList;
typedef std::list< cBlockEntity* > BlockEntityList;
typedef std::list< cEntity* > EntityList;
struct cChunk::sChunkState
{
	std::map< unsigned int, int > m_ToTickBlocks;
	FurnaceEntityList m_TickBlockEntities;
	std::vector< unsigned int > m_PendingSendBlocks;
	ClientHandleList m_LoadedByClient;
	ClientHandleList m_UnloadQuery;
	BlockEntityList m_BlockEntities;
	EntityList m_Entities;
};

cChunk::~cChunk()
{
	//LOG("~cChunk() %i %i %i", m_PosX, m_PosY, m_PosZ );
	for( std::list<cBlockEntity*>::iterator itr = m_pState->m_BlockEntities.begin(); itr != m_pState->m_BlockEntities.end(); ++itr)
	{
		delete *itr;
	}
	m_pState->m_BlockEntities.clear();

	LockEntities();
	for( EntityList::iterator itr = m_pState->m_Entities.begin(); itr != m_pState->m_Entities.end(); ++itr)
	{
		if( (*itr)->GetEntityType() != cEntity::E_PLAYER )
		{
			cRoot::Get()->GetWorld()->AddToRemoveEntityQueue( **itr ); // World also destroys the entity
		}
	}
	m_pState->m_Entities.clear();
	UnlockEntities();

	if( m_EntitiesCriticalSection )
	{
		delete m_EntitiesCriticalSection;
		m_EntitiesCriticalSection = 0;
	}
	delete m_pState;
}

cChunk::cChunk(int a_X, int a_Y, int a_Z)
	: m_pState( new sChunkState )
	, m_bCalculateLighting( false )
	, m_bCalculateHeightmap( false )
	, m_PosX( a_X )
	, m_PosY( a_Y )
	, m_PosZ( a_Z )
	, m_BlockType( m_BlockData ) // Offset the pointers
	, m_BlockMeta( m_BlockType + c_NumBlocks )
	, m_BlockLight( m_BlockMeta + c_NumBlocks/2 )
	, m_BlockSkyLight( m_BlockLight + c_NumBlocks/2 )
	, m_BlockTickNum( 0 )
	, m_BlockTickX( 0 )
	, m_BlockTickY( 0 )
	, m_BlockTickZ( 0 )
	, m_EntitiesCriticalSection( 0 )
{
	//LOG("cChunk::cChunk(%i, %i, %i)", a_X, a_Y, a_Z);
	m_EntitiesCriticalSection = new cCriticalSection();
}

void cChunk::Initialize()
{
	if( !LoadFromDisk() )
	{
		// Clear memory
		memset( m_BlockData, 0x00, c_BlockDataSize );

//		LARGE_INTEGER TicksPerSecond;
//		QueryPerformanceFrequency( &TicksPerSecond );

		GenerateTerrain();

// 		LARGE_INTEGER start;
// 		QueryPerformanceCounter( &start );

		CalculateHeightmap();
		CalculateLighting();

// 		LARGE_INTEGER end;
// 		QueryPerformanceCounter( &end );
// 		double Time = double( end.QuadPart - start.QuadPart ) / double( TicksPerSecond.QuadPart / 1000 );
// 		LOG("Calculated light in %f ms", Time );

		CreateBlockEntities();
	}
	else
	{
		//LOGINFO("Successfully loaded from disk");
		CalculateHeightmap();
	}
}

void cChunk::Tick(float a_Dt)
{
	if( m_bCalculateLighting )
		CalculateLighting();
	if( m_bCalculateHeightmap )
		CalculateHeightmap();

	unsigned int PendingSendBlocks = m_pState->m_PendingSendBlocks.size();
 	if( PendingSendBlocks > 1 )
 	{
 		cPacket_MultiBlock MultiBlock;
 		MultiBlock.m_ChunkX = m_PosX;
 		MultiBlock.m_ChunkZ = m_PosZ;
 		MultiBlock.m_NumBlocks = (short)PendingSendBlocks;
 		MultiBlock.m_BlockCoordinates = new unsigned short[PendingSendBlocks];
 		MultiBlock.m_BlockTypes = new char[PendingSendBlocks];
 		MultiBlock.m_BlockMetas = new char[PendingSendBlocks];
 		//LOG("Sending multiblock packet for %i blocks", PendingSendBlocks );
 		for( unsigned int i = 0; i < PendingSendBlocks; i++)
 		{
 			unsigned int index = m_pState->m_PendingSendBlocks[i];
 			unsigned int Y = index % 128;
 			unsigned int Z = (index / 128) % 16;
 			unsigned int X = (index / (128*16));

 			MultiBlock.m_BlockCoordinates[i] = (Z&0xf) | (X&0xf)<<4 | (Y&0xff)<<8;
 			//LOG("X: %i Y: %i Z: %i Combo: 0x%04x", X, Y, Z, MultiBlock.m_BlockCoordinates[i] );
 			MultiBlock.m_BlockTypes[i] = m_BlockType[index];
 			MultiBlock.m_BlockMetas[i] = GetLight( m_BlockMeta, index );
 		}
 		m_pState->m_PendingSendBlocks.clear();
 		PendingSendBlocks = m_pState->m_PendingSendBlocks.size();
 		Broadcast( MultiBlock );
 	}
	if( PendingSendBlocks > 0 )
	{
		for( unsigned int i = 0; i < PendingSendBlocks; i++)
		{
			unsigned int index = m_pState->m_PendingSendBlocks[i];
			int Y = index % 128;
			int Z = (index / 128) % 16;
			int X = (index / (128*16));

			cPacket_BlockChange BlockChange;
			BlockChange.m_PosX = X + m_PosX*16;
			BlockChange.m_PosY = (char)(Y + m_PosY*128);
			BlockChange.m_PosZ = Z + m_PosZ*16;
			BlockChange.m_BlockType = m_BlockType[index];
			BlockChange.m_BlockMeta = GetLight( m_BlockMeta, index );
			Broadcast( BlockChange );
		}
		m_pState->m_PendingSendBlocks.clear();
	}

	while( !m_pState->m_UnloadQuery.empty() )
	{
		cPacket_PreChunk UnloadPacket;
		UnloadPacket.m_PosX = GetPosX();
		UnloadPacket.m_PosZ = GetPosZ();
		UnloadPacket.m_bLoad = false; // Unload
		(*m_pState->m_UnloadQuery.begin())->Send( UnloadPacket );
		m_pState->m_UnloadQuery.remove( *m_pState->m_UnloadQuery.begin() );
	}

	std::map< unsigned int, int > ToTickBlocks = m_pState->m_ToTickBlocks;
	unsigned int NumTickBlocks = ToTickBlocks.size();
	if( NumTickBlocks > 0 ) LOG("To tick: %i", NumTickBlocks );
	m_pState->m_ToTickBlocks.clear();
	for( std::map< unsigned int, int>::iterator itr = ToTickBlocks.begin(); itr != ToTickBlocks.end(); ++itr )
	{
		if( (*itr).second < 0 ) continue;
		unsigned int index = (*itr).first;
		int Y = index % 128;
		int Z = (index / 128) % 16;
		int X = (index / (128*16));

		char BlockID = GetBlock( index );
		switch( BlockID )
		{
		case E_BLOCK_REEDS:
		case E_BLOCK_WOODEN_PRESSURE_PLATE:
		case E_BLOCK_STONE_PRESSURE_PLATE:
		case E_BLOCK_MINECART_TRACKS:
		case E_BLOCK_SIGN_POST:
		case E_BLOCK_CROPS:
		case E_BLOCK_SAPLING:
		case E_BLOCK_YELLOW_FLOWER:
		case E_BLOCK_RED_ROSE:
		case E_BLOCK_RED_MUSHROOM:
		case E_BLOCK_BROWN_MUSHROOM:
		case E_BLOCK_REDSTONE_WIRE:		// Stuff that drops when block below is destroyed
			{
				if( GetBlock( X, Y-1, Z ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, 0, 0 );
					cPickup* Pickup = new cPickup( (X+m_PosX*16) * 32 + 16, (Y+m_PosY*128) * 32 + 16, (Z+m_PosZ*16) * 32 + 16, cItem( (ENUM_ITEM_ID)BlockID, 1 ) );
					Pickup->Initialize();
				}
			}
			break;
		case E_BLOCK_REDSTONE_TORCH_OFF:
		case E_BLOCK_REDSTONE_TORCH_ON:
		case E_BLOCK_TORCH:
			{
				char Dir = cTorch::MetaDataToDirection( GetLight( m_BlockMeta, X, Y, Z ) );
				LOG("MetaData: %i", Dir );
				int XX = X + m_PosX*16;
				char YY = (char)Y;
				int ZZ = Z + m_PosZ*16;
				AddDirection( XX, YY, ZZ, Dir, true );
				cWorld* World = cRoot::Get()->GetWorld();
				if( World->GetBlock( XX, YY, ZZ ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, 0, 0 );
					cPickup* Pickup = new cPickup( (X+m_PosX*16) * 32 + 16, (Y+m_PosY*128) * 32 + 16, (Z+m_PosZ*16) * 32 + 16, cItem( (ENUM_ITEM_ID)BlockID, 1 ) );
					Pickup->Initialize();
				}
			}
			break;
		case E_BLOCK_LADDER:
			{
				char Dir = cLadder::MetaDataToDirection( GetLight( m_BlockMeta, X, Y, Z ) );
				int XX = X + m_PosX*16;
				char YY = (char)Y;
				int ZZ = Z + m_PosZ*16;
				AddDirection( XX, YY, ZZ, Dir, true );
				cWorld* World = cRoot::Get()->GetWorld();
				if( World->GetBlock( XX, YY, ZZ ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, 0, 0 );
					cPickup* Pickup = new cPickup( (X+m_PosX*16) * 32 + 16, (Y+m_PosY*128) * 32 + 16, (Z+m_PosZ*16) * 32 + 16,  cItem( (ENUM_ITEM_ID)BlockID, 1 ) );
					Pickup->Initialize();
				}
			}
			break;
		case E_BLOCK_STATIONARY_WATER:
		case E_BLOCK_WATER:
			
			break;
		case E_BLOCK_GRAVEL:
		case E_BLOCK_SAND:
			{
				char BottomBlock = GetBlock( X, Y-1, Z );
				if( BottomBlock == E_BLOCK_AIR || BottomBlock == E_BLOCK_WATER || BottomBlock == E_BLOCK_STATIONARY_WATER || BottomBlock == E_BLOCK_LAVA || BottomBlock == E_BLOCK_STATIONARY_LAVA )
				{
					SetBlock( X, Y, Z, 0, 0 );
					SetBlock( X, Y-1, Z, BlockID, 0 );
				}
			}
			break;
		default:
			break;
		};
	}

	// Tick dem blocks
	int RandomX = rand();
	int RandomY = rand();
	int RandomZ = rand();

	for(int i = 0; i < 50; i++)
	{
		m_BlockTickX = (m_BlockTickX + RandomX) % 16;
		m_BlockTickY = (m_BlockTickY + RandomY) % 128;
		m_BlockTickZ = (m_BlockTickZ + RandomZ) % 16;

		//LOG("%03i %03i %03i", m_BlockTickX, m_BlockTickY, m_BlockTickZ);

		if( m_BlockTickY > m_HeightMap[ m_BlockTickX + m_BlockTickZ*16 ] ) continue; // It's all air up here

		//m_BlockTickNum = (m_BlockTickNum + 1 ) % c_NumBlocks;
		unsigned int Index = MakeIndex( m_BlockTickX, m_BlockTickY, m_BlockTickZ );
		char ID = m_BlockType[Index];
		switch( ID )
		{
		case E_BLOCK_DIRT:
			{
				char AboveBlock = GetBlock( Index+1 );
				if( AboveBlock == 0 && GetLight( m_BlockSkyLight, Index ) > 0xf/2 ) // Half lit
				{
					FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_GRASS, GetLight( m_BlockMeta, Index ) );
				}
			}
			break;
		case E_BLOCK_GRASS:
			{
				char AboveBlock = GetBlock( Index+1 );
				if( AboveBlock != 0 )
				{
					FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_DIRT, GetLight( m_BlockMeta, Index ) );
				}
			}
			break;
		case E_BLOCK_SAPLING:
			{
				FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_AIR, GetLight( m_BlockMeta, Index ) );
				cRoot::Get()->GetWorld()->GrowTree( m_BlockTickX + m_PosX*16, m_BlockTickY, m_BlockTickZ + m_PosZ*16 );
			}
		default:
			break;
		}
	}

	// Tick block entities (furnace)
	std::list< cFurnaceEntity* > TickBlockEntites = m_pState->m_TickBlockEntities; // Dangerous stuff, better make a copy.
	for( std::list< cFurnaceEntity* >::iterator itr = TickBlockEntites.begin(); itr != TickBlockEntites.end(); ++itr )
	{
		if( !(*itr)->Tick( a_Dt ) ) // Remove from list
		{
			m_pState->m_TickBlockEntities.remove( *itr );
		}
	}
}

char cChunk::GetHeight( int a_X, int a_Z )
{
	if( a_X >= 0 && a_X < 16 && a_Z >= 0 && a_Z < 16 )
		return m_HeightMap[a_X + a_Z*16];
	return 0;
}

void cChunk::CreateBlockEntities()
{
	for(int x = 0; x < 16; x++)
	{
		for(int z = 0; z < 16; z++)
		{
			for(int y = 0; y < 128; y++)
			{
				ENUM_BLOCK_ID BlockType = (ENUM_BLOCK_ID)m_BlockData[ MakeIndex( x, y, z ) ];
				switch( BlockType )
				{
				case E_BLOCK_CHEST:
					{
						m_pState->m_BlockEntities.push_back( new cChestEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16 ) );
					}
					break;
				case E_BLOCK_FURNACE:
					{
						m_pState->m_BlockEntities.push_back( new cFurnaceEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16 ) );
					}
					break;
				case E_BLOCK_SIGN_POST:
				case E_BLOCK_WALLSIGN:
					{
						m_pState->m_BlockEntities.push_back( new cSignEntity( BlockType, x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16 ) );
					}
					break;
				default:
					{
					}
					break;
				}
			}
		}
	}
}

unsigned int cChunk::MakeIndex(int x, int y, int z )
{
	if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
		return y + (z * 128) + (x * 128 * 16);
	return 0;
}

char cChunk::GetLight(char* a_Buffer, int a_BlockIdx)
{
	if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks )
	{
		const int cindex = (a_BlockIdx/2);
		if( (a_BlockIdx & 1) == 0 )
		{	// First half byte
			return (a_Buffer[cindex] & 0x0f);
		}
		else
		{
			return ((a_Buffer[cindex] & 0xf0) >> 4);
		}
	}
	return 0;
}

char cChunk::GetLight(char* a_Buffer, int x, int y, int z)
{
	if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
	{
		const int cindex = (y/2) + (z * 64) + (x * 64 * 16);
		if( (y & 1) == 0 )
		{	// First half byte
			return (a_Buffer[cindex] & 0x0f);
		}
		else
		{
			return ((a_Buffer[cindex] & 0xf0) >> 4);
		}
	}
	return 0;
}

void cChunk::SetLight(char* a_Buffer, int a_BlockIdx, char a_Light)
{
	if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks )
	{
		const int cindex = (a_BlockIdx/2);
		if( (a_BlockIdx & 1) == 0 )
		{	// First half byte
			a_Buffer[cindex] &= 0xf0; // Set first half to 0
			a_Buffer[cindex] |= (a_Light) & 0x0f;
		}
		else
		{
			a_Buffer[cindex] &= 0x0f; // Set second half to 0
			a_Buffer[cindex] |= (a_Light << 4) & 0xf0;
		}
	}
}

void cChunk::SetLight(char* a_Buffer, int x, int y, int z, char light)
{
	if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
	{
		int cindex = (y/2) + (z * 64) + (x * 64 * 16);
		if( (y & 1) == 0 )
		{	// First half byte
			a_Buffer[cindex] &= 0xf0; // Set first half to 0
			a_Buffer[cindex] |= (light) & 0x0f;
		}
		else
		{
			a_Buffer[cindex] &= 0x0f; // Set second half to 0
			a_Buffer[cindex] |= (light << 4) & 0xf0;
		}
	}
}

inline void cChunk::SpreadLightOfBlock(char* a_LightBuffer, int a_X, int a_Y, int a_Z, char a_Falloff)
{
	unsigned char CurrentLight = GetLight( a_LightBuffer, a_X, a_Y, a_Z );
	SetLight( a_LightBuffer, a_X-1, a_Y, a_Z, MAX(GetLight( a_LightBuffer, a_X-1, a_Y, a_Z ), MAX(0,CurrentLight-a_Falloff) ) );
	SetLight( a_LightBuffer, a_X+1, a_Y, a_Z, MAX(GetLight( a_LightBuffer, a_X+1, a_Y, a_Z ), MAX(0,CurrentLight-a_Falloff) ) );
	SetLight( a_LightBuffer, a_X, a_Y-1, a_Z, MAX(GetLight( a_LightBuffer, a_X, a_Y-1, a_Z ), MAX(0,CurrentLight-a_Falloff) ) );
	SetLight( a_LightBuffer, a_X, a_Y+1, a_Z, MAX(GetLight( a_LightBuffer, a_X, a_Y+1, a_Z ), MAX(0,CurrentLight-a_Falloff) ) );
	SetLight( a_LightBuffer, a_X, a_Y, a_Z-1, MAX(GetLight( a_LightBuffer, a_X, a_Y, a_Z-1 ), MAX(0,CurrentLight-a_Falloff) ) );
	SetLight( a_LightBuffer, a_X, a_Y, a_Z+1, MAX(GetLight( a_LightBuffer, a_X, a_Y, a_Z+1 ), MAX(0,CurrentLight-a_Falloff) ) );
}

inline void cChunk::SpreadLightOfBlockX(char* a_LightBuffer, int a_X, int a_Y, int a_Z)
{
	unsigned char CurrentLight = GetLight( a_LightBuffer, a_X, a_Y, a_Z );
	SetLight( a_LightBuffer, a_X-1, a_Y, a_Z, MAX(GetLight( a_LightBuffer, a_X-1, a_Y, a_Z ), CurrentLight-1) );
	SetLight( a_LightBuffer, a_X+1, a_Y, a_Z, MAX(GetLight( a_LightBuffer, a_X+1, a_Y, a_Z ), CurrentLight-1) );
}

inline void cChunk::SpreadLightOfBlockY(char* a_LightBuffer, int a_X, int a_Y, int a_Z)
{
	unsigned char CurrentLight = GetLight( a_LightBuffer, a_X, a_Y, a_Z );
	SetLight( a_LightBuffer, a_X, a_Y-1, a_Z, MAX(GetLight( a_LightBuffer, a_X, a_Y-1, a_Z ), CurrentLight-1) );
	SetLight( a_LightBuffer, a_X, a_Y+1, a_Z, MAX(GetLight( a_LightBuffer, a_X, a_Y+1, a_Z ), CurrentLight-1) );
}

inline void cChunk::SpreadLightOfBlockZ(char* a_LightBuffer, int a_X, int a_Y, int a_Z)
{
	unsigned char CurrentLight = GetLight( a_LightBuffer, a_X, a_Y, a_Z );
	SetLight( a_LightBuffer, a_X, a_Y, a_Z-1, MAX(GetLight( a_LightBuffer, a_X, a_Y, a_Z-1 ), CurrentLight-1) );
	SetLight( a_LightBuffer, a_X, a_Y, a_Z+1, MAX(GetLight( a_LightBuffer, a_X, a_Y, a_Z+1 ), CurrentLight-1) );
}

void cChunk::CalculateHeightmap()
{
	m_bCalculateHeightmap = false;
	for(int x = 0; x < 16; x++)
	{
		for(int z = 0; z < 16; z++)
		{
			for(int y = 127; y > -1; y--)
			{
				int index = MakeIndex( x, y, z );
				if(m_BlockData[index] != E_BLOCK_AIR)
				{
					m_HeightMap[x + z*16] = (char)y;
					break;
				}
			}
		}
	}
}

void cChunk::CalculateLighting()
{
	// Calculate sunlight
	memset(m_BlockSkyLight, 0xff, c_NumBlocks/2 ); // Set all to fully lit, so everything above HeightMap is lit
	for(int x = 0; x < 16; x++)
	{
		for(int z = 0; z < 16; z++)
		{
			char sunlight = 0xf;
			for(int y = m_HeightMap[x + z*16]; y > -1; y--)
			{
				int index = y + (z * 128) + (x * 128 * 16);

				if( g_BlockTransparent[ (int)m_BlockData[index] ] == false )
				{
					sunlight = 0x0;
				}
				SetLight( m_BlockSkyLight, x, y, z, sunlight );
			}
		}
	}

	// Calculate blocklights
	for(int x = 0; x < 16; x++)
	{
		for(int z = 0; z < 16; z++)
		{
			int MaxHeight = m_HeightMap[x + z*16];
			for(int y = 0; y < MaxHeight; y++)
			{
				char BlockID = GetBlock(x, y, z);
				SetLight( m_BlockLight, x, y, z, g_BlockLightValue[(int)BlockID] );
			}
		}
	}

	SpreadLight(m_BlockSkyLight);
	SpreadLight(m_BlockLight);

	// Stop it from calculating again :P
	m_bCalculateLighting = false;
}

void cChunk::SpreadLight(char* a_LightBuffer)
{
	// Spread the sunlight
	for(int x = 0; x < 16; x++)	for(int z = 0; z < 16; z++)	for(int y = 0; y < 128; y++)
	{
		int index = y + (z * 128) + (x * 128 * 16);
		if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
		{
			SpreadLightOfBlock(a_LightBuffer, x, y, z, g_BlockSpreadLightFalloff[ m_BlockData[index] ]);
		}
	}

	for(int x = 15; x > -1; x--) for(int z = 15; z > -1; z--) for(int y = 127; y > -1; y--)
	{
		int index = y + (z * 128) + (x * 128 * 16);
		if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
		{
			SpreadLightOfBlock(a_LightBuffer, x, y, z, g_BlockSpreadLightFalloff[ m_BlockData[index] ]);
		}
	}

	bool bCalcLeft, bCalcRight, bCalcFront, bCalcBack;
	bCalcLeft = bCalcRight = bCalcFront = bCalcBack = false;
	// Spread to neighbour chunks X-axis
	cChunk* LeftChunk = cRoot::Get()->GetWorld()->GetChunkUnreliable( m_PosX-1, m_PosY, m_PosZ );
	cChunk* RightChunk = cRoot::Get()->GetWorld()->GetChunkUnreliable( m_PosX+1, m_PosY, m_PosZ );
	char* LeftSky = 0, *RightSky = 0;
	if(LeftChunk) LeftSky = (a_LightBuffer==m_BlockSkyLight)?LeftChunk->pGetSkyLight():LeftChunk->pGetLight();
	if(RightChunk) RightSky = (a_LightBuffer==m_BlockSkyLight)?RightChunk->pGetSkyLight():RightChunk->pGetLight();
	for(int z = 0; z < 16; z++)	for(int y = 0; y < 128; y++)
	{
		if( LeftChunk )
		{
			int index = y + (z * 128) + (0  * 128 * 16);
			if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
			{
				char CurrentLight = GetLight( a_LightBuffer, 0, y, z );
				char LeftLight = GetLight( LeftSky, 15, y, z );
				if( LeftLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
				{
					SetLight( LeftSky, 15, y, z, MAX(0, CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ]) );
					bCalcLeft = true;
				}
			}
		}
		if( RightChunk )
		{
			int index = y + (z * 128) + (15  * 128 * 16);
			if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
			{
				char CurrentLight = GetLight( a_LightBuffer, 15, y, z );
				char RightLight = GetLight( RightSky, 0, y, z );
				if( RightLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
				{
					SetLight( RightSky, 0, y, z,  MAX(0, CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ]) );
					bCalcRight = true;
				}
			}
		}
	}

	// Spread to neighbour chunks Z-axis
	cChunk* FrontChunk = cRoot::Get()->GetWorld()->GetChunkUnreliable( m_PosX, m_PosY, m_PosZ-1 );
	cChunk* BackChunk = cRoot::Get()->GetWorld()->GetChunkUnreliable( m_PosX, m_PosY, m_PosZ+1 );
	char* FrontSky = 0, *BackSky = 0;
	if(FrontChunk) FrontSky = (a_LightBuffer==m_BlockSkyLight)?FrontChunk->pGetSkyLight():FrontChunk->pGetLight();
	if(BackChunk) BackSky = (a_LightBuffer==m_BlockSkyLight)?BackChunk->pGetSkyLight():BackChunk->pGetLight();
	for(int x = 0; x < 16; x++)	for(int y = 0; y < 128; y++)
	{
		if( FrontChunk )
		{
			int index = y + (0 * 128) + (x  * 128 * 16);
			if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
			{
				char CurrentLight = GetLight( a_LightBuffer, x, y, 0 );
				char FrontLight = GetLight( FrontSky, x, y, 15 );
				if( FrontLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
				{
					SetLight( FrontSky, x, y, 15,  MAX(0, CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ]) );
					bCalcFront = true;
				}
			}
		}
		if( BackChunk )
		{
			int index = y + (15 * 128) + (x  * 128 * 16);
			if( g_BlockSpreadLightFalloff[ m_BlockData[index] ] > 0 )
			{
				char CurrentLight = GetLight( a_LightBuffer, x, y, 15 );
				char BackLight = GetLight( BackSky, x, y, 0 );
				if( BackLight < CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ] )
				{
					SetLight( BackSky, x, y, 0, MAX(0, CurrentLight-g_BlockSpreadLightFalloff[ m_BlockData[index] ]) );
					bCalcBack = true;
				}
			}
		}
	}

	if( bCalcLeft )		cRoot::Get()->GetWorld()->ReSpreadLighting( LeftChunk );
	if( bCalcRight )	cRoot::Get()->GetWorld()->ReSpreadLighting( RightChunk );
	if( bCalcFront )	cRoot::Get()->GetWorld()->ReSpreadLighting( FrontChunk );
	if( bCalcBack )		cRoot::Get()->GetWorld()->ReSpreadLighting( BackChunk );
}

float GetNoise( float x, float y, cNoise & a_Noise )
{
	float oct1 = a_Noise.SSE_CubicNoise2D( x*cGenSettings::HeightFreq1, y*cGenSettings::HeightFreq1 )*cGenSettings::HeightAmp1;
	float oct2 = a_Noise.SSE_CubicNoise2D( x*cGenSettings::HeightFreq2, y*cGenSettings::HeightFreq2 )*cGenSettings::HeightAmp2;
	float oct3 = a_Noise.SSE_CubicNoise2D( x*cGenSettings::HeightFreq3, y*cGenSettings::HeightFreq3 )*cGenSettings::HeightAmp3;

	float height = a_Noise.SSE_CubicNoise2D( x*0.1f, y*0.1f )*2;

	float flatness = ((a_Noise.SSE_CubicNoise2D( x*0.5f, y*0.5f ) + 1.f ) * 0.5f) * 1.1f; // 0 ... 1.5
	flatness *= flatness * flatness;

	return (oct1 + oct2 + oct3) * flatness + height;
}

#define PI_2 (1.57079633)
float GetMarbleNoise( float x, float y, float z, cNoise & a_Noise )
{
	float oct1 = (a_Noise.CubicNoise3D( x*0.1f, y*0.1f, z*0.1f ))*4;

	oct1 = oct1 * oct1 * oct1;
	if( oct1 < 0.f ) oct1 = PI_2;
	if( oct1 > PI_2 ) oct1 = PI_2;

	return oct1;
}

float GetOreNoise( float x, float y, float z, cNoise & a_Noise )
{
	float oct1 = a_Noise.CubicNoise3D( x*0.1f, y*0.1f, z*0.1f );
	float oct2 = a_Noise.CubicNoise3D( x*0.05f, y*0.5f, z*0.05f );

	oct2 *= oct2;
	oct1 = (1 - (oct1 * oct1 *100)) * oct2;
	//if( oct1 < 0.5f ) oct1 = 0;
	//else oct1 = 1.f;

	return oct1;
}

void cChunk::GenerateTerrain()
{

	
	const ENUM_BLOCK_ID GrassID =	E_BLOCK_GRASS;
	const ENUM_BLOCK_ID DirtID =	E_BLOCK_DIRT;
	const ENUM_BLOCK_ID StoneID =	E_BLOCK_STONE;
	const ENUM_BLOCK_ID SandID =	E_BLOCK_SAND;
	const ENUM_BLOCK_ID CaveID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID LavaID =	E_BLOCK_STATIONARY_LAVA;
	const ENUM_BLOCK_ID CoalID =	E_BLOCK_COAL_ORE;
	const ENUM_BLOCK_ID IronID =	E_BLOCK_IRON_ORE;
	const ENUM_BLOCK_ID GoldID =	E_BLOCK_GOLD_ORE;
	const ENUM_BLOCK_ID DiamondID =	E_BLOCK_DIAMOND_ORE;
	const ENUM_BLOCK_ID RedID =		E_BLOCK_REDSTONE_ORE;
	 
	 /*
	const ENUM_BLOCK_ID GrassID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID DirtID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID StoneID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID SandID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID CaveID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID LavaID =	E_BLOCK_AIR;
	const ENUM_BLOCK_ID CoalID =	E_BLOCK_COAL_ORE;
	const ENUM_BLOCK_ID IronID =	E_BLOCK_IRON_ORE;
	const ENUM_BLOCK_ID GoldID =	E_BLOCK_GOLD_ORE;
	const ENUM_BLOCK_ID DiamondID =	E_BLOCK_DIAMOND_ORE;
	const ENUM_BLOCK_ID RedID =		E_BLOCK_REDSTONE_ORE;
	*/

	cNoise m_Noise( cRoot::Get()->GetWorld()->GetWorldSeed() );
	for(int z = 0; z < 16; z++) 
	{
		const float zz = (float)(m_PosZ*16 + z);
		for(int x = 0; x < 16; x++)
		{
			// Place bedrock on bottom layer
			m_BlockType[ MakeIndex(x, 0, z) ] = E_BLOCK_BEDROCK;

			const float xx = (float)(m_PosX*16 + x);
			
			int Height = (int)(GetNoise( xx*0.05f, zz*0.05f, m_Noise )*16);
			const int Lower = 64;
			if( Height+Lower > 127 ) Height = 127-Lower;
			const int Top = Lower+Height;
			const float WaveNoise = 1;//m_Noise.CubicNoise2D( xx*0.01f, zz*0.01f ) + 0.5f;
			for( int y = 1; y < Top; ++y )
			{
				const float yy = (float)y;
				//   V prevent caves from getting too close to the surface
				if( (Top - y > (WaveNoise*2) ) && cosf(GetMarbleNoise( xx, yy*0.5f, zz, m_Noise )) * fabs( cosf( yy*0.2f + WaveNoise*2 )*0.75f + WaveNoise ) > 0.5f )
				{
					if( y > 4 )
					{
						m_BlockType[ MakeIndex(x, y, z) ] = CaveID;
						if( z > 0 ) m_BlockType[ MakeIndex(x, y, z-1) ] = CaveID;
						if( z < 15 ) m_BlockType[ MakeIndex(x, y, z+1) ] = CaveID;
						if( x > 0 ) m_BlockType[ MakeIndex(x-1, y, z) ] = CaveID;
						if( x < 15 ) m_BlockType[ MakeIndex(x+1, y, z) ] = CaveID;
					}
					else
					{
						m_BlockType[ MakeIndex(x, y, z) ] = LavaID;
					}
				}
				else if( y < 61 && Top - y < 3 )
					m_BlockType[ MakeIndex(x, y, z) ] = SandID;
				else if( Top - y > ((WaveNoise+1.5f)*1.5f) ) // rock and ores between 1.5 .. 4.5 deep
				{
					if( GetOreNoise( xx, yy, zz, m_Noise ) > 0.5f )
						m_BlockType[ MakeIndex(x, y, z) ] = CoalID;
					else if( GetOreNoise( xx, yy+100.f, zz, m_Noise ) > 0.6f )
						m_BlockType[ MakeIndex(x, y, z) ] = IronID;
					else if( yy < 20 && GetOreNoise( xx*1.5f, yy+300.f, zz*1.5f, m_Noise ) > 0.6f )
						m_BlockType[ MakeIndex(x, y, z) ] = RedID;
					else if( yy < 30 && GetOreNoise( xx*2, yy+200.f, zz*2, m_Noise ) > 0.75f )
						m_BlockType[ MakeIndex(x, y, z) ] = DiamondID;
					else if( yy < 40 && GetOreNoise( xx*2, yy+100.f, zz*2, m_Noise ) > 0.75f )
						m_BlockType[ MakeIndex(x, y, z) ] = GoldID;
					else
						m_BlockType[ MakeIndex(x, y, z) ] = StoneID;
				}
				else
					m_BlockType[ MakeIndex(x, y, z) ] = DirtID;
			}
			for( int y = Lower+Height; y < 60; ++y )
			{
				m_BlockType[ MakeIndex(x, y, z) ] = E_BLOCK_STATIONARY_WATER;
			}
		}
	}

	for(int z = 0; z < 16; z++) for(int x = 0; x < 16; x++)
	{
		// Find top most Y
		int TopY = -1;
		for(int y = 127; y > 0; y--)
		{
			int index =  MakeIndex( x, y, z );
			if( m_BlockType[index] != E_BLOCK_AIR )
			{
				TopY = y;
				break;
			}
		}
		if( TopY > 0 )
		{
			// Change top dirt into grass
			int index = MakeIndex( x, TopY, z );
			if( m_BlockType[index] == DirtID )
			{
				m_BlockType[ index ] = (char)GrassID;
			}

			
			// Plant sum trees
			{
				int xx = x + m_PosX*16;
//				int yy = TopY;
				int zz = z + m_PosZ*16;
				
				float val1 = m_Noise.SSE_CubicNoise2D( xx*0.1f, zz*0.1f );
				float val2 = m_Noise.SSE_CubicNoise2D( xx*0.01f, zz*0.01f );
				if( m_BlockType[index] == SandID )
				{
					if( (val1 + val2 > 0.f) && (rand()%128) > 124 && m_BlockType[index] == E_BLOCK_SAND )
					{
						m_BlockType[ MakeIndex(x, TopY+1, z) ] = E_BLOCK_CACTUS;
						if( (rand() & 3) == 3 )
						{
							m_BlockType[ MakeIndex(x, TopY+2, z) ] = E_BLOCK_CACTUS;
						}
						continue;
					}
				}
				else if( m_BlockType[index] == GrassID )
				{
					float val3 = m_Noise.SSE_CubicNoise2D( xx*0.01f+10, zz*0.01f+10 );
					float val4 = m_Noise.SSE_CubicNoise2D( xx*0.05f+20, zz*0.05f+20 );
					if( val1 + val2 > 0.2f && (rand()%128) > 124 )
						cRoot::Get()->GetWorld()->GrowTree( xx, TopY, zz );
					else if( val3 > 0.2f && (rand()%128) > 124 )
						m_BlockType[ MakeIndex(x, TopY+1, z) ] = E_BLOCK_YELLOW_FLOWER;
					else if( val4 > 0.2f && (rand()%128) > 124 )
						m_BlockType[ MakeIndex(x, TopY+1, z) ] = E_BLOCK_RED_ROSE;
					else if( val1+val2+val3+val4 > 0.2f && (rand()%128) > 124 )
						m_BlockType[ MakeIndex(x, TopY+1, z) ] = E_BLOCK_RED_MUSHROOM;
					else if( val1+val2+val3+val4 > 0.2f && (rand()%128) > 124 )
						m_BlockType[ MakeIndex(x, TopY+1, z) ] = E_BLOCK_BROWN_MUSHROOM;
				}
			}
			
		}
	}
}


void cChunk::AsyncUnload( cClientHandle* a_Client )
{
	m_pState->m_UnloadQuery.remove( a_Client );	// Make sure this client is only in the list once
	m_pState->m_UnloadQuery.push_back( a_Client );
}

void cChunk::Send( cClientHandle* a_Client )
{
	cPacket_PreChunk PreChunk;
	PreChunk.m_PosX = m_PosX;
	PreChunk.m_PosZ = m_PosZ;
	PreChunk.m_bLoad = true;
	a_Client->Send( PreChunk );
	a_Client->Send( cPacket_MapChunk( this ) );

	for( BlockEntityList::iterator itr = m_pState->m_BlockEntities.begin(); itr != m_pState->m_BlockEntities.end(); ++itr )
	{
		(*itr)->SendTo( a_Client );
	}
}

void cChunk::SetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_BlockMeta )
{
	if(a_X < 0 || a_X >= 16 || a_Y < 0 || a_Y >= 128 || a_Z < 0 || a_Z >= 16)
	{
		//printf(">>>>>>>>>>>>>>>> CLIPPED SETBLOCK %i %i %i\n", a_X, a_Y, a_Z );
		return; // Clip
	}

	int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
	char OldBlockMeta = GetLight( m_BlockMeta, index );
	char OldBlockType = m_BlockType[index];
	m_BlockType[index] = a_BlockType;

	SetLight( m_BlockMeta, index, a_BlockMeta );

	if( OldBlockType != a_BlockType || OldBlockMeta != a_BlockMeta )
	{
		//LOG("Old: %i %i New: %i %i", OldBlockType, OldBlockMeta, a_BlockType, a_BlockMeta );
		m_pState->m_PendingSendBlocks.push_back( index );

		m_pState->m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X+1, a_Y, a_Z ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X-1, a_Y, a_Z ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X, a_Y+1, a_Z ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X, a_Y-1, a_Z ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z+1 ) ]++;
		m_pState->m_ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z-1 ) ]++;

		cBlockEntity* BlockEntity = GetBlockEntity( a_X + m_PosX*16, a_Y+m_PosY*128, a_Z+m_PosZ*16 );
		if( BlockEntity )
		{
			BlockEntity->Destroy();
			RemoveBlockEntity( BlockEntity );
			delete BlockEntity;
		}
		switch( a_BlockType )
		{
		case E_BLOCK_CHEST:
			AddBlockEntity( new cChestEntity( a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16 ) );
			break;
		case E_BLOCK_FURNACE:
			AddBlockEntity( new cFurnaceEntity( a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16 ) );
			break;
		case E_BLOCK_SIGN_POST:
		case E_BLOCK_WALLSIGN:
			AddBlockEntity( new cSignEntity( (ENUM_BLOCK_ID)a_BlockType, a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16 ) );
			break;
		default:
			break;
		};
	}

	CalculateHeightmap();
	RecalculateLighting();
}

void cChunk::FastSetBlock( int a_X, int a_Y, int a_Z, char a_BlockType, char a_BlockMeta )
{
	if(a_X < 0 || a_X >= 16 || a_Y < 0 || a_Y >= 128 || a_Z < 0 || a_Z >= 16)
	{
		//printf(">>>>>>>>>>>>>>>> CLIPPED SETBLOCK %i %i %i\n", a_X, a_Y, a_Z );
		return; // Clip
	}

	const int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
	const char OldBlock = m_BlockType[index];
	m_BlockType[index] = a_BlockType;
	m_pState->m_PendingSendBlocks.push_back( index );
	SetLight( m_BlockMeta, index, a_BlockMeta );

	// ONLY recalculate lighting if it's nessesary!
	if(		g_BlockLightValue[ OldBlock ] != g_BlockLightValue[ a_BlockType ]
		||	g_BlockSpreadLightFalloff[ OldBlock ] != g_BlockSpreadLightFalloff[ a_BlockType ]
		||	g_BlockTransparent[ OldBlock ] != g_BlockTransparent[ a_BlockType ] )
	{
		RecalculateLighting();
	}

	// Recalculate next tick
	RecalculateHeightmap();
}

void cChunk::SendBlockTo( int a_X, int a_Y, int a_Z, cClientHandle* a_Client )
{
	if( a_Client == 0 )
	{
		m_pState->m_PendingSendBlocks.push_back( MakeIndex( a_X, a_Y, a_Z ) );
		return;
	}

	for( std::list< cClientHandle* >::iterator itr = m_pState->m_LoadedByClient.begin(); itr != m_pState->m_LoadedByClient.end(); ++itr )
	{
		if( *itr == a_Client )
		{
			unsigned int index = MakeIndex( a_X, a_Y, a_Z );
			cPacket_BlockChange BlockChange;
			BlockChange.m_PosX = a_X + m_PosX*16;
			BlockChange.m_PosY = (char)(a_Y + m_PosY*128);
			BlockChange.m_PosZ = a_Z + m_PosZ*16;
			BlockChange.m_BlockType = m_BlockType[ index ];
			BlockChange.m_BlockMeta = GetLight( m_BlockMeta, index );
			a_Client->Send( BlockChange );
			break;
		}
	}
}

void cChunk::AddBlockEntity( cBlockEntity* a_BlockEntity )
{
	m_pState->m_BlockEntities.push_back( a_BlockEntity );
}

void cChunk::RemoveBlockEntity( cBlockEntity* a_BlockEntity )
{
	m_pState->m_BlockEntities.remove( a_BlockEntity );
}

void cChunk::AddClient( cClientHandle* a_Client )
{
	m_pState->m_LoadedByClient.remove( a_Client );
	m_pState->m_LoadedByClient.push_back( a_Client );

	LockEntities();
	for( EntityList::iterator itr = m_pState->m_Entities.begin(); itr != m_pState->m_Entities.end(); ++itr )
	{
		LOG("%i %i %i Spawning on %s", m_PosX, m_PosY, m_PosZ, a_Client->GetUsername() );
		(*itr)->SpawnOn( a_Client );
	}
	UnlockEntities();
}

void cChunk::RemoveClient( cClientHandle* a_Client )
{
	m_pState->m_LoadedByClient.remove( a_Client );

	LockEntities();
	for( EntityList::iterator itr = m_pState->m_Entities.begin(); itr != m_pState->m_Entities.end(); ++itr )
	{
		LOG("%i %i %i Destroying on %s", m_PosX, m_PosY, m_PosZ, a_Client->GetUsername() );
		cPacket_DestroyEntity DestroyEntity( *itr );
		a_Client->Send( DestroyEntity );
	}
	UnlockEntities();
}

void cChunk::AddEntity( cEntity & a_Entity )
{
	LockEntities();
	m_pState->m_Entities.push_back( &a_Entity );
	UnlockEntities();
}

bool cChunk::RemoveEntity( cEntity & a_Entity, cChunk* a_CalledFrom /* = 0 */ )
{
	LockEntities();
	unsigned int SizeBefore = m_pState->m_Entities.size();
	m_pState->m_Entities.remove( &a_Entity );
	if( SizeBefore == m_pState->m_Entities.size() )
	{
		LOG("WARNING: Entity was not in chunk %i %i %i", m_PosX, m_PosY, m_PosZ );
		if( !a_CalledFrom )
		{
			UnlockEntities();
			return cRoot::Get()->GetWorld()->RemoveEntityFromChunk( a_Entity, this );
		}
		UnlockEntities();
		return false;
	}
	UnlockEntities();
	return true;
}

void cChunk::LockEntities()
{
	m_EntitiesCriticalSection->Lock();
}

void cChunk::UnlockEntities()
{
	m_EntitiesCriticalSection->Unlock();
}

char cChunk::GetBlock( int a_X, int a_Y, int a_Z )
{
	if(a_X < 0 || a_X >= 16 || a_Y < 0 || a_Y >= 128 || a_Z < 0 || a_Z >= 16) return 0; // Clip

	int index = a_Y + (a_Z * 128) + (a_X * 128 * 16);
	return m_BlockType[index];
}

char cChunk::GetBlock( int a_BlockIdx )
{
	if( a_BlockIdx < 0 || a_BlockIdx >= c_NumBlocks ) return 0;
	return m_BlockType[ a_BlockIdx ];
}

cBlockEntity* cChunk::GetBlockEntity( int a_X, int a_Y, int a_Z )
{
	for( std::list<cBlockEntity*>::iterator itr = m_pState->m_BlockEntities.begin(); itr != m_pState->m_BlockEntities.end(); ++itr)
	{
		if( (*itr)->GetPosX() == a_X &&
			(*itr)->GetPosY() == a_Y &&
			(*itr)->GetPosZ() == a_Z )
		{
			return *itr;
		}
	}
	return 0;
}

bool cChunk::LoadFromDisk()
{
	char SourceFile[128];
	sprintf_s(SourceFile, 128, "world/X%i_Y%i_Z%i.bin", m_PosX, m_PosY, m_PosZ );

	FILE* f = 0;
	#ifdef _WIN32
	if( fopen_s(&f, SourceFile, "rb" ) == 0 )	// no error
	#else
	if( (f = fopen(SourceFile, "rb" )) != 0 )	// no error
	#endif
	{
		if( fread( m_BlockData, sizeof(char)*c_BlockDataSize, 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", SourceFile); fclose(f); return false; }

		// Now load Block Entities
		ENUM_BLOCK_ID BlockType;
		while( fread( &BlockType, sizeof(ENUM_BLOCK_ID), 1, f) == 1 )
		{
			switch( BlockType )
			{
			case E_BLOCK_CHEST:
				{
					cChestEntity* ChestEntity = new cChestEntity( 0, 0, 0 );
					if( !ChestEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING CHEST FROM FILE %s", SourceFile );
						delete ChestEntity;
						fclose(f);
						return false;
					}
					m_pState->m_BlockEntities.push_back( ChestEntity );
				}
				break;
			case E_BLOCK_FURNACE:
				{
					cFurnaceEntity* FurnaceEntity = new cFurnaceEntity( 0, 0, 0 );
					if( !FurnaceEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING FURNACE FROM FILE %s", SourceFile );
						delete FurnaceEntity;
						fclose(f);
						return false;
					}
					m_pState->m_BlockEntities.push_back( FurnaceEntity );
					m_pState->m_TickBlockEntities.push_back( FurnaceEntity ); // They need tickin'
				}
				break;
			case E_BLOCK_SIGN_POST:
			case E_BLOCK_WALLSIGN:
				{
					cSignEntity* SignEntity = new cSignEntity(BlockType, 0, 0, 0 );
					if( !SignEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING SIGN FROM FILE %s", SourceFile );
						delete SignEntity;
						fclose(f);
						return false;
					}
					m_pState->m_BlockEntities.push_back( SignEntity );
				}
				break;
			default:
				break;
			}
		}

		fclose(f);

		// Delete old format file
		if( std::remove( SourceFile ) != 0 )
			LOGERROR("Could not delete file %s", SourceFile );
		else
			LOGINFO("Successfully deleted olf format file %s", SourceFile );

		return true;
	}
	else
	{
		//LOGWARN("COULD NOT OPEN FILE %s\n", SourceFile);
		return false;
	}
}

bool cChunk::SaveToDisk()
{
	return true; //no more saving old format!

	char SourceFile[128];
	sprintf_s(SourceFile, 128, "world/X%i_Y%i_Z%i.bin", m_PosX, m_PosY, m_PosZ );

    #ifdef _WIN32
	{
		SECURITY_ATTRIBUTES Attrib;
		Attrib.nLength = sizeof(SECURITY_ATTRIBUTES);
		Attrib.lpSecurityDescriptor = NULL;
		Attrib.bInheritHandle = false;
		::CreateDirectory("world", &Attrib);
	}
	#else
	{
        mkdir("world", S_IRWXU | S_IRWXG | S_IRWXO);
	}
	#endif

	FILE* f = 0;
#ifdef _WIN32
	if( fopen_s(&f, SourceFile, "wb" ) == 0 )	// no error
	#else
	if( (f = fopen(SourceFile, "wb" )) != 0 )	// no error
	#endif
	{
		fwrite( m_BlockData, sizeof(char)*c_BlockDataSize, 1, f );

		// Now write Block Entities
		for( std::list<cBlockEntity*>::iterator itr = m_pState->m_BlockEntities.begin(); itr != m_pState->m_BlockEntities.end(); ++itr)
		{
			cBlockEntity* BlockEntity = *itr;
			switch( BlockEntity->GetBlockType() )
			{
			case E_BLOCK_CHEST:
				{
					cChestEntity* ChestEntity = reinterpret_cast< cChestEntity* >( BlockEntity );
					ChestEntity->WriteToFile( f );
				}
				break;
			case E_BLOCK_FURNACE:
				{
					cFurnaceEntity* FurnaceEntity = reinterpret_cast< cFurnaceEntity* >( BlockEntity );
					FurnaceEntity->WriteToFile( f );
				}
				break;
			case E_BLOCK_SIGN_POST:
			case E_BLOCK_WALLSIGN:
				{
					cSignEntity* SignEntity = reinterpret_cast< cSignEntity* >( BlockEntity );
					SignEntity->WriteToFile( f );
				}
				break;
			default:
				break;
			}
		}

		fclose(f);
		return true;
	}
	else
	{
		LOGERROR("ERROR WRITING TO FILE %s", SourceFile);
		return false;
	}
}

void cChunk::Broadcast( const cPacket & a_Packet, cClientHandle* a_Exclude /* = 0 */ ) const
{
	for( std::list< cClientHandle* >::const_iterator itr = m_pState->m_LoadedByClient.begin(); itr != m_pState->m_LoadedByClient.end(); ++itr )
	{
		if( *itr == a_Exclude ) continue;
		(*itr)->Send( a_Packet );
	}
}


void cChunk::LoadFromJson( const Json::Value & a_Value )
{
	// Load chests
	Json::Value AllChests = a_Value.get("Chests", Json::nullValue);
	if( !AllChests.empty() )
	{
		for( Json::Value::iterator itr = AllChests.begin(); itr != AllChests.end(); ++itr )
		{
			Json::Value & Chest = *itr;
			cChestEntity* ChestEntity = new cChestEntity(0,0,0);
			if( !ChestEntity->LoadFromJson( Chest ) )
			{
				LOGERROR("ERROR READING CHEST FROM JSON!" );
				delete ChestEntity;
			}
			else m_pState->m_BlockEntities.push_back( ChestEntity );
		}
	}

	// Load furnaces
	Json::Value AllFurnaces = a_Value.get("Furnaces", Json::nullValue);
	if( !AllFurnaces.empty() )
	{
		for( Json::Value::iterator itr = AllFurnaces.begin(); itr != AllFurnaces.end(); ++itr )
		{
			Json::Value & Furnace = *itr;
			cFurnaceEntity* FurnaceEntity = new cFurnaceEntity(0,0,0);
			if( !FurnaceEntity->LoadFromJson( Furnace ) )
			{
				LOGERROR("ERROR READING FURNACE FROM JSON!" );
				delete FurnaceEntity;
			}
			else m_pState->m_BlockEntities.push_back( FurnaceEntity );
		}
	}

	// Load signs
	Json::Value AllSigns = a_Value.get("Signs", Json::nullValue);
	if( !AllSigns.empty() )
	{
		for( Json::Value::iterator itr = AllSigns.begin(); itr != AllSigns.end(); ++itr )
		{
			Json::Value & Sign = *itr;
			cSignEntity* SignEntity = new cSignEntity( E_BLOCK_SIGN_POST, 0,0,0);
			if( !SignEntity->LoadFromJson( Sign ) )
			{
				LOGERROR("ERROR READING SIGN FROM JSON!" );
				delete SignEntity;
			}
			else m_pState->m_BlockEntities.push_back( SignEntity );
		}
	}
}

void cChunk::SaveToJson( Json::Value & a_Value )
{
	Json::Value AllChests;
	Json::Value AllFurnaces;
	Json::Value AllSigns;
	for( std::list<cBlockEntity*>::iterator itr = m_pState->m_BlockEntities.begin(); itr != m_pState->m_BlockEntities.end(); ++itr)
	{
		cBlockEntity* BlockEntity = *itr;
		switch( BlockEntity->GetBlockType() )
		{
		case E_BLOCK_CHEST:
			{
				cChestEntity* ChestEntity = reinterpret_cast< cChestEntity* >( BlockEntity );
				Json::Value NewChest;
				ChestEntity->SaveToJson( NewChest );
				AllChests.append( NewChest );
			}
			break;
		case E_BLOCK_FURNACE:
			{
				cFurnaceEntity* FurnaceEntity = reinterpret_cast< cFurnaceEntity* >( BlockEntity );
				Json::Value NewFurnace;
				FurnaceEntity->SaveToJson( NewFurnace );
				AllFurnaces.append( NewFurnace );
			}
			break;
		case E_BLOCK_SIGN_POST:
		case E_BLOCK_WALLSIGN:
			{
				cSignEntity* SignEntity = reinterpret_cast< cSignEntity* >( BlockEntity );
				Json::Value NewSign;
				SignEntity->SaveToJson( NewSign );
				AllSigns.append( NewSign );
			}
			break;
		default:
			break;
		}
	}

	if( !AllChests.empty() )
		a_Value["Chests"] = AllChests;
	if( !AllFurnaces.empty() )
		a_Value["Furnaces"] = AllFurnaces;
	if( !AllSigns.empty() )
		a_Value["Signs"] = AllSigns;
}

EntityList & cChunk::GetEntities()
{
	return m_pState->m_Entities;
}

const ClientHandleList & cChunk::GetClients()
{
	return m_pState->m_LoadedByClient;
}


void cChunk::AddTickBlockEntity( cFurnaceEntity* a_Entity )
{
	m_pState->m_TickBlockEntities.remove( a_Entity );
	m_pState->m_TickBlockEntities.push_back( a_Entity );
}

void cChunk::RemoveTickBlockEntity( cFurnaceEntity* a_Entity )
{
	m_pState->m_TickBlockEntities.remove( a_Entity );
}