Zefram
2014-08-18 7d610b7c80487cd7a6e66f55c9c3b1190e5dfc7f
commit | author | age
aa8af0 1
N 2 local S = technic.getter
3
4 local tube = {
5     insert_object = function(pos, node, stack, direction)
6         local meta = minetest.get_meta(pos)
7         local inv = meta:get_inventory()
8         return inv:add_item("src", stack)
9     end,
10     can_insert = function(pos, node, stack, direction)
11         local meta = minetest.get_meta(pos)
12         local inv = meta:get_inventory()
13         return inv:room_for_item("src", stack)
14     end,
15     connect_sides = {left = 1, right = 1, back = 1, top = 1, bottom = 1},
16 }
17
814646 18 local function round(v)
Z 19     return math.floor(v + 0.5)
20 end
21
aa8af0 22 function technic.register_base_machine(data)
N 23     local typename = data.typename
dd65a6 24     local input_size = technic.recipes[typename].input_size
aa8af0 25     local machine_name = data.machine_name
N 26     local machine_desc = data.machine_desc
27     local tier = data.tier
28     local ltier = string.lower(tier)
29
563a4c 30     local groups = {cracky = 2, technic_machine = 1}
N 31     local active_groups = {cracky = 2, technic_machine = 1, not_in_creative_inventory = 1}
aa8af0 32     if data.tube then
N 33         groups.tubedevice = 1
34         groups.tubedevice_receiver = 1
35         active_groups.tubedevice = 1
36         active_groups.tubedevice_receiver = 1
37     end
38
39
40     local formspec =
41         "invsize[8,9;]"..
dd65a6 42         "list[current_name;src;"..(4-input_size)..",1;"..input_size..",1;]"..
aa8af0 43         "list[current_name;dst;5,1;2,2;]"..
N 44         "list[current_player;main;0,5;8,4;]"..
5f2d09 45         "label[0,0;"..machine_desc:format(tier).."]"
aa8af0 46     if data.upgrade then
N 47         formspec = formspec..
48             "list[current_name;upgrade1;1,3;1,1;]"..
49             "list[current_name;upgrade2;2,3;1,1;]"..
50             "label[1,4;"..S("Upgrade Slots").."]"
51     end
52
563a4c 53     local run = function(pos, node)
N 54         local meta     = minetest.get_meta(pos)
55         local inv      = meta:get_inventory()
56         local eu_input = meta:get_int(tier.."_EU_input")
57
58         local machine_desc_tier = machine_desc:format(tier)
59         local machine_node      = "technic:"..ltier.."_"..machine_name
60         local machine_demand    = data.demand
61
62         -- Setup meta data if it does not exist.
63         if not eu_input then
64             meta:set_int(tier.."_EU_demand", machine_demand[1])
65             meta:set_int(tier.."_EU_input", 0)
66             return
67         end
68
69         local EU_upgrade, tube_upgrade = 0, 0
70         if data.upgrade then
71             EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
72         end
73         if data.tube then
74             technic.handle_machine_pipeworks(pos, tube_upgrade)
75         end
76
814646 77         local powered = eu_input >= machine_demand[EU_upgrade+1]
Z 78         if powered then
79             meta:set_int("src_time", meta:get_int("src_time") + round(data.speed*10))
563a4c 80         end
814646 81         while true do
Z 82             local result = technic.get_recipe(typename, inv:get_list("src"))
83             if not result then
84                 technic.swap_node(pos, machine_node)
85                 meta:set_string("infotext", S("%s Idle"):format(machine_desc_tier))
86                 meta:set_int(tier.."_EU_demand", 0)
87                 meta:set_int("src_time", 0)
88                 return
89             end
90             meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
563a4c 91             technic.swap_node(pos, machine_node.."_active")
N 92             meta:set_string("infotext", S("%s Active"):format(machine_desc_tier))
814646 93             if meta:get_int("src_time") < round(result.time*10) then
Z 94                 if not powered then
95                     technic.swap_node(pos, machine_node)
96                     meta:set_string("infotext", S("%s Unpowered"):format(machine_desc_tier))
dd65a6 97                 end
814646 98                 return
563a4c 99             end
814646 100             local output = result.output
Z 101             if type(output) ~= "table" then output = { output } end
102             local output_stacks = {}
103             for _, o in ipairs(output) do
104                 table.insert(output_stacks, ItemStack(o))
105             end
106             local room_for_output = true
107             inv:set_size("dst_tmp", inv:get_size("dst"))
108             inv:set_list("dst_tmp", inv:get_list("dst"))
109             for _, o in ipairs(output_stacks) do
110                 if not inv:room_for_item("dst_tmp", o) then
111                     room_for_output = false
112                     break
113                 end
114                 inv:add_item("dst_tmp", o)
115             end
116             if not room_for_output then
117                 meta:set_int("src_time", round(result.time*10))
118                 return
119             end
120             meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10))
121             inv:set_list("src", result.new_input)
122             inv:set_list("dst", inv:get_list("dst_tmp"))
563a4c 123         end
N 124     end
125     
aa8af0 126     minetest.register_node("technic:"..ltier.."_"..machine_name, {
N 127         description = machine_desc:format(tier),
128         tiles = {"technic_"..ltier.."_"..machine_name.."_top.png", 
129                      "technic_"..ltier.."_"..machine_name.."_bottom.png",
130                  "technic_"..ltier.."_"..machine_name.."_side.png",
131                  "technic_"..ltier.."_"..machine_name.."_side.png",
132                  "technic_"..ltier.."_"..machine_name.."_side.png",
133                  "technic_"..ltier.."_"..machine_name.."_front.png"},
134         paramtype2 = "facedir",
135         groups = groups,
136         tube = data.tube and tube or nil,
137         legacy_facedir_simple = true,
138         sounds = default.node_sound_wood_defaults(),
139         on_construct = function(pos)
140             local node = minetest.get_node(pos)
141             local meta = minetest.get_meta(pos)
142             meta:set_string("infotext", machine_desc:format(tier))
143             meta:set_int("tube_time",  0)
144             meta:set_string("formspec", formspec)
145             local inv = meta:get_inventory()
dd65a6 146             inv:set_size("src", input_size)
aa8af0 147             inv:set_size("dst", 4)
N 148             inv:set_size("upgrade1", 1)
149             inv:set_size("upgrade2", 1)
150         end,
151         can_dig = technic.machine_can_dig,
152         allow_metadata_inventory_put = technic.machine_inventory_put,
153         allow_metadata_inventory_take = technic.machine_inventory_take,
154         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 155         technic_run = run,
aa8af0 156     })
N 157
158     minetest.register_node("technic:"..ltier.."_"..machine_name.."_active",{
159         description = machine_desc:format(tier),
160         tiles = {"technic_"..ltier.."_"..machine_name.."_top.png",
161                  "technic_"..ltier.."_"..machine_name.."_bottom.png",
162                  "technic_"..ltier.."_"..machine_name.."_side.png",
163                  "technic_"..ltier.."_"..machine_name.."_side.png",
164                  "technic_"..ltier.."_"..machine_name.."_side.png",
165                  "technic_"..ltier.."_"..machine_name.."_front_active.png"},
166         paramtype2 = "facedir",
167         drop = "technic:"..ltier.."_"..machine_name,
168         groups = active_groups,
169         legacy_facedir_simple = true,
170         sounds = default.node_sound_wood_defaults(),
171         tube = data.tube and tube or nil,
172         can_dig = technic.machine_can_dig,
173         allow_metadata_inventory_put = technic.machine_inventory_put,
174         allow_metadata_inventory_take = technic.machine_inventory_take,
175         allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 176         technic_run = run,
N 177         technic_disabled_machine_name = "technic:"..ltier.."_"..machine_name,
aa8af0 178     })
N 179
180     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name,            technic.receiver)
181     technic.register_machine(tier, "technic:"..ltier.."_"..machine_name.."_active", technic.receiver)
182
183 end -- End registration
184