summaryrefslogtreecommitdiffstats
path: root/source/cChunk.cpp
blob: 2c915d2efb5c22e01f89183d5c08bbe32e4c3c86 (plain) (blame)
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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
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
#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 "cWaterSimulator.h"
#include "cLavaSimulator.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 "cRedstone.h"
#include "cItem.h"
#include "cNoise.h"
#include "cRoot.h"
#include "cCriticalSection.h"
#include "cWorldGenerator.h"
#include "cBlockToPickup.h"
#include "MersenneTwister.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 > ToTickBlocks;
	FurnaceEntityList TickBlockEntities;
	std::vector< unsigned int > PendingSendBlocks;
	ClientHandleList LoadedByClient;
	ClientHandleList UnloadQuery;
	BlockEntityList BlockEntities;
	EntityList Entities;
};

cChunk::~cChunk()
{
	//LOG("~cChunk() %i %i %i", m_PosX, m_PosY, m_PosZ );
	if( !m_pState->LoadedByClient.empty() )
	{
		LOGWARN("WARNING: Deleting cChunk while it contains %i clients!", m_pState->LoadedByClient.size() );
	}

	for( std::list<cBlockEntity*>::iterator itr = m_pState->BlockEntities.begin(); itr != m_pState->BlockEntities.end(); ++itr)
	{
		delete *itr;
	}
	m_pState->BlockEntities.clear();

	LockEntities();
	if( m_pState->Entities.size() > 0 )
	{
		EntityList Entities = m_pState->Entities; // Copy list to a temporary list
		for( EntityList::iterator itr = Entities.begin(); itr != Entities.end(); ++itr)
		{
			if( (*itr)->GetEntityType() != cEntity::E_PLAYER )
			{
				(*itr)->RemoveFromChunk( this );
				(*itr)->Destroy();
			}
		}
		m_pState->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, cWorld* a_World)
	: 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 )
	, m_World( a_World )
{
	//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 );

		cWorldGenerator Generator;
		Generator.GenerateChunk( this );

		CalculateHeightmap();
		CalculateLighting();

		CreateBlockEntities();

		// During generation, some blocks might have been set by using (Fast)SetBlock() causing this list to fill.
		// This chunk has not been sent to anybody yet, so there is no need for separately sending block changes when you can send an entire chunk
		m_pState->PendingSendBlocks.clear();
	}
	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->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->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->PendingSendBlocks.clear();
 		PendingSendBlocks = m_pState->PendingSendBlocks.size();
 		Broadcast( MultiBlock );
 	}
	if( PendingSendBlocks > 0 )
	{
		for( unsigned int i = 0; i < PendingSendBlocks; i++)
		{
			unsigned int index = m_pState->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->PendingSendBlocks.clear();
	}

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

	std::map< unsigned int, int > ToTickBlocks = m_pState->ToTickBlocks;
	//unsigned int NumTickBlocks = ToTickBlocks.size();
	//if( NumTickBlocks > 0 ) LOG("To tick: %i", NumTickBlocks );
	m_pState->ToTickBlocks.clear();
	bool isRedstone = false;
	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_REDSTONE_REPEATER_OFF:
		case E_BLOCK_REDSTONE_REPEATER_ON:
		case E_BLOCK_REDSTONE_WIRE:
			{
				isRedstone = true;
			}
		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:		// Stuff that drops when block below is destroyed
			{
				if( GetBlock( X, Y-1, Z ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, E_BLOCK_AIR, 0 );

					int wX, wY, wZ;
					PositionToWorldPosition(X, Y, Z, wX, wY, wZ);

					m_World->GetSimulatorManager()->WakeUp(wX, wY, wZ);
					if (isRedstone) {
						cRedstone Redstone(m_World);
						Redstone.ChangeRedstone( (X+m_PosX*16), (Y+m_PosY*16), (Z+m_PosZ*16), false );
					}
					cPickup* Pickup = new cPickup( (X+m_PosX*16) * 32 + 16, (Y+m_PosY*128) * 32 + 16, (Z+m_PosZ*16) * 32 + 16, cItem( cBlockToPickup::ToPickup( (ENUM_ITEM_ID)BlockID, E_ITEM_EMPTY) , 1 ) );
					Pickup->Initialize( m_World );
				}
			}
			break;
		case E_BLOCK_REDSTONE_TORCH_OFF:
		case E_BLOCK_REDSTONE_TORCH_ON:
				isRedstone = true;
		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 );
				if( m_World->GetBlock( XX, YY, ZZ ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, 0, 0 );
					if (isRedstone) {
						cRedstone Redstone(m_World);
						Redstone.ChangeRedstone( (X+m_PosX*16), (Y+m_PosY*16), (Z+m_PosZ*16), false );
					}
					cPickup* Pickup = new cPickup( (X+m_PosX*16) * 32 + 16, (Y+m_PosY*128) * 32 + 16, (Z+m_PosZ*16) * 32 + 16, cItem( cBlockToPickup::ToPickup( (ENUM_ITEM_ID)BlockID, E_ITEM_EMPTY) , 1 ) );
					Pickup->Initialize( m_World );
				}
			}
			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 );
				if( m_World->GetBlock( XX, YY, ZZ ) == E_BLOCK_AIR )
				{
					SetBlock( X, Y, Z, E_BLOCK_AIR, 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( m_World );
				}
			}
			break;
		default:
			break;
		};
	}
	MTRand r1;
	// Tick dem blocks
	int RandomX = r1.randInt();
	int RandomY = r1.randInt();
	int RandomZ = r1.randInt();

	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 //changed to not allow grass if any one hit object is on top
				{
					FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_GRASS, GetLight( m_BlockMeta, Index ) );
				}
				if ( (g_BlockOneHitDig[AboveBlock]) && GetLight( m_BlockSkyLight, Index+1 ) > 0xf/2 ) // Half lit //ch$
				{
					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) || (g_BlockOneHitDig[AboveBlock]) || (g_BlockTransparent[AboveBlock]) ) ) //changed to not allow grass if any one hit object is on top
				{
					FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_DIRT, GetLight( m_BlockMeta, Index ) );
				}
			}
			break;
		case E_BLOCK_SAPLING: //todo: check meta of sapling. change m_World->GrowTree to look change trunk and leaves based on meta of sapling
			{
				FastSetBlock( m_BlockTickX, m_BlockTickY, m_BlockTickZ, E_BLOCK_AIR, GetLight( m_BlockMeta, Index ) );
				m_World->GrowTree( m_BlockTickX + m_PosX*16, m_BlockTickY, m_BlockTickZ + m_PosZ*16 );
			}
			break;
		case E_BLOCK_LEAVES: //todo, http://www.minecraftwiki.net/wiki/Data_values#Leaves
			{
			}
			break;
		default:
			break;
		}
	}

	// Tick block entities (furnace)
	std::list< cFurnaceEntity* > TickBlockEntites = m_pState->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->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->BlockEntities.push_back( new cChestEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
					}
					break;
				case E_BLOCK_FURNACE:
					{
						m_pState->BlockEntities.push_back( new cFurnaceEntity( x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
					}
					break;
				case E_BLOCK_SIGN_POST:
				case E_BLOCK_WALLSIGN:
					{
						m_pState->BlockEntities.push_back( new cSignEntity( BlockType, x + m_PosX*16, y + m_PosY*128, z + m_PosZ*16, this ) );
					}
					break;
				default:
					{
					}
					break;
				}
			}
		}
	}
}

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 = m_World->GetChunkUnreliable( m_PosX-1, m_PosY, m_PosZ );
	cChunk* RightChunk = m_World->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 = m_World->GetChunkUnreliable( m_PosX, m_PosY, m_PosZ-1 );
	cChunk* BackChunk = m_World->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 )		m_World->ReSpreadLighting( LeftChunk );
	if( bCalcRight )	m_World->ReSpreadLighting( RightChunk );
	if( bCalcFront )	m_World->ReSpreadLighting( FrontChunk );
	if( bCalcBack )		m_World->ReSpreadLighting( BackChunk );
}

void cChunk::AsyncUnload( cClientHandle* a_Client )
{
	m_pState->UnloadQuery.remove( a_Client );	// Make sure this client is only in the list once
	m_pState->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->BlockEntities.begin(); itr != m_pState->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->PendingSendBlocks.push_back( index );

		m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z ) ]++;
		m_pState->ToTickBlocks[ MakeIndex( a_X+1, a_Y, a_Z ) ]++;
		m_pState->ToTickBlocks[ MakeIndex( a_X-1, a_Y, a_Z ) ]++;
		m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y+1, a_Z ) ]++;
		m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y-1, a_Z ) ]++;
		m_pState->ToTickBlocks[ MakeIndex( a_X, a_Y, a_Z+1 ) ]++;
		m_pState->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, this ) );
			break;
		case E_BLOCK_FURNACE:
			AddBlockEntity( new cFurnaceEntity( a_X + m_PosX*16, a_Y + m_PosY*128, a_Z + m_PosZ*16, this ) );
			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, this ) );
			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->PendingSendBlocks.push_back( index );
	SetLight( m_BlockMeta, index, a_BlockMeta );

	// ONLY recalculate lighting if it's necessary!
	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->PendingSendBlocks.push_back( MakeIndex( a_X, a_Y, a_Z ) );
		return;
	}

	for( std::list< cClientHandle* >::iterator itr = m_pState->LoadedByClient.begin(); itr != m_pState->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->BlockEntities.push_back( a_BlockEntity );
}

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

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

	LockEntities();
	for( EntityList::iterator itr = m_pState->Entities.begin(); itr != m_pState->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->LoadedByClient.remove( a_Client );

	if( !a_Client->IsDestroyed() )
	{
		LockEntities();
		for( EntityList::iterator itr = m_pState->Entities.begin(); itr != m_pState->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->Entities.push_back( &a_Entity );
	UnlockEntities();
}

bool cChunk::RemoveEntity( cEntity & a_Entity, cChunk* a_CalledFrom /* = 0 */ )
{
	LockEntities();
	unsigned int SizeBefore = m_pState->Entities.size();
	m_pState->Entities.remove( &a_Entity );
	if( SizeBefore == m_pState->Entities.size() )
	{
		LOG("WARNING: Entity was not in chunk %i %i %i", m_PosX, m_PosY, m_PosZ );
		if( !a_CalledFrom )
		{
			UnlockEntities();
			return m_World->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->BlockEntities.begin(); itr != m_pState->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, this );
					if( !ChestEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING CHEST FROM FILE %s", SourceFile );
						delete ChestEntity;
						fclose(f);
						return false;
					}
					m_pState->BlockEntities.push_back( ChestEntity );
				}
				break;
			case E_BLOCK_FURNACE:
				{
					cFurnaceEntity* FurnaceEntity = new cFurnaceEntity( 0, 0, 0, this );
					if( !FurnaceEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING FURNACE FROM FILE %s", SourceFile );
						delete FurnaceEntity;
						fclose(f);
						return false;
					}
					m_pState->BlockEntities.push_back( FurnaceEntity );
					m_pState->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, this );
					if( !SignEntity->LoadFromFile( f ) )
					{
						LOGERROR("ERROR READING SIGN FROM FILE %s", SourceFile );
						delete SignEntity;
						fclose(f);
						return false;
					}
					m_pState->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->BlockEntities.begin(); itr != m_pState->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->LoadedByClient.begin(); itr != m_pState->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, this);
			if( !ChestEntity->LoadFromJson( Chest ) )
			{
				LOGERROR("ERROR READING CHEST FROM JSON!" );
				delete ChestEntity;
			}
			else m_pState->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, this);
			if( !FurnaceEntity->LoadFromJson( Furnace ) )
			{
				LOGERROR("ERROR READING FURNACE FROM JSON!" );
				delete FurnaceEntity;
			}
			else m_pState->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, this);
			if( !SignEntity->LoadFromJson( Sign ) )
			{
				LOGERROR("ERROR READING SIGN FROM JSON!" );
				delete SignEntity;
			}
			else m_pState->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->BlockEntities.begin(); itr != m_pState->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->Entities;
}

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


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

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


void cChunk::PositionToWorldPosition(int a_ChunkX, int a_ChunkY, int a_ChunkZ, int & a_X, int & a_Y, int & a_Z)
{
	a_Y = a_ChunkY;
	a_X = m_PosX * 16 + a_ChunkX;
	a_Z = m_PosZ * 16 + a_ChunkZ;
}