1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

start fixing stuff

This commit is contained in:
Ubuntu 2023-01-05 20:08:04 +00:00
parent 64fea0f660
commit cf7b2423d0
4 changed files with 647 additions and 629 deletions

View file

@ -1,7 +1,7 @@
pub use crate::chat::ChatPacket; pub use crate::chat::ChatPacket;
use crate::{ use crate::{
local_player::{send_tick_event, update_in_loaded_chunk, LocalPlayer}, local_player::{send_tick_event, update_in_loaded_chunk, LocalPlayer},
movement::send_position, movement::{local_player_ai_step, send_position},
packet_handling, packet_handling,
plugins::PluginStates, plugins::PluginStates,
Account, PlayerInfo, Account, PlayerInfo,
@ -200,7 +200,7 @@ impl Client {
// An event that causes the schedule to run. This is only used internally. // An event that causes the schedule to run. This is only used internally.
let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel(); let (run_schedule_sender, run_schedule_receiver) = mpsc::unbounded_channel();
let ecs_lock = start_ecs(run_schedule_receiver).await; let ecs_lock = start_ecs(run_schedule_receiver, run_schedule_sender.clone()).await;
let mut ecs = ecs_lock.lock(); let mut ecs = ecs_lock.lock();
ecs.init_resource::<EntityInfos>(); ecs.init_resource::<EntityInfos>();
@ -227,7 +227,7 @@ impl Client {
// start receiving packets // start receiving packets
let packet_receiver = packet_handling::PacketReceiver { let packet_receiver = packet_handling::PacketReceiver {
packets: Arc::new(Mutex::new(Vec::new())), packets: Arc::new(Mutex::new(Vec::new())),
run_schedule_sender, run_schedule_sender: run_schedule_sender.clone(),
}; };
let read_packets_task = tokio::spawn(packet_receiver.clone().read_task(read_conn)); let read_packets_task = tokio::spawn(packet_receiver.clone().read_task(read_conn));
@ -471,6 +471,7 @@ impl Client {
#[doc(hidden)] #[doc(hidden)]
pub async fn start_ecs( pub async fn start_ecs(
run_schedule_receiver: mpsc::UnboundedReceiver<()>, run_schedule_receiver: mpsc::UnboundedReceiver<()>,
run_schedule_sender: mpsc::UnboundedSender<()>,
) -> Arc<Mutex<bevy_ecs::world::World>> { ) -> Arc<Mutex<bevy_ecs::world::World>> {
// if you get an error right here that means you're doing something with locks // if you get an error right here that means you're doing something with locks
// wrong read the error to see where the issue is // wrong read the error to see where the issue is
@ -484,8 +485,7 @@ pub async fn start_ecs(
SystemSet::new() SystemSet::new()
.with_system(send_position) .with_system(send_position)
.with_system(update_in_loaded_chunk) .with_system(update_in_loaded_chunk)
.with_system(send_position) .with_system(local_player_ai_step)
.with_system(LocalPlayer::ai_step)
.with_system(send_tick_event), .with_system(send_tick_event),
) )
.add_system(packet_handling::handle_packets.label("handle_packets")) .add_system(packet_handling::handle_packets.label("handle_packets"))
@ -496,15 +496,12 @@ pub async fn start_ecs(
// app // app
let ecs = Arc::new(Mutex::new(app.world)); let ecs = Arc::new(Mutex::new(app.world));
let mut game_tick_interval = time::interval(time::Duration::from_millis(50));
// TODO: Minecraft bursts up to 10 ticks and then skips, we should too
game_tick_interval.set_missed_tick_behavior(time::MissedTickBehavior::Burst);
tokio::spawn(run_schedule_loop( tokio::spawn(run_schedule_loop(
ecs.clone(), ecs.clone(),
app.schedule, app.schedule,
run_schedule_receiver, run_schedule_receiver,
)); ));
tokio::spawn(tick_run_schedule_loop(run_schedule_sender));
ecs ecs
} }
@ -520,3 +517,19 @@ async fn run_schedule_loop(
schedule.run(&mut ecs.lock()); schedule.run(&mut ecs.lock());
} }
} }
/// Send an event to run the schedule every 50 milliseconds. It will stop when
/// the receiver is dropped.
pub async fn tick_run_schedule_loop(run_schedule_sender: mpsc::UnboundedSender<()>) {
let mut game_tick_interval = time::interval(time::Duration::from_millis(50));
// TODO: Minecraft bursts up to 10 ticks and then skips, we should too
game_tick_interval.set_missed_tick_behavior(time::MissedTickBehavior::Burst);
loop {
game_tick_interval.tick().await;
if run_schedule_sender.send(()).is_err() {
// the sender is closed so end the task
return;
}
}
}

View file

@ -95,7 +95,9 @@ impl LocalPlayer {
/// Spawn a task to write a packet directly to the server. /// Spawn a task to write a packet directly to the server.
pub fn write_packet(&mut self, packet: ServerboundGamePacket) { pub fn write_packet(&mut self, packet: ServerboundGamePacket) {
self.packet_writer.send(packet); self.packet_writer
.send(packet)
.expect("write_packet shouldn't be able to be called if the connection is closed");
} }
} }

View file

@ -192,71 +192,6 @@ impl LocalPlayer {
} }
} }
/// Makes the bot do one physics tick. Note that this is already handled
/// automatically by the client.
pub fn ai_step(
mut query: Query<
(
Entity,
&mut LocalPlayer,
&mut entity::Physics,
&mut entity::Position,
&mut entity::metadata::Sprinting,
&mut entity::Attributes,
),
&LocalPlayerInLoadedChunk,
>,
) {
for (
ecs_entity_id,
mut local_player,
mut physics,
mut position,
mut sprinting,
mut attributes,
) in query.iter_mut()
{
let physics_state = &mut local_player.physics_state;
Self::tick_controls(None, physics_state);
// server ai step
physics.xxa = physics_state.left_impulse;
physics.zza = physics_state.forward_impulse;
// TODO: food data and abilities
// let has_enough_food_to_sprint = self.food_data().food_level ||
// self.abilities().may_fly;
let has_enough_food_to_sprint = true;
// TODO: double tapping w to sprint i think
let trying_to_sprint = physics_state.trying_to_sprint;
if !**sprinting
&& (
// !self.is_in_water()
// || self.is_underwater() &&
Self::has_enough_impulse_to_start_sprinting(physics_state)
&& has_enough_food_to_sprint
// && !self.using_item()
// && !self.has_effect(MobEffects.BLINDNESS)
&& trying_to_sprint
)
{
Self::set_sprinting(true, &mut sprinting, &mut attributes);
}
azalea_physics::ai_step(
&local_player.world.read(),
&mut physics,
&mut position,
&sprinting,
&attributes,
)
}
}
/// Update the impulse from self.move_direction. The multipler is used for /// Update the impulse from self.move_direction. The multipler is used for
/// sneaking. /// sneaking.
pub(crate) fn tick_controls(multiplier: Option<f32>, physics_state: &mut PhysicsState) { pub(crate) fn tick_controls(multiplier: Option<f32>, physics_state: &mut PhysicsState) {
@ -371,6 +306,71 @@ impl LocalPlayer {
} }
} }
/// Makes the bot do one physics tick. Note that this is already handled
/// automatically by the client.
pub fn local_player_ai_step(
mut query: Query<
(
Entity,
&mut LocalPlayer,
&mut entity::Physics,
&mut entity::Position,
&mut entity::metadata::Sprinting,
&mut entity::Attributes,
),
&LocalPlayerInLoadedChunk,
>,
) {
for (
ecs_entity_id,
mut local_player,
mut physics,
mut position,
mut sprinting,
mut attributes,
) in query.iter_mut()
{
let physics_state = &mut local_player.physics_state;
LocalPlayer::tick_controls(None, physics_state);
// server ai step
physics.xxa = physics_state.left_impulse;
physics.zza = physics_state.forward_impulse;
// TODO: food data and abilities
// let has_enough_food_to_sprint = self.food_data().food_level ||
// self.abilities().may_fly;
let has_enough_food_to_sprint = true;
// TODO: double tapping w to sprint i think
let trying_to_sprint = physics_state.trying_to_sprint;
if !**sprinting
&& (
// !self.is_in_water()
// || self.is_underwater() &&
LocalPlayer::has_enough_impulse_to_start_sprinting(physics_state)
&& has_enough_food_to_sprint
// && !self.using_item()
// && !self.has_effect(MobEffects.BLINDNESS)
&& trying_to_sprint
)
{
LocalPlayer::set_sprinting(true, &mut sprinting, &mut attributes);
}
azalea_physics::ai_step(
&local_player.world.read(),
&mut physics,
&mut position,
&sprinting,
&attributes,
)
}
}
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
pub enum WalkDirection { pub enum WalkDirection {
#[default] #[default]

File diff suppressed because it is too large Load diff