forked from cuberite/cuberite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkMap.cpp
2297 lines (1671 loc) · 52.6 KB
/
ChunkMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "ChunkMap.h"
#include "World.h"
#include "Root.h"
#include "Entities/Player.h"
#include "Item.h"
#include "Chunk.h"
#include "Generating/Trees.h" // used in cChunkMap::ReplaceTreeBlocks() for tree block discrimination
#include "BlockArea.h"
#include "Bindings/PluginManager.h"
#include "Entities/TNTEntity.h"
#include "Blocks/BlockHandler.h"
#include "MobCensus.h"
#include "MobSpawner.h"
#include "BoundingBox.h"
#include "SetChunkData.h"
#include "Blocks/ChunkInterface.h"
#include "Entities/Pickup.h"
#include "DeadlockDetect.h"
#include "BlockEntities/BlockEntity.h"
#ifndef _WIN32
#include <cstdlib> // abs
#endif
#include "zlib/zlib.h"
#include "json/json.h"
////////////////////////////////////////////////////////////////////////////////
// cChunkMap:
cChunkMap::cChunkMap(cWorld * a_World) :
m_World(a_World),
m_Pool(
cpp14::make_unique<cListAllocationPool<cChunkData::sChunkSection>>(
cpp14::make_unique<cStarvationCallbacks>(), 1600u, 5000u
)
)
{
}
cChunkMap::~cChunkMap()
{
// Explicitly destroy all chunks, so that they're guaranteed to be
// destroyed before other internals. This fixes crashes on stopping the server.
// because the chunk destructor deletes entities and those may access the chunkmap.
// Also, the cChunkData destructor accesses the chunkMap's allocator.
m_Chunks.clear();
}
cChunkPtr cChunkMap::ConstructChunk(int a_ChunkX, int a_ChunkZ)
{
auto Chunk = FindChunk(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return (
*m_Chunks.emplace(
ChunkCoordinate{ a_ChunkX, a_ChunkZ },
cpp14::make_unique<cChunk>(
a_ChunkX,
a_ChunkZ,
this,
GetWorld(),
FindChunk(a_ChunkX - 1, a_ChunkZ),
FindChunk(a_ChunkX + 1, a_ChunkZ),
FindChunk(a_ChunkX, a_ChunkZ - 1),
FindChunk(a_ChunkX, a_ChunkZ + 1),
*m_Pool
)
).first
).second.get();
}
return Chunk;
}
cChunkPtr cChunkMap::GetChunk(int a_ChunkX, int a_ChunkZ)
{
ASSERT(m_CSChunks.IsLockedByCurrentThread()); // m_CSChunks should already be locked by the operation that called us
auto Chunk = ConstructChunk(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return nullptr;
}
if (!Chunk->IsValid() && !Chunk->IsQueued())
{
Chunk->SetPresence(cChunk::cpQueued);
Chunk->SetShouldGenerateIfLoadFailed(true);
m_World->GetStorage().QueueLoadChunk(a_ChunkX, a_ChunkZ);
}
return Chunk;
}
cChunkPtr cChunkMap::GetChunkNoGen(cChunkCoords a_Chunk)
{
ASSERT(m_CSChunks.IsLockedByCurrentThread()); // m_CSChunks should already be locked by the operation that called us
auto Chunk = ConstructChunk(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ);
if (Chunk == nullptr)
{
return nullptr;
}
if (!Chunk->IsValid() && !Chunk->IsQueued())
{
Chunk->SetPresence(cChunk::cpQueued);
m_World->GetStorage().QueueLoadChunk(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ);
}
return Chunk;
}
cChunkPtr cChunkMap::GetChunkNoLoad(cChunkCoords a_Coords)
{
ASSERT(m_CSChunks.IsLockedByCurrentThread()); // m_CSChunks should already be locked by the operation that called us
return ConstructChunk(a_Coords.m_ChunkX, a_Coords.m_ChunkZ);
}
cChunk * cChunkMap::FindChunk(int a_ChunkX, int a_ChunkZ)
{
ASSERT(m_CSChunks.IsLockedByCurrentThread());
auto Chunk = m_Chunks.find({ a_ChunkX, a_ChunkZ });
return (Chunk == m_Chunks.end()) ? nullptr : Chunk->second.get();
}
void cChunkMap::SendBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle & a_Client)
{
cCSLock Lock(m_CSChunks);
int ChunkX, ChunkZ;
cChunkDef::BlockToChunk(a_BlockX, a_BlockZ, ChunkX, ChunkZ);
cChunkPtr Chunk = GetChunkNoGen(ChunkX, ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return;
}
Chunk->SendBlockEntity(a_BlockX, a_BlockY, a_BlockZ, a_Client);
}
bool cChunkMap::UseBlockEntity(cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ)
{
// a_Player rclked block entity at the coords specified, handle it
cCSLock Lock(m_CSChunks);
int ChunkX, ChunkZ;
cChunkDef::BlockToChunk(a_BlockX, a_BlockZ, ChunkX, ChunkZ);
cChunkPtr Chunk = GetChunkNoGen(ChunkX, ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return false;
}
return Chunk->UseBlockEntity(a_Player, a_BlockX, a_BlockY, a_BlockZ);
}
bool cChunkMap::DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback a_Callback)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoLoad(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return false;
}
return a_Callback(*Chunk);
}
bool cChunkMap::DoWithChunkAt(Vector3i a_BlockPos, cChunkCallback a_Callback)
{
int ChunkX, ChunkZ;
cChunkDef::BlockToChunk(a_BlockPos.x, a_BlockPos.z, ChunkX, ChunkZ);
return DoWithChunk(ChunkX, ChunkZ, a_Callback);
}
void cChunkMap::WakeUpSimulators(Vector3i a_Block)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(cChunkDef::BlockToChunk(a_Block));
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return;
}
m_World->GetSimulatorManager()->WakeUp(a_Block, Chunk);
}
void cChunkMap::MarkChunkDirty(int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return;
}
Chunk->MarkDirty();
}
void cChunkMap::MarkChunkSaving(int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return;
}
Chunk->MarkSaving();
}
void cChunkMap::MarkChunkSaved (int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return;
}
Chunk->MarkSaved();
}
void cChunkMap::SetChunkData(cSetChunkData & a_SetChunkData)
{
int ChunkX = a_SetChunkData.GetChunkX();
int ChunkZ = a_SetChunkData.GetChunkZ();
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoLoad(ChunkX, ChunkZ);
if (Chunk == nullptr)
{
return;
}
Chunk->SetAllData(a_SetChunkData);
if (a_SetChunkData.ShouldMarkDirty())
{
Chunk->MarkDirty();
}
// Notify relevant ChunkStays:
cChunkStays ToBeDisabled;
for (cChunkStays::iterator itr = m_ChunkStays.begin(), end = m_ChunkStays.end(); itr != end; ++itr)
{
if ((*itr)->ChunkAvailable(ChunkX, ChunkZ))
{
// The chunkstay wants to be disabled, add it to a list of to-be-disabled chunkstays for later processing:
ToBeDisabled.push_back(*itr);
}
} // for itr - m_ChunkStays[]
// Disable (and possibly remove) the chunkstays that chose to get disabled:
for (cChunkStays::iterator itr = ToBeDisabled.begin(), end = ToBeDisabled.end(); itr != end; ++itr)
{
(*itr)->Disable();
}
}
// Notify plugins of the chunk becoming available
cPluginManager::Get()->CallHookChunkAvailable(*m_World, ChunkX, ChunkZ);
}
void cChunkMap::ChunkLighted(
int a_ChunkX, int a_ChunkZ,
const cChunkDef::BlockNibbles & a_BlockLight,
const cChunkDef::BlockNibbles & a_SkyLight
)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoLoad(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return;
}
Chunk->SetLight(a_BlockLight, a_SkyLight);
Chunk->MarkDirty();
}
bool cChunkMap::GetChunkData(cChunkCoords a_Coords, cChunkDataCallback & a_Callback)
{
if (!a_Callback.Coords(a_Coords.m_ChunkX, a_Coords.m_ChunkZ))
{
// The callback doesn't want the data
return false;
}
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_Coords);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
// The chunk is not present
return false;
}
Chunk->GetAllData(a_Callback);
return true;
}
bool cChunkMap::GetChunkBlockTypes(int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_BlockTypes)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return false;
}
Chunk->GetBlockTypes(a_BlockTypes);
return true;
}
bool cChunkMap::IsChunkQueued(int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoLoad(a_ChunkX, a_ChunkZ);
return (Chunk != nullptr) && Chunk->IsQueued();
}
bool cChunkMap::IsChunkValid(int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoLoad(a_ChunkX, a_ChunkZ);
return (Chunk != nullptr) && Chunk->IsValid();
}
bool cChunkMap::HasChunkAnyClients(int a_ChunkX, int a_ChunkZ)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
return (Chunk != nullptr) && Chunk->HasAnyClients();
}
int cChunkMap::GetHeight(int a_BlockX, int a_BlockZ)
{
for (;;)
{
cCSLock Lock(m_CSChunks);
int ChunkX, ChunkZ, BlockY = 0;
cChunkDef::AbsoluteToRelative(a_BlockX, BlockY, a_BlockZ, ChunkX, ChunkZ);
cChunkPtr Chunk = GetChunk(ChunkX, ChunkZ);
if (Chunk == nullptr)
{
return 0;
}
if (Chunk->IsValid())
{
return Chunk->GetHeight(a_BlockX, a_BlockZ);
}
// The chunk is not valid, wait for it to become valid:
cCSUnlock Unlock(Lock);
m_evtChunkValid.Wait();
} // while (true)
}
bool cChunkMap::TryGetHeight(int a_BlockX, int a_BlockZ, int & a_Height)
{
// Returns false if chunk not loaded / generated
cCSLock Lock(m_CSChunks);
int ChunkX, ChunkZ, BlockY = 0;
cChunkDef::AbsoluteToRelative(a_BlockX, BlockY, a_BlockZ, ChunkX, ChunkZ);
cChunkPtr Chunk = GetChunkNoLoad(ChunkX, ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
return false;
}
a_Height = Chunk->GetHeight(a_BlockX, a_BlockZ);
return true;
}
void cChunkMap::FastSetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
chunk->FastSetBlock(relPos, a_BlockType, a_BlockMeta);
}
}
void cChunkMap::SetBlocks(const sSetBlockVector & a_Blocks)
{
cCSLock lock(m_CSChunks);
cChunkPtr chunk = nullptr;
int lastChunkX = 0x7fffffff; // Bogus coords so that chunk is updated on first pass
int lastChunkZ = 0x7fffffff;
for (auto block: a_Blocks)
{
// Update the chunk, if different from last time:
if ((block.m_ChunkX != lastChunkX) || (block.m_ChunkZ != lastChunkZ))
{
lastChunkX = block.m_ChunkX;
lastChunkZ = block.m_ChunkZ;
chunk = GetChunk(lastChunkX, lastChunkZ);
}
// If the chunk is valid, set the block:
if (chunk != nullptr)
{
chunk->SetBlock({block.m_RelX, block.m_RelY, block.m_RelZ}, block.m_BlockType, block.m_BlockMeta);
}
} // for block - a_Blocks[]
}
void cChunkMap::CollectPickupsByPlayer(cPlayer & a_Player)
{
int BlockX = static_cast<int>(a_Player.GetPosX()); // Truncating doesn't matter much; we're scanning entire chunks anyway
int BlockY = static_cast<int>(a_Player.GetPosY());
int BlockZ = static_cast<int>(a_Player.GetPosZ());
int ChunkX = 0, ChunkZ = 0;
cChunkDef::AbsoluteToRelative(BlockX, BlockY, BlockZ, ChunkX, ChunkZ);
int OtherChunkX = ChunkX + ((BlockX > 8) ? 1 : -1);
int OtherChunkZ = ChunkZ + ((BlockZ > 8) ? 1 : -1);
// We suppose that each player keeps their chunks in memory, therefore it makes little sense to try to re-load or even generate them.
// The only time the chunks are not valid is when the player is downloading the initial world and they should not call this at that moment
cCSLock Lock(m_CSChunks);
GetChunkNoLoad(ChunkX, ChunkZ)->CollectPickupsByPlayer(a_Player);
// Check the neighboring chunks as well:
GetChunkNoLoad(OtherChunkX, ChunkZ)->CollectPickupsByPlayer (a_Player);
GetChunkNoLoad(OtherChunkX, OtherChunkZ)->CollectPickupsByPlayer(a_Player);
GetChunkNoLoad(ChunkX, ChunkZ)->CollectPickupsByPlayer (a_Player);
GetChunkNoLoad(ChunkX, OtherChunkZ)->CollectPickupsByPlayer(a_Player);
}
BLOCKTYPE cChunkMap::GetBlock(Vector3i a_BlockPos)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
return chunk->GetBlock(relPos);
}
return 0;
}
NIBBLETYPE cChunkMap::GetBlockMeta(Vector3i a_BlockPos)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
return chunk->GetMeta(relPos);
}
return 0;
}
NIBBLETYPE cChunkMap::GetBlockSkyLight(Vector3i a_BlockPos)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
return chunk->GetSkyLight(relPos);
}
return 0;
}
NIBBLETYPE cChunkMap::GetBlockBlockLight(Vector3i a_BlockPos)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
return chunk->GetBlockLight(relPos);
}
return 0;
}
void cChunkMap::SetBlockMeta(Vector3i a_BlockPos, NIBBLETYPE a_BlockMeta, bool a_ShouldMarkDirty, bool a_ShouldInformClients)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
chunk->SetMeta(relPos, a_BlockMeta, a_ShouldMarkDirty, a_ShouldInformClients);
}
}
void cChunkMap::SetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
BLOCKTYPE blockType;
NIBBLETYPE blockMeta;
GetBlockTypeMeta(a_BlockPos, blockType, blockMeta);
cChunkInterface ChunkInterface(this);
BlockHandler(blockType)->OnBroken(ChunkInterface, *m_World, a_BlockPos, blockType, blockMeta);
chunk->SetBlock(relPos, a_BlockType, a_BlockMeta);
m_World->GetSimulatorManager()->WakeUp(a_BlockPos, chunk);
BlockHandler(a_BlockType)->OnPlaced(ChunkInterface, *m_World, a_BlockPos, a_BlockType, a_BlockMeta);
}
}
bool cChunkMap::GetBlockTypeMeta(Vector3i a_BlockPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta)
{
auto chunkCoord = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkCoord);
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkCoord.m_ChunkX, chunkCoord.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
chunk->GetBlockTypeMeta(relPos, a_BlockType, a_BlockMeta);
return true;
}
return false;
}
bool cChunkMap::GetBlockInfo(Vector3i a_BlockPos, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight)
{
auto chunkPos = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkPos);
// Query the chunk, if loaded:
cCSLock Lock(m_CSChunks);
auto chunk = GetChunk(chunkPos.m_ChunkX, chunkPos.m_ChunkZ);
if ((chunk != nullptr) && chunk->IsValid())
{
chunk->GetBlockInfo(relPos, a_BlockType, a_Meta, a_SkyLight, a_BlockLight);
return true;
}
return false;
}
void cChunkMap::ReplaceBlocks(const sSetBlockVector & a_Blocks, BLOCKTYPE a_FilterBlockType)
{
cCSLock Lock(m_CSChunks);
for (sSetBlockVector::const_iterator itr = a_Blocks.begin(); itr != a_Blocks.end(); ++itr)
{
auto chunk = GetChunk(itr->m_ChunkX, itr->m_ChunkZ);
if ((chunk == nullptr) || !chunk->IsValid())
{
continue;
}
Vector3i relPos(itr->m_RelX, itr->m_RelY, itr->m_RelZ);
if (chunk->GetBlock(relPos) == a_FilterBlockType)
{
chunk->SetBlock(relPos, itr->m_BlockType, itr->m_BlockMeta);
}
}
}
void cChunkMap::ReplaceTreeBlocks(const sSetBlockVector & a_Blocks)
{
cCSLock Lock(m_CSChunks);
for (sSetBlockVector::const_iterator itr = a_Blocks.begin(); itr != a_Blocks.end(); ++itr)
{
auto chunk = GetChunk(itr->m_ChunkX, itr->m_ChunkZ);
if ((chunk == nullptr) || !chunk->IsValid())
{
continue;
}
Vector3i relPos(itr->m_RelX, itr->m_RelY, itr->m_RelZ);
switch (chunk->GetBlock(relPos))
{
CASE_TREE_OVERWRITTEN_BLOCKS:
{
chunk->SetBlock(relPos, itr->m_BlockType, itr->m_BlockMeta);
break;
}
case E_BLOCK_LEAVES:
case E_BLOCK_NEW_LEAVES:
{
if ((itr->m_BlockType == E_BLOCK_LOG) || (itr->m_BlockType == E_BLOCK_NEW_LOG))
{
chunk->SetBlock(relPos, itr->m_BlockType, itr->m_BlockMeta);
}
break;
}
}
} // for itr - a_Blocks[]
}
EMCSBiome cChunkMap::GetBiomeAt (int a_BlockX, int a_BlockZ)
{
int ChunkX, ChunkZ, X = a_BlockX, Y = 0, Z = a_BlockZ;
cChunkDef::AbsoluteToRelative(X, Y, Z, ChunkX, ChunkZ);
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunk(ChunkX, ChunkZ);
if ((Chunk != nullptr) && Chunk->IsValid())
{
return Chunk->GetBiomeAt(X, Z);
}
else
{
return m_World->GetGenerator().GetBiomeAt(a_BlockX, a_BlockZ);
}
}
bool cChunkMap::SetBiomeAt(int a_BlockX, int a_BlockZ, EMCSBiome a_Biome)
{
int ChunkX, ChunkZ, X = a_BlockX, Y = 0, Z = a_BlockZ;
cChunkDef::AbsoluteToRelative(X, Y, Z, ChunkX, ChunkZ);
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunk(ChunkX, ChunkZ);
if ((Chunk != nullptr) && Chunk->IsValid())
{
Chunk->SetBiomeAt(X, Z, a_Biome);
return true;
}
return false;
}
bool cChunkMap::SetAreaBiome(int a_MinX, int a_MaxX, int a_MinZ, int a_MaxZ, EMCSBiome a_Biome)
{
// Translate coords to relative:
int Y = 0;
int MinChunkX, MinChunkZ, MinX = a_MinX, MinZ = a_MinZ;
int MaxChunkX, MaxChunkZ, MaxX = a_MaxX, MaxZ = a_MaxZ;
cChunkDef::AbsoluteToRelative(MinX, Y, MinZ, MinChunkX, MinChunkZ);
cChunkDef::AbsoluteToRelative(MaxX, Y, MaxZ, MaxChunkX, MaxChunkZ);
// Go through all chunks, set:
bool res = true;
cCSLock Lock(m_CSChunks);
for (int x = MinChunkX; x <= MaxChunkX; x++)
{
int MinRelX = (x == MinChunkX) ? MinX : 0;
int MaxRelX = (x == MaxChunkX) ? MaxX : cChunkDef::Width - 1;
for (int z = MinChunkZ; z <= MaxChunkZ; z++)
{
int MinRelZ = (z == MinChunkZ) ? MinZ : 0;
int MaxRelZ = (z == MaxChunkZ) ? MaxZ : cChunkDef::Width - 1;
cChunkPtr Chunk = GetChunkNoLoad(x, z);
if ((Chunk != nullptr) && Chunk->IsValid())
{
Chunk->SetAreaBiome(MinRelX, MaxRelX, MinRelZ, MaxRelZ, a_Biome);
}
else
{
res = false;
}
} // for z
} // for x
return res;
}
bool cChunkMap::GetBlocks(sSetBlockVector & a_Blocks, bool a_ContinueOnFailure)
{
bool res = true;
cCSLock Lock(m_CSChunks);
for (sSetBlockVector::iterator itr = a_Blocks.begin(); itr != a_Blocks.end(); ++itr)
{
cChunkPtr Chunk = GetChunk(itr->m_ChunkX, itr->m_ChunkZ);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
if (!a_ContinueOnFailure)
{
return false;
}
res = false;
continue;
}
itr->m_BlockType = Chunk->GetBlock(itr->m_RelX, itr->m_RelY, itr->m_RelZ);
itr->m_BlockMeta = Chunk->GetMeta(itr->m_RelX, itr->m_RelY, itr->m_RelZ);
}
return res;
}
bool cChunkMap::DigBlock(Vector3i a_BlockPos)
{
auto chunkCoords = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkCoords);
{
cCSLock Lock(m_CSChunks);
auto destChunk = GetChunk(chunkCoords.m_ChunkX, chunkCoords.m_ChunkZ);
if ((destChunk == nullptr) || !destChunk->IsValid())
{
return false;
}
destChunk->SetBlock(relPos, E_BLOCK_AIR, 0);
m_World->GetSimulatorManager()->WakeUp(a_BlockPos, destChunk);
}
return true;
}
cItems cChunkMap::PickupsFromBlock(Vector3i a_BlockPos, const cEntity * a_Digger, const cItem * a_Tool)
{
auto chunkCoords = cChunkDef::BlockToChunk(a_BlockPos);
auto relPos = cChunkDef::AbsoluteToRelative(a_BlockPos, chunkCoords);
cCSLock Lock(m_CSChunks);
auto destChunk = GetChunk(chunkCoords.m_ChunkX, chunkCoords.m_ChunkZ);
if ((destChunk == nullptr) || !destChunk->IsValid())
{
return {};
}
return destChunk->PickupsFromBlock(relPos, a_Digger, a_Tool);
}
void cChunkMap::SendBlockTo(int a_X, int a_Y, int a_Z, cPlayer & a_Player)
{
int ChunkX, ChunkZ;
cChunkDef::AbsoluteToRelative(a_X, a_Y, a_Z, ChunkX, ChunkZ);
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunk(ChunkX, ChunkZ);
if ((Chunk != nullptr) && (Chunk->IsValid()))
{
Chunk->SendBlockTo(a_X, a_Y, a_Z, a_Player.GetClientHandle());
}
}
void cChunkMap::CompareChunkClients(int a_ChunkX1, int a_ChunkZ1, int a_ChunkX2, int a_ChunkZ2, cClientDiffCallback & a_Callback)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk1 = GetChunkNoGen(a_ChunkX1, a_ChunkZ1);
if (Chunk1 == nullptr)
{
return;
}
cChunkPtr Chunk2 = GetChunkNoGen(a_ChunkX2, a_ChunkZ2);
if (Chunk2 == nullptr)
{
return;
}
CompareChunkClients(Chunk1, Chunk2, a_Callback);
}
void cChunkMap::CompareChunkClients(cChunk * a_Chunk1, cChunk * a_Chunk2, cClientDiffCallback & a_Callback)
{
auto Clients1 = a_Chunk1->GetAllClients();
auto Clients2 = a_Chunk2->GetAllClients();
// Find "removed" clients:
for (auto * Client : Clients1)
{
bool Found = (std::find(Clients2.begin(), Clients2.end(), Client) != Clients2.end());
if (!Found)
{
a_Callback.Removed(Client);
}
} // for Client - Clients1[]
// Find "added" clients:
for (auto * Client : Clients2)
{
bool Found = (std::find(Clients1.begin(), Clients1.end(), Client) != Clients1.end());
if (!Found)
{
a_Callback.Added(Client);
}
} // for Client - Clients2[]
}
bool cChunkMap::AddChunkClient(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunk(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return false;
}
return Chunk->AddClient(a_Client);
}
void cChunkMap::RemoveChunkClient(int a_ChunkX, int a_ChunkZ, cClientHandle * a_Client)
{
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
if (Chunk == nullptr)
{
return;
}
Chunk->RemoveClient(a_Client);
}
void cChunkMap::RemoveClientFromChunks(cClientHandle * a_Client)
{
cCSLock Lock(m_CSChunks);
for (const auto & Chunk : m_Chunks)
{
Chunk.second->RemoveClient(a_Client);
}
}