aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/q3/visdata.rs
blob: 992a25bdef102e881dac26013e5a19b28040fe44 (plain)
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
/*
 * Copyright (C) Oscar Shrimpton 2020
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

//! Parses visdata from Q3 BSPs.

use bitvec::prelude::*;
use std::vec::IntoIter;

use super::file::Q3BspFile;
use crate::coords::CoordSystem;
use crate::helpers::slice_to_i32;
use crate::traits::visdata::*;
use crate::types::{ParseError, Result};

/// Stores cluster-to-cluster visibility information.
pub fn from_data(data: &[u8]) -> Result<Box<[BitBox<Local, u8>]>> {
    if data.len() < 8 {
        return Err(ParseError::Invalid);
    }

    let n_vecs = slice_to_i32(&data[0..4]) as usize;
    let size_vecs = slice_to_i32(&data[4..8]) as usize;

    if data.len() - 8 != (n_vecs * size_vecs) {
        return Err(ParseError::Invalid);
    }

    let mut vecs = Vec::with_capacity(n_vecs);
    for n in 0..n_vecs {
        let offset = 8 + (n * size_vecs);
        let slice = &data[offset..offset + size_vecs];
        vecs.push(BitBox::from_slice(slice));
    }

    Ok(vecs.into_boxed_slice())
}

impl<T: CoordSystem> HasVisData for Q3BspFile<T> {
    type VisibleIterator = IntoIter<ClusterId>;

    fn all_visible_from(&self, from: ClusterId) -> Self::VisibleIterator {
        let mut visible = vec![];

        for (idx, val) in self.visdata[from as usize].iter().enumerate() {
            if *val {
                visible.push(idx as u32);
            }
        }

        visible.into_iter()
    }

    fn cluster_visible_from(&self, from: ClusterId, dest: ClusterId) -> bool {
        self.visdata[from as usize][dest as usize]
    }
}