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

attribute stuff

todo: the default bases depend on the entity
This commit is contained in:
mat 2022-11-06 19:43:48 -06:00
parent ebc9aa7821
commit d31aeb9abc
5 changed files with 109 additions and 39 deletions

View file

@ -218,7 +218,13 @@ impl Client {
physics_state.move_direction = direction;
}
/// Toggle whether we're jumping. This acts as if you held space in
/// Start sprinting in the given direction.
pub fn sprint(&mut self, direction: SprintDirection) {
let mut physics_state = self.physics_state.lock();
physics_state.move_direction = direction;
}
/// Set whether we're jumping. This acts as if you held space in
/// vanilla. If you want to jump once, use the `jump` function.
///
/// If you're making a realistic client, calling this function every tick is
@ -258,3 +264,11 @@ pub enum WalkDirection {
BackwardRight,
BackwardLeft,
}
/// The directions that we can sprint in. It's a subset of [`WalkDirection`].
#[derive(Clone, Copy, Debug)]
pub enum SprintDirection {
Forward,
ForwardRight,
ForwardLeft,
}

View file

@ -102,6 +102,7 @@ fn tick_execute_path(bot: &mut Client, path: &mut VecDeque<Node>) {
// println!("going to {center:?} (at {pos:?})", pos = bot.entity().pos());
bot.look_at(&center);
bot.walk(WalkDirection::Forward);
bot.set_sprinting(true);
// check if we should jump
if target.pos.y > bot.entity().pos().y.floor() as i32 {
bot.jump();
@ -112,6 +113,7 @@ fn tick_execute_path(bot: &mut Client, path: &mut VecDeque<Node>) {
path.pop_front();
if path.is_empty() {
bot.walk(WalkDirection::None);
bot.set_sprinting(false);
}
// tick again, maybe we already reached the next node!
tick_execute_path(bot, path);
@ -155,7 +157,7 @@ impl Node {
);
BlockPos::from(entity.pos()) == self.pos
&& match self.vertical_vel {
VerticalVel::NoneMidair => (entity.delta.y > -0.1 && entity.delta.y < 0.1),
VerticalVel::NoneMidair => entity.delta.y > -0.1 && entity.delta.y < 0.1,
VerticalVel::None => entity.on_ground,
VerticalVel::FallingLittle => entity.delta.y < -0.1,
}

View file

@ -1,9 +1,8 @@
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufWritable};
use azalea_buf::McBuf;
use azalea_buf::McBufReadable;
use azalea_core::ResourceLocation;
use azalea_protocol_macros::ClientboundGamePacket;
use std::io::{Cursor, Write};
use uuid::Uuid;
use azalea_world::entity::attributes::AttributeModifier;
#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundUpdateAttributesPacket {
@ -16,37 +15,5 @@ pub struct ClientboundUpdateAttributesPacket {
pub struct AttributeSnapshot {
pub attribute: ResourceLocation,
pub base: f64,
pub modifiers: Vec<Modifier>,
}
#[derive(Clone, Debug, McBuf)]
pub struct Modifier {
pub uuid: Uuid,
pub amount: f64,
pub operation: u8,
}
#[derive(Clone, Debug, Copy)]
enum Operation {
Addition = 0,
MultiplyBase = 1,
MultiplyTotal = 2,
}
impl McBufReadable for Operation {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
match u8::read_from(buf)? {
0 => Ok(Operation::Addition),
1 => Ok(Operation::MultiplyBase),
2 => Ok(Operation::MultiplyTotal),
id => Err(BufReadError::UnexpectedEnumVariant { id: id.into() }),
}
}
}
impl McBufWritable for Operation {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(*self as u8).write_into(buf)?;
Ok(())
}
pub modifiers: Vec<AttributeModifier>,
}

View file

@ -0,0 +1,81 @@
//! https://minecraft.fandom.com/wiki/Attribute
use std::io::{Cursor, Write};
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
use uuid::{uuid, Uuid};
#[derive(Clone, Debug, Default)]
pub struct AttributeModifiers {
pub speed: AttributeInstance,
}
#[derive(Clone, Debug, Default)]
pub struct AttributeInstance<const BASE: f64> {
pub modifiers: Vec<AttributeModifier>,
}
impl<const BASE: f64> AttributeInstance {
pub fn calculate(&self) -> f64 {
let mut total = BASE;
for modifier in self.modifiers {
match modifier.operation {
AttributeModifierOperation::Addition => total += modifier.amount,
AttributeModifierOperation::MultiplyBase => total += BASE * modifier.amount,
_ => {}
}
match modifier.operation {
AttributeModifierOperation::MultiplyTotal => total *= 1.0 + modifier.amount,
_ => {}
}
}
total
}
}
#[derive(Clone, Debug)]
pub struct AttributeModifier {
pub uuid: Uuid,
pub name: String,
pub amount: f64,
pub operation: AttributeModifierOperation,
}
#[derive(Clone, Debug, Copy, McBuf)]
pub enum AttributeModifierOperation {
Addition,
MultiplyBase,
MultiplyTotal,
}
pub fn sprinting_modifier() -> AttributeModifier {
AttributeModifier {
uuid: uuid!("662A6B8D-DA3E-4C1C-8813-96EA6097278D"),
name: "Sprinting speed boost".to_string(),
amount: 0.30000001192092896,
operation: AttributeModifierOperation::MultiplyTotal,
}
}
impl McBufReadable for AttributeModifier {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let uuid = Uuid::read_from(buf)?;
let amount = f64::read_from(buf)?;
let operation = AttributeModifierOperation::read_from(buf)?;
Ok(Self {
uuid,
name: "Unknown synced attribute modifier".to_string(),
amount,
operation,
})
}
}
impl McBufWritable for AttributeModifier {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.uuid.write_into(buf)?;
self.amount.write_into(buf)?;
self.operation.write_into(buf)?;
Ok(())
}
}

View file

@ -1,7 +1,9 @@
pub mod attributes;
mod data;
mod dimensions;
pub mod metadata;
use self::attributes::AttributeModifiers;
pub use self::metadata::EntityMetadata;
use crate::Dimension;
use azalea_block::BlockState;
@ -212,6 +214,8 @@ pub struct EntityData {
/// Stores some extra data about the entity, including the entity type.
pub metadata: EntityMetadata,
pub attribute_modifiers: AttributeModifiers,
}
impl EntityData {
@ -247,6 +251,8 @@ impl EntityData {
jumping: false,
metadata,
attribute_modifiers: AttributeModifiers::default(),
}
}