aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot/killaura.rs
blob: 093495eaca617c131ab45bb49d2a840c884d40c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use azalea::{
    ecs::prelude::*,
    entity::{Dead, LocalEntity, Position, metadata::AbstractMonster},
    prelude::*,
    world::InstanceName,
};

use crate::State;

pub fn tick(bot: Client, state: State) -> anyhow::Result<()> {
    if !state.killaura {
        return Ok(());
    }
    if bot.has_attack_cooldown() {
        return Ok(());
    }
    let mut nearest_entity = None;
    let mut nearest_distance = f64::INFINITY;
    let bot_position = bot.eye_position();
    let bot_instance_name = bot.component::<InstanceName>();
    {
        let mut ecs = bot.ecs.lock();
        let mut query = ecs
            .query_filtered::<(Entity, &Position, &InstanceName), (
                With<AbstractMonster>,
                Without<LocalEntity>,
                Without<Dead>,
            )>();
        for (entity_id, position, instance_name) in query.iter(&ecs) {
            if instance_name != &bot_instance_name {
                continue;
            }

            let distance = bot_position.distance_to(**position);
            if distance < 4. && distance < nearest_distance {
                nearest_entity = Some(entity_id);
                nearest_distance = distance;
            }
        }
    }
    if let Some(nearest_entity) = nearest_entity {
        println!("attacking {nearest_entity:?}");
        println!("distance {nearest_distance:?}");
        bot.attack(nearest_entity);
    }

    Ok(())
}