forked from minetest/minetest
-
Notifications
You must be signed in to change notification settings - Fork 10
/
map.cpp
2065 lines (1759 loc) · 53.8 KB
/
map.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
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "filesys.h"
#include "voxel.h"
#include "voxelalgorithms.h"
#include "porting.h"
#include "serialization.h"
#include "nodemetadata.h"
#include "settings.h"
#include "log.h"
#include "profiler.h"
#include "nodedef.h"
#include "gamedef.h"
#include "util/directiontables.h"
#include "util/basic_macros.h"
#include "rollback_interface.h"
#include "environment.h"
#include "reflowscan.h"
#include "emerge.h"
#include "mapgen/mapgen_v6.h"
#include "mapgen/mg_biome.h"
#include "config.h"
#include "server.h"
#include "database/database.h"
#include "database/database-dummy.h"
#include "database/database-sqlite3.h"
#include "script/scripting_server.h"
#include <deque>
#include <queue>
#if USE_LEVELDB
#include "database/database-leveldb.h"
#endif
#if USE_REDIS
#include "database/database-redis.h"
#endif
#if USE_POSTGRESQL
#include "database/database-postgresql.h"
#endif
/*
Map
*/
Map::Map(IGameDef *gamedef):
m_gamedef(gamedef),
m_nodedef(gamedef->ndef())
{
}
Map::~Map()
{
/*
Free all MapSectors
*/
for (auto §or : m_sectors) {
delete sector.second;
}
}
void Map::addEventReceiver(MapEventReceiver *event_receiver)
{
m_event_receivers.insert(event_receiver);
}
void Map::removeEventReceiver(MapEventReceiver *event_receiver)
{
m_event_receivers.erase(event_receiver);
}
void Map::dispatchEvent(const MapEditEvent &event)
{
for (MapEventReceiver *event_receiver : m_event_receivers) {
event_receiver->onMapEditEvent(event);
}
}
MapSector * Map::getSectorNoGenerateNoLock(v2s16 p)
{
if(m_sector_cache != NULL && p == m_sector_cache_p){
MapSector * sector = m_sector_cache;
return sector;
}
auto n = m_sectors.find(p);
if (n == m_sectors.end())
return NULL;
MapSector *sector = n->second;
// Cache the last result
m_sector_cache_p = p;
m_sector_cache = sector;
return sector;
}
MapSector * Map::getSectorNoGenerate(v2s16 p)
{
return getSectorNoGenerateNoLock(p);
}
MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
{
v2s16 p2d(p3d.X, p3d.Z);
MapSector * sector = getSectorNoGenerate(p2d);
if(sector == NULL)
return NULL;
MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
return block;
}
MapBlock * Map::getBlockNoCreate(v3s16 p3d)
{
MapBlock *block = getBlockNoCreateNoEx(p3d);
if(block == NULL)
throw InvalidPositionException();
return block;
}
bool Map::isValidPosition(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
return (block != NULL);
}
// Returns a CONTENT_IGNORE node if not found
MapNode Map::getNode(v3s16 p, bool *is_valid_position)
{
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if (block == NULL) {
if (is_valid_position != NULL)
*is_valid_position = false;
return {CONTENT_IGNORE};
}
v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
MapNode node = block->getNodeNoCheck(relpos);
if (is_valid_position != NULL)
*is_valid_position = true;
return node;
}
static void set_node_in_block(MapBlock *block, v3s16 relpos, MapNode n)
{
// Never allow placing CONTENT_IGNORE, it causes problems
if(n.getContent() == CONTENT_IGNORE){
const NodeDefManager *nodedef = block->getParent()->getNodeDefManager();
v3s16 blockpos = block->getPos();
v3s16 p = blockpos * MAP_BLOCKSIZE + relpos;
errorstream<<"Not allowing to place CONTENT_IGNORE"
<<" while trying to replace \""
<<nodedef->get(block->getNodeNoCheck(relpos)).name
<<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
return;
}
block->setNodeNoCheck(relpos, n);
}
// throws InvalidPositionException if not found
void Map::setNode(v3s16 p, MapNode n)
{
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreate(blockpos);
v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
set_node_in_block(block, relpos, n);
}
void Map::addNodeAndUpdate(v3s16 p, MapNode n,
std::map<v3s16, MapBlock*> &modified_blocks,
bool remove_metadata)
{
// Collect old node for rollback
RollbackNode rollback_oldnode(this, p, m_gamedef);
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreate(blockpos);
v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
// This is needed for updating the lighting
MapNode oldnode = block->getNodeNoCheck(relpos);
// Remove node metadata
if (remove_metadata) {
removeNodeMetadata(p);
}
// Set the node on the map
ContentLightingFlags f = m_nodedef->getLightingFlags(n);
ContentLightingFlags oldf = m_nodedef->getLightingFlags(oldnode);
if (f == oldf) {
// No light update needed, just copy over the old light.
n.setLight(LIGHTBANK_DAY, oldnode.getLightRaw(LIGHTBANK_DAY, oldf), f);
n.setLight(LIGHTBANK_NIGHT, oldnode.getLightRaw(LIGHTBANK_NIGHT, oldf), f);
set_node_in_block(block, relpos, n);
modified_blocks[blockpos] = block;
} else {
// Ignore light (because calling voxalgo::update_lighting_nodes)
n.setLight(LIGHTBANK_DAY, 0, f);
n.setLight(LIGHTBANK_NIGHT, 0, f);
set_node_in_block(block, relpos, n);
// Update lighting
std::vector<std::pair<v3s16, MapNode> > oldnodes;
oldnodes.emplace_back(p, oldnode);
voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
for (auto &modified_block : modified_blocks) {
modified_block.second->expireDayNightDiff();
}
}
// Report for rollback
if(m_gamedef->rollback())
{
RollbackNode rollback_newnode(this, p, m_gamedef);
RollbackAction action;
action.setSetNode(p, rollback_oldnode, rollback_newnode);
m_gamedef->rollback()->reportAction(action);
}
}
void Map::removeNodeAndUpdate(v3s16 p,
std::map<v3s16, MapBlock*> &modified_blocks)
{
addNodeAndUpdate(p, MapNode(CONTENT_AIR), modified_blocks, true);
}
bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
{
MapEditEvent event;
event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
event.p = p;
event.n = n;
bool succeeded = true;
try{
std::map<v3s16, MapBlock*> modified_blocks;
addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
// Copy modified_blocks to event
for (auto &modified_block : modified_blocks) {
event.modified_blocks.insert(modified_block.first);
}
}
catch(InvalidPositionException &e){
succeeded = false;
}
dispatchEvent(event);
return succeeded;
}
bool Map::removeNodeWithEvent(v3s16 p)
{
MapEditEvent event;
event.type = MEET_REMOVENODE;
event.p = p;
bool succeeded = true;
try{
std::map<v3s16, MapBlock*> modified_blocks;
removeNodeAndUpdate(p, modified_blocks);
// Copy modified_blocks to event
for (auto &modified_block : modified_blocks) {
event.modified_blocks.insert(modified_block.first);
}
}
catch(InvalidPositionException &e){
succeeded = false;
}
dispatchEvent(event);
return succeeded;
}
struct TimeOrderedMapBlock {
MapSector *sect;
MapBlock *block;
TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
sect(sect),
block(block)
{}
bool operator<(const TimeOrderedMapBlock &b) const
{
return block->getUsageTimer() < b.block->getUsageTimer();
};
};
/*
Updates usage timers
*/
void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
std::vector<v3s16> *unloaded_blocks)
{
bool save_before_unloading = maySaveBlocks();
// Profile modified reasons
Profiler modprofiler;
std::vector<v2s16> sector_deletion_queue;
u32 deleted_blocks_count = 0;
u32 saved_blocks_count = 0;
u32 block_count_all = 0;
const auto start_time = porting::getTimeUs();
beginSave();
// If there is no practical limit, we spare creation of mapblock_queue
if (max_loaded_blocks < 0) {
for (auto §or_it : m_sectors) {
MapSector *sector = sector_it.second;
bool all_blocks_deleted = true;
MapBlockVect blocks;
sector->getBlocks(blocks);
for (MapBlock *block : blocks) {
block->incrementUsageTimer(dtime);
if (block->refGet() == 0
&& block->getUsageTimer() > unload_timeout) {
v3s16 p = block->getPos();
// Save if modified
if (block->getModified() != MOD_STATE_CLEAN
&& save_before_unloading) {
modprofiler.add(block->getModifiedReasonString(), 1);
if (!saveBlock(block))
continue;
saved_blocks_count++;
}
// Delete from memory
sector->deleteBlock(block);
if (unloaded_blocks)
unloaded_blocks->push_back(p);
deleted_blocks_count++;
} else {
all_blocks_deleted = false;
block_count_all++;
}
}
// Delete sector if we emptied it
if (all_blocks_deleted) {
sector_deletion_queue.push_back(sector_it.first);
}
}
} else {
std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
for (auto §or_it : m_sectors) {
MapSector *sector = sector_it.second;
MapBlockVect blocks;
sector->getBlocks(blocks);
for (MapBlock *block : blocks) {
block->incrementUsageTimer(dtime);
mapblock_queue.push(TimeOrderedMapBlock(sector, block));
}
}
block_count_all = mapblock_queue.size();
// Delete old blocks, and blocks over the limit from the memory
while (!mapblock_queue.empty() && ((s32)mapblock_queue.size() > max_loaded_blocks
|| mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
TimeOrderedMapBlock b = mapblock_queue.top();
mapblock_queue.pop();
MapBlock *block = b.block;
if (block->refGet() != 0)
continue;
v3s16 p = block->getPos();
// Save if modified
if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
modprofiler.add(block->getModifiedReasonString(), 1);
if (!saveBlock(block))
continue;
saved_blocks_count++;
}
// Delete from memory
b.sect->deleteBlock(block);
if (unloaded_blocks)
unloaded_blocks->push_back(p);
deleted_blocks_count++;
block_count_all--;
}
// Delete empty sectors
for (auto §or_it : m_sectors) {
if (sector_it.second->empty()) {
sector_deletion_queue.push_back(sector_it.first);
}
}
}
endSave();
const auto end_time = porting::getTimeUs();
reportMetrics(end_time - start_time, saved_blocks_count, block_count_all);
// Finally delete the empty sectors
deleteSectors(sector_deletion_queue);
if(deleted_blocks_count != 0)
{
PrintInfo(infostream); // ServerMap/ClientMap:
infostream<<"Unloaded "<<deleted_blocks_count
<<" blocks from memory";
if(save_before_unloading)
infostream<<", of which "<<saved_blocks_count<<" were written";
infostream<<", "<<block_count_all<<" blocks in memory";
infostream<<"."<<std::endl;
if(saved_blocks_count != 0){
PrintInfo(infostream); // ServerMap/ClientMap:
infostream<<"Blocks modified by: "<<std::endl;
modprofiler.print(infostream);
}
}
}
void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
{
timerUpdate(0.0, -1.0, 0, unloaded_blocks);
}
void Map::deleteSectors(std::vector<v2s16> §orList)
{
for (v2s16 j : sectorList) {
MapSector *sector = m_sectors[j];
// If sector is in sector cache, remove it from there
if(m_sector_cache == sector)
m_sector_cache = NULL;
// Remove from map and delete
m_sectors.erase(j);
delete sector;
}
}
void Map::PrintInfo(std::ostream &out)
{
out<<"Map: ";
}
#define WATER_DROP_BOOST 4
const static v3s16 liquid_6dirs[6] = {
// order: upper before same level before lower
v3s16( 0, 1, 0),
v3s16( 0, 0, 1),
v3s16( 1, 0, 0),
v3s16( 0, 0,-1),
v3s16(-1, 0, 0),
v3s16( 0,-1, 0)
};
enum NeighborType : u8 {
NEIGHBOR_UPPER,
NEIGHBOR_SAME_LEVEL,
NEIGHBOR_LOWER
};
struct NodeNeighbor {
MapNode n;
NeighborType t;
v3s16 p;
NodeNeighbor()
: n(CONTENT_AIR), t(NEIGHBOR_SAME_LEVEL)
{ }
NodeNeighbor(const MapNode &node, NeighborType n_type, const v3s16 &pos)
: n(node),
t(n_type),
p(pos)
{ }
};
void ServerMap::transforming_liquid_add(v3s16 p) {
m_transforming_liquid.push_back(p);
}
void ServerMap::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
ServerEnvironment *env)
{
u32 loopcount = 0;
u32 initial_size = m_transforming_liquid.size();
/*if(initial_size != 0)
infostream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
// list of nodes that due to viscosity have not reached their max level height
std::vector<v3s16> must_reflow;
std::vector<std::pair<v3s16, MapNode> > changed_nodes;
std::vector<v3s16> check_for_falling;
u32 liquid_loop_max = g_settings->getS32("liquid_loop_max");
u32 loop_max = liquid_loop_max;
while (m_transforming_liquid.size() != 0)
{
// This should be done here so that it is done when continue is used
if (loopcount >= initial_size || loopcount >= loop_max)
break;
loopcount++;
/*
Get a queued transforming liquid node
*/
v3s16 p0 = m_transforming_liquid.front();
m_transforming_liquid.pop_front();
MapNode n0 = getNode(p0);
/*
Collect information about current node
*/
s8 liquid_level = -1;
// The liquid node which will be placed there if
// the liquid flows into this node.
content_t liquid_kind = CONTENT_IGNORE;
// The node which will be placed there if liquid
// can't flow into this node.
content_t floodable_node = CONTENT_AIR;
const ContentFeatures &cf = m_nodedef->get(n0);
LiquidType liquid_type = cf.liquid_type;
switch (liquid_type) {
case LIQUID_SOURCE:
liquid_level = LIQUID_LEVEL_SOURCE;
liquid_kind = cf.liquid_alternative_flowing_id;
break;
case LIQUID_FLOWING:
liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
liquid_kind = n0.getContent();
break;
case LIQUID_NONE:
// if this node is 'floodable', it *could* be transformed
// into a liquid, otherwise, continue with the next node.
if (!cf.floodable)
continue;
floodable_node = n0.getContent();
liquid_kind = CONTENT_AIR;
break;
}
/*
Collect information about the environment
*/
NodeNeighbor sources[6]; // surrounding sources
int num_sources = 0;
NodeNeighbor flows[6]; // surrounding flowing liquid nodes
int num_flows = 0;
NodeNeighbor airs[6]; // surrounding air
int num_airs = 0;
NodeNeighbor neutrals[6]; // nodes that are solid or another kind of liquid
int num_neutrals = 0;
bool flowing_down = false;
bool ignored_sources = false;
bool floating_node_above = false;
for (u16 i = 0; i < 6; i++) {
NeighborType nt = NEIGHBOR_SAME_LEVEL;
switch (i) {
case 0:
nt = NEIGHBOR_UPPER;
break;
case 5:
nt = NEIGHBOR_LOWER;
break;
default:
break;
}
v3s16 npos = p0 + liquid_6dirs[i];
NodeNeighbor nb(getNode(npos), nt, npos);
const ContentFeatures &cfnb = m_nodedef->get(nb.n);
if (nt == NEIGHBOR_UPPER && cfnb.floats)
floating_node_above = true;
switch (cfnb.liquid_type) {
case LIQUID_NONE:
if (cfnb.floodable) {
airs[num_airs++] = nb;
// if the current node is a water source the neighbor
// should be enqueded for transformation regardless of whether the
// current node changes or not.
if (nb.t != NEIGHBOR_UPPER && liquid_type != LIQUID_NONE)
m_transforming_liquid.push_back(npos);
// if the current node happens to be a flowing node, it will start to flow down here.
if (nb.t == NEIGHBOR_LOWER)
flowing_down = true;
} else {
neutrals[num_neutrals++] = nb;
if (nb.n.getContent() == CONTENT_IGNORE) {
// If node below is ignore prevent water from
// spreading outwards and otherwise prevent from
// flowing away as ignore node might be the source
if (nb.t == NEIGHBOR_LOWER)
flowing_down = true;
else
ignored_sources = true;
}
}
break;
case LIQUID_SOURCE:
// if this node is not (yet) of a liquid type, choose the first liquid type we encounter
if (liquid_kind == CONTENT_AIR)
liquid_kind = cfnb.liquid_alternative_flowing_id;
if (cfnb.liquid_alternative_flowing_id != liquid_kind) {
neutrals[num_neutrals++] = nb;
} else {
// Do not count bottom source, it will screw things up
if(nt != NEIGHBOR_LOWER)
sources[num_sources++] = nb;
}
break;
case LIQUID_FLOWING:
if (nb.t != NEIGHBOR_SAME_LEVEL ||
(nb.n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK) {
// if this node is not (yet) of a liquid type, choose the first liquid type we encounter
// but exclude falling liquids on the same level, they cannot flow here anyway
if (liquid_kind == CONTENT_AIR)
liquid_kind = cfnb.liquid_alternative_flowing_id;
}
if (cfnb.liquid_alternative_flowing_id != liquid_kind) {
neutrals[num_neutrals++] = nb;
} else {
flows[num_flows++] = nb;
if (nb.t == NEIGHBOR_LOWER)
flowing_down = true;
}
break;
}
}
/*
decide on the type (and possibly level) of the current node
*/
content_t new_node_content;
s8 new_node_level = -1;
s8 max_node_level = -1;
u8 range = m_nodedef->get(liquid_kind).liquid_range;
if (range > LIQUID_LEVEL_MAX + 1)
range = LIQUID_LEVEL_MAX + 1;
if ((num_sources >= 2 && m_nodedef->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
// liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
// or the flowing alternative of the first of the surrounding sources (if it's air), so
// it's perfectly safe to use liquid_kind here to determine the new node content.
new_node_content = m_nodedef->get(liquid_kind).liquid_alternative_source_id;
} else if (num_sources >= 1 && sources[0].t != NEIGHBOR_LOWER) {
// liquid_kind is set properly, see above
max_node_level = new_node_level = LIQUID_LEVEL_MAX;
if (new_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
new_node_content = liquid_kind;
else
new_node_content = floodable_node;
} else if (ignored_sources && liquid_level >= 0) {
// Maybe there are neighbouring sources that aren't loaded yet
// so prevent flowing away.
new_node_level = liquid_level;
new_node_content = liquid_kind;
} else {
// no surrounding sources, so get the maximum level that can flow into this node
for (u16 i = 0; i < num_flows; i++) {
u8 nb_liquid_level = (flows[i].n.param2 & LIQUID_LEVEL_MASK);
switch (flows[i].t) {
case NEIGHBOR_UPPER:
if (nb_liquid_level + WATER_DROP_BOOST > max_node_level) {
max_node_level = LIQUID_LEVEL_MAX;
if (nb_liquid_level + WATER_DROP_BOOST < LIQUID_LEVEL_MAX)
max_node_level = nb_liquid_level + WATER_DROP_BOOST;
} else if (nb_liquid_level > max_node_level) {
max_node_level = nb_liquid_level;
}
break;
case NEIGHBOR_LOWER:
break;
case NEIGHBOR_SAME_LEVEL:
if ((flows[i].n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK &&
nb_liquid_level > 0 && nb_liquid_level - 1 > max_node_level)
max_node_level = nb_liquid_level - 1;
break;
}
}
u8 viscosity = m_nodedef->get(liquid_kind).liquid_viscosity;
if (viscosity > 1 && max_node_level != liquid_level) {
// amount to gain, limited by viscosity
// must be at least 1 in absolute value
s8 level_inc = max_node_level - liquid_level;
if (level_inc < -viscosity || level_inc > viscosity)
new_node_level = liquid_level + level_inc/viscosity;
else if (level_inc < 0)
new_node_level = liquid_level - 1;
else if (level_inc > 0)
new_node_level = liquid_level + 1;
if (new_node_level != max_node_level)
must_reflow.push_back(p0);
} else {
new_node_level = max_node_level;
}
if (max_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
new_node_content = liquid_kind;
else
new_node_content = floodable_node;
}
/*
check if anything has changed. if not, just continue with the next node.
*/
if (new_node_content == n0.getContent() &&
(m_nodedef->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
== flowing_down)))
continue;
/*
check if there is a floating node above that needs to be updated.
*/
if (floating_node_above && new_node_content == CONTENT_AIR)
check_for_falling.push_back(p0);
/*
update the current node
*/
MapNode n00 = n0;
//bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
if (m_nodedef->get(new_node_content).liquid_type == LIQUID_FLOWING) {
// set level to last 3 bits, flowing down bit to 4th bit
n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
} else {
// set the liquid level and flow bits to 0
n0.param2 &= ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
}
// change the node.
n0.setContent(new_node_content);
// on_flood() the node
if (floodable_node != CONTENT_AIR) {
if (env->getScriptIface()->node_on_flood(p0, n00, n0))
continue;
}
// Ignore light (because calling voxalgo::update_lighting_nodes)
ContentLightingFlags f0 = m_nodedef->getLightingFlags(n0);
n0.setLight(LIGHTBANK_DAY, 0, f0);
n0.setLight(LIGHTBANK_NIGHT, 0, f0);
// Find out whether there is a suspect for this action
std::string suspect;
if (m_gamedef->rollback())
suspect = m_gamedef->rollback()->getSuspect(p0, 83, 1);
if (m_gamedef->rollback() && !suspect.empty()) {
// Blame suspect
RollbackScopeActor rollback_scope(m_gamedef->rollback(), suspect, true);
// Get old node for rollback
RollbackNode rollback_oldnode(this, p0, m_gamedef);
// Set node
setNode(p0, n0);
// Report
RollbackNode rollback_newnode(this, p0, m_gamedef);
RollbackAction action;
action.setSetNode(p0, rollback_oldnode, rollback_newnode);
m_gamedef->rollback()->reportAction(action);
} else {
// Set node
setNode(p0, n0);
}
v3s16 blockpos = getNodeBlockPos(p0);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if (block != NULL) {
modified_blocks[blockpos] = block;
changed_nodes.emplace_back(p0, n00);
}
/*
enqueue neighbors for update if neccessary
*/
switch (m_nodedef->get(n0.getContent()).liquid_type) {
case LIQUID_SOURCE:
case LIQUID_FLOWING:
// make sure source flows into all neighboring nodes
for (u16 i = 0; i < num_flows; i++)
if (flows[i].t != NEIGHBOR_UPPER)
m_transforming_liquid.push_back(flows[i].p);
for (u16 i = 0; i < num_airs; i++)
if (airs[i].t != NEIGHBOR_UPPER)
m_transforming_liquid.push_back(airs[i].p);
break;
case LIQUID_NONE:
// this flow has turned to air; neighboring flows might need to do the same
for (u16 i = 0; i < num_flows; i++)
m_transforming_liquid.push_back(flows[i].p);
break;
}
}
//infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
for (const auto &iter : must_reflow)
m_transforming_liquid.push_back(iter);
voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
for (const v3s16 &p : check_for_falling) {
env->getScriptIface()->check_for_falling(p);
}
env->getScriptIface()->on_liquid_transformed(changed_nodes);
/* ----------------------------------------------------------------------
* Manage the queue so that it does not grow indefinately
*/
u16 time_until_purge = g_settings->getU16("liquid_queue_purge_time");
if (time_until_purge == 0)
return; // Feature disabled
time_until_purge *= 1000; // seconds -> milliseconds
u64 curr_time = porting::getTimeMs();
u32 prev_unprocessed = m_unprocessed_count;
m_unprocessed_count = m_transforming_liquid.size();
// if unprocessed block count is decreasing or stable
if (m_unprocessed_count <= prev_unprocessed) {
m_queue_size_timer_started = false;
} else {
if (!m_queue_size_timer_started)
m_inc_trending_up_start_time = curr_time;
m_queue_size_timer_started = true;
}
// Account for curr_time overflowing
if (m_queue_size_timer_started && m_inc_trending_up_start_time > curr_time)
m_queue_size_timer_started = false;
/* If the queue has been growing for more than liquid_queue_purge_time seconds
* and the number of unprocessed blocks is still > liquid_loop_max then we
* cannot keep up; dump the oldest blocks from the queue so that the queue
* has liquid_loop_max items in it
*/
if (m_queue_size_timer_started
&& curr_time - m_inc_trending_up_start_time > time_until_purge
&& m_unprocessed_count > liquid_loop_max) {
size_t dump_qty = m_unprocessed_count - liquid_loop_max;
infostream << "transformLiquids(): DUMPING " << dump_qty
<< " blocks from the queue" << std::endl;
while (dump_qty--)
m_transforming_liquid.pop_front();
m_queue_size_timer_started = false; // optimistically assume we can keep up now
m_unprocessed_count = m_transforming_liquid.size();
}
}
std::vector<v3s16> Map::findNodesWithMetadata(v3s16 p1, v3s16 p2)
{
std::vector<v3s16> positions_with_meta;
sortBoxVerticies(p1, p2);
v3s16 bpmin = getNodeBlockPos(p1);
v3s16 bpmax = getNodeBlockPos(p2);
VoxelArea area(p1, p2);
for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
for (s16 x = bpmin.X; x <= bpmax.X; x++) {
v3s16 blockpos(x, y, z);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if (!block) {
verbosestream << "Map::getNodeMetadata(): Need to emerge "
<< PP(blockpos) << std::endl;
block = emergeBlock(blockpos, false);
}
if (!block) {
infostream << "WARNING: Map::getNodeMetadata(): Block not found"
<< std::endl;
continue;
}
v3s16 p_base = blockpos * MAP_BLOCKSIZE;
std::vector<v3s16> keys = block->m_node_metadata.getAllKeys();
for (size_t i = 0; i != keys.size(); i++) {
v3s16 p(keys[i] + p_base);
if (!area.contains(p))
continue;
positions_with_meta.push_back(p);
}
}
return positions_with_meta;
}
NodeMetadata *Map::getNodeMetadata(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if(!block){
infostream<<"Map::getNodeMetadata(): Need to emerge "
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
if(!block){
warningstream<<"Map::getNodeMetadata(): Block not found"
<<std::endl;
return NULL;
}
NodeMetadata *meta = block->m_node_metadata.get(p_rel);
return meta;
}
bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
{
v3s16 blockpos = getNodeBlockPos(p);
v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if(!block){
infostream<<"Map::setNodeMetadata(): Need to emerge "
<<PP(blockpos)<<std::endl;
block = emergeBlock(blockpos, false);
}
if(!block){
warningstream<<"Map::setNodeMetadata(): Block not found"
<<std::endl;
return false;
}
block->m_node_metadata.set(p_rel, meta);
return true;
}
void Map::removeNodeMetadata(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if(block == NULL)
{
warningstream<<"Map::removeNodeMetadata(): Block not found"
<<std::endl;
return;
}
block->m_node_metadata.remove(p_rel);
}
NodeTimer Map::getNodeTimer(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);