1use std::io;
8use std::path::Path;
9
10pub const MAX_TILES: usize = 6144;
11
12#[derive(Debug, Clone, Copy, Default)]
14pub struct PicAnm {
15 pub frames: u8,
16 pub anim_type: u8,
18 pub xoffset: i8,
19 pub yoffset: i8,
20 pub speed: u8,
21 pub view: u8,
24}
25
26impl PicAnm {
27 pub fn from_u32(v: u32) -> Self {
28 PicAnm {
29 frames: (v & 0x3f) as u8,
30 anim_type: ((v >> 6) & 3) as u8,
31 xoffset: ((v >> 8) & 0xff) as u8 as i8,
32 yoffset: ((v >> 16) & 0xff) as u8 as i8,
33 speed: ((v >> 24) & 0x0f) as u8,
34 view: ((v >> 28) & 7) as u8,
35 }
36 }
37}
38
39#[derive(Debug, Clone, Default)]
40pub struct Tile {
41 pub width: u16,
42 pub height: u16,
43 pub anm: PicAnm,
44 pub pixels: Vec<u8>,
46}
47
48impl Tile {
49 #[inline]
50 pub fn is_empty(&self) -> bool {
51 self.pixels.is_empty()
52 }
53 #[inline]
54 pub fn column(&self, x: usize) -> &[u8] {
55 let h = self.height as usize;
56 &self.pixels[x * h..x * h + h]
57 }
58}
59
60pub struct TileSet {
61 pub tiles: Vec<Tile>,
62}
63
64fn invalid(msg: impl Into<String>) -> io::Error {
65 io::Error::new(io::ErrorKind::InvalidData, msg.into())
66}
67
68impl Default for TileSet {
69 fn default() -> Self {
70 TileSet {
71 tiles: vec![Tile::default(); MAX_TILES],
72 }
73 }
74}
75
76impl TileSet {
77 pub fn load_dir(dir: impl AsRef<Path>) -> io::Result<Self> {
79 let mut set = TileSet::default();
80 let mut loaded = 0;
81 for i in 0..100 {
82 let path = dir.as_ref().join(format!("TILES{i:03}.ART"));
83 match std::fs::read(&path) {
84 Ok(bytes) => {
85 set.add_art(&bytes)?;
86 loaded += 1;
87 }
88 Err(e) if e.kind() == io::ErrorKind::NotFound => break,
89 Err(e) => return Err(e),
90 }
91 }
92 if loaded == 0 {
93 return Err(io::Error::new(io::ErrorKind::NotFound, "no TILES000.ART found"));
94 }
95 Ok(set)
96 }
97
98 pub fn add_art(&mut self, b: &[u8]) -> io::Result<()> {
99 if b.len() < 16 {
100 return Err(invalid("ART file too short"));
101 }
102 let rd = |o: usize| u32::from_le_bytes(b[o..o + 4].try_into().unwrap());
103 if rd(0) != 1 {
104 return Err(invalid("unsupported ART version"));
105 }
106 let first = rd(8) as usize;
107 let last = rd(12) as usize;
108 if last < first || last >= MAX_TILES {
109 return Err(invalid("ART tile range invalid"));
110 }
111 let n = last - first + 1;
112 let mut p = 16;
113 let need = p + n * 8;
114 if b.len() < need {
115 return Err(invalid("ART header truncated"));
116 }
117 let w: Vec<u16> = (0..n)
118 .map(|i| u16::from_le_bytes([b[p + i * 2], b[p + i * 2 + 1]]))
119 .collect();
120 p += n * 2;
121 let h: Vec<u16> = (0..n)
122 .map(|i| u16::from_le_bytes([b[p + i * 2], b[p + i * 2 + 1]]))
123 .collect();
124 p += n * 2;
125 let anm: Vec<u32> = (0..n).map(|i| rd(p + i * 4)).collect();
126 p += n * 4;
127 for i in 0..n {
128 let size = w[i] as usize * h[i] as usize;
129 let t = &mut self.tiles[first + i];
130 t.width = w[i];
131 t.height = h[i];
132 t.anm = PicAnm::from_u32(anm[i]);
133 if size > 0 {
134 if p + size > b.len() {
135 return Err(invalid(format!("tile {} data truncated", first + i)));
136 }
137 t.pixels = b[p..p + size].to_vec();
138 p += size;
139 } else {
140 t.pixels.clear();
141 }
142 }
143 Ok(())
144 }
145
146 #[inline]
147 pub fn get(&self, pic: i32) -> &Tile {
148 static EMPTY: Tile = Tile {
149 width: 0,
150 height: 0,
151 anm: PicAnm {
152 frames: 0,
153 anim_type: 0,
154 xoffset: 0,
155 yoffset: 0,
156 speed: 0,
157 view: 0,
158 },
159 pixels: Vec::new(),
160 };
161 if pic < 0 || pic as usize >= self.tiles.len() {
162 &EMPTY
163 } else {
164 &self.tiles[pic as usize]
165 }
166 }
167
168 pub fn animate(&self, pic: i32, clock: i32) -> i32 {
170 let t = self.get(pic);
171 let a = t.anm;
172 if a.frames == 0 || a.anim_type == 0 {
173 return pic;
174 }
175 let frames = a.frames as i32;
176 let i = clock >> a.speed;
177 let off = match a.anim_type {
178 1 => {
179 let k = i % (frames * 2);
180 if k < frames {
181 k
182 } else {
183 frames * 2 - k
184 }
185 }
186 2 => i % (frames + 1),
187 3 => -(i % (frames + 1)),
188 _ => 0,
189 };
190 pic + off
191 }
192}
193
194#[cfg(test)]
195mod tests {
196 use super::*;
197
198 #[test]
199 fn picanm_unpack() {
200 let v = 5 | (2 << 6) | (0xfe << 8) | (3 << 16) | (4 << 24) | (6 << 28);
201 let a = PicAnm::from_u32(v);
202 assert_eq!(
203 (a.frames, a.anim_type, a.xoffset, a.yoffset, a.speed, a.view),
204 (5, 2, -2, 3, 4, 6)
205 );
206 }
207
208 #[test]
209 fn parse_synthetic_art() {
210 let mut b = Vec::new();
211 for v in [1u32, 0, 10, 11] {
212 b.extend_from_slice(&v.to_le_bytes());
213 }
214 b.extend_from_slice(&2u16.to_le_bytes());
215 b.extend_from_slice(&0u16.to_le_bytes());
216 b.extend_from_slice(&3u16.to_le_bytes());
217 b.extend_from_slice(&0u16.to_le_bytes());
218 b.extend_from_slice(&0u32.to_le_bytes());
219 b.extend_from_slice(&0u32.to_le_bytes());
220 b.extend_from_slice(&[1, 2, 3, 4, 5, 6]);
221 let mut set = TileSet::default();
222 set.add_art(&b).unwrap();
223 assert_eq!(set.get(10).column(1), &[4, 5, 6]);
224 assert!(set.get(11).is_empty());
225 }
226}