summaryrefslogtreecommitdiff
path: root/nodes_barrel.lua
blob: 946fb11683f5ed8f16ec0988904583a51cf778b1 (plain)
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

---------------------------------------------------------------------
-- a barrel and a tub - plus a function that makes 'round' objects
---------------------------------------------------------------------
-- IMPORTANT NOTE: The barrel requires a lot of nodeboxes. That may be
--                 too much for weak hardware!
---------------------------------------------------------------------
-- Functionality: right-click to open/close a barrel;
--                punch a barrel to change between vertical/horizontal
---------------------------------------------------------------------
-- Changelog:
-- 24.03.13 Can no longer be opended/closed on rightclick because that is now used for a formspec;
--          instead, it can be filled with liquids.
--          Filled barrels will always be closed, while empty barrels will always be open.
-- 15.07.2018 The barrels finally work, and hold 50 buckets of any liquid

-- pipes: table with the following entries for each pipe-part:
--    f: radius factor; if 1, it will have a radius of half a nodebox and fill the entire nodebox
--    h1, h2: height at witch the nodebox shall start and end; usually -0.5 and 0.5 for a full nodebox
--    b: make a horizontal part/shelf
-- horizontal: if 1, then x and y coordinates will be swapped

-- TODO: option so that it works without nodeboxes

local S = cottages.S

barrel = {};

local barrel_max = 50

local liquids = {
	lava = { name = "Lava", bucket = "bucket:bucket_lava" },
	water = { name = "Water", bucket = "bucket:bucket_water" },
	rwater = { name = "River water", bucket = "bucket:bucket_river_water" },
	cactus = { name = "Cactus pulp", bucket = "ethereal:bucket_cactus" },
	milk = { name = "Milk", bucket = "mobs:bucket_milk" },
}


barrel.prepare_formspec = function(fill, contents)
	local label = "nothing"
	local item = "air"
	local hint = ""
	local percent = 0
	if contents then
		hint = liquids[contents].name
		label = contents
		item = liquids[contents].bucket
	end
	if fill then 
		percent = 100 * fill / barrel_max
	end
	
	local formspec =  "size[8,9]"..
				"image[2.6,2;2,3;default_wood.png^[lowpart:"..
				percent .. ":cottages_water_indicator.png]"..
				"label[2.2,0;"..S("Pour:").."]"..
				"list[context;input;3,0.5;1,1;]"..
				"item_image_button[5,2;1,1;" .. item .. ";" .. label .. ";" .. hint .. "]" ..
				"label[5,3.3;"..S("Fill:").."]"..
				"list[context;output;5,3.8;1,1;]"..
				"list[current_player;main;0,5;8,4;]"
				
	return (formspec)
end

-- prepare formspec
barrel.on_construct = function( pos )

	local meta = minetest.get_meta(pos);

	meta:set_string( 'liquid_type', '' ); -- which liquid is in the barrel?
	meta:set_int(    'liquid_level', 0 ); -- how much of the liquid is in there?

	local inv = meta:get_inventory()
	inv:set_size("input",     1);  -- to fill in new liquid
	inv:set_size("output",    1);  -- to extract liquid 

	meta:set_string('formspec', barrel.prepare_formspec())
	meta:set_string('infotext', S("Empty barrel"))

end

-- can only be digged if there are no more vessels/buckets in any of the slots
-- TODO: allow digging of a filled barrel? this would disallow stacking of them
barrel.can_dig = function( pos, player )
	local  meta = minetest.get_meta(pos);
	local  inv = meta:get_inventory()

	local name = player:get_player_name()
	if minetest.is_protected(pos, name) then
		minetest.record_protection_violation(pos, name)
		return false
	end
	
	return ( inv:is_empty('input')
		and inv:is_empty('output')
		and meta:get_int('liquid_level') == 0);
end


-- allow to put into "pour": if barrel is empty or has the same type AND we know the bucket type
-- allow to put into "fill": if empty bucket and barrel is not empty
barrel.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
	local iname = stack:get_name()
                                      
	local meta = minetest.get_meta(pos)
	
	if listname == "input" then
	
		local ltype = nil
                                      
		for l,d in pairs(liquids) do
			if d.bucket == iname then
				ltype = l
				break
			end
		end
							
		if not ltype then
			return 0
		end
	
		local lt = meta:get_string('liquid_type')
		if lt == "" or (ltype == lt and meta:get_int('liquid_level') < barrel_max) then
			return 1
		else
			return 0
		end
	end
                                      
	if listname == "output" then
		if iname == "bucket:bucket_empty" and meta:get_int('liquid_level') > 0 then
			return 1
		else
			return 0
		end
	end
	
	return 0
                                      
end
            
                                      
                                     
-- the barrel received input; either a new liquid that is to be poured in or a vessel that is to be filled
barrel.on_metadata_inventory_put = function( pos, listname, index, stack, player )

	local iname = stack:get_name()
      local meta = minetest.get_meta(pos)
	local inv = meta:get_inventory()
	local node = minetest.get_node(pos)
	
	if listname == "input" then
		local liquid = ""
		for l,d in pairs(liquids) do
			if d.bucket == iname then
				liquid = d.bucket
				inv:set_stack("input", 1, {name = "bucket:bucket_empty"})
				local level = meta:get_int("liquid_level") + 1
				meta:set_int("liquid_level", level)
				meta:set_string("liquid_type", l)
				
				meta:set_string('formspec', barrel.prepare_formspec(level, l))
				if level == 1 then
					minetest.swap_node(pos, {name = node.name:gsub("_open$",""), param2 = node.param2})
					meta:set_string('infotext', S("Barrel with ") .. d.name)
				end
				break
			end
		end
	end
	
	
	if listname == "output" then
		local lt = meta:get_string("liquid_type")
		
		inv:set_stack("output", 1, {name = liquids[lt].bucket})
		local level = meta:get_int("liquid_level") - 1
		if level == 0 then
			minetest.swap_node(pos, {name = node.name .. "_open", param2 = node.param2})
			meta:set_string('infotext', S("Empty barrel"))
			meta:set_string("liquid_type", "")
			meta:set_int("liquid_level", 0)
			lt = nil
		else
			meta:set_int("liquid_level", level)
		end

		meta:set_string('formspec', barrel.prepare_formspec(level, lt))
		
	end
	
                                      
end

-- Barrels: default = open = empty // closed = has contents

-- this barrel is closed
minetest.register_node("cottages:barrel", {
	description = S("Barrel (closed)"),
	paramtype = "light",
	drawtype = "mesh",
	mesh = "cottages_barrel_closed.obj",
	tiles = {"cottages_barrel.png" },
	groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory = 1 },
	drop = "cottages:barrel_open",

	on_construct = function( pos )
		return barrel.on_construct( pos );
	end,
	can_dig = function(pos,player)
		return barrel.can_dig( pos, player );
	end,
	allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
	on_metadata_inventory_put = barrel.on_metadata_inventory_put,

	is_ground_content = false,
})

-- this barrel is opened at the top
minetest.register_node("cottages:barrel_open", {
	description = S("Barrel (open)"),
	paramtype = "light",
	drawtype = "mesh",
	mesh = "cottages_barrel.obj",
	tiles = {"cottages_barrel.png" },
	groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
	drop = "cottages:barrel_open",

	on_construct = function( pos )
		return barrel.on_construct( pos );
	end,
	can_dig = function(pos,player)
		return barrel.can_dig( pos, player );
	end,
	allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
	on_metadata_inventory_put = barrel.on_metadata_inventory_put,

	is_ground_content = false,
})

-- horizontal barrel
minetest.register_node("cottages:barrel_lying", {
	description = S("Barrel (closed), lying somewhere"),
	paramtype = "light",
	paramtype2 = "wallmounted",
	drawtype = "mesh",
	mesh = "cottages_barrel_closed_lying.obj",
	tiles = {"cottages_barrel.png" },
	groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, not_in_creative_inventory = 1 },
	drop = "cottages:barrel_lying_open",
	on_construct = function( pos )
		return barrel.on_construct( pos );
	end,
	can_dig = function(pos,player)
		return barrel.can_dig( pos, player );
	end,
	allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
	on_metadata_inventory_put = barrel.on_metadata_inventory_put,
	is_ground_content = false,
})

-- horizontal barrel, open
minetest.register_node("cottages:barrel_lying_open", {
	description = S("Barrel (opened), lying somewhere"),
	paramtype = "light",
	paramtype2 = "wallmounted",
	drawtype = "mesh",
	mesh = "cottages_barrel_lying.obj",
	tiles = {"cottages_barrel.png" },
	groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2, },
	drop = "cottages:barrel_lying_open",
	on_construct = function( pos )
		return barrel.on_construct( pos );
	end,
	can_dig = function(pos,player)
		return barrel.can_dig( pos, player );
	end,
	allow_metadata_inventory_put = barrel.allow_metadata_inventory_put,
	on_metadata_inventory_put = barrel.on_metadata_inventory_put,
	is_ground_content = false,
})

        
        
-- let's hope "tub" is the correct english word for "bottich"
minetest.register_node("cottages:tub", {
	description = S("Tub"),
	paramtype = "light",
	drawtype = "mesh",
	mesh = "cottages_tub.obj",
	tiles = {"cottages_barrel.png" },
	selection_box = {
		type = "fixed",
		fixed = {
			{-0.5, -0.5, -0.5, 0.5,-0.1, 0.5},
		}},
	collision_box = {
		type = "fixed",
		fixed = {
			{-0.5, -0.5, -0.5, 0.5,-0.1, 0.5},
		}},
	groups = { tree = 1, snappy = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2 },
	is_ground_content = false,
})


minetest.register_craft({
	output = "cottages:barrel_open",
	recipe = {
		{ cottages.craftitem_wood,	"",					cottages.craftitem_wood },
		{ cottages.craftitem_steel,	"",					cottages.craftitem_steel},
		{ cottages.craftitem_wood,	cottages.craftitem_wood,	cottages.craftitem_wood },
	},
})


minetest.register_craft({
	output = "cottages:barrel_lying_open",
	recipe = {
		{ cottages.craftitem_wood,	cottages.craftitem_steel,	cottages.craftitem_wood },
		{ cottages.craftitem_wood,	"",          			"" },
		{ cottages.craftitem_wood,	cottages.craftitem_steel,	cottages.craftitem_wood },
	},
})

minetest.register_craft({
	output = "cottages:tub 2",
	recipe = {
		{"cottages:barrel"},
	},
})

minetest.register_craft({
	output = "cottages:barrel",
	recipe = {
		{"cottages:tub"},
		{"cottages:tub"},
	},
})