commit | author | age
|
ee0765
|
1 |
|
338f3b
|
2 |
local digilines_path = minetest.get_modpath("digilines") |
D |
3 |
|
be2f30
|
4 |
local S = technic.getter |
a3a959
|
5 |
local tube_entry = "^pipeworks_tube_connection_metallic.png" |
170d3e
|
6 |
local cable_entry = "^technic_cable_connection_overlay.png" |
ee0765
|
7 |
|
7244f8
|
8 |
local fs_helpers = pipeworks.fs_helpers |
VE |
9 |
|
354ee6
|
10 |
technic.register_power_tool("technic:battery", 10000) |
1b7fa3
|
11 |
technic.register_power_tool("technic:red_energy_crystal", 50000) |
BM |
12 |
technic.register_power_tool("technic:green_energy_crystal", 150000) |
|
13 |
technic.register_power_tool("technic:blue_energy_crystal", 450000) |
354ee6
|
14 |
|
1a75cd
|
15 |
-- Battery recipes: |
D |
16 |
-- Tin-copper recipe: |
354ee6
|
17 |
minetest.register_craft({ |
1a75cd
|
18 |
output = "technic:battery", |
354ee6
|
19 |
recipe = { |
1a75cd
|
20 |
{"group:wood", "default:copper_ingot", "group:wood"}, |
d1b54a
|
21 |
{"group:wood", "default:tin_ingot", "group:wood"}, |
1a75cd
|
22 |
{"group:wood", "default:copper_ingot", "group:wood"}, |
D |
23 |
} |
|
24 |
}) |
|
25 |
-- Sulfur-lead-water recipes: |
|
26 |
-- With sulfur lumps: |
|
27 |
-- With water: |
|
28 |
minetest.register_craft({ |
|
29 |
output = "technic:battery", |
|
30 |
recipe = { |
|
31 |
{"group:wood", "technic:sulfur_lump", "group:wood"}, |
|
32 |
{"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"}, |
|
33 |
{"group:wood", "technic:sulfur_lump", "group:wood"}, |
|
34 |
}, |
|
35 |
replacements = { |
|
36 |
{"bucket:bucket_water", "bucket:bucket_empty"} |
|
37 |
} |
|
38 |
}) |
|
39 |
-- With oil extract: |
|
40 |
minetest.register_craft({ |
|
41 |
output = "technic:battery", |
|
42 |
recipe = { |
|
43 |
{"group:wood", "technic:sulfur_lump", "group:wood"}, |
|
44 |
{"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"}, |
|
45 |
{"group:wood", "technic:sulfur_lump", "group:wood"}, |
|
46 |
} |
|
47 |
}) |
|
48 |
-- With sulfur dust: |
|
49 |
-- With water: |
|
50 |
minetest.register_craft({ |
|
51 |
output = "technic:battery", |
|
52 |
recipe = { |
|
53 |
{"group:wood", "technic:sulfur_dust", "group:wood"}, |
|
54 |
{"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"}, |
|
55 |
{"group:wood", "technic:sulfur_dust", "group:wood"}, |
|
56 |
}, |
|
57 |
replacements = { |
|
58 |
{"bucket:bucket_water", "bucket:bucket_empty"} |
|
59 |
} |
|
60 |
}) |
|
61 |
-- With oil extract: |
|
62 |
minetest.register_craft({ |
|
63 |
output = "technic:battery", |
|
64 |
recipe = { |
|
65 |
{"group:wood", "technic:sulfur_dust", "group:wood"}, |
|
66 |
{"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"}, |
|
67 |
{"group:wood", "technic:sulfur_dust", "group:wood"}, |
354ee6
|
68 |
} |
S |
69 |
}) |
|
70 |
|
|
71 |
minetest.register_tool("technic:battery", { |
|
72 |
description = S("RE Battery"), |
|
73 |
inventory_image = "technic_battery.png", |
99fd5d
|
74 |
wear_represents = "technic_RE_charge", |
00d7c9
|
75 |
on_refill = technic.refill_RE_charge, |
354ee6
|
76 |
tool_capabilities = { |
S |
77 |
charge = 0, |
|
78 |
max_drop_level = 0, |
|
79 |
groupcaps = { |
|
80 |
fleshy = {times={}, uses=10000, maxlevel=0} |
|
81 |
} |
|
82 |
} |
|
83 |
}) |
|
84 |
|
557dc4
|
85 |
-- x+2 + (z+2)*2 |
VE |
86 |
local dirtab = { |
|
87 |
[4] = 2, |
|
88 |
[5] = 3, |
|
89 |
[7] = 1, |
|
90 |
[8] = 0 |
|
91 |
} |
|
92 |
|
6a0807
|
93 |
local tube = { |
N |
94 |
insert_object = function(pos, node, stack, direction) |
557dc4
|
95 |
print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2) |
VE |
96 |
if direction.y == 1 |
|
97 |
or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then |
6a0807
|
98 |
return stack |
N |
99 |
end |
|
100 |
local meta = minetest.get_meta(pos) |
|
101 |
local inv = meta:get_inventory() |
557dc4
|
102 |
if direction.y == 0 then |
6a0807
|
103 |
return inv:add_item("src", stack) |
N |
104 |
else |
|
105 |
return inv:add_item("dst", stack) |
|
106 |
end |
|
107 |
end, |
|
108 |
can_insert = function(pos, node, stack, direction) |
557dc4
|
109 |
print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2) |
VE |
110 |
if direction.y == 1 |
|
111 |
or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then |
6a0807
|
112 |
return false |
N |
113 |
end |
|
114 |
local meta = minetest.get_meta(pos) |
|
115 |
local inv = meta:get_inventory() |
557dc4
|
116 |
if direction.y == 0 then |
7244f8
|
117 |
if meta:get_int("split_src_stacks") == 1 then |
VE |
118 |
stack = stack:peek_item(1) |
|
119 |
end |
|
120 |
return inv:room_for_item("src", stack) |
6a0807
|
121 |
else |
7244f8
|
122 |
if meta:get_int("split_dst_stacks") == 1 then |
VE |
123 |
stack = stack:peek_item(1) |
|
124 |
end |
|
125 |
return inv:room_for_item("dst", stack) |
6a0807
|
126 |
end |
N |
127 |
end, |
557dc4
|
128 |
connect_sides = {left=1, right=1, back=1, top=1}, |
6a0807
|
129 |
} |
7244f8
|
130 |
|
VE |
131 |
local function add_on_off_buttons(meta, ltier, charge_percent) |
6e58a8
|
132 |
local formspec = "image[1,1;1,2;technic_power_meter_bg.png" |
7244f8
|
133 |
.."^[lowpart:"..charge_percent |
6e58a8
|
134 |
..":technic_power_meter_fg.png]" |
CH |
135 |
if ltier == "mv" or ltier == "hv" then |
|
136 |
formspec = formspec.. |
7244f8
|
137 |
fs_helpers.cycling_button( |
VE |
138 |
meta, |
|
139 |
"image_button[3,2.0;1,0.6", |
|
140 |
"split_src_stacks", |
|
141 |
{ |
fab2c4
|
142 |
pipeworks.button_off, |
VE |
143 |
pipeworks.button_on |
7244f8
|
144 |
} |
VE |
145 |
).."label[3.9,2.01;Allow splitting incoming 'charge' stacks from tubes]".. |
|
146 |
fs_helpers.cycling_button( |
|
147 |
meta, |
|
148 |
"image_button[3,2.5;1,0.6", |
|
149 |
"split_dst_stacks", |
|
150 |
{ |
fab2c4
|
151 |
pipeworks.button_off, |
VE |
152 |
pipeworks.button_on |
7244f8
|
153 |
} |
VE |
154 |
).."label[3.9,2.51;Allow splitting incoming 'discharge' stacks]" |
|
155 |
end |
|
156 |
return formspec |
|
157 |
end |
354ee6
|
158 |
|
ee0765
|
159 |
function technic.register_battery_box(data) |
S |
160 |
local tier = data.tier |
|
161 |
local ltier = string.lower(tier) |
be2f30
|
162 |
|
76a8ac
|
163 |
local formspec = |
338f3b
|
164 |
"size[8,9]".. |
be2f30
|
165 |
"image[1,1;1,2;technic_power_meter_bg.png]".. |
338f3b
|
166 |
"list[context;src;3,1;1,1;]".. |
be2f30
|
167 |
"image[4,1;1,1;technic_battery_reload.png]".. |
338f3b
|
168 |
"list[context;dst;5,1;1,1;]".. |
be2f30
|
169 |
"label[0,0;"..S("%s Battery Box"):format(tier).."]".. |
S |
170 |
"label[3,0;"..S("Charge").."]".. |
|
171 |
"label[5,0;"..S("Discharge").."]".. |
|
172 |
"label[1,3;"..S("Power level").."]".. |
d732c8
|
173 |
"list[current_player;main;0,5;8,4;]".. |
338f3b
|
174 |
"listring[context;dst]".. |
d732c8
|
175 |
"listring[current_player;main]".. |
338f3b
|
176 |
"listring[context;src]".. |
d732c8
|
177 |
"listring[current_player;main]" |
7244f8
|
178 |
|
338f3b
|
179 |
if digilines_path then |
D |
180 |
formspec = formspec.."button[0.6,3.7;2,1;edit_channel;edit Channel]" |
|
181 |
end |
d732c8
|
182 |
|
6a0807
|
183 |
if data.upgrade then |
N |
184 |
formspec = formspec.. |
338f3b
|
185 |
"list[context;upgrade1;3.5,3;1,1;]".. |
D |
186 |
"list[context;upgrade2;4.5,3;1,1;]".. |
d732c8
|
187 |
"label[3.5,4;"..S("Upgrade Slots").."]".. |
338f3b
|
188 |
"listring[context;upgrade1]".. |
d732c8
|
189 |
"listring[current_player;main]".. |
338f3b
|
190 |
"listring[context;upgrade2]".. |
d732c8
|
191 |
"listring[current_player;main]" |
6a0807
|
192 |
end |
ee0765
|
193 |
|
563a4c
|
194 |
local run = function(pos, node) |
334553
|
195 |
local below = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) |
563a4c
|
196 |
local meta = minetest.get_meta(pos) |
334553
|
197 |
|
fc2f6d
|
198 |
if not technic.is_tier_cable(below.name, tier) then |
334553
|
199 |
meta:set_string("infotext", S("%s Battery Box Has No Network"):format(tier)) |
VE |
200 |
return |
|
201 |
end |
|
202 |
|
563a4c
|
203 |
local eu_input = meta:get_int(tier.."_EU_input") |
N |
204 |
local current_charge = meta:get_int("internal_EU_charge") |
|
205 |
|
|
206 |
local EU_upgrade, tube_upgrade = 0, 0 |
|
207 |
if data.upgrade then |
|
208 |
EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta) |
|
209 |
end |
|
210 |
local max_charge = data.max_charge * (1 + EU_upgrade / 10) |
338f3b
|
211 |
|
563a4c
|
212 |
-- Charge/discharge the battery with the input EUs |
N |
213 |
if eu_input >= 0 then |
|
214 |
current_charge = math.min(current_charge + eu_input, max_charge) |
|
215 |
else |
|
216 |
current_charge = math.max(current_charge + eu_input, 0) |
|
217 |
end |
|
218 |
|
|
219 |
-- Charging/discharging tools here |
|
220 |
local tool_full, tool_empty |
|
221 |
current_charge, tool_full = technic.charge_tools(meta, |
|
222 |
current_charge, data.charge_step) |
|
223 |
current_charge, tool_empty = technic.discharge_tools(meta, |
|
224 |
current_charge, data.discharge_step, |
|
225 |
max_charge) |
338f3b
|
226 |
|
563a4c
|
227 |
if data.tube then |
N |
228 |
local inv = meta:get_inventory() |
|
229 |
technic.handle_machine_pipeworks(pos, tube_upgrade, |
|
230 |
function(pos, x_velocity, z_velocity) |
|
231 |
if tool_full and not inv:is_empty("src") then |
|
232 |
technic.send_items(pos, x_velocity, z_velocity, "src") |
|
233 |
elseif tool_empty and not inv:is_empty("dst") then |
|
234 |
technic.send_items(pos, x_velocity, z_velocity, "dst") |
|
235 |
end |
|
236 |
end) |
|
237 |
end |
|
238 |
|
|
239 |
-- We allow batteries to charge on less than the demand |
|
240 |
meta:set_int(tier.."_EU_demand", |
|
241 |
math.min(data.charge_rate, max_charge - current_charge)) |
|
242 |
meta:set_int(tier.."_EU_supply", |
|
243 |
math.min(data.discharge_rate, current_charge)) |
|
244 |
meta:set_int("internal_EU_charge", current_charge) |
|
245 |
|
|
246 |
-- Select node textures |
|
247 |
local charge_count = math.ceil((current_charge / max_charge) * 8) |
|
248 |
charge_count = math.min(charge_count, 8) |
|
249 |
charge_count = math.max(charge_count, 0) |
|
250 |
local last_count = meta:get_float("last_side_shown") |
|
251 |
if charge_count ~= last_count then |
|
252 |
technic.swap_node(pos,"technic:"..ltier.."_battery_box"..charge_count) |
|
253 |
meta:set_float("last_side_shown", charge_count) |
|
254 |
end |
|
255 |
|
|
256 |
local charge_percent = math.floor(current_charge / max_charge * 100) |
7244f8
|
257 |
meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent)) |
41f175
|
258 |
local infotext = S("@1 Battery Box: @2 / @3", tier, |
H |
259 |
technic.EU_string(current_charge), |
|
260 |
technic.EU_string(max_charge)) |
563a4c
|
261 |
if eu_input == 0 then |
N |
262 |
infotext = S("%s Idle"):format(infotext) |
|
263 |
end |
|
264 |
meta:set_string("infotext", infotext) |
|
265 |
end |
338f3b
|
266 |
|
ee0765
|
267 |
for i = 0, 8 do |
83c649
|
268 |
local groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, |
S |
269 |
technic_machine=1, ["technic_"..ltier]=1} |
ee0765
|
270 |
if i ~= 0 then |
S |
271 |
groups.not_in_creative_inventory = 1 |
|
272 |
end |
338f3b
|
273 |
|
6a0807
|
274 |
if data.tube then |
N |
275 |
groups.tubedevice = 1 |
|
276 |
groups.tubedevice_receiver = 1 |
|
277 |
end |
338f3b
|
278 |
|
a34ea5
|
279 |
local top_tex = "technic_"..ltier.."_battery_box_top.png"..tube_entry |
170d3e
|
280 |
local front_tex = "technic_"..ltier.."_battery_box_front.png^technic_power_meter"..i..".png" |
a34ea5
|
281 |
local side_tex = "technic_"..ltier.."_battery_box_side.png"..tube_entry |
557dc4
|
282 |
local bottom_tex = "technic_"..ltier.."_battery_box_bottom.png"..cable_entry |
170d3e
|
283 |
|
a3a959
|
284 |
if ltier == "lv" then |
a34ea5
|
285 |
top_tex = "technic_"..ltier.."_battery_box_top.png" |
170d3e
|
286 |
front_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png" |
a34ea5
|
287 |
side_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png" |
a3a959
|
288 |
end |
VE |
289 |
|
ee0765
|
290 |
minetest.register_node("technic:"..ltier.."_battery_box"..i, { |
be2f30
|
291 |
description = S("%s Battery Box"):format(tier), |
338f3b
|
292 |
tiles = { |
a34ea5
|
293 |
top_tex, |
VE |
294 |
bottom_tex, |
|
295 |
side_tex, |
|
296 |
side_tex, |
|
297 |
side_tex, |
170d3e
|
298 |
front_tex}, |
ee0765
|
299 |
groups = groups, |
83c649
|
300 |
connect_sides = {"bottom"}, |
6a0807
|
301 |
tube = data.tube and tube or nil, |
N |
302 |
paramtype2 = "facedir", |
ee0765
|
303 |
sounds = default.node_sound_wood_defaults(), |
S |
304 |
drop = "technic:"..ltier.."_battery_box0", |
|
305 |
on_construct = function(pos) |
|
306 |
local meta = minetest.get_meta(pos) |
0f6bdb
|
307 |
local EU_upgrade, _ = 0 |
7244f8
|
308 |
if data.upgrade then |
0f6bdb
|
309 |
EU_upgrade, _ = technic.handle_machine_upgrades(meta) |
7244f8
|
310 |
end |
VE |
311 |
local max_charge = data.max_charge * (1 + EU_upgrade / 10) |
|
312 |
local charge = meta:get_int("internal_EU_charge") |
|
313 |
local cpercent = math.floor(charge / max_charge * 100) |
ee0765
|
314 |
local inv = meta:get_inventory() |
76a8ac
|
315 |
meta:set_string("infotext", S("%s Battery Box"):format(tier)) |
7244f8
|
316 |
meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent)) |
338f3b
|
317 |
meta:set_string("channel", ltier.."_battery_box"..minetest.pos_to_string(pos)) |
76a8ac
|
318 |
meta:set_int(tier.."_EU_demand", 0) |
S |
319 |
meta:set_int(tier.."_EU_supply", 0) |
|
320 |
meta:set_int(tier.."_EU_input", 0) |
ee0765
|
321 |
meta:set_float("internal_EU_charge", 0) |
S |
322 |
inv:set_size("src", 1) |
|
323 |
inv:set_size("dst", 1) |
6a0807
|
324 |
inv:set_size("upgrade1", 1) |
N |
325 |
inv:set_size("upgrade2", 1) |
ee0765
|
326 |
end, |
0809dd
|
327 |
can_dig = technic.machine_can_dig, |
S |
328 |
allow_metadata_inventory_put = technic.machine_inventory_put, |
|
329 |
allow_metadata_inventory_take = technic.machine_inventory_take, |
|
330 |
allow_metadata_inventory_move = technic.machine_inventory_move, |
563a4c
|
331 |
technic_run = run, |
557dc4
|
332 |
on_rotate = screwdriver.rotate_simple, |
011397
|
333 |
after_place_node = data.tube and pipeworks.after_place, |
338f3b
|
334 |
after_dig_node = technic.machine_after_dig_node, |
D |
335 |
on_receive_fields = function(pos, formname, fields, sender) |
3f8478
|
336 |
local meta = minetest.get_meta(pos) |
7244f8
|
337 |
if fields.edit_channel then |
VE |
338 |
minetest.show_formspec(sender:get_player_name(), |
338f3b
|
339 |
"technic:battery_box_edit_channel"..minetest.pos_to_string(pos), |
D |
340 |
"field[channel;Digiline Channel;"..meta:get_string("channel").."]") |
7244f8
|
341 |
elseif fields["fs_helpers_cycling:0:split_src_stacks"] |
VE |
342 |
or fields["fs_helpers_cycling:0:split_dst_stacks"] |
|
343 |
or fields["fs_helpers_cycling:1:split_src_stacks"] |
|
344 |
or fields["fs_helpers_cycling:1:split_dst_stacks"] then |
a8daa4
|
345 |
meta = minetest.get_meta(pos) |
7244f8
|
346 |
if not pipeworks.may_configure(pos, sender) then return end |
VE |
347 |
fs_helpers.on_receive_fields(pos, fields) |
0f6bdb
|
348 |
local EU_upgrade, _ = 0 |
7244f8
|
349 |
if data.upgrade then |
0f6bdb
|
350 |
EU_upgrade, _ = technic.handle_machine_upgrades(meta) |
7244f8
|
351 |
end |
VE |
352 |
local max_charge = data.max_charge * (1 + EU_upgrade / 10) |
|
353 |
local charge = meta:get_int("internal_EU_charge") |
|
354 |
local cpercent = math.floor(charge / max_charge * 100) |
|
355 |
meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent)) |
|
356 |
end |
338f3b
|
357 |
end, |
D |
358 |
digiline = { |
|
359 |
receptor = {action = function() end}, |
|
360 |
effector = { |
|
361 |
action = function(pos, node, channel, msg) |
|
362 |
if msg ~= "GET" and msg ~= "get" then |
|
363 |
return |
|
364 |
end |
|
365 |
local meta = minetest.get_meta(pos) |
|
366 |
if channel ~= meta:get_string("channel") then |
|
367 |
return |
|
368 |
end |
|
369 |
local inv = meta:get_inventory() |
|
370 |
digilines.receptor_send(pos, digilines.rules.default, channel, { |
|
371 |
demand = meta:get_int(tier.."_EU_demand"), |
|
372 |
supply = meta:get_int(tier.."_EU_supply"), |
|
373 |
input = meta:get_int(tier.."_EU_input"), |
|
374 |
charge = meta:get_int("internal_EU_charge"), |
|
375 |
max_charge = data.max_charge * (1 + technic.handle_machine_upgrades(meta) / 10), |
|
376 |
src = inv:get_stack("src", 1):to_table(), |
|
377 |
dst = inv:get_stack("dst", 1):to_table(), |
|
378 |
upgrade1 = inv:get_stack("upgrade1", 1):to_table(), |
|
379 |
upgrade2 = inv:get_stack("upgrade2", 1):to_table() |
|
380 |
}) |
|
381 |
end |
|
382 |
}, |
|
383 |
}, |
ee0765
|
384 |
}) |
S |
385 |
end |
|
386 |
|
|
387 |
-- Register as a battery type |
|
388 |
-- Battery type machines function as power reservoirs and can both receive and give back power |
|
389 |
for i = 0, 8 do |
|
390 |
technic.register_machine(tier, "technic:"..ltier.."_battery_box"..i, technic.battery) |
|
391 |
end |
|
392 |
|
|
393 |
end -- End registration |
|
394 |
|
338f3b
|
395 |
minetest.register_on_player_receive_fields( |
D |
396 |
function(player, formname, fields) |
|
397 |
if formname:sub(1, 32) ~= "technic:battery_box_edit_channel" or |
|
398 |
not fields.channel then |
|
399 |
return |
|
400 |
end |
|
401 |
local pos = minetest.string_to_pos(formname:sub(33)) |
|
402 |
local plname = player:get_player_name() |
|
403 |
if minetest.is_protected(pos, plname) then |
|
404 |
minetest.record_protection_violation(pos, plname) |
|
405 |
return |
|
406 |
end |
|
407 |
local meta = minetest.get_meta(pos) |
|
408 |
meta:set_string("channel", fields.channel) |
|
409 |
end |
|
410 |
) |
ee0765
|
411 |
|
0211c5
|
412 |
local function default_get_charge(itemstack) |
S |
413 |
-- check if is chargable |
|
414 |
local tool_name = itemstack:get_name() |
|
415 |
if not technic.power_tools[tool_name] then |
|
416 |
return 0, 0 |
|
417 |
end |
|
418 |
-- Set meta data for the tool if it didn't do it itself |
|
419 |
local item_meta = minetest.deserialize(itemstack:get_metadata()) or {} |
|
420 |
if not item_meta.charge then |
|
421 |
item_meta.charge = 0 |
|
422 |
end |
|
423 |
return item_meta.charge, technic.power_tools[tool_name] |
|
424 |
end |
|
425 |
|
|
426 |
local function default_set_charge(itemstack, charge) |
|
427 |
local tool_name = itemstack:get_name() |
|
428 |
if technic.power_tools[tool_name] then |
|
429 |
technic.set_RE_wear(itemstack, charge, technic.power_tools[tool_name]) |
|
430 |
end |
|
431 |
local item_meta = minetest.deserialize(itemstack:get_metadata()) or {} |
|
432 |
item_meta.charge = charge |
|
433 |
itemstack:set_metadata(minetest.serialize(item_meta)) |
|
434 |
end |
|
435 |
|
eac484
|
436 |
function technic.charge_tools(meta, batt_charge, charge_step) |
ee0765
|
437 |
local inv = meta:get_inventory() |
eac484
|
438 |
if inv:is_empty("src") then |
6a0807
|
439 |
return batt_charge, false |
ee0765
|
440 |
end |
5382a8
|
441 |
local src_stack = inv:get_stack("src", 1) |
eac484
|
442 |
|
0211c5
|
443 |
-- get callbacks |
S |
444 |
local src_def = src_stack:get_definition() |
|
445 |
local technic_get_charge = src_def.technic_get_charge or default_get_charge |
|
446 |
local technic_set_charge = src_def.technic_set_charge or default_set_charge |
|
447 |
|
|
448 |
-- get tool charge |
|
449 |
local tool_charge, item_max_charge = technic_get_charge(src_stack) |
|
450 |
if item_max_charge==0 then |
6a0807
|
451 |
return batt_charge, false |
eac484
|
452 |
end |
0211c5
|
453 |
|
eac484
|
454 |
-- Do the charging |
6a0807
|
455 |
if tool_charge >= item_max_charge then |
N |
456 |
return batt_charge, true |
|
457 |
elseif batt_charge <= 0 then |
|
458 |
return batt_charge, false |
eac484
|
459 |
end |
S |
460 |
charge_step = math.min(charge_step, batt_charge) |
|
461 |
charge_step = math.min(charge_step, item_max_charge - tool_charge) |
|
462 |
tool_charge = tool_charge + charge_step |
|
463 |
batt_charge = batt_charge - charge_step |
0211c5
|
464 |
technic_set_charge(src_stack, tool_charge) |
5382a8
|
465 |
inv:set_stack("src", 1, src_stack) |
6a0807
|
466 |
return batt_charge, (tool_charge == item_max_charge) |
ee0765
|
467 |
end |
S |
468 |
|
|
469 |
|
eac484
|
470 |
function technic.discharge_tools(meta, batt_charge, charge_step, max_charge) |
ee0765
|
471 |
local inv = meta:get_inventory() |
eac484
|
472 |
if inv:is_empty("dst") then |
6a0807
|
473 |
return batt_charge, false |
ee0765
|
474 |
end |
0211c5
|
475 |
local src_stack = inv:get_stack("dst", 1) |
S |
476 |
|
|
477 |
-- get callbacks |
|
478 |
local src_def = src_stack:get_definition() |
|
479 |
local technic_get_charge = src_def.technic_get_charge or default_get_charge |
|
480 |
local technic_set_charge = src_def.technic_set_charge or default_set_charge |
|
481 |
|
|
482 |
-- get tool charge |
|
483 |
local tool_charge, item_max_charge = technic_get_charge(src_stack) |
|
484 |
if item_max_charge==0 then |
6a0807
|
485 |
return batt_charge, false |
eac484
|
486 |
end |
S |
487 |
|
|
488 |
-- Do the discharging |
6a0807
|
489 |
if tool_charge <= 0 then |
N |
490 |
return batt_charge, true |
|
491 |
elseif batt_charge >= max_charge then |
|
492 |
return batt_charge, false |
eac484
|
493 |
end |
S |
494 |
charge_step = math.min(charge_step, max_charge - batt_charge) |
|
495 |
charge_step = math.min(charge_step, tool_charge) |
|
496 |
tool_charge = tool_charge - charge_step |
|
497 |
batt_charge = batt_charge + charge_step |
0211c5
|
498 |
technic_set_charge(src_stack, tool_charge) |
S |
499 |
inv:set_stack("dst", 1, src_stack) |
6a0807
|
500 |
return batt_charge, (tool_charge == 0) |
ee0765
|
501 |
end |
S |
502 |
|