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