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
194pub trait Unit {}
195
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
197pub struct InTiles;
198impl Unit for InTiles {}
199
200#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
201pub struct InPixels;
202impl Unit for InPixels {}
203
204#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
205pub struct InMph;
206
207impl Unit for InMph {}
208
209#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
210pub struct InDays;
211impl Unit for InDays {}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
214pub struct Vec2<U: Unit, T> {
215	pub x: T,
216	pub y: T,
217	_unit: PhantomData<U>,
218}
219
220impl<U: Unit, T> Vec2<U, T> {
221	pub fn new(x: T, y: T) -> Self {
222		Vec2 {
223			x,
224			y,
225			_unit: PhantomData,
226		}
227	}
228}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
231pub struct Value<U: Unit, T> {
232	value: T,
233	_unit: PhantomData<U>,
234}
235
236impl<U: Unit, T> Value<U, T> {
237	pub fn new(v: T) -> Self {
238		Self {
239			value: v,
240			_unit: PhantomData,
241		}
242	}
243
244	pub fn value(self) -> T {
245		self.value
246	}
247}
248
249impl<U: Unit, T> From<T> for Value<U, T> {
250	fn from(value: T) -> Self {
251		Self {
252			value,
253			_unit: PhantomData,
254		}
255	}
256}
257
258impl<U: Unit, T> Deref for Value<U, T> {
259	type Target = T;
260	fn deref(&self) -> &Self::Target {
261		&self.value
262	}
263}
264
265impl<U: Unit, T> DerefMut for Value<U, T> {
266	fn deref_mut(&mut self) -> &mut Self::Target {
267		&mut self.value
268	}
269}
270
271pub type YAxis<U, T> = Value<U, T>;
272pub type XAxis<U, T> = Value<U, T>;