From 521c0b74bba5dd73cf7c0a59fde4a018813e835d Mon Sep 17 00:00:00 2001 From: SmallJoker <mk939@ymail.com> Date: Thu, 24 Nov 2022 21:49:20 +0100 Subject: [PATCH] Fix battery box charging on Windows --- technic/machines/register/battery_box.lua | 186 ++++++++++++++++++++++++++++++++------------- 1 files changed, 131 insertions(+), 55 deletions(-) diff --git a/technic/machines/register/battery_box.lua b/technic/machines/register/battery_box.lua index e26ee09..190ddbf 100644 --- a/technic/machines/register/battery_box.lua +++ b/technic/machines/register/battery_box.lua @@ -12,12 +12,59 @@ technic.register_power_tool("technic:green_energy_crystal", 150000) technic.register_power_tool("technic:blue_energy_crystal", 450000) +-- Battery recipes: +-- Tin-copper recipe: minetest.register_craft({ - output = 'technic:battery', + output = "technic:battery", recipe = { - {'group:wood', 'default:copper_ingot', 'group:wood'}, - {'group:wood', 'moreores:tin_ingot', 'group:wood'}, - {'group:wood', 'default:copper_ingot', 'group:wood'}, + {"group:wood", "default:copper_ingot", "group:wood"}, + {"group:wood", "default:tin_ingot", "group:wood"}, + {"group:wood", "default:copper_ingot", "group:wood"}, + } +}) +-- Sulfur-lead-water recipes: +-- With sulfur lumps: +-- With water: +minetest.register_craft({ + output = "technic:battery", + recipe = { + {"group:wood", "technic:sulfur_lump", "group:wood"}, + {"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"}, + {"group:wood", "technic:sulfur_lump", "group:wood"}, + }, + replacements = { + {"bucket:bucket_water", "bucket:bucket_empty"} + } +}) +-- With oil extract: +minetest.register_craft({ + output = "technic:battery", + recipe = { + {"group:wood", "technic:sulfur_lump", "group:wood"}, + {"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"}, + {"group:wood", "technic:sulfur_lump", "group:wood"}, + } +}) +-- With sulfur dust: +-- With water: +minetest.register_craft({ + output = "technic:battery", + recipe = { + {"group:wood", "technic:sulfur_dust", "group:wood"}, + {"technic:lead_ingot", "bucket:bucket_water", "technic:lead_ingot"}, + {"group:wood", "technic:sulfur_dust", "group:wood"}, + }, + replacements = { + {"bucket:bucket_water", "bucket:bucket_empty"} + } +}) +-- With oil extract: +minetest.register_craft({ + output = "technic:battery", + recipe = { + {"group:wood", "technic:sulfur_dust", "group:wood"}, + {"technic:lead_ingot", "homedecor:oil_extract", "technic:lead_ingot"}, + {"group:wood", "technic:sulfur_dust", "group:wood"}, } }) @@ -35,26 +82,38 @@ } }) +-- x+2 + (z+2)*2 +local dirtab = { + [4] = 2, + [5] = 3, + [7] = 1, + [8] = 0 +} + local tube = { insert_object = function(pos, node, stack, direction) - if direction.y == 0 then + print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2) + if direction.y == 1 + or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then return stack end local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - if direction.y > 0 then + if direction.y == 0 then return inv:add_item("src", stack) else return inv:add_item("dst", stack) end end, can_insert = function(pos, node, stack, direction) - if direction.y == 0 then + print(minetest.pos_to_string(direction), dirtab[direction.x+2+(direction.z+2)*2], node.param2) + if direction.y == 1 + or (direction.y == 0 and dirtab[direction.x+2+(direction.z+2)*2] == node.param2) then return false end local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - if direction.y > 0 then + if direction.y == 0 then if meta:get_int("split_src_stacks") == 1 then stack = stack:peek_item(1) end @@ -66,15 +125,15 @@ return inv:room_for_item("dst", stack) end end, - connect_sides = {left=1, right=1, back=1, top=1, bottom=1}, + connect_sides = {left=1, right=1, back=1, top=1}, } local function add_on_off_buttons(meta, ltier, charge_percent) - local formspec = "" - if ltier == "mv" or ltier == "hv" then - formspec = "image[1,1;1,2;technic_power_meter_bg.png" + local formspec = "image[1,1;1,2;technic_power_meter_bg.png" .."^[lowpart:"..charge_percent - ..":technic_power_meter_fg.png]".. + ..":technic_power_meter_fg.png]" + if ltier == "mv" or ltier == "hv" then + formspec = formspec.. fs_helpers.cycling_button( meta, "image_button[3,2.0;1,0.6", @@ -133,11 +192,10 @@ end local run = function(pos, node) - local below = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) - local meta = minetest.get_meta(pos) + local meta = minetest.get_meta(pos) + local network_id = tonumber(meta:get_string(tier.."_network")) - if below.name ~= "technic:"..ltier.."_cable" - and not string.find(below.name, "technic:"..ltier.."_cable_plate") then + if not technic.networks[network_id] then meta:set_string("infotext", S("%s Battery Box Has No Network"):format(tier)) return end @@ -197,8 +255,9 @@ local charge_percent = math.floor(current_charge / max_charge * 100) meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, charge_percent)) - local infotext = S("@1 Battery Box: @2/@3", tier, - technic.pretty_num(current_charge), technic.pretty_num(max_charge)) + local infotext = S("@1 Battery Box: @2 / @3", tier, + technic.EU_string(current_charge), + technic.EU_string(max_charge)) if eu_input == 0 then infotext = S("%s Idle"):format(infotext) end @@ -220,13 +279,12 @@ local top_tex = "technic_"..ltier.."_battery_box_top.png"..tube_entry local front_tex = "technic_"..ltier.."_battery_box_front.png^technic_power_meter"..i..".png" local side_tex = "technic_"..ltier.."_battery_box_side.png"..tube_entry - local bottom_tex = "technic_"..ltier.."_battery_box_bottom.png"..tube_entry + local bottom_tex = "technic_"..ltier.."_battery_box_bottom.png"..cable_entry if ltier == "lv" then top_tex = "technic_"..ltier.."_battery_box_top.png" front_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png" side_tex = "technic_"..ltier.."_battery_box_side.png^technic_power_meter"..i..".png" - bottom_tex = "technic_"..ltier.."_battery_box_bottom.png"..cable_entry end minetest.register_node("technic:"..ltier.."_battery_box"..i, { @@ -246,15 +304,14 @@ drop = "technic:"..ltier.."_battery_box0", on_construct = function(pos) local meta = minetest.get_meta(pos) - local EU_upgrade, tube_upgrade = 0, 0 + local EU_upgrade, _ = 0 if data.upgrade then - EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta) + EU_upgrade, _ = technic.handle_machine_upgrades(meta) end local max_charge = data.max_charge * (1 + EU_upgrade / 10) local charge = meta:get_int("internal_EU_charge") local cpercent = math.floor(charge / max_charge * 100) local inv = meta:get_inventory() - local node = minetest.get_node(pos) meta:set_string("infotext", S("%s Battery Box"):format(tier)) meta:set_string("formspec", formspec..add_on_off_buttons(meta, ltier, cpercent)) meta:set_string("channel", ltier.."_battery_box"..minetest.pos_to_string(pos)) @@ -272,11 +329,11 @@ allow_metadata_inventory_take = technic.machine_inventory_take, allow_metadata_inventory_move = technic.machine_inventory_move, technic_run = run, + on_rotate = screwdriver.rotate_simple, after_place_node = data.tube and pipeworks.after_place, after_dig_node = technic.machine_after_dig_node, on_receive_fields = function(pos, formname, fields, sender) local meta = minetest.get_meta(pos) - local nodename = minetest.get_node(pos).name if fields.edit_channel then minetest.show_formspec(sender:get_player_name(), "technic:battery_box_edit_channel"..minetest.pos_to_string(pos), @@ -285,12 +342,12 @@ or fields["fs_helpers_cycling:0:split_dst_stacks"] or fields["fs_helpers_cycling:1:split_src_stacks"] or fields["fs_helpers_cycling:1:split_dst_stacks"] then - local meta = minetest.get_meta(pos) + meta = minetest.get_meta(pos) if not pipeworks.may_configure(pos, sender) then return end fs_helpers.on_receive_fields(pos, fields) - local EU_upgrade, tube_upgrade = 0, 0 + local EU_upgrade, _ = 0 if data.upgrade then - EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta) + EU_upgrade, _ = technic.handle_machine_upgrades(meta) end local max_charge = data.max_charge * (1 + EU_upgrade / 10) local charge = meta:get_int("internal_EU_charge") @@ -352,6 +409,30 @@ end ) +local function default_get_charge(itemstack) + -- check if is chargable + local tool_name = itemstack:get_name() + if not technic.power_tools[tool_name] then + return 0, 0 + end + -- Set meta data for the tool if it didn't do it itself + local item_meta = minetest.deserialize(itemstack:get_metadata()) or {} + if not item_meta.charge then + item_meta.charge = 0 + end + return item_meta.charge, technic.power_tools[tool_name] +end + +local function default_set_charge(itemstack, charge) + local tool_name = itemstack:get_name() + if technic.power_tools[tool_name] then + technic.set_RE_wear(itemstack, charge, technic.power_tools[tool_name]) + end + local item_meta = minetest.deserialize(itemstack:get_metadata()) or {} + item_meta.charge = charge + itemstack:set_metadata(minetest.serialize(item_meta)) +end + function technic.charge_tools(meta, batt_charge, charge_step) local inv = meta:get_inventory() if inv:is_empty("src") then @@ -359,18 +440,18 @@ end local src_stack = inv:get_stack("src", 1) - local tool_name = src_stack:get_name() - if not technic.power_tools[tool_name] then + -- get callbacks + local src_def = src_stack:get_definition() + local technic_get_charge = src_def.technic_get_charge or default_get_charge + local technic_set_charge = src_def.technic_set_charge or default_set_charge + + -- get tool charge + local tool_charge, item_max_charge = technic_get_charge(src_stack) + if item_max_charge==0 then return batt_charge, false end - -- Set meta data for the tool if it didn't do it itself - local src_meta = minetest.deserialize(src_stack:get_metadata()) or {} - if not src_meta.charge then - src_meta.charge = 0 - end + -- Do the charging - local item_max_charge = technic.power_tools[tool_name] - local tool_charge = src_meta.charge if tool_charge >= item_max_charge then return batt_charge, true elseif batt_charge <= 0 then @@ -380,9 +461,7 @@ charge_step = math.min(charge_step, item_max_charge - tool_charge) tool_charge = tool_charge + charge_step batt_charge = batt_charge - charge_step - technic.set_RE_wear(src_stack, tool_charge, item_max_charge) - src_meta.charge = tool_charge - src_stack:set_metadata(minetest.serialize(src_meta)) + technic_set_charge(src_stack, tool_charge) inv:set_stack("src", 1, src_stack) return batt_charge, (tool_charge == item_max_charge) end @@ -393,21 +472,20 @@ if inv:is_empty("dst") then return batt_charge, false end - srcstack = inv:get_stack("dst", 1) - local toolname = srcstack:get_name() - if technic.power_tools[toolname] == nil then + local src_stack = inv:get_stack("dst", 1) + + -- get callbacks + local src_def = src_stack:get_definition() + local technic_get_charge = src_def.technic_get_charge or default_get_charge + local technic_set_charge = src_def.technic_set_charge or default_set_charge + + -- get tool charge + local tool_charge, item_max_charge = technic_get_charge(src_stack) + if item_max_charge==0 then return batt_charge, false - end - -- Set meta data for the tool if it didn't do it itself :-( - local src_meta = minetest.deserialize(srcstack:get_metadata()) - src_meta = src_meta or {} - if not src_meta.charge then - src_meta.charge = 0 end -- Do the discharging - local item_max_charge = technic.power_tools[toolname] - local tool_charge = src_meta.charge if tool_charge <= 0 then return batt_charge, true elseif batt_charge >= max_charge then @@ -417,10 +495,8 @@ charge_step = math.min(charge_step, tool_charge) tool_charge = tool_charge - charge_step batt_charge = batt_charge + charge_step - technic.set_RE_wear(srcstack, tool_charge, item_max_charge) - src_meta.charge = tool_charge - srcstack:set_metadata(minetest.serialize(src_meta)) - inv:set_stack("dst", 1, srcstack) + technic_set_charge(src_stack, tool_charge) + inv:set_stack("dst", 1, src_stack) return batt_charge, (tool_charge == 0) end -- Gitblit v1.8.0