Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ open = "5"
base64 = "0.22"
simdnbt = "0.10"
discord-rich-presence = "1.1"
md5 = "0.8.0"
oddio = "0.7.4"
azalea-buf = "0.16.0"

[profile.dev]
opt-level = 1
Expand Down
30 changes: 30 additions & 0 deletions src/core/id_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::fmt::Debug;

pub trait IdMap<T>: Debug {
const DEFAULT: i32 = -1;

fn get_id(&self, thing: &T) -> i32;

fn by_id(&self, id: i32) -> Option<&T>;

fn by_id_or_throw(&self, id: i32) -> &T {
match self.by_id(id) {
Some(result) => result,
None => panic!("No value with id {}", id),
}
}

fn get_id_or_throw(&self, value: &T) -> i32
where
T: Debug,
{
let id = self.get_id(value);
if id == Self::DEFAULT {
panic!("Can't find id for '{:?}' in map {:?}", value, self);
} else {
id
}
}

fn size(&self) -> usize;
}
2 changes: 2 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod id_map;
pub mod registry;
40 changes: 40 additions & 0 deletions src/core/registry/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fmt::Debug;

use azalea_registry::identifier::Identifier;

use crate::core::id_map::IdMap;
use crate::resources::resource_key::ResourceKey;

pub trait Registry<T>: IdMap<T> + Debug {
fn key(&self) -> ResourceKey<RegistryMarker<T>>;

fn get_key(&self, thing: &T) -> Option<Identifier>;

fn get_resource_key(&self, thing: &T) -> Option<ResourceKey<T>>;

fn get_value_by_resource_key(&self, key: &ResourceKey<T>) -> Option<&T>;

fn get_value_by_identifier(&self, key: &Identifier) -> Option<&T>;

fn contains_identifier(&self, key: &Identifier) -> bool {
self.get_value_by_identifier(key).is_some()
}

fn contains_resource_key(&self, key: &ResourceKey<T>) -> bool {
self.get_value_by_resource_key(key).is_some()
}

fn get_value_or_throw(&self, key: &ResourceKey<T>) -> &T {
match self.get_value_by_resource_key(key) {
Some(value) => value,
None => panic!("Missing key in {:?}: {}", self.key(), key),
}
}

fn key_set(&self) -> Vec<Identifier>;

fn registry_key_set(&self) -> Vec<ResourceKey<T>>;
}

#[derive(Debug, Clone, Copy, Default)]
pub struct RegistryMarker<T>(std::marker::PhantomData<fn() -> T>);
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod args;
mod assets;
mod benchmark;
mod core;
mod dirs;
mod discord;
mod entity;
Expand All @@ -10,7 +11,10 @@ mod physics;
mod player;
mod renderer;
mod resource_pack;
mod resources;
mod sound;
mod ui;
mod util;
mod window;
mod world;

Expand Down
10 changes: 10 additions & 0 deletions src/net/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ pub fn handle_game_packet(
tracing::info!("Server popping resource pack {:?}", p.id);
let _ = event_tx.try_send(NetworkEvent::ResourcePackPop { id: p.id });
}
ClientboundGamePacket::SoundEntity(sound) => {
let _ = event_tx.try_send(NetworkEvent::PlayEntitySound {
sound: sound.clone(),
});
}
ClientboundGamePacket::Sound(sound) => {
let _ = event_tx.try_send(NetworkEvent::PlaySound {
sound: sound.clone(),
});
}
ClientboundGamePacket::PlayerInfoUpdate(p) => {
use crate::player::tab_list::{PlayerInfoActions, PlayerInfoEntry};
let actions = PlayerInfoActions {
Expand Down
7 changes: 7 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use azalea_block::BlockState;
use azalea_core::heightmap_kind::HeightmapKind;
use azalea_core::position::{BlockPos, ChunkPos};
use azalea_inventory::ItemStack;
use azalea_protocol::packets::game::{ClientboundSound, ClientboundSoundEntity};
use azalea_registry::builtin::EntityKind;

pub enum NetworkEvent {
Expand Down Expand Up @@ -155,6 +156,12 @@ pub enum NetworkEvent {
Disconnected {
reason: String,
},
PlayEntitySound {
sound: ClientboundSoundEntity,
},
PlaySound {
sound: ClientboundSound,
},
PlayerInfoUpdate {
actions: crate::player::tab_list::PlayerInfoActions,
entries: Vec<crate::player::tab_list::PlayerInfoEntry>,
Expand Down
37 changes: 1 addition & 36 deletions src/renderer/chunk/mesher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use azalea_core::position::ChunkPos;

use super::greedy;
use crate::renderer::chunk::atlas::{AtlasRegion, AtlasUVMap};
use crate::util::rng::JavaRng;
use crate::world::block::model::{BakedModel, Direction};
use crate::world::block::registry::{BlockRegistry, FaceTextures, Tint};
use crate::world::chunk::{self, ChunkStore};
Expand Down Expand Up @@ -294,42 +295,6 @@ fn corner_noise(gi: usize, x: f64, y: f64, z: f64, falloff: f64) -> f64 {
}
}

struct JavaRng {
seed: i64,
}

impl JavaRng {
fn new(seed: i64) -> Self {
Self {
seed: (seed ^ 0x5DEECE66D) & ((1i64 << 48) - 1),
}
}

fn next(&mut self, bits: u32) -> i32 {
self.seed = (self.seed.wrapping_mul(0x5DEECE66D).wrapping_add(0xB)) & ((1i64 << 48) - 1);
(self.seed >> (48 - bits)) as i32
}

fn next_int(&mut self, bound: i32) -> i32 {
if bound & (bound - 1) == 0 {
return ((bound as i64 * self.next(31) as i64) >> 31) as i32;
}
loop {
let bits = self.next(31);
let val = bits % bound;
if bits - val + (bound - 1) >= 0 {
return val;
}
}
}

fn next_double(&mut self) -> f64 {
let hi = self.next(26) as i64;
let lo = self.next(27) as i64;
((hi << 27) + lo) as f64 / ((1i64 << 53) as f64)
}
}

pub fn int_to_rgb(color: i32) -> [f32; 3] {
let r = ((color >> 16) & 0xFF) as f32 / 255.0;
let g = ((color >> 8) & 0xFF) as f32 / 255.0;
Expand Down
30 changes: 2 additions & 28 deletions src/renderer/pipelines/sky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::renderer::MAX_FRAMES_IN_FLIGHT;
use crate::renderer::camera::Camera;
use crate::renderer::shader;
use crate::renderer::util;
use crate::util::rng::JavaRng;

const STAR_COUNT: u32 = 1500;
const SUN_SIZE: f32 = 30.0;
Expand Down Expand Up @@ -540,33 +541,6 @@ impl SkyPipeline {
}
}

struct JavaRandom {
seed: u64,
}

impl JavaRandom {
fn new(seed: i64) -> Self {
Self {
seed: (seed as u64 ^ 25214903917) & 0xFFFF_FFFF_FFFF,
}
}

fn next(&mut self, bits: u32) -> i32 {
self.seed = (self.seed.wrapping_mul(25214903917).wrapping_add(11)) & 0xFFFF_FFFF_FFFF;
(self.seed >> (48 - bits)) as i32
}

fn next_float(&mut self) -> f32 {
self.next(24) as f32 / (1u32 << 24) as f32
}

fn next_double(&mut self) -> f64 {
let hi = self.next(26) as i64;
let lo = self.next(27) as i64;
((hi << 27) + lo) as f64 / (1i64 << 53) as f64
}
}

/// CSS-style cubic Bezier easing with P0=(0,0), P3=(1,1). Solves Bx(s)=x via 4 Newton-Raphson
/// iterations (matches vanilla EasingType.CubicBezier) and returns By(s).
fn cubic_bezier_ease(x: f32, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
Expand Down Expand Up @@ -738,7 +712,7 @@ fn build_sky_disc(verts: &mut Vec<SkyVertex>, y: f32) {
}

fn build_stars(verts: &mut Vec<SkyVertex>) {
let mut rng = JavaRandom::new(10842);
let mut rng = JavaRng::new(10842);
let uv = [0.0, 0.0];

for _ in 0..STAR_COUNT {
Expand Down
29 changes: 29 additions & 0 deletions src/resources/file_to_id_converter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::resources::identifier::Identifier;

struct FileToIdConverter {
prefix: String,
extension: String,
}

impl FileToIdConverter {
pub fn new_json(prefix: String) -> Self {
FileToIdConverter {
prefix,
extension: "json".into(),
}
}

pub fn new(prefix: String, extension: String) -> Self {
FileToIdConverter { prefix, extension }
}

pub fn id_to_file(&self, id: Identifier) -> Option<Identifier> {
id.with_path(format!(
"{}/{}{}",
&self.prefix,
id.get_path(),
&self.extension
))
.ok()
}
}
Loading
Loading