use ghast_client::entity::{EntityClass, EntityType};
use ghast_client::{BlockPos, GameVersion, TickContext, Vec3};
use crate::{Bot, Error};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Block {
version: GameVersion,
raw: u32,
}
impl Block {
#[must_use]
pub const fn new(version: GameVersion, raw: u32) -> Self {
Block { version, raw }
}
#[must_use]
pub const fn raw(self) -> u32 {
self.raw
}
#[must_use]
pub fn name(self) -> &'static str {
match self.version {
GameVersion::V1_8_9 => u8::try_from(self.raw >> 4)
.ok()
.and_then(ghast_data::v1_8::Block::from_id)
.map_or("", ghast_data::v1_8::Block::name),
_ => u16::try_from(self.raw)
.ok()
.and_then(ghast_data::v1_16_5::BlockState::from_id)
.map_or("", |s| s.block().name()),
}
}
#[must_use]
pub fn meta(self) -> u8 {
match self.version {
GameVersion::V1_8_9 => (self.raw & 15) as u8,
_ => 0,
}
}
#[must_use]
pub fn is_air(self) -> bool {
matches!(self.name(), "air" | "cave_air" | "void_air")
}
#[must_use]
pub fn is_ore(self) -> bool {
let n = self.name();
n.ends_with("_ore") || n == "ancient_debris"
}
#[must_use]
pub fn is_log(self) -> bool {
let n = self.name();
n == "log"
|| n == "log2"
|| n.ends_with("_log")
|| n.ends_with("_stem")
|| n.ends_with("_hyphae")
|| n.ends_with("_wood")
}
#[must_use]
pub fn is_liquid(self) -> bool {
matches!(
self.name(),
"water" | "flowing_water" | "lava" | "flowing_lava"
)
}
}
pub enum BlockMatch {
Name(String),
Names(Vec<String>),
Pred(Box<dyn Fn(Block) -> bool + Send + Sync>),
}
impl std::fmt::Debug for BlockMatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BlockMatch::Name(n) => f.debug_tuple("Name").field(n).finish(),
BlockMatch::Names(n) => f.debug_tuple("Names").field(n).finish(),
BlockMatch::Pred(_) => f.write_str("Pred(..)"),
}
}
}
impl BlockMatch {
fn matches(&self, b: Block) -> bool {
match self {
BlockMatch::Name(n) => strip(n) == b.name(),
BlockMatch::Names(ns) => ns.iter().any(|n| strip(n) == b.name()),
BlockMatch::Pred(p) => p(b),
}
}
}
fn strip(n: &str) -> &str {
n.strip_prefix("minecraft:").unwrap_or(n)
}
impl From<&str> for BlockMatch {
fn from(n: &str) -> Self {
BlockMatch::Name(n.to_owned())
}
}
impl From<String> for BlockMatch {
fn from(n: String) -> Self {
BlockMatch::Name(n)
}
}
impl From<&[&str]> for BlockMatch {
fn from(ns: &[&str]) -> Self {
BlockMatch::Names(ns.iter().map(|s| (*s).to_owned()).collect())
}
}
impl<const N: usize> From<[&str; N]> for BlockMatch {
fn from(ns: [&str; N]) -> Self {
BlockMatch::Names(ns.iter().map(|s| (*s).to_owned()).collect())
}
}
impl<F: Fn(Block) -> bool + Send + Sync + 'static> From<F> for BlockMatch {
fn from(f: F) -> Self {
BlockMatch::Pred(Box::new(f))
}
}
pub(crate) fn find_blocks(
ctx: &TickContext<'_>,
m: &BlockMatch,
center: Vec3,
max_distance: f64,
count: usize,
) -> Vec<BlockPos> {
let v = ctx.version();
let w = ctx.world();
let r = max_distance.max(0.0);
let r2 = r * r;
let mut memo = vec![0u8; 1 << 16];
let mut test = |raw: u32| -> bool {
let i = (raw as usize) & 0xffff;
if memo[i] == 0 {
memo[i] = if m.matches(Block::new(v, raw)) { 2 } else { 1 };
}
memo[i] == 2
};
let air_matches = test(0);
let (min_y, max_y) = (
((center.y - r).floor() as i32).max(0),
((center.y + r).floor() as i32).min(255),
);
let (x0, x1) = ((center.x - r).floor() as i32, (center.x + r).floor() as i32);
let (z0, z1) = ((center.z - r).floor() as i32, (center.z + r).floor() as i32);
let mut found: Vec<(f64, BlockPos)> = Vec::new();
for cx in (x0 >> 4)..=(x1 >> 4) {
for cz in (z0 >> 4)..=(z1 >> 4) {
if !w.is_column_loaded(cx, cz) {
continue;
}
let col = w.column(cx, cz);
for sy in (min_y >> 4)..=(max_y >> 4) {
let section = col.map(|c| c.section(sy as usize));
if matches!(section, Some(None)) && !air_matches {
continue; }
for y in (sy << 4).max(min_y)..=((sy << 4) + 15).min(max_y) {
let dy = f64::from(y) + 0.5 - center.y;
for z in ((cz << 4).max(z0))..=(((cz << 4) + 15).min(z1)) {
let dz = f64::from(z) + 0.5 - center.z;
for x in ((cx << 4).max(x0))..=(((cx << 4) + 15).min(x1)) {
let dx = f64::from(x) + 0.5 - center.x;
let d2 = dx * dx + dy * dy + dz * dz;
if d2 > r2 {
continue;
}
let raw = match col {
Some(c) => u32::from(c.raw(x, y, z)),
None => match w.raw_block(x, y, z) {
Some(r) => r,
None => continue,
},
};
if test(raw) {
found.push((d2, BlockPos::new(x, y, z)));
}
}
}
}
}
}
}
found.sort_by(|a, b| a.0.total_cmp(&b.0));
found.truncate(count);
found.into_iter().map(|(_, p)| p).collect()
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct EntityInfo {
pub id: i32,
pub name: &'static str,
pub class: EntityClass,
pub pos: Vec3,
pub player_name: Option<String>,
pub item: Option<(i32, i32)>,
}
fn entity_name(ty: EntityType) -> &'static str {
match ty {
EntityType::Player => "player",
EntityType::Registry(_) => ghast_client::entity::v1_16_5::info(ty).map_or("", |i| i.name),
_ => ghast_client::entity::v1_8::info(ty).map_or("", |i| i.name),
}
}
pub(crate) fn entity_info(ctx: &TickContext<'_>, id: i32) -> Option<EntityInfo> {
let es = ctx.entities()?;
let e = es.get(id)?;
let [x, y, z] = e.position();
Some(EntityInfo {
id,
name: entity_name(e.entity_type()),
class: e.class(),
pos: Vec3::new(x, y, z),
player_name: es.player_name(id).map(str::to_owned),
item: e.item().map(|i| (i.id, i32::from(i.count))),
})
}
impl Bot {
pub async fn block_at(&self, pos: BlockPos) -> Result<Option<Block>, Error> {
self.with(move |ctx| {
let v = ctx.version();
ctx.world()
.raw_block(pos.x, pos.y, pos.z)
.map(|raw| Block::new(v, raw))
})
.await
}
pub async fn find_blocks(
&self,
what: impl Into<BlockMatch>,
max_distance: f64,
count: usize,
) -> Result<Vec<BlockPos>, Error> {
let m = what.into();
self.with(move |ctx| {
let eyes = ctx.eyes(ctx.pose());
find_blocks(ctx, &m, eyes, max_distance, count)
})
.await
}
pub async fn find_block(
&self,
what: impl Into<BlockMatch>,
max_distance: f64,
) -> Result<Option<BlockPos>, Error> {
Ok(self
.find_blocks(what, max_distance, 1)
.await?
.into_iter()
.next())
}
pub async fn find_entities<F>(&self, pred: F, radius: f64) -> Result<Vec<EntityInfo>, Error>
where
F: Fn(&EntityInfo) -> bool + Send + 'static,
{
self.with(move |ctx| {
let me = ctx.pose().pos;
let Some(es) = ctx.entities() else {
return Vec::new();
};
let self_id = es.self_id();
let ids: Vec<i32> = es
.within([me.x, me.y, me.z], radius)
.map(|e| e.id())
.filter(|id| Some(*id) != self_id)
.collect();
let mut v: Vec<(f64, EntityInfo)> = ids
.into_iter()
.filter_map(|id| entity_info(ctx, id))
.filter(|e| pred(e))
.map(|e| {
let d = (e.pos.x - me.x).powi(2)
+ (e.pos.y - me.y).powi(2)
+ (e.pos.z - me.z).powi(2);
(d, e)
})
.collect();
v.sort_by(|a, b| a.0.total_cmp(&b.0));
v.into_iter().map(|(_, e)| e).collect()
})
.await
}
pub async fn nearest_entity<F>(&self, pred: F, radius: f64) -> Result<Option<EntityInfo>, Error>
where
F: Fn(&EntityInfo) -> bool + Send + 'static,
{
Ok(self.find_entities(pred, radius).await?.into_iter().next())
}
pub async fn entity(&self, id: i32) -> Result<Option<EntityInfo>, Error> {
self.with(move |ctx| entity_info(ctx, id)).await
}
pub async fn player(&self, name: &str) -> Result<Option<EntityInfo>, Error> {
let name = name.to_owned();
self.with(move |ctx| {
let es = ctx.entities()?;
let id = es
.iter()
.filter(|e| e.is_player())
.find(|e| {
es.player_name(e.id())
.is_some_and(|n| n.eq_ignore_ascii_case(&name))
})?
.id();
entity_info(ctx, id)
})
.await
}
}