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
use std::any::{Any, type_name};

use crate::block::{BlockLogic, DeserializeError, SerializeError};
use crate::data::dynamic::DynData;

macro_rules!gen_state_empty
{
	() =>
	{
		fn data_from_i32(&self, _: i32) -> DynData
		{
			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 serialize_state(&self, _: &dyn Any) -> Result<DynData, SerializeError>
		{
			Ok(DynData::Empty)
		}
	};
}

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 struct SimpleBlock
{
	size: u8,
	symmetric: bool,
}

impl SimpleBlock
{
	pub const fn new(size: u8, symmetric: bool) -> Self
	{
		Self{size, symmetric}
	}
}

impl BlockLogic for SimpleBlock
{
	fn get_size(&self) -> u8
	{
		self.size
	}
	
	fn is_symmetric(&self) -> bool
	{
		self.symmetric
	}
	
	gen_state_empty!();
}