ShadowNinja
2013-07-11 5d470cd753efe8f4640099165a7bfc0c6e181c35
commit | author | age
ee5c6c 1 -- LV Electric Furnace
K 2 -- This is a faster version of the stone furnace which runs on EUs
82cba9 3
ee5c6c 4 -- FIXME: kpoppel I'd like to introduce an induction heating element here also
K 5 minetest.register_craft(
6    {output = 'technic:electric_furnace',
7     recipe = {
8        {'default:cobble',      'default:cobble',        'default:cobble'},
9        {'default:cobble',      '',                      'default:cobble'},
10        {'default:steel_ingot', 'moreores:copper_ingot', 'default:steel_ingot'},
11     }
12  })
82cba9 13
ee5c6c 14 local electric_furnace_formspec =
K 15    "invsize[8,9;]"..
16    "list[current_name;src;3,1;1,1;]"..
17    "list[current_name;dst;5,1;2,2;]"..
18    "list[current_player;main;0,5;8,4;]"..
19    "label[0,0;Electric Furnace]"..
20    "label[1,3;Power level]"
82cba9 21
ee5c6c 22 minetest.register_node(
K 23    "technic:electric_furnace",
24    {description = "Electric furnace",
25     tiles = {"technic_electric_furnace_top.png", "technic_electric_furnace_bottom.png", "technic_electric_furnace_side.png",
26          "technic_electric_furnace_side.png", "technic_electric_furnace_side.png", "technic_electric_furnace_front.png"},
27     paramtype2 = "facedir",
28     groups = {cracky=2},
29     legacy_facedir_simple = true,
30     sounds = default.node_sound_stone_defaults(),
31     on_construct = function(pos)
32               local meta = minetest.env:get_meta(pos)
33               meta:set_string("infotext", "Electric Furnace")
34               meta:set_float("technic_power_machine", 1)
35               meta:set_string("formspec", electric_furnace_formspec)
36               local inv = meta:get_inventory()
37               inv:set_size("src", 1)
38               inv:set_size("dst", 4)
39            end,
40     can_dig = function(pos,player)
41          local meta = minetest.env:get_meta(pos);
42          local inv = meta:get_inventory()
43          if not inv:is_empty("src") or not inv:is_empty("dst") then
44             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
45             return false
46          else
47             return true
48          end
49           end,
50  })
82cba9 51
ee5c6c 52 minetest.register_node(
K 53    "technic:electric_furnace_active",
54    {description = "Electric Furnace",
55     tiles = {"technic_electric_furnace_top.png", "technic_electric_furnace_bottom.png", "technic_electric_furnace_side.png",
56          "technic_electric_furnace_side.png", "technic_electric_furnace_side.png", "technic_electric_furnace_front_active.png"},
57     paramtype2 = "facedir",
58     light_source = 8,
59     drop = "technic:electric_furnace",
60     groups = {cracky=2, not_in_creative_inventory=1},
61     legacy_facedir_simple = true,
62     sounds = default.node_sound_stone_defaults(),
63     can_dig = function(pos,player)
64          local meta = minetest.env:get_meta(pos);
65          local inv = meta:get_inventory()
66          if not inv:is_empty("src") or not inv:is_empty("dst") then
67             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
68             return false
69          else
70             return true
71          end
72           end,
73  })
82cba9 74
ee5c6c 75 minetest.register_abm(
K 76    { nodenames = {"technic:electric_furnace","technic:electric_furnace_active"},
77      interval = 1,
78      chance   = 1,
79      action = function(pos, node, active_object_count, active_object_count_wider)
80          local meta         = minetest.env:get_meta(pos)
81          local eu_input     = meta:get_int("LV_EU_input")
82          local state        = meta:get_int("state")
83          local next_state   = state
82cba9 84
ee5c6c 85          -- Machine information
K 86          local machine_name         = "Electric furnace"
87          local machine_node         = "technic:electric_furnace"
88          local machine_state_demand = { 50, 1000 }
89              
90          -- Setup meta data if it does not exist. state is used as an indicator of this
91          if state == 0 then
92             meta:set_int("state", 1)
93             meta:set_int("LV_EU_demand", machine_state_demand[1])
94             meta:set_int("LV_EU_input", 0)
95             return
96          end
97              
98          -- Power off automatically if no longer connected to a switching station
99          technic.switching_station_timeout_count(pos, "LV")
100              
101          -- State machine
102          if eu_input == 0 then
103             -- Unpowered - go idle
104             hacky_swap_node(pos, machine_node)
105             meta:set_string("infotext", machine_name.." Unpowered")
106             next_state = 1
107          elseif eu_input == machine_state_demand[state] then
108             -- Powered - do the state specific actions
109                 
110             -- Execute always if powered logic
111             local inv    = meta:get_inventory()
112             local empty  = inv:is_empty("src")
82cba9 113
ee5c6c 114             if state == 1 then
K 115                hacky_swap_node(pos, machine_node)
116                meta:set_string("infotext", machine_name.." Idle")
82cba9 117
ee5c6c 118                local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
K 119                if not empty and result and inv:room_for_item("dst",result) then
120               next_state = 2
121                end
82cba9 122
ee5c6c 123             elseif state == 2 then
K 124                hacky_swap_node(pos, machine_node.."_active")
125                meta:set_string("infotext", machine_name.." Active")
612349 126
ee5c6c 127                if empty then
K 128               next_state = 1
129                else
130               meta:set_int("src_time", meta:get_int("src_time") + 3) -- Cooking time 3x
131               local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
132               if result and result.item and meta:get_int("src_time") >= result.time then
133                  -- check if there's room for output in "dst" list
134                  meta:set_int("src_time", 0)
135                  if inv:room_for_item("dst",result.item) then
136                 -- take stuff from "src" list
137                 srcstack = inv:get_stack("src", 1)
138                 srcstack:take_item()
139                 inv:set_stack("src", 1, srcstack)
140                 -- Put result in "dst" list
141                 inv:add_item("dst", result.item)
142                  else
143                 -- all full: go idle
144                 next_state = 1
145                  end
146               end
147                end
148             end
149          end
150          -- Change state?
151          if next_state ~= state then
152             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
153             meta:set_int("state", next_state)
154          end
155           end,
156   })
82cba9 157
ee5c6c 158 technic.register_LV_machine ("technic:electric_furnace","RE")
K 159 technic.register_LV_machine ("technic:electric_furnace_active","RE")
612349 160