veloren_common_systems/
mount.rs

1use common::{
2    combat::RiderEffects,
3    comp::{
4        Body, Buff, BuffCategory, BuffChange, Buffs, CharacterActivity, CharacterState, Collider,
5        ControlAction, Controller, InputKind, Mass, Ori, PhysicsState, Pos, Scale, Stats, Vel,
6        buff::DestInfo,
7    },
8    event::{BuffEvent, EmitExt},
9    event_emitters,
10    link::Is,
11    mounting::{Mount, Rider, VolumeRider},
12    resources::Time,
13    terrain::TerrainGrid,
14    uid::IdMaps,
15};
16use common_ecs::{Job, Origin, Phase, System};
17use specs::{Entities, Join, LendJoin, Read, ReadExpect, ReadStorage, WriteStorage};
18use vek::*;
19
20event_emitters! {
21    struct Events[EventEmitters] {
22        buff: BuffEvent,
23    }
24}
25
26/// This system is responsible for controlling mounts
27#[derive(Default)]
28pub struct Sys;
29impl<'a> System<'a> for Sys {
30    type SystemData = (
31        Read<'a, IdMaps>,
32        Read<'a, Time>,
33        ReadExpect<'a, TerrainGrid>,
34        Events<'a>,
35        Entities<'a>,
36        WriteStorage<'a, Controller>,
37        ReadStorage<'a, Is<Rider>>,
38        ReadStorage<'a, Is<Mount>>,
39        ReadStorage<'a, Is<VolumeRider>>,
40        WriteStorage<'a, Pos>,
41        WriteStorage<'a, Vel>,
42        WriteStorage<'a, Ori>,
43        WriteStorage<'a, CharacterActivity>,
44        WriteStorage<'a, PhysicsState>,
45        ReadStorage<'a, Body>,
46        ReadStorage<'a, Scale>,
47        ReadStorage<'a, Collider>,
48        ReadStorage<'a, Buffs>,
49        ReadStorage<'a, Stats>,
50        ReadStorage<'a, Mass>,
51        ReadStorage<'a, RiderEffects>,
52        ReadStorage<'a, CharacterState>,
53    );
54
55    const NAME: &'static str = "mount";
56    const ORIGIN: Origin = Origin::Common;
57    const PHASE: Phase = Phase::Create;
58
59    fn run(
60        _job: &mut Job<Self>,
61        (
62            id_maps,
63            time,
64            terrain,
65            events,
66            entities,
67            mut controllers,
68            is_riders,
69            is_mounts,
70            is_volume_riders,
71            mut positions,
72            mut velocities,
73            mut orientations,
74            mut character_activities,
75            mut physics_states,
76            bodies,
77            scales,
78            colliders,
79            buffs,
80            stats,
81            masses,
82            rider_effects,
83            char_states,
84        ): Self::SystemData,
85    ) {
86        let mut emitters = events.get_emitters();
87        // For each mount...
88        for (entity, is_mount, body, rider_effects) in
89            (&entities, &is_mounts, bodies.maybe(), rider_effects.maybe()).join()
90        {
91            let Some(rider_entity) = id_maps.uid_entity(is_mount.rider) else {
92                continue;
93            };
94
95            // Rider effects from mount.
96            if let Some(rider_effects) = rider_effects
97                && let Some(target_buffs) = buffs.get(rider_entity)
98            {
99                for effect in rider_effects.0.iter() {
100                    let emit_buff = !target_buffs.buffs.iter().any(|(_, buff)| {
101                        buff.cat_ids.iter()
102                            .any(|cat_id| matches!(cat_id, BuffCategory::FromLink(link) if link.is_link(is_mount.get_link())))
103                            && buff.kind == effect.kind && buff.data.strength >= effect.data.strength
104                    });
105
106                    if emit_buff {
107                        let dest_info = DestInfo {
108                            stats: stats.get(rider_entity),
109                            mass: masses.get(rider_entity),
110                        };
111                        let mut cat_ids = effect.cat_ids.clone();
112                        cat_ids.push(BuffCategory::FromLink(
113                            is_mount.get_link().downgrade().into_dyn(),
114                        ));
115
116                        emitters.emit(BuffEvent {
117                            entity: rider_entity,
118                            buff_change: BuffChange::Add(Buff::new(
119                                effect.kind,
120                                effect.data,
121                                cat_ids,
122                                common::comp::BuffSource::Character { by: is_mount.mount },
123                                *time,
124                                dest_info,
125                                masses.get(entity),
126                            )),
127                        });
128                    }
129                }
130            }
131            // ...find the rider...
132            let Some(inputs_and_actions) = controllers.get_mut(rider_entity).map(|c| {
133                // Only take inputs and actions from the rider if the mount is not
134                // intelligent (TODO: expand the definition of 'intelligent').
135                if body.is_some_and(|b| !b.has_free_will()) {
136                    let actions = c
137                        .actions
138                        .extract_if(.., |action| match action {
139                            ControlAction::StartInput { input: i, .. }
140                            | ControlAction::CancelInput { input: i } => {
141                                matches!(
142                                    i,
143                                    InputKind::Jump
144                                        | InputKind::WallJump
145                                        | InputKind::Fly
146                                        | InputKind::Roll
147                                )
148                            },
149                            _ => false,
150                        })
151                        .collect();
152                    Some((c.inputs.clone(), actions))
153                } else {
154                    None
155                }
156            }) else {
157                continue;
158            };
159
160            // ...apply the mount's position/ori/velocity to the rider...
161            let pos = positions.get(entity).copied();
162            let ori = orientations.get(entity).copied();
163            let vel = velocities.get(entity).copied();
164            if let (Some(pos), Some(ori), Some(vel)) = (pos, ori, vel) {
165                let mounter_body = bodies.get(rider_entity);
166                let mounting_offset = body.map_or(Vec3::unit_z(), Body::mount_offset)
167                    * scales.get(entity).map_or(1.0, |s| s.0)
168                    + mounter_body.map_or(Vec3::zero(), Body::rider_offset)
169                        * scales.get(rider_entity).map_or(1.0, |s| s.0);
170                let _ =
171                    positions.insert(rider_entity, Pos(pos.0 + ori.to_quat() * mounting_offset));
172
173                // When the rider is doing an activity that requires them to aim
174                // in their look_dir, the mount shouldn't override their ori.
175                let should_set_ori = char_states
176                    .get(rider_entity)
177                    .is_none_or(|cs| !cs.can_look_while_mounted());
178
179                if should_set_ori {
180                    let _ = orientations.insert(rider_entity, ori);
181                }
182
183                let _ = velocities.insert(rider_entity, vel);
184            }
185            // ...and apply the rider's inputs to the mount's controller
186            if let Some((inputs, actions)) = inputs_and_actions
187                && let Some(controller) = controllers.get_mut(entity)
188            {
189                controller.inputs = inputs;
190                controller.actions = actions;
191            }
192        }
193
194        // Since physics state isn't updated while riding we set it to default.
195        // TODO: Could this be done only once when the link is first created? Has to
196        // happen on both server and client.
197        for (physics_state, _) in (
198            &mut physics_states,
199            is_riders.mask() | is_volume_riders.mask(),
200        )
201            .join()
202        {
203            *physics_state = PhysicsState::default();
204        }
205
206        // For each volume rider.
207        for (entity, is_volume_rider) in (&entities, &is_volume_riders).join() {
208            if let Some((mat, _)) = is_volume_rider.pos.get_mount_mat(
209                &terrain,
210                &id_maps,
211                |e| positions.get(e).copied().zip(orientations.get(e).copied()),
212                &colliders,
213            ) {
214                if let Some(pos) = positions.get_mut(entity) {
215                    pos.0 = mat.mul_point(Vec3::zero());
216                }
217                if let Some(ori) = orientations.get_mut(entity) {
218                    *ori = Ori::from_unnormalized_vec(mat.mul_direction(Vec3::unit_y()))
219                        .unwrap_or_default();
220                }
221            }
222            let v = match is_volume_rider.pos.kind {
223                common::mounting::Volume::Terrain => Vec3::zero(),
224                common::mounting::Volume::Entity(uid) => {
225                    if let Some(v) = id_maps.uid_entity(uid).and_then(|e| velocities.get(e)) {
226                        v.0
227                    } else {
228                        Vec3::zero()
229                    }
230                },
231            };
232            if let Some(vel) = velocities.get_mut(entity) {
233                vel.0 = v;
234            }
235
236            // Check if the volume has buffs if they do apply them to the rider via a
237            // BuffEvent
238
239            // TODO: This is code copy of the mounting effects. We can probably consolidate
240            // at some point.
241            if let Some(target_buffs) = buffs.get(entity)
242                && let Some(block_buffs) = is_volume_rider.block.mount_buffs()
243            {
244                for effect in block_buffs.iter() {
245                    let emit_buff = !target_buffs.buffs.iter().any(|(_, buff)| {
246                        buff.cat_ids.iter()
247                            .any(|cat_id| matches!(cat_id, BuffCategory::FromLink(link) if link.is_link(is_volume_rider.get_link())))
248                            && buff.kind == effect.kind && buff.data.strength >= effect.data.strength
249                    });
250
251                    if emit_buff {
252                        let dest_info = DestInfo {
253                            stats: stats.get(entity),
254                            mass: masses.get(entity),
255                        };
256                        let mut cat_ids = effect.cat_ids.clone();
257                        cat_ids.push(BuffCategory::FromLink(
258                            is_volume_rider.get_link().downgrade().into_dyn(),
259                        ));
260
261                        emitters.emit(BuffEvent {
262                            entity,
263                            buff_change: BuffChange::Add(Buff::new(
264                                effect.kind,
265                                effect.data,
266                                cat_ids,
267                                common::comp::BuffSource::Block,
268                                *time,
269                                dest_info,
270                                masses.get(entity),
271                            )),
272                        });
273                    }
274                }
275            }
276
277            let inputs = controllers.get_mut(entity).map(|c| {
278                let actions: Vec<_> = c
279                    .actions
280                    .extract_if(.., |action| match action {
281                        ControlAction::StartInput { input: i, .. }
282                        | ControlAction::CancelInput { input: i } => {
283                            matches!(
284                                i,
285                                InputKind::Jump
286                                    | InputKind::WallJump
287                                    | InputKind::Fly
288                                    | InputKind::Roll
289                            )
290                        },
291                        _ => false,
292                    })
293                    .collect();
294                let inputs = c.inputs.clone();
295
296                (actions, inputs)
297            });
298
299            if is_volume_rider.block.is_controller()
300                && let Some((actions, inputs)) = inputs
301            {
302                if let Some(mut character_activity) = character_activities
303                    .get_mut(entity)
304                    .filter(|c| c.steer_dir != inputs.move_dir.y)
305                {
306                    character_activity.steer_dir = inputs.move_dir.y;
307                }
308                match is_volume_rider.pos.kind {
309                    common::mounting::Volume::Entity(uid) => {
310                        if let Some(controller) =
311                            id_maps.uid_entity(uid).and_then(|e| controllers.get_mut(e))
312                        {
313                            controller.inputs = inputs;
314                            controller.actions = actions;
315                        }
316                    },
317                    common::mounting::Volume::Terrain => {},
318                }
319            }
320        }
321    }
322}