-
Notifications
You must be signed in to change notification settings - Fork 0
/
barrel.lua
348 lines (272 loc) · 8.41 KB
/
barrel.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
minetest.register_craftitem("bitumen:oil_drum_filled", {
description = "Filled Oil Drum",
inventory_image = "bitumen_drum_side.png",
stack_max = 1,
on_place = function(itemstack, placer, pointed_thing)
local imeta = itemstack:get_meta()
local fluid = imeta:get_string("fluid")
local fill = imeta:get_int("fill")
local max_fill = imeta:get_int("maxfill")
local pos = minetest.get_pointed_thing_position(pointed_thing, true)
-- todo: check if buildable/ protected/ etc
minetest.set_node(pos, {name="bitumen:oil_drum"})
local bmeta = minetest.get_meta(pos)
bmeta:set_string("fluid", fluid)
bmeta:set_float("fill", fill)
bmeta:set_float("maxfill", max_fill)
bmeta:set_string("infotext", fluid .." (".. math.floor(((fill*100)/max_fill)+0.5) .."%)")
itemstack:take_item()
return itemstack
end,
})
local function user_name(user)
return user and user:get_player_name() or ""
end
-- Returns a logging function. For empty names, does not log.
local function make_log(name)
return name ~= "" and core.log or function() end
end
minetest.register_node("bitumen:oil_drum", {
description = "Oil Drum",
tiles = {"bitumen_drum_top.png", "bitumen_drum_bottom.png", "bitumen_drum_side.png",
"bitumen_drum_side.png", "bitumen_drum_side.png", "bitumen_drum_side.png"},
paramtype2 = "facedir",
-- inventory_image = "bitumen_oil_drum.png",
groups = {
cracky=2,
oddly_breakable_by_hand=2,
oil_container = 1
},
paramtype = "light",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
--11.25
{-0.49, -0.5, -0.10, 0.49, 0.5, 0.10},
{-0.10, -0.5, -0.49, 0.10, 0.5, 0.49},
--22.5
{-0.46, -0.5, -0.19, 0.46, 0.5, 0.19},
{-0.19, -0.5, -0.46, 0.19, 0.5, 0.46},
-- 33.75
{-0.416, -0.5, -0.28, 0.416, 0.5, 0.28},
{-0.28, -0.5, -0.416, 0.28, 0.5, 0.416},
--45
{-0.35, -0.5, -0.35, 0.35, 0.5, 0.35},
},
},
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
},
stack_max = 99,
-- tube = tubes_properties,legacy_facedir_simple = true,
-- sounds = default.node_sound_wood_defaults(),
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_string("infotext", "Oil Drum (Empty)")
meta:set_string("fluid", "air")
meta:set_float("fill", "0")
meta:set_float("maxfill", "48")
meta:set_string("infotext", "Empty drum")
-- meta:set_string("fillmax", "200") -- 55 US Gal, 44 Imp. Gal, 200 L
end,
-- can_dig = function(pos, player)
-- local meta = minetest.env:get_meta(pos)
-- if meta:get_int('filllevel') > 0 then
-- return false
-- end
-- return true
-- end,
on_dig = function(pos, node, digger)
local diggername = user_name(digger)
local log = make_log(diggername)
local def = minetest.registered_nodes[node.name]
if def and (not def.diggable or
(def.can_dig and not def.can_dig(pos, digger))) then
minetest.log("info", diggername .. " tried to dig "
.. node.name .. " which is not diggable "
.. minetest.pos_to_string(pos))
return
end
if minetest.is_protected(pos, diggername) then
log("action", diggername
.. " tried to dig " .. node.name
.. " at protected position "
.. minetest.pos_to_string(pos))
minetest.record_protection_violation(pos, diggername)
return
end
-- create an item and propagate meta
local item
local bmeta = minetest.get_meta(pos)
local fluid = bmeta:get_string("fluid")
local fill = bmeta:get_int("fill")
local max_fill = bmeta:get_int("maxfill")
print("fill: "..fill.." fluid:"..fluid)
if fill == 0 or fluid == "air" then
item = ItemStack("bitumen:oil_drum 1")
else
item = ItemStack("bitumen:oil_drum_filled 1")
local imeta = item:get_meta()
imeta:set_string("fluid", fluid)
imeta:set_string("infotext", fluid .. " drum")
imeta:set_float("fill", fill)
imeta:set_float("maxfill", max_fill)
end
-- check if the digger has enough inventory
local inv = digger and digger:get_inventory()
if inv and inv:room_for_item("main", item) then
inv:add_item("main", item)
else
-- can't dig item
return
end
minetest.remove_node(pos)
-- Run callback
if def and def.after_dig_node then
-- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
end
-- Run script hook
local _, callback
for _, callback in ipairs(core.registered_on_dignodes) do
local origin = minetest.callback_origins[callback]
if origin then
minetest.set_last_run_mod(origin.mod)
--print("Running " .. tostring(callback) ..
-- " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
else
--print("No data associated with callback")
end
-- Copy pos and node because callback can modify them
local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
callback(pos_copy, node_copy, digger)
end
end
})
-- filler
minetest.register_node("bitumen:drum_filler", {
description = "Petroleum Drum Filler",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -.1, -.1, -.1, .1, .5, .1},
{ -.4, -.5, -.4, .4, 0, .4},
},
},
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
},
},
paramtype = "light",
is_ground_content = false,
tiles = { "default_bronze_block.png" },
walkable = true,
groups = { cracky = 3, petroleum_fixture = 1 },
})
minetest.register_abm({
nodenames = {"bitumen:drum_filler"},
neighbors = {"bitumen:oil_drum"},
interval = 2,
chance = 1,
action = function(pos, node)
local npos = {x=pos.x, y=pos.y + 1, z=pos.z}
local pnet = bitumen.pipes.get_net(npos)
if not pnet then
return
end
local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
local bmeta = minetest.env:get_meta(bpos)
local fluid = bmeta:get_string("fluid")
local fill = bmeta:get_int("fill")
local max_fill = bmeta:get_int("maxfill")
if pnet.buffer <= 0 then
print("barrel filler: no oil in pipe")
return -- no water in the pipe
end
if pnet.fluid ~= fluid and fluid ~= "air" then
print("barrel filler: bad_fluid")
return -- incompatible fluids
end
local cap = math.max(max_fill - fill, 0)
-- print("cap: "..cap)
local to_take = math.min(10, math.min(cap, pnet.buffer))
if to_take == 0 then
-- print("barrel full")
return
end
local taken, fluid = bitumen.pipes.take_fluid(npos, to_take)
if fluid == "air" or fill == 0 then
bmeta:set_string("fluid", pnet.fluid)
end
bmeta:set_float("fill", math.min(taken + fill, max_fill))
bmeta:set_string("infotext", fluid .." (".. math.floor(((taken+fill)*100/max_fill)+0.5) .."%)")
print("barrel took ".. taken)
end,
})
-- extractor
minetest.register_node("bitumen:drum_extractor", {
description = "Petroleum Drum Extractor",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -.1, -.4, -.1, .1, .5, .1},
{ -.4, -.5, -.4, .4, -.3, .4},
},
},
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 0.5, 0.0, 0.5},
},
},
paramtype = "light",
is_ground_content = false,
tiles = { "default_tin_block.png" },
walkable = true,
groups = { cracky = 3, petroleum_fixture = 1 },
})
minetest.register_abm({
nodenames = {"bitumen:drum_extractor"},
neighbors = {"bitumen:oil_drum"},
interval = 2,
chance = 1,
action = function(pos, node)
local npos = {x=pos.x, y=pos.y + 1, z=pos.z}
local pnet = bitumen.pipes.get_net(npos)
local bpos = {x=pos.x, y=pos.y - 1, z=pos.z}
local bmeta = minetest.env:get_meta(bpos)
local fluid = bmeta:get_string("fluid")
local fill = bmeta:get_int("fill")
local max_fill = bmeta:get_int("maxfill")
local lift = 4
local max_push = 3
if pnet.buffer >= 64 then
return
end
local to_push = math.min(max_push, math.min(64 - pnet.buffer, fill))
if to_push <= 0 then
--print("barrel extractor: barrel empty")
return
end
print("to_push: "..to_push)
if pnet.fluid ~= fluid and pnet.fluid ~= "air" then
--print("barrel extractor: bad fluid")
return -- incompatible fluids
end
local pushed = bitumen.pipes.push_fluid(npos, fluid, to_push, lift)
bmeta:set_float("fill", math.max(fill - pushed, 0))
bmeta:set_string("infotext", fluid .." (".. math.floor(((fill-pushed)*100/max_fill)+0.5) .."%)")
--print("barrel pushed ".. pushed)
end,
})