--[[
Copyright: © Ирбиs , Real Wolf
]]

local hud_name = "hud_health"

-- записываем переменную
function save_variable(variable_name, value)
xr_logic.pstor_store(db.actor, variable_name, value)
end

-- загружаем переменную
function load_variable(variable_name, value_if_not_found)
return xr_logic.pstor_retrieve(db.actor, variable_name, value_if_not_found)
end

-- удаляем переменную
function del_variable(variable_name)
if db.storage[db.actor:id()].pstor[variable_name] then
db.storage[db.actor:id()].pstor[variable_name] = nil
end
end

-- координаты(параметры) x, y, width, height
local pbg = {x=0,y=0,w=0,h=0} --/ bg
local plv = {x=0,y=0,w=0,h=0} --/ lv

local skl_w = -1 --/ текущая длина шкалы
local wide = false --/ текущий режим экрана

local color = 0
local change_color = false
local change_wpn = false
local hud_show = false

function update(bShow)
local hud = get_hud()
local cs_bg = hud:GetCustomStatic("hud_health_bg")
local cs_lv = hud:GetCustomStatic("hud_health_lv")

if not db.actor:alive() then
	if cs_bg then hud:RemoveCustomStatic("hud_health_bg") end
	if cs_lv then hud:RemoveCustomStatic("hud_health_lv") end
	return
end

if (load_variable("opt_hp",true) == false or bShow == false or not db.actor:alive()) then
if cs_bg then hud:RemoveCustomStatic("hud_health_bg") end
if cs_lv then hud:RemoveCustomStatic("hud_health_lv") end
return
save_variable("opt_hp",false)
end

local hp = db.actor.health

if (hp == nil or hp == 0) then
if hud_show == false then --/ рамку можно не стирать при смене оружия
if cs_bg then hud:RemoveCustomStatic("hud_health_bg") end
end
if cs_lv then hud:RemoveCustomStatic("hud_health_lv") end
return
end

-- проверка смены режима экрана
if wide ~= db.wide then
wide = db.wide
if cs_bg then hud:RemoveCustomStatic("hud_health_bg") end
if cs_lv then hud:RemoveCustomStatic("hud_health_lv") end
cs_bg, cs_lv = nil, nil
end

local cur_hud = "hud_health_bg"
if cs_bg == nil then
hud:AddCustomStatic(cur_hud, true)
cs_bg = hud:GetCustomStatic(cur_hud)
local wnd = cs_bg:wnd()
if wnd then
pbg = read_params(cur_hud)
wnd:SetWndPos(pbg.x,pbg.y)
wnd:SetWidth (pbg.w)
wnd:SetHeight(pbg.h)
wnd:SetAutoDelete(true)
end
end

cur_hud = "hud_health_lv"
if cs_lv == nil then
hud:AddCustomStatic(cur_hud, true)
cs_lv = hud:GetCustomStatic(cur_hud)
local wnd = cs_lv:wnd()
if wnd ~= nil then
plv = read_params(cur_hud)
wnd:SetWndPos(pbg.x+plv.x,pbg.y+plv.y)
wnd:SetWidth (skl_w)
wnd:SetHeight(plv.h)
wnd:SetAutoDelete(true)
change_color = true --/ смена цвета
end
end

if cs_lv ~= nil then
local hp_w = math.floor(hp * plv.w)
if hp_w ~= skl_w then
if hp_w < 1 then
skl_w = -1
else
skl_w = hp_w
end
end
local texture_c = get_texture(hp)
local wnd = cs_lv:wnd()
wnd:SetWidth(skl_w) --/ Set Level Condition
wnd:InitTexture(texture_c) --/ Set ColorTexture
wnd:SetText(string.format(math.floor(hp*100+0.0001)).."%")
end

end

function read_params(cur_hud)
local ltx = ini_file("scripts\\new_hud_health.ltx")
local section = cur_hud
if wide then section = section.."_wide" end
if ltx and ltx:section_exist(section) then
local p = {x=0,y=0,w=0,h=0}
local result, idx, value, i
for i=0, ltx:line_count(section)-1 do
result, idx, value = ltx:r_line(section, i, "", "")
if idx == "x" then
p.x = tonumber(value)
elseif idx == "y" then
p.y = tonumber(value)
elseif idx == "width" then
p.w = tonumber(value)
elseif idx == "height" then
p.h = tonumber(value)
end
end
return p
end
end

function get_texture(hp)
local textures = {
[0] = "ui_hud_shk_health", --/ зеленая
[1] = "ui_hud_shk_health", --/ оранжевая
[2] = "ui_hud_shk_red" --/ красная
}
local clr = 0 --/ current color
if hp > 0.7 then clr = 0
elseif hp > 0.2 then clr = 1
elseif hp < 0.2 then clr = 2
end
if color ~= clr then
color = clr
change_color = true
else
change_color = false
end
return textures[color]
end 


----------------------------------/ постэффекты крови -----------------------


lite_treshold = 0.1 -- насколько должно уменьшиться здоровье с предыдущего обновления чтоб экран окрасился в красный
crit_treshold = 0.5 -- насколько должно уменьшиться здоровье с предыдущего обновления чтоб ГГ начало шатать
drop_item_on_crit_prob = 0.2 -- вероятность того что ГГ выронит оружие
effector_power_coeff = 0.7
prev_health = -1
chk_h_t = 0

--/ добавил табличку, т.к. много исключений. Real Wolf
nd_wpn = { bolt = true, bolt_d1 = true, bolt_d2 = true, bolt_d3 = true, wpn_knife_f = true, wpn_machette_f = true, wpn_crowbar = true }

function wounded_pp_update()
	if (chk_h_t or 0) < time_global() then
		chk_h_t = time_global()+1000
		if prev_health > (db.actor.health + lite_treshold) then
			level.add_pp_effector("fire_hit.ppe", 2011, false)
			local effector_power = (prev_health - db.actor.health)*100*effector_power_coeff
			level.set_pp_effector_factor(2011, effector_power)
			if prev_health > db.actor.health + crit_treshold then
				level.add_cam_effector("camera_effects\\shell_shock.anm", 999, false, "")
				local snd_obj = xr_sound.get_safe_sound_object([[actor\pain_3]])
				snd_obj:play_no_feedback(db.actor, sound_object.s2d, 0, vector(), 1.0)
				if math.random() < drop_item_on_crit_prob then
					local active_item = db.actor:active_item()
					if active_item and not nd_wpn[active_item:section()] then
						db.actor:drop_item(active_item)
					end
				end
			end
		end
		prev_health = db.actor.health
	end
end 

