-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.lua
More file actions
1383 lines (1268 loc) · 41.3 KB
/
functions.lua
File metadata and controls
1383 lines (1268 loc) · 41.3 KB
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
local S = technic.getter
local c_vacuum = minetest.get_content_id("vacuum:vacuum")
local c_atmos_thin = minetest.get_content_id("vacuum:atmos_thin")
local c_atmos_thick = minetest.get_content_id("vacuum:atmos_thick")
local c_atmos_asteroid = minetest.get_content_id("asteroid:atmos")
local c_air = minetest.get_content_id("air")
local atmospheres = {}
local atmospheres_ttl_ms = 120 * 1000;
local function get_atmos_vent(vent_pos)
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
return atmospheres[pos_str]
end
local function has_atmos_vent(vent_pos)
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
return atmospheres[pos_str] ~= nil
end
local function rem_atmos_vent(vent_pos)
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
atmospheres[pos_str] = nil
end
local function check_atmos_vent_stale(vent_pos)
if not has_atmos_vent(vent_pos) then
-- not tracked, so return as stale
return true
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local atmos = atmospheres[pos_str]
local t_us = core.get_us_time();
local elapsed_time_in_ms = (t_us - atmos.time_checked) / 1000.0;
if elapsed_time_in_ms > atmospheres_ttl_ms / 4 then
return true
end
return false
end
local function check_atmos_vent_expired(vent_pos)
if not has_atmos_vent(vent_pos) then
-- not tracked, so return as expired
return true
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local atmos = atmospheres[pos_str]
local t_us = core.get_us_time();
local elapsed_time_in_ms = (t_us - atmos.time_created) / 1000.0;
if elapsed_time_in_ms > atmospheres_ttl_ms then
return true
end
return false
end
local function add_atmos_vent(vent_pos, atmos_nodes, cost)
if has_atmos_vent(vent_pos) then
return false
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local t_us = core.get_us_time();
local atmos = {
time_created = t_us,
time_checked = t_us,
nodes = atmos_nodes,
cost = cost
}
atmospheres[pos_str] = atmos
return true
end
local function update_atmos_vent(vent_pos, atmos_nodes, cost)
local time_created;
local t_us = core.get_us_time();
if has_atmos_vent(vent_pos) then
time_created = get_atmos_vent(vent_pos).time_created
else
time_created = t_us
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local atmos = {
time_created = time_created,
time_checked = t_us,
nodes = atmos_nodes,
cost = cost
}
atmospheres[pos_str] = atmos
return true
end
local function expand_atmos_vent(vent_pos, atmos_nodes)
if add_atmos_vent(vent_pos, atmos_nodes) then
return false
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local t_us = core.get_us_time();
local atmos = atmospheres[pos_str]
-- check if has node already
local function has_node(n)
if atmos.nodes[n] then
return true
end
return false
end
-- check and add node if not tracked in mapping
for str_pos, node in pairs(atmos_nodes) do
if not has_node(str_pos) then
atmos.nodes[str_pos] = node
end
end
-- update time
atmos.time_checked = t_us
-- update mapping entry
atmospheres[pos_str] = atmos
return true
end
local function shrink_atmos_vent(vent_pos, distance)
if not has_atmos_vent(vent_pos) then
return false
end
local pos_str = vent_pos.x .. "," .. vent_pos.y .. "," .. vent_pos.z
local t_us = core.get_us_time();
local atmos = atmospheres[pos_str]
-- get distance from orgin to atmos node
local function dist_node(n)
return vector.distance(vent_pos, n)
end
local nodes = {}
-- check distance and remove nodes over limit
for str_pos, node in pairs(atmos.nodes) do
if dist_node(node) <= distance then
nodes[str_pos] = node
end
end
-- update time
atmos.time_checked = t_us
-- update nodes
atmos.nodes = nodes
-- update mapping entry
atmospheres[pos_str] = atmos
return true
end
------------------------------------------------
-- check if enabled
ctg_airs.machine_enabled = function(meta)
return meta:get_int("enabled") == 1
end
function ctg_airs.get_color_range_text(val, val_max)
local col = '#FFFFFF'
if not val or not val_max then
return col
end
local qua = val_max / 8
if val <= qua then
col = "#FF0000"
elseif val <= (qua * 2) then
col = "#FF3D00"
elseif val <= (qua * 3) then
col = "#FF7A00"
elseif val <= (qua * 4) then
col = "#FFB500"
elseif val <= (qua * 5) then
col = "#FFFF00"
elseif val <= (qua * 6) then
col = "#B4FF00"
elseif val <= (qua * 7) then
col = "#00FF00"
elseif val > (qua * 7) then
col = "#00FF50"
end
return col
end
local function list_set(list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
local is_duct_tube = function(pos)
local ducts = {"ctg_airs:air_duct_S", "ctg_airs:air_duct_S2", "ctg_airs:air_duct_A", "ctg_airs:air_duct_A2"}
local node = minetest.get_node(pos)
if list_set(ducts)[node.name] ~= nil then
return true
end
return false
end
local is_duct_vent = function(pos)
local vents = {"ctg_airs:air_duct_vent", "ctg_airs:air_duct_vent_dirty", "ctg_airs:air_duct_vent_lite",
"ctg_airs:air_duct_vent_lite_dirty"}
local node = minetest.get_node(pos)
if list_set(vents)[node.name] ~= nil then
return true
end
return false
end
local is_ducting = function(pos)
local junction = "ctg_airs:air_duct_junc"
local node = minetest.get_node(pos)
if node.name == junction then
return true
end
return is_duct_tube(pos) or is_duct_vent(pos)
end
local is_handler = function(pos)
local handlers = { "ctg_airs:lv_air_handler", "ctg_airs:lv_air_handler_active", "ctg_airs:lv_air_handler_wait",
"ctg_airs:mv_air_handler", "ctg_airs:mv_air_handler_active", "ctg_airs:mv_air_handler_wait",
"ctg_airs:lv_air_handler_gen", "ctg_airs:lv_air_handler_gen_active", "ctg_airs:lv_air_handler_gen_wait",
"ctg_airs:lv_air_fan", "ctg_airs:lv_air_fan_active",
"ctg_airs:mv_air_fan", "ctg_airs:mv_air_fan_active" }
local node = minetest.get_node(pos)
if list_set(handlers)[node.name] ~= nil then
return true
end
return false
end
ctg_airs.is_duct_tube = is_duct_tube
ctg_airs.is_duct_vent = is_duct_vent
ctg_airs.is_ducting = is_ducting
ctg_airs.is_handler = is_handler
local function is_vacuum_node(pos)
local node = minetest.get_node(pos)
if minetest.get_item_group(node.name, "vacuum") == 1 or minetest.get_item_group(node.name, "atmosphere") == 1 then
return true
end
return false
end
local function is_thin_atmos_node(pos)
local node = minetest.get_node(pos)
local atmos = minetest.get_item_group(node.name, "atmosphere")
if minetest.get_item_group(node.name, "vacuum") == 1 or atmos == 1 or atmos == 3 or atmos == 10 or atmos == 11 then
return true
end
return false
end
local function is_atmos_node(pos)
local node = minetest.get_node(pos)
if node.name == "air" then
return true
end
if minetest.get_item_group(node.name, "vacuum") == 1 or minetest.get_item_group(node.name, "atmosphere") > 0 then
return true
end
if node.name == "technic:dummy_light_source" then
return true
end
return false
end
ctg_airs.is_atmos_node = is_atmos_node
local function get_node_cost(pos)
local node = minetest.get_node(pos)
if minetest.get_item_group(node.name, "vacuum") == 1 then
-- vacuum
return 0.02
end
local atmos = minetest.get_item_group(node.name, "atmosphere")
if atmos == 1 or atmos == 10 or atmos == 11 then
-- thin
return 0.025
end
if atmos == 2 or node.name == "air" then
-- thick/air
return 1.0
end
if atmos == 3 then
-- atmos
return 0.25
end
if node.name == "technic:dummy_light_source" then
return 0.5
end
return 0.0
end
local is_player_near = function(pos)
local objs = core.get_objects_inside_radius(pos, 64)
for _, obj in pairs(objs) do
if obj:is_player() then
return true;
end
end
return false;
end
local function str_pos(pos)
return pos.x .. ":" .. pos.y .. ":" .. pos.z
end
local function has_pos(tab, val)
return tab[str_pos(val)] ~= nil
end
--[[local function has_pos(tab, val)
for index, value in ipairs(tab) do
if value.x == val.x and value.y == val.y and value.z == val.z then
return true
end
end
return false
end]] --
local function shuffle(t)
local tbl = {}
for i = 1, #t do
tbl[i] = t[i]
end
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
local function traverse_atmos_local(pos_orig, dir, pos, r, d)
local positions = {{
x = pos.x + 1,
y = pos.y,
z = pos.z
}, {
x = pos.x - 1,
y = pos.y,
z = pos.z
}, {
x = pos.x,
y = pos.y + 1,
z = pos.z
}, {
x = pos.x,
y = pos.y - 1,
z = pos.z
}, {
x = pos.x,
y = pos.y,
z = pos.z + 1
}, {
x = pos.x,
y = pos.y,
z = pos.z - 1
}}
if d < 2 then
table.insert(positions, {
x = pos.x - dir.x * 2,
y = pos.y - dir.y * 2,
z = pos.z - dir.z * 2
});
end
local nodes = {}
local dist = vector.distance({
x = pos.x,
y = pos.y,
z = pos.z
}, {
x = pos_orig.x,
y = pos_orig.y,
z = pos_orig.z
})
nodes[str_pos(pos)] = pos
if (dist > r) then
return nodes;
end
for i, cur_pos in pairs(shuffle(positions)) do
if is_atmos_node(cur_pos) then
nodes[str_pos(cur_pos)] = cur_pos
end
end
return nodes;
end
local function traverse_atmos(t, pos, dir, pos_next, r, depth, max_cost)
if pos_next == nil then
pos_next = pos;
end
local nodes = {};
local costs = get_node_cost(pos_next);
-- add pos to listing
nodes[str_pos(pos_next)] = pos_next
-- depth check
local max_depth = math.min(r, 8) + 8
if depth > max_depth then
return nodes, costs
end
depth = depth + 1
-- elapsed time check
local elapsed_time_in_ms_max = 3.17;
local t0_us = core.get_us_time();
local elapsed_time_in_ms = (t0_us - t) / 1000.0;
if elapsed_time_in_ms >= elapsed_time_in_ms_max then
return nodes, costs
end
-- traverse nodes in local area
local trav_nodes = traverse_atmos_local(pos, dir, pos_next, r, depth);
for _, tpos in pairs(trav_nodes) do
-- add to listing
if not has_pos(nodes, tpos) then
nodes[str_pos(tpos)] = tpos
if math.random(0, math.max(3, depth)) <= 5 then
-- traverse atmos for next pos in chain
local atmoss, cost = traverse_atmos(t, pos, dir, tpos, r, depth, max_cost);
costs = costs + cost
for i, n in pairs(atmoss) do
nodes[str_pos(n)] = n
end
else
costs = costs + get_node_cost(tpos);
end
end
-- cost overage cehck
if costs > max_cost then
break
end
end
-- return nodes and costs
return nodes, costs;
end
local function traverse_atmos_cache(t_us, origin, dir, pos_next, r, depth, max_cost)
local function handle_local_atmos_cache(vent_pos)
if not has_atmos_vent(vent_pos) then
-- traverse nearby atmos
local nodes, cost = traverse_atmos(t_us, vent_pos, dir, pos_next, r, depth, max_cost);
add_atmos_vent(vent_pos, nodes, cost)
return nodes, cost
else
local is_expired = check_atmos_vent_expired(vent_pos)
if not is_expired then
local is_stale = check_atmos_vent_stale(vent_pos)
if not is_stale then
local atmos = get_atmos_vent(vent_pos)
if atmos then
return atmos.nodes, atmos.cost
end
else
shrink_atmos_vent(vent_pos, r * 0.67)
local nodes, cost = traverse_atmos(t_us, vent_pos, dir, pos_next, r * 0.88, depth + 1, max_cost);
expand_atmos_vent(vent_pos, nodes)
local atmos = get_atmos_vent(vent_pos)
if atmos then
return atmos.nodes, atmos.cost + cost
end
end
return {}, 0
else
-- traverse nearby atmos
local nodes, cost = traverse_atmos(t_us, vent_pos, dir, pos_next, r + 1, depth, max_cost);
rem_atmos_vent(vent_pos)
update_atmos_vent(vent_pos, nodes, cost)
return nodes, cost
end
end
end
return handle_local_atmos_cache(origin)
end
local fill_atmos_near = function(pos, dir, r)
local origin = vector.subtract(pos, dir);
local max_cost = r * math.random(10, 20);
local t0_us = core.get_us_time();
-- traverse nearby atmos
local nodes, cost = traverse_atmos_cache(t0_us, origin, dir, nil, r, 0, max_cost);
-- minetest.log("found " .. #nodes);
local count = 0;
-- iterate over nodes found
for i, node_pos in pairs(nodes) do
if (count > 1000) then
break
end
count = count + 1;
if is_thin_atmos_node(node_pos) then
core.set_node(node_pos, {
name = "air"
})
if math.random(0, 7) <= 1 then
ctg_airs.spawn_particle(node_pos, math.random(-0.001, 0.001), math.random(-0.001, 0.001),
math.random(-0.001, 0.001), 0, 0, 0, math.random(1.8, 3), 10, 1)
end
end
end
return count, cost
end
function ctg_airs.spawn_particle(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, lvl, time, amount)
if (not is_player_near(pos)) then
return;
end
local animation = {
type = "vertical_frames",
aspect_w = 16,
aspect_h = 16,
length = (time or 6) + 1
}
local texture = {
name = "ctg_air_vent_vapor_anim.png",
blend = "alpha",
scale = 1,
alpha = 1.0,
alpha_tween = {1, 0.2},
scale_tween = {{
x = 0.5,
y = 0.5
}, {
x = 2.1,
y = 2.0
}}
}
local prt = {
texture = texture,
vel = 2,
time = (time or 6),
size = 3 + (lvl or 1),
glow = math.random(1, 3),
cols = false
}
local v = vector.new()
v.x = 0.0001
v.y = 0.001
v.z = 0.0001
if math.random(0, 10) > 1 then
local rx = dir_x * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
local ry = dir_y * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
local rz = dir_z * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
minetest.add_particlespawner({
amount = amount,
pos = pos,
minpos = {
x = 0,
y = 0,
z = 0
},
maxpos = {
x = dir_x,
y = dir_y,
z = dir_z
},
minvel = {
x = rx * 0.8,
y = ry * 0.8,
z = rz * 0.8
},
maxvel = {
x = rx,
y = ry,
z = rz
},
minacc = {
x = acl_x * 0.8,
y = acl_y * 0.8,
z = acl_z * 0.8
},
maxacc = {
x = acl_x,
y = acl_y + math.random(-0.008, 0),
z = acl_z
},
time = prt.time + 2,
minexptime = prt.time - math.random(0, 2),
maxexptime = prt.time + math.random(0, 1),
minsize = ((math.random(0.77, 0.93)) * 2 + 1.6) * prt.size,
maxsize = ((math.random(0.77, 0.93)) * 2 + 1.6) * prt.size,
collisiondetection = prt.cols,
vertical = false,
texture = texture,
animation = animation,
glow = prt.glow
})
end
end
function ctg_airs.spawn_particle2(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, lvl, time, amount)
if (not is_player_near(pos)) then
return;
end
local animation = {
type = "vertical_frames",
aspect_w = 16,
aspect_h = 16,
length = (time or 6) + 1
}
local texture = {
name = "ctg_air_vent_vapor_anim.png",
blend = "alpha",
scale = 1,
alpha = 1.0,
alpha_tween = {1, 0.2},
scale_tween = {{
x = 0.5,
y = 0.5
}, {
x = 2.1,
y = 2.0
}}
}
local prt = {
texture = texture,
vel = 2,
time = (time or 6),
size = 3 + (lvl or 1),
glow = math.random(2, 4),
cols = true
}
local v = vector.new()
v.x = 0.0001
v.y = 0.001
v.z = 0.0001
if math.random(0, 10) > 1 then
local rx = dir_x * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
local ry = dir_y * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
local rz = dir_z * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
minetest.add_particlespawner({
amount = amount,
pos = pos,
minpos = {
x = math.random(-2, -0.25),
y = math.random(-0.75, -0.01),
z = math.random(-2, -0.25)
},
maxpos = {
x = math.random(0.25, 2),
y = math.random(0.0, 0.75),
z = math.random(0.25, 2)
},
minvel = {
x = rx * 0.8,
y = ry * 0.8,
z = rz * 0.8
},
maxvel = {
x = rx,
y = ry,
z = rz
},
minacc = {
x = acl_x,
y = acl_y,
z = acl_z
},
maxacc = {
x = acl_x,
y = acl_y + math.random(-0.01, 0),
z = acl_z
},
time = prt.time + 2,
minexptime = prt.time - math.random(0, 2),
maxexptime = prt.time + math.random(0, 1),
minsize = ((math.random(0.77, 0.93)) * 2 + 1.6) * prt.size,
maxsize = ((math.random(0.77, 0.93)) * 2 + 1.6) * prt.size,
collisiondetection = prt.cols,
vertical = false,
texture = texture,
animation = animation,
glow = prt.glow
})
end
end
function ctg_airs.process_atmos(pos, max)
local count = 0
for i = 1, 2 do
local range = {
x = i,
y = i,
z = i
}
local pos1 = vector.subtract(pos, range)
local pos2 = vector.add(pos, range)
local manip = minetest.get_voxel_manip()
local e1, e2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({
MinEdge = e1,
MaxEdge = e2
})
local data = manip:get_data()
for y = pos1.y, pos2.y do
for z = pos1.z, pos2.z do
for x = pos1.x, pos2.x do
if (count >= max) then
break
end
if math.random(0, 1) == 0 then
local index = area:index(x, y, z)
if data[index] == c_atmos_thick then
data[index] = c_atmos_thin
count = count + 1
elseif data[index] == c_air then
data[index] = c_atmos_thin
count = count + 1
elseif data[index] == c_atmos_asteroid then
data[index] = c_atmos_thin
count = count + 1
end
end
end
end
end
manip:set_data(data)
manip:write_to_map()
if (count > max) then
break
end
end
return count
end
function ctg_airs.process_leak(pos, power)
if not pos then
return 0, power
end
if power <= -10 then
return 0, power
end
if math.random(0, 100) <= 1 then
return 0, power
end
local node = minetest.get_node(pos)
local param2 = node.param2
local dir_x = 0.0001
local dir_z = 0.0001
local dir_y = 0.0001
if param2 == 1 then -- west
dir_x = 1
elseif param2 == 2 then -- north?
dir_z = -1
elseif param2 == 3 then -- east
dir_x = -1
elseif param2 == 4 then -- south
dir_z = 1
-- elseif param2 == 1 then -- up
-- dir_y = -0.25
-- elseif param2 == 5 then -- down
-- dir_y = 0.25
else
dir_x = math.random(-0.5, 0.5)
dir_y = math.random(-0.75, 0.75)
dir_z = math.random(-0.5, 0.5)
end
local acl_x = 0.28 * (dir_x)
local acl_y = 0.05 * (dir_y)
local acl_z = 0.28 * (dir_z)
ctg_airs.spawn_particle(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, 1.5, 7, 2)
for i = 0, 5 do
minetest.after(i, function()
ctg_airs.spawn_particle(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, 1, 6, 1)
end)
end
local range = {
x = 1,
y = 1,
z = 1
}
local pos1 = vector.subtract(pos, range)
local pos2 = vector.add(pos, range)
local manip = minetest.get_voxel_manip()
local e1, e2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({
MinEdge = e1,
MaxEdge = e2
})
local data = manip:get_data()
local count = 0
for z = pos1.z, pos2.z do
for y = pos1.y, pos2.y do
for x = pos1.x, pos2.x do
local index = area:index(x, y, z)
if data[index] == c_atmos_thin then
data[index] = c_air
count = count + 1
elseif data[index] == c_vacuum then
data[index] = c_atmos_thin
count = count + 1
elseif data[index] == c_atmos_thick then
data[index] = c_air
count = count + 1
end
end
end
end
manip:set_data(data)
manip:write_to_map()
power = power - (25 + count)
if ((count > 0 or math.random(0, 5) == 0)) then
minetest.sound_play("air_vent_short", {
pos = pos,
gain = 0.071,
pitch = math.random(0.5, 1),
max_hear_distance = 18
})
end
-- minetest.log("leaking atmos..")
return count, power
end
local function process_vent2(pos, power, cost, hasPur)
if power <= 0 then
return 0, power
end
local meta = minetest.get_meta(pos)
if power - cost <= 0 then
meta:set_int("active", 0)
return 0, power
end
local t0_us = minetest.get_us_time();
local t2_us = tonumber(meta:get_string("time_run")) or 0
local t_lag = tonumber(meta:get_string("time_lag"))
local elapsed_time_in_seconds = (t0_us - t2_us) / 1000000.0;
if elapsed_time_in_seconds > 90 or elapsed_time_in_seconds <= 0 then
meta:set_string("time_run", tostring(t0_us));
return 0, power
end
if elapsed_time_in_seconds <= 1 then
return 0, power + 1
end
if t_lag and t_lag > 44 and elapsed_time_in_seconds < 37 then
return 0, power - 7
end
if t_lag and t_lag > 35 and elapsed_time_in_seconds < 34 then
return 0, power - 6
end
if t_lag and t_lag > 26 and elapsed_time_in_seconds < 30 then
return 0, power - 5
end
if t_lag and t_lag > 20 and elapsed_time_in_seconds < 25 then
return 0, power - 4
end
if t_lag and t_lag > 12 and elapsed_time_in_seconds < 20 then
return 0, power - 3
end
if t_lag and t_lag > 5 and elapsed_time_in_seconds < 15 then
return 0, power - 2
end
if t_lag and t_lag > 2.12 and elapsed_time_in_seconds < 10 then
return 0, power - 1
end
if t_lag and t_lag > 0.51 and elapsed_time_in_seconds < 5 then
return 0, power - 0
end
local node = minetest.get_node(pos)
local param2 = node.param2
local dir_x = 0.0001
local dir_z = 0.0001
local dir_y = 0.0001
local aclr = 0;
local dir = math.floor(param2 / 4)
local rot = param2 % 4
if dir == 0 or dir == 5 then
if rot == 0 then
dir_z = 1
elseif rot == 1 then
dir_x = 1
elseif rot == 2 then
dir_z = -1
elseif rot == 3 then
dir_x = -1
end
aclr = math.random(-1, 1)
elseif dir == 1 or dir == 3 then
if rot == 0 then
dir_y = -1
elseif rot == 1 then
dir_y = -1
elseif rot == 2 then
dir_y = 1
elseif rot == 3 then
dir_y = 1
end
elseif dir == 2 or dir == 4 then
if rot == 0 then
dir_y = 1
elseif rot == 1 then
dir_y = 1
elseif rot == 2 then
dir_y = 1
elseif rot == 3 then
dir_y = 1
end
end
local acl_x = 0.15 * (dir_x)
local acl_y = 0.10 * (dir_y + aclr)
local acl_z = 0.15 * (dir_z)
local lvl = 0
if (cost > 8) then
lvl = 2
end
minetest.after(0, function()
ctg_airs.spawn_particle(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, lvl, 8, cost + 1)
end)
if vacuum.is_pos_in_spawn(pos) then
if ((cost > 0 and math.random(0, 20) == 0) and power > -5) then
local r = math.random(0, 2)
minetest.after(r, function()
minetest.sound_play("air_vent_short", {
pos = pos,
gain = 0.007,
pitch = 0.6 + math.random(-0.001, 0.001),
max_hear_distance = 20
})
end)
end
return cost, power
end
if hasPur == false and string.match(node.name, "duct_vent") then
if not string.match(node.name, "_dirty") and math.random(0, 1000000) == 0 then
minetest.set_node(pos, {
name = node.name .. "_dirty",
param2 = node.param2
})
if string.match(node.name, "_lite") then
minetest.get_meta(pos):set_string("infotext", S("Dirty Lite Vent"))
else
minetest.get_meta(pos):set_string("infotext", S("Dirty Vent"))
end
end
end
local dir = vector.new(dir_x, dir_y, dir_z)
local dir_pos = vector.subtract(pos, dir);
-- get dirty group
local dirty = minetest.get_item_group(node.name, "vent_dirty") or 0
-- recalc power
power = math.max(0, power - (cost + dirty))
local r = 4
if cost > 9 then
r = 13
elseif cost > 6 then
r = 9
elseif cost > 3 then
r = 6
end
if (hasPur and dirty == 0) then
r = r + 1;
end
-- fill area with air
local count, tcost, travs = fill_atmos_near(dir_pos, dir, r - (dirty * 2));
-- recalc power
power = power - (tcost * 0.01)
if ((count > 0 and math.random(0, 25) == 0) and power > -5) then