Vitaliy
2018-07-21 d1b54a573c6287ea1d63b9c3f979c39058b1331a
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)
132     local formspec = ""
133     if ltier == "mv" or ltier == "hv" then
134         formspec = "image[1,1;1,2;technic_power_meter_bg.png"
135             .."^[lowpart:"..charge_percent
136             ..":technic_power_meter_fg.png]"..
137             fs_helpers.cycling_button(
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)
7244f8 307                 local EU_upgrade, tube_upgrade = 0, 0
VE 308                 if data.upgrade then
309                     EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
310                 end
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()
S 315                 local node = minetest.get_node(pos)
76a8ac 316                 meta:set_string("infotext", S("%s Battery Box"):format(tier))
7244f8 317                 meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent))
338f3b 318                 meta:set_string("channel", ltier.."_battery_box"..minetest.pos_to_string(pos))
76a8ac 319                 meta:set_int(tier.."_EU_demand", 0)
S 320                 meta:set_int(tier.."_EU_supply", 0)
321                 meta:set_int(tier.."_EU_input",  0)
ee0765 322                 meta:set_float("internal_EU_charge", 0)
S 323                 inv:set_size("src", 1)
324                 inv:set_size("dst", 1)
6a0807 325                 inv:set_size("upgrade1", 1)
N 326                 inv:set_size("upgrade2", 1)
ee0765 327             end,
0809dd 328             can_dig = technic.machine_can_dig,
S 329             allow_metadata_inventory_put = technic.machine_inventory_put,
330             allow_metadata_inventory_take = technic.machine_inventory_take,
331             allow_metadata_inventory_move = technic.machine_inventory_move,
563a4c 332             technic_run = run,
557dc4 333             on_rotate = screwdriver.rotate_simple,
011397 334             after_place_node = data.tube and pipeworks.after_place,
338f3b 335             after_dig_node = technic.machine_after_dig_node,
D 336             on_receive_fields = function(pos, formname, fields, sender)
3f8478 337                 local meta = minetest.get_meta(pos)
7244f8 338                 local nodename = minetest.get_node(pos).name
VE 339                 if fields.edit_channel then
340                     minetest.show_formspec(sender:get_player_name(),
338f3b 341                         "technic:battery_box_edit_channel"..minetest.pos_to_string(pos),
D 342                         "field[channel;Digiline Channel;"..meta:get_string("channel").."]")
7244f8 343                 elseif fields["fs_helpers_cycling:0:split_src_stacks"]
VE 344                   or   fields["fs_helpers_cycling:0:split_dst_stacks"]
345                   or   fields["fs_helpers_cycling:1:split_src_stacks"]
346                   or   fields["fs_helpers_cycling:1:split_dst_stacks"] then
347                     local meta = minetest.get_meta(pos)
348                     if not pipeworks.may_configure(pos, sender) then return end
349                     fs_helpers.on_receive_fields(pos, fields)
350                     local EU_upgrade, tube_upgrade = 0, 0
351                     if data.upgrade then
352                         EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
353                     end
354                     local max_charge = data.max_charge * (1 + EU_upgrade / 10)
355                     local charge = meta:get_int("internal_EU_charge")
356                     local cpercent = math.floor(charge / max_charge * 100)
357                     meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent))
358                 end
338f3b 359             end,
D 360             digiline = {
361                 receptor = {action = function() end},
362                 effector = {
363                     action = function(pos, node, channel, msg)
364                         if msg ~= "GET" and msg ~= "get" then
365                             return
366                         end
367                         local meta = minetest.get_meta(pos)
368                         if channel ~= meta:get_string("channel") then
369                             return
370                         end
371                         local inv = meta:get_inventory()
372                         digilines.receptor_send(pos, digilines.rules.default, channel, {
373                             demand = meta:get_int(tier.."_EU_demand"),
374                             supply = meta:get_int(tier.."_EU_supply"),
375                             input  = meta:get_int(tier.."_EU_input"),
376                             charge = meta:get_int("internal_EU_charge"),
377                             max_charge = data.max_charge * (1 + technic.handle_machine_upgrades(meta) / 10),
378                             src      = inv:get_stack("src", 1):to_table(),
379                             dst      = inv:get_stack("dst", 1):to_table(),
380                             upgrade1 = inv:get_stack("upgrade1", 1):to_table(),
381                             upgrade2 = inv:get_stack("upgrade2", 1):to_table()
382                         })
383                     end
384                 },
385             },
ee0765 386         })
S 387     end
388
389     -- Register as a battery type
390     -- Battery type machines function as power reservoirs and can both receive and give back power
391     for i = 0, 8 do
392         technic.register_machine(tier, "technic:"..ltier.."_battery_box"..i, technic.battery)
393     end
394
395 end -- End registration
396
338f3b 397 minetest.register_on_player_receive_fields(
D 398     function(player, formname, fields)
399         if formname:sub(1, 32) ~= "technic:battery_box_edit_channel" or
400                 not fields.channel then
401             return
402         end
403         local pos = minetest.string_to_pos(formname:sub(33))
404         local plname = player:get_player_name()
405         if minetest.is_protected(pos, plname) then
406             minetest.record_protection_violation(pos, plname)
407             return
408         end
409         local meta = minetest.get_meta(pos)
410         meta:set_string("channel", fields.channel)
411     end
412 )
ee0765 413
eac484 414 function technic.charge_tools(meta, batt_charge, charge_step)
ee0765 415     local inv = meta:get_inventory()
eac484 416     if inv:is_empty("src") then
6a0807 417         return batt_charge, false
ee0765 418     end
5382a8 419     local src_stack = inv:get_stack("src", 1)
eac484 420
5382a8 421     local tool_name = src_stack:get_name()
S 422     if not technic.power_tools[tool_name] then
6a0807 423         return batt_charge, false
eac484 424     end
S 425     -- Set meta data for the tool if it didn't do it itself
5382a8 426     local src_meta = minetest.deserialize(src_stack:get_metadata()) or {}
eac484 427     if not src_meta.charge then
S 428         src_meta.charge = 0
429     end
430     -- Do the charging
5382a8 431     local item_max_charge = technic.power_tools[tool_name]
eac484 432     local tool_charge     = src_meta.charge
6a0807 433     if tool_charge >= item_max_charge then
N 434         return batt_charge, true
435     elseif batt_charge <= 0 then
436         return batt_charge, false
eac484 437     end
S 438     charge_step = math.min(charge_step, batt_charge)
439     charge_step = math.min(charge_step, item_max_charge - tool_charge)
440     tool_charge = tool_charge + charge_step
441     batt_charge = batt_charge - charge_step
5382a8 442     technic.set_RE_wear(src_stack, tool_charge, item_max_charge)
eac484 443     src_meta.charge = tool_charge
5382a8 444     src_stack:set_metadata(minetest.serialize(src_meta))
S 445     inv:set_stack("src", 1, src_stack)
6a0807 446     return batt_charge, (tool_charge == item_max_charge)
ee0765 447 end
S 448
449
eac484 450 function technic.discharge_tools(meta, batt_charge, charge_step, max_charge)
ee0765 451     local inv = meta:get_inventory()
eac484 452     if inv:is_empty("dst") then
6a0807 453         return batt_charge, false
ee0765 454     end
eac484 455     srcstack = inv:get_stack("dst", 1)
S 456     local toolname = srcstack:get_name()
457     if technic.power_tools[toolname] == nil then
6a0807 458         return batt_charge, false
eac484 459     end
S 460     -- Set meta data for the tool if it didn't do it itself :-(
5cf765 461     local src_meta = minetest.deserialize(srcstack:get_metadata())
eac484 462     src_meta = src_meta or {}
S 463     if not src_meta.charge then
464         src_meta.charge = 0
465     end
466
467     -- Do the discharging
468     local item_max_charge = technic.power_tools[toolname]
469     local tool_charge     = src_meta.charge
6a0807 470     if tool_charge <= 0 then
N 471         return batt_charge, true
472     elseif batt_charge >= max_charge then
473         return batt_charge, false
eac484 474     end
S 475     charge_step = math.min(charge_step, max_charge - batt_charge)
476     charge_step = math.min(charge_step, tool_charge)
477     tool_charge = tool_charge - charge_step
478     batt_charge = batt_charge + charge_step
479     technic.set_RE_wear(srcstack, tool_charge, item_max_charge)
480     src_meta.charge = tool_charge
5cf765 481     srcstack:set_metadata(minetest.serialize(src_meta))
eac484 482     inv:set_stack("dst", 1, srcstack)
6a0807 483     return batt_charge, (tool_charge == 0)
ee0765 484 end
S 485