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