kpoppel
2013-07-02 053fa59739f4b772174bf0a090969b3395ab3f98
commit | author | age
ee5c6c 1 -- Technic CNC v1.0 by kpoppel
6463b7 2 -- Based on the NonCubic Blocks MOD v1.4 by yves_de_beck
fef948 3
K 4 -- Idea:
ee5c6c 5 --   Somehow have a tabbed/paged panel if the number of shapes should expand
fef948 6 --   beyond what is available in the panel today.
K 7 --   I could imagine some form of API allowing modders to come with their own node
8 --   box definitions and easily stuff it in the this machine for production.
6463b7 9 local shape = {}
K 10 local onesize_products = {
c1df31 11    slope                    = 2,
K 12    slope_edge               = 1,
13    slope_inner_edge         = 1,
14    pyramid                  = 2,
15    spike                    = 1,
16    cylinder                 = 2,
17    sphere                   = 1,
18    stick                    = 8,
19    slope_upsdown            = 2,
20    slope_edge_upsdown       = 1,
21    slope_inner_edge_upsdown = 1,
22    cylinder_horizontal      = 2,
23    slope_lying              = 2,
24    onecurvededge            = 1,
25    twocurvededge            = 1,
6463b7 26 }
K 27 local twosize_products = {
c1df31 28    element_straight         = 4,
K 29    element_end              = 2,
30    element_cross            = 1,
31    element_t                = 1,
32    element_edge             = 2,
6463b7 33 }
K 34
c1df31 35 local cnc_formspec =
K 36    "invsize[9,11;]"..
37    "label[1,0;Choose Milling Program:]"..
38    "image_button[1,0.5;1,1;technic_cnc_slope.png;slope; ]"..
39    "image_button[2,0.5;1,1;technic_cnc_slope_edge.png;slope_edge; ]"..
40    "image_button[3,0.5;1,1;technic_cnc_slope_inner_edge.png;slope_inner_edge; ]"..
41    "image_button[4,0.5;1,1;technic_cnc_pyramid.png;pyramid; ]"..
42    "image_button[5,0.5;1,1;technic_cnc_spike.png;spike; ]"..
43    "image_button[6,0.5;1,1;technic_cnc_cylinder.png;cylinder; ]"..
44    "image_button[7,0.5;1,1;technic_cnc_sphere.png;sphere; ]"..
45    "image_button[8,0.5;1,1;technic_cnc_stick.png;stick; ]"..
46    
47    "image_button[1,1.5;1,1;technic_cnc_slope_upsdwn.png;slope_upsdown; ]"..
48    "image_button[2,1.5;1,1;technic_cnc_slope_edge_upsdwn.png;slope_edge_upsdown; ]"..
49    "image_button[3,1.5;1,1;technic_cnc_slope_inner_edge_upsdwn.png;slope_inner_edge_upsdown; ]"..
50    "image_button[4,1.5;1,1;technic_cnc_cylinder_horizontal.png;cylinder_horizontal; ]"..
51    
52    "image_button[1,2.5;1,1;technic_cnc_slope_lying.png;slope_lying; ]"..
53    "image_button[2,2.5;1,1;technic_cnc_onecurvededge.png;onecurvededge; ]"..
54    "image_button[3,2.5;1,1;technic_cnc_twocurvededge.png;twocurvededge; ]"..
55    
56    "label[1,3.5;Slim Elements half / normal height:]"..
57    
58    "image_button[1,4;1,0.5;technic_cnc_full.png;full; ]"..
59    "image_button[1,4.5;1,0.5;technic_cnc_half.png;half; ]"..
60    "image_button[2,4;1,1;technic_cnc_element_straight.png;element_straight; ]"..
61    "image_button[3,4;1,1;technic_cnc_element_end.png;element_end; ]"..
62    "image_button[4,4;1,1;technic_cnc_element_cross.png;element_cross; ]"..
63    "image_button[5,4;1,1;technic_cnc_element_t.png;element_t; ]"..
64    "image_button[6,4;1,1;technic_cnc_element_edge.png;element_edge; ]"..
65    
66    "label[0, 5.5;In:]"..
67    "list[current_name;src;0.5,5.5;1,1;]"..
68    "label[4, 5.5;Out:]"..
69    "list[current_name;dst;5,5.5;4,1;]"..
70    
71    "list[current_player;main;0,7;8,4;]"
6463b7 72
c1df31 73 local size     = 1;
K 74
fef948 75 -- The form handler is declared here because we need it in both the inactive and active modes
K 76 -- in order to be able to change programs wile it is running.
77 local form_handler = function(pos, formname, fields, sender)
ee5c6c 78             -- REGISTER MILLING PROGRAMS AND OUTPUTS:
K 79             ------------------------------------------
80             -- Program for half/full size
81             if fields["full"] then
82                size = 1
83                return
84             end
85             
86             if fields["half"] then
87                size = 2
88                return
89             end
90             
91             -- Resolve the node name and the number of items to make
92             local meta       = minetest.env:get_meta(pos)
93             local inv        = meta:get_inventory()
94             local inputstack = inv:get_stack("src", 1)
95             local inputname  = inputstack:get_name()
96             local multiplier = 0
97             for k, _ in pairs(fields) do
98                -- Set a multipier for the half/full size capable blocks
99                if twosize_products[k] ~= nil then
100                   multiplier = size*twosize_products[k]
101                else
102                   multiplier = onesize_products[k]
103                end
104                
105                if onesize_products[k] ~= nil or twosize_products[k] ~= nil then
106                   meta:set_float( "cnc_multiplier", multiplier)
107                   meta:set_string("cnc_user", sender:get_player_name())
108                end
109                
110                if onesize_products[k] ~= nil or (twosize_products[k] ~= nil and size==2) then
111                   meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k)
112                   --print(inputname .. "_technic_cnc_" .. k)
113                   break
114                end
115                
116                if twosize_products[k] ~= nil and size==1 then
117                   meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k .. "_double")
118                   --print(inputname .. "_technic_cnc_" .. k .. "_double")
119                   break
120                end
121             end
122             return
123              end -- callback function
fef948 124
K 125 -- The actual block inactive state
ee5c6c 126 minetest.register_node(
K 127    "technic:cnc",
128    {
129       description = "CNC Milling Machine",
130       tiles       = {"technic_cnc_top.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
131              "technic_cnc_side.png", "technic_cnc_side.png", "technic_cnc_front.png"},
132       drawtype    = "nodebox",
133       paramtype   = "light",
134       paramtype2  = "facedir",
135       node_box    = {
136      type  = "fixed",
137      fixed = {
138         {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
139         
140      },
141       },
142       selection_box = {
143      type = "fixed",
144      fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
145       },
146       groups = {cracky=2},
147       legacy_facedir_simple = true,
148       on_construct = function(pos)
149             local meta = minetest.env:get_meta(pos)
150             meta:set_string("infotext", "CNC Machine")
151             meta:set_float("technic_power_machine", 1)
152             meta:set_string("formspec", cnc_formspec)
153             local inv = meta:get_inventory()
154             inv:set_size("src", 1)
155             inv:set_size("dst", 4)
156              end,
157       can_dig = function(pos,player)
158            local meta = minetest.env:get_meta(pos);
159            local inv = meta:get_inventory()
160            if not inv:is_empty("src") or not inv:is_empty("dst") then
161               minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
162               return false
163            else
164               return true
165            end
166         end,
167       on_receive_fields = form_handler,
168    })
6463b7 169
fef948 170 -- Active state block
c1df31 171 minetest.register_node("technic:cnc_active", {
ee5c6c 172               description = "CNC Machine",
K 173               tiles       = {"technic_cnc_top_active.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
174                      "technic_cnc_side.png",       "technic_cnc_side.png",   "technic_cnc_front_active.png"},
175               paramtype2 = "facedir",
176               groups = {cracky=2,not_in_creative_inventory=1},
177               legacy_facedir_simple = true,
178               can_dig = function(pos,player)
179                        local meta = minetest.env:get_meta(pos);
180                        local inv = meta:get_inventory()
181                        if not inv:is_empty("src") or not inv:is_empty("dst") then
182                       minetest.chat_send_player(player:get_player_name(), "CNC machine cannot be removed because it is not empty");
183                       return false
184                        end
185                        return true
186                     end,
187               on_receive_fields = form_handler,
188                })
6463b7 189
fef948 190 -- Action code performing the transformation
c1df31 191 minetest.register_abm(
ee5c6c 192    { nodenames = {"technic:cnc","technic:cnc_active"},
K 193      interval = 1,
194      chance   = 1,
195      action = function(pos, node, active_object_count, active_object_count_wider)
196          local meta         = minetest.env:get_meta(pos)
197          local eu_input     = meta:get_int("LV_EU_input")
198          local state        = meta:get_int("state")
199          local next_state   = state
c1df31 200
ee5c6c 201          -- Machine information
K 202          local machine_name         = "CNC"
203          local machine_node         = "technic:cnc"
204          local machine_state_demand = { 50, 450 }
205              
206          -- Setup meta data if it does not exist. state is used as an indicator of this
207          if state == 0 then
208             meta:set_int("state", 1)
209             meta:set_int("LV_EU_demand", machine_state_demand[1])
210             meta:set_int("LV_EU_input", 0)
211             return
212          end
213              
214          -- Power off automatically if no longer connected to a switching station
215          technic.switching_station_timeout_count(pos, "LV")
216              
217          -- State machine
218          if eu_input == 0 then
219             -- Unpowered - go idle
220             hacky_swap_node(pos, machine_node)
221             meta:set_string("infotext", machine_name.." Unpowered")
222             next_state = 1
223          elseif eu_input == machine_state_demand[state] then
224             -- Powered - do the state specific actions
225                 
226             local inv   = meta:get_inventory()
227             local empty = inv:is_empty("src")
c1df31 228
ee5c6c 229             if state == 1 then
K 230                hacky_swap_node(pos, machine_node)
231                meta:set_string("infotext", machine_name.." Idle")
232
233                local result = meta:get_string("cnc_product")
234                if not empty and minetest.registered_nodes[result] ~= nil and inv:room_for_item("dst",result) then
235               next_state = 2
236                else
237               meta:set_string("cnc_product", "") -- Reset the program
238                end
239                --minetest.chat_send_player(meta:get_string("cnc_user"), "CNC machine does not know how to handle this material. Please remove it.");
240
241             elseif state == 2 then
242                hacky_swap_node(pos, machine_node.."_active")
243                meta:set_string("infotext", machine_name.." Active")
244
245                if empty then
246               next_state = 1
247                else
248               meta:set_int("src_time", meta:get_int("src_time") + 1)
249               if meta:get_int("src_time") >= 3 then -- 3 ticks per output
250                  local result = meta:get_string("cnc_product")
251                  -- check if there's room for output in "dst" list
252                  if inv:room_for_item("dst",result) then
253                 -- CNC does the transformation
254                 ------------------------------
255                 meta:set_int("src_time", 0)
256                 -- take stuff from "src" list
257                 srcstack = inv:get_stack("src", 1)
258                 srcstack:take_item()
259                 inv:set_stack("src", 1, srcstack)
260                 -- Put result in "dst" list
261                 inv:add_item("dst",result .. " " .. meta:get_int("cnc_multiplier"))
262                  else
263                 next_state = 1
264                  end
265               end
266                end
267             end
268          end
269          -- Change state?
270          if next_state ~= state then
271             meta:set_int("LV_EU_demand", machine_state_demand[next_state])
272             meta:set_int("state", next_state)
273          end
274           end
275   }) 
276
277 technic.register_LV_machine ("technic:cnc","RE")
278 technic.register_LV_machine ("technic:cnc_active","RE")
c1df31 279 -------------------------
K 280
281 -- CNC Machine Recipe
6463b7 282 -------------------------
K 283 minetest.register_craft({
284                output = 'technic:cnc',
285                recipe = {
286                   {'default:glass',              'technic:diamond_drill_head', 'default:glass'},
287                   {'technic:control_logic_unit', 'technic:motor',              'default:steel_ingot'},
288                   {'default:steel_ingot',        'default:copper_ingot',       'default:steel_ingot'},         
289                },
290             })
291 -------------------------