import*as mc from"@minecraft/server";import*as vector from"../utils/vector";import*as selectors from"../utils/selectors";import*as umvuthana from"./umvuthana";const validGround={"minecraft:grass_block":!0,"minecraft:dirt":!0,"minecraft:acacia_slab":!0};export function eventBehavior(t){if(umvuthana.eventBehavior(t),"attempt_teleport"===t.eventId)return void 0===t.entity.getDynamicProperty("hf_mzs:x_pos")?void t.entity.triggerEvent("minecraft:entity_spawned"):(t.entity.setProperty("hf_mzs:teleport",!0),void t.entity.triggerEvent("teleport"));if("teleport"!==t.eventId);else try{t.entity.setProperty("hf_mzs:teleport",!0),mc.system.runTimeout((()=>{if(!t.entity.isValid())return;let e=t.entity.dimension.getEntities({maxDistance:32,families:["hf_mzs_structure_umvuthana_village"],location:t.entity.location});if(void 0===e[0]&&(e=t.entity.dimension.getEntities({maxDistance:32,families:["hf_mzs_umvuthi"],location:t.entity.location}),void 0===e[0]))return;const o=e[0].location;for(let e=0;e<50;e++){const e=2*Math.random()*Math.PI,n={x:o.x+Math.sin(e)*(7+14*Math.random()),y:300,z:o.z+Math.cos(e)*(7+14*Math.random())},r=t.entity.dimension.getBlockFromRay(n,vector.down,{maxDistance:900,includeLiquidBlocks:!0,includePassableBlocks:!1});if(void 0===r||!validGround[r.block.typeId])continue;const a=r.block.location;let i=selectors.union({location:a,maxDistance:5},["player","hf_mzs_umvuthana_crane"],t.entity.dimension,t.entity.id);if(!i[0]&&(i=t.entity.dimension.getEntities({maxDistance:7,families:["hf_mzs_umvuthi"],location:t.entity.location}),!i[0]))return void t.entity.teleport(vector.add(a,vector.up))}}),26)}catch{}}export function tick5(t){if(t.getProperty("hf_mzs:spawn")||t.getProperty("hf_mzs:teleport"))return;const e=t.getComponent("tameable").tamedToPlayer;if(validateTarget(t,e,8))return rotateToTarget(t,e),t.runCommand(`/tp @s ~~~ facing "${e.name}"`),e.getComponent("health").currentValue<=20&&(e.getComponent("health").setCurrentValue(Math.min(e.getComponent("health").currentValue+.2,20)),19===Math.round(e.getComponent("health").currentValue)&&e.getComponent("health").setCurrentValue(20)),t.runCommand(`/effect "${e.name}" resistance 2 0 true`),e.runCommand("/particle hf_mzs:umvuthana_crane_heal1 ~~~"),void e.runCommand("/particle hf_mzs:umvuthana_crane_heal2 ~~~");t.setProperty("hf_mzs:healing",!1);const o=t.dimension.getEntities({location:t.location,maxDistance:38,families:["hf_mzs_umvuthi"]})[0];if(validateTarget(t,o,38))return rotateToTarget(t,o),t.runCommand("/tp @s ~~~ facing @e[c=1, type=hf_mzs:umvuthi]"),t.runCommand("/effect @e[c=1, type=hf_mzs:umvuthi] resistance 1 100 true"),o.setProperty("hf_mzs:sunblocked",!0),o.setDynamicProperty("hf_mzs:sunblocked_timer",3),void(o.getComponent("health").currentValue<=150&&o.getComponent("health").setCurrentValue(Math.min(o.getComponent("health").currentValue+.1,150)));t.setProperty("hf_mzs:healing",!1),t.setProperty("hf_mzs:distance",0)}export function tick1(t){if(t.getProperty("hf_mzs:tamed")||!t.getProperty("hf_mzs:healing"))return;if(t.getDynamicProperty("hf_mzs:dodge_cooldown")>0)return void t.setDynamicProperty("hf_mzs:dodge_cooldown",t.getDynamicProperty("hf_mzs:dodge_cooldown")-1);const e=t.dimension.getEntities({type:"arrow",maxDistance:18,location:t.location});for(const o of e)if(vector.magnitude(o.getVelocity())>0&&(0===t.getDynamicProperty("hf_mzs:dodge_cooldown")||!t.getDynamicProperty("hf_mzs:dodge_cooldown")))return void dodgeArrow(t,o)}function dodgeArrow(t,e){const o=vector.flatten(vector.subtract(t.location,e.location),"y");let n=vector.flatten(vector.normalize(e.getVelocity()),"y");const r=n.z*o.x-n.x*o.z,a=vector.flatten(vector.cross(vector.normalize(e.getVelocity()),vector.up),"y");t.applyImpulse(vector.multiply(a,r>=0?-1:1)),t.setDynamicProperty("hf_mzs:dodge_cooldown",15)}function validateTarget(t,e,o){if(void 0===e||!e.isValid())return!1;if("hf_mzs:umvuthi"===e.typeId&&(t.getProperty("hf_mzs:tamed")||e.getProperty("hf_mzs:death")||!(e.getComponent("health").currentValue<150||e.getProperty("hf_mzs:has_target"))))return;if("minecraft:player"===e.typeId&&e.getComponent("health").currentValue>=18.5)return;const n=vector.subtract(e.location,t.location),r=vector.magnitude(n);if(r>o)return!1;return void 0===t.dimension.getBlockFromRay(vector.add(t.location,{x:0,y:2.1,z:0}),vector.normalize(n),{maxDistance:r,includeLiquidBlocks:!0,includePassableBlocks:!1})}function rotateToTarget(t,e){const o=vector.subtract(e.location,t.location),n=Math.acos(o.y/vector.magnitude(o))/Math.PI*180-90,r=-Math.atan2(o.x,o.z)/Math.PI*180;t.setProperty("hf_mzs:x_rot",n),t.setProperty("hf_mzs:y_rot",r),t.setProperty("hf_mzs:healing",!0),"hf_mzs:umvuthi"===e.typeId?t.setProperty("hf_mzs:distance",.028*vector.magnitude(o)):t.setProperty("hf_mzs:distance",Math.max(0,.028*vector.magnitude(o)-.02)),t.runCommand("/effect @s slowness 1 100 true")}