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