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