#ifndef INCLUDE_ENDER_LIGHTNING
    #define INCLUDE_ENDER_LIGHTNING

    // Procedural "petir" (lightning-like) flash effect for The End sky.
    // Works purely off frameTimeCounter, so it's independent of the vanilla
    // 1.21.9 End-flash feature (END_FLASHES) and needs no extra uniforms -
    // it will flash on any Iris/OptiFine version.

    float EnderLightningHash(float x) {
        return fract(sin(x * 12.9898) * 43758.5453);
    }

    // Returns a 0-1 brightness for the current frame - a quick rise then a
    // slower decay, occasionally double-pulsing, like distant lightning.
    float GetEnderLightningFlash() {
        float t = frameTimeCounter * 0.05 * ENDER_LIGHTNING_FREQ;
        float cycle = floor(t);
        float cyclePhase = fract(t);

        float strikeChance = EnderLightningHash(cycle);
        if (strikeChance > 0.55) return 0.0; // most cycles have no strike

        float flashStart = EnderLightningHash(cycle + 0.37) * 0.55;
        float flashLen = 0.03 + 0.05 * EnderLightningHash(cycle + 0.81);

        float localT = cyclePhase - flashStart;

        if (localT < 0.0 || localT > flashLen) {
            // Occasional quick double-pulse shortly after the main flash
            float secondStart = flashStart + flashLen + 0.03 + 0.05 * EnderLightningHash(cycle + 1.53);
            float secondLen = flashLen * 0.5;
            float localT2 = cyclePhase - secondStart;

            if (localT2 < 0.0 || localT2 > secondLen || EnderLightningHash(cycle + 2.1) > 0.5) return 0.0;

            localT = localT2;
            flashLen = secondLen;
        }

        float flashProgress = clamp(localT / max(flashLen, 0.0001), 0.0, 1.0);
        float flash = flashProgress < 0.2
            ? flashProgress / 0.2
            : 1.0 - (flashProgress - 0.2) / 0.8;
        flash = clamp(flash, 0.0, 1.0);
        flash *= flash;

        return flash;
    }

#endif //INCLUDE_ENDER_LIGHTNING
