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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::any::Any;
use std::error::Error;
use std::fmt;

use crate::block::{BlockLogic, DataConvertError, DeserializeError, make_register, SerializeError};
use crate::block::simple::{SimpleBlock, state_impl};
use crate::content;
use crate::data::GridPos;
use crate::data::dynamic::{DynData, DynType};
use crate::item;

make_register!
(
	CONVEYOR: "conveyor" => SimpleBlock::new(1, false);
	TITANIUM_CONVEYOR: "titanium-conveyor" => SimpleBlock::new(1, false);
	PLASTANIUM_CONVEYOR: "plastanium-conveyor" => SimpleBlock::new(1, false);
	ARMORED_CONVEYOR: "armored-conveyor" => SimpleBlock::new(1, false);
	JUNCTION: "junction" => SimpleBlock::new(1, true);
	BRIDGE_CONVEYOR: "bridge-conveyor" => SimpleBlock::new(1, false); // TODO config: destination
	PHASE_CONVEYOR: "phase-conveyor" => SimpleBlock::new(1, false); // TODO config: destination
	SORTER: "sorter" => ItemBlock::new(1, true);
	INVERTED_SORTER: "inverted-sorter" => ItemBlock::new(1, true);
	ROUTER: "router" => SimpleBlock::new(1, true);
	DISTRIBUTOR: "distributor" => SimpleBlock::new(2, true);
	OVERFLOW_GATE: "overflow-gate" => SimpleBlock::new(1, true);
	UNDERFLOW_GATE: "underflow-gate" => SimpleBlock::new(1, true);
	MASS_DRIVER: "mass-driver" => SimpleBlock::new(3, true); // TODO config: destination
	// sandbox only
	ITEM_SOURCE: "item-source" => ItemBlock::new(1, true);
	ITEM_VOID: "item-void" => SimpleBlock::new(1, true);
);

pub struct ItemBlock
{
	size: u8,
	symmetric: bool,
}

impl ItemBlock
{
	pub const fn new(size: u8, symmetric: bool) -> Self
	{
		if size == 0
		{
			panic!("invalid size");
		}
		Self{size, symmetric}
	}
	
	state_impl!(pub Option<item::Type>);
}

impl BlockLogic for ItemBlock
{
	fn get_size(&self) -> u8
	{
		self.size
	}
	
	fn is_symmetric(&self) -> bool
	{
		self.symmetric
	}
	
	fn data_from_i32(&self, config: i32, _: GridPos) -> Result<DynData, DataConvertError>
	{
		if config < 0 || config > u16::MAX as i32
		{
			return Err(DataConvertError(Box::new(ItemConvertError(config))));
		}
		Ok(DynData::Content(content::Type::Item, config as u16))
	}
	
	fn deserialize_state(&self, data: DynData) -> Result<Option<Box<dyn Any>>, DeserializeError>
	{
		match data
		{
			DynData::Empty => Ok(Some(Self::create_state(None))),
			DynData::Content(content::Type::Item, id) => Ok(Some(Self::create_state(Some(ItemDeserializeError::forward(item::Type::try_from(id))?)))),
			DynData::Content(have, ..) => Err(DeserializeError::Custom(Box::new(ItemDeserializeError::ContentType(have)))),
			_ => Err(DeserializeError::InvalidType{have: data.get_type(), expect: DynType::Content}),
		}
	}
	
	fn clone_state(&self, state: &dyn Any) -> Box<dyn Any>
	{
		let state = Self::get_state(state);
		Box::new(Self::create_state(*state))
	}
	
	fn serialize_state(&self, state: &dyn Any) -> Result<DynData, SerializeError>
	{
		match Self::get_state(state)
		{
			None => Ok(DynData::Empty),
			Some(item) => Ok(DynData::Content(content::Type::Item, (*item).into())),
		}
	}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ItemConvertError(pub i32);

impl fmt::Display for ItemConvertError
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
	{
		write!(f, "invalid config ({}) for item", self.0)
	}
}

impl Error for ItemConvertError {}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ItemDeserializeError
{
	ContentType(content::Type),
	NotFound(item::TryFromU16Error),
}

impl ItemDeserializeError
{
	pub fn forward<T, E: Into<Self>>(result: Result<T, E>) -> Result<T, DeserializeError>
	{
		match result
		{
			Ok(v) => Ok(v),
			Err(e) => Err(DeserializeError::Custom(Box::new(e.into()))),
		}
	}
}

impl From<item::TryFromU16Error> for ItemDeserializeError
{
	fn from(err: item::TryFromU16Error) -> Self
	{
		Self::NotFound(err)
	}
}

impl fmt::Display for ItemDeserializeError
{
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
	{
		match self
		{
			Self::ContentType(have) => write!(f, "expected content {:?} but got {have:?}", content::Type::Item),
			Self::NotFound(e) => e.fmt(f),
		}
	}
}

impl Error for ItemDeserializeError
{
	fn source(&self) -> Option<&(dyn Error + 'static)>
	{
		match self
		{
			Self::NotFound(e) => Some(e),
			_ => None,
		}
	}
}