import { world, system } from "@minecraft/server";
import { fo } from './utilities/fo_constants.js';



export function initSheep() {

    
    const chance = 1 / 40; 
    const forebodingCheck = fo.scoreChapter3; 
    const searchRadius = 36; 
    const interval = 3600; 

    
    function shearRandomSheep() {
        
        const sheepEntities = fo.overworld.getEntities({
            type: "minecraft:sheep"
        });

        if (sheepEntities.length === 0) system.runTimeout(shearRandomSheep, 1207);

        const randomSheep = sheepEntities[Math.floor(Math.random() * sheepEntities.length)];

        const nearbyPlayers = fo.overworld.getPlayers({
            Location: randomSheep.location,
            maxDistance: searchRadius,
        });

        if (nearbyPlayers.length !== 0) system.runTimeout(shearRandomSheep, 1207);

        try {
            randomSheep.triggerEvent("minecraft:on_sheared");
        } catch (e) { system.runTimeout(shearRandomSheep, 1207) }
    }

    system.runInterval(() => {
        if (fo.forebodingScore > forebodingCheck && Math.random() < (chance * fo.netProbabilityModifier)) {

            shearRandomSheep();
        }

    }, interval);

}