mindustry logic execution, map- and schematic- parsing and rendering
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
//! type used for basic blocks, eg turrets and factorys
use crate::item;

macro_rules! state_impl {
	($vis:vis $type:ty) => {
		$vis fn get_state(state: &$crate::block::State) -> &$type
		where Self: Sized {
			state.downcast_ref::<$type>().unwrap()
		}

		$vis fn get_state_mut(state: &mut $crate::block::State) -> &mut $type
		where Self: Sized {
			state.downcast_mut::<$type>().unwrap()
		}

		fn create_state(val: $type) -> $crate::block::State
		where Self: Sized {
			Box::new(val)
		}
	};
}

pub(crate) use state_impl;

macro_rules! make_simple {
    ($name: ident, $draw: expr) => {
        pub struct $name {
            size: u8,
            symmetric: bool,
            build_cost: BuildCost,
        }
        impl $name {
            #[must_use]
            pub const fn new(size: u8, symmetric: bool, build_cost: BuildCost) -> Self {
                assert!(size != 0, "invalid size");
                Self {
                    size,
                    symmetric,
                    build_cost,
                }
            }
        }

        use crate::block::{
            impl_block, simple::BuildCost, BlockLogic, DataConvertError, DeserializeError,
            SerializeError, State,
        };
        use crate::data::dynamic::DynData;
        use crate::data::GridPos;
        impl BlockLogic for $name {
            impl_block!();

            fn data_from_i32(&self, _: i32, _: GridPos) -> Result<DynData, DataConvertError> {
                Ok(DynData::Empty)
            }

            fn deserialize_state(&self, _: DynData) -> Result<Option<State>, DeserializeError> {
                Ok(None)
            }

            fn clone_state(&self, _: &State) -> State {
                panic!("{} has no custom state", stringify!($name))
            }

            fn mirror_state(&self, _: &mut State, _: bool, _: bool) {
                panic!("{} has no custom state", stringify!($name));
            }

            fn rotate_state(&self, _: &mut State, _: bool) {
                panic!("{} has no custom state", stringify!($name));
            }

            fn serialize_state(&self, _: &State) -> Result<DynData, SerializeError> {
                Ok(DynData::Empty)
            }

            fn draw(
                &self,
                category: &str,
                name: &str,
                state: Option<&State>,
            ) -> Option<image::RgbaImage> {
                $draw(self, category, name, state)
            }
        }
    };
    ($name: ident) => {
        crate::block::simple::make_simple!($name, |_, _, _, _| { None });
    };
}
pub(crate) use make_simple;

pub type BuildCost = &'static [(item::Type, u32)];

macro_rules! cost {
	($($item:ident: $cnt:expr),+) => {
		&[$((crate::item::Type::$item, $cnt)),*]
	};
}
pub(crate) use cost;