testbot/
killaura.rs

1use azalea::{
2    ecs::prelude::*,
3    entity::{Dead, LocalEntity, Position, metadata::AbstractMonster},
4    prelude::*,
5    world::InstanceName,
6};
7
8use crate::State;
9
10pub fn tick(bot: Client, state: State) -> anyhow::Result<()> {
11    if !state.killaura {
12        return Ok(());
13    }
14    if bot.has_attack_cooldown() {
15        return Ok(());
16    }
17    let mut nearest_entity = None;
18    let mut nearest_distance = f64::INFINITY;
19    let bot_position = bot.eye_position();
20    let bot_instance_name = bot.component::<InstanceName>();
21    {
22        let mut ecs = bot.ecs.lock();
23        let mut query = ecs
24            .query_filtered::<(Entity, &Position, &InstanceName), (
25                With<AbstractMonster>,
26                Without<LocalEntity>,
27                Without<Dead>,
28            )>();
29        for (entity_id, position, instance_name) in query.iter(&ecs) {
30            if instance_name != &bot_instance_name {
31                continue;
32            }
33
34            let distance = bot_position.distance_to(**position);
35            if distance < 4. && distance < nearest_distance {
36                nearest_entity = Some(entity_id);
37                nearest_distance = distance;
38            }
39        }
40    }
41    if let Some(nearest_entity) = nearest_entity {
42        println!("attacking {nearest_entity:?}");
43        println!("distance {nearest_distance:?}");
44        bot.attack(nearest_entity);
45    }
46
47    Ok(())
48}