ShadowNinja
2013-10-30 be2f30a1a2f5b6c2aae7fd4cf8231aec2da0844d
commit | author | age
ee0765 1 -- API for the technic CNC machine
S 2 -- Again code is adapted from the NonCubic Blocks MOD v1.4 by yves_de_beck
3
be2f30 4 local S = technic.getter
S 5
6 technic.cnc = {}
ee0765 7
S 8 technic.cnc.detail_level = 16
9
10 -- REGISTER NONCUBIC FORMS, CREATE MODELS AND RECIPES:
11 ------------------------------------------------------
12 local function cnc_sphere()
13     local nodebox = {}
14     local detail = technic.cnc.detail_level
15     local sehne
16     for i = 1, detail - 1 do
17         sehne = math.sqrt(0.25 - (((i / detail) - 0.5) ^ 2))
18         nodebox[i]={-sehne, (i/detail) - 0.5, -sehne, sehne, (i/detail)+(1/detail)-0.5, sehne}
19     end
20     return nodebox
21 end
22
23 local function cnc_cylinder_horizontal()
24     local nodebox = {}
25     local detail = technic.cnc.detail_level
26     local sehne
27     for i = 1, detail - 1 do
28         sehne = math.sqrt(0.25 - (((i / detail) - 0.5) ^ 2))
29         nodebox[i]={-0.5, (i/detail)-0.5, -sehne, 0.5, (i/detail)+(1/detail)-0.5, sehne}
30     end
31     return nodebox
32 end
33
34 local function cnc_cylinder()
35     local nodebox = {}
36     local detail = technic.cnc.detail_level
37     local sehne
38     for i = 1, detail - 1 do
39         sehne = math.sqrt(0.25 - (((i / detail) - 0.5) ^ 2))
40         nodebox[i]={(i/detail) - 0.5, -0.5, -sehne, (i/detail)+(1/detail)-0.5, 0.5, sehne}
41     end
42     return nodebox
43 end
44
45 local function cnc_twocurvededge()
46     local nodebox = {}
47     local detail = technic.cnc.detail_level * 2
48     local sehne
49     for i = (detail / 2) - 1, detail - 1 do
50         sehne = math.sqrt(0.25 - (((i / detail) - 0.5) ^ 2))
51         nodebox[i]={-sehne, -0.5, -sehne, 0.5, (i/detail)+(1/detail)-0.5, 0.5}
52     end
53     return nodebox
54 end
55
56 local function cnc_onecurvededge()
57     local nodebox = {}
58     local detail = technic.cnc.detail_level * 2
59     local sehne
60     for i = (detail / 2) - 1, detail - 1 do
61         sehne = math.sqrt(0.25 - (((i / detail) - 0.5) ^ 2))
62         nodebox[i]={-0.5, -0.5, -sehne, 0.5, (i/detail)+(1/detail)-0.5, 0.5}
63     end 
64     return nodebox
65 end
66
67 local function cnc_spike()
68     local nodebox = {}
69     local detail = technic.cnc.detail_level
70     for i = 0, detail - 1 do
71         nodebox[i+1] = {(i/detail/2)-0.5, (i/detail/2)-0.5, (i/detail/2)-0.5,
72                 0.5-(i/detail/2), (i/detail)-0.5+(1/detail), 0.5-(i/detail/2)}
73     end
74     return nodebox
75 end
76
77 local function cnc_pyramid()
78     local nodebox = {}
79     local detail = technic.cnc.detail_level / 2
80     for i = 0, detail - 1 do
81         nodebox[i+1] = {(i/detail/2)-0.5, (i/detail/2)-0.5, (i/detail/2)-0.5, 0.5-(i/detail/2), (i/detail/2)-0.5+(1/detail), 0.5-(i/detail/2)}
82     end
83     return nodebox
84 end
85
86 local function cnc_slope_inner_edge_upsdown()
87     local nodebox = {}
88     local detail = technic.cnc.detail_level
89     for i = 0, detail-1 do
90         nodebox[i+1] = {0.5-(i/detail)-(1/detail), (i/detail)-0.5, -0.5, 0.5, (i/detail)-0.5+(1/detail), 0.5}
91         nodebox[i+detail+1] = {-0.5, (i/detail)-0.5, 0.5-(i/detail)-(1/detail), 0.5, (i/detail)-0.5+(1/detail), 0.5}
92     end
93     return nodebox
94 end
95
96 local function cnc_slope_edge_upsdown()
97     local nodebox = {}
98     local detail = technic.cnc.detail_level
99     for i = 0, detail-1 do
100         nodebox[i+1] = {(-1*(i/detail))+0.5-(1/detail), (i/detail)-0.5, (-1*(i/detail))+0.5-(1/detail), 0.5, (i/detail)-0.5+(1/detail), 0.5}
101     end
102     return nodebox
103 end
104
105 local function cnc_slope_inner_edge()
106     local nodebox = {}
107     local detail = technic.cnc.detail_level
108     for i = 0, detail-1 do
109         nodebox[i+1] = {(i/detail)-0.5, -0.5, -0.5, 0.5, (i/detail)-0.5+(1/detail), 0.5}
110         nodebox[i+detail+1] = {-0.5, -0.5, (i/detail)-0.5, 0.5, (i/detail)-0.5+(1/detail), 0.5}
111     end
112     return nodebox
113 end
114
115 local function cnc_slope_edge()
116     local nodebox = {}
117     local detail = technic.cnc.detail_level
118     for i = 0, detail-1 do
119         nodebox[i+1] = {(i/detail)-0.5, -0.5, (i/detail)-0.5, 0.5, (i/detail)-0.5+(1/detail), 0.5}
120     end
121     return nodebox
122 end
123
124 local function cnc_slope_upsdown()
125     local nodebox = {}
126     local detail = technic.cnc.detail_level
127     for i = 0, detail-1 do
128         nodebox[i+1] = {-0.5, (i/detail)-0.5, (-1*(i/detail))+0.5-(1/detail), 0.5, (i/detail)-0.5+(1/detail), 0.5}
129     end
130     return nodebox
131 end
132
133 local function cnc_slope_lying()
134     local nodebox = {}
135     local detail = technic.cnc.detail_level
136     for i = 0, detail-1 do
137         nodebox[i+1] = {(i/detail)-0.5, -0.5, (i/detail)-0.5, (i/detail)-0.5+(1/detail), 0.5 , 0.5}
138     end
139     return nodebox
140 end
141
142 local function cnc_slope()
143     local nodebox = {}
144     local detail = technic.cnc.detail_level
145     for i = 0, detail-1 do
146         nodebox[i+1] = {-0.5, (i/detail)-0.5, (i/detail)-0.5, 0.5, (i/detail)-0.5+(1/detail), 0.5}
147     end
148     return nodebox
149 end
150
151 -- Define slope boxes for the various nodes
152 -------------------------------------------
153 technic.cnc.programs = {
154     {suffix  = "technic_cnc_stick",
155     nodebox = {-0.15, -0.5, -0.15, 0.15, 0.5, 0.15},
be2f30 156     desc    = S("Stick")},
ee0765 157
S 158     {suffix  = "technic_cnc_element_end_double",
159     nodebox = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.5},
be2f30 160     desc    = S("Element End Double")},
ee0765 161
S 162     {suffix  = "technic_cnc_element_cross_double",
163     nodebox = {
164         {0.3, -0.5, -0.3, 0.5, 0.5, 0.3},
165         {-0.3, -0.5, -0.5, 0.3, 0.5, 0.5},
166         {-0.5, -0.5, -0.3, -0.3, 0.5, 0.3}},
be2f30 167     desc    = S("Element Cross Double")},
ee0765 168
S 169     {suffix  = "technic_cnc_element_t_double",
170     nodebox = {
171         {-0.3, -0.5, -0.5, 0.3, 0.5, 0.3},
172         {-0.5, -0.5, -0.3, -0.3, 0.5, 0.3},
173         {0.3, -0.5, -0.3, 0.5, 0.5, 0.3}},
be2f30 174     desc    = S("Element T Double")},
ee0765 175
S 176     {suffix  = "technic_cnc_element_edge_double",
177     nodebox = {
178         {-0.3, -0.5, -0.5, 0.3, 0.5, 0.3},
179         {-0.5, -0.5, -0.3, -0.3, 0.5, 0.3}},
be2f30 180     desc    = S("Element Edge Double")},
ee0765 181
S 182     {suffix  = "technic_cnc_element_straight_double",
183     nodebox = {-0.3, -0.5, -0.5, 0.3, 0.5, 0.5},
be2f30 184     desc    = S("Element Straight Double")},
ee0765 185
S 186     {suffix  = "technic_cnc_element_end",
187     nodebox = {-0.3, -0.5, -0.3, 0.3, 0, 0.5},
be2f30 188     desc    = S("Element End")},
ee0765 189
S 190     {suffix  = "technic_cnc_element_cross",
191     nodebox = {
192         {0.3, -0.5, -0.3, 0.5, 0, 0.3},
193         {-0.3, -0.5, -0.5, 0.3, 0, 0.5},
194         {-0.5, -0.5, -0.3, -0.3, 0, 0.3}},
be2f30 195     desc    = S("Element Cross")},
ee0765 196
S 197     {suffix  = "technic_cnc_element_t",
198     nodebox = {
199         {-0.3, -0.5, -0.5, 0.3, 0, 0.3},
200         {-0.5, -0.5, -0.3, -0.3, 0, 0.3},
201         {0.3, -0.5, -0.3, 0.5, 0, 0.3}},
be2f30 202     desc    = S("Element T")},
ee0765 203
S 204     {suffix  = "technic_cnc_element_edge",
205     nodebox = {
206         {-0.3, -0.5, -0.5, 0.3, 0, 0.3},
207         {-0.5, -0.5, -0.3, -0.3, 0, 0.3}},
be2f30 208     desc    = S("Element Edge")},
ee0765 209
S 210     {suffix  = "technic_cnc_element_straight",
211     nodebox = {-0.3, -0.5, -0.5, 0.3, 0, 0.5},
be2f30 212     desc    = S("Element Straight")},
ee0765 213
S 214     {suffix  = "technic_cnc_sphere",
215     nodebox = cnc_sphere(),
be2f30 216     desc    = S("Sphere")},
ee0765 217
S 218     {suffix  = "technic_cnc_cylinder_horizontal",
219     nodebox = cnc_cylinder_horizontal(),
be2f30 220     desc    = S("Horizontal Cylinder")},
ee0765 221
S 222     {suffix  = "technic_cnc_cylinder",
223     nodebox = cnc_cylinder(),
be2f30 224     desc    = S("Cylinder")},
ee0765 225
S 226     {suffix  = "technic_cnc_twocurvededge",
227     nodebox = cnc_twocurvededge(),
be2f30 228     desc    = S("Two Curved Edge Block")},
ee0765 229
S 230     {suffix  = "technic_cnc_onecurvededge",
231     nodebox = cnc_onecurvededge(),
be2f30 232     desc    = S("One Curved Edge Block")},
ee0765 233
S 234     {suffix  = "technic_cnc_spike",
235     nodebox = cnc_spike(),
be2f30 236     desc    = S("Spike")},
ee0765 237
S 238     {suffix  = "technic_cnc_pyramid",
239     nodebox = cnc_pyramid(),
be2f30 240     desc    = S("Pyramid")},
ee0765 241
S 242     {suffix  = "technic_cnc_slope_inner_edge_upsdown",
243     nodebox = cnc_slope_inner_edge_upsdown(),
be2f30 244     desc    = S("Slope Upside Down Inner Edge")},
ee0765 245
S 246     {suffix  = "technic_cnc_slope_edge_upsdown",
247     nodebox = cnc_slope_edge_upsdown(),
be2f30 248     desc    = S("Slope Upside Down Edge")},
ee0765 249
S 250     {suffix  = "technic_cnc_slope_inner_edge",
251     nodebox = cnc_slope_inner_edge(),
be2f30 252     desc    = S("Slope Inner Edge")},
ee0765 253
S 254     {suffix  = "technic_cnc_slope_edge",
255     nodebox = cnc_slope_edge(),
be2f30 256     desc    = S("Slope Edge")},
ee0765 257
S 258     {suffix  = "technic_cnc_slope_upsdown",
259     nodebox = cnc_slope_upsdown(),
be2f30 260     desc    = S("Slope Upside Down")},
ee0765 261
S 262     {suffix  = "technic_cnc_slope_lying",
263     nodebox = cnc_slope_lying(),
be2f30 264     desc    = S("Slope Lying")},
ee0765 265
S 266     {suffix  = "technic_cnc_slope",
267     nodebox = cnc_slope(),
be2f30 268     desc    = S("Slope")},
ee0765 269 }
S 270
271 -- Allow disabling certain programs for some node. Default is allowing all types for all nodes
272 technic.cnc.programs_disable = {
273     -- ["default:brick"] = {"technic_cnc_stick"}, -- Example: Disallow the stick for brick
274     -- ...
275     ["default:dirt"] = {"technic_cnc_sphere", "technic_cnc_slope_upsdown", "technic_cnc_edge",
276                         "technic_cnc_inner_edge", "technic_cnc_slope_edge_upsdown",
277                         "technic_cnc_slope_inner_edge_upsdown", "technic_cnc_stick",
278                         "technic_cnc_cylinder_horizontal"}
279 }
280
281 -- Generic function for registering all the different node types
282 function technic.cnc.register_program(recipeitem, suffix, nodebox, groups, images, description)
283     minetest.register_node(":"..recipeitem.."_"..suffix, {
284         description   = description,
285         drawtype      = "nodebox",
286         tiles         = images,
287         paramtype     = "light",
288         paramtype2    = "facedir",
289         walkable      = true,
290         node_box = {
291             type  = "fixed",
292             fixed = nodebox
293         },
294         groups        = groups,
295     })
296 end
297
298 -- function to iterate over all the programs the CNC machine knows
299 function technic.cnc.register_all(recipeitem, groups, images, description)
300     for _, data in ipairs(technic.cnc.programs) do
301         -- Disable node creation for disabled node types for some material
302         local do_register = true
303         if technic.cnc.programs_disable[recipeitem] ~= nil then
304             for __, disable in ipairs(technic.cnc.programs_disable[recipeitem]) do
305                 if disable == data.suffix then
306                     do_register = false
307                 end
308             end
309         end
310         -- Create the node if it passes the test
311         if do_register then
312             technic.cnc.register_program(recipeitem, data.suffix, data.nodebox, groups, images, description.." "..data.desc)
313         end
314     end
315 end
316
317
318 -- REGISTER NEW TECHNIC_CNC_API's PART 2: technic.cnc..register_element_end(subname, recipeitem, groups, images, desc_element_xyz)
319 -----------------------------------------------------------------------------------------------------------------------
320 function technic.cnc.register_slope_edge_etc(recipeitem, groups, images, desc_slope, desc_slope_lying, desc_slope_upsdown, desc_slope_edge, desc_slope_inner_edge, desc_slope_upsdwn_edge, desc_slope_upsdwn_inner_edge, desc_pyramid, desc_spike, desc_onecurvededge, desc_twocurvededge, desc_cylinder, desc_cylinder_horizontal, desc_sphere, desc_element_straight, desc_element_edge, desc_element_t, desc_element_cross, desc_element_end)
321
322          technic.cnc.register_slope(recipeitem, groups, images, desc_slope)
323          technic.cnc.register_slope_lying(recipeitem, groups, images, desc_slope_lying)
324          technic.cnc.register_slope_upsdown(recipeitem, groups, images, desc_slope_upsdown)
325          technic.cnc.register_slope_edge(recipeitem, groups, images, desc_slope_edge)
326          technic.cnc.register_slope_inner_edge(recipeitem, groups, images, desc_slope_inner_edge)
327          technic.cnc.register_slope_edge_upsdown(recipeitem, groups, images, desc_slope_upsdwn_edge)
328          technic.cnc.register_slope_inner_edge_upsdown(recipeitem, groups, images, desc_slope_upsdwn_inner_edge)
329          technic.cnc.register_pyramid(recipeitem, groups, images, desc_pyramid)
330          technic.cnc.register_spike(recipeitem, groups, images, desc_spike)
331          technic.cnc.register_onecurvededge(recipeitem, groups, images, desc_onecurvededge)
332          technic.cnc.register_twocurvededge(recipeitem, groups, images, desc_twocurvededge)
333          technic.cnc.register_cylinder(recipeitem, groups, images, desc_cylinder)
334          technic.cnc.register_cylinder_horizontal(recipeitem, groups, images, desc_cylinder_horizontal)
335          technic.cnc.register_sphere(recipeitem, groups, images, desc_sphere)
336          technic.cnc.register_element_straight(recipeitem, groups, images, desc_element_straight)
337          technic.cnc.register_element_edge(recipeitem, groups, images, desc_element_edge)
338          technic.cnc.register_element_t(recipeitem, groups, images, desc_element_t)
339          technic.cnc.register_element_cross(recipeitem, groups, images, desc_element_cross)
340          technic.cnc.register_element_end(recipeitem, groups, images, desc_element_end)
341 end
342
343 -- REGISTER STICKS: noncubic.register_xyz(recipeitem, groups, images, desc_element_xyz)
344 ------------------------------------------------------------------------------------------------------------
345 function technic.cnc.register_stick_etc(recipeitem, groups, images, desc_stick)
346          technic.cnc.register_stick(recipeitem, groups, images, desc_stick)
347 end
348
349 function technic.cnc.register_elements(recipeitem, groups, images, desc_element_straight_double, desc_element_edge_double, desc_element_t_double, desc_element_cross_double, desc_element_end_double)
350          technic.cnc.register_element_straight_double(recipeitem, groups, images, desc_element_straight_double)
351          technic.cnc.register_element_edge_double(recipeitem, groups, images, desc_element_edge_double)
352          technic.cnc.register_element_t_double(recipeitem, groups, images, desc_element_t_double)
353          technic.cnc.register_element_cross_double(recipeitem, groups, images, desc_element_cross_double)
354          technic.cnc.register_element_end_double(recipeitem, groups, images, desc_element_end_double)
355 end
356