1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#[macro_export]
macro_rules! impl_from_into {
	($outer:ident($inner:ident)) => {
		impl From<$inner> for $outer {
			fn from(v: $inner) -> $outer {
				$outer(v)
			}
		}

		impl From<$outer> for $inner {
			fn from($outer(v): $outer) -> $inner {
				v
			}
		}
	}
}

#[macro_export]
macro_rules! newtype {
	($outer:ident($inner:ident)) => {
		#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
		#[repr(transparent)]
		pub struct $outer(pub $inner);
		$crate::impl_from_into!($outer($inner));
	};
	($outer:ident($inner:ident), $fmt:literal) => {
		#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
		#[repr(transparent)]
		pub struct $outer(pub $inner);
		$crate::impl_from_into!($outer($inner));

		impl ::core::fmt::Debug for $outer {
			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
				f.debug_tuple(stringify!($outer))
					.field(&format_args!($fmt, &self.0))
					.finish()
			}
		}
	};
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FuncId(pub u16, pub u16);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Pos2 {
	pub x: i32,
	pub z: i32,
}

pub type Pos3 = glam::IVec3;

newtype!(NameId(u16));
newtype!(BgmId(u16));
newtype!(SoundId(u32));
newtype!(ItemId(u16));
newtype!(RecipeId(u16));
newtype!(TownId(u16));
newtype!(ShopId(u8));
newtype!(MagicId(u16));

newtype!(FileId(u32), "0x{:08X}");

impl FileId {
	pub const NONE: FileId = FileId(0);
}

newtype!(Color(u32), "0x{:08X}");

newtype!(Flag(u16));

newtype!(Angle(i16));
newtype!(Length(i32));
newtype!(Time(u32));
newtype!(Speed(u32));
newtype!(AngularSpeed(u16));
newtype!(Angle32(i32));

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CharId {
	FieldParty(u16),
	Local(LocalCharId),
	Party(u16),
	Custom(u16),
	Null,
	Self_,
	Name(NameId),
}
newtype!(LocalCharId(u16));

newtype!(ChipId(u16));
newtype!(LookPointId(u16));
newtype!(ObjectId(u16));
newtype!(EntranceId(u8));
newtype!(TriggerId(u16));
newtype!(LabelId(u16));
newtype!(AnimId(u16));

newtype!(BattleId(u32));
newtype!(SepithId(u16));
newtype!(PlacementId(u16));
newtype!(AtRollId(u16));

// 0x00000001 SF_CAMERA_AUTO
// 0x00400000 SF_ENTRY_DISABLE
// 0x02000000 SF_FADEBGM_DISABLE
newtype!(SystemFlags(u32));

// 0x10 done
newtype!(QuestId(u16));
newtype!(QuestFlags(u8));
newtype!(QuestTask(u16));

// 0x0002 PF_NOVEC
// 0x0004 PF_NOHEIGHT
// 0x0008 PF_NODISP
// 0x0010 PF_NOTURN
// 0x0020 PF_NOANIME
// 0x0040 PF_NOATARI
// 0x0080 PF_UNDEF
newtype!(CharFlags(u16));

// 0x0004 MOF_NODISP
// 0x0020 MOF_LOOPPLAY
newtype!(ObjectFlags(u32));

newtype!(LookPointFlags(u16));
newtype!(EntryFlags(u16));
newtype!(TriggerFlags(u16));

// A bitfield in 3rd's party select menu
newtype!(TcMembers(u32));

newtype!(Var(u16)); // called Work internally
newtype!(Global(u8));

// 0 SW_ENTRY_NO
// 1 SW_BGM_NO
// 3 battle result
// 4 current chapter
// 10 party lead
// 11 party second
// 12 party third
// 13 party fourth
// 14 party fifth (guest)
// 15 party sixth (guest)
// 18 current mira
// 19 ItemId used in item handler
// 21 number of battles
// 26 used much during the Madrigal, and when Joshua is activated in SC.
// 27 Boolean. Often set together with 26.
// 35 set to 255 once after rescuing Tita in FC
// 28 Boolean. Generally only set to true for a short time
// 40 SW_CURSOR_FORM (24 MSCRS_NORMAL, FFFF MSCRS_VOID)
// 41 BattleId, generally checked in reinit
// 42 sometimes set to a negative number in reinit. I suspect it's something with altitude.
// 43 set to 255 in some reinit in FC
// 45 SW_MOVIE_STATE
// 46 CharId, set together with 26 and 27 in the Madrogal. Spotlight character?
// 47 Bracer rank
// 49 TownId for the next save, values include 19, 140, 302, 400, 401, 274, 259, 297, 296, 299
newtype!(Attr(u8));

newtype!(EffId(u8));
newtype!(EffInstanceId(u8));
newtype!(MenuId(u16));
newtype!(VisId(u8));
newtype!(ForkId(u16));

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct CharAttr(pub CharId, pub u8);

// Translatable string
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Default)]
#[repr(transparent)]
pub struct TString(pub String);
impl_from_into!(TString(String));

impl std::ops::Deref for TString {
	type Target = String;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl std::ops::DerefMut for TString {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.0
	}
}

impl<'a> From<&'a String> for &'a TString {
	fn from(value: &'a String) -> Self {
		// SAFETY: repr(transparent)
		unsafe { std::mem::transmute(value) }
	}
}

impl<'a> From<&'a TString> for &'a String {
	fn from(value: &'a TString) -> Self {
		&value.0
	}
}

impl From<&str> for TString {
	fn from(value: &str) -> Self {
		String::from(value).into()
	}
}

impl std::fmt::Debug for TString {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		write!(f, "t{:?}", &self.0)
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BaseGame {
	Fc, Sc, Tc,
	Zero, Ao,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Game {
	Fc, FcEvo, FcKai,
	Sc, ScEvo, ScKai,
	Tc, TcEvo, TcKai,

	Zero, ZeroEvo, ZeroKai,
	Ao, AoEvo, AoKai,
}

impl Game {
	pub fn base(self) -> BaseGame {
		use Game::*;
		match self {
			Fc|FcEvo|FcKai => BaseGame::Fc,
			Sc|ScEvo|ScKai => BaseGame::Sc,
			Tc|TcEvo|TcKai => BaseGame::Tc,

			Zero|ZeroEvo|ZeroKai => BaseGame::Zero,
			Ao|AoEvo|AoKai => BaseGame::Ao,
		}
	}
}

impl Game {
	pub fn is_ed7(self) -> bool {
		matches!(self.base(), BaseGame::Zero|BaseGame::Ao)
	}
}