Back to Home

ESO Lua File v100034

ingame/healthwarning/zo_healthwarning.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
local HEALTH_WARNING_PERCENTAGES =
{
    [HEALTH_WARNING_STAGE_1] = 0,
    [HEALTH_WARNING_STAGE_2] = 0.3,
    [HEALTH_WARNING_STAGE_3] = 0.15,
    [HEALTH_WARNING_FLASH_1] = 0.5,
    [HEALTH_WARNING_FLASH_2] = 0.4,
}
local FLASH_TIME_OUT_SECONDS =
{
    [HEALTH_WARNING_FLASH_1] = 0.3,
    [HEALTH_WARNING_FLASH_2] = 0.3,
}
local IS_HEALTH_FLASH =
{
    [HEALTH_WARNING_FLASH_1] = true,
    [HEALTH_WARNING_FLASH_2] = true,
}
local currentHealthPercent = 0
local function ShouldFlash(healthPercent, flashPercent)
    local healthWasAboveFlash = currentHealthPercent > flashPercent
    local healthIsNowBelowFlash = healthPercent <= flashPercent
    return healthWasAboveFlash and healthIsNowBelowFlash
end
local function GetWarningStage(healthPercent)
    if(healthPercent <= HEALTH_WARNING_PERCENTAGES[HEALTH_WARNING_STAGE_3])
    then
        return HEALTH_WARNING_STAGE_3
    elseif(healthPercent <= HEALTH_WARNING_PERCENTAGES[HEALTH_WARNING_STAGE_2])
    then
        return HEALTH_WARNING_STAGE_2
    elseif(healthPercent <= HEALTH_WARNING_PERCENTAGES[HEALTH_WARNING_STAGE_1])
    then
        return HEALTH_WARNING_STAGE_1
    elseif(ShouldFlash(healthPercent, HEALTH_WARNING_PERCENTAGES[HEALTH_WARNING_FLASH_2]))
    then
        return HEALTH_WARNING_FLASH_2
    elseif(ShouldFlash(healthPercent, HEALTH_WARNING_PERCENTAGES[HEALTH_WARNING_FLASH_1]))
    then
        return HEALTH_WARNING_FLASH_1
    else
        return HEALTH_WARNING_NONE
    end
end
local function ShowWarningForHealthPercent(healthPercent)
    local warningStage = GetWarningStage(healthPercent)
    if(IS_HEALTH_FLASH[warningStage])
    then
        FlashHealthWarningStage(warningStage, FLASH_TIME_OUT_SECONDS[warningStage] * 1000)
    else
        SetHealthWarningStage(warningStage)
    end
end
local function PlayerDead()
end
local function OnPowerUpdate(evt, tag, powerIndex, powerType, health, maxHealth)
    if IsUnitDead("player") then
        PlayerDead()
        return
    end
    local healthPercent = 0
    if(maxHealth and maxHealth > 0)
    then
        healthPercent = health / maxHealth
    end
    ShowWarningForHealthPercent(healthPercent)
    currentHealthPercent = healthPercent
end
local function UpdateHealthWarning()
    local health, maxHealth = GetUnitPower("player", POWERTYPE_HEALTH)
    if(maxHealth and maxHealth > 0)
    then
        currentHealthPercent = health / maxHealth
    else
        currentHealthPercent = 0
    end
    ShowWarningForHealthPercent(currentHealthPercent)
end
EVENT_MANAGER:RegisterForEvent("ZO_HealthWarning", EVENT_PLAYER_DEAD, PlayerDead)
EVENT_MANAGER:RegisterForEvent("ZO_HealthWarning", EVENT_PLAYER_ALIVE, UpdateHealthWarning)
EVENT_MANAGER:RegisterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, OnPowerUpdate)
EVENT_MANAGER:AddFilterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, REGISTER_FILTER_POWER_TYPE, POWERTYPE_HEALTH)
EVENT_MANAGER:AddFilterForEvent("ZO_HealthWarning", EVENT_POWER_UPDATE, REGISTER_FILTER_UNIT_TAG, "player")