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

mining descending and autotool

This commit is contained in:
mat 2023-12-10 17:32:51 -06:00
parent 18b7cf226f
commit 967da5aeba
4 changed files with 88 additions and 12 deletions

View file

@ -12,6 +12,7 @@ use azalea_inventory::{
use azalea_protocol::packets::game::{
serverbound_container_click_packet::ServerboundContainerClickPacket,
serverbound_container_close_packet::ServerboundContainerClosePacket,
serverbound_set_carried_item_packet::ServerboundSetCarriedItemPacket,
};
use azalea_registry::MenuKind;
use bevy_app::{App, Plugin, Update};
@ -40,9 +41,11 @@ impl Plugin for InventoryPlugin {
.add_event::<CloseContainerEvent>()
.add_event::<ContainerClickEvent>()
.add_event::<SetContainerContentEvent>()
.add_event::<SetSelectedHotbarSlotEvent>()
.add_systems(
Update,
(
handle_set_selected_hotbar_slot_event,
handle_menu_opened_event,
handle_set_container_content_event,
handle_container_click_event,
@ -734,3 +737,33 @@ fn handle_set_container_content_event(
}
}
}
#[derive(Event)]
pub struct SetSelectedHotbarSlotEvent {
pub entity: Entity,
/// The hotbar slot to select. This should be in the range 0..=8.
pub slot: u8,
}
fn handle_set_selected_hotbar_slot_event(
mut events: EventReader<SetSelectedHotbarSlotEvent>,
mut send_packet_events: EventWriter<SendPacketEvent>,
mut query: Query<&mut InventoryComponent>,
) {
for event in events.read() {
let mut inventory = query.get_mut(event.entity).unwrap();
// if the slot is already selected, don't send a packet
if inventory.selected_hotbar_slot == event.slot {
continue;
}
inventory.selected_hotbar_slot = event.slot;
send_packet_events.send(SendPacketEvent {
entity: event.entity,
packet: ServerboundSetCarriedItemPacket {
slot: event.slot as u16,
}
.get(),
});
}
}

View file

@ -24,7 +24,7 @@ use crate::ecs::{
use crate::pathfinder::moves::PathfinderCtx;
use crate::pathfinder::world::CachedWorld;
use azalea_client::chat::SendChatEvent;
use azalea_client::inventory::{InventoryComponent, InventorySet};
use azalea_client::inventory::{InventoryComponent, InventorySet, SetSelectedHotbarSlotEvent};
use azalea_client::mining::{Mining, StartMiningBlockEvent};
use azalea_client::movement::MoveEventsSet;
use azalea_client::{InstanceHolder, StartSprintEvent, StartWalkEvent};
@ -623,14 +623,18 @@ fn tick_execute_path(
&Physics,
Option<&Mining>,
&InstanceHolder,
&InventoryComponent,
)>,
mut look_at_events: EventWriter<LookAtEvent>,
mut sprint_events: EventWriter<StartSprintEvent>,
mut walk_events: EventWriter<StartWalkEvent>,
mut jump_events: EventWriter<JumpEvent>,
mut start_mining_events: EventWriter<StartMiningBlockEvent>,
mut set_selected_hotbar_slot_events: EventWriter<SetSelectedHotbarSlotEvent>,
) {
for (entity, executing_path, position, physics, mining, instance_holder) in &mut query {
for (entity, executing_path, position, physics, mining, instance_holder, inventory_component) in
&mut query
{
if let Some(movement) = executing_path.path.front() {
let ctx = ExecuteCtx {
entity,
@ -640,12 +644,14 @@ fn tick_execute_path(
physics,
is_currently_mining: mining.is_some(),
instance: instance_holder.instance.clone(),
menu: inventory_component.inventory_menu.clone(),
look_at_events: &mut look_at_events,
sprint_events: &mut sprint_events,
walk_events: &mut walk_events,
jump_events: &mut jump_events,
start_mining_events: &mut start_mining_events,
set_selected_hotbar_slot_events: &mut set_selected_hotbar_slot_events,
};
trace!("executing move");
(movement.data.execute)(ctx);

View file

@ -61,14 +61,22 @@ fn ascend_move(ctx: &mut PathfinderCtx, pos: BlockPos) {
for dir in CardinalDirection::iter() {
let offset = BlockPos::new(dir.x(), 1, dir.z());
if !ctx.world.is_block_passable(pos.up(2)) {
let break_cost_1 = ctx
.world
.cost_for_breaking_block(pos.up(2), ctx.mining_cache);
if break_cost_1 == f32::MAX {
continue;
}
if !ctx.world.is_standable(pos + offset) {
let break_cost_2 = ctx.world.cost_for_standing(pos + offset, ctx.mining_cache);
if break_cost_2 == f32::MAX {
continue;
}
let cost = SPRINT_ONE_BLOCK_COST + JUMP_PENALTY + *JUMP_ONE_BLOCK_COST;
let cost = SPRINT_ONE_BLOCK_COST
+ JUMP_PENALTY
+ *JUMP_ONE_BLOCK_COST
+ break_cost_1
+ break_cost_2;
ctx.edges.push(Edge {
movement: astar::Movement {
@ -91,6 +99,16 @@ fn execute_ascend_move(mut ctx: ExecuteCtx) {
..
} = ctx;
if ctx.mine(start.up(2)) {
return;
}
if ctx.mine(target) {
return;
}
if ctx.mine(target.up(1)) {
return;
}
let target_center = target.center();
ctx.look_at(target_center);

View file

@ -3,7 +3,7 @@ pub mod parkour;
use std::{fmt::Debug, sync::Arc};
use crate::{JumpEvent, LookAtEvent};
use crate::{auto_tool::best_tool_in_hotbar_for_block, JumpEvent, LookAtEvent};
use super::{
astar,
@ -11,9 +11,11 @@ use super::{
world::{is_block_state_passable, CachedWorld},
};
use azalea_client::{
mining::StartMiningBlockEvent, SprintDirection, StartSprintEvent, StartWalkEvent, WalkDirection,
inventory::SetSelectedHotbarSlotEvent, mining::StartMiningBlockEvent, SprintDirection,
StartSprintEvent, StartWalkEvent, WalkDirection,
};
use azalea_core::position::{BlockPos, Vec3};
use azalea_inventory::Menu;
use azalea_world::Instance;
use bevy_ecs::{entity::Entity, event::EventWriter};
use parking_lot::RwLock;
@ -43,7 +45,7 @@ impl Debug for MoveData {
}
}
pub struct ExecuteCtx<'w1, 'w2, 'w3, 'w4, 'w5, 'a> {
pub struct ExecuteCtx<'w1, 'w2, 'w3, 'w4, 'w5, 'w6, 'a> {
pub entity: Entity,
/// The node that we're trying to reach.
pub target: BlockPos,
@ -53,15 +55,17 @@ pub struct ExecuteCtx<'w1, 'w2, 'w3, 'w4, 'w5, 'a> {
pub physics: &'a azalea_entity::Physics,
pub is_currently_mining: bool,
pub instance: Arc<RwLock<Instance>>,
pub menu: Menu,
pub look_at_events: &'a mut EventWriter<'w1, LookAtEvent>,
pub sprint_events: &'a mut EventWriter<'w2, StartSprintEvent>,
pub walk_events: &'a mut EventWriter<'w3, StartWalkEvent>,
pub jump_events: &'a mut EventWriter<'w4, JumpEvent>,
pub start_mining_events: &'a mut EventWriter<'w5, StartMiningBlockEvent>,
pub set_selected_hotbar_slot_events: &'a mut EventWriter<'w6, SetSelectedHotbarSlotEvent>,
}
impl ExecuteCtx<'_, '_, '_, '_, '_, '_> {
impl ExecuteCtx<'_, '_, '_, '_, '_, '_, '_> {
pub fn look_at(&mut self, position: Vec3) {
self.look_at_events.send(LookAtEvent {
entity: self.entity,
@ -74,6 +78,13 @@ impl ExecuteCtx<'_, '_, '_, '_, '_, '_> {
});
}
pub fn look_at_exact(&mut self, position: Vec3) {
self.look_at_events.send(LookAtEvent {
entity: self.entity,
position,
});
}
pub fn sprint(&mut self, direction: SprintDirection) {
self.sprint_events.send(StartSprintEvent {
entity: self.entity,
@ -107,10 +118,18 @@ impl ExecuteCtx<'_, '_, '_, '_, '_, '_> {
return false;
}
if self.is_currently_mining {
return true;
}
let best_tool_result = best_tool_in_hotbar_for_block(block_state, &self.menu);
self.set_selected_hotbar_slot_events
.send(SetSelectedHotbarSlotEvent {
entity: self.entity,
slot: best_tool_result.index as u8,
});
self.is_currently_mining = true;
self.walk(WalkDirection::None);
self.look_at_exact(block.center());
self.start_mining_events.send(StartMiningBlockEvent {
entity: self.entity,
position: block,