ShadowNinja
2013-10-30 be2f30a1a2f5b6c2aae7fd4cf8231aec2da0844d
commit | author | age
be2f30 1
S 2 technic.compressor_recipes = {}
3
4 local S = technic.getter
ee0765 5
S 6 technic.register_compressor_recipe = function(src, src_count, dst, dst_count)
7     technic.compressor_recipes[src] = {src_count = src_count, dst_name = dst, dst_count = dst_count}
8     if unified_inventory then
be2f30 9         unified_inventory.register_craft({
ee0765 10             type = "compressing",
S 11             output = dst.." "..dst_count,
12             items = {src.." "..src_count},
13             width = 0,
14         })
15     end
16 end
17
18 technic.get_compressor_recipe = function(item)
19     if technic.compressor_recipes[item.name] and 
20        item.count >= technic.compressor_recipes[item.name].src_count then
21         return technic.compressor_recipes[item.name]
22     else
23         return nil
24     end
25 end
26
27 technic.register_compressor_recipe("default:snowblock",         1, "default:ice",             1)
28 technic.register_compressor_recipe("default:sand",              1, "default:sandstone",       1)
29 technic.register_compressor_recipe("default:desert_sand",       1, "default:desert_stone",    1)
30 technic.register_compressor_recipe("technic:mixed_metal_ingot", 1, "technic:composite_plate", 1)
31 technic.register_compressor_recipe("default:copper_ingot",      5, "technic:copper_plate",    1)
32 technic.register_compressor_recipe("technic:coal_dust",         4, "technic:graphite",        1)
33 technic.register_compressor_recipe("technic:carbon_cloth",      1, "technic:carbon_plate",    1)
34 technic.register_compressor_recipe("technic:enriched_uranium",  4, "technic:uranium_fuel",    1)
35
36
37 minetest.register_alias("compressor", "technic:compressor")
38 minetest.register_craft({
39     output = 'technic:compressor',
40     recipe = {
41         {'default:stone',    'default:stone',    'default:stone'},
42         {'mesecons:piston',    'technic:motor',    'mesecons:piston'},
43         {'default:stone',    'technic:lv_cable0',    'default:stone'},
44     }
45 })
46
47 local compressor_formspec =
48     "invsize[8,9;]"..
be2f30 49     "label[0,0;"..S("Compressor").."]"..
ee0765 50     "list[current_name;src;3,1;1,1;]"..
S 51     "list[current_name;dst;5,1;2,2;]"..
52     "list[current_player;main;0,5;8,4;]"
53
54 minetest.register_node("technic:compressor", {
be2f30 55     description = S("Compressor"),
ee0765 56     tiles = {"technic_compressor_top.png",  "technic_compressor_bottom.png",
S 57              "technic_compressor_side.png", "technic_compressor_side.png",
58              "technic_compressor_back.png", "technic_compressor_front.png"},
59     paramtype2 = "facedir",
60     groups = {cracky=2},
61     legacy_facedir_simple = true,
62     sounds = default.node_sound_wood_defaults(),
63     on_construct = function(pos)
64         local meta = minetest.get_meta(pos)
be2f30 65         meta:set_string("infotext", S("Compressor"))
ee0765 66         meta:set_string("formspec", compressor_formspec)
S 67         local inv = meta:get_inventory()
68         inv:set_size("src", 1)
69         inv:set_size("dst", 4)
70     end,
71     can_dig = function(pos,player)
72         local meta = minetest.get_meta(pos)
73         local inv = meta:get_inventory()
74         if not inv:is_empty("src") or not inv:is_empty("dst") then
75             minetest.chat_send_player(player:get_player_name(),
be2f30 76                     S("Machine cannot be removed because it is not empty"))
ee0765 77             return false
S 78         else
79             return true
80         end
81     end,
82 })
83
84 minetest.register_node("technic:compressor_active", {
be2f30 85     description = S("Compressor"),
ee0765 86     tiles = {"technic_compressor_top.png",  "technic_compressor_bottom.png",
S 87              "technic_compressor_side.png", "technic_compressor_side.png",
88              "technic_compressor_back.png", "technic_compressor_front_active.png"},
89     paramtype2 = "facedir",
90     groups = {cracky=2, not_in_creative_inventory=1},
91     legacy_facedir_simple = true,
92     sounds = default.node_sound_wood_defaults(),
93     can_dig = function(pos,player)
94         local meta = minetest.get_meta(pos);
95         local inv = meta:get_inventory()
96         if not inv:is_empty("src") or not inv:is_empty("dst") then
97             minetest.chat_send_player(player:get_player_name(),
be2f30 98                     S("Machine cannot be removed because it is not empty"))
ee0765 99             return false
S 100         else
101             return true
102         end
103     end,
104 })
105
106 minetest.register_abm({
107     nodenames = {"technic:compressor","technic:compressor_active"},
108     interval = 1,
109     chance   = 1,
110     action = function(pos, node, active_object_count, active_object_count_wider)
111         local meta         = minetest.get_meta(pos)
112         local eu_input     = meta:get_int("LV_EU_input")
be2f30 113         local machine_name = S("Compressor")
ee0765 114         local machine_node = "technic:compressor"
S 115         local demand       = 300
116  
117          -- Setup meta data if it does not exist.
118         if not eu_input then
119             meta:set_int("LV_EU_demand", demand)
120             meta:set_int("LV_EU_input", 0)
121             return
122         end
123  
124         -- Power off automatically if no longer connected to a switching station
125         technic.switching_station_timeout_count(pos, "LV")
126         local inv    = meta:get_inventory()
127         local empty  = inv:is_empty("src")
128         local srcstack  = inv:get_stack("src", 1)
129         local src_item, recipe, result = nil, nil, nil
130
131         if srcstack then
132             src_item = srcstack:to_table()
133         end
134         if src_item then
135             recipe = technic.get_compressor_recipe(src_item)
136         end
137         if recipe then
138             result = {name=recipe.dst_name, count=recipe.dst_count}
139         end 
140         if empty or (not result) or
141            (not inv:room_for_item("dst", result)) then
142             hacky_swap_node(pos, machine_node)
be2f30 143             meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 144             meta:set_int("LV_EU_demand", 0)
S 145             meta:set_int("src_time", 0)
146             return
147         end
148
149         if eu_input < demand then
150             hacky_swap_node(pos, machine_node)
be2f30 151             meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
ee0765 152         elseif eu_input >= demand then
S 153             hacky_swap_node(pos, machine_node.."_active")
be2f30 154             meta:set_string("infotext", S("%s Active"):format(machine_name))
ee0765 155
S 156             meta:set_int("src_time", meta:get_int("src_time") + 1)
157             if meta:get_int("src_time") >= 4 then 
158                 meta:set_int("src_time", 0)
159                 srcstack:take_item(recipe.src_count)
160                 inv:set_stack("src", 1, srcstack)
161                 inv:add_item("dst", result)
162             end
163         end
164         meta:set_int("LV_EU_demand", demand)
165     end
166 })
167
168 technic.register_machine("LV", "technic:compressor",        technic.receiver)
169 technic.register_machine("LV", "technic:compressor_active", technic.receiver)
170