Merge pull request #417 from ludovic111/feat/vault-game
Vaults in the game: ticking, keys, rewards and the display item
This commit is contained in:
@@ -70,6 +70,8 @@ pub enum BlockEntity {
|
||||
Spawner(Box<crate::mobs::spawner::Spawner>),
|
||||
/// `TrialSpawnerBlockEntity` (`crate::mobs::trial_spawner`).
|
||||
TrialSpawner(Box<crate::mobs::trial_spawner::TrialSpawner>),
|
||||
/// `VaultBlockEntity` (`crate::vault`).
|
||||
Vault(Box<crate::vault::Vault>),
|
||||
/// `BeehiveBlockEntity` of beehives and bee nests (`crate::beehive`).
|
||||
Beehive(Box<crate::beehive::Beehive>),
|
||||
}
|
||||
@@ -192,6 +194,9 @@ impl BlockEntity {
|
||||
if name == "minecraft:trial_spawner" {
|
||||
return Some(Self::TrialSpawner(Box::default()));
|
||||
}
|
||||
if name == "minecraft:vault" {
|
||||
return Some(Self::Vault(Box::default()));
|
||||
}
|
||||
if name == "minecraft:beehive" || name == "minecraft:bee_nest" {
|
||||
return Some(Self::Beehive(Box::default()));
|
||||
}
|
||||
@@ -272,6 +277,7 @@ impl BlockEntity {
|
||||
Self::CreakingHeart(_) => "minecraft:creaking_heart",
|
||||
Self::Spawner(_) => "minecraft:mob_spawner",
|
||||
Self::TrialSpawner(_) => "minecraft:trial_spawner",
|
||||
Self::Vault(_) => "minecraft:vault",
|
||||
Self::Beehive(_) => "minecraft:beehive",
|
||||
}
|
||||
}
|
||||
@@ -286,6 +292,7 @@ impl BlockEntity {
|
||||
| Self::CreakingHeart(_)
|
||||
| Self::Spawner(_)
|
||||
| Self::TrialSpawner(_)
|
||||
| Self::Vault(_)
|
||||
| Self::Beehive(_) => Vec::new(),
|
||||
Self::Lectern(l) => l.book.iter().cloned().collect(),
|
||||
Self::Sculk(_) => Vec::new(),
|
||||
@@ -308,6 +315,7 @@ impl BlockEntity {
|
||||
Self::CreakingHeart(_)
|
||||
| Self::Spawner(_)
|
||||
| Self::TrialSpawner(_)
|
||||
| Self::Vault(_)
|
||||
| Self::Beehive(_) => &[],
|
||||
}
|
||||
}
|
||||
@@ -326,6 +334,7 @@ impl BlockEntity {
|
||||
Self::CreakingHeart(_)
|
||||
| Self::Spawner(_)
|
||||
| Self::TrialSpawner(_)
|
||||
| Self::Vault(_)
|
||||
| Self::Beehive(_) => &mut [],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,6 +125,7 @@ mod trial_spawner;
|
||||
mod trident;
|
||||
mod tweaks;
|
||||
mod ui_control;
|
||||
mod vault;
|
||||
mod villagers;
|
||||
mod wandering_trader;
|
||||
mod wind;
|
||||
@@ -2360,6 +2361,10 @@ impl Game<'_> {
|
||||
self.swing_arm();
|
||||
return Ok(true);
|
||||
}
|
||||
if self.use_vault(hit.pos, held.as_ref())? {
|
||||
self.swing_arm();
|
||||
return Ok(true);
|
||||
}
|
||||
match self.use_respawn_anchor(hit.pos, held.as_ref())? {
|
||||
respawn_anchor::AnchorUse::Charged => {
|
||||
self.swing_arm();
|
||||
@@ -3889,6 +3894,7 @@ impl Game<'_> {
|
||||
| BlockEntity::CreakingHeart(_)
|
||||
| BlockEntity::Spawner(_)
|
||||
| BlockEntity::TrialSpawner(_)
|
||||
| BlockEntity::Vault(_)
|
||||
| BlockEntity::Beehive(_),
|
||||
) => {}
|
||||
None => {
|
||||
@@ -4144,6 +4150,7 @@ impl Game<'_> {
|
||||
| BlockEntity::CreakingHeart(_)
|
||||
| BlockEntity::Spawner(_)
|
||||
| BlockEntity::TrialSpawner(_)
|
||||
| BlockEntity::Vault(_)
|
||||
| BlockEntity::Beehive(_),
|
||||
) => {}
|
||||
None if self.mount_active() => self.draw_mount_background(batch, ox, oy),
|
||||
@@ -5284,6 +5291,8 @@ impl Game<'_> {
|
||||
}
|
||||
// `ItemEntityRenderer`.
|
||||
self.dropped_item_geometry(camera, alpha, &mut out);
|
||||
// `VaultRenderer`.
|
||||
self.vault_geometry(camera, alpha, &mut out);
|
||||
// Mobs and their projectiles.
|
||||
self.mob_geometry(camera, alpha, &mut out);
|
||||
self.player_model_geometry(camera, alpha, &mut out);
|
||||
@@ -5801,6 +5810,7 @@ impl Game<'_> {
|
||||
self.tick_creaking_hearts()?;
|
||||
self.tick_spawners()?;
|
||||
self.tick_trial_spawners()?;
|
||||
self.tick_vaults()?;
|
||||
self.tick_beehives();
|
||||
let mut lit_changes = Vec::new();
|
||||
for (pos, entity) in self.world.block_entities.iter_mut() {
|
||||
|
||||
@@ -184,7 +184,7 @@ impl Game<'_> {
|
||||
}
|
||||
for event in events {
|
||||
match event {
|
||||
TrialEvent::BlockUpdated => self.trial_spawner_updated(key),
|
||||
TrialEvent::BlockUpdated => self.mark_block_entity_updated(key),
|
||||
TrialEvent::LevelEvent(id, at, data) => self.level_event_at(id, at, data),
|
||||
TrialEvent::Sound(name) => {
|
||||
self.audio
|
||||
@@ -220,8 +220,8 @@ impl Game<'_> {
|
||||
let id = self.res.assets.registry.resolve(&block)?;
|
||||
self.level_set([key.0, key.1, key.2], id)
|
||||
}
|
||||
/// `markUpdated`: the clients load the new update tag.
|
||||
fn trial_spawner_updated(&mut self, key: Pos) {
|
||||
/// `sendBlockUpdated` of a block entity: the clients load its new update tag.
|
||||
pub(super) fn mark_block_entity_updated(&mut self, key: Pos) {
|
||||
if let Some(log) = self.world.block_log.as_mut() {
|
||||
log.push(key);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
//! Vaults in the game: `VaultBlockEntity.Server.tick` for every vault
|
||||
//! (`vault`), the block's state as it changes, the rewards it ejects, the
|
||||
//! keys players insert (`VaultBlock.useItemOn`) and the display item spinning
|
||||
//! inside (`VaultRenderer`).
|
||||
use super::Game;
|
||||
use horizon_assets::item_model::DisplayContext;
|
||||
use horizon_client::{
|
||||
Result,
|
||||
block_entity_render::vertex_light,
|
||||
containers::BlockEntity,
|
||||
item_render,
|
||||
items::ItemStack,
|
||||
mesher::Vertex,
|
||||
vault::{Vault, VaultEvent, VaultLevel, VaultPlayer, VaultState},
|
||||
};
|
||||
use horizon_core::random::LegacyRandom;
|
||||
|
||||
type Pos = (i32, i32, i32);
|
||||
|
||||
/// What a vault reads: the game time and the player.
|
||||
struct Players {
|
||||
time: i64,
|
||||
players: Vec<VaultPlayer>,
|
||||
}
|
||||
impl VaultLevel for Players {
|
||||
fn game_time(&self) -> i64 {
|
||||
self.time
|
||||
}
|
||||
fn players(&self) -> Vec<VaultPlayer> {
|
||||
self.players.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Game<'_> {
|
||||
fn vault_players(&self) -> Players {
|
||||
let players = self.player.as_ref().map(|p| VaultPlayer {
|
||||
uuid: self.level.player_uuid.unwrap_or_default(),
|
||||
block: [p.position.x, p.position.y, p.position.z].map(|v| v.floor() as i32),
|
||||
spectator: self.spectator(),
|
||||
});
|
||||
Players {
|
||||
time: self.world.game_time,
|
||||
players: players.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
/// The vault at `key` with its block's state and ominous flag.
|
||||
fn take_vault(&mut self, key: Pos) -> Option<(Box<Vault>, VaultState, bool)> {
|
||||
let state = self.world.block(key.0, key.1, key.2);
|
||||
let block = self.res.assets.registry.state(state)?;
|
||||
if block.name != "minecraft:vault" {
|
||||
return None;
|
||||
}
|
||||
let current = block
|
||||
.properties
|
||||
.get("vault_state")
|
||||
.and_then(|s| VaultState::from_name(s))
|
||||
.unwrap_or_default();
|
||||
let ominous = block.properties.get("ominous").is_some_and(|v| v == "true");
|
||||
let Some(BlockEntity::Vault(vault)) = self.world.block_entities.remove(&key) else {
|
||||
return None;
|
||||
};
|
||||
Some((vault, current, ominous))
|
||||
}
|
||||
/// Runs `f` on the vault at `key` with the level random and a loot roll,
|
||||
/// then applies its events.
|
||||
fn with_vault<T>(
|
||||
&mut self,
|
||||
key: Pos,
|
||||
f: impl FnOnce(
|
||||
&mut Vault,
|
||||
&Players,
|
||||
&mut LegacyRandom,
|
||||
VaultState,
|
||||
bool,
|
||||
&mut horizon_client::vault::Roll<LegacyRandom>,
|
||||
&mut Vec<VaultEvent>,
|
||||
) -> T,
|
||||
) -> Option<Result<T>> {
|
||||
let (mut vault, current, ominous) = self.take_vault(key)?;
|
||||
let players = self.vault_players();
|
||||
let mut random = std::mem::replace(&mut self.world.random, LegacyRandom::new(0));
|
||||
let mut sequences = std::mem::take(&mut self.rand_sequences);
|
||||
let loot = horizon_client::structure_blocks::loot_data(&self.options.resources);
|
||||
let seed = self.level.seed;
|
||||
let mut events = Vec::new();
|
||||
let out = {
|
||||
// LootTable.getRandomItems, stacks split; the reward rolls with the
|
||||
// table's random sequence.
|
||||
let mut roll = |table: &str, random: &mut LegacyRandom, sequence: bool| {
|
||||
let Some(loot) = &loot else {
|
||||
return Vec::new();
|
||||
};
|
||||
let drops = match loot.random_sequence(table).filter(|_| sequence) {
|
||||
Some(s) => loot.roll_split(table, sequences.get(s, seed)),
|
||||
None => loot.roll_split(table, random),
|
||||
};
|
||||
drops
|
||||
.iter()
|
||||
.map(horizon_client::structure_blocks::loot_item_with_components)
|
||||
.collect()
|
||||
};
|
||||
f(
|
||||
&mut vault,
|
||||
&players,
|
||||
&mut random,
|
||||
current,
|
||||
ominous,
|
||||
&mut roll,
|
||||
&mut events,
|
||||
)
|
||||
};
|
||||
self.world.random = random;
|
||||
self.rand_sequences = sequences;
|
||||
self.world
|
||||
.block_entities
|
||||
.insert(key, BlockEntity::Vault(vault));
|
||||
Some(self.vault_events(key, events).map(|()| out))
|
||||
}
|
||||
/// Every vault's server tick, then its client tick's spin.
|
||||
pub(super) fn tick_vaults(&mut self) -> Result<()> {
|
||||
let keys: Vec<Pos> = self
|
||||
.world
|
||||
.block_entities
|
||||
.iter()
|
||||
.filter(|(_, e)| matches!(e, BlockEntity::Vault(_)))
|
||||
.map(|(k, _)| *k)
|
||||
.collect();
|
||||
for key in keys {
|
||||
let pos = [key.0, key.1, key.2];
|
||||
let ticked = self.with_vault(key, |v, l, random, state, ominous, roll, events| {
|
||||
v.server_tick(l, random, pos, state, ominous, roll, events);
|
||||
v.update_display_item_spin();
|
||||
});
|
||||
if let Some(result) = ticked {
|
||||
result?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// `VaultBlock.useItemOn` with `held` in the hand that acts: true when the
|
||||
/// vault took the click (an active vault, with anything in the hand).
|
||||
pub(super) fn use_vault(&mut self, pos: [i32; 3], held: Option<&ItemStack>) -> Result<bool> {
|
||||
let Some(stack) = held.cloned() else {
|
||||
return Ok(false);
|
||||
};
|
||||
let player = self.level.player_uuid.unwrap_or_default();
|
||||
let used = self.with_vault(
|
||||
(pos[0], pos[1], pos[2]),
|
||||
|v, l, random, state, ominous, roll, events| {
|
||||
v.use_item_on(
|
||||
l,
|
||||
random,
|
||||
pos,
|
||||
state,
|
||||
ominous,
|
||||
player,
|
||||
Some(&stack),
|
||||
roll,
|
||||
events,
|
||||
)
|
||||
},
|
||||
);
|
||||
let Some(taken) = used.transpose()?.flatten() else {
|
||||
return Ok(false);
|
||||
};
|
||||
if taken > 0 {
|
||||
// awardStat(ITEM_USED), then stackToInsert.consume(count, player).
|
||||
self.stats.add("used", &stack.item, 1);
|
||||
for _ in 0..taken {
|
||||
self.consume_held();
|
||||
}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
fn vault_events(&mut self, key: Pos, events: Vec<VaultEvent>) -> Result<()> {
|
||||
let pos = [key.0, key.1, key.2];
|
||||
let center = pos.map(|v| f64::from(v) + 0.5);
|
||||
for event in events {
|
||||
match event {
|
||||
VaultEvent::State(state) => self.set_vault_state(key, state)?,
|
||||
VaultEvent::BlockUpdated => self.mark_block_entity_updated(key),
|
||||
VaultEvent::LevelEvent(id, data) => self.level_event_at(id, pos, data),
|
||||
VaultEvent::Sound(name, volume, pitch) => {
|
||||
self.audio
|
||||
.play(&format!("minecraft:{name}"), Some(center), volume, pitch)
|
||||
}
|
||||
// Vec3.atBottomCenterOf(pos).relative(UP, 1.2), lowered by spawnItem.
|
||||
VaultEvent::Eject(stack, v) => {
|
||||
self.spawn_item(stack, [center[0], center[1] + 0.7 - 0.125, center[2]], v, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// `setBlockAndUpdate` with the new `vault_state`.
|
||||
fn set_vault_state(&mut self, key: Pos, state: VaultState) -> Result<()> {
|
||||
let current = self.world.block(key.0, key.1, key.2);
|
||||
let Some(mut block) = self.res.assets.registry.state(current).cloned() else {
|
||||
return Ok(());
|
||||
};
|
||||
block
|
||||
.properties
|
||||
.insert("vault_state".into(), state.name().into());
|
||||
let id = self.res.assets.registry.resolve(&block)?;
|
||||
self.level_set([key.0, key.1, key.2], id)
|
||||
}
|
||||
/// `VaultRenderer`: the display item of every vault near the camera that
|
||||
/// shows one, turning above its floor.
|
||||
pub(super) fn vault_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec<Vertex>) {
|
||||
for (key, entity) in &self.world.block_entities {
|
||||
let BlockEntity::Vault(vault) = entity else {
|
||||
continue;
|
||||
};
|
||||
let Some(stack) = &vault.display_item else {
|
||||
continue;
|
||||
};
|
||||
let at = glam::DVec3::new(f64::from(key.0), f64::from(key.1), f64::from(key.2));
|
||||
if at.distance_squared(camera) >= 64.0 * 64.0 {
|
||||
continue;
|
||||
}
|
||||
let state = self.resolve_item(stack, DisplayContext::Ground, None, 0);
|
||||
if state.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let pose =
|
||||
glam::Mat4::from_translation(
|
||||
(at - camera).as_vec3() + glam::Vec3::new(0.5, 0.4, 0.5),
|
||||
) * glam::Mat4::from_rotation_y(vault.display_spin(alpha as f32).to_radians());
|
||||
let light = self.world.light(key.0, key.1, key.2);
|
||||
let seed =
|
||||
i64::from(self.res.item_renderer.item_id(&stack.item)) + i64::from(stack.damage);
|
||||
for copy in item_render::cluster_poses(
|
||||
&state,
|
||||
pose,
|
||||
item_render::rendered_amount(stack.count),
|
||||
seed,
|
||||
) {
|
||||
self.submit_item_world(&state, copy, vertex_light(light), out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -528,6 +528,7 @@ pub fn encode_block_entity(
|
||||
}
|
||||
BlockEntity::Spawner(s) => entries.extend(s.save()),
|
||||
BlockEntity::TrialSpawner(s) => entries.extend(s.save()),
|
||||
BlockEntity::Vault(v) => entries.extend(v.save()),
|
||||
BlockEntity::Beehive(h) => entries.extend(h.to_nbt()),
|
||||
}
|
||||
Tag::compound(entries)
|
||||
@@ -658,6 +659,7 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
|
||||
| BlockEntity::CreakingHeart(_) => {}
|
||||
BlockEntity::Spawner(s) => **s = crate::mobs::spawner::Spawner::load(tag),
|
||||
BlockEntity::TrialSpawner(s) => **s = crate::mobs::trial_spawner::TrialSpawner::load(tag),
|
||||
BlockEntity::Vault(v) => **v = crate::vault::Vault::load(tag),
|
||||
BlockEntity::Beehive(h) => **h = crate::beehive::Beehive::from_nbt(tag),
|
||||
}
|
||||
Some((pos, entity))
|
||||
|
||||
@@ -147,6 +147,9 @@ pub fn block_entities(
|
||||
if let BlockEntity::TrialSpawner(s) = &mut entity {
|
||||
**s = crate::mobs::trial_spawner::TrialSpawner::load(&Tag::Compound(nbt.clone()));
|
||||
}
|
||||
if let BlockEntity::Vault(v) = &mut entity {
|
||||
**v = crate::vault::Vault::load(&Tag::Compound(nbt.clone()));
|
||||
}
|
||||
if let (BlockEntity::Chest(slots), Some(table), Some(loot)) =
|
||||
(&mut entity, text(nbt, "LootTable"), loot)
|
||||
{
|
||||
|
||||
@@ -186,6 +186,9 @@ pub struct Vault {
|
||||
pub connected_particles_range: f64,
|
||||
/// The shared data changed since the last tick sent it.
|
||||
pub shared_dirty: bool,
|
||||
/// `VaultClientData`: the display item's spin and the previous tick's, in degrees.
|
||||
pub spin: f32,
|
||||
pub o_spin: f32,
|
||||
}
|
||||
|
||||
impl Default for Vault {
|
||||
@@ -201,6 +204,8 @@ impl Default for Vault {
|
||||
connected_players: Vec::new(),
|
||||
connected_particles_range: DEFAULT_DEACTIVATION_RANGE,
|
||||
shared_dirty: false,
|
||||
spin: 0.0,
|
||||
o_spin: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,7 +261,28 @@ fn same_set(a: &[Uuid], b: &[Uuid]) -> bool {
|
||||
a.len() == b.len() && a.iter().all(|u| b.contains(u))
|
||||
}
|
||||
|
||||
/// `Mth.wrapDegrees`.
|
||||
fn wrap_degrees(degrees: f32) -> f32 {
|
||||
let t = degrees % 360.0;
|
||||
if t >= 180.0 {
|
||||
t - 360.0
|
||||
} else if t < -180.0 {
|
||||
t + 360.0
|
||||
} else {
|
||||
t
|
||||
}
|
||||
}
|
||||
|
||||
impl Vault {
|
||||
/// `VaultClientData.updateDisplayItemSpin`, every client tick.
|
||||
pub fn update_display_item_spin(&mut self) {
|
||||
self.o_spin = self.spin;
|
||||
self.spin = wrap_degrees(self.spin + 10.0);
|
||||
}
|
||||
/// `VaultRenderer`'s spin: `Mth.rotLerp` between the last two ticks.
|
||||
pub fn display_spin(&self, partial: f32) -> f32 {
|
||||
self.o_spin + partial * wrap_degrees(self.spin - self.o_spin)
|
||||
}
|
||||
/// `loadAdditional`. `VaultServerData.set` leaves the total of ejections
|
||||
/// out, so a vault loaded mid-ejection counts from 0.
|
||||
pub fn load(tag: &Tag) -> Self {
|
||||
|
||||
@@ -67,7 +67,8 @@ IGNORED = {
|
||||
# chunk generation first), so counters and positions of live things move on.
|
||||
r'^chunk\.block_entities\.\(2, -60, 5\)\.(cooking_time_spent|lit_time_remaining|Items\[\]\.count)$':
|
||||
'the lit furnace keeps smelting',
|
||||
r'^chunk\.block_entities\..*\.(ticks_since_song_started|Age|BrewTime|warning_level|ticks_in_hive|last_vibration_frequency|listener\..*|RecipesUsed\..*)$':
|
||||
r'^chunk\.block_entities\..*\.(ticks_since_song_started|Age|BrewTime|warning_level|ticks_in_hive|last_vibration_frequency|listener\..*|RecipesUsed\..*|'
|
||||
r'server_data\.state_updating_resumes_at)$':
|
||||
'block entity timers advance',
|
||||
r'^entity\.(Age|Motion\[\]|Rotation\[\]|Pos\[\]|OnGround|HurtTime|Health|TimeInOverworld|fall_distance|'
|
||||
r'wasOnGround|Fire|Air|DeathTime|Fuse|life|inGround|ticks_in_ground|Time)$': 'entity simulation advances',
|
||||
|
||||
Reference in New Issue
Block a user