Add the ender dragon fight
The dragon with its phases, path nodes, fireballs and breath, end crystals that heal it, the boss bar and boss music, and the kill: XP, the exit portal, the egg and a new gateway. The fight state is saved where 26.3 saves it. Creative players are now invulnerable, as in vanilla. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ mod commands;
|
||||
mod control;
|
||||
mod creatures;
|
||||
mod crossbow;
|
||||
mod dragon;
|
||||
mod enchant_effects;
|
||||
mod end;
|
||||
mod explosion;
|
||||
@@ -569,6 +570,13 @@ struct Game<'a> {
|
||||
jukebox_sounds: HashMap<(i32, i32, i32), u64>,
|
||||
bolts: Vec<lightning::Bolt>,
|
||||
maps: maps::Maps,
|
||||
/// The ender dragon fight (game/dragon.rs).
|
||||
fight: dragon::Fight,
|
||||
dragon: Option<dragon::Dragon>,
|
||||
crystals: Vec<dragon::Crystal>,
|
||||
dragon_fireballs: Vec<dragon::DragonFireball>,
|
||||
breath_clouds: Vec<dragon::Breath>,
|
||||
dragon_model: Option<horizon_client::mobs::Part>,
|
||||
/// Map textures waiting for the next HUD batch to upload them.
|
||||
map_patches: Vec<horizon_client::font::Patch>,
|
||||
cart_models: minecarts::CartModels,
|
||||
@@ -2084,6 +2092,7 @@ impl Game<'_> {
|
||||
let mut player = PlayerMotion::new(self.motion, feet)?;
|
||||
player.yaw = self.yaw;
|
||||
player.may_fly = self.creative;
|
||||
player.invulnerable = self.creative;
|
||||
player.pitch = self.pitch;
|
||||
// LivingEntity.canGlideUsing: a glider in its slot that will not break next.
|
||||
player.glider = self.armor[1].as_ref().is_some_and(|s| {
|
||||
@@ -2166,6 +2175,7 @@ impl Game<'_> {
|
||||
}
|
||||
player.yaw = self.yaw;
|
||||
player.may_fly = self.creative;
|
||||
player.invulnerable = self.creative;
|
||||
player.pitch = self.pitch;
|
||||
// LivingEntity.canGlideUsing: a glider in its slot that will not break next.
|
||||
player.glider = self.armor[1].as_ref().is_some_and(|s| {
|
||||
@@ -2679,6 +2689,7 @@ impl Game<'_> {
|
||||
}
|
||||
self.draw_effect_icons(&mut batch);
|
||||
// Hud: the action bar and titles draw over the hotbar (nextStratum).
|
||||
self.draw_boss_bar(&mut batch);
|
||||
self.draw_titles(&mut batch);
|
||||
let atlas = &self.res.atlas;
|
||||
if !self.creative
|
||||
@@ -3969,6 +3980,7 @@ impl Game<'_> {
|
||||
|| self.attack_armor_stand()
|
||||
|| self.attack_boat(hand_damage)
|
||||
|| self.attack_cart(hand_damage)
|
||||
|| self.attack_dragon()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -6269,6 +6281,7 @@ impl Game<'_> {
|
||||
.save(&chunks, self.res.assets.registry, &self.res.biomes)?;
|
||||
self.save_entities()?;
|
||||
self.save_maps();
|
||||
self.save_fight();
|
||||
self.level.selected_slot = self.selected as i32;
|
||||
let level = &mut self.level;
|
||||
level.creative = self.creative;
|
||||
@@ -6430,7 +6443,7 @@ impl Game<'_> {
|
||||
// Map textures changed since the last drawn frame (game/maps.rs).
|
||||
hud.patches.append(&mut self.map_patches);
|
||||
let title = self.title.is_some();
|
||||
let entities = if title {
|
||||
let mut entities = if title {
|
||||
Vec::new()
|
||||
} else {
|
||||
self.entity_geometry(camera, alpha)
|
||||
@@ -6438,6 +6451,12 @@ impl Game<'_> {
|
||||
let mut additive = Vec::new();
|
||||
if !title {
|
||||
self.bolt_geometry(camera, &mut additive);
|
||||
if self.dim == Dim::End {
|
||||
let mut dragon = Vec::new();
|
||||
self.dragon_geometry(camera, alpha, &mut dragon, &mut additive);
|
||||
self.dragon_fireball_geometry(camera, alpha, &mut dragon);
|
||||
entities.extend(dragon);
|
||||
}
|
||||
}
|
||||
let celestial = if title {
|
||||
self.panorama_geometry()
|
||||
@@ -6458,7 +6477,6 @@ impl Game<'_> {
|
||||
} else {
|
||||
self.screen_quads(alpha as f32, size)
|
||||
};
|
||||
let mut entities = entities;
|
||||
if !title {
|
||||
self.end_geometry(camera, &mut entities);
|
||||
}
|
||||
@@ -7366,6 +7384,12 @@ pub fn run() -> Result<()> {
|
||||
jukebox_sounds: HashMap::new(),
|
||||
bolts: Vec::new(),
|
||||
maps: maps::Maps::default(),
|
||||
fight: dragon::Fight::default(),
|
||||
dragon: None,
|
||||
crystals: Vec::new(),
|
||||
dragon_fireballs: Vec::new(),
|
||||
breath_clouds: Vec::new(),
|
||||
dragon_model: horizon_client::mobs::load_model(&root, "minecraft:ender_dragon#main"),
|
||||
map_patches: Vec::new(),
|
||||
cart_models: minecarts::CartModels::load(&root),
|
||||
riding: None,
|
||||
|
||||
@@ -386,6 +386,12 @@ impl Game<'_> {
|
||||
self.save_game()?;
|
||||
ok()
|
||||
}
|
||||
"dragon-hurt" => {
|
||||
// Test hook: damage the dragon's head as the player would.
|
||||
let amount: f32 = arg(1)?.parse()?;
|
||||
self.hit_special(super::dragon::SPECIAL, amount, true);
|
||||
ok()
|
||||
}
|
||||
"quit" => {
|
||||
if self.title.is_none() {
|
||||
// Player.closeContainer: menu items go back before saving.
|
||||
@@ -482,6 +488,7 @@ impl Game<'_> {
|
||||
"fishing": self.hook_status(),
|
||||
"tridents": self.tridents.len(),
|
||||
"rockets": self.rockets.len(),
|
||||
"fight": self.dragon_status(),
|
||||
"carts": self.carts.iter().map(|c| serde_json::json!({"kind": c.kind.id(), "position": [c.position.x, c.position.y, c.position.z], "velocity": [c.velocity.x, c.velocity.y, c.velocity.z]})).collect::<Vec<_>>(),
|
||||
"riding": self.riding,
|
||||
"using": self.using,
|
||||
|
||||
@@ -956,6 +956,12 @@ impl Game<'_> {
|
||||
{
|
||||
targets.push((Who::Player, p.bounds));
|
||||
}
|
||||
// Dragon parts and end crystals under reserved ids (game/dragon.rs).
|
||||
targets.extend(
|
||||
self.special_targets()
|
||||
.into_iter()
|
||||
.map(|(id, b)| (Who::Mob(id), b)),
|
||||
);
|
||||
let w = self.mob_world_base(&[]);
|
||||
let pw = ProjWorld { w: &w, targets };
|
||||
projectiles.retain_mut(|p| {
|
||||
@@ -977,6 +983,19 @@ impl Game<'_> {
|
||||
Hit::Entity { at, .. } | Hit::Block { at, .. } => at,
|
||||
Hit::None => return Ok(()),
|
||||
};
|
||||
if let Hit::Entity {
|
||||
who: Who::Mob(id), ..
|
||||
} = hit
|
||||
&& id >= super::dragon::SPECIAL
|
||||
{
|
||||
let amount = match p.kind {
|
||||
ProjectileKind::Arrow { .. } => p.arrow_damage(&mut self.world.random),
|
||||
ProjectileKind::SmallFireball | ProjectileKind::LargeFireball { .. } => 6.0,
|
||||
_ => 0.0,
|
||||
};
|
||||
self.hit_special(id, amount, p.owner == Some(Who::Player));
|
||||
return Ok(());
|
||||
}
|
||||
let atp = [at.x, at.y, at.z];
|
||||
let source_kind = match p.kind {
|
||||
ProjectileKind::Arrow { .. } => DamageKind::Arrow,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
//! The End: End portal frames filled with eyes of ender, End portal and End
|
||||
//! gateway travel, the obsidian arrival platform, the exit portal and the End
|
||||
//! sky. The ender dragon is not implemented: the fight counts as already won,
|
||||
//! so the exit portal is active and the first gateway exists.
|
||||
//! sky. The dragon fight itself lives in game/dragon.rs: the podium is
|
||||
//! inactive until the dragon dies.
|
||||
use super::{Dim, Game};
|
||||
use horizon_client::{
|
||||
Result, environment::Environment, items::ItemStack, mesher::Vertex, motion::PlayerMotion,
|
||||
@@ -343,6 +343,10 @@ impl Game<'_> {
|
||||
/// End portals, End gateways and the exit portal, once per tick.
|
||||
pub(super) fn tick_end(&mut self) -> Result<()> {
|
||||
self.end.gateway_cooldown = self.end.gateway_cooldown.saturating_sub(1);
|
||||
if self.dim == Dim::End {
|
||||
self.tick_fight();
|
||||
self.tick_dragon_fireballs();
|
||||
}
|
||||
self.scan_portals();
|
||||
self.tick_eyes();
|
||||
if self.dim == Dim::End {
|
||||
@@ -510,13 +514,16 @@ impl Game<'_> {
|
||||
fn ensure_exit_portal(&mut self) -> Result<()> {
|
||||
if self.travelling.is_some()
|
||||
|| !(-1..=0).all(|cz| (-1..=0).all(|cx| self.streaming.is_lit(cx, cz)))
|
||||
|| !self.fight_ready()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
let won = self.fight.previously_killed;
|
||||
let gateway = self.first_gateway();
|
||||
if (0..256).any(|y| &*self.block_name([0, y, 0]) == "minecraft:bedrock") {
|
||||
// The podium exists; the gateway may have been out of range when it was placed.
|
||||
if self.world.loaded(gateway[0], gateway[2])
|
||||
if won
|
||||
&& self.world.loaded(gateway[0], gateway[2])
|
||||
&& &*self.block_name(gateway) != "minecraft:end_gateway"
|
||||
&& &*self.block_name([gateway[0], gateway[1] + 2, gateway[2]])
|
||||
!= "minecraft:bedrock"
|
||||
@@ -538,12 +545,31 @@ impl Game<'_> {
|
||||
y -= 1;
|
||||
}
|
||||
let origin = [0, y.max(1), 0];
|
||||
self.place_podium(origin)?;
|
||||
if self.world.loaded(gateway[0], gateway[2]) {
|
||||
self.set_exit_portal(origin);
|
||||
self.place_podium(origin, won)?;
|
||||
if won && self.world.loaded(gateway[0], gateway[2]) {
|
||||
self.place_gateway(gateway)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// `spawnExitPortal(true)` after the dragon dies.
|
||||
pub(super) fn activate_exit_portal(&mut self) -> Result<()> {
|
||||
let origin = match self.exit_portal() {
|
||||
Some(o) => o,
|
||||
None => {
|
||||
// The rim's level: the top of the bedrock column minus three.
|
||||
let top = (0..256)
|
||||
.rev()
|
||||
.find(|&y| &*self.block_name([0, y, 0]) == "minecraft:bedrock")
|
||||
.unwrap_or(64);
|
||||
[0, top - 3, 0]
|
||||
}
|
||||
};
|
||||
self.place_podium(origin, true)
|
||||
}
|
||||
pub(super) fn place_gateway_at(&mut self, pos: [i32; 3]) -> Result<()> {
|
||||
self.place_gateway(pos)
|
||||
}
|
||||
|
||||
/// The first gateway of the shuffled ring (EnderDragonFight constructor
|
||||
/// and spawnNewGateway).
|
||||
@@ -564,7 +590,7 @@ impl Game<'_> {
|
||||
}
|
||||
|
||||
/// EndPodiumFeature(active).place at `origin` (the portal level).
|
||||
fn place_podium(&mut self, origin: [i32; 3]) -> Result<()> {
|
||||
fn place_podium(&mut self, origin: [i32; 3], active: bool) -> Result<()> {
|
||||
for y in origin[1] - 1..=origin[1] + 32 {
|
||||
for z in origin[2] - 4..=origin[2] + 4 {
|
||||
for x in origin[0] - 4..=origin[0] + 4 {
|
||||
@@ -585,8 +611,10 @@ impl Game<'_> {
|
||||
"minecraft:air"
|
||||
} else if !inside_rim {
|
||||
"minecraft:bedrock"
|
||||
} else {
|
||||
} else if active {
|
||||
"minecraft:end_portal"
|
||||
} else {
|
||||
"minecraft:air"
|
||||
};
|
||||
self.set_state_named([x, y, z], name, &[])?;
|
||||
}
|
||||
@@ -1065,8 +1093,7 @@ impl Game<'_> {
|
||||
self.eye_geometry(camera, out);
|
||||
}
|
||||
|
||||
/// EndCrystalRenderer and EndCrystalModel for the crystals EndSpikeFeature
|
||||
/// spawns on the spikes (render only; crystals are not entities yet).
|
||||
/// EndCrystalRenderer and EndCrystalModel for the end crystals.
|
||||
fn end_crystal_geometry(&self, camera: glam::DVec3, out: &mut Vec<Vertex>) {
|
||||
let Some(r) = self
|
||||
.res
|
||||
@@ -1077,26 +1104,21 @@ impl Game<'_> {
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let age = self.ticks as f32 + self.clock.fraction() as f32;
|
||||
let sin45 = std::f32::consts::FRAC_PI_4.sin();
|
||||
let axis = glam::Vec3::new(sin45, 0.0, sin45).normalize();
|
||||
let tilt = glam::Quat::from_axis_angle(axis, std::f32::consts::FRAC_PI_3);
|
||||
let spin = glam::Quat::from_rotation_y((age * 3.0).to_radians());
|
||||
// getY: the bob, in blocks.
|
||||
let hh = (age * 0.2).sin() / 2.0 + 0.5;
|
||||
let bob = (hh * hh + hh) * 0.4 - 1.4;
|
||||
// A 8×8×8 model box at texture offset (u, v) of the 64×32 texture.
|
||||
let uv = |u: f32, v: f32, w: f32, h: f32| {
|
||||
let su = |x: f32| r.u0 + (r.u1 - r.u0) * x / 64.0;
|
||||
let sv = |y: f32| r.v0 + (r.v1 - r.v0) * y / 32.0;
|
||||
(su(u), su(u + w), sv(v), sv(v + h))
|
||||
};
|
||||
for spike in horizon_world::worldgen::end_spike::spikes_for_seed(self.level.seed) {
|
||||
let feet = glam::DVec3::new(
|
||||
f64::from(spike.center_x) + 0.5,
|
||||
f64::from(spike.height + 1),
|
||||
f64::from(spike.center_z) + 0.5,
|
||||
);
|
||||
for crystal in &self.crystals {
|
||||
let feet = glam::DVec3::new(crystal.position.x, crystal.position.y, crystal.position.z);
|
||||
let age = crystal.time as f32 + self.clock.fraction() as f32;
|
||||
let spin = glam::Quat::from_rotation_y((age * 3.0).to_radians());
|
||||
let hh = (age * 0.2).sin() / 2.0 + 0.5;
|
||||
let bob = (hh * hh + hh) * 0.4 - 1.4;
|
||||
let rel = feet - camera;
|
||||
if rel.length_squared() > 256.0 * 256.0 {
|
||||
continue;
|
||||
|
||||
@@ -168,6 +168,7 @@ impl Game<'_> {
|
||||
if radius < 1.0e-5 {
|
||||
return;
|
||||
}
|
||||
self.explosion_hits_special(center, radius);
|
||||
let diameter = f64::from(radius * 2.0);
|
||||
let distance = |p: [f64; 3]| {
|
||||
((p[0] - center[0]).powi(2) + (p[1] - center[1]).powi(2) + (p[2] - center[2]).powi(2))
|
||||
|
||||
@@ -33,6 +33,10 @@ impl Game<'_> {
|
||||
.name
|
||||
.as_str()
|
||||
});
|
||||
// Minecraft.getSituationalMusic: the boss bar's music in the End.
|
||||
if self.boss_music() {
|
||||
return (Some(Music::end_boss()), 1.0);
|
||||
}
|
||||
let dimension = self.dim.id();
|
||||
let tables = &self.res.music;
|
||||
// Player.isUnderWater: eyes in water.
|
||||
|
||||
@@ -156,6 +156,20 @@ impl Game<'_> {
|
||||
self.carts.push(c);
|
||||
}
|
||||
}
|
||||
"minecraft:ender_dragon" => {
|
||||
let seed = self.world.random.next_long();
|
||||
if self.dragon.is_none()
|
||||
&& let Some(d) = super::dragon::Dragon::from_nbt(&tag, seed)
|
||||
{
|
||||
self.dragon = Some(d);
|
||||
}
|
||||
}
|
||||
"minecraft:end_crystal" => {
|
||||
let time = self.world.random.next_int_bound(100_000).unwrap_or(0);
|
||||
if let Some(c) = super::dragon::Crystal::from_nbt(&tag, time) {
|
||||
self.crystals.push(c);
|
||||
}
|
||||
}
|
||||
"minecraft:armor_stand" => {
|
||||
self.next_entity += 1;
|
||||
if let Some(a) = entity_nbt::armor_stand_from_nbt(&tag, self.next_entity) {
|
||||
@@ -326,6 +340,20 @@ impl Game<'_> {
|
||||
out.entry(chunk_of(b.position)).or_default().push(tag);
|
||||
}
|
||||
}
|
||||
if let Some(d) = &self.dragon
|
||||
&& keys.contains(&chunk_of(d.position))
|
||||
{
|
||||
out.entry(chunk_of(d.position))
|
||||
.or_default()
|
||||
.push(d.to_nbt(random_uuid(), None));
|
||||
}
|
||||
for c in &self.crystals {
|
||||
if keys.contains(&chunk_of(c.position)) {
|
||||
out.entry(chunk_of(c.position))
|
||||
.or_default()
|
||||
.push(c.to_nbt(random_uuid(), None));
|
||||
}
|
||||
}
|
||||
for c in &self.carts {
|
||||
if keys.contains(&chunk_of(c.position)) {
|
||||
let uuid = self
|
||||
@@ -402,6 +430,10 @@ impl Game<'_> {
|
||||
}
|
||||
keep
|
||||
});
|
||||
self.crystals.retain(|c| !gone(c.position));
|
||||
if self.dragon.as_ref().is_some_and(|d| gone(d.position)) {
|
||||
self.dragon = None;
|
||||
}
|
||||
self.carts.retain(|c| {
|
||||
let keep = !gone(c.position) || Some(c.id) == riding;
|
||||
if !keep {
|
||||
|
||||
@@ -240,8 +240,10 @@ impl PlayerMotion {
|
||||
.source::<()>(source, None, None, None)
|
||||
.ok_or("unknown resident damage source")?;
|
||||
let totem_ready = self.health.totem_ready;
|
||||
let invulnerable = self.invulnerable;
|
||||
let h = &mut self.health;
|
||||
let mut environment = Environment {
|
||||
invulnerable,
|
||||
rules,
|
||||
source,
|
||||
events: &mut self.events,
|
||||
@@ -293,6 +295,8 @@ impl PlayerMotion {
|
||||
}
|
||||
}
|
||||
pub(crate) struct Environment<'a> {
|
||||
/// Abilities.invulnerable.
|
||||
pub(crate) invulnerable: bool,
|
||||
pub(crate) rules: HealthRules,
|
||||
pub(crate) source: DamageSource<'a, ()>,
|
||||
pub(crate) events: &'a mut Vec<MotionEvent>,
|
||||
@@ -418,7 +422,7 @@ impl ServerDamageEnvironment<Attributes> for Environment<'_> {
|
||||
Ok(self.source.definition().exhaustion)
|
||||
}
|
||||
fn ability_invulnerable(&self) -> bool {
|
||||
false
|
||||
self.invulnerable
|
||||
}
|
||||
fn level_is_client(&mut self) -> io::Result<bool> {
|
||||
Ok(false)
|
||||
@@ -615,7 +619,7 @@ impl HurtWorld for Environment<'_> {
|
||||
}
|
||||
impl PlayerHurtWorld for Environment<'_> {
|
||||
fn ability_invulnerable(&self) -> bool {
|
||||
false
|
||||
self.invulnerable
|
||||
}
|
||||
fn bypasses_invulnerability(&mut self, _: &mut State) -> io::Result<bool> {
|
||||
Ok(self.source.in_tag("minecraft:bypasses_invulnerability"))
|
||||
|
||||
@@ -124,6 +124,7 @@ impl FoodWorld for TickWorld<'_> {
|
||||
self.events
|
||||
.push(MotionEvent::Health(HealthEvent::StarvationDamage(amount)));
|
||||
let mut environment = Environment {
|
||||
invulnerable: false,
|
||||
rules: self.rules,
|
||||
source: self
|
||||
.resources
|
||||
|
||||
@@ -140,6 +140,8 @@ pub struct PlayerMotion {
|
||||
pub jump_trigger_time: i32,
|
||||
/// Abilities.mayfly / flying (creative flight).
|
||||
pub may_fly: bool,
|
||||
/// Abilities.invulnerable (creative): only damage bypassing invulnerability hurts.
|
||||
pub invulnerable: bool,
|
||||
pub flying: bool,
|
||||
/// The jump key last tick (LocalPlayer.aiStep's wasJumping).
|
||||
pub was_jumping: bool,
|
||||
@@ -222,6 +224,7 @@ impl PlayerMotion {
|
||||
no_jump_delay: 0,
|
||||
jump_trigger_time: 0,
|
||||
may_fly: false,
|
||||
invulnerable: false,
|
||||
flying: false,
|
||||
was_jumping: false,
|
||||
glider: false,
|
||||
|
||||
Reference in New Issue
Block a user