forked from minetest-mods/dynamic_liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
619 lines (557 loc) · 22.3 KB
/
init.lua
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
dynamic_liquid = {} -- global table to expose liquid_abm for other mods' usage
dynamic_liquid.registered_liquids = {} -- used by the flow-through node abm
dynamic_liquid.registered_liquid_neighbors = {}
local water_level = tonumber(minetest.get_mapgen_setting("water_level"))
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname)
dofile(modpath.."/cooling_lava.lua")
-- deactivate water flow
--minetest.registered_nodes["default:water_source"].liquid_range = 0
--local override_liquidrange = {liquid_range = 0}
--minetest.override_item("default:water_source", override_liquidrange)
-- By making this giant table of all possible permutations of horizontal direction we can avoid
-- lots of redundant calculations.
local all_direction_permutations = {
{{x=0,z=1},{x=0,z=-1},{x=1,z=0},{x=-1,z=0}},
{{x=0,z=1},{x=0,z=-1},{x=-1,z=0},{x=1,z=0}},
{{x=0,z=1},{x=1,z=0},{x=0,z=-1},{x=-1,z=0}},
{{x=0,z=1},{x=1,z=0},{x=-1,z=0},{x=0,z=-1}},
{{x=0,z=1},{x=-1,z=0},{x=0,z=-1},{x=1,z=0}},
{{x=0,z=1},{x=-1,z=0},{x=1,z=0},{x=0,z=-1}},
{{x=0,z=-1},{x=0,z=1},{x=-1,z=0},{x=1,z=0}},
{{x=0,z=-1},{x=0,z=1},{x=1,z=0},{x=-1,z=0}},
{{x=0,z=-1},{x=1,z=0},{x=-1,z=0},{x=0,z=1}},
{{x=0,z=-1},{x=1,z=0},{x=0,z=1},{x=-1,z=0}},
{{x=0,z=-1},{x=-1,z=0},{x=1,z=0},{x=0,z=1}},
{{x=0,z=-1},{x=-1,z=0},{x=0,z=1},{x=1,z=0}},
{{x=1,z=0},{x=0,z=1},{x=0,z=-1},{x=-1,z=0}},
{{x=1,z=0},{x=0,z=1},{x=-1,z=0},{x=0,z=-1}},
{{x=1,z=0},{x=0,z=-1},{x=0,z=1},{x=-1,z=0}},
{{x=1,z=0},{x=0,z=-1},{x=-1,z=0},{x=0,z=1}},
{{x=1,z=0},{x=-1,z=0},{x=0,z=1},{x=0,z=-1}},
{{x=1,z=0},{x=-1,z=0},{x=0,z=-1},{x=0,z=1}},
{{x=-1,z=0},{x=0,z=1},{x=1,z=0},{x=0,z=-1}},
{{x=-1,z=0},{x=0,z=1},{x=0,z=-1},{x=1,z=0}},
{{x=-1,z=0},{x=0,z=-1},{x=1,z=0},{x=0,z=1}},
{{x=-1,z=0},{x=0,z=-1},{x=0,z=1},{x=1,z=0}},
{{x=-1,z=0},{x=1,z=0},{x=0,z=-1},{x=0,z=1}},
{{x=-1,z=0},{x=1,z=0},{x=0,z=1},{x=0,z=-1}},
}
local get_node = minetest.get_node
local swap_node = minetest.swap_node --preserve metadatas
local set_node = minetest.set_node --used to reset metadatas
-- Dynamic liquids
-----------------------------------------------------------------------------------------------------------------------
dynamic_liquid.liquid_abm = function(liquid, flowing_liquid, chance)
minetest.register_abm({
label = "dynamic_liquid " .. liquid,
nodenames = {liquid},
neighbors = {air},
interval = 0.5,
chance = 0.5, -- chance or 1,
catch_up = true,
action = function(pos,node) -- Do everything possible to optimize this method
-- Gravity first! If the node is above an air block, it must fall.
local i = 1
local check_pos = {x=pos.x, y=pos.y-i, z=pos.z}
local check_node = get_node(check_pos)
local check_node_name = check_node.name
if check_node_name == flowing_liquid or check_node_name == "air" then
-- local j = 1
-- while check_node_name == liquid and j <= 16 then do
-- pos.x -= dirs.x
-- pos.z -= dirs.z
-- j += 1
-- end
minetest.after(0.2, function()
-- minetest.chat_send_all("V" .. tostring(math.random(1, 64)))
-- minetest.chat_send_all(node .. " at " .. pos)
--math.random(1, 64)*0.01
local check_node = get_node(check_pos) -- check the node type down, expecting air again
local check_node_name = check_node.name
local node_at_pos = get_node(pos) -- are we still water?
-- minetest.chat_send_all("are we still water? We are " .. tostring(node_at_pos.name))
if node_at_pos.name == node.name then
-- minetest.chat_send_all("are we still water? Yes")
if check_node_name == flowing_liquid or check_node_name == "air" then -- recheck if space is still free to avoid replacing another node
-- minetest.chat_send_all("replace " .. tostring(check_node.name) .. " with " .. tostring(node.name))
set_node(pos, check_node) --use set_node to erase metadatas at the previous position
swap_node(check_pos, node)
end
end
end)
-- if node fall, no momentum needed above it, so delete it
-- not usefull anymore because set_node already erase metadatas
-- local meta = minetest.get_meta(pos)
-- local momentum = minetest.deserialize(meta:get_string("momentum"))
-- if meta:contains("momentum") then do
-- meta:set_string("momentum", "")
-- end
return
end
-- Momentum next! If it started moving in a direction, then continue until it reaches an obstacle.
local meta = minetest.get_meta(pos)
local momentum = minetest.deserialize(meta:get_string("momentum"))
if meta:contains("momentum") then -- if metadata exist
-- minetest.chat_send_all("node has momentum!")
local check_pos = vector.add(pos, momentum)
check_node = get_node(check_pos)
local check_node_name = check_node.name
if check_node_name == flowing_liquid or check_node_name == "air" then -- if no obstacles
--detect if water is going "against a previous flow"
local meta = minetest.get_meta(check_pos)
local momentumfront = minetest.deserialize(meta:get_string("momentum"))
--if not, just continue according to momentum
if not meta:contains("momentum") or vector.equals(vector.add(momentum, momentumfront), vector.direction(momentum, momentum)) ~= true then
minetest.after(0.2, function()
local check_pos = vector.add(pos, momentum)
local check_node = get_node(check_pos) -- check the node type down, expecting air again
local check_node_name = check_node.name
local node_at_pos = get_node(pos) -- are we still water?
minetest.chat_send_all("are we still water? We are " .. tostring(node_at_pos.name))
if node_at_pos.name == node.name then
-- minetest.chat_send_all("are we still water? Yes")
if check_node_name == flowing_liquid or check_node_name == "air" then -- recheck if space is still free to avoid replacing another node
minetest.chat_send_all("replace " .. tostring(check_node.name) .. " with " .. tostring(node.name))
swap_node(pos, check_node) --use swap_node to preserve metadatas at the previous position
swap_node(check_pos, node)
end
end
end)
local meta = minetest.get_meta(check_pos)
meta:set_string("momentum", minetest.serialize(vector.subtract(check_pos, pos)))
return
-- if it does, just start moving randomly again
-- improvement idea : try to move left or right first, or even diagonal front left or right
-- idea 2 : if stuck in a trench, move according to momentum anyway
else
-- minetest.chat_send_all("node is going against the flow, abort!")
end
-- minetest.chat_send_all("end 1")
end
-- minetest.chat_send_all("node meets wall :(")
end
-- minetest.chat_send_all("after end")
-- don't turn back, explore new areas first!
-- You are a water node, not an ice node. Keep moving even if it doesn't make sense!
local perm = all_direction_permutations[math.random(24)]
local dirs -- declare outside of loop so it won't keep entering/exiting scope
for i=1,4 do
dirs = perm[i]
-- reuse check_pos to avoid allocating a new table
local check_pos = {x=pos.x, y=pos.y, z=pos.z}
check_pos.x = pos.x + dirs.x
check_pos.y = pos.y
check_pos.z = pos.z + dirs.z
check_node = get_node(check_pos)
check_node_name = check_node.name
if check_node_name == flowing_liquid or check_node_name == "air" then
minetest.after(0.2, function()
local check_pos = {x=pos.x, y=pos.y, z=pos.z}
check_pos.x = pos.x + dirs.x
check_pos.y = pos.y
check_pos.z = pos.z + dirs.z
local check_node = get_node(check_pos) -- check the node type down, expecting air again
local check_node_name = check_node.name
local node_at_pos = get_node(pos) -- are we still water?
if minetest.get_node(check_pos).name == "air" then
swap_node(pos, check_node)
swap_node(check_pos, node)
end
end)
local meta = minetest.get_meta(check_pos) -- meta = metadata at te checked position
local momentum = vector.subtract(check_pos, pos)
meta:set_string("momentum", minetest.serialize(momentum))
-- Follow me! Water nodes that start moving for the first time also set the metadata to the node behind them (according to its momentum).
check_pos.x = pos.x + dirs.x
check_pos.y = pos.y
check_pos.z = pos.z + dirs.z
local meta = minetest.get_meta(check_pos)
if check_node_name == liquid then
meta:set_string("momentum", minetest.serialize(momentum))
end
return
end
end
end
})
dynamic_liquid.registered_liquids[liquid] = flowing_liquid
table.insert(dynamic_liquid.registered_liquid_neighbors, liquid)
end
if not minetest.get_modpath("default") then
return
end
local water = minetest.settings:get_bool("dynamic_liquid_water", true)
local river_water = minetest.settings:get_bool("dynamic_liquid_river_water", false)
local lava = minetest.settings:get_bool("dynamic_liquid_lava", true)
local water_probability = tonumber(minetest.settings:get("dynamic_liquid_water_flow_propability"))
if water_probability == nil then
water_probability = 1
end
local river_water_probability = tonumber(minetest.settings:get("dynamic_liquid_river_water_flow_propability"))
if river_water_probability == nil then
river_water_probability = 1
end
local lava_probability = tonumber(minetest.settings:get("dynamic_liquid_lava_flow_propability"))
if lava_probability == nil then
lava_probability = 5
end
local springs = minetest.settings:get_bool("dynamic_liquid_springs", true)
if water then
-- override water_source and water_flowing with liquid_renewable set to false
local override_def = {liquid_range = 0, liquid_renewable = false}
minetest.override_item("default:water_source", override_def)
minetest.override_item("default:water_flowing", override_def)
end
if lava then
dynamic_liquid.liquid_abm("default:lava_source", "default:lava_flowing", lava_probability)
end
if water then
dynamic_liquid.liquid_abm("default:water_source", "default:water_flowing", water_probability)
end
if river_water then
dynamic_liquid.liquid_abm("default:river_water_source", "default:river_water_flowing", river_water_probability)
end
-- Flow-through nodes
-----------------------------------------------------------------------------------------------------------------------
local flow_through = minetest.settings:get_bool("dynamic_liquid_flow_through", true)
if flow_through then
local flow_through_directions = {
{{x=1,z=0},{x=0,z=1}},
{{x=0,z=1},{x=1,z=0}},
}
minetest.register_abm({
label = "dynamic_liquid flow-through",
nodenames = {"group:flow_through", "group:leaves", "group:sapling", "group:grass", "group:dry_grass", "group:flora", "groups:rail", "groups:flower"},
neighbors = dynamic_liquid.registered_liquid_neighbors,
interval = 1,
chance = 2, -- since liquid is teleported two nodes by this abm, halve the chance
catch_up = false,
action = function(pos)
local source_pos = {x=pos.x, y=pos.y+1, z=pos.z}
local dest_pos = {x=pos.x, y=pos.y-1, z=pos.z}
local source_node = get_node(source_pos)
local dest_node
local source_flowing_node = dynamic_liquid.registered_liquids[source_node.name]
local dest_flowing_node
if source_flowing_node ~= nil then
dest_node = minetest.get_node(dest_pos)
if dest_node.name == source_flowing_node or dest_node.name == "air" then
swap_node(dest_pos, source_node)
swap_node(source_pos, dest_node)
return
end
end
local perm = flow_through_directions[math.random(2)]
local dirs -- declare outside of loop so it won't keep entering/exiting scope
for i=1,2 do
dirs = perm[i]
-- reuse to avoid allocating a new table
source_pos.x = pos.x + dirs.x
source_pos.y = pos.y
source_pos.z = pos.z + dirs.z
dest_pos.x = pos.x - dirs.x
dest_pos.y = pos.y
dest_pos.z = pos.z - dirs.z
source_node = get_node(source_pos)
dest_node = get_node(dest_pos)
source_flowing_node = dynamic_liquid.registered_liquids[source_node.name]
dest_flowing_node = dynamic_liquid.registered_liquids[dest_node.name]
if (source_flowing_node ~= nil and (dest_node.name == source_flowing_node or dest_node.name == "air")) or
(dest_flowing_node ~= nil and (source_node.name == dest_flowing_node or source_node.name == "air"))
then
swap_node(source_pos, dest_node)
swap_node(dest_pos, source_node)
return
end
end
end,
})
local add_flow_through = function(node_name)
local node_def = minetest.registered_nodes[node_name]
if node_def == nil then
minetest.log("warning", "dynamic_liquid attempted to call add_flow_through on the node name "
.. node_name .. ", which was not found in minetest.registered_nodes.")
return
end
local new_groups = node_def.groups
new_groups.flow_through = 1
minetest.override_item(node_name,{groups = new_groups})
end
if minetest.get_modpath("default") then
for _, name in pairs({
"default:apple",
"default:papyrus",
"default:dry_shrub",
"default:bush_stem",
"default:acacia_bush_stem",
"default:sign_wall_wood",
"default:sign_wall_steel",
"default:ladder_wood",
"default:ladder_steel",
"default:fence_wood",
"default:fence_acacia_wood",
"default:fence_junglewood",
"default:fence_pine_wood",
"default:fence_aspen_wood",
}) do
add_flow_through(name)
end
end
if minetest.get_modpath("xpanes") then
add_flow_through("xpanes:bar")
add_flow_through("xpanes:bar_flat")
end
if minetest.get_modpath("carts") then
add_flow_through("carts:rail")
add_flow_through("carts:powerrail")
add_flow_through("carts:brakerail")
end
end
-- Springs
-----------------------------------------------------------------------------------------------------------------------
local function deep_copy(table_in)
local table_out = {}
for index, value in pairs(table_in) do
if type(value) == "table" then
table_out[index] = deep_copy(value)
else
table_out[index] = value
end
end
return table_out
end
local duplicate_def = function (name)
local old_def = minetest.registered_nodes[name]
return deep_copy(old_def)
end
-- register damp clay whether we're going to set the ABM or not, if the user disables this feature we don't want existing
-- spring clay to turn into unknown nodes.
local clay_def = duplicate_def("default:clay")
clay_def.description = S("Damp Clay")
if not springs then
clay_def.groups.not_in_creative_inventory = 1 -- take it out of creative inventory though
end
minetest.register_node("dynamic_liquid:clay", clay_def)
local data = {}
if springs then
local c_clay = minetest.get_content_id("default:clay")
local c_spring_clay = minetest.get_content_id("dynamic_liquid:clay")
-- Turn mapgen clay into spring clay
minetest.register_on_generated(function(minp, maxp, seed)
if minp.y >= water_level or maxp.y <= -15 then
return
end
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
vm:get_data(data)
for voxelpos, voxeldata in pairs(data) do
if voxeldata == c_clay then
data[voxelpos] = c_spring_clay
end
end
vm:set_data(data)
vm:write_to_map()
end)
minetest.register_abm({
label = "dynamic_liquid damp clay spring",
nodenames = {"dynamic_liquid:clay"},
neighbors = {"air", "default:water_source", "default:water_flowing"},
interval = 1,
chance = 1,
catch_up = false,
action = function(pos,node)
local check_node
local check_node_name
while pos.y < water_level do
pos.y = pos.y + 1
check_node = get_node(pos)
check_node_name = check_node.name
if check_node_name == "air" or check_node_name == "default:water_flowing" then
swap_node(pos, {name="default:water_source"})
elseif check_node_name ~= "default:water_source" then
--Something's been put on top of this clay, don't send water through it
break
end
end
end
})
local spring_sounds = nil
if default.node_sound_gravel_defaults ~= nil then
spring_sounds = default.node_sound_gravel_defaults()
elseif default.node_sound_sand_defaults ~= nil then
spring_sounds = default.node_sound_dirt_defaults()
end
-- This is a creative-mode only node that produces a modest amount of water continuously no matter where it is.
-- Allow this one to turn into "unknown node" when this feature is disabled, since players had to explicitly place it.
minetest.register_node("dynamic_liquid:spring", {
description = S("Spring"),
_doc_items_longdesc = S("A natural spring that generates an endless stream of water source blocks"),
_doc_items_usagehelp = S("Generates one source block of water directly on top of itself once per second, provided the space is clear. If this natural spring is dug out the flow stops and it is turned into ordinary cobble."),
drops = "default:gravel",
tiles = {"default_cobble.png^[combine:16x80:0,-48=crack_anylength.png",
"default_cobble.png","default_cobble.png","default_cobble.png","default_cobble.png","default_cobble.png",
},
is_ground_content = false,
groups = {cracky = 3, stone = 2},
sounds = spring_sounds,
})
minetest.register_abm({
label = "dynamic_liquid creative spring",
nodenames = {"dynamic_liquid:spring"},
neighbors = {"air", "default:water_flowing"},
interval = 1,
chance = 1,
catch_up = false,
action = function(pos,node)
pos.y = pos.y + 1
local check_node = get_node(pos)
local check_node_name = check_node.name
if check_node_name == "air" or check_node_name == "default:water_flowing" then
swap_node(pos, {name="default:water_source"})
end
end
})
end
local mapgen_prefill = minetest.settings:get_bool("dynamic_liquid_mapgen_prefill", true)
local waternodes
if mapgen_prefill then
local c_water = minetest.get_content_id("default:water_source")
local c_air = minetest.get_content_id("air")
waternodes = {}
local fill_to = function (vi, data, area)
if area:containsi(vi) and area:position(vi).y <= water_level then
if data[vi] == c_air then
data[vi] = c_water
table.insert(waternodes, vi)
end
end
end
-- local count = 0
local drop_liquid = function(vi, data, area, min_y)
if data[vi] ~= c_water then
-- we only care about water.
return
end
local start = vi -- remember the water node we started from
local ystride = area.ystride
vi = vi - ystride
if data[vi] ~= c_air then
-- if there's no air below this water node, give up immediately.
return
end
vi = vi - ystride -- There's air below the water, so move down one.
while data[vi] == c_air and area:position(vi).y > min_y do
-- the min_y check is here to ensure that we don't put water into the mapgen
-- border zone below our current map chunk where it might get erased by future mapgen activity.
-- if there's more air, keep going.
vi = vi - ystride
end
vi = vi + ystride -- Move back up one. vi is now pointing at the last air node above the first non-air node.
data[vi] = c_water
data[start] = c_air
-- count = count + 1
-- if count % 100 == 0 then
-- minetest.chat_send_all("dropped water " .. (start-vi)/ystride .. " at " .. minetest.pos_to_string(area:position(vi)))
-- end
end
minetest.register_on_generated(function(minp, maxp, seed)
if minp.y > water_level then
-- we're in the sky.
return
end
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
vm:get_data(data)
local maxp_y = maxp.y
local minp_y = minp.y
if maxp_y > -70 then
local top = vector.new(maxp.x, math.min(maxp_y, water_level), maxp.z) -- prevents flood fill from affecting any water above sea level
for vi in area:iterp(minp, top) do
if data[vi] == c_water then
table.insert(waternodes, vi)
end
end
while table.getn(waternodes) > 0 do
local vi = table.remove(waternodes)
local below = vi - area.ystride
local left = vi - area.zstride
local right = vi + area.zstride
local front = vi - 1
local back = vi + 1
fill_to(below, data, area)
fill_to(left, data, area)
fill_to(right, data, area)
fill_to(front, data, area)
fill_to(back, data, area)
end
else
-- Caves sometimes generate with liquid nodes hovering in mid air.
-- This immediately drops them straight down as far as they can go, reducing the ABM thrashing.
-- We only iterate down to minp.y+1 because anything at minp.y will never be dropped farther anyway.
for vi in area:iter(minp.x, minp_y+1, minp.z, maxp.x, maxp_y, maxp.z) do
-- fortunately, area:iter iterates through y columns going upward. Just what we need!
-- We could possibly be a bit more efficient by remembering how far we dropped then
-- last liquid node in a column and moving stuff down that far,
-- but for now let's keep it simple.
drop_liquid(vi, data, area, minp_y)
end
end
vm:set_data(data)
vm:write_to_map()
vm:update_liquids()
end)
end
local displace_liquid = minetest.settings:get_bool("dynamic_liquid_displace_liquid", true)
if displace_liquid then
local cardinal_dirs = {
{x= 0, y=0, z= 1},
{x= 1, y=0, z= 0},
{x= 0, y=0, z=-1},
{x=-1, y=0, z= 0},
{x= 0, y=-1, z= 0},
{x= 0, y=1, z= 0},
}
-- breadth-first search passing through liquid searching for air or flowing liquid.
local flood_search_outlet = function(start_pos, source, flowing)
local start_node = minetest.get_node(start_pos)
local start_node_name = start_node.name
if start_node_name == "air" or start_node_name == flowing then
return start_pos
end
local visited = {}
visited[minetest.hash_node_position(start_pos)] = true
local queue = {start_pos}
local queue_pointer = 1
while #queue >= queue_pointer do
local current_pos = queue[queue_pointer]
queue_pointer = queue_pointer + 1
for _, cardinal_dir in ipairs(cardinal_dirs) do
local new_pos = vector.add(current_pos, cardinal_dir)
local new_hash = minetest.hash_node_position(new_pos)
if visited[new_hash] == nil then
local new_node = minetest.get_node(new_pos)
local new_node_name = new_node.name
if new_node_name == "air" or new_node_name == flowing then
return new_pos
end
visited[new_hash] = true
if new_node_name == source then
table.insert(queue, new_pos)
end
end
end
end
return nil
end
-- Conserve liquids, when placing nodes in liquids try to find a place to displace the liquid to.
minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack, pointed_thing)
local flowing = dynamic_liquid.registered_liquids[oldnode.name]
if flowing ~= nil then
local dest = flood_search_outlet(pos, oldnode.name, flowing)
if dest ~= nil then
minetest.swap_node(dest, oldnode)
end
end
end)
end