kpoppel
2013-08-27 ec9f92d98b88b13adada6250622b9a51f3705026
commit | author | age
d37cf7 1 -- MV grinder
JRJK 2
3 minetest.register_craft({
4     output = 'technic:mv_grinder',
5     recipe = {
6         {'technic:stainless_steel_ingot', 'technic:grinder', 'technic:stainless_steel_ingot'},
7         {'pipeworks:tube_000000', 'technic:mv_transformer', 'pipeworks:tube_000000'},
8         {'technic:stainless_steel_ingot', 'technic:mv_cable', 'technic:stainless_steel_ingot'},
9     }
10 })
11 minetest.register_craftitem("technic:mv_grinder", {
12     description = "MV Grinder",
13     stack_max = 99,
14 })
15
16 local mv_grinder_formspec =
17    "invsize[8,10;]"..
18    "list[current_name;src;3,1;1,1;]"..
19    "list[current_name;dst;5,1;2,2;]"..
20    "list[current_player;main;0,6;8,4;]"..
21    "label[0,0;MV Grinder]"..
22    "list[current_name;upgrade1;1,4;1,1;]"..
23    "list[current_name;upgrade2;2,4;1,1;]"..
24    "label[1,5;Upgrade Slots]"
25
26 minetest.register_node(
27 "technic:mv_grinder",
28 {
29     description = "MV Grinder",
30     tiles = {"technic_mv_grinder_top.png", "technic_mv_grinder_bottom.png", "technic_mv_grinder_side.png",
31             "technic_mv_grinder_side.png", "technic_mv_grinder_side.png", "technic_mv_grinder_front.png"},
32     paramtype2 = "facedir",
33     groups = {cracky=2, tubedevice=1,tubedevice_receiver=1,},
34     tube={insert_object=function(pos,node,stack,direction)
35             local meta=minetest.env:get_meta(pos)
36             local inv=meta:get_inventory()
37             return inv:add_item("src",stack)
38         end,
39         can_insert=function(pos,node,stack,direction)
40             local meta=minetest.env:get_meta(pos)
41             local inv=meta:get_inventory()
42             return inv:room_for_item("src",stack)
43         end,
2e4d98 44         connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
d37cf7 45     },
JRJK 46     legacy_facedir_simple = true,
47     sounds = default.node_sound_wood_defaults(),
48     on_construct = function(pos)
49         local meta = minetest.env:get_meta(pos)
50         meta:set_string("infotext", "MV Grinder")
51         meta:set_float("technic_mv_power_machine", 1)
52         meta:set_int("tube_time",  0)
53         meta:set_string("formspec", mv_grinder_formspec)
54         local inv = meta:get_inventory()
55         inv:set_size("src", 1)
56         inv:set_size("dst", 4)
57         inv:set_size("upgrade1", 1)
58         inv:set_size("upgrade2", 1)
59     end,
60     can_dig = function(pos,player)
61         local meta = minetest.env:get_meta(pos);
62         local inv = meta:get_inventory()
63         if not inv:is_empty("src") or not inv:is_empty("dst") or not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
64             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
65             return false
66         else
67             return true
68         end
69     end,
70 })
71
72 minetest.register_node(
73     "technic:mv_grinder_active",
74     {
75         description = "Grinder",
76         tiles = {"technic_mv_grinder_top.png", "technic_mv_grinder_bottom.png", "technic_mv_grinder_side.png",
77             "technic_mv_grinder_side.png", "technic_mv_grinder_side.png", "technic_mv_grinder_front_active.png"},
78         paramtype2 = "facedir",
79         groups = {cracky=2,tubedevice=1,tubedevice_receiver=1,not_in_creative_inventory=1},
80         tube={    insert_object=function(pos,node,stack,direction)
81                 local meta=minetest.env:get_meta(pos)
82                 local inv=meta:get_inventory()
83                 return inv:add_item("src",stack)
84             end,
85             can_insert=function(pos,node,stack,direction)
86                 local meta=minetest.env:get_meta(pos)
87                 local inv=meta:get_inventory()
88                 return inv:room_for_item("src",stack)
89             end,
2e4d98 90             connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
d37cf7 91         },
JRJK 92         legacy_facedir_simple = true,
93         sounds = default.node_sound_wood_defaults(),
94         can_dig = function(pos,player)
95             local meta = minetest.env:get_meta(pos);
96             local inv = meta:get_inventory()
97             if not inv:is_empty("src") or not inv:is_empty("dst") or not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
98                 minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
99                 return false
100             else
101                 return true
102             end
103         end,
104         -- These three makes sure upgrades are not moved in or out while the grinder is active.
105         allow_metadata_inventory_put = function(pos, listname, index, stack, player)
106             if listname == "src" or listname == "dst" then
107                 return 99
108             else
109                 return 0 -- Disallow the move
110             end
111         end,
112         allow_metadata_inventory_take = function(pos, listname, index, stack, player)
113             if listname == "src" or listname == "dst" then
114                 return 99
115             else
116                 return 0 -- Disallow the move
117             end
118         end,
119         allow_metadata_inventory_move = function(pos, from_list, to_list, to_list, to_index, count, player)
120             return 0
121         end,
122     })
123
124 local send_grinded_items = function(pos,x_velocity,z_velocity)
125     -- Send items on their way in the pipe system.
126     local meta=minetest.env:get_meta(pos) 
127     local inv = meta:get_inventory()
128     local i=0
129     for _,stack in ipairs(inv:get_list("dst")) do
130         i=i+1
131         if stack then
132             local item0=stack:to_table()
133             if item0 then 
134                 item0["count"]="1"
135                 local item1=tube_item({x=pos.x,y=pos.y,z=pos.z},item0)
136                 item1:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
137                 item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
138                 item1:setacceleration({x=0, y=0, z=0})
139                 stack:take_item(1);
140                 inv:set_stack("dst", i, stack)
141                 return
142             end
143         end
144     end
145 end
146
147 minetest.register_abm(
148     { nodenames = {"technic:mv_grinder","technic:mv_grinder_active"},
149     interval = 1,
150     chance   = 1,
151     action = function(pos, node, active_object_count, active_object_count_wider)
152         -- Run a machine through its states. Takes the same arguments as the ABM action
153         -- and adds the machine's states and any extra data which is needed by the machine.
154         -- A machine is characterized by running through a set number of states (usually 2:
155         -- Idle and active) in some order. A state decides when to move to the next one
156         -- and the machine only changes state if it is powered correctly.
157         -- The machine will automatically shut down if disconnected from power in some fashion.
158         local meta         = minetest.env:get_meta(pos)
159         local eu_input     = meta:get_int("MV_EU_input")
160         local state        = meta:get_int("state")
161         local next_state   = state
162
163         -- Machine information
164         local machine_name         = "MV Grinder"
165         local machine_node         = "technic:mv_grinder"
166         local machine_state_demand = { 50, 600, 450, 300 }
167
168         -- Setup meta data if it does not exist. state is used as an indicator of this
169         if state == 0 then
170             meta:set_int("state", 1)
171             meta:set_int("MV_EU_demand", machine_state_demand[1])
172             meta:set_int("MV_EU_input", 0)
173             return
174         end
175     
176         -- Power off automatically if no longer connected to a switching station
177         technic.switching_station_timeout_count(pos, "MV")
178     
179         -- State machine
180         if eu_input == 0 then
181             -- unpowered - go idle
182             hacky_swap_node(pos, machine_node)
183             meta:set_string("infotext", machine_name.." Unpowered")
184             next_state = 1
185         elseif eu_input == machine_state_demand[state] then
186             -- Powered - do the state specific actions
187         
188             local inv    = meta:get_inventory()
189             local empty  = inv:is_empty("src")
190             
191             -- get the names of the upgrades
192             local upg_item1
193             local upg_item1_name=""
194             local upg_item2
195             local upg_item2_name=""
196             local srcstack = inv:get_stack("upgrade1", 1)
197             if  srcstack then upg_item1=srcstack:to_table() end
198             srcstack = inv:get_stack("upgrade2", 1)
199             if  srcstack then upg_item2=srcstack:to_table() end
200             if upg_item1 then upg_item1_name=upg_item1.name end
201             if upg_item2 then upg_item2_name=upg_item2.name end
202
203             -- Save some power by installing battery upgrades. Fully upgraded makes this
204             -- furnace use the same amount of power as the LV version
205             local EU_saving_upgrade = 0
206             if upg_item1_name=="technic:battery" then EU_saving_upgrade = EU_saving_upgrade + 1 end
207             if upg_item2_name=="technic:battery" then EU_saving_upgrade = EU_saving_upgrade + 1 end
208
209             -- Tube loading speed can be upgraded using control logic units
210             local tube_speed_upgrade = 0
211             if upg_item1_name=="technic:control_logic_unit" then tube_speed_upgrade = tube_speed_upgrade + 1 end
212             if upg_item2_name=="technic:control_logic_unit" then tube_speed_upgrade = tube_speed_upgrade + 1 end
213
214             -- Handle pipeworks (consumes tube_speed_upgrade)
215             local pos1={x=pos.x, y=pos.y, z=pos.z}
216             local x_velocity=0
217             local z_velocity=0
218
219             -- Output is on the left side of the furnace
220             if node.param2==3 then pos1.z=pos1.z-1 z_velocity =-1 end
221             if node.param2==2 then pos1.x=pos1.x-1 x_velocity =-1 end
222             if node.param2==1 then pos1.z=pos1.z+1 z_velocity = 1 end
223             if node.param2==0 then pos1.x=pos1.x+1 x_velocity = 1 end
224
225             local output_tube_connected = false
226             local meta1 = minetest.env:get_meta(pos1) 
227             if meta1:get_int("tubelike") == 1 then
228                 output_tube_connected=true
229             end
230             tube_time = meta:get_int("tube_time")
231             tube_time = tube_time + tube_speed_upgrade
232             if tube_time > 3 then
233                 tube_time = 0
234                 if output_tube_connected then
235                     send_grinded_items(pos,x_velocity,z_velocity)
236                 end
237             end
238             meta:set_int("tube_time", tube_time)
239
50b8ae 240             -- The machine shuts down if we have nothing to smelt since we tube stuff
JRJK 241             -- out while being idle.
242             if inv:is_empty("src") then
d37cf7 243                 next_state = 1
JRJK 244             end
245
246             if state == 1 then
247                 hacky_swap_node(pos, machine_node)
248                 meta:set_string("infotext", machine_name.." Idle")
249
250                 local result = technic.get_grinder_recipe(inv:get_stack("src", 1))
251                 if not empty and result and inv:room_for_item("dst",result) then
252                     meta:set_int("src_time", 0)
253                     next_state = 2+EU_saving_upgrade
254                 end
255
256             elseif state == 2 or state == 3 or state == 4 then
257                 hacky_swap_node(pos, machine_node.."_active")
258                 meta:set_string("infotext", machine_name.." Active")
259
260                 if empty then
261                     next_state = 1
262                 else
263                     meta:set_int("src_time", meta:get_int("src_time") + 1)
264                     if meta:get_int("src_time") == 4 then -- 4 ticks per output
265                         -- check if there's room for output in "dst" list
266                         local result = technic.get_grinder_recipe(inv:get_stack("src", 1))
267
268                         meta:set_int("src_time", 0)
269                         if inv:room_for_item("dst",result) then
270                             -- take stuff from "src" list
271                             srcstack = inv:get_stack("src", 1)
272                             srcstack:take_item()
273                             inv:set_stack("src", 1, srcstack)
274                             -- Put result in "dst" list
275                             inv:add_item("dst", result)
276                         else
277                             -- all full: go idle
278                             next_state = 1
279                         end
280                     end
281                 end
282             end
283         end
284         -- Change state?
285         if next_state ~= state then
286             meta:set_int("MV_EU_demand", machine_state_demand[next_state])
287             meta:set_int("state", next_state)
288         end
289     end
290 })
291
292 technic.register_MV_machine ("technic:mv_grinder","RE")
293 technic.register_MV_machine ("technic:mv_grinder_active","RE")