Vanessa Ezekowitz
2014-07-23 29c7ff5228864bcf5456f391b122c9eb477c754b
commit | author | age
ee0765 1
0809dd 2 local S = technic.getter
S 3
ee0765 4 function technic.handle_machine_upgrades(meta)
S 5     -- Get the names of the upgrades
6     local inv = meta:get_inventory()
7     local upg_item1
8     local upg_item2
9     local srcstack = inv:get_stack("upgrade1", 1)
10     if srcstack then
11         upg_item1 = srcstack:to_table()
12     end
13     srcstack = inv:get_stack("upgrade2", 1)
14     if srcstack then
15         upg_item2 = srcstack:to_table()
16     end
17
18     -- Save some power by installing battery upgrades.
19     -- Tube loading speed can be upgraded using control logic units.
20     local EU_upgrade = 0
21     local tube_upgrade = 0
22     if upg_item1 then
23         if     upg_item1.name == "technic:battery" then
24             EU_upgrade = EU_upgrade + 1
25         elseif upg_item1.name == "technic:control_logic_unit" then
26             tube_upgrade = tube_upgrade + 1
27         end
28     end
29     if upg_item2 then
30         if     upg_item2.name == "technic:battery" then
31             EU_upgrade = EU_upgrade + 1
32         elseif upg_item2.name == "technic:control_logic_unit" then
33             tube_upgrade = tube_upgrade + 1
34         end
35     end
36     return EU_upgrade, tube_upgrade
37 end
38
39
6a0807 40 function technic.send_items(pos, x_velocity, z_velocity, output_name)
ee0765 41     -- Send items on their way in the pipe system.
6a0807 42     if output_name == nil then
N 43         output_name = "dst"
44     end
45     
ee0765 46     local meta = minetest.get_meta(pos) 
S 47     local inv = meta:get_inventory()
48     local i = 0
6a0807 49     for _, stack in ipairs(inv:get_list(output_name)) do
ee0765 50         i = i + 1
S 51         if stack then
52             local item0 = stack:to_table()
53             if item0 then 
54                 item0["count"] = "1"
8ef3f2 55                 local item1 = pipeworks.tube_item({x=pos.x, y=pos.y, z=pos.z}, item0)
ee0765 56                 item1:get_luaentity().start_pos = {x=pos.x, y=pos.y, z=pos.z}
S 57                 item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
58                 item1:setacceleration({x=0, y=0, z=0})
59                 stack:take_item(1)
6a0807 60                 inv:set_stack(output_name, i, stack)
ee0765 61                 return
S 62             end
63         end
64     end
65 end
66
67
68 function technic.smelt_item(meta, result, speed)
69     local inv = meta:get_inventory()
70     meta:set_int("cook_time", meta:get_int("cook_time") + 1)
71     if meta:get_int("cook_time") < result.time / speed then
72         return
73     end
6ec12b 74     local result
G 75     local afterfuel
76     result, afterfuel = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
ee0765 77
S 78     if result and result.item then
79         meta:set_int("cook_time", 0)
80         -- check if there's room for output in "dst" list
bd3cc7 81         if inv:room_for_item("dst", result.item) then
6ec12b 82             inv:set_stack("src", 1, afterfuel.items[1])
ee0765 83             inv:add_item("dst", result.item)
S 84         end
85     end
86 end
87
6a0807 88 function technic.handle_machine_pipeworks(pos, tube_upgrade, send_function)
N 89     if send_function == nil then
90         send_function = technic.send_items
91     end
92     
ee0765 93     local node = minetest.get_node(pos)
S 94     local meta = minetest.get_meta(pos)
95     local inv = meta:get_inventory()
96     local pos1 = vector.new(pos)
97     local x_velocity = 0
98     local z_velocity = 0
99
100     -- Output is on the left side of the furnace
101     if node.param2 == 3 then pos1.z = pos1.z - 1  z_velocity = -1 end
102     if node.param2 == 2 then pos1.x = pos1.x - 1  x_velocity = -1 end
103     if node.param2 == 1 then pos1.z = pos1.z + 1  z_velocity =  1 end
104     if node.param2 == 0 then pos1.x = pos1.x + 1  x_velocity =  1 end
105
106     local output_tube_connected = false
107     local meta1 = minetest.get_meta(pos1) 
108     if meta1:get_int("tubelike") == 1 then
109         output_tube_connected = true
110     end
111     tube_time = meta:get_int("tube_time")
112     tube_time = tube_time + tube_upgrade
c5d287 113     if tube_time >= 2 then
ee0765 114         tube_time = 0
S 115         if output_tube_connected then
6a0807 116             send_function(pos, x_velocity, z_velocity)
ee0765 117         end
S 118     end
119     meta:set_int("tube_time", tube_time)
120 end
121
0809dd 122
S 123 function technic.machine_can_dig(pos, player)
124     local meta = minetest.get_meta(pos)
125     local inv = meta:get_inventory()
126     if not inv:is_empty("src") or not inv:is_empty("dst") or
127        not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
128         minetest.chat_send_player(player:get_player_name(),
129             S("Machine cannot be removed because it is not empty"))
130         return false
131     else
132         return true
133     end
134 end
135
136 local function inv_change(pos, player, count)
137     if minetest.is_protected(pos, player:get_player_name()) then
138         minetest.chat_send_player(player:get_player_name(),
139             S("Inventory move disallowed due to protection"))
140         return 0
141     end
142     return count
143 end
144
145 function technic.machine_inventory_put(pos, listname, index, stack, player)
146     return inv_change(pos, player, stack:get_count())
147 end
148
149 function technic.machine_inventory_take(pos, listname, index, stack, player)
150     return inv_change(pos, player, stack:get_count())
151 end
152
153 function technic.machine_inventory_move(pos, from_list, from_index,
154         to_list, to_index, count, player)
155     return inv_change(pos, player, count)
156 end
157