#include "cas_inc_rndloc"

#include "cas_inc_rndloc"

void main()
{
    SendMessageToPC(GetFirstPC(), "DEBUG: cas_firecannons script fired!");

    int nMsg = GetUserDefinedEventNumber();
    SendMessageToPC(GetFirstPC(), "DEBUG: UserDefined Event Number = " + IntToString(nMsg));
    if (nMsg != 1001) return;

    object oSelf = OBJECT_SELF;
    object oPC = GetFirstPC();
    string sCannonTag = GetTag(oSelf);
    SendMessageToPC(oPC, "DEBUG: Activated on object: " + sCannonTag);
    ActionSpeakString("Firing sequence started!");

    // Try to read a custom target tag from the placeable
    string sTargetTag = GetLocalString(oSelf, "TARGET_TAG");

    if (sTargetTag == "")
    {
        SendMessageToPC(oPC, "DEBUG: No TARGET_TAG local string. Falling back to nearest CAS_CANNONTARGET.");
        object oFallback = GetNearestObjectByTag("CAS_CANNONTARGET", oSelf);

        if (!GetIsObjectValid(oFallback))
        {
            SendMessageToPC(oPC, "ERROR: No fallback target found.");
            ActionSpeakString("ERROR: No target found!");
            return;
        }

        sTargetTag = GetTag(oFallback);
    }

    SendMessageToPC(oPC, "DEBUG: Using target tag: " + sTargetTag);
    object oTarget = GetWaypointByTag(sTargetTag);

    if (!GetIsObjectValid(oTarget))
    {
        SendMessageToPC(oPC, "ERROR: Target waypoint not found: " + sTargetTag);
        ActionSpeakString("ERROR: Target not found.");
        return;
    }

    location lSelf = GetLocation(oSelf);
    location lTarget = GetLocation(oTarget);
    vector vTarget = GetPositionFromLocation(lTarget);

    float fYOffset = RndFloat(20) * RndSign();
    float fZOffset = RndFloat(2);
    vTarget.y += fYOffset;
    vTarget.z += fZOffset;
    lTarget = Location(GetArea(oTarget), vTarget, GetFacing(oTarget));

    float fDistance = GetDistanceBetweenLocations(lTarget, lSelf);
    float fDelay = fDistance / 15.0;

    SendMessageToPC(oPC, "DEBUG: Firing at distance " + FloatToString(fDistance));
    SetFacingPoint(vTarget);
    ActionCastFakeSpellAtLocation(SPELL_FIREBALL, lTarget, PROJECTILE_PATH_TYPE_BALLISTIC);
    ActionSpeakString("BOOM!");

    // Explosion
    DelayCommand(fDelay, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_FIREBALL), lTarget));

    // Debris
    DelayCommand(fDelay + 0.1, ApplyEffectToObject(
        DURATION_TYPE_TEMPORARY,
        EffectVisualEffect(VFX_DUR_PROT_GREATER_STONESKIN),
        DestroyObjectReturn(CreateObject(
            OBJECT_TYPE_PLACEABLE,
            "plc_stones",
            lTarget, TRUE),
        IntToFloat(d6(4)))
    ));

    // Splash fire
    int i, iCount = d3(3);
    for (i = 1; i <= iCount; i++)
    {
        location lSplash = RndLoc(lTarget, 6, 2, BASE_SHAPE_CIRCLE);
        DelayCommand(fDelay + 0.2, ApplyEffectToObject(
            DURATION_TYPE_TEMPORARY,
            EffectVisualEffect(VFX_FNF_FIREBALL),
            DestroyObjectReturn(CreateObject(
                OBJECT_TYPE_PLACEABLE,
                "plc_flamesmall",
                lSplash, TRUE),
            IntToFloat(d6(3)))
        ));
    }

    PlaySound("as_pl_cannon");
}
