From 72c7563a859aecd152c10db3ef4738199dc52a64 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 22 Mar 2019 03:18:45 +0000 Subject: [PATCH] Add hpregen for medics --- mods/ctf/ctf_classes/init.lua | 22 +++++++++- mods/ctf/ctf_classes/regen.lua | 75 ++++++++++++++++++++++++++++++++++ mods/pvp/gauges/init.lua | 2 +- mods/pvp/hpregen/init.lua | 18 ++++++-- 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 mods/ctf/ctf_classes/regen.lua diff --git a/mods/ctf/ctf_classes/init.lua b/mods/ctf/ctf_classes/init.lua index bd19398..cc681b5 100644 --- a/mods/ctf/ctf_classes/init.lua +++ b/mods/ctf/ctf_classes/init.lua @@ -5,6 +5,7 @@ ctf_classes = { dofile(minetest.get_modpath("ctf_classes") .. "/api.lua") dofile(minetest.get_modpath("ctf_classes") .. "/gui.lua") +dofile(minetest.get_modpath("ctf_classes") .. "/regen.lua") ctf_classes.register("knight", { description = "Knight", @@ -67,9 +68,26 @@ local flags = { "ctf_flag:flag_top_blue", } -for _, flag in pairs(flags) do +for _, flagname in pairs(flags) do + local old_func = minetest.registered_nodes[flagname].on_punch + local function on_punch(pos, node, player, ...) + if ctf_classes.get(player).name == "medic" then + local flag = ctf_flag.get(pos) + local team = ctf.player(player:get_player_name()).team + if not flag or not flag.team or not team or team ~= flag.team then + minetest.chat_send_player(player:get_player_name(), + "Medics can't capture the flag!") + return + end + end + + return old_func(pos, node, player, ...) + end local function show(_, _, player) ctf_classes.show_gui(player:get_player_name(), player) end - minetest.override_item(flag, { on_rightclick = show }) + minetest.override_item(flagname, { + on_punch = on_punch, + on_rightclick = show, + }) end diff --git a/mods/ctf/ctf_classes/regen.lua b/mods/ctf/ctf_classes/regen.lua new file mode 100644 index 0000000..bde42a2 --- /dev/null +++ b/mods/ctf/ctf_classes/regen.lua @@ -0,0 +1,75 @@ +local regen_interval = tonumber(minetest.settings:get("regen_interval")) +if regen_interval <= 0 then + regen_interval = 6 +end + +local regen_amount = tonumber(minetest.settings:get("regen_amount")) +if regen_amount <= 0 then + regen_amount = 1 +end + +local function regen_update() + local get = ctf_classes.get + local players = minetest.get_connected_players() + local medic_by_team = { red = {}, blue = {} } + local tnames = {} + local found_medic = false + + -- First get medic positions and team names + for i=1, #players do + local player = players[i] + local pname = player:get_player_name() + local class = get(player) + local tname = ctf.player(pname).team + tnames[pname] = tname + if class.name == "medic" then + if tname then + medic_by_team[tname][#medic_by_team[tname] + 1] = player:get_pos() + found_medic = true + end + end + end + + if not found_medic then + return + end + + -- Next, update hp + + local function sqdist(a, b) + local x = a.x - b.x + local y = a.y - b.y + local z = a.z - b.z + return x*x + y*y + z*z + end + + for i=1, #players do + local player = players[i] + local pname = player:get_player_name() + local tname = tnames[pname] + local hp = player:get_hp() + local max_hp = player:get_properties().hp_max + if tname and hp ~= max_hp and hp > 0 then + local pos = player:get_pos() + local medics = medic_by_team[tname] + for j=1, #medics do + if sqdist(pos, medics[j]) < 100 then + hp = hp + regen_amount + player:set_hp(hp) + break + end + end + end + end +end + +local update = regen_interval / 2 +minetest.register_globalstep(function(delta) + update = update + delta + if update < regen_interval then + return + end + update = update - regen_interval + + regen_update() +end) diff --git a/mods/pvp/gauges/init.lua b/mods/pvp/gauges/init.lua index 775b451..d6ea7d9 100644 --- a/mods/pvp/gauges/init.lua +++ b/mods/pvp/gauges/init.lua @@ -34,7 +34,7 @@ function hp_bar:on_step(dtime) end local hp = math.floor(20 * wielder:get_hp() / wielder:get_properties().hp_max) - local breath = math.floor(20 * wielder:get_breath() / wielder:get_properties().breath_max) + local breath = math.floor(11 * wielder:get_breath() / wielder:get_properties().breath_max) self.object:set_properties({ textures = { "health_" .. tostring(hp) .. ".png^breath_" .. tostring(breath) .. ".png", diff --git a/mods/pvp/hpregen/init.lua b/mods/pvp/hpregen/init.lua index 7ef94ab..15222fb 100644 --- a/mods/pvp/hpregen/init.lua +++ b/mods/pvp/hpregen/init.lua @@ -15,9 +15,21 @@ local function regen_all() if newhp > player:get_properties().hp_max then newhp = player:get_properties().hp_max end - player:set_hp(newhp) + if oldhp ~= newhp then + player:set_hp(newhp) + end end end - minetest.after(regen_interval, regen_all) end -minetest.after(regen_interval, regen_all) + + +local update = 0 +minetest.register_globalstep(function(delta) + update = update + delta + if update < regen_interval then + return + end + update = update - regen_interval + + regen_all() +end)