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