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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
#[cfg(feature = "worldgen")]
use crate::rtsim::RtSim;
use crate::{
    automod::AutoMod,
    chat::ChatExporter,
    client::Client,
    events::{self, shared::update_map_markers},
    persistence::PersistedComponents,
    pet::restore_pet,
    presence::RepositionOnChunkLoad,
    settings::Settings,
    sys::sentinel::DeletedEntities,
    wiring, BattleModeBuffer, SpawnPoint,
};
#[cfg(feature = "worldgen")]
use common::{calendar::Calendar, resources::TimeOfDay, slowjob::SlowJobPool};
use common::{
    character::CharacterId,
    comp::{
        self, item::ItemKind, misc::PortalData, object, ChatType, Content, Group, Inventory,
        LootOwner, Object, Player, Poise, Presence, PresenceKind, BASE_ABILITY_LIMIT,
    },
    link::{Is, Link, LinkHandle},
    mounting::{Mounting, Rider, VolumeMounting, VolumeRider},
    resources::{Secs, Time},
    rtsim::{Actor, RtSimEntity},
    tether::Tethered,
    uid::{IdMaps, Uid},
    util::Dir,
    LoadoutBuilder, ViewDistances,
};
use common_net::{
    msg::{CharacterInfo, PlayerListUpdate, ServerGeneral},
    sync::WorldSyncExt,
};
use common_state::State;
use rand::prelude::*;
use specs::{
    storage::{GenericReadStorage, GenericWriteStorage},
    Builder, Entity as EcsEntity, EntityBuilder as EcsEntityBuilder, Join, WorldExt, WriteStorage,
};
use std::time::{Duration, Instant};
use tracing::{error, trace, warn};
use vek::*;

pub trait StateExt {
    /// Build a non-player character
    fn create_npc(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        stats: comp::Stats,
        skill_set: comp::SkillSet,
        health: Option<comp::Health>,
        poise: Poise,
        inventory: Inventory,
        body: comp::Body,
    ) -> EcsEntityBuilder;
    /// Create an entity with only a position
    fn create_empty(&mut self, pos: comp::Pos) -> EcsEntityBuilder;
    /// Build a static object entity
    fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder;
    /// Create an item drop or merge the item with an existing drop, if a
    /// suitable candidate exists.
    fn create_item_drop(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        vel: comp::Vel,
        item: comp::PickupItem,
        loot_owner: Option<LootOwner>,
    ) -> Option<EcsEntity>;
    fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        ship: comp::ship::Body,
        make_collider: F,
    ) -> EcsEntityBuilder;
    /// Build a projectile
    fn create_projectile(
        &mut self,
        pos: comp::Pos,
        vel: comp::Vel,
        body: comp::Body,
        projectile: comp::Projectile,
    ) -> EcsEntityBuilder;
    /// Build a shockwave entity
    fn create_shockwave(
        &mut self,
        properties: comp::shockwave::Properties,
        pos: comp::Pos,
        ori: comp::Ori,
    ) -> EcsEntityBuilder;
    /// Creates a safezone
    fn create_safezone(&mut self, range: Option<f32>, pos: comp::Pos) -> EcsEntityBuilder;
    fn create_wiring(
        &mut self,
        pos: comp::Pos,
        object: comp::object::Body,
        wiring_element: wiring::WiringElement,
    ) -> EcsEntityBuilder;
    // NOTE: currently only used for testing
    /// Queues chunk generation in the view distance of the persister, this
    /// entity must be built before those chunks are received (the builder
    /// borrows the ecs world so that is kind of impossible in practice)
    #[cfg(feature = "worldgen")]
    fn create_persister(
        &mut self,
        pos: comp::Pos,
        view_distance: u32,
        world: &std::sync::Arc<world::World>,
        index: &world::IndexOwned,
    ) -> EcsEntityBuilder;
    /// Creates a teleporter entity, which allows players to teleport to the
    /// `target` position. You might want to require the teleporting entity
    /// to not have agro for teleporting.
    fn create_teleporter(&mut self, pos: comp::Pos, portal: PortalData) -> EcsEntityBuilder;
    /// Insert common/default components for a new character joining the server
    fn initialize_character_data(
        &mut self,
        entity: EcsEntity,
        character_id: CharacterId,
        view_distances: ViewDistances,
    );
    /// Insert common/default components for a new spectator joining the server
    fn initialize_spectator_data(&mut self, entity: EcsEntity, view_distances: ViewDistances);
    /// Update the components associated with the entity's current character.
    /// Performed after loading component data from the database
    fn update_character_data(
        &mut self,
        entity: EcsEntity,
        components: PersistedComponents,
    ) -> Result<(), String>;
    /// Iterates over registered clients and send each `ServerMsg`
    fn validate_chat_msg(
        &self,
        player: EcsEntity,
        chat_type: &comp::ChatType<comp::Group>,
        msg: &str,
    ) -> bool;
    fn send_chat(&self, msg: comp::UnresolvedChatMsg);
    fn notify_players(&self, msg: ServerGeneral);
    fn notify_in_game_clients(&self, msg: ServerGeneral);
    /// Create a new link between entities (see [`common::mounting`] for an
    /// example).
    fn link<L: Link>(&mut self, link: L) -> Result<(), L::Error>;
    /// Maintain active links between entities
    fn maintain_links(&mut self);
    /// Delete an entity, recording the deletion in [`DeletedEntities`]
    fn delete_entity_recorded(
        &mut self,
        entity: EcsEntity,
    ) -> Result<(), specs::error::WrongGeneration>;
    /// Get the given entity as an [`Actor`], if it is one.
    fn entity_as_actor(&self, entity: EcsEntity) -> Option<Actor>;
    /// Mutate the position of an entity or, if the entity is mounted, the
    /// mount.
    ///
    /// If `dismount_volume` is `true`, an entity mounted on a volume entity
    /// (such as an airship) will be dismounted to avoid teleporting the volume
    /// entity.
    fn position_mut<T>(
        &mut self,
        entity: EcsEntity,
        dismount_volume: bool,
        f: impl for<'a> FnOnce(&'a mut comp::Pos) -> T,
    ) -> Result<T, Content>;
}

impl StateExt for State {
    fn create_npc(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        stats: comp::Stats,
        skill_set: comp::SkillSet,
        health: Option<comp::Health>,
        poise: Poise,
        inventory: Inventory,
        body: comp::Body,
    ) -> EcsEntityBuilder {
        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(comp::Vel(Vec3::zero()))
            .with(ori)
            .with(body.mass())
            .with(body.density())
            .with(body.collider())
            .with(comp::Controller::default())
            .with(body)
            .with(comp::Energy::new(body))
            .with(stats)
            .with(if body.is_humanoid() {
                comp::ActiveAbilities::default_limited(BASE_ABILITY_LIMIT)
            } else {
                comp::ActiveAbilities::default()
            })
            .with(skill_set)
            .maybe_with(health)
            .with(poise)
            .with(comp::Alignment::Npc)
            .with(comp::CharacterState::default())
            .with(comp::CharacterActivity::default())
            .with(inventory)
            .with(comp::Buffs::default())
            .with(comp::Combo::default())
            .with(comp::Auras::default())
            .with(comp::EnteredAuras::default())
            .with(comp::Stance::default())
    }

    fn create_empty(&mut self, pos: comp::Pos) -> EcsEntityBuilder {
        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(comp::Vel(Vec3::zero()))
            .with(comp::Ori::default())
    }

    fn create_object(&mut self, pos: comp::Pos, object: comp::object::Body) -> EcsEntityBuilder {
        let body = comp::Body::Object(object);
        self.create_empty(pos)
            .with(body.mass())
            .with(body.density())
            .with(body.collider())
            .with(body)
    }

    fn create_item_drop(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        vel: comp::Vel,
        world_item: comp::PickupItem,
        loot_owner: Option<LootOwner>,
    ) -> Option<EcsEntity> {
        // Attempt merging with any nearby entities if possible
        {
            use crate::sys::item::get_nearby_mergeable_items;

            let positions = self.ecs().read_storage::<comp::Pos>();
            let loot_owners = self.ecs().read_storage::<LootOwner>();
            let mut items = self.ecs().write_storage::<comp::PickupItem>();
            let entities = self.ecs().entities();
            let spatial_grid = self.ecs().read_resource();

            let nearby_items = get_nearby_mergeable_items(
                &world_item,
                &pos,
                loot_owner.as_ref(),
                (&entities, &items, &positions, &loot_owners, &spatial_grid),
            );

            // Merge the nearest item if possible, skip to creating a drop otherwise
            if let Some((mergeable_item, _)) =
                nearby_items.min_by_key(|(_, dist)| (dist * 1000.0) as i32)
            {
                items
                    .get_mut(mergeable_item)
                    .expect("we know that the item exists")
                    .try_merge(world_item)
                    .expect("`try_merge` should succeed because `can_merge` returned `true`");
                return None;
            }
        }

        let spawned_at = *self.ecs().read_resource::<Time>();

        let item_drop = comp::item_drop::Body::from(world_item.item());
        let body = comp::Body::ItemDrop(item_drop);
        let light_emitter = match &*world_item.item().kind() {
            ItemKind::Lantern(lantern) => Some(comp::LightEmitter {
                col: lantern.color(),
                strength: lantern.strength(),
                flicker: lantern.flicker(),
                animated: true,
            }),
            _ => None,
        };
        Some(
            self.ecs_mut()
                .create_entity_synced()
                .with(world_item)
                .with(pos)
                .with(ori)
                .with(vel)
                .with(item_drop.orientation(&mut thread_rng()))
                .with(item_drop.mass())
                .with(item_drop.density())
                .with(body.collider())
                .with(body)
                .with(Object::DeleteAfter {
                    spawned_at,
                    // Delete the item drop after 5 minutes
                    timeout: Duration::from_secs(300),
                })
                .maybe_with(loot_owner)
                .maybe_with(light_emitter)
                .build(),
        )
    }

    fn create_ship<F: FnOnce(comp::ship::Body) -> comp::Collider>(
        &mut self,
        pos: comp::Pos,
        ori: comp::Ori,
        ship: comp::ship::Body,
        make_collider: F,
    ) -> EcsEntityBuilder {
        let body = comp::Body::Ship(ship);
        let builder = self
            .ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(comp::Vel(Vec3::zero()))
            .with(ori)
            .with(body.mass())
            .with(body.density())
            .with(make_collider(ship))
            .with(body)
            .with(comp::Controller::default())
            .with(Inventory::with_empty())
            .with(comp::CharacterState::default())
            .with(comp::CharacterActivity::default())
            // TODO: some of these are required in order for the character_behavior system to
            // recognize a possesed airship; that system should be refactored to use `.maybe()`
            .with(comp::Energy::new(ship.into()))
            .with(comp::Stats::new("Airship".to_string(), body))
            .with(comp::SkillSet::default())
            .with(comp::ActiveAbilities::default())
            .with(comp::Combo::default());

        builder
    }

    fn create_projectile(
        &mut self,
        pos: comp::Pos,
        vel: comp::Vel,
        body: comp::Body,
        projectile: comp::Projectile,
    ) -> EcsEntityBuilder {
        let mut projectile_base = self
            .ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(vel)
            .with(comp::Ori::from_unnormalized_vec(vel.0).unwrap_or_default())
            .with(body.mass())
            .with(body.density());

        if projectile.is_sticky {
            projectile_base = projectile_base.with(comp::Sticky)
        }
        if projectile.is_point {
            projectile_base = projectile_base.with(comp::Collider::Point)
        } else {
            projectile_base = projectile_base.with(body.collider())
        }

        projectile_base.with(projectile).with(body)
    }

    fn create_shockwave(
        &mut self,
        properties: comp::shockwave::Properties,
        pos: comp::Pos,
        ori: comp::Ori,
    ) -> EcsEntityBuilder {
        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(ori)
            .with(comp::Shockwave {
                properties,
                creation: None,
            })
            .with(comp::ShockwaveHitEntities {
                hit_entities: Vec::<Uid>::new(),
            })
    }

    fn create_safezone(&mut self, range: Option<f32>, pos: comp::Pos) -> EcsEntityBuilder {
        use comp::{
            aura::{Aura, AuraKind, AuraTarget, Auras},
            buff::{BuffCategory, BuffData, BuffKind, BuffSource},
        };
        let time = self.get_time();
        // TODO: Consider using the area system for this
        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(Auras::new(vec![Aura::new(
                AuraKind::Buff {
                    kind: BuffKind::Invulnerability,
                    data: BuffData::new(1.0, Some(Secs(1.0))),
                    category: BuffCategory::Natural,
                    source: BuffSource::World,
                },
                range.unwrap_or(100.0),
                None,
                AuraTarget::All,
                Time(time),
            )]))
    }

    fn create_wiring(
        &mut self,
        pos: comp::Pos,
        object: comp::object::Body,
        wiring_element: wiring::WiringElement,
    ) -> EcsEntityBuilder {
        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(comp::Vel(Vec3::zero()))
            .with(comp::Ori::default())
            .with({
                let body: comp::Body = object.into();
                body.collider()
            })
            .with(comp::Body::Object(object))
            .with(comp::Mass(100.0))
            // .with(comp::Sticky)
            .with(wiring_element)
            .with(comp::LightEmitter {
                col: Rgb::new(0.0, 0.0, 0.0),
                strength: 2.0,
                flicker: 1.0,
                animated: true,
            })
    }

    // NOTE: currently only used for testing
    /// Queues chunk generation in the view distance of the persister, this
    /// entity must be built before those chunks are received (the builder
    /// borrows the ecs world so that is kind of impossible in practice)
    #[cfg(feature = "worldgen")]
    fn create_persister(
        &mut self,
        pos: comp::Pos,
        view_distance: u32,
        world: &std::sync::Arc<world::World>,
        index: &world::IndexOwned,
    ) -> EcsEntityBuilder {
        use common::{terrain::TerrainChunkSize, vol::RectVolSize};
        use std::sync::Arc;
        // Request chunks
        {
            let ecs = self.ecs();
            let slow_jobs = ecs.write_resource::<SlowJobPool>();
            let rtsim = ecs.read_resource::<RtSim>();
            let mut chunk_generator =
                ecs.write_resource::<crate::chunk_generator::ChunkGenerator>();
            let chunk_pos = self.terrain().pos_key(pos.0.map(|e| e as i32));
            (-(view_distance as i32)..view_distance as i32 + 1)
            .flat_map(|x| {
                (-(view_distance as i32)..view_distance as i32 + 1).map(move |y| Vec2::new(x, y))
            })
            .map(|offset| offset + chunk_pos)
            // Filter chunks outside the view distance
            // Note: calculation from client chunk request filtering
            .filter(|chunk_key| {
                pos.0.xy().map(|e| e as f64).distance(
                    chunk_key.map(|e| e as f64 + 0.5) * TerrainChunkSize::RECT_SIZE.map(|e| e as f64),
                ) < (view_distance as f64 - 1.0 + 2.5 * 2.0_f64.sqrt())
                    * TerrainChunkSize::RECT_SIZE.x as f64
            })
            .for_each(|chunk_key| {
                {
                    let time = (*ecs.read_resource::<TimeOfDay>(), (*ecs.read_resource::<Calendar>()).clone());
                    chunk_generator.generate_chunk(None, chunk_key, &slow_jobs, Arc::clone(world), &rtsim, index.clone(), time);
                }
            });
        }

        self.ecs_mut()
            .create_entity_synced()
            .with(pos)
            .with(Presence::new(
                ViewDistances {
                    terrain: view_distance,
                    entity: view_distance,
                },
                PresenceKind::Spectator,
            ))
    }

    fn create_teleporter(&mut self, pos: comp::Pos, portal: PortalData) -> EcsEntityBuilder {
        self.create_object(pos, object::Body::Portal)
            .with(comp::Immovable)
            .with(comp::Object::from(portal))
    }

    fn initialize_character_data(
        &mut self,
        entity: EcsEntity,
        character_id: CharacterId,
        view_distances: ViewDistances,
    ) {
        let spawn_point = self.ecs().read_resource::<SpawnPoint>().0;

        if let Some(player_uid) = self.read_component_copied::<Uid>(entity) {
            // NOTE: By fetching the player_uid, we validated that the entity exists, and we
            // call nothing that can delete it in any of the subsequent
            // commands, so we can assume that all of these calls succeed,
            // justifying ignoring the result of insertion.
            self.write_component_ignore_entity_dead(entity, comp::Controller::default());
            self.write_component_ignore_entity_dead(entity, comp::Pos(spawn_point));
            self.write_component_ignore_entity_dead(entity, comp::Vel(Vec3::zero()));
            self.write_component_ignore_entity_dead(entity, comp::Ori::default());
            self.write_component_ignore_entity_dead(entity, comp::Collider::CapsulePrism {
                p0: Vec2::zero(),
                p1: Vec2::zero(),
                radius: 0.4,
                z_min: 0.0,
                z_max: 1.75,
            });
            self.write_component_ignore_entity_dead(entity, comp::CharacterState::default());
            self.write_component_ignore_entity_dead(entity, comp::CharacterActivity::default());
            self.write_component_ignore_entity_dead(entity, comp::Alignment::Owned(player_uid));
            self.write_component_ignore_entity_dead(entity, comp::Buffs::default());
            self.write_component_ignore_entity_dead(entity, comp::Auras::default());
            self.write_component_ignore_entity_dead(entity, comp::EnteredAuras::default());
            self.write_component_ignore_entity_dead(entity, comp::Combo::default());
            self.write_component_ignore_entity_dead(entity, comp::Stance::default());

            // Make sure physics components are updated
            self.write_component_ignore_entity_dead(entity, comp::ForceUpdate::forced());

            self.write_component_ignore_entity_dead(
                entity,
                Presence::new(view_distances, PresenceKind::LoadingCharacter(character_id)),
            );

            // Tell the client its request was successful.
            if let Some(client) = self.ecs().read_storage::<Client>().get(entity) {
                client.send_fallible(ServerGeneral::CharacterSuccess);
            }
        }
    }

    fn initialize_spectator_data(&mut self, entity: EcsEntity, view_distances: ViewDistances) {
        let spawn_point = self.ecs().read_resource::<SpawnPoint>().0;

        if self.read_component_copied::<Uid>(entity).is_some() {
            // NOTE: By fetching the player_uid, we validated that the entity exists, and we
            // call nothing that can delete it in any of the subsequent
            // commands, so we can assume that all of these calls succeed,
            // justifying ignoring the result of insertion.
            self.write_component_ignore_entity_dead(entity, comp::Pos(spawn_point));

            // Make sure physics components are updated
            self.write_component_ignore_entity_dead(entity, comp::ForceUpdate::forced());

            self.write_component_ignore_entity_dead(
                entity,
                Presence::new(view_distances, PresenceKind::Spectator),
            );

            // Tell the client its request was successful.
            if let Some(client) = self.ecs().read_storage::<Client>().get(entity) {
                client.send_fallible(ServerGeneral::SpectatorSuccess(spawn_point));
            }
        }
    }

    /// Returned error intended to be sent to the client.
    fn update_character_data(
        &mut self,
        entity: EcsEntity,
        components: PersistedComponents,
    ) -> Result<(), String> {
        let PersistedComponents {
            body,
            stats,
            skill_set,
            inventory,
            waypoint,
            pets,
            active_abilities,
            map_marker,
        } = components;

        if let Some(player_uid) = self.read_component_copied::<Uid>(entity) {
            let result =
                if let Some(presence) = self.ecs().write_storage::<Presence>().get_mut(entity) {
                    if let PresenceKind::LoadingCharacter(id) = presence.kind {
                        presence.kind = PresenceKind::Character(id);
                        self.ecs()
                            .write_resource::<IdMaps>()
                            .add_character(id, entity);
                        Ok(())
                    } else {
                        Err("PresenceKind is not LoadingCharacter")
                    }
                } else {
                    Err("Presence component missing")
                };
            if let Err(err) = result {
                let err = format!("Unexpected state when applying loaded character info: {err}");
                error!("{err}");
                // TODO: we could produce a `comp::Content` for this to allow localization.
                return Err(err);
            }

            // Notify clients of a player list update
            self.notify_players(ServerGeneral::PlayerListUpdate(
                PlayerListUpdate::SelectedCharacter(player_uid, CharacterInfo {
                    name: String::from(&stats.name),
                    // NOTE: hack, read docs on body::Gender for more
                    gender: stats.original_body.humanoid_gender(),
                }),
            ));

            // NOTE: By fetching the player_uid, we validated that the entity exists,
            // and we call nothing that can delete it in any of the subsequent
            // commands, so we can assume that all of these calls succeed,
            // justifying ignoring the result of insertion.
            self.write_component_ignore_entity_dead(entity, body.collider());
            self.write_component_ignore_entity_dead(entity, body);
            self.write_component_ignore_entity_dead(entity, body.mass());
            self.write_component_ignore_entity_dead(entity, body.density());
            self.write_component_ignore_entity_dead(entity, comp::Health::new(body));
            self.write_component_ignore_entity_dead(entity, comp::Energy::new(body));
            self.write_component_ignore_entity_dead(entity, Poise::new(body));
            self.write_component_ignore_entity_dead(entity, stats);
            self.write_component_ignore_entity_dead(entity, active_abilities);
            self.write_component_ignore_entity_dead(entity, skill_set);
            self.write_component_ignore_entity_dead(entity, inventory);
            self.write_component_ignore_entity_dead(
                entity,
                comp::InventoryUpdate::new(comp::InventoryUpdateEvent::default()),
            );

            if let Some(waypoint) = waypoint {
                self.write_component_ignore_entity_dead(entity, RepositionOnChunkLoad {
                    needs_ground: true,
                });
                self.write_component_ignore_entity_dead(entity, waypoint);
                self.write_component_ignore_entity_dead(entity, comp::Pos(waypoint.get_pos()));
                self.write_component_ignore_entity_dead(entity, comp::Vel(Vec3::zero()));
                // TODO: We probably want to increment the existing force update counter since
                // it is added in initialized_character (to be robust we can also insert it if
                // it doesn't exist)
                self.write_component_ignore_entity_dead(entity, comp::ForceUpdate::forced());
            }

            if let Some(map_marker) = map_marker {
                self.write_component_ignore_entity_dead(entity, map_marker);
            }

            let player_pos = self.ecs().read_storage::<comp::Pos>().get(entity).copied();
            if let Some(player_pos) = player_pos {
                trace!(
                    "Loading {} pets for player at pos {:?}",
                    pets.len(),
                    player_pos
                );
                let mut rng = rand::thread_rng();

                for (pet, body, stats) in pets {
                    let ori = comp::Ori::from(Dir::random_2d(&mut rng));
                    let pet_entity = self
                        .create_npc(
                            player_pos,
                            ori,
                            stats,
                            comp::SkillSet::default(),
                            Some(comp::Health::new(body)),
                            Poise::new(body),
                            Inventory::with_loadout(
                                LoadoutBuilder::from_default(&body).build(),
                                body,
                            ),
                            body,
                        )
                        .with(comp::Scale(1.0))
                        .with(comp::Vel(Vec3::new(0.0, 0.0, 0.0)))
                        .build();

                    restore_pet(self.ecs(), pet_entity, entity, pet);
                }
            } else {
                error!("Player has no pos, cannot load {} pets", pets.len());
            }

            let presences = self.ecs().read_storage::<Presence>();
            let presence = presences.get(entity);
            if let Some(Presence {
                kind: PresenceKind::Character(char_id),
                ..
            }) = presence
            {
                let battlemode_buffer = self.ecs().fetch::<BattleModeBuffer>();
                let mut players = self.ecs().write_storage::<comp::Player>();
                if let Some((mode, change)) = battlemode_buffer.get(char_id) {
                    if let Some(mut player_info) = players.get_mut(entity) {
                        player_info.battle_mode = *mode;
                        player_info.last_battlemode_change = Some(*change);
                    }
                } else {
                    // TODO: this sounds related to handle_exit_ingame? Actually, sounds like
                    // trying to place character specific info on the `Player` component. TODO
                    // document component better.
                    // FIXME:
                    // ???
                    //
                    // This probably shouldn't exist,
                    // but without this code, character gets battle_mode from
                    // another character on this account.
                    let settings = self.ecs().read_resource::<Settings>();
                    let mode = settings.gameplay.battle_mode.default_mode();
                    if let Some(mut player_info) = players.get_mut(entity) {
                        player_info.battle_mode = mode;
                        player_info.last_battlemode_change = None;
                    }
                }
            }
        }

        Ok(())
    }

    fn validate_chat_msg(
        &self,
        entity: EcsEntity,
        chat_type: &comp::ChatType<comp::Group>,
        msg: &str,
    ) -> bool {
        let mut automod = self.ecs().write_resource::<AutoMod>();
        let client = self.ecs().read_storage::<Client>();
        let player = self.ecs().read_storage::<Player>();
        let Some(client) = client.get(entity) else {
            return true;
        };
        let Some(player) = player.get(entity) else {
            return true;
        };

        match automod.validate_chat_msg(
            player.uuid(),
            self.ecs()
                .read_storage::<comp::Admin>()
                .get(entity)
                .map(|a| a.0),
            Instant::now(),
            chat_type,
            msg,
        ) {
            Ok(note) => {
                if let Some(note) = note {
                    let _ = client.send(ServerGeneral::server_msg(
                        ChatType::CommandInfo,
                        format!("{}", note),
                    ));
                }
                true
            },
            Err(err) => {
                let _ = client.send(ServerGeneral::server_msg(
                    ChatType::CommandError,
                    format!("{}", err),
                ));
                false
            },
        }
    }

    /// Send the chat message to the proper players. Say and region are limited
    /// by location. Faction and group are limited by component.
    fn send_chat(&self, msg: comp::UnresolvedChatMsg) {
        let ecs = self.ecs();
        let is_within =
            |target, a: &comp::Pos, b: &comp::Pos| a.0.distance_squared(b.0) < target * target;

        let group_manager = ecs.read_resource::<comp::group::GroupManager>();
        let chat_exporter = ecs.read_resource::<ChatExporter>();

        let group_info = msg.get_group().and_then(|g| group_manager.group_info(*g));

        if let Some(exported_message) = ChatExporter::generate(&msg, ecs) {
            chat_exporter.send(exported_message);
        }

        let resolved_msg = msg
            .clone()
            .map_group(|_| group_info.map_or_else(|| "???".to_string(), |i| i.name.clone()));

        let id_maps = ecs.read_resource::<IdMaps>();
        let entity_from_uid = |uid| id_maps.uid_entity(uid);

        if msg.chat_type.uid().map_or(true, |sender| {
            entity_from_uid(sender).map_or(false, |e| {
                self.validate_chat_msg(
                    e,
                    &msg.chat_type,
                    msg.content().as_plain().unwrap_or_default(),
                )
            })
        }) {
            match &msg.chat_type {
                comp::ChatType::Offline(_)
                | comp::ChatType::CommandInfo
                | comp::ChatType::CommandError
                | comp::ChatType::Meta
                | comp::ChatType::World(_) => {
                    self.notify_players(ServerGeneral::ChatMsg(resolved_msg))
                },
                comp::ChatType::Online(u) => {
                    for (client, uid) in
                        (&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
                    {
                        if uid != u {
                            client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                        }
                    }
                },
                comp::ChatType::Tell(from, to) => {
                    for (client, uid) in
                        (&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
                    {
                        if uid == from || uid == to {
                            client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                        }
                    }
                },
                comp::ChatType::Kill(kill_source, uid) => {
                    let clients = ecs.read_storage::<Client>();
                    let clients_count = clients.count();
                    // Avoid chat spam, send kill message only to group or nearby players if a
                    // certain amount of clients are online
                    if clients_count
                        > ecs
                            .fetch::<Settings>()
                            .max_player_for_kill_broadcast
                            .unwrap_or_default()
                    {
                        // Send kill message to the dead player's group
                        let killed_entity = entity_from_uid(*uid);
                        let groups = ecs.read_storage::<Group>();
                        let killed_group = killed_entity.and_then(|e| groups.get(e));
                        if let Some(g) = &killed_group {
                            send_to_group(g, ecs, &resolved_msg);
                        }

                        // Send kill message to nearby players that aren't part of the deceased's
                        // group
                        let positions = ecs.read_storage::<comp::Pos>();
                        if let Some(died_player_pos) = killed_entity.and_then(|e| positions.get(e))
                        {
                            for (ent, client, pos) in
                                (&*ecs.entities(), &clients, &positions).join()
                            {
                                let client_group = groups.get(ent);
                                let is_different_group =
                                    !(killed_group == client_group && client_group.is_some());
                                if is_within(comp::ChatMsg::SAY_DISTANCE, pos, died_player_pos)
                                    && is_different_group
                                {
                                    client.send_fallible(ServerGeneral::ChatMsg(
                                        resolved_msg.clone(),
                                    ));
                                }
                            }
                        }
                    } else {
                        self.notify_players(ServerGeneral::server_msg(
                            comp::ChatType::Kill(kill_source.clone(), *uid),
                            msg.into_content(),
                        ))
                    }
                },
                comp::ChatType::Say(uid) => {
                    let entity_opt = entity_from_uid(*uid);

                    let positions = ecs.read_storage::<comp::Pos>();
                    if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
                        for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
                            if is_within(comp::ChatMsg::SAY_DISTANCE, pos, speaker_pos) {
                                client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                            }
                        }
                    }
                },
                comp::ChatType::Region(uid) => {
                    let entity_opt = entity_from_uid(*uid);

                    let positions = ecs.read_storage::<comp::Pos>();
                    if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
                        for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
                            if is_within(comp::ChatMsg::REGION_DISTANCE, pos, speaker_pos) {
                                client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                            }
                        }
                    }
                },
                comp::ChatType::Npc(uid) => {
                    let entity_opt = entity_from_uid(*uid);

                    let positions = ecs.read_storage::<comp::Pos>();
                    if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
                        for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
                            if is_within(comp::ChatMsg::NPC_DISTANCE, pos, speaker_pos) {
                                client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                            }
                        }
                    }
                },
                comp::ChatType::NpcSay(uid) => {
                    let entity_opt = entity_from_uid(*uid);

                    let positions = ecs.read_storage::<comp::Pos>();
                    if let Some(speaker_pos) = entity_opt.and_then(|e| positions.get(e)) {
                        for (client, pos) in (&ecs.read_storage::<Client>(), &positions).join() {
                            if is_within(comp::ChatMsg::NPC_SAY_DISTANCE, pos, speaker_pos) {
                                client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                            }
                        }
                    }
                },
                comp::ChatType::NpcTell(from, to) => {
                    for (client, uid) in
                        (&ecs.read_storage::<Client>(), &ecs.read_storage::<Uid>()).join()
                    {
                        if uid == from || uid == to {
                            client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                        }
                    }
                },
                comp::ChatType::FactionMeta(s) | comp::ChatType::Faction(_, s) => {
                    for (client, faction) in (
                        &ecs.read_storage::<Client>(),
                        &ecs.read_storage::<comp::Faction>(),
                    )
                        .join()
                    {
                        if s == &faction.0 {
                            client.send_fallible(ServerGeneral::ChatMsg(resolved_msg.clone()));
                        }
                    }
                },
                comp::ChatType::Group(from, g) => {
                    if group_info.is_none() {
                        // Group not found, reply with command error
                        // This should usually NEVER happen since now it is checked whether the
                        // sender is still in the group upon emitting the message (TODO: Can this be
                        // triggered if the message is sent in the same tick as the sender is
                        // removed from the group?)

                        let reply = comp::ChatType::CommandError
                            .into_msg(Content::localized("command-message-group-missing"));

                        let clients = ecs.read_storage::<Client>();
                        if let Some(client) =
                            entity_from_uid(*from).and_then(|entity| clients.get(entity))
                        {
                            client.send_fallible(ServerGeneral::ChatMsg(reply));
                        }
                    } else {
                        send_to_group(g, ecs, &resolved_msg);
                    }
                },
                comp::ChatType::GroupMeta(g) => {
                    send_to_group(g, ecs, &resolved_msg);
                },
            }
        }
    }

    /// Sends the message to all connected clients
    fn notify_players(&self, msg: ServerGeneral) {
        let mut msg = Some(msg);
        let mut lazy_msg = None;
        for (client, _) in (
            &self.ecs().read_storage::<Client>(),
            &self.ecs().read_storage::<comp::Player>(),
        )
            .join()
        {
            if let Some(msg) = msg.take() {
                lazy_msg = Some(client.prepare(msg));
            }
            lazy_msg.as_ref().map(|msg| client.send_prepared(msg));
        }
    }

    /// Sends the message to all clients playing in game
    fn notify_in_game_clients(&self, msg: ServerGeneral) {
        let mut msg = Some(msg);
        let mut lazy_msg = None;
        for (client, _) in (
            &mut self.ecs().write_storage::<Client>(),
            &self.ecs().read_storage::<Presence>(),
        )
            .join()
        {
            if let Some(msg) = msg.take() {
                lazy_msg = Some(client.prepare(msg));
            }
            lazy_msg.as_ref().map(|msg| client.send_prepared(msg));
        }
    }

    fn link<L: Link>(&mut self, link: L) -> Result<(), L::Error> {
        let linker = LinkHandle::from_link(link);

        L::create(&linker, &mut self.ecs().system_data())?;

        self.ecs_mut()
            .entry::<Vec<LinkHandle<L>>>()
            .or_insert_with(Vec::new)
            .push(linker);

        Ok(())
    }

    fn maintain_links(&mut self) {
        fn maintain_link<L: Link>(state: &State) {
            if let Some(mut handles) = state.ecs().try_fetch_mut::<Vec<LinkHandle<L>>>() {
                let mut persist_data = None;
                handles.retain(|link| {
                    if L::persist(
                        link,
                        persist_data.get_or_insert_with(|| state.ecs().system_data()),
                    ) {
                        true
                    } else {
                        // Make sure to drop persist data before running deletion to avoid potential
                        // access violations
                        persist_data.take();
                        L::delete(link, &mut state.ecs().system_data());
                        false
                    }
                });
            }
        }

        maintain_link::<Mounting>(self);
        maintain_link::<VolumeMounting>(self);
        maintain_link::<Tethered>(self);
    }

    fn delete_entity_recorded(
        &mut self,
        entity: EcsEntity,
    ) -> Result<(), specs::error::WrongGeneration> {
        // NOTE: both this and handle_exit_ingame call delete_entity_common, so cleanup
        // added here may need to be duplicated in handle_exit_ingame (depending
        // on its nature).

        // Remove entity from a group if they are in one.
        {
            let clients = self.ecs().read_storage::<Client>();
            let uids = self.ecs().read_storage::<Uid>();
            let mut group_manager = self.ecs().write_resource::<comp::group::GroupManager>();
            let map_markers = self.ecs().read_storage::<comp::MapMarker>();
            group_manager.entity_deleted(
                entity,
                &mut self.ecs().write_storage(),
                &self.ecs().read_storage(),
                &uids,
                &self.ecs().entities(),
                &mut |entity, group_change| {
                    clients
                        .get(entity)
                        .and_then(|c| {
                            group_change
                                .try_map_ref(|e| uids.get(*e).copied())
                                .map(|g| (g, c))
                        })
                        .map(|(g, c)| {
                            update_map_markers(&map_markers, &uids, c, &group_change);
                            c.send_fallible(ServerGeneral::GroupUpdate(g));
                        });
                },
            );
        }

        // Cancel extant trades
        events::shared::cancel_trades_for(self, entity);

        // NOTE: We expect that these 3 components are never removed from an entity (nor
        // mutated) (at least not without updating the relevant mappings)!
        let maybe_uid = self.read_component_copied::<Uid>(entity);
        let (maybe_character, sync_me) = self
            .read_storage::<Presence>()
            .get(entity)
            .map(|p| (p.kind.character_id(), p.kind.sync_me()))
            .unzip();
        let maybe_rtsim = self.read_component_copied::<RtSimEntity>(entity);

        self.mut_resource::<IdMaps>().remove_entity(
            Some(entity),
            maybe_uid,
            maybe_character.flatten(),
            maybe_rtsim,
        );

        delete_entity_common(self, entity, maybe_uid, sync_me.unwrap_or(true))
    }

    fn entity_as_actor(&self, entity: EcsEntity) -> Option<Actor> {
        if let Some(rtsim_entity) = self
            .ecs()
            .read_storage::<RtSimEntity>()
            .get(entity)
            .copied()
        {
            Some(Actor::Npc(rtsim_entity.0))
        } else if let Some(PresenceKind::Character(character)) = self
            .ecs()
            .read_storage::<Presence>()
            .get(entity)
            .map(|p| p.kind)
        {
            Some(Actor::Character(character))
        } else {
            None
        }
    }

    fn position_mut<T>(
        &mut self,
        entity: EcsEntity,
        dismount_volume: bool,
        f: impl for<'a> FnOnce(&'a mut comp::Pos) -> T,
    ) -> Result<T, Content> {
        let ecs = self.ecs_mut();
        position_mut(
            entity,
            dismount_volume,
            f,
            &ecs.read_resource(),
            &mut ecs.write_storage(),
            ecs.write_storage(),
            ecs.write_storage(),
            ecs.read_storage(),
            ecs.read_storage(),
            ecs.read_storage(),
        )
    }
}

pub fn position_mut<T>(
    entity: EcsEntity,
    dismount_volume: bool,
    f: impl for<'a> FnOnce(&'a mut comp::Pos) -> T,
    id_maps: &IdMaps,
    is_volume_riders: &mut WriteStorage<Is<VolumeRider>>,
    mut positions: impl GenericWriteStorage<Component = comp::Pos>,
    mut force_updates: impl GenericWriteStorage<Component = comp::ForceUpdate>,
    is_riders: impl GenericReadStorage<Component = Is<Rider>>,
    presences: impl GenericReadStorage<Component = Presence>,
    clients: impl GenericReadStorage<Component = Client>,
) -> Result<T, Content> {
    if dismount_volume {
        is_volume_riders.remove(entity);
    }

    let entity = is_riders
        .get(entity)
        .and_then(|is_rider| id_maps.uid_entity(is_rider.mount))
        .map(Ok)
        .or_else(|| {
            is_volume_riders.get(entity).and_then(|volume_rider| {
                Some(match volume_rider.pos.kind {
                    common::mounting::Volume::Terrain => Err("Tried to move the world."),
                    common::mounting::Volume::Entity(uid) => Ok(id_maps.uid_entity(uid)?),
                })
            })
        })
        .unwrap_or(Ok(entity))?;

    let mut maybe_pos = None;

    let res = positions
        .get_mut(entity)
        .map(|pos| {
            let res = f(pos);
            maybe_pos = Some(pos.0);
            res
        })
        .ok_or(Content::localized_with_args(
            "command-position-unavailable",
            [("target", "entity")],
        ));

    if let Some(pos) = maybe_pos {
        if presences
            .get(entity)
            .map(|presence| presence.kind == PresenceKind::Spectator)
            .unwrap_or(false)
        {
            clients.get(entity).map(|client| {
                client.send_fallible(ServerGeneral::SpectatePosition(pos));
            });
        } else {
            force_updates
                .get_mut(entity)
                .map(|force_update| force_update.update());
        }
    }

    res
}

fn send_to_group(g: &Group, ecs: &specs::World, msg: &comp::ChatMsg) {
    for (client, group) in (&ecs.read_storage::<Client>(), &ecs.read_storage::<Group>()).join() {
        if g == group {
            client.send_fallible(ServerGeneral::ChatMsg(msg.clone()));
        }
    }
}

/// This should only be called from `handle_exit_ingame` and
/// `delete_entity_recorded`!!!!!!!
pub(crate) fn delete_entity_common(
    state: &mut State,
    entity: EcsEntity,
    maybe_uid: Option<Uid>,
    sync_me: bool,
) -> Result<(), specs::error::WrongGeneration> {
    if maybe_uid.is_none() {
        // For now we expect all entities have a Uid component.
        error!("Deleting entity without Uid component");
    }
    let maybe_pos = state.read_component_copied::<comp::Pos>(entity);

    // TODO: workaround for https://github.com/amethyst/specs/pull/766
    let actual_gen = state.ecs().entities().entity(entity.id()).gen();
    let res = if actual_gen == entity.gen() {
        state.ecs_mut().delete_entity(entity)
    } else {
        Err(specs::error::WrongGeneration {
            action: "delete",
            actual_gen,
            entity,
        })
    };

    if res.is_ok() {
        let region_map = state.mut_resource::<common::region::RegionMap>();
        let uid_pos_region_key = maybe_uid
            .zip(maybe_pos)
            .map(|(uid, pos)| (uid, pos, region_map.find_region(entity, pos.0)));
        region_map.entity_deleted(entity);
        // Note: Adding the `Uid` to the deleted list when exiting "in-game" relies on
        // the client not being able to immediately re-enter the game in the
        // same tick (since we could then mix up the ordering of things and
        // tell other clients to forget the new entity).
        //
        // The client will ignore requests to delete its own entity that are triggered
        // by this.
        if let Some((uid, pos, region_key)) = uid_pos_region_key {
            if let Some(region_key) = region_key {
                state
                    .mut_resource::<DeletedEntities>()
                    .record_deleted_entity(uid, region_key);
            // If there is a position and sync_me is true, but the entity is not
            // in a region, something might be wrong.
            } else if sync_me {
                // Don't panic if the entity wasn't found in a region, maybe it was just created
                // and then deleted before the region manager had a chance to assign it a region
                warn!(
                    ?uid,
                    ?pos,
                    "Failed to find region containing entity during entity deletion, assuming it \
                     wasn't sent to any clients and so deletion doesn't need to be recorded for \
                     sync purposes"
                );
            }
        }
    }
    res
}