summaryrefslogtreecommitdiff
path: root/init.lua
blob: 0fc615a5b56c62e5fcf9b294f65f522e6bde2f39 (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
local function campfire_timer(pos)
	local node = minetest.get_node(pos)
	local timer = minetest.get_node_timer(pos)
	if node.name ~= "campfire_redo:campfire" and node.name ~= "campfire_redo:campfire_active" then
		timer:stop()
		return false
	end
	local meta = minetest.get_meta(pos)
	local time_remain = meta:get_int("time_remain")
	local time_total = meta:get_int("time_total")
	if time_remain <= 0 then
		local inv = meta:get_inventory()
		local fuel_list = inv:get_list("fuel")
		local fuel,leftover = minetest.get_craft_result({method = "fuel",width = 1,items = fuel_list})
		if fuel.time > 0 then
			time_total = fuel.time
			meta:set_int("time_total",time_total)
			time_remain = time_total
			inv:set_stack("fuel",1,leftover.items[1])
		end
	end
	if time_remain > 0 then
		meta:set_string("infotext","")
		meta:set_int("time_remain",time_remain - 1)
		if node.name == "campfire_redo:campfire" then
			node.name = "campfire_redo:campfire_active"
			minetest.swap_node(pos,node)
		end
		local part = (time_remain / time_total) * 100
		local fs = "size[8,9]"..
			"list[context;fuel;3.5,2;1,1;]"..
			"list[current_player;main;0,5;8,4;]"..
			"listring[]"..
			"image[3.5,1;1,1;default_furnace_fire_bg.png^[lowpart:"..part..":default_furnace_fire_fg.png]"
		meta:set_string("formspec",fs)
	else
		meta:set_string("infotext","Put more wood on the fire!")
		meta:set_int("time_remain",0)
		if node.name == "campfire_redo:campfire_active" then
			node.name = "campfire_redo:campfire"
			minetest.swap_node(pos,node)
			timer:stop()
			local fs = "size[8,9]"..
				"list[context;fuel;3.5,2;1,1;]"..
				"list[current_player;main;0,5;8,4;]"..
				"listring[]"..
				"image[3.5,1;1,1;default_furnace_fire_bg.png]"
			meta:set_string("formspec",fs)
			return false
		end
	end
	return true
end

minetest.register_node("campfire_redo:campfire", {
	description = "Campfire",
	drawtype = "plantlike",
	tiles = {"campfire_redo_campfire.png"},
	walkable = false,
	paramtype = "light",
	sunlight_propogates = true,
	groups = {oddly_breakable_by_hand = 1},
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("infotext","Put some wood on the fire!")
		local fs = "size[8,9]"..
			"list[context;fuel;3.5,2;1,1;]"..
			"list[current_player;main;0,5;8,4;]"..
			"listring[]"..
			"image[3.5,1;1,1;default_furnace_fire_bg.png]"
		meta:set_string("formspec",fs)
		local inv = meta:get_inventory()
		inv:set_size("fuel",1)
	end,
	on_metadata_inventory_put = function(pos)
		minetest.get_node_timer(pos):start(1)
	end,
	on_timer = campfire_timer,
	can_dig = function(pos)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		return inv:is_empty("fuel")
	end
})

minetest.register_node("campfire_redo:campfire_active", {
	drawtype = "plantlike",
	tiles = {{name="campfire_redo_campfire_active.png",animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=1.5}}},
	drop = "campfire_redo:campfire",
	walkable = false,
	paramtype = "light",
	light_source = 8,
	sunlight_propogates = true,
	groups = {oddly_breakable_by_hand = 1,not_in_creative_inventory = 1},
	on_timer = campfire_timer,
	can_dig = function(pos)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		return inv:is_empty("fuel")
	end
})

minetest.register_alias("campfire:campfire","campfire_redo:campfire")
minetest.register_alias("campfire:campfire_active","campfire_redo:campfire_active")

minetest.register_craft({
	output = "campfire_redo:campfire 1",
	recipe = {
			{"","default:stick",""},
			{"default:stick","","default:stick"}
	}
})