1use std::io;
9
10pub const SECTOR_SIZE: usize = 40;
11pub const WALL_SIZE: usize = 32;
12pub const SPRITE_SIZE: usize = 44;
13
14pub fn map_crypt(buf: &mut [u8], key: u32) {
16 for (i, b) in buf.iter_mut().enumerate() {
17 *b ^= key.wrapping_add(i as u32) as u8;
18 }
19}
20
21fn invalid(msg: impl Into<String>) -> io::Error {
22 io::Error::new(io::ErrorKind::InvalidData, msg.into())
23}
24
25pub struct BitReader<'a> {
27 data: &'a [u8],
28 pos: usize,
29}
30
31impl<'a> BitReader<'a> {
32 pub fn new(data: &'a [u8]) -> Self {
33 BitReader { data, pos: 0 }
34 }
35 pub fn u(&mut self, bits: u32) -> u32 {
36 let mut v: u64 = 0;
37 for i in 0..bits {
38 let byte = self.data.get(self.pos / 8).copied().unwrap_or(0);
39 let bit = (byte >> (self.pos % 8)) & 1;
40 v |= (bit as u64) << i;
41 self.pos += 1;
42 }
43 v as u32
44 }
45 pub fn s(&mut self, bits: u32) -> i32 {
46 let v = self.u(bits);
47 if bits < 32 && v & (1 << (bits - 1)) != 0 {
48 (v | (!0u32 << bits)) as i32
49 } else {
50 v as i32
51 }
52 }
53 pub fn bit(&mut self) -> bool {
54 self.u(1) != 0
55 }
56}
57
58struct Cursor<'a> {
59 d: &'a [u8],
60 p: usize,
61}
62
63impl<'a> Cursor<'a> {
64 fn take(&mut self, n: usize) -> io::Result<&'a [u8]> {
65 if self.p + n > self.d.len() {
66 return Err(invalid("unexpected end of map file"));
67 }
68 let s = &self.d[self.p..self.p + n];
69 self.p += n;
70 Ok(s)
71 }
72}
73
74fn i16_at(b: &[u8], o: usize) -> i16 {
75 i16::from_le_bytes([b[o], b[o + 1]])
76}
77fn u16_at(b: &[u8], o: usize) -> u16 {
78 u16::from_le_bytes([b[o], b[o + 1]])
79}
80fn i32_at(b: &[u8], o: usize) -> i32 {
81 i32::from_le_bytes(b[o..o + 4].try_into().unwrap())
82}
83
84#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, Default)]
85pub struct Plane {
86 pub z: i32,
87 pub stat: u16,
88 pub picnum: i16,
89 pub heinum: i16,
90 pub shade: i8,
91 pub pal: u8,
92 pub xpan: u8,
93 pub ypan: u8,
94}
95
96#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
97pub struct Sector {
98 pub wallptr: i16,
99 pub wallnum: i16,
100 pub ceiling: Plane,
101 pub floor: Plane,
102 pub visibility: u8,
103 pub filler: u8,
104 pub lotag: i16,
106 pub hitag: i16,
107 pub extra: i16,
109}
110
111#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
112pub struct Wall {
113 pub x: i32,
114 pub y: i32,
115 pub point2: i16,
116 pub nextwall: i16,
117 pub nextsector: i16,
118 pub cstat: u16,
119 pub picnum: i16,
120 pub overpicnum: i16,
121 pub shade: i8,
122 pub pal: u8,
123 pub xrepeat: u8,
124 pub yrepeat: u8,
125 pub xpanning: u8,
126 pub ypanning: u8,
127 pub lotag: i16,
128 pub hitag: i16,
129 pub extra: i16,
130}
131
132#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
133pub struct Sprite {
134 pub x: i32,
135 pub y: i32,
136 pub z: i32,
137 pub cstat: u16,
138 pub picnum: i16,
139 pub shade: i8,
140 pub pal: u8,
141 pub clipdist: u8,
142 pub flags: u8,
143 pub xrepeat: u8,
144 pub yrepeat: u8,
145 pub xoffset: i8,
146 pub yoffset: i8,
147 pub sectnum: i16,
148 pub statnum: i16,
149 pub ang: i16,
150 pub owner: i16,
151 pub xvel: i16,
152 pub yvel: i16,
153 pub zvel: i16,
154 pub lotag: i16,
156 pub hitag: i16,
157 pub extra: i16,
158}
159
160#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
161pub struct XSector {
162 pub reference: i32,
163 pub state: u8,
164 pub busy: u32,
165 pub data: u32,
166 pub tx_id: u32,
167 pub busy_wave_a: u8,
168 pub busy_wave_b: u8,
169 pub rx_id: u32,
170 pub command: u8,
171 pub trigger_on: bool,
172 pub trigger_off: bool,
173 pub busy_time_a: u32,
174 pub wait_time_a: u32,
175 pub rest_state: bool,
176 pub interruptable: bool,
177 pub amplitude: i32,
178 pub freq: u32,
179 pub retrigger_a: bool,
180 pub retrigger_b: bool,
181 pub phase: u32,
182 pub wave: u8,
183 pub shade_always: bool,
184 pub shade_floor: bool,
185 pub shade_ceiling: bool,
186 pub shade_walls: bool,
187 pub shade: i32,
188 pub pan_always: bool,
189 pub pan_floor: bool,
190 pub pan_ceiling: bool,
191 pub drag: bool,
192 pub underwater: bool,
193 pub depth: u8,
194 pub pan_vel: u32,
195 pub pan_angle: u32,
196 pub decoupled: bool,
197 pub trigger_once: bool,
198 pub is_triggered: bool,
199 pub key: u8,
200 pub push: bool,
201 pub vector: bool,
202 pub reserved: bool,
203 pub enter: bool,
204 pub exit: bool,
205 pub wallpush: bool,
206 pub color: bool,
207 pub busy_time_b: u32,
208 pub wait_time_b: u32,
209 pub stop_on: bool,
210 pub stop_off: bool,
211 pub ceil_pal: u8,
212 pub off_ceil_z: i32,
213 pub on_ceil_z: i32,
214 pub off_floor_z: i32,
215 pub on_floor_z: i32,
216 pub marker0: u32,
217 pub marker1: u32,
218 pub crush: bool,
219 pub ceil_xpan_frac: u8,
220 pub ceil_ypan_frac: u8,
221 pub floor_xpan_frac: u8,
222 pub damage_type: u8,
223 pub floor_pal: u8,
224 pub floor_ypan_frac: u8,
225 pub locked: bool,
226 pub wind_vel: u32,
227 pub wind_ang: u32,
228 pub wind_always: bool,
229 pub dude_lockout: bool,
230 pub bob_theta: u32,
231 pub bob_z_range: u32,
232 pub bob_speed: i32,
233 pub bob_always: bool,
234 pub bob_floor: bool,
235 pub bob_ceiling: bool,
236 pub bob_rotate: bool,
237}
238
239impl XSector {
240 #[allow(clippy::field_reassign_with_default)]
242 pub fn parse(b: &[u8]) -> Self {
243 let mut r = BitReader::new(b);
244 let mut x = XSector::default();
245 x.reference = r.s(14);
246 x.state = r.u(1) as u8;
247 x.busy = r.u(17);
248 x.data = r.u(16);
249 x.tx_id = r.u(10);
250 x.busy_wave_a = r.u(3) as u8;
251 x.busy_wave_b = r.u(3) as u8;
252 x.rx_id = r.u(10);
253 x.command = r.u(8) as u8;
254 x.trigger_on = r.bit();
255 x.trigger_off = r.bit();
256 x.busy_time_a = r.u(12);
257 x.wait_time_a = r.u(12);
258 x.rest_state = r.bit();
259 x.interruptable = r.bit();
260 x.amplitude = r.s(8);
261 x.freq = r.u(8);
262 x.retrigger_a = r.bit();
263 x.retrigger_b = r.bit();
264 x.phase = r.u(8);
265 x.wave = r.u(4) as u8;
266 x.shade_always = r.bit();
267 x.shade_floor = r.bit();
268 x.shade_ceiling = r.bit();
269 x.shade_walls = r.bit();
270 x.shade = r.s(8);
271 x.pan_always = r.bit();
272 x.pan_floor = r.bit();
273 x.pan_ceiling = r.bit();
274 x.drag = r.bit();
275 x.underwater = r.bit();
276 x.depth = r.u(3) as u8;
277 x.pan_vel = r.u(8);
278 x.pan_angle = r.u(11);
279 r.u(1);
280 x.decoupled = r.bit();
281 x.trigger_once = r.bit();
282 x.is_triggered = r.bit();
283 x.key = r.u(3) as u8;
284 x.push = r.bit();
285 x.vector = r.bit();
286 x.reserved = r.bit();
287 x.enter = r.bit();
288 x.exit = r.bit();
289 x.wallpush = r.bit();
290 x.color = r.bit();
291 r.u(1);
292 x.busy_time_b = r.u(12);
293 x.wait_time_b = r.u(12);
294 x.stop_on = r.bit();
295 x.stop_off = r.bit();
296 x.ceil_pal = r.u(4) as u8;
297 x.off_ceil_z = r.s(32);
298 x.on_ceil_z = r.s(32);
299 x.off_floor_z = r.s(32);
300 x.on_floor_z = r.s(32);
301 x.marker0 = r.u(16);
302 x.marker1 = r.u(16);
303 x.crush = r.bit();
304 x.ceil_xpan_frac = r.u(8) as u8;
305 x.ceil_ypan_frac = r.u(8) as u8;
306 x.floor_xpan_frac = r.u(8) as u8;
307 x.damage_type = r.u(3) as u8;
308 x.floor_pal = r.u(4) as u8;
309 x.floor_ypan_frac = r.u(8) as u8;
310 x.locked = r.bit();
311 x.wind_vel = r.u(10);
312 x.wind_ang = r.u(11);
313 x.wind_always = r.bit();
314 x.dude_lockout = r.bit();
315 x.bob_theta = r.u(11);
316 x.bob_z_range = r.u(5);
317 x.bob_speed = r.s(12);
318 x.bob_always = r.bit();
319 x.bob_floor = r.bit();
320 x.bob_ceiling = r.bit();
321 x.bob_rotate = r.bit();
322 x
323 }
324}
325
326#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
327pub struct XWall {
328 pub reference: i32,
329 pub state: u8,
330 pub busy: u32,
331 pub data: i32,
332 pub tx_id: u32,
333 pub rx_id: u32,
334 pub command: u8,
335 pub trigger_on: bool,
336 pub trigger_off: bool,
337 pub busy_time: u32,
338 pub wait_time: u32,
339 pub rest_state: bool,
340 pub interruptable: bool,
341 pub pan_always: bool,
342 pub pan_xvel: i32,
343 pub pan_yvel: i32,
344 pub decoupled: bool,
345 pub trigger_once: bool,
346 pub is_triggered: bool,
347 pub key: u8,
348 pub trigger_push: bool,
349 pub trigger_vector: bool,
350 pub trigger_touch: bool,
351 pub xpan_frac: u8,
352 pub ypan_frac: u8,
353 pub locked: bool,
354 pub dude_lockout: bool,
355}
356
357impl XWall {
358 #[allow(clippy::field_reassign_with_default)]
360 pub fn parse(b: &[u8]) -> Self {
361 let mut r = BitReader::new(b);
362 let mut x = XWall::default();
363 x.reference = r.s(14);
364 x.state = r.u(1) as u8;
365 x.busy = r.u(17);
366 x.data = r.s(16);
367 x.tx_id = r.u(10);
368 r.u(6);
369 x.rx_id = r.u(10);
370 x.command = r.u(8) as u8;
371 x.trigger_on = r.bit();
372 x.trigger_off = r.bit();
373 x.busy_time = r.u(12);
374 x.wait_time = r.u(12);
375 x.rest_state = r.bit();
376 x.interruptable = r.bit();
377 x.pan_always = r.bit();
378 x.pan_xvel = r.s(8);
379 x.pan_yvel = r.s(8);
380 x.decoupled = r.bit();
381 x.trigger_once = r.bit();
382 x.is_triggered = r.bit();
383 x.key = r.u(3) as u8;
384 x.trigger_push = r.bit();
385 x.trigger_vector = r.bit();
386 x.trigger_touch = r.bit();
387 r.u(2);
388 x.xpan_frac = r.u(8) as u8;
389 x.ypan_frac = r.u(8) as u8;
390 x.locked = r.bit();
391 x.dude_lockout = r.bit();
392 x
393 }
394}
395
396#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
397pub struct XSprite {
398 pub reference: i32,
399 pub state: u8,
400 pub busy: u32,
401 pub tx_id: u32,
402 pub rx_id: u32,
403 pub command: u8,
404 pub trigger_on: bool,
405 pub trigger_off: bool,
406 pub wave: u8,
407 pub busy_time: u32,
408 pub wait_time: u32,
409 pub rest_state: bool,
410 pub interruptable: bool,
411 pub respawn_pending: u8,
412 pub launch_team: bool,
413 pub drop_msg: u8,
414 pub decoupled: bool,
415 pub trigger_once: bool,
416 pub is_triggered: bool,
417 pub key: u8,
418 pub push: bool,
419 pub vector: bool,
420 pub impact: bool,
421 pub pickup: bool,
422 pub touch: bool,
423 pub sight: bool,
424 pub proximity: bool,
425 pub launch_skill: u8,
427 pub launch_single: bool,
428 pub launch_bloodbath: bool,
429 pub launch_coop: bool,
430 pub dude_lockout: bool,
431 pub data1: i32,
432 pub data2: i32,
433 pub data3: i32,
434 pub goal_ang: u32,
435 pub dodge_dir: i32,
436 pub locked: bool,
437 pub medium: u8,
438 pub respawn: u8,
439 pub data4: u32,
440 pub lock_msg: u8,
441 pub health: u32,
442 pub dude_deaf: bool,
443 pub dude_ambush: bool,
444 pub dude_guard: bool,
445 pub dude_flag4: bool,
446 pub target: i32,
447 pub target_x: i32,
448 pub target_y: i32,
449 pub target_z: i32,
450 pub burn_time: u32,
451 pub burn_source: i32,
452 pub height: u32,
453 pub state_timer: u32,
454}
455
456impl XSprite {
457 #[allow(clippy::field_reassign_with_default)]
459 pub fn parse(b: &[u8]) -> Self {
460 let mut r = BitReader::new(b);
461 let mut x = XSprite::default();
462 x.reference = r.s(14);
463 x.state = r.u(1) as u8;
464 x.busy = r.u(17);
465 x.tx_id = r.u(10);
466 x.rx_id = r.u(10);
467 x.command = r.u(8) as u8;
468 x.trigger_on = r.bit();
469 x.trigger_off = r.bit();
470 x.wave = r.u(2) as u8;
471 x.busy_time = r.u(12);
472 x.wait_time = r.u(12);
473 x.rest_state = r.bit();
474 x.interruptable = r.bit();
475 r.u(2);
476 x.respawn_pending = r.u(2) as u8;
477 r.u(1);
478 x.launch_team = r.bit();
479 x.drop_msg = r.u(8) as u8;
480 x.decoupled = r.bit();
481 x.trigger_once = r.bit();
482 x.is_triggered = r.bit();
483 x.key = r.u(3) as u8;
484 x.push = r.bit();
485 x.vector = r.bit();
486 x.impact = r.bit();
487 x.pickup = r.bit();
488 x.touch = r.bit();
489 x.sight = r.bit();
490 x.proximity = r.bit();
491 r.u(2);
492 x.launch_skill = r.u(5) as u8;
493 x.launch_single = r.bit();
494 x.launch_bloodbath = r.bit();
495 x.launch_coop = r.bit();
496 x.dude_lockout = r.bit();
497 x.data1 = r.s(16);
498 x.data2 = r.s(16);
499 x.data3 = r.s(16);
500 x.goal_ang = r.u(11);
501 x.dodge_dir = r.s(2);
502 x.locked = r.bit();
503 x.medium = r.u(2) as u8;
504 x.respawn = r.u(2) as u8;
505 x.data4 = r.u(16);
506 r.u(6);
507 x.lock_msg = r.u(8) as u8;
508 x.health = r.u(12);
509 x.dude_deaf = r.bit();
510 x.dude_ambush = r.bit();
511 x.dude_guard = r.bit();
512 x.dude_flag4 = r.bit();
513 x.target = r.s(16);
514 x.target_x = r.s(32);
515 x.target_y = r.s(32);
516 x.target_z = r.s(32);
517 x.burn_time = r.u(16);
518 x.burn_source = r.s(16);
519 x.height = r.u(16);
520 x.state_timer = r.u(16);
521 x
522 }
523}
524
525#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Default)]
526pub struct Map {
527 pub version: u16,
528 pub start_x: i32,
529 pub start_y: i32,
530 pub start_z: i32,
531 pub start_ang: i16,
532 pub start_sect: i16,
533 pub sky_bits: i16,
534 pub visibility: i32,
535 pub song_id: i32,
536 pub parallax: u8,
537 pub revision: i32,
538 pub sky_offsets: Vec<i16>,
539 pub sectors: Vec<Sector>,
540 pub walls: Vec<Wall>,
541 pub sprites: Vec<Sprite>,
542 pub xsectors: Vec<XSector>,
544 pub xwalls: Vec<XWall>,
545 pub xsprites: Vec<XSprite>,
546 pub crc: u32,
547}
548
549fn parse_plane(b: &[u8], z_off: usize, stat_off: usize, pic_off: usize) -> Plane {
550 Plane {
551 z: i32_at(b, z_off),
552 stat: u16_at(b, stat_off),
553 picnum: i16_at(b, pic_off),
554 heinum: i16_at(b, pic_off + 2),
555 shade: b[pic_off + 4] as i8,
556 pal: b[pic_off + 5],
557 xpan: b[pic_off + 6],
558 ypan: b[pic_off + 7],
559 }
560}
561
562impl Sector {
563 pub fn parse(b: &[u8]) -> Self {
564 Sector {
565 wallptr: i16_at(b, 0),
566 wallnum: i16_at(b, 2),
567 ceiling: parse_plane(b, 4, 12, 16),
568 floor: parse_plane(b, 8, 14, 24),
569 visibility: b[32],
570 filler: b[33],
571 lotag: i16_at(b, 34),
572 hitag: i16_at(b, 36),
573 extra: i16_at(b, 38),
574 }
575 }
576}
577
578impl Wall {
579 pub fn parse(b: &[u8]) -> Self {
580 Wall {
581 x: i32_at(b, 0),
582 y: i32_at(b, 4),
583 point2: i16_at(b, 8),
584 nextwall: i16_at(b, 10),
585 nextsector: i16_at(b, 12),
586 cstat: u16_at(b, 14),
587 picnum: i16_at(b, 16),
588 overpicnum: i16_at(b, 18),
589 shade: b[20] as i8,
590 pal: b[21],
591 xrepeat: b[22],
592 yrepeat: b[23],
593 xpanning: b[24],
594 ypanning: b[25],
595 lotag: i16_at(b, 26),
596 hitag: i16_at(b, 28),
597 extra: i16_at(b, 30),
598 }
599 }
600}
601
602impl Sprite {
603 pub fn parse(b: &[u8]) -> Self {
604 Sprite {
605 x: i32_at(b, 0),
606 y: i32_at(b, 4),
607 z: i32_at(b, 8),
608 cstat: u16_at(b, 12),
609 picnum: i16_at(b, 14),
610 shade: b[16] as i8,
611 pal: b[17],
612 clipdist: b[18],
613 flags: b[19],
614 xrepeat: b[20],
615 yrepeat: b[21],
616 xoffset: b[22] as i8,
617 yoffset: b[23] as i8,
618 sectnum: i16_at(b, 24),
619 statnum: i16_at(b, 26),
620 ang: i16_at(b, 28),
621 owner: i16_at(b, 30),
622 xvel: i16_at(b, 32),
623 yvel: i16_at(b, 34),
624 zvel: i16_at(b, 36),
625 lotag: i16_at(b, 38),
626 hitag: i16_at(b, 40),
627 extra: i16_at(b, 42),
628 }
629 }
630}
631
632impl Map {
633 pub fn parse(data: &[u8]) -> io::Result<Map> {
634 if data.len() < 6 || &data[0..4] != b"BLM\x1a" {
635 return Err(invalid("not a Blood map (missing BLM signature)"));
636 }
637 let version = u16_at(data, 4);
638 let encrypted = match version & 0xff00 {
639 0x0700 => true,
640 0x0600 => false,
641 _ => return Err(invalid(format!("unsupported map version {version:#x}"))),
642 };
643 let mut c = Cursor { d: data, p: 6 };
644 let mut h = c.take(37)?.to_vec();
645 let song_raw = i32_at(&h, 22);
646 if song_raw != 0 && song_raw != 0x504d_6172 && song_raw != 0x7261_504d {
647 map_crypt(&mut h, 0x4d);
648 }
649 let mut m = Map {
650 version,
651 start_x: i32_at(&h, 0),
652 start_y: i32_at(&h, 4),
653 start_z: i32_at(&h, 8),
654 start_ang: i16_at(&h, 12),
655 start_sect: i16_at(&h, 14),
656 sky_bits: i16_at(&h, 16),
657 visibility: i32_at(&h, 18),
658 song_id: i32_at(&h, 22),
659 parallax: h[26],
660 revision: i32_at(&h, 27),
661 ..Default::default()
662 };
663 let nsectors = u16_at(&h, 31) as usize;
664 let nwalls = u16_at(&h, 33) as usize;
665 let nsprites = u16_at(&h, 35) as usize;
666
667 let (mut xsprite_size, mut xwall_size, mut xsector_size) = (56usize, 24usize, 60usize);
668 if encrypted {
669 let mut h2 = c.take(128)?.to_vec();
670 map_crypt(&mut h2, nwalls as u32);
671 let rd = |o: usize| i32_at(&h2, o) as usize;
672 if rd(64) != 0 {
673 xsprite_size = rd(64);
674 xwall_size = rd(68);
675 xsector_size = rd(72);
676 }
677 }
678
679 let nsky = 1usize << m.sky_bits.clamp(0, 12);
680 let mut sky = c.take(nsky * 2)?.to_vec();
681 if encrypted {
682 map_crypt(&mut sky, (nsky * 2) as u32);
683 }
684 m.sky_offsets = sky
685 .as_chunks::<2>()
686 .0
687 .iter()
688 .map(|b| i16::from_le_bytes([b[0], b[1]]))
689 .collect();
690
691 let rev = m.revision as u32;
692 m.xsectors.push(XSector::default());
693 for _ in 0..nsectors {
694 let mut b = c.take(SECTOR_SIZE)?.to_vec();
695 if encrypted {
696 map_crypt(&mut b, rev.wrapping_mul(SECTOR_SIZE as u32));
697 }
698 let mut s = Sector::parse(&b);
699 if s.extra > 0 {
700 let xb = c.take(xsector_size)?;
701 s.extra = m.xsectors.len() as i16;
702 m.xsectors.push(XSector::parse(xb));
703 } else {
704 s.extra = -1;
705 }
706 m.sectors.push(s);
707 }
708 m.xwalls.push(XWall::default());
709 for _ in 0..nwalls {
710 let mut b = c.take(WALL_SIZE)?.to_vec();
711 if encrypted {
712 map_crypt(&mut b, rev.wrapping_mul(SECTOR_SIZE as u32) | 0x4d);
713 }
714 let mut w = Wall::parse(&b);
715 if w.extra > 0 {
716 let xb = c.take(xwall_size)?;
717 w.extra = m.xwalls.len() as i16;
718 m.xwalls.push(XWall::parse(xb));
719 } else {
720 w.extra = -1;
721 }
722 m.walls.push(w);
723 }
724 m.xsprites.push(XSprite::default());
725 for _ in 0..nsprites {
726 let mut b = c.take(SPRITE_SIZE)?.to_vec();
727 if encrypted {
728 map_crypt(&mut b, rev.wrapping_mul(SPRITE_SIZE as u32) | 0x4d);
729 }
730 let mut s = Sprite::parse(&b);
731 if s.extra > 0 {
732 let xb = c.take(xsprite_size)?;
733 s.extra = m.xsprites.len() as i16;
734 m.xsprites.push(XSprite::parse(xb));
735 } else {
736 s.extra = -1;
737 }
738 m.sprites.push(s);
739 }
740 if let Ok(crc) = c.take(4) {
741 m.crc = u32::from_le_bytes(crc.try_into().unwrap());
742 }
743 if c.p != data.len() {
744 return Err(invalid(format!(
745 "map parse ended at {} of {} bytes (layout mismatch)",
746 c.p,
747 data.len()
748 )));
749 }
750 m.validate()?;
751 Ok(m)
752 }
753
754 pub fn validate(&self) -> io::Result<()> {
756 let nw = self.walls.len() as i32;
757 let ns = self.sectors.len() as i32;
758 for (i, s) in self.sectors.iter().enumerate() {
759 let a = s.wallptr as i32;
760 if a < 0 || a + s.wallnum as i32 > nw {
761 return Err(invalid(format!("sector {i} walls out of range")));
762 }
763 }
764 for (i, w) in self.walls.iter().enumerate() {
765 if w.point2 < 0 || w.point2 as i32 >= nw {
766 return Err(invalid(format!("wall {i} point2 out of range")));
767 }
768 if w.nextsector >= ns as i16 || w.nextwall >= nw as i16 {
769 return Err(invalid(format!("wall {i} portal out of range")));
770 }
771 }
772 Ok(())
773 }
774
775 pub fn xsector(&self, sect: usize) -> Option<&XSector> {
776 let e = self.sectors.get(sect)?.extra;
777 (e > 0).then(|| &self.xsectors[e as usize])
778 }
779 pub fn xwall(&self, wall: usize) -> Option<&XWall> {
780 let e = self.walls.get(wall)?.extra;
781 (e > 0).then(|| &self.xwalls[e as usize])
782 }
783 pub fn xsprite(&self, spr: usize) -> Option<&XSprite> {
784 let e = self.sprites.get(spr)?.extra;
785 (e > 0).then(|| &self.xsprites[e as usize])
786 }
787}
788
789#[cfg(test)]
790mod tests {
791 use super::*;
792
793 #[test]
794 fn bitreader_signed_and_unsigned() {
795 let data = [0b1010_1101u8, 0b0000_0011];
796 let mut r = BitReader::new(&data);
797 assert_eq!(r.u(3), 0b101);
798 assert_eq!(r.s(3), -3); assert_eq!(r.u(4), 0b1110);
800 }
801
802 #[test]
803 fn xstruct_bit_totals() {
804 let ones = [0xffu8; 60];
806 let xs = XSector::parse(&ones);
807 assert!(xs.bob_rotate);
808 let xw = XWall::parse(&ones[..24]);
809 assert!(xw.dude_lockout);
810 let xp = XSprite::parse(&ones[..56]);
811 assert_eq!(xp.state_timer, 0xffff);
812 }
813}