Kevin Zheng
2013-11-28 7cfb3874a381d5923611242b4cf7756d86049def
commit | author | age
ee0765 1
7c4b70 2 -- Fuel driven alloy furnace. This uses no EUs:
ee0765 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
611c77 15 local machine_name = S("Fuel-Fired Alloy Furnace")
Z 16 local formspec =
17     "size[8,9]"..
18     "label[0,0;"..machine_name.."]"..
19     "image[2,2;1,1;default_furnace_fire_bg.png]"..
20     "list[current_name;fuel;2,3;1,1;]"..
21     "list[current_name;src;2,1;2,1;]"..
22     "list[current_name;dst;5,1;2,2;]"..
23     "list[current_player;main;0,5;8,4;]"
24
ee0765 25 minetest.register_node("technic:coal_alloy_furnace", {
611c77 26     description = machine_name,
0809dd 27     tiles = {"technic_coal_alloy_furnace_top.png",  "technic_coal_alloy_furnace_bottom.png",
S 28              "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png",
29              "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front.png"},
ee0765 30     paramtype2 = "facedir",
S 31     groups = {cracky=2},
32     legacy_facedir_simple = true,
33     sounds = default.node_sound_stone_defaults(),
34     on_construct = function(pos)
35         local meta = minetest.env:get_meta(pos)
611c77 36         meta:set_string("formspec", formspec)
Z 37         meta:set_string("infotext", machine_name)
ee0765 38         local inv = meta:get_inventory()
S 39         inv:set_size("fuel", 1)
d55ecc 40         inv:set_size("src", 2)
ee0765 41         inv:set_size("dst", 4)
S 42     end,
0809dd 43     can_dig = technic.machine_can_dig,
S 44     allow_metadata_inventory_put = technic.machine_inventory_put,
45     allow_metadata_inventory_take = technic.machine_inventory_take,
46     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 47 })
S 48
49 minetest.register_node("technic:coal_alloy_furnace_active", {
611c77 50     description = machine_name,
0809dd 51     tiles = {"technic_coal_alloy_furnace_top.png",  "technic_coal_alloy_furnace_bottom.png",
S 52              "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_side.png",
53              "technic_coal_alloy_furnace_side.png", "technic_coal_alloy_furnace_front_active.png"},
ee0765 54     paramtype2 = "facedir",
S 55     light_source = 8,
56     drop = "technic:coal_alloy_furnace",
57     groups = {cracky=2, not_in_creative_inventory=1},
58     legacy_facedir_simple = true,
59     sounds = default.node_sound_stone_defaults(),
0809dd 60     can_dig = technic.machine_can_dig,
S 61     allow_metadata_inventory_put = technic.machine_inventory_put,
62     allow_metadata_inventory_take = technic.machine_inventory_take,
63     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 64 })
S 65
66 minetest.register_abm({
67     nodenames = {"technic:coal_alloy_furnace", "technic:coal_alloy_furnace_active"},
68     interval = 1,
69     chance = 1,
70     action = function(pos, node, active_object_count, active_object_count_wider)
71         local meta = minetest.get_meta(pos)
72         local inv    = meta:get_inventory()
d55ecc 73         
N 74         if inv:get_size("src") == 1 then -- Old furnace -> convert it
75             inv:set_size("src", 2)
76             inv:set_stack("src", 2, inv:get_stack("src2", 1))
77             inv:set_size("src2", 0)
78         end
79         
ee0765 80         local recipe = nil
S 81
82         for i, name in pairs({
83                 "fuel_totaltime",
84                 "fuel_time",
85                 "src_totaltime",
86                 "src_time"}) do
87             if not meta:get_float(name) then
88                 meta:set_float(name, 0.0)
89             end
90         end
91
92         -- Get what to cook if anything
d55ecc 93         local result = technic.get_recipe("alloy", inv:get_list("src"))
ee0765 94
S 95         local was_active = false
96
97         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
98             was_active = true
99             meta:set_int("fuel_time", meta:get_int("fuel_time") + 1)
d55ecc 100             if result then
ee0765 101                 meta:set_int("src_time", meta:get_int("src_time") + 1)
d55ecc 102                 if meta:get_int("src_time") >= result.time then
ee0765 103                     meta:set_int("src_time", 0)
d55ecc 104                     local result_stack = ItemStack(result.output)
N 105                     if inv:room_for_item("dst", result_stack) then
106                         inv:set_list("src", result.new_input)
107                         inv:add_item("dst", result_stack)
108                     end
ee0765 109                 end
S 110             else
111                 meta:set_int("src_time", 0)
112             end
113         end
114
115         if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
116             local percent = math.floor(meta:get_float("fuel_time") /
117                     meta:get_float("fuel_totaltime") * 100)
be2f30 118             meta:set_string("infotext", S("%s Active"):format(machine_name).." ("..percent.."%)")
f3d8b4 119             technic.swap_node(pos, "technic:coal_alloy_furnace_active")
ee0765 120             meta:set_string("formspec",
S 121                     "size[8,9]"..
be2f30 122                     "label[0,0;"..machine_name.."]"..
ee0765 123                     "image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:"..
be2f30 124                     (100 - percent)..":default_furnace_fire_fg.png]"..
ee0765 125                     "list[current_name;fuel;2,3;1,1;]"..
d55ecc 126                     "list[current_name;src;2,1;2,1;]"..
ee0765 127                     "list[current_name;dst;5,1;2,2;]"..
S 128                     "list[current_player;main;0,5;8,4;]")
129             return
130         end
131
d55ecc 132         local recipe = technic.get_recipe("alloy", inv:get_list("src"))
ee0765 133
d55ecc 134         if not recipe then
ee0765 135             if was_active then
d55ecc 136                 meta:set_string("infotext", S("%s is empty"):format(machine_name))
f3d8b4 137                 technic.swap_node(pos, "technic:coal_alloy_furnace")
ee0765 138                 meta:set_string("formspec", formspec)
S 139             end
140             return
141         end
142
143         -- Next take a hard look at the fuel situation
144         local fuel = nil
9290e6 145         local afterfuel
ee0765 146         local fuellist = inv:get_list("fuel")
S 147
148         if fuellist then
9290e6 149             fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
ee0765 150         end
S 151
152         if fuel.time <= 0 then
be2f30 153             meta:set_string("infotext", S("%s Out Of Fuel"):format(machine_name))
f3d8b4 154             technic.swap_node(pos, "technic:coal_alloy_furnace")
ee0765 155             meta:set_string("formspec", formspec)
S 156             return
157         end
158
159         meta:set_string("fuel_totaltime", fuel.time)
160         meta:set_string("fuel_time", 0)
161
9290e6 162         inv:set_stack("fuel", 1, afterfuel.items[1])
ee0765 163     end,
S 164 })
165