terra_plr/
lib.rs

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