import { world, system } from "@minecraft/server";

let mutationActive = false;

// Check the day count every 5 seconds (100 ticks)
system.runInterval(() => {
    const dayCount = Math.floor(world.getTime() / 24000);

    // Activate mutation permanently from Day 40 onwards
    if (dayCount >= 40 && !mutationActive) {
        mutationActive = true;
        world.sendMessage("Zombies have mutated! They can now track you through walls...");
    }

    if (mutationActive) {
        const players = world.getPlayers();

        players.forEach(player => {
            const playerPos = player.location;

            // Check if the player moves
            const isMoving = player.getVelocity().lengthSquared() > 0.01;

            if (isMoving) {
                // Find all zombies within a 70-block radius
                const nearbyZombies = world.getEntities({
                    type: "minecraft:zombie",
                    location: playerPos,
                    maxDistance: 70
                });

                // Make zombies track players through walls (ignores line-of-sight)
                nearbyZombies.forEach(zombie => {
                    zombie.navigateTo(playerPos, 1.0);
                });
            }
        });
    }
}, 100);  // Runs every 100 ticks (5 seconds)