Cristiano Magro
2020-10-11 482cb191c3287ca9836a702b922b4bbd8795ddeb
commit | author | age
ee0765 1 -- Technic CNC v1.0 by kpoppel
S 2 -- Based on the NonCubic Blocks MOD v1.4 by yves_de_beck
3
4 -- Idea:
5 --   Somehow 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
d40946 10 local S = technic_cnc.getter
ee0765 11
d40946 12 local allow_metadata_inventory_put
VD 13 local allow_metadata_inventory_take
14 local allow_metadata_inventory_move
15 local can_dig
3cafe7 16 local desc_tr = S("CNC Machine")
83c649 17
d40946 18 if technic_cnc.use_technic then
VD 19     minetest.register_craft({
20         output = 'technic:cnc',
21         recipe = {
22             {'default:glass',              'technic:diamond_drill_head', 'default:glass'},
23             {'technic:control_logic_unit', 'technic:machine_casing',     'basic_materials:motor'},
24             {'technic:carbon_steel_ingot', 'technic:lv_cable',           'technic:carbon_steel_ingot'},
25         },
26     })
83c649 27
d40946 28     allow_metadata_inventory_put = technic.machine_inventory_put
VD 29     allow_metadata_inventory_take = technic.machine_inventory_take
30     allow_metadata_inventory_move = technic.machine_inventory_move
31     can_dig = technic.machine_can_dig
3cafe7 32     desc_tr = S("%s CNC Machine"):format("LV")
d40946 33 else
VD 34     minetest.register_craft({
35         output = 'technic:cnc',
36         recipe = {
37             {'default:glass',       'default:diamond',    'default:glass'},
38             {'basic_materials:ic',  'default:steelblock', 'basic_materials:motor'},
39             {'default:steel_ingot', 'default:mese',       'default:steel_ingot'},
40         },
41     })
42
43     allow_metadata_inventory_put = function(pos, listname, index, stack, player)
44         if minetest.is_protected(pos, player:get_player_name()) then
45             return 0
46         end
47         return stack:get_count()
48     end
49
50     allow_metadata_inventory_take = function(pos, listname, index, stack, player)
51         if minetest.is_protected(pos, player:get_player_name()) then
52             return 0
53         end
54         return stack:get_count()
55     end
56
57     allow_metadata_inventory_move = function(pos, from_list, from_index,
58                                     to_list, to_index, count, player)
59         if minetest.is_protected(pos, player:get_player_name()) then
60             return 0
61         end
fbc4cc 62         return count
d40946 63     end
VD 64
65     can_dig = function(pos, player)
66         if player and minetest.is_protected(pos, player:get_player_name()) then return false end
67         local meta = minetest.get_meta(pos);
68         local inv = meta:get_inventory()
69         return inv:is_empty("dst")
70             and inv:is_empty("src")
71             and default.can_interact_with_node(player, pos)
72     end
73 end
83c649 74
ee0765 75 local onesize_products = {
S 76     slope                    = 2,
77     slope_edge               = 1,
78     slope_inner_edge         = 1,
79     pyramid                  = 2,
80     spike                    = 1,
81     cylinder                 = 2,
cf7591 82     oblate_spheroid          = 1,
ee0765 83     sphere                   = 1,
S 84     stick                    = 8,
85     slope_upsdown            = 2,
86     slope_edge_upsdown       = 1,
87     slope_inner_edge_upsdown = 1,
88     cylinder_horizontal      = 2,
89     slope_lying              = 2,
90     onecurvededge            = 1,
91     twocurvededge            = 1,
92 }
93 local twosize_products = {
c068ed 94     element_straight         = 2,
ee0765 95     element_end              = 2,
S 96     element_cross            = 1,
97     element_t                = 1,
98     element_edge             = 2,
99 }
100
101 local cnc_formspec =
fb9338 102     "size[9,11;]"..
39c41a 103     "label[1,0;"..S("Choose Milling Program:").."]"..
ee0765 104     "image_button[1,0.5;1,1;technic_cnc_slope.png;slope; ]"..
S 105     "image_button[2,0.5;1,1;technic_cnc_slope_edge.png;slope_edge; ]"..
106     "image_button[3,0.5;1,1;technic_cnc_slope_inner_edge.png;slope_inner_edge; ]"..
107     "image_button[4,0.5;1,1;technic_cnc_pyramid.png;pyramid; ]"..
108     "image_button[5,0.5;1,1;technic_cnc_spike.png;spike; ]"..
109     "image_button[6,0.5;1,1;technic_cnc_cylinder.png;cylinder; ]"..
cf7591 110     "image_button[7,0.5;1,1;technic_cnc_oblate_spheroid.png;oblate_spheroid; ]"..
ee0765 111     "image_button[8,0.5;1,1;technic_cnc_stick.png;stick; ]"..
S 112
113     "image_button[1,1.5;1,1;technic_cnc_slope_upsdwn.png;slope_upsdown; ]"..
114     "image_button[2,1.5;1,1;technic_cnc_slope_edge_upsdwn.png;slope_edge_upsdown; ]"..
115     "image_button[3,1.5;1,1;technic_cnc_slope_inner_edge_upsdwn.png;slope_inner_edge_upsdown; ]"..
116     "image_button[4,1.5;1,1;technic_cnc_cylinder_horizontal.png;cylinder_horizontal; ]"..
cf7591 117     "image_button[5,1.5;1,1;technic_cnc_sphere.png;sphere; ]"..
ee0765 118
S 119     "image_button[1,2.5;1,1;technic_cnc_slope_lying.png;slope_lying; ]"..
120     "image_button[2,2.5;1,1;technic_cnc_onecurvededge.png;onecurvededge; ]"..
121     "image_button[3,2.5;1,1;technic_cnc_twocurvededge.png;twocurvededge; ]"..
122
39c41a 123     "label[1,3.5;"..S("Slim Elements half / normal height:").."]"..
ee0765 124
S 125     "image_button[1,4;1,0.5;technic_cnc_full.png;full; ]"..
126     "image_button[1,4.5;1,0.5;technic_cnc_half.png;half; ]"..
127     "image_button[2,4;1,1;technic_cnc_element_straight.png;element_straight; ]"..
128     "image_button[3,4;1,1;technic_cnc_element_end.png;element_end; ]"..
129     "image_button[4,4;1,1;technic_cnc_element_cross.png;element_cross; ]"..
130     "image_button[5,4;1,1;technic_cnc_element_t.png;element_t; ]"..
131     "image_button[6,4;1,1;technic_cnc_element_edge.png;element_edge; ]"..
132
39c41a 133     "label[0, 5.5;"..S("In:").."]"..
ee0765 134     "list[current_name;src;0.5,5.5;1,1;]"..
39c41a 135     "label[4, 5.5;"..S("Out:").."]"..
ee0765 136     "list[current_name;dst;5,5.5;4,1;]"..
S 137
54b6d9 138     "list[current_player;main;0,7;8,4;]"..
d732c8 139     "listring[current_name;dst]"..
E 140     "listring[current_player;main]"..
141     "listring[current_name;src]"..
142     "listring[current_player;main]"
ee0765 143
S 144 -- The form handler is declared here because we need it in both the inactive and active modes
145 -- in order to be able to change programs wile it is running.
146 local function form_handler(pos, formname, fields, sender)
4ecb3d 147     local meta       = minetest.get_meta(pos)
VD 148
ee0765 149     -- REGISTER MILLING PROGRAMS AND OUTPUTS:
S 150     ------------------------------------------
151     -- Program for half/full size
152     if fields["full"] then
4ecb3d 153         meta:set_int("size", 1)
ee0765 154         return
S 155     end
156
157     if fields["half"] then
4ecb3d 158         meta:set_int("size", 2)
ee0765 159         return
S 160     end
161
162     -- Resolve the node name and the number of items to make
163     local inv        = meta:get_inventory()
164     local inputstack = inv:get_stack("src", 1)
165     local inputname  = inputstack:get_name()
166     local multiplier = 0
4ecb3d 167     local size       = meta:get_int("size")
VD 168     if size < 1 then size = 1 end
169
ee0765 170     for k, _ in pairs(fields) do
S 171         -- Set a multipier for the half/full size capable blocks
172         if twosize_products[k] ~= nil then
173             multiplier = size * twosize_products[k]
174         else
175             multiplier = onesize_products[k]
176         end
177
178         if onesize_products[k] ~= nil or twosize_products[k] ~= nil then
179             meta:set_float( "cnc_multiplier", multiplier)
180             meta:set_string("cnc_user", sender:get_player_name())
181         end
182
183         if onesize_products[k] ~= nil or (twosize_products[k] ~= nil and size==2) then
184             meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k)
185             --print(inputname .. "_technic_cnc_" .. k)
186             break
187         end
188
189         if twosize_products[k] ~= nil and size==1 then
190             meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k .. "_double")
191             --print(inputname .. "_technic_cnc_" .. k .. "_double")
192             break
193         end
194     end
d40946 195
VD 196     if not technic_cnc.use_technic then
197         local result = meta:get_string("cnc_product")
198
199         if not inv:is_empty("src")
200           and minetest.registered_nodes[result]
201           and inv:room_for_item("dst", result) then
202             local srcstack = inv:get_stack("src", 1)
203             srcstack:take_item()
204             inv:set_stack("src", 1, srcstack)
205             inv:add_item("dst", result.." "..meta:get_int("cnc_multiplier"))
206         end
207     end
ee0765 208 end
S 209
563a4c 210 -- Action code performing the transformation
N 211 local run = function(pos, node)
212     local meta         = minetest.get_meta(pos)
213     local inv          = meta:get_inventory()
214     local eu_input     = meta:get_int("LV_EU_input")
3cafe7 215     local machine_name = desc_tr
563a4c 216     local machine_node = "technic:cnc"
N 217     local demand       = 450
218
219     local result = meta:get_string("cnc_product")
54b6d9 220     if inv:is_empty("src") or
563a4c 221        (not minetest.registered_nodes[result]) or
N 222        (not inv:room_for_item("dst", result)) then
223         technic.swap_node(pos, machine_node)
224         meta:set_string("infotext", S("%s Idle"):format(machine_name))
225         meta:set_string("cnc_product", "")
226         meta:set_int("LV_EU_demand", 0)
227         return
228     end
229
230     if eu_input < demand then
231         technic.swap_node(pos, machine_node)
232         meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
233     elseif eu_input >= demand then
234         technic.swap_node(pos, machine_node.."_active")
235         meta:set_string("infotext", S("%s Active"):format(machine_name))
236         meta:set_int("src_time", meta:get_int("src_time") + 1)
237         if meta:get_int("src_time") >= 3 then -- 3 ticks per output
238             meta:set_int("src_time", 0)
239             srcstack = inv:get_stack("src", 1)
240             srcstack:take_item()
241             inv:set_stack("src", 1, srcstack)
242             inv:add_item("dst", result.." "..meta:get_int("cnc_multiplier"))
243         end
244     end
245     meta:set_int("LV_EU_demand", demand)
246 end
247
ee0765 248 -- The actual block inactive state
dc0689 249 minetest.register_node(":technic:cnc", {
3cafe7 250     description = desc_tr,
ee0765 251     tiles       = {"technic_cnc_top.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
S 252                    "technic_cnc_side.png", "technic_cnc_side.png", "technic_cnc_front.png"},
83c649 253     groups = {cracky=2, technic_machine=1, technic_lv=1},
S 254     connect_sides = {"bottom", "back", "left", "right"},
ee0765 255     paramtype2  = "facedir",
S 256     legacy_facedir_simple = true,
257     on_construct = function(pos)
258         local meta = minetest.get_meta(pos)
3cafe7 259         meta:set_string("infotext", desc_tr)
ee0765 260         meta:set_float("technic_power_machine", 1)
S 261         meta:set_string("formspec", cnc_formspec)
262         local inv = meta:get_inventory()
263         inv:set_size("src", 1)
264         inv:set_size("dst", 4)
265     end,
d40946 266     can_dig = can_dig,
VD 267     allow_metadata_inventory_put = allow_metadata_inventory_put,
268     allow_metadata_inventory_take = allow_metadata_inventory_take,
269     allow_metadata_inventory_move = allow_metadata_inventory_move,
ee0765 270     on_receive_fields = form_handler,
d40946 271     technic_run = technic_cnc.use_technic and run,
ee0765 272 })
S 273
274 -- Active state block
d40946 275 if technic_cnc.use_technic then
ee0765 276
d40946 277     minetest.register_node(":technic:cnc_active", {
3cafe7 278         description = desc_tr,
d40946 279         tiles       = {"technic_cnc_top_active.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
VD 280                        "technic_cnc_side.png",       "technic_cnc_side.png",   "technic_cnc_front_active.png"},
281         groups = {cracky=2, technic_machine=1, technic_lv=1, not_in_creative_inventory=1},
282         connect_sides = {"bottom", "back", "left", "right"},
283         paramtype2 = "facedir",
284         drop = "technic:cnc",
285         legacy_facedir_simple = true,
286         can_dig = can_dig,
287         allow_metadata_inventory_put = allow_metadata_inventory_put,
288         allow_metadata_inventory_take = allow_metadata_inventory_take,
289         allow_metadata_inventory_move = allow_metadata_inventory_move,
290         on_receive_fields = form_handler,
291         technic_run = run,
292         technic_disabled_machine_name = "technic:cnc",
293     })
ee0765 294
d40946 295     technic.register_machine("LV", "technic:cnc",        technic.receiver)
VD 296     technic.register_machine("LV", "technic:cnc_active", technic.receiver)
297 else
298     minetest.register_alias("technic:cnc_active", "technic:cnc")
299 end