/*
    PROJECT Q IV
    (c) 2022 by Pstemarie

    Purple Worm OnDamaged Event Script

    Version: 1.0
    Created: 28 Jan 2003
    Author:  Lacero

*/

#include "NW_I0_GENERIC"
#include "q_i0_worm"

void main()
{
    if(!GetIsObjectValid(GetAttemptedAttackTarget()) && !GetIsObjectValid(GetAttemptedSpellTarget()))
    {
        if(GetIsObjectValid(GetLastDamager()))
        {
            pw_DetermineCombatRound();
        }
    }
    else if (!GetIsObjectValid(GetAttemptedSpellTarget()))
    {
        object oTarget = GetAttackTarget();
        if(!GetIsObjectValid(oTarget))
        {
            oTarget = GetAttemptedAttackTarget();
        }
        object oAttacker = GetLastHostileActor();
        if (GetIsObjectValid(oAttacker) && oTarget != oAttacker && GetIsEnemy(oAttacker) &&
           (GetTotalDamageDealt() > (GetMaxHitPoints(OBJECT_SELF) / 4) ||
            (GetHitDice(oAttacker) - 2) > GetHitDice(oTarget) ) )
        {
            pw_DetermineCombatRound(oAttacker);
        }
    }

    if(GetSpawnInCondition(NW_FLAG_DAMAGED_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(1006));
    }
}

