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