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




export function initVoid() {

    
    function getDistance(pos1, pos2) {
        if (!pos1 || !pos2) {
            return Infinity; 
        }
        return Math.sqrt(
            Math.pow(pos1.x - pos2.x, 2) +
            Math.pow(pos1.y - pos2.y, 2) +
            Math.pow(pos1.z - pos2.z, 2)
        );
    }

    const overworld = fo.overworld;

    system.runInterval(() => {
        
        const players = overworld.getPlayers().filter(player => player.location.y < -39);

        
        const processedPlayers = new Set();

        for (const player of players) {
            
            if (processedPlayers.has(player)) continue;

            
            const nearbyPlayers = world.getPlayers().filter(
                p => p !== player &&
                    p.location.y < -39 && 
                    getDistance(p.location, player.location) <= 25 
            );

            
            nearbyPlayers.push(player);

            
            const targetPlayer = nearbyPlayers.length > 0
                ? nearbyPlayers[Math.floor(Math.random() * nearbyPlayers.length)]
                : player;


            const ParticleLocation = {
                x: targetPlayer.location.x,
                y: -55,
                z: targetPlayer.location.z,

            };

            
            overworld.spawnParticle("fo_void", ParticleLocation);
            

            
            nearbyPlayers.forEach(p => processedPlayers.add(p));
        }
    }, 120); 

}