Zefram
2014-07-30 12d0c6522bbca906910aae0321cbaa7eb48db8c2
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
be2f30 10 local S = technic.getter
ee0765 11
S 12 local shape = {}
13 local onesize_products = {
14     slope                    = 2,
15     slope_edge               = 1,
16     slope_inner_edge         = 1,
17     pyramid                  = 2,
18     spike                    = 1,
19     cylinder                 = 2,
20     sphere                   = 1,
21     stick                    = 8,
22     slope_upsdown            = 2,
23     slope_edge_upsdown       = 1,
24     slope_inner_edge_upsdown = 1,
25     cylinder_horizontal      = 2,
26     slope_lying              = 2,
27     onecurvededge            = 1,
28     twocurvededge            = 1,
29 }
30 local twosize_products = {
31     element_straight         = 4,
32     element_end              = 2,
33     element_cross            = 1,
34     element_t                = 1,
35     element_edge             = 2,
36 }
37
38 local cnc_formspec =
39     "invsize[9,11;]"..
39c41a 40     "label[1,0;"..S("Choose Milling Program:").."]"..
ee0765 41     "image_button[1,0.5;1,1;technic_cnc_slope.png;slope; ]"..
S 42     "image_button[2,0.5;1,1;technic_cnc_slope_edge.png;slope_edge; ]"..
43     "image_button[3,0.5;1,1;technic_cnc_slope_inner_edge.png;slope_inner_edge; ]"..
44     "image_button[4,0.5;1,1;technic_cnc_pyramid.png;pyramid; ]"..
45     "image_button[5,0.5;1,1;technic_cnc_spike.png;spike; ]"..
46     "image_button[6,0.5;1,1;technic_cnc_cylinder.png;cylinder; ]"..
47     "image_button[7,0.5;1,1;technic_cnc_sphere.png;sphere; ]"..
48     "image_button[8,0.5;1,1;technic_cnc_stick.png;stick; ]"..
49
50     "image_button[1,1.5;1,1;technic_cnc_slope_upsdwn.png;slope_upsdown; ]"..
51     "image_button[2,1.5;1,1;technic_cnc_slope_edge_upsdwn.png;slope_edge_upsdown; ]"..
52     "image_button[3,1.5;1,1;technic_cnc_slope_inner_edge_upsdwn.png;slope_inner_edge_upsdown; ]"..
53     "image_button[4,1.5;1,1;technic_cnc_cylinder_horizontal.png;cylinder_horizontal; ]"..
54
55     "image_button[1,2.5;1,1;technic_cnc_slope_lying.png;slope_lying; ]"..
56     "image_button[2,2.5;1,1;technic_cnc_onecurvededge.png;onecurvededge; ]"..
57     "image_button[3,2.5;1,1;technic_cnc_twocurvededge.png;twocurvededge; ]"..
58
39c41a 59     "label[1,3.5;"..S("Slim Elements half / normal height:").."]"..
ee0765 60
S 61     "image_button[1,4;1,0.5;technic_cnc_full.png;full; ]"..
62     "image_button[1,4.5;1,0.5;technic_cnc_half.png;half; ]"..
63     "image_button[2,4;1,1;technic_cnc_element_straight.png;element_straight; ]"..
64     "image_button[3,4;1,1;technic_cnc_element_end.png;element_end; ]"..
65     "image_button[4,4;1,1;technic_cnc_element_cross.png;element_cross; ]"..
66     "image_button[5,4;1,1;technic_cnc_element_t.png;element_t; ]"..
67     "image_button[6,4;1,1;technic_cnc_element_edge.png;element_edge; ]"..
68
39c41a 69     "label[0, 5.5;"..S("In:").."]"..
ee0765 70     "list[current_name;src;0.5,5.5;1,1;]"..
39c41a 71     "label[4, 5.5;"..S("Out:").."]"..
ee0765 72     "list[current_name;dst;5,5.5;4,1;]"..
S 73
74     "list[current_player;main;0,7;8,4;]"
75
76 local size = 1;
77
78 -- The form handler is declared here because we need it in both the inactive and active modes
79 -- in order to be able to change programs wile it is running.
80 local function form_handler(pos, formname, fields, sender)
81     -- REGISTER MILLING PROGRAMS AND OUTPUTS:
82     ------------------------------------------
83     -- Program for half/full size
84     if fields["full"] then
85         size = 1
86         return
87     end
88
89     if fields["half"] then
90         size = 2
91         return
92     end
93
94     -- Resolve the node name and the number of items to make
95     local meta       = minetest.get_meta(pos)
96     local inv        = meta:get_inventory()
97     local inputstack = inv:get_stack("src", 1)
98     local inputname  = inputstack:get_name()
99     local multiplier = 0
100     for k, _ in pairs(fields) do
101         -- Set a multipier for the half/full size capable blocks
102         if twosize_products[k] ~= nil then
103             multiplier = size * twosize_products[k]
104         else
105             multiplier = onesize_products[k]
106         end
107
108         if onesize_products[k] ~= nil or twosize_products[k] ~= nil then
109             meta:set_float( "cnc_multiplier", multiplier)
110             meta:set_string("cnc_user", sender:get_player_name())
111         end
112
113         if onesize_products[k] ~= nil or (twosize_products[k] ~= nil and size==2) then
114             meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k)
115             --print(inputname .. "_technic_cnc_" .. k)
116             break
117         end
118
119         if twosize_products[k] ~= nil and size==1 then
120             meta:set_string("cnc_product",  inputname .. "_technic_cnc_" .. k .. "_double")
121             --print(inputname .. "_technic_cnc_" .. k .. "_double")
122             break
123         end
124     end
125     return
126 end
127
563a4c 128 -- Action code performing the transformation
N 129 local run = function(pos, node)
130     local meta         = minetest.get_meta(pos)
131     local inv          = meta:get_inventory()
132     local eu_input     = meta:get_int("LV_EU_input")
133     local machine_name = S("%s CNC Machine"):format("LV")
134     local machine_node = "technic:cnc"
135     local demand       = 450
136
137     local result = meta:get_string("cnc_product")
138     if inv:is_empty("src") or 
139        (not minetest.registered_nodes[result]) or
140        (not inv:room_for_item("dst", result)) then
141         technic.swap_node(pos, machine_node)
142         meta:set_string("infotext", S("%s Idle"):format(machine_name))
143         meta:set_string("cnc_product", "")
144         meta:set_int("LV_EU_demand", 0)
145         return
146     end
147
148     if eu_input < demand then
149         technic.swap_node(pos, machine_node)
150         meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
151     elseif eu_input >= demand then
152         technic.swap_node(pos, machine_node.."_active")
153         meta:set_string("infotext", S("%s Active"):format(machine_name))
154         meta:set_int("src_time", meta:get_int("src_time") + 1)
155         if meta:get_int("src_time") >= 3 then -- 3 ticks per output
156             meta:set_int("src_time", 0)
157             srcstack = inv:get_stack("src", 1)
158             srcstack:take_item()
159             inv:set_stack("src", 1, srcstack)
160             inv:add_item("dst", result.." "..meta:get_int("cnc_multiplier"))
161         end
162     end
163     meta:set_int("LV_EU_demand", demand)
164 end
165
ee0765 166 -- The actual block inactive state
S 167 minetest.register_node("technic:cnc", {
7c4b70 168     description = S("%s CNC Machine"):format("LV"),
ee0765 169     tiles       = {"technic_cnc_top.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
S 170                    "technic_cnc_side.png", "technic_cnc_side.png", "technic_cnc_front.png"},
171     drawtype    = "nodebox",
172     paramtype   = "light",
173     paramtype2  = "facedir",
174     node_box    = {
175         type  = "fixed",
176         fixed = {
177             {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
178         },
179     },
563a4c 180     groups = {cracky=2, technic_machine=1},
ee0765 181     legacy_facedir_simple = true,
S 182     on_construct = function(pos)
183         local meta = minetest.get_meta(pos)
7c4b70 184         meta:set_string("infotext", S("%s CNC Machine"):format("LV"))
ee0765 185         meta:set_float("technic_power_machine", 1)
S 186         meta:set_string("formspec", cnc_formspec)
187         local inv = meta:get_inventory()
188         inv:set_size("src", 1)
189         inv:set_size("dst", 4)
190     end,
0809dd 191     can_dig = technic.machine_can_dig,
S 192     allow_metadata_inventory_put = technic.machine_inventory_put,
193     allow_metadata_inventory_take = technic.machine_inventory_take,
194     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 195     on_receive_fields = form_handler,
563a4c 196     technic_run = run,
ee0765 197 })
S 198
199 -- Active state block
200 minetest.register_node("technic:cnc_active", {
7c4b70 201     description = S("%s CNC Machine"):format("LV"),
ee0765 202     tiles       = {"technic_cnc_top_active.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
S 203                    "technic_cnc_side.png",       "technic_cnc_side.png",   "technic_cnc_front_active.png"},
204     paramtype2 = "facedir",
f50417 205     drop = "technic:cnc",
563a4c 206     groups = {cracky=2, technic_machine=1, not_in_creative_inventory=1},
ee0765 207     legacy_facedir_simple = true,
0809dd 208     can_dig = technic.machine_can_dig,
S 209     allow_metadata_inventory_put = technic.machine_inventory_put,
210     allow_metadata_inventory_take = technic.machine_inventory_take,
211     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 212     on_receive_fields = form_handler,
563a4c 213     technic_run = run,
N 214     technic_disabled_machine_name = "technic:cnc",
ee0765 215 })
S 216
217 technic.register_machine("LV", "technic:cnc",        technic.receiver)
218 technic.register_machine("LV", "technic:cnc_active", technic.receiver)
219
220 -------------------------
221 -- CNC Machine Recipe
222 -------------------------
223 minetest.register_craft({
224     output = 'technic:cnc',
225     recipe = {
226         {'default:glass',              'technic:diamond_drill_head', 'default:glass'},
e8a5a6 227         {'technic:control_logic_unit', 'technic:machine_casing',     'technic:motor'},
68ea0a 228         {'technic:carbon_steel_ingot', 'technic:lv_cable0',          'technic:carbon_steel_ingot'},         
ee0765 229     },
S 230 })
231