terra_types/
lib.rs

1use std::{
2	marker::PhantomData,
3	ops::{Deref, DerefMut},
4};
5
6use bounded_integer::{BoundedI16, BoundedI32, BoundedI8};
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
205pub trait Unit {}
206
207#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
208pub struct InTiles;
209impl Unit for InTiles {}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
212pub struct InPixels;
213impl Unit for InPixels {}
214
215#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
216pub struct InMph;
217
218impl Unit for InMph {}
219
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
221pub struct InDays;
222impl Unit for InDays {}
223
224#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
225pub struct Vec2<U: Unit, T> {
226	pub x: T,
227	pub y: T,
228	_unit: PhantomData<U>,
229}
230
231impl<U: Unit, T> Vec2<U, T> {
232	pub fn new(x: T, y: T) -> Self {
233		Vec2 {
234			x,
235			y,
236			_unit: PhantomData,
237		}
238	}
239}
240
241#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
242pub struct Value<U: Unit, T> {
243	value: T,
244	_unit: PhantomData<U>,
245}
246
247impl<U: Unit, T> Value<U, T> {
248	pub fn new(v: T) -> Self {
249		Self {
250			value: v,
251			_unit: PhantomData,
252		}
253	}
254
255	pub fn value(self) -> T {
256		self.value
257	}
258}
259
260impl<U: Unit, T> From<T> for Value<U, T> {
261	fn from(value: T) -> Self {
262		Self {
263			value,
264			_unit: PhantomData,
265		}
266	}
267}
268
269impl<U: Unit, T> Deref for Value<U, T> {
270	type Target = T;
271	fn deref(&self) -> &Self::Target {
272		&self.value
273	}
274}
275
276impl<U: Unit, T> DerefMut for Value<U, T> {
277	fn deref_mut(&mut self) -> &mut Self::Target {
278		&mut self.value
279	}
280}
281
282pub type YAxis<U, T> = Value<U, T>;
283pub type XAxis<U, T> = Value<U, T>;