Add hpregen for medics

This commit is contained in:
rubenwardy 2019-03-22 03:18:45 +00:00
parent 34d891c6c5
commit 72c7563a85
4 changed files with 111 additions and 6 deletions

View file

@ -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

View file

@ -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)

View file

@ -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",

View file

@ -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)