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