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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use derive_new::new;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use traffloat_types::space::{Matrix, Position};
use traffloat_types::units;
use typed_builder::TypedBuilder;
use xylem::Identifiable;
use crate::{building, cargo, gas, liquid, CustomizableName, Schema};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, new, Serialize, Deserialize)]
pub struct NodeId {
inner: u32,
}
#[cfg(feature = "xy")]
impl Identifiable<Schema> for Node {
type Scope = ();
fn id(&self) -> crate::Id<Node> {
crate::Id::new(self.id.inner.try_into().expect("Too many items"))
}
}
codegen::component_depends! {
NodeId = (
NodeId,
building::Id,
CustomizableName,
Position,
units::Portion<units::Hitpoint>,
) + ?()
}
#[derive(Debug, Clone, Getters, CopyGetters, TypedBuilder, Serialize, Deserialize)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize), process))]
pub struct Node {
#[getset(get_copy = "pub")]
#[cfg_attr(feature = "xy", xylem(args(new = true)))]
id: NodeId,
#[getset(get_copy = "pub")]
#[cfg_attr(feature = "xy", xylem(args(import = vec![std::any::TypeId::of::<building::storage::liquid::Def>()])))]
building: building::Id,
#[getset(get = "pub")]
#[cfg_attr(feature = "xy", xylem(default = CustomizableName::Custom(arcstr::literal!(""))))]
name: CustomizableName,
#[getset(get_copy = "pub")]
position: Position,
#[getset(get_copy = "pub")]
#[cfg_attr(feature = "xy", xylem(serde(default)))]
rotation: Matrix,
#[getset(get_copy = "pub")]
hitpoint: units::Hitpoint,
#[getset(get = "pub")]
#[cfg_attr(feature = "xy", xylem(serde(default)))]
cargo: Vec<CargoStorageEntry>,
#[getset(get = "pub")]
#[cfg_attr(feature = "xy", xylem(serde(default)))]
gas: Vec<GasStorageEntry>,
#[getset(get = "pub")]
#[cfg_attr(feature = "xy", xylem(serde(default)))]
liquid: Vec<LiquidStorageEntry>,
}
#[derive(Debug, Clone, Copy, CopyGetters, new, Serialize, Deserialize)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize)))]
pub struct CargoStorageEntry {
#[getset(get_copy = "pub")]
ty: cargo::Id,
#[getset(get_copy = "pub")]
size: units::CargoSize,
}
#[derive(Debug, Clone, Copy, CopyGetters, new, Serialize, Deserialize)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize)))]
pub struct GasStorageEntry {
#[getset(get_copy = "pub")]
ty: gas::Id,
#[getset(get_copy = "pub")]
volume: units::GasVolume,
}
#[derive(Debug, Clone, Copy, Default, CopyGetters, new, Serialize, Deserialize)]
#[cfg_attr(feature = "xy", derive(xylem::Xylem))]
#[cfg_attr(feature = "xy", xylem(derive(Deserialize)))]
pub struct LiquidStorageEntry {
#[getset(get_copy = "pub")]
ty: liquid::Id,
#[getset(get_copy = "pub")]
volume: units::LiquidVolume,
}
#[cfg(feature = "xy")]
pub mod xy {
use std::any::TypeId;
use std::collections::BTreeMap;
use xylem::{Context, DefaultContext, IdArgs, Processable, Xylem};
use super::{Node, NodeId};
use crate::building::xy::BuildingNameMap;
use crate::{building, CustomizableName, Schema};
impl Xylem<Schema> for NodeId {
type From = String;
type Args = IdArgs;
fn convert_impl(
from: Self::From,
context: &mut DefaultContext,
args: &Self::Args,
) -> anyhow::Result<Self> {
let id = crate::Id::<Node>::convert(from, context, args)?;
Ok(NodeId { inner: id.index().try_into().expect("Too many items") })
}
}
#[derive(Default)]
pub struct NodeBuildingMap {
map: BTreeMap<NodeId, building::Id>,
}
impl NodeBuildingMap {
pub fn insert(&mut self, id: NodeId, item: building::Id) { self.map.insert(id, item); }
pub fn get(&self, id: NodeId) -> Option<building::Id> { self.map.get(&id).copied() }
}
impl Processable<Schema> for Node {
fn postprocess(&mut self, context: &mut DefaultContext) -> anyhow::Result<()> {
{
let map =
context.get_mut::<BuildingNameMap, _>(TypeId::of::<()>(), Default::default);
let item = map.get(self.building).expect("Dangling building reference");
self.name = CustomizableName::Original(item.clone());
}
{
let map =
context.get_mut::<NodeBuildingMap, _>(TypeId::of::<()>(), Default::default);
map.insert(self.id, self.building);
}
Ok(())
}
}
}