2022-07-15 16:18:20 +10:00
|
|
|
//! Dynamic actors in a world.
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
pub mod state;
|
2022-07-11 22:08:02 +10:00
|
|
|
pub mod types;
|
2022-04-29 17:48:41 +10:00
|
|
|
|
2022-04-15 07:55:45 +10:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::iter::FusedIterator;
|
2022-06-19 17:25:25 +10:00
|
|
|
use std::num::NonZeroU32;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
use bitfield_struct::bitfield;
|
2022-04-29 17:48:41 +10:00
|
|
|
use rayon::iter::ParallelIterator;
|
2022-07-16 13:40:39 +10:00
|
|
|
pub use types::{EntityKind, EntityState};
|
2022-04-15 07:55:45 +10:00
|
|
|
use uuid::Uuid;
|
2022-05-16 21:09:51 +10:00
|
|
|
use vek::{Aabb, Vec3};
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
use crate::config::Config;
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::protocol_inner::packets::play::s2c::{
|
2022-07-01 07:18:29 +10:00
|
|
|
AddEntity, AddExperienceOrb, AddPlayer, S2cPlayPacket, SetEntityMetadata,
|
2022-05-16 19:36:14 +10:00
|
|
|
};
|
2022-07-11 22:08:02 +10:00
|
|
|
use crate::protocol_inner::{ByteAngle, RawBytes, VarInt};
|
2022-04-29 17:48:41 +10:00
|
|
|
use crate::slotmap::{Key, SlotMap};
|
2022-05-16 21:09:51 +10:00
|
|
|
use crate::util::aabb_from_bottom_and_size;
|
2022-07-07 11:27:59 +10:00
|
|
|
use crate::world::WorldId;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// A container for all [`Entity`]s on a [`Server`](crate::server::Server).
|
|
|
|
///
|
|
|
|
/// # Spawning Player Entities
|
|
|
|
///
|
|
|
|
/// [`Player`] entities are treated specially by the client. For the player
|
|
|
|
/// entity to be visible to clients, the player's UUID must be added to the
|
|
|
|
/// [`PlayerList`] _before_ being loaded by the client.
|
|
|
|
///
|
|
|
|
/// [`Player`]: crate::entity::types::Player
|
|
|
|
/// [`PlayerList`]: crate::player_list::PlayerList
|
2022-07-16 13:40:39 +10:00
|
|
|
pub struct Entities<C: Config> {
|
|
|
|
sm: SlotMap<Entity<C>>,
|
2022-04-15 07:55:45 +10:00
|
|
|
uuid_to_entity: HashMap<Uuid, EntityId>,
|
2022-06-19 17:25:25 +10:00
|
|
|
network_id_to_entity: HashMap<NonZeroU32, u32>,
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
impl<C: Config> Entities<C> {
|
2022-05-16 19:36:14 +10:00
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
sm: SlotMap::new(),
|
|
|
|
uuid_to_entity: HashMap::new(),
|
2022-06-19 17:25:25 +10:00
|
|
|
network_id_to_entity: HashMap::new(),
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Spawns a new entity with a random UUID. A reference to the entity along
|
|
|
|
/// with its ID is returned.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn create(&mut self, kind: EntityKind, data: C::EntityData) -> (EntityId, &mut Entity<C>) {
|
|
|
|
self.create_with_uuid(kind, Uuid::from_bytes(rand::random()), data)
|
2022-05-18 20:05:10 +10:00
|
|
|
.expect("UUID collision")
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Like [`Self::create`], but requires specifying the new
|
2022-06-19 17:25:25 +10:00
|
|
|
/// entity's UUID.
|
2022-04-15 07:55:45 +10:00
|
|
|
///
|
2022-07-11 22:08:02 +10:00
|
|
|
/// The provided UUID must not conflict with an existing entity UUID. If it
|
|
|
|
/// does, `None` is returned and the entity is not spawned.
|
2022-07-04 13:31:20 +10:00
|
|
|
pub fn create_with_uuid(
|
|
|
|
&mut self,
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: EntityKind,
|
2022-07-05 08:51:28 +10:00
|
|
|
uuid: Uuid,
|
2022-07-16 13:40:39 +10:00
|
|
|
data: C::EntityData,
|
|
|
|
) -> Option<(EntityId, &mut Entity<C>)> {
|
2022-06-30 04:09:00 +10:00
|
|
|
match self.uuid_to_entity.entry(uuid) {
|
2022-04-15 07:55:45 +10:00
|
|
|
Entry::Occupied(_) => None,
|
|
|
|
Entry::Vacant(ve) => {
|
2022-06-30 04:09:00 +10:00
|
|
|
let (k, e) = self.sm.insert(Entity {
|
2022-07-16 13:40:39 +10:00
|
|
|
data,
|
|
|
|
state: EntityState::new(kind),
|
2022-05-16 19:36:14 +10:00
|
|
|
flags: EntityFlags(0),
|
2022-07-11 22:08:02 +10:00
|
|
|
world: WorldId::NULL,
|
2022-05-16 21:09:51 +10:00
|
|
|
new_position: Vec3::default(),
|
|
|
|
old_position: Vec3::default(),
|
2022-05-16 19:36:14 +10:00
|
|
|
yaw: 0.0,
|
|
|
|
pitch: 0.0,
|
|
|
|
head_yaw: 0.0,
|
|
|
|
velocity: Vec3::default(),
|
2022-04-29 17:48:41 +10:00
|
|
|
uuid,
|
2022-05-18 20:05:10 +10:00
|
|
|
});
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-05 09:30:37 +10:00
|
|
|
// TODO check for overflowing version?
|
|
|
|
self.network_id_to_entity.insert(k.version(), k.index());
|
|
|
|
|
2022-06-19 17:25:25 +10:00
|
|
|
ve.insert(EntityId(k));
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
Some((EntityId(k), e))
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Removes an entity from the server.
|
|
|
|
///
|
|
|
|
/// If the given entity ID is valid, `true` is returned and the entity is
|
|
|
|
/// deleted. Otherwise, `false` is returned and the function has no effect.
|
2022-04-29 17:48:41 +10:00
|
|
|
pub fn delete(&mut self, entity: EntityId) -> bool {
|
2022-06-30 04:09:00 +10:00
|
|
|
if let Some(e) = self.sm.remove(entity.0) {
|
|
|
|
self.uuid_to_entity
|
2022-04-29 17:48:41 +10:00
|
|
|
.remove(&e.uuid)
|
2022-04-15 07:55:45 +10:00
|
|
|
.expect("UUID should have been in UUID map");
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
self.network_id_to_entity
|
2022-06-19 17:25:25 +10:00
|
|
|
.remove(&entity.0.version())
|
|
|
|
.expect("network ID should have been in the network ID map");
|
|
|
|
|
2022-04-15 07:55:45 +10:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Removes all entities from the server for which `f` returns `true`.
|
|
|
|
///
|
|
|
|
/// All entities are visited in an unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn retain(&mut self, mut f: impl FnMut(EntityId, &mut Entity<C>) -> bool) {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.retain(|k, v| {
|
|
|
|
if f(EntityId(k), v) {
|
2022-06-19 17:25:25 +10:00
|
|
|
true
|
|
|
|
} else {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.uuid_to_entity
|
2022-06-19 17:25:25 +10:00
|
|
|
.remove(&v.uuid)
|
|
|
|
.expect("UUID should have been in UUID map");
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
self.network_id_to_entity
|
2022-06-19 17:25:25 +10:00
|
|
|
.remove(&k.version())
|
|
|
|
.expect("network ID should have been in the network ID map");
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns the number of entities in this container.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
self.sm.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the [`EntityId`] of the entity with the given UUID in an efficient
|
|
|
|
/// manner.
|
|
|
|
///
|
2022-07-11 22:08:02 +10:00
|
|
|
/// If there is no entity with the UUID, `None` is returned.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn get_with_uuid(&self, uuid: Uuid) -> Option<EntityId> {
|
|
|
|
self.uuid_to_entity.get(&uuid).cloned()
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets a shared reference to the entity with the given [`EntityId`].
|
|
|
|
///
|
|
|
|
/// If the ID is invalid, `None` is returned.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn get(&self, entity: EntityId) -> Option<&Entity<C>> {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.get(entity.0)
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets an exclusive reference to the entity with the given [`EntityId`].
|
|
|
|
///
|
|
|
|
/// If the ID is invalid, `None` is returned.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn get_mut(&mut self, entity: EntityId) -> Option<&mut Entity<C>> {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.get_mut(entity.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_with_network_id(&self, network_id: i32) -> Option<EntityId> {
|
|
|
|
let version = NonZeroU32::new(network_id as u32)?;
|
|
|
|
let index = *self.network_id_to_entity.get(&version)?;
|
|
|
|
Some(EntityId(Key::new(index, version)))
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns an immutable iterator over all entities on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn iter(&self) -> impl FusedIterator<Item = (EntityId, &Entity<C>)> + Clone + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.iter().map(|(k, v)| (EntityId(k), v))
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a mutable iterator over all entities on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn iter_mut(&mut self) -> impl FusedIterator<Item = (EntityId, &mut Entity<C>)> + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.iter_mut().map(|(k, v)| (EntityId(k), v))
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a parallel immutable iterator over all entities on the server in
|
|
|
|
/// an unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn par_iter(&self) -> impl ParallelIterator<Item = (EntityId, &Entity<C>)> + Clone + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.par_iter().map(|(k, v)| (EntityId(k), v))
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns a parallel mutable iterator over all clients on the server in an
|
|
|
|
/// unspecified order.
|
2022-07-16 13:40:39 +10:00
|
|
|
pub fn par_iter_mut(
|
|
|
|
&mut self,
|
|
|
|
) -> impl ParallelIterator<Item = (EntityId, &mut Entity<C>)> + '_ {
|
2022-06-30 04:09:00 +10:00
|
|
|
self.sm.par_iter_mut().map(|(k, v)| (EntityId(k), v))
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
pub(crate) fn update(&mut self) {
|
|
|
|
for (_, e) in self.iter_mut() {
|
2022-06-30 04:09:00 +10:00
|
|
|
e.old_position = e.new_position;
|
2022-07-16 13:40:39 +10:00
|
|
|
e.state.clear_modifications();
|
2022-05-17 19:58:43 +10:00
|
|
|
|
2022-07-04 17:24:55 +10:00
|
|
|
e.flags.set_yaw_or_pitch_modified(false);
|
|
|
|
e.flags.set_head_yaw_modified(false);
|
|
|
|
e.flags.set_velocity_modified(false);
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-15 16:18:20 +10:00
|
|
|
/// An identifier for an [`Entity`] on the server.
|
2022-07-11 22:08:02 +10:00
|
|
|
///
|
|
|
|
/// Entity IDs are either _valid_ or _invalid_. Valid entity IDs point to
|
|
|
|
/// entities that have not been deleted, while invalid IDs point to those that
|
|
|
|
/// have. Once an ID becomes invalid, it will never become valid again.
|
|
|
|
///
|
|
|
|
/// The [`Ord`] instance on this type is correct but otherwise unspecified. This
|
|
|
|
/// is useful for storing IDs in containers such as
|
|
|
|
/// [`BTreeMap`](std::collections::BTreeMap).
|
2022-07-04 11:02:00 +10:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
|
2022-05-16 19:36:14 +10:00
|
|
|
pub struct EntityId(Key);
|
|
|
|
|
|
|
|
impl EntityId {
|
2022-07-11 22:08:02 +10:00
|
|
|
/// The value of the default entity ID which is always invalid.
|
2022-07-04 11:02:00 +10:00
|
|
|
pub const NULL: Self = Self(Key::NULL);
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
pub(crate) fn to_network_id(self) -> i32 {
|
2022-06-19 17:25:25 +10:00
|
|
|
self.0.version().get() as i32
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Represents an entity on the server.
|
|
|
|
///
|
|
|
|
/// In essence, an entity is anything in a world that isn't a block or client.
|
|
|
|
/// Entities include paintings, falling blocks, zombies, fireballs, and more.
|
|
|
|
///
|
|
|
|
/// Every entity has common state which is accessible directly from
|
|
|
|
/// this struct. This includes position, rotation, velocity, UUID, and hitbox.
|
|
|
|
/// To access data that is not common to every kind of entity, see
|
|
|
|
/// [`Self::data`].
|
2022-07-16 13:40:39 +10:00
|
|
|
pub struct Entity<C: Config> {
|
|
|
|
/// Custom data.
|
|
|
|
pub data: C::EntityData,
|
|
|
|
/// Kind-specific state for this entity.
|
|
|
|
pub state: EntityState,
|
2022-05-16 19:36:14 +10:00
|
|
|
flags: EntityFlags,
|
2022-07-11 22:08:02 +10:00
|
|
|
world: WorldId,
|
2022-05-16 21:09:51 +10:00
|
|
|
new_position: Vec3<f64>,
|
|
|
|
old_position: Vec3<f64>,
|
2022-05-16 19:36:14 +10:00
|
|
|
yaw: f32,
|
|
|
|
pitch: f32,
|
|
|
|
head_yaw: f32,
|
2022-05-16 21:09:51 +10:00
|
|
|
velocity: Vec3<f32>,
|
2022-05-16 19:36:14 +10:00
|
|
|
uuid: Uuid,
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
#[bitfield(u8)]
|
|
|
|
pub(crate) struct EntityFlags {
|
2022-05-17 19:58:43 +10:00
|
|
|
pub yaw_or_pitch_modified: bool,
|
|
|
|
pub head_yaw_modified: bool,
|
|
|
|
pub velocity_modified: bool,
|
|
|
|
pub on_ground: bool,
|
2022-07-04 17:24:55 +10:00
|
|
|
#[bits(4)]
|
2022-05-16 19:36:14 +10:00
|
|
|
_pad: u8,
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-07-16 13:40:39 +10:00
|
|
|
impl<C: Config> Entity<C> {
|
2022-05-16 19:36:14 +10:00
|
|
|
pub(crate) fn flags(&self) -> EntityFlags {
|
|
|
|
self.flags
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the [`EntityKind`] of this entity.
|
2022-07-06 12:21:52 +10:00
|
|
|
pub fn kind(&self) -> EntityKind {
|
2022-07-16 13:40:39 +10:00
|
|
|
self.state.kind()
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the [`WorldId`](crate::world::WorldId) of the world this entity is
|
|
|
|
/// located in.
|
|
|
|
///
|
|
|
|
/// By default, entities are located in
|
|
|
|
/// [`WorldId::NULL`](crate::world::WorldId::NULL).
|
|
|
|
pub fn world(&self) -> WorldId {
|
2022-07-04 08:31:24 +10:00
|
|
|
self.world
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the world this entity is located in.
|
|
|
|
pub fn set_world(&mut self, world: WorldId) {
|
|
|
|
self.world = world;
|
2022-07-04 08:31:24 +10:00
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the position of this entity in the world it inhabits.
|
|
|
|
///
|
|
|
|
/// The position of an entity is located on the botton of its
|
|
|
|
/// hitbox and not the center.
|
2022-05-16 21:09:51 +10:00
|
|
|
pub fn position(&self) -> Vec3<f64> {
|
2022-05-16 19:36:14 +10:00
|
|
|
self.new_position
|
|
|
|
}
|
|
|
|
|
2022-06-30 04:09:00 +10:00
|
|
|
/// Sets the position of this entity in the world it inhabits.
|
2022-07-11 22:08:02 +10:00
|
|
|
///
|
|
|
|
/// The position of an entity is located on the botton of its
|
|
|
|
/// hitbox and not the center.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_position(&mut self, pos: impl Into<Vec3<f64>>) {
|
|
|
|
self.new_position = pos.into();
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
/// Returns the position of this entity as it existed at the end of the
|
|
|
|
/// previous tick.
|
2022-07-11 22:08:02 +10:00
|
|
|
pub(crate) fn old_position(&self) -> Vec3<f64> {
|
2022-05-16 19:36:14 +10:00
|
|
|
self.old_position
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the yaw of this entity in degrees.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn yaw(&self) -> f32 {
|
|
|
|
self.yaw
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the yaw of this entity in degrees.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_yaw(&mut self, yaw: f32) {
|
|
|
|
if self.yaw != yaw {
|
|
|
|
self.yaw = yaw;
|
|
|
|
self.flags.set_yaw_or_pitch_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the pitch of this entity in degrees.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn pitch(&self) -> f32 {
|
|
|
|
self.pitch
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the pitch of this entity in degrees.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_pitch(&mut self, pitch: f32) {
|
|
|
|
if self.pitch != pitch {
|
|
|
|
self.pitch = pitch;
|
|
|
|
self.flags.set_yaw_or_pitch_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the head yaw of this entity in degrees.
|
2022-05-16 19:36:14 +10:00
|
|
|
pub fn head_yaw(&self) -> f32 {
|
|
|
|
self.head_yaw
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the head yaw of this entity in degrees.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_head_yaw(&mut self, head_yaw: f32) {
|
|
|
|
if self.head_yaw != head_yaw {
|
|
|
|
self.head_yaw = head_yaw;
|
|
|
|
self.flags.set_head_yaw_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
/// Gets the velocity of this entity in meters per second.
|
2022-05-16 21:09:51 +10:00
|
|
|
pub fn velocity(&self) -> Vec3<f32> {
|
2022-05-16 19:36:14 +10:00
|
|
|
self.velocity
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the velocity of this entity in meters per second.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_velocity(&mut self, velocity: impl Into<Vec3<f32>>) {
|
|
|
|
let new_vel = velocity.into();
|
|
|
|
|
|
|
|
if self.velocity != new_vel {
|
|
|
|
self.velocity = new_vel;
|
|
|
|
self.flags.set_velocity_modified(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the value of the "on ground" flag.
|
2022-05-17 19:58:43 +10:00
|
|
|
pub fn on_ground(&self) -> bool {
|
|
|
|
self.flags.on_ground()
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Sets the value of the "on ground" flag.
|
2022-06-30 04:09:00 +10:00
|
|
|
pub fn set_on_ground(&mut self, on_ground: bool) {
|
|
|
|
self.flags.set_on_ground(on_ground);
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Gets the UUID of this entity.
|
2022-07-04 17:48:21 +10:00
|
|
|
pub fn uuid(&self) -> Uuid {
|
|
|
|
self.uuid
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:08:02 +10:00
|
|
|
/// Returns the hitbox of this entity.
|
|
|
|
///
|
|
|
|
/// The hitbox describes the space that an entity occupies. Clients interact
|
|
|
|
/// with this space to create an [interact event].
|
|
|
|
///
|
|
|
|
/// The hitbox of an entity is determined by its position, entity type, and
|
|
|
|
/// other state specific to that type.
|
|
|
|
///
|
|
|
|
/// [interact event]: crate::client::Event::InteractWithEntity
|
2022-05-16 21:09:51 +10:00
|
|
|
pub fn hitbox(&self) -> Aabb<f64> {
|
2022-07-16 13:40:39 +10:00
|
|
|
let dims = match &self.state {
|
|
|
|
EntityState::Allay(_) => [0.6, 0.35, 0.6],
|
|
|
|
EntityState::ChestBoat(_) => [1.375, 0.5625, 1.375],
|
|
|
|
EntityState::Frog(_) => [0.5, 0.5, 0.5],
|
|
|
|
EntityState::Tadpole(_) => [0.4, 0.3, 0.4],
|
|
|
|
EntityState::Warden(_) => [0.9, 2.9, 0.9],
|
|
|
|
EntityState::AreaEffectCloud(e) => [
|
2022-05-16 19:36:14 +10:00
|
|
|
e.get_radius() as f64 * 2.0,
|
|
|
|
0.5,
|
|
|
|
e.get_radius() as f64 * 2.0,
|
|
|
|
],
|
2022-07-16 13:40:39 +10:00
|
|
|
EntityState::ArmorStand(e) => {
|
2022-05-16 19:36:14 +10:00
|
|
|
if e.get_marker() {
|
|
|
|
[0.0, 0.0, 0.0]
|
|
|
|
} else if e.get_small() {
|
|
|
|
[0.5, 0.9875, 0.5]
|
|
|
|
} else {
|
|
|
|
[0.5, 1.975, 0.5]
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-07-16 13:40:39 +10:00
|
|
|
EntityState::Arrow(_) => [0.5, 0.5, 0.5],
|
|
|
|
EntityState::Axolotl(_) => [1.3, 0.6, 1.3],
|
|
|
|
EntityState::Bat(_) => [0.5, 0.9, 0.5],
|
|
|
|
EntityState::Bee(_) => [0.7, 0.6, 0.7], // TODO: baby size?
|
|
|
|
EntityState::Blaze(_) => [0.6, 1.8, 0.6],
|
|
|
|
EntityState::Boat(_) => [1.375, 0.5625, 1.375],
|
|
|
|
EntityState::Cat(_) => [0.6, 0.7, 0.6],
|
|
|
|
EntityState::CaveSpider(_) => [0.7, 0.5, 0.7],
|
|
|
|
EntityState::Chicken(_) => [0.4, 0.7, 0.4], // TODO: baby size?
|
|
|
|
EntityState::Cod(_) => [0.5, 0.3, 0.5],
|
|
|
|
EntityState::Cow(_) => [0.9, 1.4, 0.9], // TODO: baby size?
|
|
|
|
EntityState::Creeper(_) => [0.6, 1.7, 0.6],
|
|
|
|
EntityState::Dolphin(_) => [0.9, 0.6, 0.9],
|
|
|
|
EntityState::Donkey(_) => [1.5, 1.39648, 1.5], // TODO: baby size?
|
|
|
|
EntityState::DragonFireball(_) => [1.0, 1.0, 1.0],
|
|
|
|
EntityState::Drowned(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::ElderGuardian(_) => [1.9975, 1.9975, 1.9975],
|
|
|
|
EntityState::EndCrystal(_) => [2.0, 2.0, 2.0],
|
|
|
|
EntityState::EnderDragon(_) => [16.0, 8.0, 16.0],
|
|
|
|
EntityState::Enderman(_) => [0.6, 2.9, 0.6],
|
|
|
|
EntityState::Endermite(_) => [0.4, 0.3, 0.4],
|
|
|
|
EntityState::Evoker(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::EvokerFangs(_) => [0.5, 0.8, 0.5],
|
|
|
|
EntityState::ExperienceOrb(_) => [0.5, 0.5, 0.5],
|
|
|
|
EntityState::EyeOfEnder(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::FallingBlock(_) => [0.98, 0.98, 0.98],
|
|
|
|
EntityState::FireworkRocket(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::Fox(_) => [0.6, 0.7, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Ghast(_) => [4.0, 4.0, 4.0],
|
|
|
|
EntityState::Giant(_) => [3.6, 12.0, 3.6],
|
|
|
|
EntityState::GlowItemFrame(_) => todo!("account for rotation"),
|
|
|
|
EntityState::GlowSquid(_) => [0.8, 0.8, 0.8],
|
|
|
|
EntityState::Goat(_) => [1.3, 0.9, 1.3], // TODO: baby size?
|
|
|
|
EntityState::Guardian(_) => [0.85, 0.85, 0.85],
|
|
|
|
EntityState::Hoglin(_) => [1.39648, 1.4, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::Horse(_) => [1.39648, 1.6, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::Husk(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Illusioner(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::IronGolem(_) => [1.4, 2.7, 1.4],
|
|
|
|
EntityState::Item(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::ItemFrame(_) => todo!("account for rotation"),
|
|
|
|
EntityState::Fireball(_) => [1.0, 1.0, 1.0],
|
|
|
|
EntityState::LeashKnot(_) => [0.375, 0.5, 0.375],
|
|
|
|
EntityState::LightningBolt(_) => [0.0, 0.0, 0.0],
|
|
|
|
EntityState::Llama(_) => [0.9, 1.87, 0.9], // TODO: baby size?
|
|
|
|
EntityState::LlamaSpit(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::MagmaCube(e) => {
|
2022-05-16 19:36:14 +10:00
|
|
|
let s = e.get_size() as f64 * 0.51000005;
|
|
|
|
[s, s, s]
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-07-16 13:40:39 +10:00
|
|
|
EntityState::Marker(_) => [0.0, 0.0, 0.0],
|
|
|
|
EntityState::Minecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::ChestMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::CommandBlockMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::FurnaceMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::HopperMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::SpawnerMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::TntMinecart(_) => [0.98, 0.7, 0.98],
|
|
|
|
EntityState::Mule(_) => [1.39648, 1.6, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::Mooshroom(_) => [0.9, 1.4, 0.9], // TODO: baby size?
|
|
|
|
EntityState::Ocelot(_) => [0.6, 0.7, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Painting(_) => todo!("account for rotation and type"),
|
|
|
|
EntityState::Panda(_) => [0.6, 0.7, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Parrot(_) => [0.5, 0.9, 0.5],
|
|
|
|
EntityState::Phantom(_) => [0.9, 0.5, 0.9],
|
|
|
|
EntityState::Pig(_) => [0.9, 0.9, 0.9], // TODO: baby size?
|
|
|
|
EntityState::Piglin(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::PiglinBrute(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::Pillager(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::PolarBear(_) => [1.4, 1.4, 1.4], // TODO: baby size?
|
|
|
|
EntityState::Tnt(_) => [0.98, 0.98, 0.98],
|
|
|
|
EntityState::Pufferfish(_) => [0.7, 0.7, 0.7],
|
|
|
|
EntityState::Rabbit(_) => [0.4, 0.5, 0.4], // TODO: baby size?
|
|
|
|
EntityState::Ravager(_) => [1.95, 2.2, 1.95],
|
|
|
|
EntityState::Salmon(_) => [0.7, 0.4, 0.7],
|
|
|
|
EntityState::Sheep(_) => [0.9, 1.3, 0.9], // TODO: baby size?
|
|
|
|
EntityState::Shulker(_) => [1.0, 1.0, 1.0], // TODO: how is height calculated?
|
|
|
|
EntityState::ShulkerBullet(_) => [0.3125, 0.3125, 0.3125],
|
|
|
|
EntityState::Silverfish(_) => [0.4, 0.3, 0.4],
|
|
|
|
EntityState::Skeleton(_) => [0.6, 1.99, 0.6],
|
|
|
|
EntityState::SkeletonHorse(_) => [1.39648, 1.6, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::Slime(e) => {
|
2022-05-16 19:36:14 +10:00
|
|
|
let s = 0.51000005 * e.get_size() as f64;
|
|
|
|
[s, s, s]
|
|
|
|
}
|
2022-07-16 13:40:39 +10:00
|
|
|
EntityState::SmallFireball(_) => [0.3125, 0.3125, 0.3125],
|
|
|
|
EntityState::SnowGolem(_) => [0.7, 1.9, 0.7],
|
|
|
|
EntityState::Snowball(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::SpectralArrow(_) => [0.5, 0.5, 0.5],
|
|
|
|
EntityState::Spider(_) => [1.4, 0.9, 1.4],
|
|
|
|
EntityState::Squid(_) => [0.8, 0.8, 0.8],
|
|
|
|
EntityState::Stray(_) => [0.6, 1.99, 0.6],
|
|
|
|
EntityState::Strider(_) => [0.9, 1.7, 0.9], // TODO: baby size?
|
|
|
|
EntityState::Egg(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::EnderPearl(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::ExperienceBottle(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::Potion(_) => [0.25, 0.25, 0.25],
|
|
|
|
EntityState::Trident(_) => [0.5, 0.5, 0.5],
|
|
|
|
EntityState::TraderLlama(_) => [0.9, 1.87, 0.9],
|
|
|
|
EntityState::TropicalFish(_) => [0.5, 0.4, 0.5],
|
|
|
|
EntityState::Turtle(_) => [1.2, 0.4, 1.2], // TODO: baby size?
|
|
|
|
EntityState::Vex(_) => [0.4, 0.8, 0.4],
|
|
|
|
EntityState::Villager(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Vindicator(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::WanderingTrader(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::Witch(_) => [0.6, 1.95, 0.6],
|
|
|
|
EntityState::Wither(_) => [0.9, 3.5, 0.9],
|
|
|
|
EntityState::WitherSkeleton(_) => [0.7, 2.4, 0.7],
|
|
|
|
EntityState::WitherSkull(_) => [0.3125, 0.3125, 0.3125],
|
|
|
|
EntityState::Wolf(_) => [0.6, 0.85, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Zoglin(_) => [1.39648, 1.4, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::Zombie(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::ZombieHorse(_) => [1.39648, 1.6, 1.39648], // TODO: baby size?
|
|
|
|
EntityState::ZombieVillager(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::ZombifiedPiglin(_) => [0.6, 1.95, 0.6], // TODO: baby size?
|
|
|
|
EntityState::Player(_) => [0.6, 1.8, 0.6], // TODO: changes depending on the pose.
|
|
|
|
EntityState::FishingBobber(_) => [0.25, 0.25, 0.25],
|
2022-05-16 19:36:14 +10:00
|
|
|
};
|
|
|
|
|
2022-05-16 21:09:51 +10:00
|
|
|
aabb_from_bottom_and_size(self.new_position, dims.into())
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2022-07-04 17:24:55 +10:00
|
|
|
|
|
|
|
/// Gets the metadata packet to send to clients after this entity has been
|
|
|
|
/// spawned.
|
|
|
|
///
|
|
|
|
/// Is `None` if there is no initial metadata.
|
|
|
|
pub(crate) fn initial_metadata_packet(&self, this_id: EntityId) -> Option<SetEntityMetadata> {
|
2022-07-16 13:40:39 +10:00
|
|
|
self.state.initial_metadata().map(|meta| SetEntityMetadata {
|
2022-07-04 17:24:55 +10:00
|
|
|
entity_id: VarInt(this_id.to_network_id()),
|
|
|
|
metadata: RawBytes(meta),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the metadata packet to send to clients when the entity is modified.
|
|
|
|
///
|
|
|
|
/// Is `None` if this entity's metadata has not been modified.
|
|
|
|
pub(crate) fn updated_metadata_packet(&self, this_id: EntityId) -> Option<SetEntityMetadata> {
|
2022-07-16 13:40:39 +10:00
|
|
|
self.state.updated_metadata().map(|meta| SetEntityMetadata {
|
2022-07-04 17:24:55 +10:00
|
|
|
entity_id: VarInt(this_id.to_network_id()),
|
|
|
|
metadata: RawBytes(meta),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn spawn_packet(&self, this_id: EntityId) -> Option<EntitySpawnPacket> {
|
2022-07-16 13:40:39 +10:00
|
|
|
match &self.state {
|
|
|
|
EntityState::Marker(_) => None,
|
|
|
|
EntityState::ExperienceOrb(_) => {
|
2022-07-15 16:18:20 +10:00
|
|
|
Some(EntitySpawnPacket::ExperienceOrb(AddExperienceOrb {
|
2022-07-04 17:24:55 +10:00
|
|
|
entity_id: VarInt(this_id.to_network_id()),
|
|
|
|
position: self.new_position,
|
|
|
|
count: 0, // TODO
|
|
|
|
}))
|
|
|
|
}
|
2022-07-16 13:40:39 +10:00
|
|
|
EntityState::Player(_) => Some(EntitySpawnPacket::Player(AddPlayer {
|
2022-07-04 17:24:55 +10:00
|
|
|
entity_id: VarInt(this_id.to_network_id()),
|
|
|
|
player_uuid: self.uuid,
|
|
|
|
position: self.new_position,
|
|
|
|
yaw: ByteAngle::from_degrees(self.yaw),
|
|
|
|
pitch: ByteAngle::from_degrees(self.pitch),
|
|
|
|
})),
|
2022-07-15 16:18:20 +10:00
|
|
|
_ => Some(EntitySpawnPacket::Entity(AddEntity {
|
2022-07-04 17:24:55 +10:00
|
|
|
entity_id: VarInt(this_id.to_network_id()),
|
|
|
|
object_uuid: self.uuid,
|
2022-07-06 12:21:52 +10:00
|
|
|
kind: VarInt(self.kind() as i32),
|
2022-07-04 17:24:55 +10:00
|
|
|
position: self.new_position,
|
|
|
|
pitch: ByteAngle::from_degrees(self.pitch),
|
|
|
|
yaw: ByteAngle::from_degrees(self.yaw),
|
|
|
|
head_yaw: ByteAngle::from_degrees(self.head_yaw),
|
|
|
|
data: VarInt(1), // TODO
|
|
|
|
velocity: velocity_to_packet_units(self.velocity),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-05-17 19:58:43 +10:00
|
|
|
pub(crate) fn velocity_to_packet_units(vel: Vec3<f32>) -> Vec3<i16> {
|
2022-05-16 19:36:14 +10:00
|
|
|
// The saturating cast to i16 is desirable.
|
2022-05-16 21:09:51 +10:00
|
|
|
(vel * 400.0).as_()
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-05-16 19:36:14 +10:00
|
|
|
pub(crate) enum EntitySpawnPacket {
|
2022-07-15 16:18:20 +10:00
|
|
|
Entity(AddEntity),
|
|
|
|
ExperienceOrb(AddExperienceOrb),
|
|
|
|
Player(AddPlayer),
|
2022-05-16 19:36:14 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-06-10 13:26:21 +10:00
|
|
|
impl From<EntitySpawnPacket> for S2cPlayPacket {
|
2022-05-16 19:36:14 +10:00
|
|
|
fn from(pkt: EntitySpawnPacket) -> Self {
|
|
|
|
match pkt {
|
2022-07-15 16:18:20 +10:00
|
|
|
EntitySpawnPacket::Entity(pkt) => pkt.into(),
|
|
|
|
EntitySpawnPacket::ExperienceOrb(pkt) => pkt.into(),
|
|
|
|
EntitySpawnPacket::Player(pkt) => pkt.into(),
|
2022-04-29 17:48:41 +10:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|