Skip to main content

azalea/pathfinder/
simulation.rs

1//! Simulate the Minecraft world, currently only used for tests.
2
3use std::sync::Arc;
4
5use azalea_client::{
6    PhysicsState, interact::BlockStatePredictionHandler, local_player::LocalGameMode,
7    mining::MineBundle,
8};
9use azalea_core::{
10    entity_id::MinecraftEntityId, game_type::GameMode, position::Vec3, tick::GameTick,
11};
12use azalea_entity::{
13    Attributes, LookDirection, Physics, Position, dimensions::EntityDimensions,
14    inventory::Inventory,
15};
16use azalea_registry::builtin::EntityKind;
17use azalea_world::{ChunkStorage, PartialWorld, World, WorldName, Worlds};
18use bevy_app::App;
19use bevy_ecs::prelude::*;
20use parking_lot::RwLock;
21use uuid::Uuid;
22
23#[derive(Bundle, Clone)]
24pub struct SimulatedPlayerBundle {
25    pub position: Position,
26    pub physics: Physics,
27    pub physics_state: PhysicsState,
28    pub look_direction: LookDirection,
29    pub attributes: Attributes,
30    pub inventory: Inventory,
31}
32
33impl SimulatedPlayerBundle {
34    pub fn new(position: Vec3) -> Self {
35        let dimensions = EntityDimensions::from(EntityKind::Player);
36
37        SimulatedPlayerBundle {
38            position: Position::new(position),
39            physics: Physics::new(&dimensions, position),
40            physics_state: PhysicsState::default(),
41            look_direction: LookDirection::default(),
42            attributes: Attributes::new(EntityKind::Player),
43            inventory: Inventory::default(),
44        }
45    }
46}
47
48fn simulation_world_name() -> WorldName {
49    WorldName::new("azalea:simulation")
50}
51
52fn create_simulation_world(chunks: ChunkStorage) -> (App, Arc<RwLock<World>>) {
53    let world_name = simulation_world_name();
54
55    let world = Arc::new(RwLock::new(World {
56        chunks,
57        ..Default::default()
58    }));
59
60    let mut app = App::new();
61    // we don't use all the default azalea plugins because we don't need all of them
62    app.add_plugins((
63        azalea_physics::PhysicsPlugin,
64        azalea_entity::EntityPlugin,
65        azalea_client::movement::MovementPlugin,
66        super::PathfinderPlugin,
67        crate::bot::BotPlugin,
68        azalea_client::task_pool::TaskPoolPlugin::default(),
69        // for mining
70        azalea_client::inventory::InventoryPlugin,
71        azalea_client::mining::MiningPlugin,
72        azalea_client::interact::InteractPlugin,
73        azalea_client::loading::PlayerLoadedPlugin,
74    ))
75    .insert_resource(Worlds {
76        map: [(world_name.clone(), Arc::downgrade(&world.clone()))]
77            .iter()
78            .cloned()
79            .collect(),
80    });
81
82    app.edit_schedule(bevy_app::Main, |schedule| {
83        schedule.set_executor_kind(bevy_ecs::schedule::ExecutorKind::SingleThreaded);
84    });
85
86    app.finish();
87
88    (app, world)
89}
90
91fn create_simulation_player_complete_bundle(
92    world: Arc<RwLock<World>>,
93    player: &SimulatedPlayerBundle,
94) -> impl Bundle {
95    let world_name = simulation_world_name();
96
97    (
98        MinecraftEntityId(0),
99        azalea_entity::LocalEntity,
100        azalea_entity::metadata::PlayerMetadataBundle::default(),
101        azalea_entity::EntityBundle::new(
102            Uuid::nil(),
103            *player.position,
104            EntityKind::Player,
105            world_name,
106        ),
107        azalea_client::local_player::WorldHolder {
108            // the partial world is never actually used by the pathfinder, so we can leave it empty
109            partial: Arc::new(RwLock::new(PartialWorld::default())),
110            shared: world.clone(),
111        },
112        Inventory::default(),
113        LocalGameMode::from(GameMode::Survival),
114        MineBundle::default(),
115        BlockStatePredictionHandler::default(),
116        azalea_client::local_player::PermissionLevel::default(),
117        azalea_entity::PlayerAbilities::default(),
118    )
119}
120
121pub fn create_simulation_player(
122    ecs: &mut bevy_ecs::world::World,
123    world: Arc<RwLock<World>>,
124    player: SimulatedPlayerBundle,
125) -> Entity {
126    let mut entity = ecs.spawn(create_simulation_player_complete_bundle(world, &player));
127    entity.insert(player);
128    entity.id()
129}
130
131/// Simulate the Minecraft world to see if certain movements would be possible.
132pub struct Simulation {
133    pub app: App,
134    pub entity: Entity,
135    pub world: Arc<RwLock<World>>,
136}
137
138impl Simulation {
139    pub fn new(chunks: ChunkStorage, player: SimulatedPlayerBundle) -> Self {
140        let (mut app, world) = create_simulation_world(chunks);
141        let entity = create_simulation_player(app.world_mut(), world.clone(), player);
142        Self { app, entity, world }
143    }
144
145    /// Despawn the old simulated player and create a new one.
146    ///
147    /// This is cheaper than creating a new [`Simulation`] from scratch.
148    pub fn reset(&mut self, player: SimulatedPlayerBundle) {
149        self.app.world_mut().despawn(self.entity);
150        let entity = create_simulation_player(self.app.world_mut(), self.world.clone(), player);
151        self.entity = entity;
152    }
153
154    pub fn tick(&mut self) {
155        self.app.update();
156        self.app.world_mut().run_schedule(GameTick);
157    }
158    pub fn component<T: Component + Clone>(&self) -> T {
159        self.app.world().get::<T>(self.entity).unwrap().clone()
160    }
161    pub fn get_component<T: Component + Clone>(&self) -> Option<T> {
162        self.app.world().get::<T>(self.entity).cloned()
163    }
164    pub fn position(&self) -> Vec3 {
165        *self.component::<Position>()
166    }
167    pub fn physics(&self) -> Physics {
168        self.component::<Physics>().clone()
169    }
170    pub fn is_mining(&self) -> bool {
171        // return true if the component is present and Some
172        self.get_component::<azalea_client::mining::MineBlockPos>()
173            .and_then(|c| *c)
174            .is_some()
175    }
176}
177
178/// A set of simulations, useful for efficiently doing multiple simulations.
179pub struct SimulationSet {
180    pub app: App,
181    world: Arc<RwLock<World>>,
182}
183impl SimulationSet {
184    pub fn new(chunks: ChunkStorage) -> Self {
185        let (app, world) = create_simulation_world(chunks);
186        Self { app, world }
187    }
188    pub fn tick(&mut self) {
189        self.app.update();
190        self.app.world_mut().run_schedule(GameTick);
191    }
192
193    pub fn spawn(&mut self, player: SimulatedPlayerBundle) -> Entity {
194        create_simulation_player(self.app.world_mut(), self.world.clone(), player)
195    }
196    pub fn despawn(&mut self, entity: Entity) {
197        self.app.world_mut().despawn(entity);
198    }
199
200    pub fn position(&self, entity: Entity) -> Vec3 {
201        **self.app.world().get::<Position>(entity).unwrap()
202    }
203}