-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement experience and experiene/xp commands (#521)
* Initial experience support For some reason I am getting link errors * Implement experience math * Fix packet and format/clippy * Fix translate strings * Fix experience handling * Clippy & Format * Fix experience calculations * Fix how we store experience. Math WIP * Redo all experience calculations This should be 100% vanilla parity.
- Loading branch information
Showing
8 changed files
with
515 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use pumpkin_data::packet::clientbound::PLAY_SET_EXPERIENCE; | ||
use pumpkin_macros::client_packet; | ||
use serde::Serialize; | ||
|
||
use crate::VarInt; | ||
|
||
#[derive(Serialize)] | ||
#[client_packet(PLAY_SET_EXPERIENCE)] | ||
pub struct CSetExperience { | ||
progress: f32, | ||
level: VarInt, | ||
total_experience: VarInt, | ||
} | ||
|
||
impl CSetExperience { | ||
pub fn new(progress: f32, level: VarInt, total_experience: VarInt) -> Self { | ||
Self { | ||
progress, | ||
level, | ||
total_experience, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// Get the number of points in a level | ||
pub fn points_in_level(level: i32) -> i32 { | ||
match level { | ||
0..=15 => 2 * level + 7, | ||
16..=30 => 5 * level - 38, | ||
_ => 9 * level - 158, | ||
} | ||
} | ||
|
||
/// Calculate the total number of points to reach a level | ||
pub fn points_to_level(level: i32) -> i32 { | ||
match level { | ||
0..=15 => (level * level + 6 * level) / 2, | ||
16..=30 => ((2.5 * f64::from(level * level)) - (40.5 * f64::from(level)) + 360.0) as i32, | ||
_ => ((4.5 * f64::from(level * level)) - (162.5 * f64::from(level)) + 2220.0) as i32, | ||
} | ||
} | ||
|
||
/// Calculate level and points from total points | ||
pub fn total_to_level_and_points(total_points: i32) -> (i32, i32) { | ||
let level = match total_points { | ||
0..=352 => ((total_points as f64 + 9.0).sqrt() - 3.0) as i32, | ||
353..=1507 => (81.0 + (total_points as f64 - 7839.0) / 40.0).sqrt() as i32, | ||
_ => (325.0 + (total_points as f64 - 54215.0) / 72.0).sqrt() as i32, | ||
}; | ||
|
||
let level_start = points_to_level(level); | ||
let points_into_level = total_points - level_start; | ||
|
||
(level, points_into_level) | ||
} | ||
|
||
/// Calculate progress (0.0 to 1.0) from points within a level | ||
pub fn progress_in_level(points: i32, level: i32) -> f32 { | ||
let max_points = points_in_level(level); | ||
#[allow(clippy::cast_precision_loss)] | ||
let progress = (points as f32) / (max_points as f32); | ||
progress.clamp(0.0, 1.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.