forked from mt-mods/vacuum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairpump.lua
304 lines (248 loc) · 7.55 KB
/
airpump.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
local has_pipeworks = minetest.get_modpath("pipeworks")
local tube_entry = ""
if has_pipeworks then
tube_entry = "^pipeworks_tube_connection_wooden.png"
end
local has_full_air_bottle = function(inv)
return inv:contains_item("main", {name="vacuum:air_bottle", count=1})
end
local has_empty_air_bottle = function(inv)
return inv:contains_item("main", {name="vessels:steel_bottle", count=1})
end
local do_empty_bottle = function(inv)
if not has_full_air_bottle(inv) then
return false
end
local new_stack = ItemStack("vessels:steel_bottle")
inv:remove_item("main", {name="vacuum:air_bottle", count=1})
if inv:room_for_item("main", new_stack) then
inv:add_item("main", new_stack)
return true
end
return false
end
local do_fill_bottle = function(inv)
if not has_empty_air_bottle(inv) then
return false
end
local new_stack = ItemStack("vacuum:air_bottle")
inv:remove_item("main", {name="vessels:steel_bottle", count=1})
if inv:room_for_item("main", new_stack) then
inv:add_item("main", new_stack)
return true
end
return false
end
local do_repair_spacesuit = function(inv)
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
local item_def = minetest.registered_items[stack:get_name()]
if item_def and item_def.wear_represents == "spacesuit_wear" and stack:get_wear() > 0 then
stack:set_wear(0)
inv:set_stack("main", i, stack)
return true
end
end
return false
end
-- just enabled
vacuum.airpump_enabled = function(meta)
return meta:get_int("enabled") == 1
end
-- enabled and actively pumping
vacuum.airpump_active = function(meta)
local inv = meta:get_inventory()
return vacuum.airpump_enabled(meta) and has_full_air_bottle(inv)
end
local update_infotext = function(meta)
local str = "Airpump: "
if vacuum.airpump_enabled(meta) then
str = str .. " (Enabled)"
else
str = str .. " (Disabled)"
end
meta:set_string("infotext", str)
end
-- update airpump formspec
local update_formspec = function(meta)
local btnName = "State: "
if meta:get_int("enabled") == 1 then
btnName = btnName .. "<Enabled>"
else
btnName = btnName .. "<Disabled>"
end
meta:set_string("formspec", "size[8,7.2;]" ..
"image[3,0;1,1;" .. vacuum.air_bottle_image .. "]" ..
"image[4,0;1,1;vessels_steel_bottle.png]" ..
"button[0,1;8,1;toggle;" .. btnName .. "]" ..
"list[context;main;0,2;8,1;]" ..
"list[current_player;main;0,3.2;8,4;]" ..
"listring[]" ..
"")
update_infotext(meta)
end
minetest.register_node("vacuum:airpump", {
description = "Air pump",--tube_entry
tiles = { -- top, bottom
"vacuum_airpump_top.png",
"vacuum_airpump_side.png" .. tube_entry,
"vacuum_airpump_side.png" .. tube_entry,
"vacuum_airpump_side.png" .. tube_entry,
"vacuum_airpump_side.png" .. tube_entry,
"vacuum_airpump_front.png"
},
paramtype = "light",
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {cracky=3, oddly_breakable_by_hand=3, tubedevice=1, tubedevice_receiver=1},
sounds = default.node_sound_glass_defaults(),
mesecons = {effector = {
action_on = function (pos, node)
local meta = minetest.get_meta(pos)
meta:set_int("enabled", 1)
update_infotext(meta)
end,
action_off = function (pos, node)
local meta = minetest.get_meta(pos)
meta:set_int("enabled", 0)
update_infotext(meta)
end
}},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_int("enabled", 0)
local inv = meta:get_inventory()
inv:set_size("main", 8)
update_formspec(meta)
end,
can_dig = function(pos,player)
if player and player:is_player() and minetest.is_protected(pos, player:get_player_name()) then
-- protected
return false
end
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if player and player:is_player() and minetest.is_protected(pos, player:get_player_name()) then
-- protected
return 0
end
return stack:get_count()
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if player and player:is_player() and minetest.is_protected(pos, player:get_player_name()) then
-- protected
return 0
end
return stack:get_count()
end,
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos);
if minetest.is_protected(pos, sender:get_player_name()) then
-- not allowed
return
end
if fields.toggle then
if meta:get_int("enabled") == 1 then
meta:set_int("enabled", 0)
else
meta:set_int("enabled", 1)
end
end
update_formspec(meta)
end,
tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("main", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
stack = stack:peek_item(1)
return inv:room_for_item("main", stack)
end,
input_inventory = "main",
connect_sides = {left = 1, right = 1, back = 1, bottom = 1, top = 1}
}
})
minetest.register_abm({
label = "airpump",
nodenames = {"vacuum:airpump"},
interval = 5,
chance = 1,
action = function(pos)
local meta = minetest.get_meta(pos)
if vacuum.airpump_enabled(meta) then
-- The spacesuit mod must be loaded after this mod, so we can't check at the start.
local has_spacesuit = minetest.get_modpath("spacesuit")
local used
if vacuum.is_pos_in_space(pos) then
used = do_empty_bottle(meta:get_inventory())
if used and has_spacesuit then
do_repair_spacesuit(meta:get_inventory())
end
else
if has_spacesuit then
used = do_repair_spacesuit(meta:get_inventory())
end
if not used then
used = do_fill_bottle(meta:get_inventory())
end
end
if used then
minetest.sound_play("vacuum_hiss", {pos = pos, gain = 0.5})
minetest.add_particlespawner(
12, --amount
4, --time
{x=pos.x-0.95, y=pos.y-0.95, z=pos.z-0.95},
{x=pos.x+0.95, y=pos.y+0.95, z=pos.z+0.95},
{x=-1.2, y=-1.2, z=-1.2},
{x=1.2, y=1.2, z=1.2},
{x=0,y=0,z=0},
{x=0,y=0,z=0},
0.5,
1,
1,
2,
false,
"bubble.png"
)
end
update_infotext(meta)
end
end
})
-- initial airpump step
minetest.register_abm({
label = "airpump seed",
nodenames = {"vacuum:airpump"},
neighbors = {"vacuum:vacuum"},
interval = 1,
chance = 1,
action = function(pos)
local meta = minetest.get_meta(pos)
if vacuum.airpump_active(meta) then
-- seed initial air
local node = minetest.find_node_near(pos, 1, {"vacuum:vacuum"})
if node ~= nil then
minetest.set_node(node, {name = "air"})
end
end
end
})
minetest.register_craft({
output = "vacuum:airpump",
recipe = {
{"default:steel_ingot", "default:mese_block", "default:steel_ingot"},
{"default:diamond", "default:glass", "default:steel_ingot"},
{"default:steel_ingot", "default:steelblock", "default:steel_ingot"},
},
})