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