ShadowNinja
2013-10-30 be2f30a1a2f5b6c2aae7fd4cf8231aec2da0844d
commit | author | age
be2f30 1
S 2 local S = technic.getter
3
ee0765 4 -- Register alloy recipes
S 5 technic.alloy_recipes = {}
6
7 -- Register recipe in a table
8 technic.register_alloy_recipe = function(metal1, count1, metal2, count2, result, count3)
9     in1 = {
10         name  = metal1,
11         count = count1,
12     }
13     in2 = {
14         name  = metal2,
15         count = count2,
16     }
17     -- Sort the inputs alphebetically
18     if in1.name > in2.name then
19         local temp = in1
20         in1 = in2
21         in2 = temp
22     end
23     technic.alloy_recipes[in1.name.." "..in2.name] = {
24         input = {in1, in2},
25         output = {
26             name = result,
27             count = count3,
28         },
29     }
30     if unified_inventory then
31         unified_inventory.register_craft({
32             type = "alloy",
33             output = result.." "..count3,
34             items = {metal1.." "..count1, metal2.." "..count2},
35             width = 2,
36         })
37     end
38 end
39
40 -- Retrieve a recipe given the input metals.
41 function technic.get_alloy_recipe(stack1, stack2)
42     -- Sort the stacks alphebetically
43     if stack1:get_name() > stack2:get_name() then
44         local temp = stack1
45         stack1 = stack2
46         stack2 = temp
47     end
48     for _, recipe in pairs(technic.alloy_recipes) do
49         if recipe.input[1].name == stack1:get_name() and
50            recipe.input[2].name == stack2:get_name() and
51            stack1:get_count() >= recipe.input[1].count and
52            stack2:get_count() >= recipe.input[2].count then
53             return recipe
54         end
55     end
56 end
57
58 technic.register_alloy_recipe("technic:copper_dust",   3, "technic:tin_dust",       1, "technic:bronze_dust",           4)
59 technic.register_alloy_recipe("default:copper_ingot",  3, "moreores:tin_ingot",     1, "moreores:bronze_ingot",         4)
60 technic.register_alloy_recipe("technic:iron_dust",     3, "technic:chromium_dust",  1, "technic:stainless_steel_dust",  4)
61 technic.register_alloy_recipe("default:steel_ingot",   3, "technic:chromium_ingot", 1, "technic:stainless_steel_ingot", 4)
62 technic.register_alloy_recipe("technic:copper_dust",   2, "technic:zinc_dust",      1, "technic:brass_dust",            3)
63 technic.register_alloy_recipe("default:copper_ingot",  2, "technic:zinc_ingot",     1, "technic:brass_ingot",           3)
64 technic.register_alloy_recipe("default:sand",          2, "technic:coal_dust",      2, "technic:silicon_wafer",         1)
65 technic.register_alloy_recipe("technic:silicon_wafer", 1, "technic:gold_dust",      1, "technic:doped_silicon_wafer",   1)
66
67
68 function technic.register_alloy_furnace(data)
69     local tier = data.tier
70     local ltier = string.lower(tier)
71
72     local tube_side_texture = data.tube and "technic_"..ltier.."_alloy_furnace_side_tube.png"
73             or "technic_"..ltier.."_alloy_furnace_side.png"
74     local groups = {cracky=2}
75     local active_groups = {cracky=2, not_in_creative_inventory=1}
76     if data.tube then
77         groups.tubedevice = 1
78         groups.tubedevice_receiver = 1
79         active_groups.tubedevice = 1
80         active_groups.tubedevice_receiver = 1
81     end
82
83     local formspec =
84         "invsize[8,10;]"..
be2f30 85         "label[0,0;"..S("%s Alloy Furnace"):format(tier).."]"..
ee0765 86         "list[current_name;src;3,1;1,2;]"..
S 87         "list[current_name;dst;5,1;2,2;]"..
88         "list[current_player;main;0,6;8,4;]"
89     if data.upgrade then
90         formspec = formspec..
91             "list[current_name;upgrade1;1,4;1,1;]"..
92             "list[current_name;upgrade2;2,4;1,1;]"..
93             "label[1,5;Upgrade Slots]"
94     end
95
96     data.formspec = formspec
97
98     local tube = {
99         insert_object = function(pos, node, stack, direction)
100             local meta = minetest.get_meta(pos)
101             local inv = meta:get_inventory()
102             return inv:add_item("src", stack)
103         end,
104         can_insert = function(pos, node, stack, direction)
105             local meta = minetest.get_meta(pos)
106             local inv = meta:get_inventory()
107             return inv:room_for_item("src", stack)
108         end,
109         connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
110     }
111
112     minetest.register_node("technic:"..ltier.."_alloy_furnace", {
be2f30 113         description = S("%s Alloy Furnace"):format(tier),
ee0765 114         tiles = {"technic_"..ltier.."_alloy_furnace_top.png",
S 115                  "technic_"..ltier.."_alloy_furnace_bottom.png",
116              tube_side_texture,
117                  tube_side_texture,
118              "technic_"..ltier.."_alloy_furnace_side.png",
119                  "technic_"..ltier.."_alloy_furnace_front.png"},
120         paramtype2 = "facedir",
121         groups = groups,
122         tube = tube,
123         technic = data,
124         legacy_facedir_simple = true,
125         sounds = default.node_sound_stone_defaults(),
126         on_construct = function(pos)
127             local meta = minetest.get_meta(pos)
128             local name = minetest.get_node(pos).name
129             local data = minetest.registered_nodes[name].technic
130
131
be2f30 132             meta:set_string("infotext", S("%s Alloy Furnace"):format(data.tier))
ee0765 133             meta:set_string("formspec", data.formspec)
S 134             meta:set_int("tube_time",  0)
135             local inv = meta:get_inventory()
136             inv:set_size("src", 2)
137             inv:set_size("dst", 4)
138             inv:set_size("upgrade1", 1)
139             inv:set_size("upgrade2", 1)
140         end,
141         can_dig = function(pos, player)
142             local meta = minetest.get_meta(pos);
143             local inv = meta:get_inventory()
144             if not inv:is_empty("src") or not inv:is_empty("dst") or
145                not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
146                 minetest.chat_send_player(player:get_player_name(),
be2f30 147                     S("Machine cannot be removed because it is not empty"))
ee0765 148                 return false
S 149             else
150                 return true
151             end
152         end,
153     })
154
155     minetest.register_node("technic:"..ltier.."_alloy_furnace_active",{
be2f30 156         description = S(tier.." Alloy Furnace"),
ee0765 157         tiles = {"technic_"..ltier.."_alloy_furnace_top.png",
S 158                  "technic_"..ltier.."_alloy_furnace_bottom.png",
159              tube_side_texture,
160                  tube_side_texture,
161              "technic_"..ltier.."_alloy_furnace_side.png",
162                  "technic_"..ltier.."_alloy_furnace_front_active.png"},
163         paramtype2 = "facedir",
164         light_source = 8,
165         drop = "technic:"..ltier.."_alloy_furnace",
166         groups = active_groups,
167         tube = tube,
168         technic = data,
169         legacy_facedir_simple = true,
170         sounds = default.node_sound_stone_defaults(),
171         can_dig = function(pos, player)
172             local meta = minetest.get_meta(pos);
173             local inv = meta:get_inventory()
174             if not inv:is_empty("src") or not inv:is_empty("dst") or
175                not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
176                 minetest.chat_send_player(player:get_player_name(),
be2f30 177                     S("Machine cannot be removed because it is not empty"))
ee0765 178                 return false
S 179             else
180                 return true
181             end
182         end,
183         -- These three makes sure upgrades are not moved in or out while the furnace is active.
184         allow_metadata_inventory_put = function(pos, listname, index, stack, player)
185             if listname == "src" or listname == "dst" then
186                 return stack:get_count()
187             else
188                 return 0 -- Disallow the move
189             end
190         end,
191         allow_metadata_inventory_take = function(pos, listname, index, stack, player)
192             if listname == "src" or listname == "dst" then
193                 return stack:get_count()
194             else
195                 return 0 -- Disallow the move
196             end
197         end,
198         allow_metadata_inventory_move = function(pos, from_list, to_list, to_list, to_index, count, player)
199             return 0
200         end,
201     })
202
203     minetest.register_abm({
204         nodenames = {"technic:"..ltier.."_alloy_furnace", "technic:"..ltier.."_alloy_furnace_active"},
205         interval = 1,
206         chance = 1,
207         action = function(pos, node, active_object_count, active_object_count_wider)
208             local data         = minetest.registered_nodes[node.name].technic
209             local meta         = minetest.get_meta(pos)
210             local inv          = meta:get_inventory()
211             local eu_input     = meta:get_int(data.tier.."_EU_input")
212
213             -- Machine information
be2f30 214             local machine_name   = S("%s Alloy Furnace"):format(data.tier)
ee0765 215             local machine_node   = "technic:"..string.lower(data.tier).."_alloy_furnace"
S 216             local machine_demand = data.demand
217
218             -- Setup meta data if it does not exist.
219             if not eu_input then
220                 meta:set_int(data.tier.."_EU_demand", machine_demand[1])
221                 meta:set_int(data.tier.."_EU_input", 0)
222             end
223
224             -- Power off automatically if no longer connected to a switching station
225             technic.switching_station_timeout_count(pos, data.tier)
226
227             local EU_upgrade, tube_upgrade = 0, 0
228             if data.upgrade then
229                 EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
230             end
231             if data.tube then
232                 technic.handle_machine_pipeworks(pos, tube_upgrade)
233             end
234
235             -- Get what to cook if anything
236             local srcstack  = inv:get_stack("src", 1)
237             local src2stack = inv:get_stack("src", 2)
238             local recipe = technic.get_alloy_recipe(srcstack, src2stack)
239             local result = recipe and ItemStack(recipe.output) or nil
240             -- Sort the stacks alphabetically
241             if srcstack:get_name() > src2stack:get_name() then
242                 local temp = srcstack
243                 srcstack = src2stack
244                 src2stack = temp
245             end
bd3cc7 246             if not result or
S 247                not inv:room_for_item("dst", result) then
ee0765 248                 hacky_swap_node(pos, machine_node)
be2f30 249                 meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 250                 meta:set_int(data.tier.."_EU_demand", 0)
S 251                 return
252             end
253
254             if eu_input < machine_demand[EU_upgrade+1] then
255                 -- Unpowered - go idle
256                 hacky_swap_node(pos, machine_node)
be2f30 257                 meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
ee0765 258             elseif eu_input >= machine_demand[EU_upgrade+1] then
S 259                 -- Powered
260                 hacky_swap_node(pos, machine_node.."_active")
be2f30 261                 meta:set_string("infotext", S("%s Active"):format(machine_name))
ee0765 262                 meta:set_int("src_time", meta:get_int("src_time") + 1)
S 263                 if meta:get_int("src_time") == data.cook_time then
264                     meta:set_int("src_time", 0)
265                     -- check if there's room for output and that we have the materials
266                     if inv:room_for_item("dst", result) then
267                         srcstack:take_item(recipe.input[1].count)
268                         inv:set_stack("src", 1, srcstack)
269                         src2stack:take_item(recipe.input[2].count)
270                         inv:set_stack("src", 2, src2stack)
271                         -- Put result in "dst" list
272                         inv:add_item("dst", result)
273                     else
274                         next_state = 1
275                     end
276                 end
277             
278             end
279             meta:set_int(data.tier.."_EU_demand", machine_demand[EU_upgrade+1])
280         end,
281     })
282
283     technic.register_machine(tier, "technic:"..ltier.."_alloy_furnace",        technic.receiver)
284     technic.register_machine(tier, "technic:"..ltier.."_alloy_furnace_active", technic.receiver)
285
286 end -- End registration
287