terra_types/
lib.rs

1use bounded_integer::{BoundedI16, BoundedI32, BoundedI8};
2use std::{
3	marker::PhantomData,
4	ops::{Deref, DerefMut},
5};
6use time::Duration;
7
8pub type NonNegativeI32 = BoundedI32<0, { i32::MAX }>;
9pub type PositiveI32 = BoundedI32<1, { i32::MAX }>;
10pub type PositiveI16 = BoundedI16<1, { i16::MAX }>;
11pub type NonNegativeI16 = BoundedI16<0, { i16::MAX }>;
12pub type NonNegativeI8 = BoundedI8<0, { i8::MAX }>;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct Color {
16	pub red: u8,
17	pub green: u8,
18	pub blue: u8,
19}
20
21impl Color {
22	pub fn new((red, green, blue): (u8, u8, u8)) -> Self {
23		Self { red, green, blue }
24	}
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum TerrariaFileType {
29	Unknown,
30	Map,
31	World,
32	Player,
33}
34
35impl TerrariaFileType {
36	pub fn name(self) -> &'static str {
37		match self {
38			Self::Map => "map",
39			Self::Player => "player",
40			Self::World => "world",
41			Self::Unknown => "unknown",
42		}
43	}
44}
45
46impl TerrariaFileType {
47	pub fn to_id(self) -> Option<u8> {
48		Some(match self {
49			Self::Map => 1,
50			Self::World => 2,
51			Self::Player => 3,
52			Self::Unknown => return None,
53		})
54	}
55
56	pub fn from_id(id: u8) -> Self {
57		match id {
58			1 => Self::Map,
59			2 => Self::World,
60			3 => Self::Player,
61			_ => Self::Unknown,
62		}
63	}
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
67pub enum MoonPhase {
68	#[default]
69	FullMoon = 0,
70	WaningGibbous = 1,
71	ThirdQuarter = 2,
72	WaningCrescent = 3,
73	NewMoon = 4,
74	WaxingCrescent = 5,
75	FirstQuarter = 6,
76	WaxingGibbous = 7,
77}
78
79impl MoonPhase {
80	pub fn from_id(id: u8) -> Option<Self> {
81		Some(match id {
82			0 => Self::FullMoon,
83			1 => Self::WaningGibbous,
84			2 => Self::ThirdQuarter,
85			3 => Self::WaningCrescent,
86			4 => Self::NewMoon,
87			5 => Self::WaxingCrescent,
88			6 => Self::FirstQuarter,
89			7 => Self::WaxingGibbous,
90			_ => return None,
91		})
92	}
93
94	pub fn to_id(self) -> u8 {
95		self as u8
96	}
97
98	pub fn next(self) -> Self {
99		match self {
100			Self::FullMoon => Self::WaningGibbous,
101			Self::WaningGibbous => Self::ThirdQuarter,
102			Self::ThirdQuarter => Self::WaningCrescent,
103			Self::WaningCrescent => Self::NewMoon,
104			Self::NewMoon => Self::WaxingCrescent,
105			Self::WaxingCrescent => Self::FirstQuarter,
106			Self::FirstQuarter => Self::WaxingGibbous,
107			Self::WaxingGibbous => Self::FullMoon,
108		}
109	}
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
113pub enum Paint {
114	RedPaint = 1,
115	OrangePaint = 2,
116	YellowPaint = 3,
117	LimePaint = 4,
118	GreenPaint = 5,
119	TealPaint = 6,
120	CyanPaint = 7,
121	SkyBluePaint = 8,
122	BluePaint = 9,
123	PurplePaint = 10,
124	VioletPaint = 11,
125	PinkPaint = 12,
126	DeepRedPaint = 13,
127	DeepOrangePaint = 14,
128	DeepYellowPaint = 15,
129	DeepLimePaint = 16,
130	DeepGreenPaint = 17,
131	DeepTealPaint = 18,
132	DeepCyanPaint = 19,
133	DeepSkyBluePaint = 20,
134	DeepBluePaint = 21,
135	DeepPurplePaint = 22,
136	DeepVioletPaint = 23,
137	DeepPinkPaint = 24,
138	BlackPaint = 25,
139	WhitePaint = 26,
140	GrayPaint = 27,
141	BrownPaint = 28,
142	ShadowPaint = 29,
143	NegativePaint = 30,
144}
145
146impl Paint {
147	pub fn to_id(self) -> u8 {
148		self as u8
149	}
150
151	pub fn from_id(id: u8) -> Option<Self> {
152		Some(match id {
153			1 => Self::RedPaint,
154			2 => Self::OrangePaint,
155			3 => Self::YellowPaint,
156			4 => Self::LimePaint,
157			5 => Self::GreenPaint,
158			6 => Self::TealPaint,
159			7 => Self::CyanPaint,
160			8 => Self::SkyBluePaint,
161			9 => Self::BluePaint,
162			10 => Self::PurplePaint,
163			11 => Self::VioletPaint,
164			12 => Self::PinkPaint,
165			13 => Self::DeepRedPaint,
166			14 => Self::DeepOrangePaint,
167			15 => Self::DeepYellowPaint,
168			16 => Self::DeepLimePaint,
169			17 => Self::DeepGreenPaint,
170			18 => Self::DeepTealPaint,
171			19 => Self::DeepCyanPaint,
172			20 => Self::DeepSkyBluePaint,
173			21 => Self::DeepBluePaint,
174			22 => Self::DeepPurplePaint,
175			23 => Self::DeepVioletPaint,
176			24 => Self::DeepPinkPaint,
177			25 => Self::BlackPaint,
178			26 => Self::WhitePaint,
179			27 => Self::GrayPaint,
180			28 => Self::BrownPaint,
181			29 => Self::ShadowPaint,
182			30 => Self::NegativePaint,
183			_ => return None,
184		})
185	}
186}
187
188#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
189pub enum Evil {
190	Corruption = 0,
191	Crimson = 1,
192}
193
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
195pub enum TileShape {
196	#[default]
197	Full,
198	Half,
199	SlopeDownRight,
200	SlopeDownLeft,
201	SlopeUpRight,
202	SlopeUpLeft,
203}
204
205#[derive(Debug, Clone, Copy, PartialEq)]
206pub struct Rain {
207	pub time: Duration,
208	pub max: f32,
209}
210
211pub trait Unit {}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
214pub struct InTiles;
215impl Unit for InTiles {}
216
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
218pub struct InPixels;
219impl Unit for InPixels {}
220
221#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
222pub struct InMph;
223
224impl Unit for InMph {}
225
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
227pub struct InDays;
228impl Unit for InDays {}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
231pub struct Vec2<U: Unit, T> {
232	pub x: T,
233	pub y: T,
234	_unit: PhantomData<U>,
235}
236
237impl<U: Unit, T> Vec2<U, T> {
238	pub fn new(x: T, y: T) -> Self {
239		Vec2 {
240			x,
241			y,
242			_unit: PhantomData,
243		}
244	}
245}
246
247#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
248pub struct Value<U: Unit, T> {
249	value: T,
250	_unit: PhantomData<U>,
251}
252
253impl<U: Unit, T> Value<U, T> {
254	pub fn new(v: T) -> Self {
255		Self {
256			value: v,
257			_unit: PhantomData,
258		}
259	}
260
261	pub fn value(self) -> T {
262		self.value
263	}
264}
265
266impl<U: Unit, T> From<T> for Value<U, T> {
267	fn from(value: T) -> Self {
268		Self {
269			value,
270			_unit: PhantomData,
271		}
272	}
273}
274
275impl<U: Unit, T> Deref for Value<U, T> {
276	type Target = T;
277	fn deref(&self) -> &Self::Target {
278		&self.value
279	}
280}
281
282impl<U: Unit, T> DerefMut for Value<U, T> {
283	fn deref_mut(&mut self) -> &mut Self::Target {
284		&mut self.value
285	}
286}
287
288pub type YAxis<U, T> = Value<U, T>;
289pub type XAxis<U, T> = Value<U, T>;