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