summaryrefslogtreecommitdiff
path: root/src/to_clt/map.rs
blob: 296a23f0dac6b2d4a6b7cad50999ee4b6d02688c (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
71
use super::*;
use mt_ser::{DeserializeError, SerializeError};

#[mt_derive(to = "clt", repr = "u8", enumset)]
pub enum MapBlockFlag {
    IsUnderground = 0,
    DayNightDiff,
    LightExpired,
    NotGenerated,
}

pub const ALWAYS_LIT_FROM: u16 = 0xf000;

pub const CONTENT_UNKNOWN: u16 = 125;
pub const CONTENT_AIR: u16 = 126;
pub const CONTENT_IGNORE: u16 = 127;

#[mt_derive(to = "clt")]
pub struct NodeMetaField {
    #[mt(len = "u32")]
    value: String,
    private: bool,
}

#[mt_derive(to = "clt")]
pub struct NodeMeta {
    #[mt(len = "u32")]
    fields: HashMap<String, NodeMetaField>,
    inv: Inventory,
}

#[derive(Debug)]
pub struct NodeMetasLen;

impl MtCfg for NodeMetasLen {
    type Len = <DefCfg as MtCfg>::Len;
    type Inner = <DefCfg as MtCfg>::Inner;

    fn write_len(len: usize, writer: &mut impl std::io::Write) -> Result<(), SerializeError> {
        if len == 0 {
            0u8.mt_serialize::<DefCfg>(writer)
        } else {
            2u8.mt_serialize::<DefCfg>(writer)?;
            DefCfg::write_len(len, writer)
        }
    }

    fn read_len(reader: &mut impl std::io::Read) -> Result<Self::Len, DeserializeError> {
        match u8::mt_deserialize::<DefCfg>(reader)? {
            0 => Ok(0),
            2 => DefCfg::read_len(reader),
            x => Err(DeserializeError::InvalidEnum("NodeMetasLen", Box::new(x))),
        }
    }
}

#[mt_derive(to = "clt")]
pub struct MapBlock {
    pub flags: EnumSet<MapBlockFlag>,
    pub lit_from: u16,
    #[mt(const_before = "2u8")] // param0 size
    #[mt(const_before = "2u8")] // param1 size + param2 size
    #[serde(with = "serde_arrays")]
    pub param_0: [u16; 4096],
    #[serde(with = "serde_arrays")]
    pub param_1: [u8; 4096],
    #[serde(with = "serde_arrays")]
    pub param_2: [u8; 4096],
    #[mt(len = "NodeMetasLen")]
    pub metas: HashMap<u16, NodeMeta>,
}