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;]".. |
|
40 |
"label[1,0;Choose Milling Program:]".. |
|
41 |
"image_button[1,0.5;1,1;technic_cnc_slope.png;slope; ]".. |
|
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 |
|
|
59 |
"label[1,3.5;Slim Elements half / normal height:]".. |
|
60 |
|
|
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 |
|
|
69 |
"label[0, 5.5;In:]".. |
|
70 |
"list[current_name;src;0.5,5.5;1,1;]".. |
|
71 |
"label[4, 5.5;Out:]".. |
|
72 |
"list[current_name;dst;5,5.5;4,1;]".. |
|
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, |
|
153 |
can_dig = function(pos,player) |
|
154 |
local meta = minetest.get_meta(pos); |
|
155 |
local inv = meta:get_inventory() |
|
156 |
if not inv:is_empty("src") or not inv:is_empty("dst") then |
|
157 |
minetest.chat_send_player(player:get_player_name(), |
be2f30
|
158 |
S("Machine cannot be removed because it is not empty")) |
ee0765
|
159 |
return false |
S |
160 |
else |
|
161 |
return true |
|
162 |
end |
|
163 |
end, |
|
164 |
on_receive_fields = form_handler, |
|
165 |
}) |
|
166 |
|
|
167 |
-- Active state block |
|
168 |
minetest.register_node("technic:cnc_active", { |
be2f30
|
169 |
description = S("CNC Machine"), |
ee0765
|
170 |
tiles = {"technic_cnc_top_active.png", "technic_cnc_bottom.png", "technic_cnc_side.png", |
S |
171 |
"technic_cnc_side.png", "technic_cnc_side.png", "technic_cnc_front_active.png"}, |
|
172 |
paramtype2 = "facedir", |
|
173 |
groups = {cracky=2, not_in_creative_inventory=1}, |
|
174 |
legacy_facedir_simple = true, |
|
175 |
can_dig = function(pos,player) |
|
176 |
local meta = minetest.get_meta(pos); |
|
177 |
local inv = meta:get_inventory() |
|
178 |
if not inv:is_empty("src") or not inv:is_empty("dst") then |
|
179 |
minetest.chat_send_player(player:get_player_name(), |
be2f30
|
180 |
S("Machine cannot be removed because it is not empty")) |
ee0765
|
181 |
return false |
S |
182 |
end |
|
183 |
return true |
|
184 |
end, |
|
185 |
on_receive_fields = form_handler, |
|
186 |
}) |
|
187 |
|
|
188 |
-- Action code performing the transformation |
|
189 |
minetest.register_abm({ |
|
190 |
nodenames = {"technic:cnc","technic:cnc_active"}, |
|
191 |
interval = 1, |
|
192 |
chance = 1, |
|
193 |
action = function(pos, node, active_object_count, active_object_count_wider) |
|
194 |
local meta = minetest.get_meta(pos) |
|
195 |
local inv = meta:get_inventory() |
|
196 |
local eu_input = meta:get_int("LV_EU_input") |
be2f30
|
197 |
local machine_name = S("CNC Machine") |
ee0765
|
198 |
local machine_node = "technic:cnc" |
S |
199 |
local demand = 450 |
|
200 |
|
|
201 |
-- Setup meta data if it does not exist. state is used as an indicator of this |
|
202 |
if not eu_input then |
|
203 |
meta:set_int("LV_EU_demand", demand) |
|
204 |
meta:set_int("LV_EU_input", 0) |
|
205 |
return |
|
206 |
end |
|
207 |
|
|
208 |
-- Power off automatically if no longer connected to a switching station |
|
209 |
technic.switching_station_timeout_count(pos, "LV") |
|
210 |
|
|
211 |
|
|
212 |
local result = meta:get_string("cnc_product") |
|
213 |
if inv:is_empty("src") or |
|
214 |
(not minetest.registered_nodes[result]) or |
|
215 |
(not inv:room_for_item("dst", result)) then |
|
216 |
hacky_swap_node(pos, machine_node) |
be2f30
|
217 |
meta:set_string("infotext", S("%s Idle"):format(machine_name)) |
ee0765
|
218 |
meta:set_string("cnc_product", "") |
S |
219 |
return |
|
220 |
end |
|
221 |
|
|
222 |
if eu_input < demand then |
|
223 |
hacky_swap_node(pos, machine_node) |
be2f30
|
224 |
meta:set_string("infotext", S("%s Unpowered"):format(machine_name)) |
ee0765
|
225 |
elseif eu_input >= demand then |
S |
226 |
hacky_swap_node(pos, machine_node.."_active") |
be2f30
|
227 |
meta:set_string("infotext", S("%s Active"):format(machine_name)) |
ee0765
|
228 |
meta:set_int("src_time", meta:get_int("src_time") + 1) |
S |
229 |
if meta:get_int("src_time") >= 3 then -- 3 ticks per output |
|
230 |
meta:set_int("src_time", 0) |
|
231 |
srcstack = inv:get_stack("src", 1) |
|
232 |
srcstack:take_item() |
|
233 |
inv:set_stack("src", 1, srcstack) |
|
234 |
inv:add_item("dst", result.." "..meta:get_int("cnc_multiplier")) |
|
235 |
end |
|
236 |
end |
|
237 |
meta:set_int("LV_EU_demand", demand) |
|
238 |
end |
|
239 |
}) |
|
240 |
|
|
241 |
technic.register_machine("LV", "technic:cnc", technic.receiver) |
|
242 |
technic.register_machine("LV", "technic:cnc_active", technic.receiver) |
|
243 |
|
|
244 |
------------------------- |
|
245 |
-- CNC Machine Recipe |
|
246 |
------------------------- |
|
247 |
minetest.register_craft({ |
|
248 |
output = 'technic:cnc', |
|
249 |
recipe = { |
|
250 |
{'default:glass', 'technic:diamond_drill_head', 'default:glass'}, |
|
251 |
{'technic:control_logic_unit', 'technic:motor', 'default:steel_ingot'}, |
|
252 |
{'default:steel_ingot', 'default:copper_ingot', 'default:steel_ingot'}, |
|
253 |
}, |
|
254 |
}) |
|
255 |
|