hdastwb
2013-07-24 2d7f750d9a734e2c84cc1ee14746a4ee2f8757e6
commit | author | age
410afa 1 technic.compressor_recipes ={}
R 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
33
34 minetest.register_alias("compressor", "technic:compressor")
35 minetest.register_craft({
36     output = 'technic:compressor',
37     recipe = {
38         {'default:stone',    'default:stone',    'default:stone'},
39         {'mesecons:piston',    'technic:motor',    'mesecons:piston'},
40         {'default:stone',    'technic:lv_cable',    'default:stone'},
41     }
42 })
43
44 minetest.register_craftitem("technic:compressor", {
45     description = "Compressor",
46     stack_max = 99,
47 })
48
49 local compressor_formspec =
50     "invsize[8,9;]"..
51     "label[0,0;Compressor]"..
52     "list[current_name;src;3,1;1,1;]"..
53     "list[current_name;dst;5,1;2,2;]"..
54     "list[current_player;main;0,5;8,4;]"
55
56 minetest.register_node("technic:compressor", {
57     description = "Compressor",
58     tiles = {"technic_compressor_top.png", "technic_compressor_bottom.png", "technic_compressor_side.png",
59            "technic_compressor_side.png", "technic_compressor_back.png", "technic_compressor_front.png"},
60     paramtype2 = "facedir",
61     groups = {cracky=2},
62     legacy_facedir_simple = true,
63     sounds = default.node_sound_wood_defaults(),
64     on_construct = function(pos)
65         local meta = minetest.env:get_meta(pos)
66         meta:set_string("infotext", "Compressor")
67         meta:set_float("technic_power_machine", 1)
68         meta:set_string("formspec", compressor_formspec)
69         local inv = meta:get_inventory()
70         inv:set_size("src", 1)
71         inv:set_size("dst", 4)
72     end,
73     can_dig = function(pos,player)
74         local meta = minetest.env:get_meta(pos);
75         local inv = meta:get_inventory()
76         if not inv:is_empty("src") or not inv:is_empty("dst") then
77             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
78             return false
79         else
80             return true
81         end
82     end,
83 })
84
85 minetest.register_node("technic:compressor_active", {
86     description = "Compressor",
87     tiles = {"technic_compressor_top.png", "technic_compressor_bottom.png", "technic_compressor_side.png",
88            "technic_compressor_side.png", "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.env: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(), "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         -- Run a machine through its states. Takes the same arguments as the ABM action
111         -- and adds the machine's states and any extra data which is needed by the machine.
112         -- A machine is characterized by running through a set number of states (usually 2:
113         -- Idle and active) in some order. A state decides when to move to the next one
114         -- and the machine only changes state if it is powered correctly.
115         -- The machine will automatically shut down if disconnected from power in some fashion.
116         local meta         = minetest.env:get_meta(pos)
117         local eu_input     = meta:get_int("LV_EU_input")
118         local state        = meta:get_int("state")
119         local next_state   = state
120
121         -- Machine information
122         local machine_name         = "Compressor"
123         local machine_node         = "technic:compressor"
124         local machine_state_demand = { 50, 300 }
125  
126          -- Setup meta data if it does not exist. state is used as an indicator of this
127         if state == 0 then
128             meta:set_int("state", 1)
129             meta:set_int("LV_EU_demand", machine_state_demand[1])
130             meta:set_int("LV_EU_input", 0)
131             return
132         end
133  
134         -- Power off automatically if no longer connected to a switching station
135         technic.switching_station_timeout_count(pos, "LV")
136  
137         -- State machine
138         if eu_input == 0 then
139             -- unpowered - go idle
140             hacky_swap_node(pos, machine_node)
141             meta:set_string("infotext", machine_name.." Unpowered")
142             next_state = 1
143         elseif eu_input == machine_state_demand[state] then
144             -- Powered - do the state specific actions
145                 
146             local inv    = meta:get_inventory()
147             local empty  = inv:is_empty("src")
148             local srcstack  = inv:get_stack("src", 1)
149             local src_item = nil
150             local recipe = nil
151             local result = nil
152  
153             if srcstack then
154                 src_item = srcstack:to_table()
155             end
156             if src_item then
157                 recipe = technic.get_compressor_recipe(src_item)
158             end
159             if recipe then
160                 result = {name=recipe.dst_name, count=recipe.dst_count}
161             end 
162
163             if state == 1 then
164                 hacky_swap_node(pos, machine_node)
165                 meta:set_string("infotext", machine_name.." Idle")
166
167                 if not empty and result and inv:room_for_item("dst",result) then
168                     meta:set_int("src_time", 0)
169                     next_state = 2
170                 end
171             elseif state == 2 then
172                 hacky_swap_node(pos, machine_node.."_active")
173                 meta:set_string("infotext", machine_name.." Active")
174
175                 if empty then
176                     next_state = 1
177                 else
178                     meta:set_int("src_time", meta:get_int("src_time") + 1)
179                     if meta:get_int("src_time") == 4 then 
180                         -- 4 ticks per output
181                         -- check if there's room for output in "dst" list
182
183                         meta:set_int("src_time", 0)
184                         if recipe and inv:room_for_item("dst",result) then
185                             -- take stuff from "src" list
186                             srcstack:take_item(recipe.src_count)
187                             inv:set_stack("src", 1, srcstack)
188                             -- Put result in "dst" list
189                             inv:add_item("dst", result)
190                         else
191                             -- all full: go idle
192                             next_state = 1
193                         end
194                     end
195                 end
196             end
197         end
198         -- Change state?
199         if next_state ~= state then
200             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
201             meta:set_int("state", next_state)
202         end
203     end
204 })
205
206 technic.register_LV_machine ("technic:compressor","RE")
207 technic.register_LV_machine ("technic:compressor_active","RE")