@@ -6,14 +6,12 @@ use glam::Vec3;
6
6
use hyperion_inventory:: PlayerInventory ;
7
7
use hyperion_utils:: EntityExt ;
8
8
use tracing:: { error, info_span} ;
9
- use valence_protocol:: { ByteAngle , RawBytes , VarInt , packets:: play} ;
9
+ use valence_protocol:: { RawBytes , VarInt , packets:: play} ;
10
10
11
11
use crate :: {
12
12
Prev ,
13
13
net:: { Compose , ConnectionId } ,
14
- simulation:: {
15
- Pitch , Position , Velocity , Xp , Yaw , animation:: ActiveAnimation , metadata:: MetadataChanges ,
16
- } ,
14
+ simulation:: { Position , Velocity , Xp , animation:: ActiveAnimation , metadata:: MetadataChanges } ,
17
15
system_registry:: { SYNC_ENTITY_POSITION , SystemId } ,
18
16
util:: TracingExt ,
19
17
} ;
@@ -224,96 +222,59 @@ impl Module for EntityStateSyncModule {
224
222
} ,
225
223
) ;
226
224
225
+ // Add a new system specifically for projectiles (arrows)
227
226
system ! (
228
- "entity_velocity_sync" ,
229
- world,
230
- & Compose ( $) ,
231
- & Velocity ,
232
- )
233
- . multi_threaded ( )
234
- . kind :: < flecs:: pipeline:: OnStore > ( )
235
- . tracing_each_entity (
236
- info_span ! ( "entity_velocity_sync" ) ,
237
- move |entity, ( compose, velocity) | {
238
- let run = || {
239
- let entity_id = VarInt ( entity. minecraft_id ( ) ) ;
240
- let world = entity. world ( ) ;
241
-
242
- if velocity. velocity != Vec3 :: ZERO {
243
- let pkt = play:: EntityVelocityUpdateS2c {
244
- entity_id,
245
- velocity : ( * velocity) . try_into ( ) ?,
246
- } ;
247
-
248
- compose. broadcast ( & pkt, system_id) . send ( & world) ?;
249
- }
250
-
251
- anyhow:: Ok ( ( ) )
252
- } ;
253
-
254
- if let Err ( e) = run ( ) {
255
- error ! ( "failed to run velocity sync: {e}" ) ;
256
- }
257
- } ,
258
- ) ;
259
-
260
- system ! (
261
- "entity_state_sync" ,
227
+ "projectile_sync" ,
262
228
world,
263
229
& Compose ( $) ,
264
230
& Position ,
265
- & Yaw ,
266
- & Pitch ,
267
- ?& ConnectionId ,
231
+ & ( Prev , Position ) ,
232
+ & mut Velocity ,
268
233
)
269
234
. multi_threaded ( )
270
- . kind :: < flecs:: pipeline:: OnStore > ( )
235
+ . kind :: < flecs:: pipeline:: PreStore > ( )
271
236
. tracing_each_entity (
272
- info_span ! ( "entity_state_sync" ) ,
273
- move |entity, ( compose, position, yaw, pitch, io) | {
274
- let run = || {
275
- let entity_id = VarInt ( entity. minecraft_id ( ) ) ;
276
-
277
- let io = io. copied ( ) ;
278
-
279
- let world = entity. world ( ) ;
237
+ info_span ! ( "projectile_sync" ) ,
238
+ move |entity, ( compose, position, previous_position, velocity) | {
239
+ let entity_id = VarInt ( entity. minecraft_id ( ) ) ;
240
+ let world = entity. world ( ) ;
241
+ let chunk_pos = position. to_chunk ( ) ;
280
242
281
- let chunk_pos = position. to_chunk ( ) ;
243
+ let position_delta = * * position - * * previous_position;
244
+ let needs_teleport = position_delta. abs ( ) . max_element ( ) >= 8.0 ;
245
+ let changed_position = * * position != * * previous_position;
282
246
283
- let pkt = play:: EntityPositionS2c {
247
+ if changed_position && !needs_teleport {
248
+ let pkt = play:: MoveRelativeS2c {
284
249
entity_id,
285
- position : position. as_dvec3 ( ) ,
286
- yaw : ByteAngle :: from_degrees ( * * yaw) ,
287
- pitch : ByteAngle :: from_degrees ( * * pitch) ,
250
+ #[ allow( clippy:: cast_possible_truncation) ]
251
+ delta : ( position_delta * 4096.0 ) . to_array ( ) . map ( |x| x as i16 ) ,
288
252
on_ground : false ,
289
253
} ;
290
254
291
255
compose
292
256
. broadcast_local ( & pkt, chunk_pos, system_id)
293
- . exclude ( io)
294
- . send ( & world) ?;
257
+ . send ( & world)
258
+ . unwrap ( ) ;
259
+ }
295
260
296
- // todo: unsure if we always want to set this
297
- let pkt = play:: EntitySetHeadYawS2c {
261
+ // Sync velocity if non-zero
262
+ if velocity. velocity != Vec3 :: ZERO {
263
+ let pkt = play:: EntityVelocityUpdateS2c {
298
264
entity_id,
299
- head_yaw : ByteAngle :: from_degrees ( * * yaw) ,
265
+ velocity : ( * velocity) . try_into ( ) . unwrap_or_else ( |_| {
266
+ Velocity :: ZERO
267
+ . try_into ( )
268
+ . expect ( "failed to convert velocity to i16" )
269
+ } ) ,
300
270
} ;
301
271
302
- compose
303
- . broadcast ( & pkt, system_id)
304
- . exclude ( io)
305
- . send ( & world) ?;
306
-
307
- anyhow:: Ok ( ( ) )
308
- } ;
309
- if let Err ( e) = run ( ) {
310
- error ! ( "failed to run sync_position: {e}" ) ;
272
+ compose. broadcast ( & pkt, system_id) . send ( & world) . unwrap ( ) ;
273
+ // velocity.velocity = Vec3::ZERO;
311
274
}
312
275
} ,
313
276
) ;
314
277
315
278
track_previous :: < Position > ( world) ;
316
- track_previous :: < Yaw > ( world) ;
317
- track_previous :: < Pitch > ( world) ;
318
279
}
319
280
}
0 commit comments