use std::f64::consts::PI;
use azalea_client::interact::SwingArmEvent;
use azalea_client::mining::Mining;
use azalea_client::TickBroadcast;
use azalea_core::position::{BlockPos, Vec3};
use azalea_core::tick::GameTick;
use azalea_entity::{
clamp_look_direction, metadata::Player, EyeHeight, Jumping, LocalEntity, LookDirection,
Position,
};
use azalea_physics::PhysicsSet;
use bevy_app::Update;
use bevy_ecs::prelude::Event;
use bevy_ecs::schedule::IntoSystemConfigs;
use futures_lite::Future;
use tracing::trace;
use crate::accept_resource_packs::AcceptResourcePacksPlugin;
use crate::app::{App, Plugin, PluginGroup, PluginGroupBuilder};
use crate::auto_respawn::AutoRespawnPlugin;
use crate::container::ContainerPlugin;
use crate::ecs::{
component::Component,
entity::Entity,
event::EventReader,
query::{With, Without},
system::{Commands, Query},
};
use crate::pathfinder::PathfinderPlugin;
#[derive(Clone, Default)]
pub struct BotPlugin;
impl Plugin for BotPlugin {
fn build(&self, app: &mut App) {
app.add_event::<LookAtEvent>()
.add_event::<JumpEvent>()
.add_systems(
Update,
(
insert_bot,
look_at_listener.before(clamp_look_direction),
jump_listener,
),
)
.add_systems(GameTick, stop_jumping.after(PhysicsSet));
}
}
#[derive(Default, Component)]
pub struct Bot {
jumping_once: bool,
}
#[allow(clippy::type_complexity)]
fn insert_bot(
mut commands: Commands,
mut query: Query<Entity, (Without<Bot>, With<LocalEntity>, With<Player>)>,
) {
for entity in &mut query {
commands.entity(entity).insert(Bot::default());
}
}
fn stop_jumping(mut query: Query<(&mut Jumping, &mut Bot)>) {
for (mut jumping, mut bot) in &mut query {
if bot.jumping_once && **jumping {
bot.jumping_once = false;
**jumping = false;
}
}
}
pub trait BotClientExt {
fn jump(&mut self);
fn look_at(&mut self, pos: Vec3);
fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()>;
fn mine(&mut self, position: BlockPos) -> impl Future<Output = ()> + Send;
}
impl BotClientExt for azalea_client::Client {
fn jump(&mut self) {
let mut ecs = self.ecs.lock();
ecs.send_event(JumpEvent {
entity: self.entity,
});
}
fn look_at(&mut self, position: Vec3) {
let mut ecs = self.ecs.lock();
ecs.send_event(LookAtEvent {
entity: self.entity,
position,
});
}
fn get_tick_broadcaster(&self) -> tokio::sync::broadcast::Receiver<()> {
let ecs = self.ecs.lock();
let tick_broadcast = ecs.resource::<TickBroadcast>();
tick_broadcast.subscribe()
}
async fn mine(&mut self, position: BlockPos) {
self.start_mining(position);
self.ecs.lock().send_event(SwingArmEvent {
entity: self.entity,
});
let mut receiver = self.get_tick_broadcaster();
while receiver.recv().await.is_ok() {
let ecs = self.ecs.lock();
if ecs.get::<Mining>(self.entity).is_none() {
break;
}
}
}
}
#[derive(Event)]
pub struct JumpEvent {
pub entity: Entity,
}
pub fn jump_listener(
mut query: Query<(&mut Jumping, &mut Bot)>,
mut events: EventReader<JumpEvent>,
) {
for event in events.read() {
if let Ok((mut jumping, mut bot)) = query.get_mut(event.entity) {
**jumping = true;
bot.jumping_once = true;
}
}
}
#[derive(Event)]
pub struct LookAtEvent {
pub entity: Entity,
pub position: Vec3,
}
fn look_at_listener(
mut events: EventReader<LookAtEvent>,
mut query: Query<(&Position, &EyeHeight, &mut LookDirection)>,
) {
for event in events.read() {
if let Ok((position, eye_height, mut look_direction)) = query.get_mut(event.entity) {
let new_look_direction =
direction_looking_at(&position.up(eye_height.into()), &event.position);
trace!(
"look at {:?} (currently at {:?})",
event.position,
**position
);
*look_direction = new_look_direction;
}
}
}
pub fn direction_looking_at(current: &Vec3, target: &Vec3) -> LookDirection {
let delta = target - current;
let y_rot = (PI - f64::atan2(-delta.x, -delta.z)) * (180.0 / PI);
let ground_distance = f64::sqrt(delta.x * delta.x + delta.z * delta.z);
let x_rot = f64::atan2(delta.y, ground_distance) * -(180.0 / PI);
let y_rot = y_rot.rem_euclid(360.0);
let x_rot = x_rot.clamp(-90.0, 90.0) % 360.0;
LookDirection {
x_rot: x_rot as f32,
y_rot: y_rot as f32,
}
}
pub struct DefaultBotPlugins;
impl PluginGroup for DefaultBotPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(BotPlugin)
.add(PathfinderPlugin)
.add(ContainerPlugin)
.add(AutoRespawnPlugin)
.add(AcceptResourcePacksPlugin)
}
}