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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
use std::collections::{HashMap, HashSet};
use serde::{Serialize, Deserialize};
use strum_macros::{EnumString, Display};
use specs::Builder;
use std::str::FromStr;
use crate::{
PlayerId,
RoomId,
ItemId,
Sprite,
playerstate::RoomPos,
components::{
AttackType,
Clan,
Flag,
Trigger
},
parameter::{Parameter},
fromtoparameter::FromToParameter,
Timestamp,
Template,
Pos,
Result,
aerr,
errors
};
macro_rules! components {
(post: $($comp: ident ($($paramname: ident : $paramtype: ty, $extraction: expr),*) $creation: expr);*;) => {
#[derive(Clone)]
pub enum ComponentWrapper{
$(
$comp(crate::components::$comp),
)*
}
impl ComponentWrapper {
pub fn build<A: Builder>(&self, builder: A ) -> A {
match self.clone() {
$(
Self::$comp(c) => builder.with(c),
)*
}
}
pub fn load_component(comptype: ComponentType, mut parameters: HashMap<&str, Parameter>) -> Result<Self> {
#[allow(unused_imports, unreachable_code)]
match comptype {
$(
ComponentType::$comp => Ok(Self::$comp({
use crate::components::$comp;
$(
let $paramname = {
let param = parameters
.remove(stringify!($paramname))
.ok_or(aerr!("required parameter '{}'not found", stringify!($paramname)))?;
<$paramtype>::from_parameter(param.clone())
.ok_or(aerr!("parameter {} is invalid type: {:?} is not of type {}", stringify!($paramname), param, stringify!($paramtype)))?
};
)*
$creation
})),
)*
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, EnumString, Display)]
pub enum ComponentType {
$(
$comp,
)*
}
impl ComponentType {
pub fn parameters(&self) -> Vec<&str> {
match self {
$(
Self::$comp => {
#[allow(unused_mut)]
let mut h = Vec::new();
$(
h.push(stringify!($paramname));
)*
h
},
)*
}
}
}
use specs::{World, Entity, WorldExt};
pub fn extract_parameter(typ: ComponentType, parameter: &str, world: &World, ent: Entity) -> Option<Parameter> {
match typ {
$(
#[allow(path_statements)]
ComponentType::$comp => {
$(
if parameter == stringify!($paramname) {
#[allow(unreachable_code, non_snake_case)]
return Some({
let components = world.read_component::<crate::components::$comp>();
#[allow(unused_variables)]
let $comp = components.get(ent)?;
#[allow(unused_variables)]
let extracted: $paramtype = ({$extraction});
return Some(extracted.to_parameter())
})
}
)*
None::<Parameter>
}
)*
}
}
};
// no parameters: make unit struct
(pre: ($($done: tt)*) $comp: ident; $($tail:tt)*) => {
components!(pre: ($($done)* $comp () {$comp};) $($tail)*);
};
// struct is just parameters
(pre: ($($done: tt)*) $comp: ident ($($paramname: ident : $paramtype: ty),*);$($tail:tt)*) => {
components!(pre: ($($done)* $comp ($($paramname : $paramtype, {$comp.$paramname.clone()}),*) {$comp{$($paramname,)*}};) $($tail)*);
};
// full definition minus variable exraction
(pre: ($($done: tt)*) $comp: ident ($($paramname: ident : $paramtype: ty),*) $creation: expr; $($tail:tt)*) => {
components!(pre: ($($done)* $comp ($($paramname : $paramtype, {None?}),*) $creation;) $($tail)*);
};
// full definition
(pre: ($($done: tt)*) $comp: ident ($($paramname: ident : $paramtype: ty, ($extraction: expr)),*) $creation: expr; $($tail:tt)*) => {
components!(pre: ($($done)* $comp ($($paramname : $paramtype, $extraction),*) $creation;) $($tail)*);
};
(pre: ($($done: tt)*)) => {
components!(post: $($done)*);
};
(all: $($all: tt)*) => {components!(pre: () $($all)*);};
}
components!(all:
Visible (name: String, sprite: Sprite, height: f64);
Movable (cooldown: i64);
Player (name: PlayerId) {Player::new(name)};
Item (item: ItemId) {Item(item)};
Inventory () {panic!("inventory from parameters not implemented")};
Health (health: i64, maxhealth: i64);
Serialise () {panic!("serialise from parameters not implemented")};
RoomExit (destination: RoomId, dest_pos: String) {
RoomExit {
destination,
dest_pos: if dest_pos.is_empty() {
RoomPos::Unknown
} else {
RoomPos::Name(dest_pos)
}
}
};
Trap (damage: i64) {Trap{attack: AttackType::Attack(damage)}};
Fighter (damage: i64, cooldown: i64) {Fighter{attack: AttackType::Attack(damage), cooldown, range: 1}};
Healing (delay: i64, health: i64) {Healing{delay, health, next_heal: None}};
Autofight () {Autofight::default()};
MonsterAI (move_chance: f64, view_distance: i64, homesickness: f64);
Spawner (amount: i64, clan: String, template: Template, radius: i64) {
Spawner{
amount: amount as usize,
clan: Clan{name: clan},
template: template,
saturated: false,
radius
}
};
Clan (name: String);
Home (home: Pos);
Faction (faction: String) {Faction::from_str(faction.as_str()).ok_or(aerr!("invalid faction name"))?};
Interactable (typ: String, arg: Parameter) {
Interactable::parse_from_parameter(&typ, &arg).ok_or(aerr!("invalid interaction {:?} {:?}", typ, arg))?
};
Loot (loot: Vec<(Template, f64)>);
Timer (trigger: Trigger, delay: i64, spread: f64, target_time: Option<Timestamp>);
Equipment () {panic!("equipment from parameters not implemented")};
TimeOffset (dtime: i64);
Flags (flags: Vec<String>) {
Flags(
flags
.iter()
.map(|s| Flag::from_str(s).ok())
.collect::<Option<HashSet<Flag>>>().ok_or(aerr!("invalid flag name"))?
)
};
Ear () {Ear::default()};
Build (obj: Template);
Whitelist (allowed: HashMap<String, HashSet<PlayerId>>);
Minable (trigger: String, total: i64) {
Minable {
trigger: Trigger::from_str(&trigger).map_err(|_|aerr!("invalid trigger name {}", trigger))?,
progress: 0,
total
}
};
Removed;
LootHolder () {panic!("LootHolder from parameters not implemented")};
OnSpawn (trigger: String) {
OnSpawn {
trigger: Trigger::from_str(&trigger).map_err(|_|aerr!("invalid trigger name {}", trigger))?
}
};
Substitute (into: Template);
TriggerBox (triggers: Vec<String>) {
TriggerBox {
messages: triggers.iter().map(|s|
Trigger::from_str(s).map_err(|_|aerr!("invalid trigger name {}", s))
).collect::<std::result::Result<Vec<Trigger>, std::boxed::Box<errors::AError>>>()?
}
};
);
pub type PreEntity = Vec<ComponentWrapper>;
|