{
    name: "Apocalypse",
    rating: 9.9, 
    num: -356001,
    
    onStart(pokemon) {
        try {
            for (const target of pokemon.foes()) {
                if (target.isSemiInvulnerable()) continue;
                
                if (!target.addVolatile('yawn')) {
                    this.add('-immune', target, '[from] ability: Apocalypse');
                }
            }
        } catch (e) {
            console.error("Apocalypse onStart error:", e);
        }
        pokemon.apocalypseDamageTaken = 0;
        pokemon.apocalypseTurnCount = 0;
    },
    
    onTryHit(pokemon, target, move) {
        if (move.ohko) {
            this.add("-immune", pokemon, "[from] ability: Apocalypse");
            return null;
        }
    },
    
    onDamage(damage, target, source, effect) {
        try {
            if (target.ability !== 'apocalypse') return damage;
            
            let finalDamage = damage;
            
            if (finalDamage > 200) {
                const excess = finalDamage - 200;
                const reductionPercent = Math.min(98, Math.floor(excess / 10) * 10);
                const reductionFactor = (100 - reductionPercent) / 100;
                finalDamage = (excess * reductionFactor);
                
                this.add('-activate', target, 'ability: Apocalypse', `[damage reduction ${reductionPercent}%]`);
            }
            
            
            if (!target.apocalypseDamageTaken) target.apocalypseDamageTaken = 0;
            target.apocalypseDamageTaken += finalDamage;
            
            const threshold = Math.floor(target.maxhp * 0.1);
            if (target.apocalypseDamageTaken >= threshold && threshold > 0) {
                const boosts = Math.floor(target.apocalypseDamageTaken / threshold);
                
                const actualBoosts = Math.min(boosts, 6 - (target.boosts?.spe || 0));
                if (actualBoosts > 0) {
                    this.boost({spe: actualBoosts}, target, target);
                    this.add('-activate', target, 'ability: Apocalypse', '[speed boost]');
                }
                
                target.apocalypseDamageTaken = target.apocalypseDamageTaken % threshold;
            }
            
            return finalDamage;
        } catch (e) {
            console.error("Apocalypse onDamage error:", e);
            return damage;
        }
    },
    
    onModifyDamage(damage, source, target, move) {
        if (source && source !== this.effectState.target && source.ability !== 'apocalypse') {
            return this.chainModify(0.5);
        }
    },
    
    onModifyMovePriority: -5,
    onModifyMove(move) {
        if (!move.ignoreImmunity) move.ignoreImmunity = {};
        if (move.ignoreImmunity !== true) {
            move.ignoreImmunity["Fighting"] = true;
            move.ignoreImmunity["Normal"] = true;
        }
    },
    
    onTryBoost(boost, target, source, effect) {
        if (effect.name === "Intimidate" && boost.atk) {
            delete boost.atk;
            this.add("-fail", target, "unboost", "Attack", "[from] ability: Apocalypse", "[of] " + target);
        }
    },
    
    onSetStatus(status, target, source, effect) {
        if (effect?.status) {
            this.add("-immune", target, "[from] ability: Apocalypse");
        }
        return false;
    },
    onTryAddVolatile(status, target) {
    if (status.id === "yawn" || status.id === "confusion") {
        this.add("-immune", target, "[from] ability: Apocalypse");
        return null;
        }
    }
}