// Constants
const int OBJECT_TYPE_PLACEABLE = 64;

void FireBeamAtTarget(int nTargetNumber, int bFinalTarget = FALSE)
{
    object oSource = OBJECT_SELF; // The object that was clicked
    object oTarget = GetNearestObject(OBJECT_TYPE_PLACEABLE, oSource, nTargetNumber);

    if(!GetIsObjectValid(oTarget))
    {
        SendMessageToPC(GetFirstPC(), "Target placeable #" + IntToString(nTargetNumber) + " not found.");
        return; // Exit if the target placeable is not found
    }

    SendMessageToPC(GetFirstPC(), "Target placeable #" + IntToString(nTargetNumber) + " found.");

    // Rest of the beam firing code...
}

void main()
{
    SendMessageToPC(GetFirstPC(), "Script started. Source object clicked.");

    // Loop through all targets
    for (int i = 1; i <= 12; i++)
    {
        FireBeamAtTarget(i);
        DelayCommand(0.25, FireBeamAtTarget(i, FALSE)); // Delay for each beam
    }

    // Randomly choose a placeable to stop at after 15 seconds
    int nRandomPlaceable = Random(12) + 1;
    DelayCommand(15.0, FireBeamAtTarget(nRandomPlaceable, TRUE)); // Apply final effect on this target

    SendMessageToPC(GetFirstPC(), "Random target for final effect: #" + IntToString(nRandomPlaceable));
}



/*
void main()
{
    object oSelf = OBJECT_SELF;
    object oArea = GetArea(oSelf);
    
    effect eLightSmall = EffectVisualEffect(VFX_COM_HIT_FIRE);
    
    vector vPos;
    vector vPC = GetPosition(oSelf);
    
    location lStar;
    
    int iCount;
    int iRand = d4(1)+2;
    
    float fD;
    float fS = 3.0;
    float fTimer = 0.08;
    float fDelay = 0.03;
    
    for (iCount = 0; iCount <= iRand; iCount++)
    {
        for(fD = 0.0; fD < 360.0; fD += 18.0)
        {
            vPos = Vector(vPC.x + sin(fD) * fS,
                         vPC.y + cos(fD) * fS, vPC.z + 0.1);
    
            lStar = Location(oArea, vPos, 0.0);
            
            DelayCommand(fTimer, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eLightSmall, lStar));
            
            fDelay = fDelay*1.02;
            
            SendMessageToPC(oSelf, FloatToString(fDelay));
            
            if (fDelay < 0.01) fDelay = 0.01;
            
            fTimer += fDelay;
        }
    }
} */
