ShadowNinja
2013-10-06 363f0332788e04e2e4bb63af5cd21fac5ae56ae5
commit | author | age
ee0765 1
S 2 -- Coal driven alloy furnace. This uses no EUs:
3
4
5 minetest.register_craft({
6     output = 'technic:coal_alloy_furnace',
7     recipe = {
8         {'default:brick', 'default:brick', 'default:brick'},
9         {'default:brick', '',              'default:brick'},
10         {'default:brick', 'default:brick', 'default:brick'},
11     }
12 })
13
14 minetest.register_node("technic:coal_alloy_furnace", {
15     description = "Alloy Furnace",
16     tiles = {"technic_coal_alloy_furnace_top.png", "technic_coal_alloy_furnace_bottom.png", "technic_coal_alloy_furnace_side.png",
17         "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front.png"},
18     paramtype2 = "facedir",
19     groups = {cracky=2},
20     legacy_facedir_simple = true,
21     sounds = default.node_sound_stone_defaults(),
22     on_construct = function(pos)
23         local meta = minetest.env:get_meta(pos)
24         meta:set_string("formspec", coal_alloy_furnace_formspec)
25         meta:set_string("infotext", "Alloy Furnace")
26         local inv = meta:get_inventory()
27         inv:set_size("fuel", 1)
28         inv:set_size("src", 1)
29         inv:set_size("src2", 1)
30         inv:set_size("dst", 4)
31     end,
32     can_dig = function(pos,player)
33         local meta = minetest.env:get_meta(pos);
34         local inv = meta:get_inventory()
35         if not (inv:is_empty("fuel") or inv:is_empty("dst") or inv:is_empty("src") or inv:is_empty("src2") )then
36             return false
37             end
38         return true
39     end,
40 })
41
42 minetest.register_node("technic:coal_alloy_furnace_active", {
43     description = "Alloy Furnace",
44     tiles = {"technic_coal_alloy_furnace_top.png", "technic_coal_alloy_furnace_bottom.png", "technic_coal_alloy_furnace_side.png",
45              "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front_active.png"},
46     paramtype2 = "facedir",
47     light_source = 8,
48     drop = "technic:coal_alloy_furnace",
49     groups = {cracky=2, not_in_creative_inventory=1},
50     legacy_facedir_simple = true,
51     sounds = default.node_sound_stone_defaults(),
52     can_dig = function(pos,player)
53         local meta = minetest.env:get_meta(pos);
54         local inv = meta:get_inventory()
55         if not (inv:is_empty("fuel") or inv:is_empty("dst") or
56             inv:is_empty("src") or inv:is_empty("src2")) then
57             return false
58         end
59         return true
60     end,
61 })
62
63 minetest.register_abm({
64     nodenames = {"technic:coal_alloy_furnace", "technic:coal_alloy_furnace_active"},
65     interval = 1,
66     chance = 1,
67     action = function(pos, node, active_object_count, active_object_count_wider)
68         local meta = minetest.get_meta(pos)
69         local inv    = meta:get_inventory()
70         local recipe = nil
71         local formspec =
72             "size[8,9]"..
73             "label[0,0;Alloy Furnace]"..
74             "image[2,2;1,1;default_furnace_fire_bg.png]"..
75             "list[current_name;fuel;2,3;1,1;]"..
76             "list[current_name;src;2,1;1,1;]"..
77             "list[current_name;src2;3,1;1,1;]"..
78             "list[current_name;dst;5,1;2,2;]"..
79             "list[current_player;main;0,5;8,4;]"
80
81         for i, name in pairs({
82                 "fuel_totaltime",
83                 "fuel_time",
84                 "src_totaltime",
85                 "src_time"}) do
86             if not meta:get_float(name) then
87                 meta:set_float(name, 0.0)
88             end
89         end
90
91         -- Get what to cook if anything
92         local srcstack = inv:get_stack("src", 1)
93         local src2stack = inv:get_stack("src2", 1)
94         local recipe = technic.get_alloy_recipe(srcstack, src2stack)
95         if srcstack:get_name() > src2stack:get_name() then
96             local temp = srcstack
97             srcstack = src2stack
98             src2stack = temp
99         end
100
101         local was_active = false
102
103         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
104             was_active = true
105             meta:set_int("fuel_time", meta:get_int("fuel_time") + 1)
106             if recipe then
107                 meta:set_int("src_time", meta:get_int("src_time") + 1)
108                 if meta:get_int("src_time") == 6 then
109                     -- check if there's room for output in "dst" list
110                     local dst_stack = ItemStack(recipe.output)
111                     if inv:room_for_item("dst", dst_stack) then
112                         srcstack:take_item(recipe.input[1].count)
113                         inv:set_stack("src", 1, srcstack)
114                         src2stack:take_item(recipe.input[2].count)
115                         inv:set_stack("src2", 1, src2stack)
116                         inv:add_item("dst", dst_stack)
117                     end
118                     meta:set_int("src_time", 0)
119                 end
120             else
121                 meta:set_int("src_time", 0)
122             end
123         end
124
125         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
126             local percent = math.floor(meta:get_float("fuel_time") /
127                     meta:get_float("fuel_totaltime") * 100)
128             meta:set_string("infotext","Furnace active: "..percent.."%")
129             hacky_swap_node(pos, "technic:coal_alloy_furnace_active")
130             meta:set_string("formspec",
131                     "size[8,9]"..
132                     "label[0,0;Electric Alloy Furnace]"..
133                     "image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:"..
134                     (100-percent)..":default_furnace_fire_fg.png]"..
135                     "list[current_name;fuel;2,3;1,1;]"..
136                     "list[current_name;src;2,1;1,1;]"..
137                     "list[current_name;src2;3,1;1,1;]"..
138                     "list[current_name;dst;5,1;2,2;]"..
139                     "list[current_player;main;0,5;8,4;]")
140             return
141         end
142
143         -- FIXME: Make this look more like the electrical version.
144         -- This code refetches the recipe to see if it can be done again after the iteration
145         srcstack = inv:get_stack("src", 1)
146         srcstack = inv:get_stack("src2", 1)
147         local recipe = technic.get_alloy_recipe(srcstack, src2stack)
148
149         if recipe then
150             if was_active then
151                 meta:set_string("infotext", "Furnace is empty")
152                 hacky_swap_node(pos, "technic:coal_alloy_furnace")
153                 meta:set_string("formspec", formspec)
154             end
155             return
156         end
157
158         -- Next take a hard look at the fuel situation
159         local fuel = nil
160         local fuellist = inv:get_list("fuel")
161
162         if fuellist then
163             fuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
164         end
165
166         if fuel.time <= 0 then
167             meta:set_string("infotext", "Furnace out of fuel")
168             hacky_swap_node(pos, "technic:coal_alloy_furnace")
169             meta:set_string("formspec", formspec)
170             return
171         end
172
173         meta:set_string("fuel_totaltime", fuel.time)
174         meta:set_string("fuel_time", 0)
175
176         local stack = inv:get_stack("fuel", 1)
177         stack:take_item()
178         inv:set_stack("fuel", 1, stack)
179     end,
180 })
181