forked from mt-mods/vacuum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.lua
398 lines (354 loc) · 9.85 KB
/
common.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
vacuum.air_bottle_image = "vessels_steel_bottle.png^[colorize:#0028FF90^bottle_top.png"
local c_vacuum = minetest.get_content_id("vacuum:vacuum")
local c_atmos = minetest.get_content_id("asteroid:atmos")
local c_aeri = minetest.get_content_id("vacuum:atmos_thick")
local c_aer = minetest.get_content_id("vacuum:atmos_thin")
local c_air = minetest.get_content_id("air")
-- space pos checker
local check_pos_in_space = function(pos)
for _, p in pairs(vacuum.vac_heights) do
if p.enabled then
local min = p.start_height
local max = p.end_height
if pos.y >= min and pos.y < max then
return true
end
end
end
return false
end
vacuum.is_pos_in_space = function(pos)
return check_pos_in_space(pos)
end
-- ground pos checker
local check_pos_on_ground = function(pos)
for _, p in pairs(vacuum.air_heights) do
if p.enabled then
local min = p.start_height
local max = p.end_height
if pos.y >= min and pos.y < max then
return true
end
end
end
return false
end
vacuum.is_pos_on_ground = function(pos)
return check_pos_on_ground(pos)
end
local check_pos_in_spawn = function(pos)
local spawn_spoint = minetest.setting_get_pos("static_spawnpoint") or {
x = 0,
y = 4500,
z = 0
}
local in_x = false
local in_y = false
local in_z = false
if (pos.x <= spawn_spoint.x + 200 and pos.x >= spawn_spoint.x - 200) then
in_x = true
end
if (pos.y <= spawn_spoint.y + 200 and pos.y >= spawn_spoint.y - 200) then
in_y = true
end
if (pos.x <= spawn_spoint.z + 200 and pos.z >= spawn_spoint.z - 200) then
in_z = true
end
return in_x and in_y and in_z
end
vacuum.is_pos_in_spawn = function(pos)
return check_pos_in_spawn(pos)
end
-- returns true if the position is near a powered air pump
function vacuum.near_powered_airpump(pos, range)
if range == nil then
range = vacuum.air_pump_range
end
local pos1 = vector.subtract(pos, {
x = range,
y = range,
z = range
})
local pos2 = vector.add(pos, {
x = range,
y = range,
z = range
})
local nodes = minetest.find_nodes_in_area(pos1, pos2,
{"vacuum:airpump", "vacuum:airpump_wait", "vacuum:airpump_active"})
for _, node in ipairs(nodes) do
local meta = minetest.get_meta(node)
if vacuum.airpump_active(meta) then
return true
end
end
return false
end
function vacuum.near_aeri(pos, c)
local pos1 = vector.subtract(pos, {
x = 1,
y = 1,
z = 1
})
local pos2 = vector.add(pos, {
x = 1,
y = 1,
z = 1
})
local count = 0;
-- local nodes = minetest.find_nodes_in_area(pos1, pos2, {"group:atmosphere"})
local nodes = minetest.find_nodes_in_area(pos1, pos2, {"vacuum:atmos_thick", "air"})
for _, node in ipairs(nodes) do
count = count + 1
end
return count >= c
end
function vacuum.near_atmos(pos, c)
local pos1 = vector.subtract(pos, {
x = 1,
y = 1,
z = 1
})
local pos2 = vector.add(pos, {
x = 1,
y = 1,
z = 1
})
local count = 0;
local nodes = minetest.find_nodes_in_area(pos1, pos2, {"group:atmosphere", "air"})
-- local nodes = minetest.find_nodes_in_area(pos1, pos2, {"vacuum:atmos_thick"})
for _, node in ipairs(nodes) do
count = count + 1
end
return count >= c
end
function vacuum.near_air(pos, dist, c)
local pos1 = vector.subtract(pos, {
x = dist,
y = dist,
z = dist
})
local pos2 = vector.add(pos, {
x = dist,
y = dist,
z = dist
})
local count = 0
local nodes = minetest.find_nodes_in_area(pos1, pos2, {"vacuum:atmos_thick", "air"})
for _, node in ipairs(nodes) do
count = count + 1
end
return count >= c
end
function vacuum.near_vacuum(pos, dist, c)
local pos1 = vector.subtract(pos, {
x = dist,
y = dist,
z = dist
})
local pos2 = vector.add(pos, {
x = dist,
y = dist,
z = dist
})
local count = 0
local nodes = minetest.find_nodes_in_area(pos1, pos2, {"group:vacuum"})
for _, node in ipairs(nodes) do
count = count + 1
end
return count >= c
end
function vacuum.has_in_area(p, c_name, rng, thres)
local pos = {
x = p.x,
y = p.y,
z = p.z
}
local range = {
x = rng,
y = rng,
z = rng
}
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
local total = 0
local c_n = minetest.get_content_id(c_name)
for y = pos1.y, pos2.y do
for z = pos1.z, pos2.z do
for x = pos1.x, pos2.x do
local index = area:index(x, y, z)
-- if data[index] == c_vacuum or data[index] == c_atmos or data[index] == c_air or data[index] == c_aer then
-- if data[index] == c_id then
--[[if minetest.get_node({
x = x,
y = y,
z = z
}).name == c_name then
count = count + 1
end--]]
if data[index] == c_n then
count = count + 1
end
total = total + 1
end
end
end
if total > 0 then
local res = count >= thres
-- minetest.log("Has " .. count .. " for " .. c_name .. " Has: " .. tostring(res))
return res
end
return false
end
function vacuum.has_in_range(p, c_name, rng, thres)
local pos = {
x = p.x,
y = p.y,
z = p.z
}
local range = {
x = rng,
y = rng,
z = rng
}
local pos1 = vector.subtract(pos, range)
local pos2 = vector.add(pos, range)
local nodes = minetest.find_nodes_in_area(pos1, pos2, c_name)
return #nodes >= thres
end
function vacuum.replace_nodes_at(p, rad, c_name, c_replace)
local pos = {
x = p.x,
y = p.y,
z = p.z
}
local range = {
x = rad,
y = rad + 1,
z = rad
}
local pos1 = vector.subtract(pos, range)
local pos2 = vector.add(pos, range)
local nodes = minetest.find_nodes_in_area(pos1, pos2, c_name)
for _, node in ipairs(nodes) do
minetest.set_node(node, {
name = c_replace
})
-- vacuum.replace_nodes_at(noce, rad - 1, c_name, c_replace)
end
end
-- get nodes in area
function vacuum.get_area_nodes(p, dist)
local pos = {
x = p.x,
y = p.y,
z = p.z
}
local range = {
x = dist,
y = dist,
z = dist
}
local pos1 = vector.subtract(pos, range)
local pos2 = vector.add(pos, range)
-- get voxel area manip
local manip = minetest.get_voxel_manip()
local e1, e2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({
MinEdge = e1,
MaxEdge = e2
})
return pos1, pos2, area, manip:get_data();
end
function vacuum.has_in_area_data(pos1, pos2, area, data, c_name, thres)
local count = 0
local total = 0
local c_n = minetest.get_content_id(c_name)
for y = pos1.y, pos2.y do
for z = pos1.z, pos2.z do
for x = pos1.x, pos2.x do
local index = area:index(x, y, z)
if data[index] == c_n then
count = count + 1
end
total = total + 1
end
end
end
if total > 0 then
local result = count >= thres
-- minetest.log("Has " .. count .. " for " .. c_name .. " Has: " .. tostring(res))
return result
end
return false
end
function vacuum.spawn_particle(pos, dir_x, dir_y, dir_z, acl_x, acl_y, acl_z, lvl, time)
local texture_name = "vacuum_air_particle_1.png"
if (math.random() > 0.5) then
texture_name = "vacuum_air_particle_1.png^[transformR90]"
end
if (math.random() > 0.5) then
texture_name = texture_name .. "^[colorize:#4aebf7:10"
end
local animation = nil
local texture = {
name = texture_name,
blend = "alpha",
scale = 1,
alpha = 1.0,
alpha_tween = {1, 0},
scale_tween = {{
x = 0.75,
y = 0.75
}, {
x = 2.2,
y = 2.0
}}
}
local prt = {
texture = texture,
animation = animation,
vel = 1,
time = time or 6,
size = 2 + (lvl or 1),
glow = 3,
cols = true
}
local v = vector.new()
v.x = 0.0001
v.y = 0.001
v.z = 0.0001
if math.random(1, 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.6 * 100) / 100
local rz = dir_z * prt.vel * -math.random(0.3 * 100, 0.7 * 100) / 100
minetest.add_particle({
pos = pos,
velocity = vector.add(v, {
x = rx,
y = ry,
z = rz
}),
acceleration = {
x = acl_x,
y = acl_y + math.random(-0.008, 0.0001),
z = acl_z
},
expirationtime = ((math.random() / 5) + 0.5) * prt.time,
size = ((math.random(0.65, 0.90)) * 2 + 0.1) * prt.size,
collisiondetection = prt.cols,
vertical = false,
texture = prt.texture,
animation = prt.animation,
glow = prt.glow
})
end
end