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