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
101
102
103
104
105
106
107
108
use std::any::{type_name, Any};

use crate::block::{BlockLogic, DataConvertError, DeserializeError, SerializeError};
use crate::data::dynamic::DynData;
use crate::data::GridPos;
use crate::item;
use crate::item::storage::Storage;

macro_rules!state_impl
{
	($vis:vis $type:ty) =>
	{
		$vis fn get_state<'l>(state: &'l dyn Any) -> &'l $type
			where Self: Sized
		{
			state.downcast_ref::<$type>().unwrap()
		}

		$vis fn get_state_mut<'l>(state: &'l mut dyn Any) -> &'l mut $type
			where Self: Sized
		{
			state.downcast_mut::<$type>().unwrap()
		}

		fn create_state(val: $type) -> Box<dyn Any>
			where Self: Sized
		{
			Box::new(val)
		}
	};
}
pub(crate) use state_impl;

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

pub struct SimpleBlock {
    size: u8,
    symmetric: bool,
    build_cost: BuildCost,
}

impl SimpleBlock {
    pub const fn new(size: u8, symmetric: bool, build_cost: BuildCost) -> Self {
        if size == 0 {
            panic!("invalid size");
        }
        Self {
            size,
            symmetric,
            build_cost,
        }
    }
}

impl BlockLogic for SimpleBlock {
    fn get_size(&self) -> u8 {
        self.size
    }

    fn is_symmetric(&self) -> bool {
        self.symmetric
    }

    fn create_build_cost(&self) -> Option<Storage> {
        if !self.build_cost.is_empty() {
            let mut storage = Storage::new();
            for (ty, cnt) in self.build_cost {
                storage.add(*ty, *cnt, u32::MAX);
            }
            Some(storage)
        } else {
            None
        }
    }

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

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

    fn clone_state(&self, _: &dyn Any) -> Box<dyn Any> {
        panic!("{} has no custom state", type_name::<Self>())
    }

    fn mirror_state(&self, _: &mut dyn Any, _: bool, _: bool) {
        panic!("{} has no custom state", type_name::<Self>());
    }

    fn rotate_state(&self, _: &mut dyn Any, _: bool) {
        panic!("{} has no custom state", type_name::<Self>());
    }

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

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