Zefram
2014-05-16 68b7bcc28e39bdf0926072b834eeeeec0ee6c721
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
128 -- The actual block inactive state
129 minetest.register_node("technic:cnc", {
be2f30 130     description = S("CNC Machine"),
ee0765 131     tiles       = {"technic_cnc_top.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
S 132                    "technic_cnc_side.png", "technic_cnc_side.png", "technic_cnc_front.png"},
133     drawtype    = "nodebox",
134     paramtype   = "light",
135     paramtype2  = "facedir",
136     node_box    = {
137         type  = "fixed",
138         fixed = {
139             {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
140         },
141     },
142     groups = {cracky=2},
143     legacy_facedir_simple = true,
144     on_construct = function(pos)
145         local meta = minetest.get_meta(pos)
be2f30 146         meta:set_string("infotext", S("CNC Machine"))
ee0765 147         meta:set_float("technic_power_machine", 1)
S 148         meta:set_string("formspec", cnc_formspec)
149         local inv = meta:get_inventory()
150         inv:set_size("src", 1)
151         inv:set_size("dst", 4)
152     end,
0809dd 153     can_dig = technic.machine_can_dig,
S 154     allow_metadata_inventory_put = technic.machine_inventory_put,
155     allow_metadata_inventory_take = technic.machine_inventory_take,
156     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 157     on_receive_fields = form_handler,
S 158 })
159
160 -- Active state block
161 minetest.register_node("technic:cnc_active", {
be2f30 162     description = S("CNC Machine"),
ee0765 163     tiles       = {"technic_cnc_top_active.png", "technic_cnc_bottom.png", "technic_cnc_side.png",
S 164                    "technic_cnc_side.png",       "technic_cnc_side.png",   "technic_cnc_front_active.png"},
165     paramtype2 = "facedir",
f50417 166     drop = "technic:cnc",
ee0765 167     groups = {cracky=2, not_in_creative_inventory=1},
S 168     legacy_facedir_simple = true,
0809dd 169     can_dig = technic.machine_can_dig,
S 170     allow_metadata_inventory_put = technic.machine_inventory_put,
171     allow_metadata_inventory_take = technic.machine_inventory_take,
172     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 173     on_receive_fields = form_handler,
S 174 })
175
176 -- Action code performing the transformation
177 minetest.register_abm({
178     nodenames = {"technic:cnc","technic:cnc_active"},
179     interval = 1,
180     chance   = 1,
181     action = function(pos, node, active_object_count, active_object_count_wider)
182         local meta         = minetest.get_meta(pos)
183         local inv          = meta:get_inventory()
184         local eu_input     = meta:get_int("LV_EU_input")
be2f30 185         local machine_name = S("CNC Machine")
ee0765 186         local machine_node = "technic:cnc"
S 187         local demand       = 450
188
189         -- Power off automatically if no longer connected to a switching station
190         technic.switching_station_timeout_count(pos, "LV")
191
192         local result = meta:get_string("cnc_product")
193         if inv:is_empty("src") or 
194            (not minetest.registered_nodes[result]) or
195            (not inv:room_for_item("dst", result)) then
f3d8b4 196             technic.swap_node(pos, machine_node)
be2f30 197             meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 198             meta:set_string("cnc_product", "")
821fba 199             meta:set_int("LV_EU_demand", 0)
ee0765 200             return
S 201         end
202
203         if eu_input < demand then
f3d8b4 204             technic.swap_node(pos, machine_node)
be2f30 205             meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
ee0765 206         elseif eu_input >= demand then
f3d8b4 207             technic.swap_node(pos, machine_node.."_active")
be2f30 208             meta:set_string("infotext", S("%s Active"):format(machine_name))
ee0765 209             meta:set_int("src_time", meta:get_int("src_time") + 1)
S 210             if meta:get_int("src_time") >= 3 then -- 3 ticks per output
211                 meta:set_int("src_time", 0)
212                 srcstack = inv:get_stack("src", 1)
213                 srcstack:take_item()
214                 inv:set_stack("src", 1, srcstack)
215                 inv:add_item("dst", result.." "..meta:get_int("cnc_multiplier"))
216             end
217         end
218         meta:set_int("LV_EU_demand", demand)
219     end
220 }) 
221
222 technic.register_machine("LV", "technic:cnc",        technic.receiver)
223 technic.register_machine("LV", "technic:cnc_active", technic.receiver)
224
225 -------------------------
226 -- CNC Machine Recipe
227 -------------------------
228 minetest.register_craft({
229     output = 'technic:cnc',
230     recipe = {
231         {'default:glass',              'technic:diamond_drill_head', 'default:glass'},
68b7bc 232         {'technic:control_logic_unit', 'technic:motor',              'technic:carbon_steel_ingot'},
Z 233         {'technic:carbon_steel_ingot', 'default:copper_ingot',       'technic:carbon_steel_ingot'},         
ee0765 234     },
S 235 })
236