#include "jump_include"
#include "jump_inc_pattern"

void Jump_DelayedSparkCrumbs();
void Jump_DelayedSparkCrumbs()
{
    location lDrop = GetLocalLocation(OBJECT_SELF, "COOKIE_SPARK_LOCATION");
    vector vPos = GetPositionFromLocation(lDrop);
    vPos.z += 0.5;

    location lVFX = Location(GetAreaFromLocation(lDrop), vPos, GetFacingFromLocation(lDrop));
    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(118), lVFX);
}

void Jump_CookieCrumbleTrail(int iInterval = 20, float fDelay = 0.02, float fExplosionDelay = 0.4, float fCookieDuration = 2.0)
{
    float fDistanceLocal = GetDistanceBetweenLocations(lPC, lTarget);
    float fSkip = fDistanceLocal / IntToFloat(iInterval);
    float fDSkip = 0.0;
    float fIncrement = 0.0;
    float fFacing = GetFacing(oPC);

    int i, j;
    for (i = 0; i < iInterval; i++)
    {
        fIncrement += fSkip;
        fDSkip += fDelay;

        vector vBase = GetPositionFromLocation(GenerateNewLocationFromLocation(lPC, fIncrement, fFacing, 0.0));

        for (j = 0; j < 3; j++)
        {
            float fOffsetX = (IntToFloat(Random(1000)) / 1000.0) * 1.5 - 0.75;
            float fOffsetY = (IntToFloat(Random(1000)) / 1000.0) * 1.5 - 0.75;

            vector vCookie = Vector(vBase.x + fOffsetX, vBase.y + fOffsetY, vBase.z);
            location lDrop = Location(oArea, vCookie, fFacing);

            DelayCommand(fDSkip, Jump_SpawnPlaceableDeleyable("cas_cookieplc", lDrop, fCookieDuration));

            // Schedule crumb spark 0.5 units above
            SetLocalLocation(OBJECT_SELF, "COOKIE_SPARK_LOCATION", lDrop);
            DelayCommand(fDSkip + fExplosionDelay, ExecuteScript("cas_jumpcookie", OBJECT_SELF));
        }
    }
}

void main()
{
    // If this is being called as a delayed VFX handler
    if (GetLocalLocation(OBJECT_SELF, "COOKIE_SPARK_LOCATION") != GetInvalidLocation())
    {
        Jump_DelayedSparkCrumbs();
        DeleteLocalLocation(OBJECT_SELF, "COOKIE_SPARK_LOCATION");
        return;
    }

    float fDelay = 1.0;
    int bTurnToTarget = TRUE;

    if (ActivateJumper(fDelay, bTurnToTarget))
    {
        Jump_CookieCrumbleTrail();
    }
}





void main()
{
    float fDelay = 1.0;
    int bTurnToTarget = TRUE;

    if (ActivateJumper(fDelay, bTurnToTarget))
    {
        int iInterval = 10;

        Jump_CookieCrumbleTrail(iInterval, 0.05, 0.5, 2.0);
    }
}
