1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 23:44:38 +00:00

start converting some functions in az-client into systems

committing because i'm about to try something that might go horribly wrong
This commit is contained in:
mat 2022-12-26 20:50:37 -06:00
commit 12269a9dc0
4 changed files with 110 additions and 110 deletions

View file

@ -36,6 +36,10 @@ use azalea_world::{
},
PartialWorld, WeakWorld, WeakWorldContainer,
};
use bevy_ecs::{
schedule::{Schedule, StageLabel, SystemStage},
system::{Query, SystemParam, SystemState},
};
use log::{debug, error, info, trace, warn};
use parking_lot::{Mutex, RwLock};
use std::{
@ -613,12 +617,12 @@ impl Client {
debug!("Got player position packet {:?}", p);
let (new_pos, y_rot, x_rot) = {
let player_entity_id = *client.entity_id.read();
let mut world = client.world();
let player_entity_id = *client.entity();
let world = client.world();
// let mut player_entity = world.entity_mut(player_entity_id).unwrap();
let mut entities = world.entities.write();
let (mut physics, mut position) =
entities.query_entity_mut::<(&mut entity::Physics, &mut entity::Position)>(
let (mut physics, position) = entities
.query_entity_mut::<(&mut entity::Physics, &mut entity::Position)>(
player_entity_id,
);
@ -975,7 +979,7 @@ impl Client {
ClientboundGamePacket::PlayerCombatEnter(_) => {}
ClientboundGamePacket::PlayerCombatKill(p) => {
debug!("Got player kill packet {:?}", p);
if *client.entity_id.read() == EntityId(p.player_id) {
if client.entity() == EntityId(p.player_id) {
// we can't define a variable here with client.dead.lock()
// because of https://github.com/rust-lang/rust/issues/57478
if !*client.dead.lock() {
@ -1029,40 +1033,40 @@ impl Client {
// TODO: Minecraft bursts up to 10 ticks and then skips, we should too
game_tick_interval.set_missed_tick_behavior(time::MissedTickBehavior::Burst);
loop {
if !client.in_loaded_chunk() {
continue;
}
game_tick_interval.tick().await;
Self::game_tick(&mut client, &tx).await;
tx.send(Event::Tick)
.await
.expect("Sending tick event should never fail");
// schedule.run_once(&mut client.world().entities.read().ecs);
let world = client.world();
let mut ecs = &mut world.entities.write().ecs;
client.send_position(
SystemState::<
Query<(
&entity::Position,
&mut entity::Physics,
&entity::metadata::Sprinting,
)>,
>::new(&mut ecs)
.get_mut(&mut ecs),
);
}
}
/// Runs every 50 milliseconds.
async fn game_tick(client: &mut Client, tx: &Sender<Event>) {
// return if there's no chunk at the player's position
/// Whether our player is in a loaded chunk
fn in_loaded_chunk(&self) -> bool {
let world = self.world();
let entities = world.entities.read();
{
let world_lock = client.world();
let player_entity_id = *client.entity_id.read();
let player_entity = world_lock.entity(player_entity_id);
let Some(player_entity) = player_entity else {
return;
};
let player_chunk_pos: ChunkPos = player_entity.pos().into();
if world_lock.get_chunk(&player_chunk_pos).is_none() {
return;
}
}
let player_entity_id = self.entity();
let position = entities.query_entity::<&entity::Position>(player_entity_id);
tx.send(Event::Tick)
.await
.expect("Sending tick event should never fail");
// TODO: if we're a passenger, send the required packets
if let Err(e) = client.send_position().await {
warn!("Error sending position: {:?}", e);
}
client.ai_step();
// TODO: minecraft does ambient sounds here
let player_chunk_pos = ChunkPos::from(position);
world.get_chunk(&player_chunk_pos).is_some()
}
/// Get a reference to our (potentially shared) world.
@ -1075,29 +1079,17 @@ impl Client {
self.world.read().shared.clone()
}
// /// Returns the entity associated to the player.
// pub fn entity(&self) -> Entity<Arc<WeakWorld>> {
// let entity_id = *self.entity_id.read();
pub fn entity(&self) -> EntityId {
*self.entity_id.read()
}
// let world = self.world();
// let entity_data = world
// .entity_storage
// .read()
// .get_by_id(entity_id)
// .expect("Player entity should be in the given world");
// let entity_ptr = unsafe { entity_data.as_ptr() };
// Entity::new(world, entity_id, entity_ptr)
// }
// pub fn query_entity<'w, Q: bevy_ecs::query::WorldQuery>(
// &'w self,
// ) -> bevy_ecs::query::ROQueryItem<'w, Q> {
// let e = parking_lot::RwLockReadGuard::map(
// self.world().entity_storage.read(),
// |entity_storage| entity_storage.query_entity::<'w,
// Q>(*self.entity_id.read()), );
// e
// }
pub fn query<'w, 's, Param: SystemParam>(
&'w self,
) -> <Param::Fetch as bevy_ecs::system::SystemParamFetch<'w, 's>>::Item {
let world = self.world();
let mut ecs = &mut world.entities.write().ecs;
SystemState::<Param>::new(ecs).get_mut(ecs)
}
/// Returns whether we have a received the login packet yet.
pub fn logged_in(&self) -> bool {
@ -1143,12 +1135,6 @@ impl Client {
Ok(())
}
/// Get your player entity's metadata. You can use this to get your health,
/// xp score, and other useful information.
pub fn metadata(&self) -> metadata::Player {
self.entity().metadata.clone().into_player().unwrap()
}
}
impl<T> From<std::sync::PoisonError<T>> for HandleError {

View file

@ -1,8 +1,8 @@
use std::backtrace::Backtrace;
use crate::Client;
use crate::{Client, PhysicsState};
use azalea_core::Vec3;
use azalea_physics::collision::MoverType;
use azalea_physics::collision::{move_colliding, MoverType};
use azalea_protocol::packets::game::serverbound_player_command_packet::ServerboundPlayerCommandPacket;
use azalea_protocol::packets::game::{
serverbound_move_player_pos_packet::ServerboundMovePlayerPosPacket,
@ -11,6 +11,7 @@ use azalea_protocol::packets::game::{
serverbound_move_player_status_only_packet::ServerboundMovePlayerStatusOnlyPacket,
};
use azalea_world::{entity, MoveEntityError};
use bevy_ecs::system::Query;
use thiserror::Error;
#[derive(Error, Debug)]
@ -33,15 +34,26 @@ impl From<MoveEntityError> for MovePlayerError {
impl Client {
/// This gets called automatically every tick.
pub(crate) async fn send_position(&mut self) -> Result<(), MovePlayerError> {
self.send_sprinting_if_needed().await?;
pub(crate) fn send_position(
&mut self,
mut query: Query<(
&entity::Position,
&mut entity::Physics,
&entity::metadata::Sprinting,
)>,
) -> Result<(), MovePlayerError> {
let (player_pos, mut physics, sprinting) = query
.get_mut((*self.entity_id.read()).into())
.expect("Player should always be in world");
let mut physics_state = self.physics_state.lock();
self.send_sprinting_if_needed(sprinting, &mut physics_state)?;
let packet = {
// TODO: the camera being able to be controlled by other entities isn't
// implemented yet if !self.is_controlled_camera() { return };
let mut physics_state = self.physics_state.lock();
// i don't like this
let entity_storage_lock = self.world().entities.clone();
let mut entity_storage = entity_storage_lock.write();
@ -128,32 +140,36 @@ impl Client {
};
if let Some(packet) = packet {
self.write_packet(packet).await?;
tokio::spawn(self.write_packet(packet));
}
Ok(())
}
async fn send_sprinting_if_needed(&mut self) -> Result<(), MovePlayerError> {
let is_sprinting = self.entity().metadata.sprinting;
let was_sprinting = self.physics_state.lock().was_sprinting;
if is_sprinting != was_sprinting {
let sprinting_action = if is_sprinting {
fn send_sprinting_if_needed(
&mut self,
sprinting: &entity::metadata::Sprinting,
physics_state: &mut PhysicsState,
) -> Result<(), MovePlayerError> {
let was_sprinting = physics_state.was_sprinting;
if **sprinting != was_sprinting {
let sprinting_action = if **sprinting {
azalea_protocol::packets::game::serverbound_player_command_packet::Action::StartSprinting
} else {
azalea_protocol::packets::game::serverbound_player_command_packet::Action::StopSprinting
};
let player_entity_id = *self.entity_id.read();
self.write_packet(
ServerboundPlayerCommandPacket {
id: *player_entity_id,
action: sprinting_action,
data: 0,
}
.get(),
)
.await?;
self.physics_state.lock().was_sprinting = is_sprinting;
tokio::spawn(
self.write_packet(
ServerboundPlayerCommandPacket {
id: *player_entity_id,
action: sprinting_action,
data: 0,
}
.get(),
),
);
physics_state.was_sprinting = **sprinting;
}
Ok(())
@ -170,36 +186,27 @@ impl Client {
Ok(())
}
pub async fn move_entity(&mut self, movement: &Vec3) -> Result<(), MovePlayerError> {
let mut world_lock = self.world.write();
let player_entity_id = *self.entity_id.read();
let mut entity = world_lock
.entity_mut(player_entity_id)
.ok_or(MovePlayerError::PlayerNotInWorld(Backtrace::capture()))?;
log::trace!(
"move entity bounding box: {} {:?}",
entity.id,
entity.bounding_box
);
entity.move_colliding(&MoverType::Own, movement)?;
Ok(())
}
/// Makes the bot do one physics tick. Note that this is already handled
/// automatically by the client.
pub fn ai_step(&mut self) {
pub fn ai_step(
&mut self,
query: Query<(
&mut entity::Physics,
&mut entity::Position,
&mut entity::metadata::Sprinting,
&entity::Attributes,
)>,
) {
self.tick_controls(None);
let (mut physics, mut position, mut sprinting, attributes) =
query.get_mut((*self.entity_id.read()).into()).unwrap();
// server ai step
{
let mut player_entity = self.entity();
let physics_state = self.physics_state.lock();
player_entity.xxa = physics_state.left_impulse;
player_entity.zza = physics_state.forward_impulse;
physics.xxa = physics_state.left_impulse;
physics.zza = physics_state.forward_impulse;
}
// TODO: food data and abilities
@ -225,8 +232,14 @@ impl Client {
self.set_sprinting(true);
}
let mut player_entity = self.entity();
player_entity.ai_step();
azalea_physics::ai_step(
*self.entity_id.read(),
&self.world(),
&mut physics,
&mut position,
&sprinting,
attributes,
);
}
/// Update the impulse from self.move_direction. The multipler is used for
@ -345,6 +358,7 @@ impl Client {
/// recommended.
pub fn set_jumping(&mut self, jumping: bool) {
let mut player_entity = self.entity();
let physics = self.query::<Query<&entity::Physics>>();
player_entity.jumping = jumping;
}

View file

@ -82,7 +82,7 @@ fn travel(
/// applies air resistance, calls self.travel(), and some other random
/// stuff.
fn ai_step(
pub fn ai_step(
entity_id: EntityId,
world: &WeakWorld,
physics: &mut entity::Physics,

View file

@ -63,7 +63,7 @@ pub struct PartialEntityStorage {
#[derive(Default)]
pub struct WeakEntityStorage {
/// The ECS world that actually contains the entities.
pub(crate) ecs: bevy_ecs::world::World,
pub ecs: bevy_ecs::world::World,
/// The number of `PartialWorld`s that have this entity loaded.
/// (this is reference counting)