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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! Contains an "x macro" for all synced components as well as [NetSync]
//! implementations for those components.
//!
//!
//! An x macro accepts another macro as input and calls it with a list of
//! inputs. This allows adding components to the list in the x macro declaration
//! and then writing macros that will accept this list and generate code that
//! handles every synced component without further repitition of the component
//! set.
//!
//! This module also re-exports all the component types that are synced.
//!
//! A glob import from this can be used so that the component types are in scope
//! when using the x macro defined here which requires this.

/// This provides a lowercase name and the component type.
///
/// See [module](self) level docs for more details.
#[macro_export]
macro_rules! synced_components {
    ($macro:ident) => {
        $macro! {
            body: Body,
            stats: Stats,
            buffs: Buffs,
            auras: Auras,
            energy: Energy,
            health: Health,
            poise: Poise,
            light_emitter: LightEmitter,
            loot_owner: LootOwner,
            item: PickupItem,
            scale: Scale,
            group: Group,
            is_mount: IsMount,
            is_rider: IsRider,
            is_volume_rider: IsVolumeRider,
            is_leader: IsLeader,
            is_follower: IsFollower,
            mass: Mass,
            density: Density,
            collider: Collider,
            sticky: Sticky,
            immovable: Immovable,
            character_state: CharacterState,
            character_activity: CharacterActivity,
            shockwave: Shockwave,
            beam: Beam,
            alignment: Alignment,
            stance: Stance,
            // TODO: change this to `SyncFrom::ClientEntity` and sync the bare minimum
            // from other entities (e.g. just keys needed to show appearance
            // based on their loadout). Also, it looks like this actually has
            // an alternative sync method implemented in entity_sync via
            // ServerGeneral::InventoryUpdate so we could use that instead
            // or remove the part where it clones the inventory.
            inventory: Inventory,
            // TODO: this is used in combat rating calculation in voxygen but we can probably
            // remove it from that and then see if it's used for anything else and try to move
            // to only being synced for the client's entity.
            skill_set: SkillSet,

            // Synced to the client only for its own entity

            admin: Admin,
            combo: Combo,
            active_abilities: ActiveAbilities,
            can_build: CanBuild,
            object: Object,
        }
    };
}

macro_rules! reexport_comps {
    ($($name:ident: $type:ident,)*) => {
        mod inner {
            pub use common::comp::*;
            use common::link::Is;
            use common::{
                mounting::{Mount, Rider, VolumeRider},
                tether::{Leader, Follower},
            };

            // We alias these because the identifier used for the
            // component's type is reused as an enum variant name
            // in the macro's that we pass to `synced_components!`.
            //
            // This is also the reason we need this inner module, since
            // we can't just re-export all the types directly from `common::comp`.
            pub type IsMount = Is<Mount>;
            pub type IsRider = Is<Rider>;
            pub type IsVolumeRider = Is<VolumeRider>;
            pub type IsLeader = Is<Leader>;
            pub type IsFollower = Is<Follower>;
        }

        // Re-export all the component types. So that uses of `synced_components!` outside this
        // module can bring them into scope with a single glob import.
        $(pub use inner::$type;)*
    }
}
// Pass `reexport_comps` macro to the "x macro" which will invoke it with a list
// of components.
//
// Note: this brings all these components into scope for the implementations
// below.
synced_components!(reexport_comps);

// ===============================
// === NetSync implementations ===
// ===============================

use crate::sync::{NetSync, SyncFrom};

impl NetSync for Body {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Stats {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Buffs {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Auras {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Energy {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Health {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;

    fn pre_insert(&mut self, world: &specs::World) {
        use common::resources::Time;
        use specs::WorldExt;

        // Time isn't synced between client and server so replace the Time from the
        // server with the Client's local Time to enable accurate comparison.
        self.last_change.time = *world.read_resource::<Time>();
    }

    fn pre_modify(&mut self, world: &specs::World) {
        use common::resources::Time;
        use specs::WorldExt;

        // Time isn't synced between client and server so replace the Time from the
        // server with the Client's local Time to enable accurate comparison.
        self.last_change.time = *world.read_resource::<Time>();
    }
}

impl NetSync for Poise {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for LightEmitter {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for LootOwner {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for PickupItem {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Scale {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Group {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for IsMount {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for IsRider {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for IsVolumeRider {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for IsLeader {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for IsFollower {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Mass {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Density {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Collider {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Sticky {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Immovable {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for CharacterState {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for CharacterActivity {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Shockwave {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Beam {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Alignment {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Inventory {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for SkillSet {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

impl NetSync for Stance {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}

// These are synced only from the client's own entity.

impl NetSync for Admin {
    const SYNC_FROM: SyncFrom = SyncFrom::ClientEntity;
}

impl NetSync for Combo {
    const SYNC_FROM: SyncFrom = SyncFrom::ClientEntity;
}

impl NetSync for ActiveAbilities {
    const SYNC_FROM: SyncFrom = SyncFrom::ClientEntity;
}

impl NetSync for CanBuild {
    const SYNC_FROM: SyncFrom = SyncFrom::ClientEntity;
}

impl NetSync for Object {
    const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity;
}