/*
    PROJECT Q IV
    (c) 2022 by Pstemarie

    Troll OnUserDefined Event Script

       Remembers how much acid/fire damagethe troll has taken

    Leeched from hf_cs_troll_ud.NSS (c) Ossian Studios (used with permission)

    Version: 1.1
    Created:
    Author:
    v1.1:    30 May 2022 Pstemarie - moved setting of user defined events to an actual OnSpawn script
    v1.2:    8 Jun 2022 Pstemarie - added ice troll regeneration 4 to the Heartbeat function
*/

#include "nw_i0_generic"

const int EVENT_USER_DEFINED_PRESPAWN = 1510;
const int EVENT_USER_DEFINED_POSTSPAWN = 1511;

// regenerate any damage (except that caused by either fire or acid)
void Heartbeat()
{
    int nFireDmg = GetLocalInt(OBJECT_SELF, "nFireDmg");
    int nAcidDmg = GetLocalInt(OBJECT_SELF, "nAcidDmg");
    int nCurHP = GetCurrentHitPoints(OBJECT_SELF);
    int nMaxHP = GetMaxHitPoints(OBJECT_SELF) - nFireDmg - nAcidDmg;
    if (nCurHP < nMaxHP)
    {
        int nHeal = nMaxHP - nCurHP;
        if (nHeal > 5)
            nHeal = 5;
        //* Pstemarie - ice trolls regenerate 4 hp per round
        if (GetAppearanceType(OBJECT_SELF) == 15136) // APPEARANCE_TYPE_ICETROLL
        {
            nHeal = 4;
        }

        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectHeal(nHeal), OBJECT_SELF);
    }
}

// remember how much fire and acid damage was done to this troll
void Damaged()
{
    int nF = GetDamageDealtByType(DAMAGE_TYPE_FIRE);
    if (nF > 0)
    {
        int nFireDmg = GetLocalInt(OBJECT_SELF, "nFireDmg");
        SetLocalInt(OBJECT_SELF, "nFireDmg", nFireDmg + nF);
    }
    int nA = GetDamageDealtByType(DAMAGE_TYPE_ACID);
    if (nA > 0)
    {
        int nAcidDmg = GetLocalInt(OBJECT_SELF, "nAcidDmg");
        SetLocalInt(OBJECT_SELF, "nAcidDmg", nAcidDmg + nA);
    }
}

void main()
{
    int nUser = GetUserDefinedEventNumber();

    if (nUser == EVENT_HEARTBEAT)
    {
        Heartbeat();
    }
    else if (nUser == EVENT_DAMAGED)
    {
        Damaged();
    }
//    else if (nUser == EVENT_USER_DEFINED_POSTSPAWN)
//    {
//        SetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT);
//        SetSpawnInCondition(NW_FLAG_DAMAGED_EVENT);
//    }
}
