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

const overworld = world.getDimension("overworld");


system.runInterval(() => {
  world.getAllPlayers().forEach((player) => {
    if (player.dimension.id !== "minecraft:overworld") return;
    if (!player || !player.isValid()) return;

    const coneStartLocation = getLocationFromFOV(player, { x: 0, y: 0 }, -1);
    const playerRotation = player.getRotation();

    if (!coneStartLocation) return;
    const scanPoints = generateConePoints(coneStartLocation, playerRotation, 8, 90);
    if (!scanPoints) return;

    try {
      for (const point of scanPoints) {
        const block = overworld.getBlock(point);
        if (block && block.typeId === "ecbl_bc:fake_air") {
          fillAir(block);
        }
      }
    } catch (e) { }
  });
});

function generateConePoints(origin, rotation, range, angleSpread) {
  try {
    const points = new Set();

    for (let dist = 1; dist <= range; dist++) {
      const circumference = 2 * Math.PI * dist;
      const angleStep = (360 / circumference) * 2;

      for (let vAngle = -angleSpread / 2; vAngle <= angleSpread / 2; vAngle += angleStep) {
        for (let hAngle = -angleSpread / 2; hAngle <= angleSpread / 2; hAngle += angleStep) {
          const radiansV = ((-rotation.x - vAngle) * Math.PI) / 180;
          const radiansH = ((rotation.y + 90 + hAngle) * Math.PI) / 180;

          const x = Math.round(origin.x + Math.cos(radiansV) * Math.cos(radiansH) * dist);
          const y = Math.round(origin.y + Math.sin(radiansV) * dist);
          const z = Math.round(origin.z + Math.cos(radiansV) * Math.sin(radiansH) * dist);

          points.add(`${x},${y},${z}`);
        }
      }
    }

    return Array.from(points).map((p) => {
      const [x, y, z] = p.split(",");
      return { x: Number(x), y: Number(y), z: Number(z) };
    });
  } catch { return; }

}

function fillAir(block) {
  const loc = block.location;
  const range = 3;
  for (let dx = -range; dx < range; dx++) {
    for (let dy = -range; dy < range; dy++) {
      for (let dz = -range; dz < range; dz++) {
        const block = overworld.getBlock({
          x: loc.x + dx,
          y: loc.y + dy,
          z: loc.z + dz,
        });
        if (block?.typeId === "ecbl_bc:fake_air") {
          block.setType("minecraft:air");
        }
      }
    }
  }
}

/**
 *
 * @param {Player} player
 * @param {{x: number, y: number}} [offset]
 * @returns {{x: number, y: number, z: number}}
 */
function getLocationFromFOV(player, offset = { x: 0, y: 0 }, range) {
  const playerRotation = player.getRotation();
  const playerLocation = player.getHeadLocation();

  const modifiedPlayerRotation = {
    x: -playerRotation.x + offset.x,
    y: playerRotation.y + 90 + offset.y,
  };

  const radiansX = (modifiedPlayerRotation.x * Math.PI) / 180;
  const radiansY = (modifiedPlayerRotation.y * Math.PI) / 180;

  const direction = {
    x: Math.cos(radiansX) * Math.cos(radiansY),
    y: Math.sin(radiansX),
    z: Math.cos(radiansX) * Math.sin(radiansY),
  };

  const location = {
    x: playerLocation.x + direction.x * range,
    y: playerLocation.y + direction.y * range,
    z: playerLocation.z + direction.z * range,
  };

  return location;
}
