Skip to main content

terra_plr/
lib.rs

1mod player;
2
3pub use player::{
4	buff::{Buff, BuffEffect},
5	default,
6	error::{Error, Result},
7	inventory,
8	looks::{HairDye, Style},
9	read, write, Difficulty, Player, SpawnPoint, Visibility, SUPPORTED_VERSIONS,
10};
11use std::ops::RangeInclusive;
12pub use terra_types::{Color, InTiles, NonNegativeI32, PositiveI32, Vec2};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum TerrariaFileType {
16	Unknown,
17	Map,
18	World,
19	Player,
20}
21
22impl TerrariaFileType {
23	pub fn name(self) -> &'static str {
24		match self {
25			Self::Map => "map",
26			Self::Player => "player",
27			Self::World => "world",
28			Self::Unknown => "unknown",
29		}
30	}
31}
32
33impl TerrariaFileType {
34	pub fn to_id(self) -> Option<u8> {
35		Some(match self {
36			Self::Map => 1,
37			Self::World => 2,
38			Self::Player => 3,
39			Self::Unknown => return None,
40		})
41	}
42
43	pub fn from_id(id: u8) -> Self {
44		match id {
45			1 => Self::Map,
46			2 => Self::World,
47			3 => Self::Player,
48			_ => Self::Unknown,
49		}
50	}
51}
52
53fn round_division_i64(dividend: i64, divisor: i64) -> i64 {
54	let double_remainder = (dividend % divisor) * 2;
55	dividend / divisor
56		+ if double_remainder.abs() >= divisor.abs() {
57			if dividend.is_negative() == divisor.is_negative() {
58				1
59			} else {
60				-1
61			}
62		} else {
63			0
64		}
65}
66
67fn round_division_i128(dividend: i128, divisor: i128) -> i128 {
68	let double_remainder = (dividend % divisor) * 2;
69	dividend / divisor
70		+ if double_remainder.abs() >= divisor.abs() {
71			if dividend.is_negative() == divisor.is_negative() {
72				1
73			} else {
74				-1
75			}
76		} else {
77			0
78		}
79}
80
81fn remap(v: f32, x: RangeInclusive<f32>, y: RangeInclusive<f32>) -> f32 {
82	if v <= *x.start() {
83		return *y.start();
84	}
85
86	if v >= *x.end() {
87		return *y.end();
88	}
89
90	let a = (*y.end() - *y.start()) / (*x.end() - *x.start());
91	v * a + *y.start() - a * *x.start()
92}