From 718a5beda197cd0a775fd41aedfb5b1bbd5d1d8b Mon Sep 17 00:00:00 2001 From: ZettaScript <contact@zettascript.org> Date: Thu, 20 Oct 2022 17:56:10 +0200 Subject: [PATCH] Supply converter: demany only the needed power (#577) --- technic/tools/mining_lasers.lua | 155 +++++++++++++++++---------------------------------- 1 files changed, 52 insertions(+), 103 deletions(-) diff --git a/technic/tools/mining_lasers.lua b/technic/tools/mining_lasers.lua index 4c83ae9..e4dd178 100644 --- a/technic/tools/mining_lasers.lua +++ b/technic/tools/mining_lasers.lua @@ -4,175 +4,124 @@ {"2", 14, 200000, 2000}, {"3", 21, 650000, 3000}, } +local allow_entire_discharging = true local S = technic.getter minetest.register_craft({ - output = 'technic:laser_mk1', + output = "technic:laser_mk1", recipe = { - {'default:diamond', 'technic:brass_ingot', 'default:obsidian_glass'}, - {'', 'technic:brass_ingot', 'technic:red_energy_crystal'}, - {'', '', 'default:copper_ingot'}, + {"default:diamond", "basic_materials:brass_ingot", "default:obsidian_glass"}, + {"", "basic_materials:brass_ingot", "technic:red_energy_crystal"}, + {"", "", "default:copper_ingot"}, } }) minetest.register_craft({ - output = 'technic:laser_mk2', + output = "technic:laser_mk2", recipe = { - {'default:diamond', 'technic:carbon_steel_ingot', 'technic:laser_mk1'}, - {'', 'technic:carbon_steel_ingot', 'technic:green_energy_crystal'}, - {'', '', 'default:copper_ingot'}, + {"default:diamond", "technic:carbon_steel_ingot", "technic:laser_mk1"}, + {"", "technic:carbon_steel_ingot", "technic:green_energy_crystal"}, + {"", "", "default:copper_ingot"}, } }) minetest.register_craft({ - output = 'technic:laser_mk3', + output = "technic:laser_mk3", recipe = { - {'default:diamond', 'technic:carbon_steel_ingot', 'technic:laser_mk2'}, - {'', 'technic:carbon_steel_ingot', 'technic:blue_energy_crystal'}, - {'', '', 'default:copper_ingot'}, + {"default:diamond", "technic:carbon_steel_ingot", "technic:laser_mk2"}, + {"", "technic:carbon_steel_ingot", "technic:blue_energy_crystal"}, + {"", "", "default:copper_ingot"}, } }) - --- Based on code by Uberi: https://gist.github.com/Uberi/3125280 -local function rayIter(pos, dir, range) - local p = vector.round(pos) - local x_step, y_step, z_step = 0, 0, 0 - local x_component, y_component, z_component = 0, 0, 0 - local x_intersect, y_intersect, z_intersect = 0, 0, 0 - - if dir.x == 0 then - x_intersect = math.huge - elseif dir.x > 0 then - x_step = 1 - x_component = 1 / dir.x - x_intersect = x_component - else - x_step = -1 - x_component = 1 / -dir.x - end - if dir.y == 0 then - y_intersect = math.huge - elseif dir.y > 0 then - y_step = 1 - y_component = 1 / dir.y - y_intersect = y_component - else - y_step = -1 - y_component = 1 / -dir.y - end - if dir.z == 0 then - z_intersect = math.huge - elseif dir.z > 0 then - z_step = 1 - z_component = 1 / dir.z - z_intersect = z_component - else - z_step = -1 - z_component = 1 / -dir.z - end - - return function() - if x_intersect < y_intersect then - if x_intersect < z_intersect then - p.x = p.x + x_step - x_intersect = x_intersect + x_component - else - p.z = p.z + z_step - z_intersect = z_intersect + z_component - end - elseif y_intersect < z_intersect then - p.y = p.y + y_step - y_intersect = y_intersect + y_component - else - p.z = p.z + z_step - z_intersect = z_intersect + z_component - end - if vector.distance(pos, p) > range then - return nil - end - return p - end -end local function laser_node(pos, node, player) local def = minetest.registered_nodes[node.name] - if def and def.liquidtype ~= "none" then + if def.liquidtype ~= "none" and def.buildable_to then minetest.remove_node(pos) minetest.add_particle({ pos = pos, - vel = {x=0, y=2, z=0}, - acc = {x=0, y=-1, z=0}, - expirationtime = 1.5, + velocity = {x = 0, y = 1.5 + math.random(), z = 0}, + acceleration = {x = 0, y = -1, z = 0}, size = 6 + math.random() * 2, texture = "smoke_puff.png^[transform" .. math.random(0, 7), }) return end - minetest.node_dig(pos, node, player) + def.on_dig(pos, node, player) end -local no_destroy = { - ["air"] = true, - ["default:lava_source"] = true, - ["default:lava_flowing"] = true, -} +local keep_node = {air = true} +local function can_keep_node(name) + if keep_node[name] ~= nil then + return keep_node[name] + end + keep_node[name] = minetest.get_item_group(name, "hot") ~= 0 + return keep_node[name] +end + local function laser_shoot(player, range, particle_texture, sound) - local player_pos = player:getpos() + local player_pos = player:get_pos() local player_name = player:get_player_name() local dir = player:get_look_dir() local start_pos = vector.new(player_pos) -- Adjust to head height - start_pos.y = start_pos.y + 1.9 + start_pos.y = start_pos.y + (player:get_properties().eye_height or 1.625) minetest.add_particle({ - pos = startpos, - vel = dir, - acc = vector.multiply(dir, 50), - expirationtime = range / 11, + pos = start_pos, + velocity = dir, + acceleration = vector.multiply(dir, 50), + expirationtime = (math.sqrt(1 + 100 * (range + 0.4)) - 1) / 50, size = 1, texture = particle_texture .. "^[transform" .. math.random(0, 7), }) minetest.sound_play(sound, {pos = player_pos, max_hear_distance = range}) - for pos in rayIter(start_pos, dir, range) do + for pos in technic.trace_node_ray_fat(start_pos, dir, range) do if minetest.is_protected(pos, player_name) then minetest.record_protection_violation(pos, player_name) break end - local node = minetest.get_node_or_nil(pos) - if not node then + local node = minetest.get_node(pos) + if node.name == "ignore" + or not minetest.registered_nodes[node.name] then break end - if not no_destroy[node.name] then + if not can_keep_node(node.name) then laser_node(pos, node, player) end end end - for _, m in pairs(mining_lasers_list) do technic.register_power_tool("technic:laser_mk"..m[1], m[3]) minetest.register_tool("technic:laser_mk"..m[1], { description = S("Mining Laser Mk%d"):format(m[1]), inventory_image = "technic_mining_laser_mk"..m[1]..".png", + range = 0, stack_max = 1, wear_represents = "technic_RE_charge", on_refill = technic.refill_RE_charge, on_use = function(itemstack, user) local meta = minetest.deserialize(itemstack:get_metadata()) - if not meta or not meta.charge then + if not meta or not meta.charge or meta.charge == 0 then return end - -- If there's enough charge left, fire the laser - if meta.charge >= m[4] then - laser_shoot(user, m[2], "technic_laser_beam_mk"..m[1]..".png", "technic_laser_mk"..m[1]) - if not technic.creative_mode then - meta.charge = meta.charge - m[4] - technic.set_RE_wear(itemstack, meta.charge, m[3]) - itemstack:set_metadata(minetest.serialize(meta)) + local range = m[2] + if meta.charge < m[4] then + if not allow_entire_discharging then + return end + -- If charge is too low, give the laser a shorter range + range = range * meta.charge / m[4] + end + laser_shoot(user, range, "technic_laser_beam_mk" .. m[1] .. ".png", + "technic_laser_mk" .. m[1]) + if not technic.creative_mode then + meta.charge = math.max(meta.charge - m[4], 0) + technic.set_RE_wear(itemstack, meta.charge, m[3]) + itemstack:set_metadata(minetest.serialize(meta)) end return itemstack end, }) end - -- Gitblit v1.8.0