aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot/main.rs
blob: 7778463c98b3aaaa24f0ad7add7b2e5f6a662450 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! A relatively simple bot for demonstrating some of Azalea's capabilities.
//!
//! ## Usage
//!
//! - Modify the consts below if necessary.
//! - Run `cargo r --example testbot -- [arguments]`. (see below)
//! - Commands are prefixed with `!` in chat. You can send them either in public
//!   chat or as a /msg.
//! - Some commands to try are `!goto`, `!killaura true`, `!down`. Check the
//!   `commands` directory to see all of them.
//!
//! ### Arguments
//!
//! - `--owner` or `-O`: The username of the player who owns the bot. The bot
//!   will ignore commands from other players.
//! - `--account` or `-A`: The username or email of the bot.
//! - `--server` or `-S`: The address of the server to join.
//! - `--pathfinder-debug-particles` or `-P`: Whether the bot should run
//!   /particle a ton of times to show where it's pathfinding to. You should
//!   only have this on if the bot has operator permissions, otherwise it'll
//!   just spam the server console unnecessarily.
//! - `--simulation-pathfinder`: Use the alternative simulation-based execution
//!   engine for the pathfinder.

mod commands;
pub mod killaura;
pub mod mspt;

use std::{env, process, sync::Arc, thread, time::Duration};

use azalea::{
    ClientInformation, EntityRef,
    brigadier::command_dispatcher::CommandDispatcher,
    ecs::prelude::*,
    pathfinder::{
        PathfinderOpts,
        debug::PathfinderDebugParticles,
        execute::simulation::SimulationPathfinderExecutionPlugin,
        goals::{Goal, RadiusGoal},
    },
    prelude::*,
    swarm::prelude::*,
};
use commands::{CommandSource, register_commands};
use parking_lot::Mutex;

#[tokio::main]
async fn main() -> AppExit {
    let args = parse_args();

    thread::spawn(deadlock_detection_thread);

    let join_address = args.server.clone();

    let mut builder = SwarmBuilder::new()
        .set_handler(handle)
        .set_swarm_handler(swarm_handle);

    if args.simulation_pathfinder {
        builder = builder.add_plugins(SimulationPathfinderExecutionPlugin);
    }

    for username_or_email in &args.accounts {
        let account = if username_or_email.contains('@') {
            Account::microsoft(username_or_email).await.unwrap()
        } else {
            Account::offline(username_or_email)
        };

        builder = builder.add_account_with_state(account, State::new());
    }

    let mut commands = CommandDispatcher::new();
    register_commands(&mut commands);

    builder
        .join_delay(Duration::from_millis(100))
        .set_swarm_state(SwarmState {
            args: args.into(),
            commands: commands.into(),
        })
        // .add_plugins(mspt::MsptPlugin)
        .start(join_address)
        .await
}

/// Runs a loop that checks for deadlocks every 10 seconds.
///
/// Note that this requires the `deadlock_detection` parking_lot feature to be
/// enabled, which is only enabled in azalea by default when running in debug
/// mode.
fn deadlock_detection_thread() {
    loop {
        thread::sleep(Duration::from_secs(10));
        let deadlocks = parking_lot::deadlock::check_deadlock();
        if deadlocks.is_empty() {
            continue;
        }

        println!("{} deadlocks detected", deadlocks.len());
        for (i, threads) in deadlocks.iter().enumerate() {
            println!("Deadlock #{i}");
            for t in threads {
                println!("Thread Id {:#?}", t.thread_id());
                println!("{:#?}", t.backtrace());
            }
        }
    }
}

#[derive(Clone, Component, Default)]
pub struct State {
    pub killaura: bool,
    pub following_entity: Arc<Mutex<Option<EntityRef>>>,
}

impl State {
    fn new() -> Self {
        Self {
            killaura: false,
            following_entity: Default::default(),
        }
    }
}

#[derive(Clone, Default, Resource)]
struct SwarmState {
    pub args: Arc<Args>,
    pub commands: Arc<commands::Dispatcher>,
}

async fn handle(bot: Client, event: azalea::Event, state: State) -> eyre::Result<()> {
    let swarm = bot.resource::<SwarmState>();

    match event {
        azalea::Event::Init => {
            bot.set_client_information(ClientInformation {
                view_distance: 32,
                ..Default::default()
            })?;
            if swarm.args.pathfinder_debug_particles {
                bot.ecs
                    .write()
                    .entity_mut(bot.entity)
                    .insert(PathfinderDebugParticles);
            }
        }
        azalea::Event::Chat(chat) => {
            let (Some(username), content) = chat.split_sender_and_content() else {
                return Ok(());
            };
            if username != swarm.args.owner_username {
                return Ok(());
            }

            println!("{:?}", chat.message());

            let command = if chat.is_whisper() {
                Some(content)
            } else {
                content.strip_prefix('!').map(|s| s.to_owned())
            };
            if let Some(command) = command {
                match swarm.commands.execute(
                    command,
                    Mutex::new(CommandSource {
                        bot: bot.clone(),
                        chat: chat.clone(),
                        state: state.clone(),
                    }),
                ) {
                    Ok(Ok(_)) => {}
                    Ok(Err(err)) => {
                        eprintln!("azalea error: {err:?}");
                        let command_source = CommandSource {
                            bot,
                            chat: chat.clone(),
                            state: state.clone(),
                        };
                        command_source.reply(format!("azalea error: {err:?}"));
                    }
                    Err(err) => {
                        eprintln!("{err:?}");
                        let command_source = CommandSource {
                            bot,
                            chat: chat.clone(),
                            state: state.clone(),
                        };
                        command_source.reply(format!("{err:?}"));
                    }
                }
            }
        }
        azalea::Event::Tick => {
            killaura::tick(bot.clone(), state.clone())?;

            if bot.ticks_connected().is_multiple_of(5) {
                if let Some(following) = &*state.following_entity.lock()
                    && following.is_alive()
                {
                    let goal = RadiusGoal::new(following.position()?, 3.);
                    if bot.is_calculating_path() {
                        // keep waiting
                    } else if !goal.success(bot.position()?.into()) || bot.is_executing_path() {
                        bot.start_goto_with_opts(
                            goal,
                            PathfinderOpts::new()
                                .retry_on_no_path(false)
                                .max_timeout(Duration::from_secs(1)),
                        );
                    } else {
                        following.look_at()?;
                    }
                }
            }
        }
        azalea::Event::Login => {
            println!("Got login event")
        }
        _ => {}
    }

    Ok(())
}
async fn swarm_handle(_swarm: Swarm, event: SwarmEvent, _state: SwarmState) -> eyre::Result<()> {
    match &event {
        SwarmEvent::Disconnect(account, _join_opts) => {
            println!("bot got kicked! {}", account.username());
        }
        SwarmEvent::Chat(chat) => {
            if chat.message().to_string() == "The particle was not visible for anybody" {
                return Ok(());
            }
            println!("{}", chat.message().to_ansi());
        }
        _ => {}
    }

    Ok(())
}

#[derive(Clone, Debug, Default)]
pub struct Args {
    pub owner_username: String,
    pub accounts: Vec<String>,
    pub server: String,
    pub pathfinder_debug_particles: bool,
    pub simulation_pathfinder: bool,
}

fn parse_args() -> Args {
    let mut owner_username = "admin".to_owned();
    let mut accounts = Vec::new();
    let mut server = "localhost".to_owned();
    let mut pathfinder_debug_particles = false;
    let mut simulation_pathfinder = false;

    let mut args = env::args().skip(1);
    while let Some(arg) = args.next() {
        match arg.as_str() {
            "--owner" | "-O" => {
                owner_username = args.next().expect("Missing owner username");
            }
            "--account" | "-A" => {
                for account in args.next().expect("Missing account").split(',') {
                    accounts.push(account.to_string());
                }
            }
            "--server" | "-S" => {
                server = args.next().expect("Missing server address");
            }
            "--pathfinder-debug-particles" | "-P" => {
                pathfinder_debug_particles = true;
            }
            "--simulation-pathfinder" => {
                simulation_pathfinder = true;
            }
            _ => {
                eprintln!("Unknown argument: {arg}");
                process::exit(1);
            }
        }
    }

    if accounts.is_empty() {
        accounts.push("azalea".to_owned());
    }

    Args {
        owner_username,
        accounts,
        server,
        pathfinder_debug_particles,
        simulation_pathfinder,
    }
}