Give piglins, brutes, hoglins and zoglins their behaviour
Piglins leave gold-wearing players alone, pick up and admire gold, barter gold ingots for a roll of the bartering table, fight with crossbows or golden swords, hunt baby hoglins, flee zombified piglins and get angry when their chests are opened or their gold mined. Brutes always attack. Hoglins headbutt their targets into the air, babies flee, and both turn into zombified piglins and zoglins after 15 seconds out of the Nether. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -183,6 +183,13 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag {
|
||||
map.entry("sound_variant".into())
|
||||
.or_insert_with(|| string("minecraft:classic"));
|
||||
}
|
||||
"minecraft:piglin" | "minecraft:piglin_brute" | "minecraft:hoglin" => {
|
||||
map.insert(
|
||||
"IsImmuneToZombification".into(),
|
||||
Tag::Byte(i8::from(m.extra.flag2)),
|
||||
);
|
||||
map.insert("TimeInOverworld".into(), Tag::Int(m.extra.counter2));
|
||||
}
|
||||
"minecraft:slime" | "minecraft:magma_cube" => {
|
||||
map.insert("Size".into(), Tag::Int(m.extra.size.max(1) - 1));
|
||||
map.insert("wasOnGround".into(), Tag::Byte(i8::from(m.extra.flag2)));
|
||||
@@ -251,6 +258,15 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap<String, Arc<MobType>>, id: u32) -
|
||||
if m.species() == crate::mobs::Species::Creeper {
|
||||
m.extra.charged = flag(tag, "powered");
|
||||
}
|
||||
if matches!(
|
||||
m.species(),
|
||||
crate::mobs::Species::Piglin
|
||||
| crate::mobs::Species::PiglinBrute
|
||||
| crate::mobs::Species::Hoglin
|
||||
) {
|
||||
m.extra.flag2 = flag(tag, "IsImmuneToZombification");
|
||||
m.extra.counter2 = number(tag.get("TimeInOverworld")).unwrap_or(0) as i32;
|
||||
}
|
||||
if m.species() == crate::mobs::Species::Cat {
|
||||
m.extra.owner = crate::save::uuid_from_tag(tag.get("Owner"));
|
||||
m.extra.tame = m.extra.owner.is_some();
|
||||
|
||||
@@ -33,6 +33,7 @@ mod options;
|
||||
mod packs;
|
||||
mod paintings;
|
||||
mod persist;
|
||||
mod piglins;
|
||||
mod postprocess;
|
||||
mod potions;
|
||||
mod remote;
|
||||
@@ -1378,6 +1379,10 @@ impl Game<'_> {
|
||||
} else if let Some(entity) = BlockEntity::for_block(&name) {
|
||||
self.world.block_entities.entry(key).or_insert(entity);
|
||||
self.container = Some(key);
|
||||
// Opening a piglin-guarded container angers the piglins who see it.
|
||||
if self.guarded_by_piglins(&name) {
|
||||
self.anger_piglins(true);
|
||||
}
|
||||
let sound = if storage::is_shulker_box(&name) {
|
||||
Some("minecraft:block.shulker_box.open")
|
||||
} else if name.contains("chest") {
|
||||
@@ -1484,6 +1489,10 @@ impl Game<'_> {
|
||||
fn break_block(&mut self, pos: [i32; 3]) -> Result<()> {
|
||||
let state = self.world.block(pos[0], pos[1], pos[2]);
|
||||
let facts = self.res.facts.get(state).clone();
|
||||
// Block.playerWillDestroy: mining their gold angers all nearby piglins.
|
||||
if self.guarded_by_piglins(&facts.name) {
|
||||
self.anger_piglins(false);
|
||||
}
|
||||
if !self.creative {
|
||||
if let Some(p) = self.player.as_mut() {
|
||||
p.health.food.exhaustion = (p.health.food.exhaustion + 0.005).min(40.0);
|
||||
|
||||
@@ -129,6 +129,7 @@ impl Game<'_> {
|
||||
head: self.armor[0].as_ref().map(|s| s.item.clone()),
|
||||
armor_cover: self.armor.iter().filter(|a| a.is_some()).count() as f32 / 4.0,
|
||||
spectator: self.spectator(),
|
||||
gold_armor: self.wearing_gold(),
|
||||
hurt_by: self.player_hurt_by(),
|
||||
last_hurt: self.combat.last_hurt_mob.map(|(id, at)| (Who::Mob(id), at)),
|
||||
})
|
||||
@@ -557,6 +558,7 @@ impl Game<'_> {
|
||||
self.handle_mob_events(events)?;
|
||||
self.tick_projectiles()?;
|
||||
self.tick_player_effects();
|
||||
self.piglin_pickups();
|
||||
Ok(())
|
||||
}
|
||||
/// LivingEntity.die / dropAllDeathLoot.
|
||||
@@ -687,6 +689,7 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
MobEvent::FinishAdmiring { mob } => self.finish_admiring(mob),
|
||||
MobEvent::Launch { target, up } => match target {
|
||||
Who::Player => {
|
||||
if let Some(p) = self.player.as_mut() {
|
||||
@@ -836,6 +839,9 @@ impl Game<'_> {
|
||||
if setup.baby {
|
||||
m.extra.age = -24000;
|
||||
}
|
||||
if setup.nausea {
|
||||
m.add_effect("minecraft:nausea", 200, 0);
|
||||
}
|
||||
if setup.size > 0 {
|
||||
m.extra.size = setup.size;
|
||||
}
|
||||
@@ -1361,6 +1367,17 @@ impl Game<'_> {
|
||||
.unwrap_or_default();
|
||||
let creative = self.creative;
|
||||
let selected = self.selected;
|
||||
// PiglinAi.mobInteract: hand over something gold to admire.
|
||||
if self.mobs[index].alive() && self.give_piglin(index, &item) {
|
||||
self.swing_arm();
|
||||
if !creative && let Some(stack) = self.inventory[selected].as_mut() {
|
||||
stack.count -= 1;
|
||||
if stack.count == 0 {
|
||||
self.inventory[selected] = None;
|
||||
}
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
let m = &mut self.mobs[index];
|
||||
if !m.alive() {
|
||||
return Ok(false);
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
//! The game side of piglins (`PiglinAi`): spotting and picking up gold,
|
||||
//! bartering when the admiring is over, and anger at players who open
|
||||
//! their chests or mine their gold.
|
||||
use super::Game;
|
||||
use horizon_client::{
|
||||
items::ItemStack,
|
||||
mobs::{Species, Who, piglin::ADMIRE},
|
||||
};
|
||||
use horizon_core::random::RandomSource;
|
||||
|
||||
impl Game<'_> {
|
||||
fn piglin_loved(&self, item: &str) -> bool {
|
||||
self.res
|
||||
.items
|
||||
.item_tags
|
||||
.get("minecraft:piglin_loved")
|
||||
.is_some_and(|t| t.contains(item))
|
||||
}
|
||||
/// `PiglinAi.isWearingSafeArmor`.
|
||||
pub(super) fn wearing_gold(&self) -> bool {
|
||||
let tag = self.res.items.item_tags.get("minecraft:piglin_safe_armor");
|
||||
self.armor
|
||||
.iter()
|
||||
.flatten()
|
||||
.any(|s| tag.is_some_and(|t| t.contains(&*s.item)))
|
||||
}
|
||||
/// Piglins notice gold on the ground within 9 blocks, walk to it and
|
||||
/// take it (`GoToWantedItem`, `Piglin.pickUpItem`).
|
||||
pub(super) fn piglin_pickups(&mut self) {
|
||||
if !self.ui.rules.bool("mob_griefing") {
|
||||
return;
|
||||
}
|
||||
for i in 0..self.mobs.len() {
|
||||
let m = &self.mobs[i];
|
||||
if m.species() != Species::Piglin
|
||||
|| !m.alive()
|
||||
|| m.is_baby()
|
||||
|| m.extra.using_item > 0
|
||||
|| m.extra.egg_time > 0
|
||||
|| m.extra.off_hand.is_some()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let pos = m.position;
|
||||
let best = self
|
||||
.dropped
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| self.piglin_loved(&e.stack.item))
|
||||
.map(|(j, e)| (j, e.position.distance_squared(pos)))
|
||||
.filter(|(_, d)| *d <= 81.0)
|
||||
.min_by(|a, b| a.1.total_cmp(&b.1));
|
||||
let Some((j, _)) = best else {
|
||||
self.mobs[i].extra.point = None;
|
||||
continue;
|
||||
};
|
||||
let e = &self.dropped[j];
|
||||
let close = (e.position.x - pos.x).abs() <= 1.3
|
||||
&& (e.position.z - pos.z).abs() <= 1.3
|
||||
&& (e.position.y - pos.y).abs() <= 1.0;
|
||||
if !close || e.pickup_delay > 0 {
|
||||
self.mobs[i].extra.point = Some([e.position.x, e.position.y, e.position.z]);
|
||||
continue;
|
||||
}
|
||||
// Nuggets come whole, anything else one at a time.
|
||||
let item = e.stack.item.clone();
|
||||
let taken = if &*item == "minecraft:gold_nugget" {
|
||||
self.dropped.remove(j).stack.count
|
||||
} else {
|
||||
let e = &mut self.dropped[j];
|
||||
e.stack.count -= 1;
|
||||
if e.stack.count == 0 {
|
||||
self.dropped.remove(j);
|
||||
}
|
||||
1
|
||||
};
|
||||
let _ = taken;
|
||||
self.start_admiring(i, item);
|
||||
}
|
||||
}
|
||||
fn start_admiring(&mut self, index: usize, item: std::sync::Arc<str>) {
|
||||
let m = &mut self.mobs[index];
|
||||
m.extra.off_hand = Some(item);
|
||||
m.extra.using_item = ADMIRE;
|
||||
m.extra.point = None;
|
||||
m.nav_stop();
|
||||
let at = m.center();
|
||||
self.audio
|
||||
.play("minecraft:entity.piglin.admiring_item", Some(at), 1.0, 1.0);
|
||||
}
|
||||
/// `PiglinAi.mobInteract`: hand a piglin something it loves.
|
||||
pub(super) fn give_piglin(&mut self, index: usize, item: &str) -> bool {
|
||||
let m = &self.mobs[index];
|
||||
if m.species() != Species::Piglin
|
||||
|| m.is_baby()
|
||||
|| m.extra.using_item > 0
|
||||
|| m.extra.egg_time > 0
|
||||
|| !self.piglin_loved(item)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
self.start_admiring(index, item.into());
|
||||
true
|
||||
}
|
||||
/// `PiglinAi.stopHoldingOffHandItem` after admiring: gold ingots buy a
|
||||
/// roll of the bartering table, thrown toward the player.
|
||||
pub(super) fn finish_admiring(&mut self, mob: u32) {
|
||||
let Some(i) = self.mobs.iter().position(|m| m.id == mob) else {
|
||||
return;
|
||||
};
|
||||
let Some(item) = self.mobs[i].extra.off_hand.take() else {
|
||||
return;
|
||||
};
|
||||
let m = &self.mobs[i];
|
||||
let from = [m.position.x, m.position.y + 1.3, m.position.z];
|
||||
let items = if &*item == "minecraft:gold_ingot" && !m.is_baby() {
|
||||
self.res.items.loot_table(
|
||||
"minecraft:gameplay/piglin_bartering",
|
||||
false,
|
||||
None,
|
||||
&mut self.world.random,
|
||||
)
|
||||
} else {
|
||||
vec![ItemStack::new(&item, 1)]
|
||||
};
|
||||
// BehaviorUtils.throwItem toward the nearest visible player, else nearby.
|
||||
let target = self
|
||||
.player
|
||||
.as_ref()
|
||||
.filter(|p| p.position.distance_squared(m.position) < 256.0)
|
||||
.map(|p| [p.position.x, p.position.y + 1.0, p.position.z]);
|
||||
for stack in items {
|
||||
let to = target.unwrap_or_else(|| {
|
||||
let r = &mut self.world.random;
|
||||
[
|
||||
from[0] + f64::from(r.next_float() * 8.0 - 4.0),
|
||||
from[1],
|
||||
from[2] + f64::from(r.next_float() * 8.0 - 4.0),
|
||||
]
|
||||
});
|
||||
let d = [to[0] - from[0], to[1] - from[1], to[2] - from[2]];
|
||||
let len = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt().max(1e-6);
|
||||
let v = [d[0] / len * 0.3, d[1] / len * 0.3 + 0.1, d[2] / len * 0.3];
|
||||
self.spawn_item(stack, from, v, 40);
|
||||
}
|
||||
}
|
||||
/// `PiglinAi.angerNearbyPiglins`.
|
||||
pub(super) fn anger_piglins(&mut self, only_if_seen: bool) {
|
||||
let Some(p) = self.player.as_ref() else {
|
||||
return;
|
||||
};
|
||||
if self.creative || self.spectator() {
|
||||
return;
|
||||
}
|
||||
let at = p.position;
|
||||
let eye = [at.x, at.y + 1.62, at.z];
|
||||
for i in 0..self.mobs.len() {
|
||||
let m = &self.mobs[i];
|
||||
if m.species() != Species::Piglin
|
||||
|| m.is_baby()
|
||||
|| !m.alive()
|
||||
|| m.ai.target.is_some()
|
||||
|| (m.position.x - at.x).abs() > 16.0
|
||||
|| (m.position.y - at.y).abs() > 16.0
|
||||
|| (m.position.z - at.z).abs() > 16.0
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if only_if_seen && !self.can_see(m.center(), eye) {
|
||||
continue;
|
||||
}
|
||||
let m = &mut self.mobs[i];
|
||||
m.extra.anger = 600;
|
||||
m.ai.target = Some(Who::Player);
|
||||
}
|
||||
}
|
||||
/// A clear line of sight between two points (blocks with collision stop it).
|
||||
fn can_see(&self, from: [f64; 3], to: [f64; 3]) -> bool {
|
||||
let steps = 32;
|
||||
(1..steps).all(|i| {
|
||||
let f = f64::from(i) / f64::from(steps);
|
||||
let p = [
|
||||
from[0] + (to[0] - from[0]) * f,
|
||||
from[1] + (to[1] - from[1]) * f,
|
||||
from[2] + (to[2] - from[2]) * f,
|
||||
];
|
||||
!self
|
||||
.res
|
||||
.facts
|
||||
.get(self.world.block(
|
||||
p[0].floor() as i32,
|
||||
p[1].floor() as i32,
|
||||
p[2].floor() as i32,
|
||||
))
|
||||
.full_collision
|
||||
})
|
||||
}
|
||||
/// Whether breaking or opening this block angers piglins.
|
||||
pub(super) fn guarded_by_piglins(&self, name: &str) -> bool {
|
||||
self.res
|
||||
.items
|
||||
.block_tags
|
||||
.get("minecraft:guarded_by_piglins")
|
||||
.is_some_and(|t| t.contains(name))
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ pub mod living;
|
||||
pub mod nav;
|
||||
pub mod nether;
|
||||
pub mod path;
|
||||
pub mod piglin;
|
||||
pub mod render;
|
||||
pub mod spawning;
|
||||
pub mod species;
|
||||
@@ -568,6 +569,10 @@ pub enum MobEvent {
|
||||
name: &'static str,
|
||||
effect: bool,
|
||||
},
|
||||
/// A piglin's admiring is over: barter or keep the item.
|
||||
FinishAdmiring {
|
||||
mob: u32,
|
||||
},
|
||||
/// Throw an entity upward (iron golem swings).
|
||||
Launch {
|
||||
target: Who,
|
||||
@@ -595,6 +600,8 @@ pub struct SpawnSetup {
|
||||
pub health: Option<f32>,
|
||||
pub persistent: bool,
|
||||
pub variant: Option<u8>,
|
||||
/// Zombified piglins and zoglins wake up nauseous (`ConversionParams`).
|
||||
pub nausea: bool,
|
||||
}
|
||||
/// A snapshot of another entity mobs can see this tick.
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -628,6 +635,8 @@ pub struct PlayerView {
|
||||
pub head: Option<Arc<str>>,
|
||||
pub armor_cover: f32,
|
||||
pub spectator: bool,
|
||||
/// `PiglinAi.isWearingSafeArmor`: some golden armour.
|
||||
pub gold_armor: bool,
|
||||
/// `getLastHurtByMob` (within 100 ticks) and when.
|
||||
pub hurt_by: Option<(Who, i64)>,
|
||||
/// `getLastHurtMob` and when.
|
||||
|
||||
@@ -74,6 +74,8 @@ pub enum Family {
|
||||
Nether,
|
||||
Golem,
|
||||
Illager,
|
||||
/// Piglins, brutes, hoglins and zoglins.
|
||||
Piglin,
|
||||
/// Slimes and magma cubes.
|
||||
Cube,
|
||||
}
|
||||
@@ -105,6 +107,7 @@ pub fn family(s: Species) -> Family {
|
||||
S::Blaze | S::Ghast => Family::Nether,
|
||||
S::IronGolem | S::SnowGolem => Family::Golem,
|
||||
S::Pillager | S::Vindicator => Family::Illager,
|
||||
S::Piglin | S::PiglinBrute | S::Hoglin | S::Zoglin => Family::Piglin,
|
||||
S::Enderman => Family::Enderman,
|
||||
S::Slime | S::MagmaCube => Family::Cube,
|
||||
_ => Family::Generic,
|
||||
@@ -127,6 +130,12 @@ pub fn register(m: &mut Mob) {
|
||||
Family::Golem => super::golem::register_snow(m),
|
||||
Family::Illager if m.species() == Species::Pillager => super::illager::register_pillager(m),
|
||||
Family::Illager => super::illager::register_vindicator(m),
|
||||
Family::Piglin => match m.species() {
|
||||
Species::Piglin => super::piglin::register_piglin(m),
|
||||
Species::PiglinBrute => super::piglin::register_brute(m),
|
||||
Species::Hoglin => super::piglin::register_hoglin(m),
|
||||
_ => super::piglin::register_zoglin(m),
|
||||
},
|
||||
Family::Enderman => super::enderman::register(m),
|
||||
Family::Cube => super::cube::register(m),
|
||||
}
|
||||
@@ -483,6 +492,10 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
Species::Cat => super::tame::cat_ai_step(m, events),
|
||||
Species::Blaze => super::nether::blaze_ai_step(m, events),
|
||||
Species::IronGolem => super::golem::iron_ai_step(m),
|
||||
Species::Piglin | Species::PiglinBrute | Species::Hoglin => {
|
||||
super::piglin::ai_step(m, w, events)
|
||||
}
|
||||
Species::Zoglin if m.extra.anim_ticks > 0 => m.extra.anim_ticks -= 1,
|
||||
Species::SnowGolem => super::golem::snow_ai_step(m, w, events),
|
||||
Species::Slime | Species::MagmaCube => super::cube::ai_step(m, w, events),
|
||||
Species::Chicken => farm::chicken_ai_step(m, w, events),
|
||||
@@ -588,6 +601,13 @@ pub fn after_hurt(m: &mut Mob, source: &Damage) {
|
||||
Species::Enderman => super::enderman::after_hurt(m, source),
|
||||
Species::Wolf => super::tame::after_hurt(m, source),
|
||||
// NeutralMob: an iron golem hit by the player stays angry a while.
|
||||
// PiglinAi.wasHurtBy: drop what is being admired, no admiring for
|
||||
// a while after a player's hit, and anger.
|
||||
Species::Piglin if source.attacker == Some(Who::Player) => {
|
||||
m.extra.using_item = 0;
|
||||
m.extra.egg_time = 400;
|
||||
m.extra.anger = 600;
|
||||
}
|
||||
Species::IronGolem if source.attacker == Some(Who::Player) => {
|
||||
m.extra.anger = 400 + m.random.next_int_bound(380).unwrap_or(0);
|
||||
}
|
||||
@@ -599,6 +619,9 @@ pub fn do_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) {
|
||||
if m.species() == Species::IronGolem {
|
||||
return super::golem::iron_hurt_target(m, who, cx);
|
||||
}
|
||||
if matches!(m.species(), Species::Hoglin | Species::Zoglin) {
|
||||
return super::piglin::hoglin_hurt_target(m, who, cx);
|
||||
}
|
||||
let mut amount = m.attrs.attack_damage;
|
||||
let from = [m.position.x, m.position.y, m.position.z];
|
||||
let source = Damage::mob(m.id, from);
|
||||
|
||||
@@ -205,6 +205,15 @@ pub enum ArmPose {
|
||||
CrossbowHold,
|
||||
CrossbowCharge,
|
||||
}
|
||||
/// HumanoidModel.setupAnim holding an item or not (piglins).
|
||||
pub fn humanoid_item(p: &mut Poses, s: &State, holding: bool) {
|
||||
let right = if holding {
|
||||
ArmPose::Item
|
||||
} else {
|
||||
ArmPose::Empty
|
||||
};
|
||||
humanoid(p, s, right, ArmPose::Empty, None, holding);
|
||||
}
|
||||
/// HumanoidModel.setupAnim with empty hands (endermen).
|
||||
pub fn humanoid_pub(p: &mut Poses, s: &State, _m: &Mob) {
|
||||
humanoid(p, s, ArmPose::Empty, ArmPose::Empty, None, false);
|
||||
@@ -421,6 +430,8 @@ pub fn setup(
|
||||
Anim::Wolf => super::tame::anim(m, s, p),
|
||||
Anim::IronGolem => super::golem::iron_anim(m, s, p),
|
||||
Anim::Illager => super::illager::anim(m, s, p),
|
||||
Anim::Piglin => super::piglin::piglin_anim(m, s, p),
|
||||
Anim::Hoglin => super::piglin::hoglin_anim(m, s, p),
|
||||
Anim::Blaze => super::nether::blaze_anim(s, p),
|
||||
Anim::Ghast => super::nether::ghast_anim(s, p),
|
||||
Anim::Feline if m.species() == super::Species::Cat => super::tame::cat_anim(m, s, p),
|
||||
|
||||
@@ -83,6 +83,10 @@ impl CrossbowAttack {
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The crossbow goal for other crossbow users (piglins).
|
||||
pub fn crossbow_goal(speed: f64, radius: f64) -> impl Goal {
|
||||
CrossbowAttack::new(speed, radius)
|
||||
}
|
||||
/// `CrossbowItem.getChargeDuration` without Quick Charge.
|
||||
const CHARGE: i32 = 25;
|
||||
fn holding_crossbow(m: &Mob) -> bool {
|
||||
|
||||
@@ -0,0 +1,298 @@
|
||||
//! Piglins, piglin brutes, hoglins and zoglins: gold-wearing players are
|
||||
//! left alone, gold is admired and bartered, piglins and hoglins zombify
|
||||
//! outside the Nether, hoglins headbutt their targets into the air.
|
||||
use super::{
|
||||
Mob, MobEvent, MobWorld, Species, Who,
|
||||
anim::{Poses, State},
|
||||
goals::{
|
||||
Avoid, AvoidEntity, Cx, Float, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, MeleeAttack,
|
||||
NearestTarget, Prey, RandomLookAround, Stroll, goal_clone,
|
||||
},
|
||||
};
|
||||
use horizon_core::random::RandomSource;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
/// `PiglinAi.ADMIRE_DURATION`.
|
||||
pub const ADMIRE: i32 = 119;
|
||||
/// `AbstractPiglin.CONVERSION_TIME` (and the hoglin's).
|
||||
const CONVERSION: i32 = 300;
|
||||
|
||||
fn hostile_to_player(m: &Mob, w: &MobWorld) -> bool {
|
||||
if m.is_baby() {
|
||||
return false;
|
||||
}
|
||||
match m.species() {
|
||||
Species::Piglin => m.extra.anger > 0 || w.player.as_ref().is_some_and(|p| !p.gold_armor),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
pub fn register_piglin(m: &mut Mob) {
|
||||
let crossbow = m.extra.main_hand.as_deref() == Some("minecraft:crossbow");
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(1, Admire);
|
||||
g.add(
|
||||
2,
|
||||
AvoidEntity::new(
|
||||
Avoid::Species(vec![Species::ZombifiedPiglin, Species::Zoglin]),
|
||||
6.0,
|
||||
1.0,
|
||||
1.2,
|
||||
),
|
||||
);
|
||||
if crossbow {
|
||||
g.add(3, super::illager::crossbow_goal(1.0, 8.0));
|
||||
}
|
||||
g.add(3, MeleeAttack::new(1.0, true));
|
||||
g.add(4, GoToWantedItem);
|
||||
g.add(5, Stroll::avoiding_water(0.6));
|
||||
g.add(6, LookAtPlayer::new(8.0));
|
||||
g.add(7, RandomLookAround::default());
|
||||
let mut hurt = HurtByTarget::new().alert();
|
||||
hurt.ignore = vec![Species::Piglin, Species::PiglinBrute];
|
||||
m.ai.targets.add(1, hurt);
|
||||
m.ai.targets
|
||||
.add(2, NearestTarget::player(true).when(hostile_to_player));
|
||||
m.ai.targets.add(
|
||||
3,
|
||||
NearestTarget::new(
|
||||
Prey::Filter(|s| s.species == Species::Hoglin && s.baby),
|
||||
true,
|
||||
)
|
||||
.interval(10)
|
||||
.when(|m, _| !m.is_baby()),
|
||||
);
|
||||
}
|
||||
pub fn register_brute(m: &mut Mob) {
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(2, MeleeAttack::new(1.0, true));
|
||||
g.add(5, Stroll::avoiding_water(0.6));
|
||||
g.add(6, LookAtPlayer::new(8.0));
|
||||
let mut hurt = HurtByTarget::new().alert();
|
||||
hurt.ignore = vec![Species::Piglin, Species::PiglinBrute];
|
||||
m.ai.targets.add(1, hurt);
|
||||
m.ai.targets.add(2, NearestTarget::player(true));
|
||||
m.ai.targets
|
||||
.add(3, NearestTarget::species(&[Species::WitherSkeleton], true));
|
||||
}
|
||||
pub fn register_hoglin(m: &mut Mob) {
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(1, BabyPanic(super::goals::Panic::new(1.25)));
|
||||
g.add(2, MeleeAttack::new(1.0, true));
|
||||
g.add(3, super::goals::Breed::new(1.0));
|
||||
g.add(4, super::goals::FollowParent::new(1.1));
|
||||
g.add(5, Stroll::avoiding_water(0.4));
|
||||
g.add(6, LookAtPlayer::new(8.0));
|
||||
m.ai.targets.add(1, HurtByTarget::new().alert());
|
||||
m.ai.targets
|
||||
.add(2, NearestTarget::player(true).when(|m, _| !m.is_baby()));
|
||||
}
|
||||
pub fn register_zoglin(m: &mut Mob) {
|
||||
let g = &mut m.ai.goals;
|
||||
g.add(0, Float);
|
||||
g.add(2, MeleeAttack::new(1.0, true));
|
||||
g.add(5, Stroll::avoiding_water(0.4));
|
||||
g.add(6, LookAtPlayer::new(8.0));
|
||||
m.ai.targets.add(1, HurtByTarget::new());
|
||||
m.ai.targets.add(2, NearestTarget::player(true));
|
||||
// Zoglin.isTargetable: anything but creepers and other zoglins.
|
||||
m.ai.targets.add(
|
||||
3,
|
||||
NearestTarget::new(
|
||||
Prey::Filter(|s| !matches!(s.species, Species::Creeper | Species::Zoglin)),
|
||||
true,
|
||||
)
|
||||
.interval(10),
|
||||
);
|
||||
}
|
||||
/// Baby hoglins flee what hurts them; adults stand their ground.
|
||||
#[derive(Clone, Debug)]
|
||||
struct BabyPanic(super::goals::Panic);
|
||||
impl Goal for BabyPanic {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
self.0.flags()
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
m.is_baby() && self.0.can_use(m, cx)
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
self.0.can_continue(m, cx)
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.0.start(m, cx)
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, cx: &mut Cx) {
|
||||
self.0.stop(m, cx)
|
||||
}
|
||||
}
|
||||
/// Admiring a gold item held in the off hand: stand and look at it.
|
||||
#[derive(Clone, Debug)]
|
||||
struct Admire;
|
||||
impl Goal for Admire {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE | LOOK
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
|
||||
m.extra.using_item > 0
|
||||
}
|
||||
fn start(&mut self, m: &mut Mob, _: &mut Cx) {
|
||||
m.nav_stop();
|
||||
}
|
||||
}
|
||||
/// `GoToWantedItem`: walk to the gold the game spotted.
|
||||
#[derive(Clone, Debug)]
|
||||
struct GoToWantedItem;
|
||||
impl Goal for GoToWantedItem {
|
||||
goal_clone!();
|
||||
fn flags(&self) -> u8 {
|
||||
MOVE
|
||||
}
|
||||
fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool {
|
||||
let Some(p) = m.extra.point else {
|
||||
return false;
|
||||
};
|
||||
m.extra.using_item == 0 && m.nav_move_to(cx.w, p, 1.0)
|
||||
}
|
||||
fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool {
|
||||
m.extra.point.is_some() && !m.ai.nav.is_done()
|
||||
}
|
||||
fn stop(&mut self, m: &mut Mob, _: &mut Cx) {
|
||||
m.extra.point = None;
|
||||
}
|
||||
}
|
||||
/// Per-tick piglin and hoglin state: admiring, anger, zombification and the
|
||||
/// hoglin attack animation. Returns a conversion when it happens.
|
||||
pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec<MobEvent>) {
|
||||
if m.extra.anim_ticks > 0 {
|
||||
m.extra.anim_ticks -= 1;
|
||||
}
|
||||
if m.extra.anger > 0 {
|
||||
m.extra.anger -= 1;
|
||||
if m.extra.anger == 0 && m.ai.target == Some(Who::Player) && !hostile_to_player(m, w) {
|
||||
m.ai.target = None;
|
||||
}
|
||||
}
|
||||
// Admiring disabled after being hit by a player.
|
||||
if m.extra.egg_time > 0 {
|
||||
m.extra.egg_time -= 1;
|
||||
}
|
||||
if m.extra.using_item > 0 {
|
||||
m.extra.using_item -= 1;
|
||||
if m.extra.using_item == 0 {
|
||||
events.push(MobEvent::FinishAdmiring { mob: m.id });
|
||||
}
|
||||
}
|
||||
// Zombification outside the Nether (piglins without immunity, hoglins).
|
||||
if !w.nether && !m.extra.flag2 && m.alive() {
|
||||
m.extra.counter2 += 1;
|
||||
if m.extra.counter2 > CONVERSION {
|
||||
let (kind, sound) = match m.species() {
|
||||
Species::Hoglin => ("minecraft:zoglin", "entity.hoglin.converted_to_zombified"),
|
||||
Species::PiglinBrute => (
|
||||
"minecraft:zombified_piglin",
|
||||
"entity.piglin_brute.converted_to_zombified",
|
||||
),
|
||||
_ => (
|
||||
"minecraft:zombified_piglin",
|
||||
"entity.piglin.converted_to_zombified",
|
||||
),
|
||||
};
|
||||
events.push(MobEvent::Sound(sound, m.center(), 1.0, 1.0));
|
||||
events.push(MobEvent::SpawnKind {
|
||||
kind: kind.into(),
|
||||
at: [m.position.x, m.position.y, m.position.z],
|
||||
setup: super::SpawnSetup {
|
||||
baby: m.is_baby(),
|
||||
yaw: m.yaw,
|
||||
persistent: m.persistent,
|
||||
nausea: true,
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
m.removed = true;
|
||||
}
|
||||
} else {
|
||||
m.extra.counter2 = 0;
|
||||
}
|
||||
}
|
||||
/// `HoglinBase.hurtAndThrowTarget`.
|
||||
pub fn hoglin_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) {
|
||||
m.extra.anim_ticks = 10;
|
||||
let attack = m.attrs.attack_damage;
|
||||
let amount = if !m.is_baby() && attack as i32 > 0 {
|
||||
attack / 2.0 + m.random.next_int_bound(attack as i32).unwrap_or(0) as f32
|
||||
} else {
|
||||
attack
|
||||
};
|
||||
let from = [m.position.x, m.position.y, m.position.z];
|
||||
match who {
|
||||
Who::Player => cx.events.push(MobEvent::AttackPlayer(amount, from)),
|
||||
Who::Mob(id) => cx.events.push(MobEvent::AttackMob {
|
||||
target: id,
|
||||
amount,
|
||||
source: super::Damage::mob(m.id, from),
|
||||
}),
|
||||
}
|
||||
if !m.is_baby() {
|
||||
// throwTarget: a strong shove up and away.
|
||||
cx.events.push(MobEvent::Launch {
|
||||
target: who,
|
||||
up: 0.5 * f64::from(m.random.next_float()) + 0.3,
|
||||
});
|
||||
}
|
||||
let sound = if m.species() == Species::Zoglin {
|
||||
"entity.zoglin.attack"
|
||||
} else {
|
||||
"entity.hoglin.attack"
|
||||
};
|
||||
cx.events.push(MobEvent::Sound(sound, m.center(), 1.0, 1.0));
|
||||
}
|
||||
/// Piglins' and brutes' ears flap (`AbstractPiglinModel`), arms follow the
|
||||
/// arm pose (`PiglinModel`).
|
||||
pub fn piglin_anim(m: &Mob, s: &State, p: &mut Poses) {
|
||||
super::anim::humanoid_item(p, s, m.extra.main_hand.is_some());
|
||||
let default = PI / 6.0;
|
||||
let freq = s.age * 0.1 + s.pos * 0.5;
|
||||
let amp = 0.08 + s.speed * 0.4;
|
||||
p.zrot("left_ear", -default - (freq * 1.2).cos() * amp);
|
||||
p.zrot("right_ear", default + freq.cos() * amp);
|
||||
let (hy, hx) = (s.y_rot.to_radians(), s.x_rot.to_radians());
|
||||
if m.extra.using_item > 0 {
|
||||
// ADMIRING_ITEM: head down at the left hand.
|
||||
p.xrot("head", 0.5);
|
||||
p.yrot("head", 0.0);
|
||||
p.yrot("left_arm", 0.5);
|
||||
p.xrot("left_arm", -0.9);
|
||||
} else if m.extra.flag {
|
||||
p.yrot("right_arm", -0.8);
|
||||
p.xrot("right_arm", -0.970_796_35);
|
||||
let a = (m.extra.using_item as f32).clamp(0.0, 25.0) / 25.0;
|
||||
p.yrot("left_arm", 0.4 + 0.45 * a);
|
||||
p.xrot("left_arm", -0.970_796_35 + (-PI / 2.0 + 0.970_796_35) * a);
|
||||
} else if m.extra.main_hand.as_deref() == Some("minecraft:crossbow") && m.ai.aggressive {
|
||||
p.yrot("right_arm", -0.3 + hy);
|
||||
p.yrot("left_arm", 0.6 + hy);
|
||||
p.xrot("right_arm", -PI / 2.0 + hx + 0.1);
|
||||
p.xrot("left_arm", -1.5 + hx);
|
||||
} else if m.ai.aggressive && m.extra.main_hand.is_some() && s.swing == 0.0 {
|
||||
p.xrot("right_arm", -1.8);
|
||||
}
|
||||
}
|
||||
/// `HoglinModel.setupAnim`.
|
||||
pub fn hoglin_anim(m: &Mob, s: &State, p: &mut Poses) {
|
||||
let (pos, sp) = (s.pos, s.speed);
|
||||
p.zrot("right_ear", -PI * 2.0 / 9.0 - sp * pos.sin());
|
||||
p.zrot("left_ear", PI * 2.0 / 9.0 + sp * pos.sin());
|
||||
p.yrot("head", s.y_rot.to_radians());
|
||||
let remaining = m.extra.anim_ticks as f32;
|
||||
let f = 1.0 - (10.0 - 2.0 * remaining).abs() / 10.0;
|
||||
p.xrot("head", 0.872_664_63 + (-PI / 9.0 - 0.872_664_63) * f);
|
||||
p.xrot("right_front_leg", pos.cos() * 1.2 * sp);
|
||||
p.xrot("left_front_leg", (pos + PI).cos() * 1.2 * sp);
|
||||
p.xrot("right_hind_leg", (pos + PI).cos() * 1.2 * sp);
|
||||
p.xrot("left_hind_leg", pos.cos() * 1.2 * sp);
|
||||
}
|
||||
@@ -209,6 +209,19 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts) {
|
||||
// ZombifiedPiglin.populateDefaultEquipmentSlots: a golden sword.
|
||||
S::ZombifiedPiglin => m.extra.main_hand = Some("minecraft:golden_sword".into()),
|
||||
S::Pillager => m.extra.main_hand = Some("minecraft:crossbow".into()),
|
||||
// Piglin.createSpawnWeapon: half crossbows, half golden swords.
|
||||
S::Piglin if !m.is_baby() => {
|
||||
let crossbow = m.random.next_float() < 0.5;
|
||||
m.extra.main_hand = Some(
|
||||
if crossbow {
|
||||
"minecraft:crossbow"
|
||||
} else {
|
||||
"minecraft:golden_sword"
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
S::PiglinBrute => m.extra.main_hand = Some("minecraft:golden_axe".into()),
|
||||
S::Vindicator => m.extra.main_hand = Some("minecraft:iron_axe".into()),
|
||||
S::Villager => {
|
||||
m.extra.variant = villager_type(biome);
|
||||
|
||||
Reference in New Issue
Block a user