import*as mc from"@minecraft/server";import*as vector from"./vector";export function getEntitiesArc(t,o,e,n,i,c=!1,r=void 0){let l=[];const a=union({location:t.location,maxDistance:e,excludeFamilies:i},o,t.dimension,t.id),s=vector.add(t.location,vector.up);for(let o=0;o<a.length;o++){if(void 0===a[o])continue;const e=vector.add(a[o].location,vector.up),i=vector.subtract(e,s);if(void 0!==t.dimension.getBlockFromRay(s,vector.normalize(i),{maxDistance:vector.magnitude(i),includeLiquidBlocks:!0,includePassableBlocks:!1}))continue;let u=-1*(180*Math.atan2(i.x,i.z)/Math.PI);const d=void 0===r?t.getRotation().y:r,m=n/2;u=normalizeAngle(u);const f=normalizeAngle(d);(c?!isAngleInRange(u,f,m):isAngleInRange(u,f,m))&&l.push(a[o])}return l}export function union(t,o,e,n,i){let c=[];if(o.length>0)for(let r=0;r<o.length;r++){let l=t;t.families=[o[r]],e.getEntities(l).forEach((t=>{if(void 0===n||t.id!==n)if(i){const o=mc.world.getEntity(n).getHeadLocation(),i=t.location,r=vector.subtract(o,i),l=Math.round(vector.distance(o,i));void 0===e.getBlockFromRay(o,r,{maxDistance:l,includePassableBlocks:!1,includeLiquidBlocks:!1})&&c.push(t)}else c.push(t)}))}else{let o=t;e.getEntities(o).forEach((t=>{if(void 0===n||t.id!==n)if(i){const o=mc.world.getEntity(n).getHeadLocation(),i=t.location,r=vector.subtract(o,i),l=Math.round(vector.distance(o,i));void 0===e.getBlockFromRay(o,r,{maxDistance:l,includePassableBlocks:!1,includeLiquidBlocks:!1})&&c.push(t)}else c.push(t)}))}return c}export async function GetBlockInRadius(t,o,e,n){const i=t.x+e,c=t.y+e,r=t.z+e,l={x:t.x-e,y:t.y-e,z:t.z-e};let a=[];for(let s=l.y;s<=c;s++)for(let c=l.x;c<=i;c++)for(let i=l.z;i<=r;i++){const r={x:c,y:s,z:i};if(vector.distance(r,t)<=e){let t;try{t=n.getBlock(r)}catch(t){}void 0!==t&&t.typeId===o&&a.push(t.location)}}return a}export function getVolumeFromLocalCoords(t,o,e){try{if(!t||void 0===t)return new mc.BlockVolume({x:0,y:0,z:0},{x:0,y:0,z:0});const i={north:{x:0,z:-1,y:0},south:{x:0,z:1,y:0},east:{x:1,z:0,y:0},west:{x:-1,z:0,y:0},up:{x:0,z:0,y:1},down:{x:0,z:0,y:-1}},c={0:"down",1:"up",2:"north",3:"south",4:"west",5:"east"}[t.permutation.getState("facing_direction")],r=i[c];function n(o){return o&&void 0!==o?"up"===c||"down"===c?{x:t?.location.x+o.x,y:t?.location.y+o.y*r.y,z:t?.location.z+o.z}:{x:t?.location.x+(o.x*r.x-o.z*r.z),y:t?.location.y+o.y,z:t?.location.z+(o.x*r.z+o.z*r.x)}:t?.location}const l=n(o),a=n(e);return new mc.BlockVolume(l,a)}catch{return new mc.BlockVolume({x:0,y:0,z:0},{x:0,y:0,z:0})}}function normalizeAngle(t){return(t+180)%360-180}function angularDifference(t,o){let e=normalizeAngle(t-o);return Math.abs(e)}function isAngleInRange(t,o,e){return angularDifference(t,o)<=e/2}export function getRandomQuantity(t,o){return Math.floor(Math.random()*(o-t+1))+t}export function getAllDimensions(t){const o=[];for(const e of[mc.world.getDimension("overworld"),mc.world.getDimension("nether"),mc.world.getDimension("the_end")])o.push(...e.getEntities(t));return o}export function verifySize(t,o,e,n,i){const c=getVolumeFromLocalCoords(t,e,n);for(const t of o){const o=i.getBlocks(c,{includeTypes:[t]});for(const t of o.getBlockLocationIterator()){i.getBlock(t);return!0}}return!1}