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
use crate::{
    ai::Action,
    data::{Reports, Sentiments},
    gen::name,
};
pub use common::rtsim::{NpcId, Profession};
use common::{
    character::CharacterId,
    comp,
    grid::Grid,
    rtsim::{
        Actor, ChunkResource, FactionId, NpcAction, NpcActivity, NpcInput, Personality, ReportId,
        Role, SiteId,
    },
    store::Id,
    terrain::CoordinateConversions,
    util::Dir,
};
use hashbrown::{HashMap, HashSet};
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use slotmap::HopSlotMap;
use std::{
    collections::VecDeque,
    ops::{Deref, DerefMut},
};
use tracing::error;
use vek::*;
use world::{
    civ::Track,
    site::Site as WorldSite,
    util::{RandomPerm, LOCALITY},
};

#[derive(Copy, Clone, Debug, Default)]
pub enum SimulationMode {
    /// The NPC is unloaded and is being simulated via rtsim.
    #[default]
    Simulated,
    /// The NPC has been loaded into the game world as an ECS entity.
    Loaded,
}

#[derive(Clone)]
pub struct PathData<P, N> {
    pub end: N,
    pub path: VecDeque<P>,
    pub repoll: bool,
}

#[derive(Clone, Default)]
pub struct PathingMemory {
    pub intrasite_path: Option<(PathData<Vec2<i32>, Vec2<i32>>, Id<WorldSite>)>,
    pub intersite_path: Option<(PathData<(Id<Track>, bool), SiteId>, usize)>,
}

#[derive(Default)]
pub struct Controller {
    pub actions: Vec<NpcAction>,
    pub activity: Option<NpcActivity>,
    pub new_home: Option<SiteId>,
    pub look_dir: Option<Dir>,
}

impl Controller {
    pub fn do_idle(&mut self) { self.activity = None; }

    pub fn do_goto(&mut self, wpos: Vec3<f32>, speed_factor: f32) {
        self.activity = Some(NpcActivity::Goto(wpos, speed_factor));
    }

    pub fn do_gather(&mut self, resources: &'static [ChunkResource]) {
        self.activity = Some(NpcActivity::Gather(resources));
    }

    pub fn do_hunt_animals(&mut self) { self.activity = Some(NpcActivity::HuntAnimals); }

    pub fn do_dance(&mut self, dir: Option<Dir>) { self.activity = Some(NpcActivity::Dance(dir)); }

    pub fn do_cheer(&mut self, dir: Option<Dir>) { self.activity = Some(NpcActivity::Cheer(dir)); }

    pub fn do_sit(&mut self, dir: Option<Dir>, pos: Option<Vec3<i32>>) {
        self.activity = Some(NpcActivity::Sit(dir, pos));
    }

    pub fn say(&mut self, target: impl Into<Option<Actor>>, content: comp::Content) {
        self.actions.push(NpcAction::Say(target.into(), content));
    }

    pub fn attack(&mut self, target: impl Into<Actor>) {
        self.actions.push(NpcAction::Attack(target.into()));
    }

    pub fn set_new_home(&mut self, new_home: SiteId) { self.new_home = Some(new_home); }
}

pub struct Brain {
    pub action: Box<dyn Action<(), !>>,
}

#[derive(Serialize, Deserialize)]
pub struct Npc {
    pub uid: u64,
    // Persisted state
    pub seed: u32,
    /// Represents the location of the NPC.
    pub wpos: Vec3<f32>,
    pub dir: Vec2<f32>,

    pub body: comp::Body,
    pub role: Role,
    pub home: Option<SiteId>,
    pub faction: Option<FactionId>,
    /// The current health of the NPC, < 0.0 is dead and 1.0 is max.
    pub health_fraction: f32,

    /// The [`Report`]s that the NPC is aware of.
    pub known_reports: HashSet<ReportId>,

    #[serde(default)]
    pub personality: Personality,
    #[serde(default)]
    pub sentiments: Sentiments,

    // Unpersisted state
    #[serde(skip)]
    pub chunk_pos: Option<Vec2<i32>>,
    #[serde(skip)]
    pub current_site: Option<SiteId>,

    #[serde(skip)]
    pub controller: Controller,
    #[serde(skip)]
    pub inbox: VecDeque<NpcInput>,

    /// Whether the NPC is in simulated or loaded mode (when rtsim is run on the
    /// server, loaded corresponds to being within a loaded chunk). When in
    /// loaded mode, the interactions of the NPC should not be simulated but
    /// should instead be derived from the game.
    #[serde(skip)]
    pub mode: SimulationMode,

    #[serde(skip)]
    pub brain: Option<Brain>,
}

impl Clone for Npc {
    fn clone(&self) -> Self {
        Self {
            uid: self.uid,
            seed: self.seed,
            wpos: self.wpos,
            dir: self.dir,
            role: self.role.clone(),
            home: self.home,
            faction: self.faction,
            health_fraction: self.health_fraction,
            known_reports: self.known_reports.clone(),
            body: self.body,
            personality: self.personality,
            sentiments: self.sentiments.clone(),
            // Not persisted
            chunk_pos: None,
            current_site: Default::default(),
            controller: Default::default(),
            inbox: Default::default(),
            mode: Default::default(),
            brain: Default::default(),
        }
    }
}

impl Npc {
    pub const PERM_ENTITY_CONFIG: u32 = 1;
    const PERM_NAME: u32 = 0;

    pub fn new(seed: u32, wpos: Vec3<f32>, body: comp::Body, role: Role) -> Self {
        Self {
            // To be assigned later
            uid: 0,
            seed,
            wpos,
            dir: Vec2::unit_x(),
            body,
            personality: Default::default(),
            sentiments: Default::default(),
            role,
            home: None,
            faction: None,
            health_fraction: 1.0,
            known_reports: Default::default(),
            chunk_pos: None,
            current_site: None,
            controller: Default::default(),
            inbox: Default::default(),
            mode: SimulationMode::Simulated,
            brain: None,
        }
    }

    pub fn is_dead(&self) -> bool { self.health_fraction <= 0.0 }

    // TODO: have a dedicated `NpcBuilder` type for this.
    pub fn with_personality(mut self, personality: Personality) -> Self {
        self.personality = personality;
        self
    }

    // // TODO: have a dedicated `NpcBuilder` type for this.
    // pub fn with_profession(mut self, profession: impl Into<Option<Profession>>)
    // -> Self {     if let Role::Humanoid(p) = &mut self.role {
    //         *p = profession.into();
    //     } else {
    //         panic!("Tried to assign profession {:?} to NPC, but has role {:?},
    // which cannot have a profession", profession.into(), self.role);     }
    //     self
    // }

    // TODO: have a dedicated `NpcBuilder` type for this.
    pub fn with_home(mut self, home: impl Into<Option<SiteId>>) -> Self {
        self.home = home.into();
        self
    }

    // TODO: have a dedicated `NpcBuilder` type for this.
    pub fn with_faction(mut self, faction: impl Into<Option<FactionId>>) -> Self {
        self.faction = faction.into();
        self
    }

    pub fn rng(&self, perm: u32) -> impl Rng { RandomPerm::new(self.seed.wrapping_add(perm)) }

    // TODO: Don't make this depend on deterministic RNG, actually persist names
    // once we've decided that we want to
    pub fn get_name(&self) -> String { name::generate(&mut self.rng(Self::PERM_NAME)) }

    pub fn profession(&self) -> Option<Profession> {
        match &self.role {
            Role::Civilised(profession) => profession.clone(),
            Role::Monster | Role::Wild | Role::Vehicle => None,
        }
    }

    pub fn cleanup(&mut self, reports: &Reports) {
        // Clear old or superfluous sentiments
        // TODO: It might be worth giving more important NPCs a higher sentiment
        // 'budget' than less important ones.
        self.sentiments
            .cleanup(crate::data::sentiment::NPC_MAX_SENTIMENTS);
        // Clear reports that have been forgotten
        self.known_reports
            .retain(|report| reports.contains_key(*report));
        // TODO: Limit number of reports
        // TODO: Clear old inbox items
    }
}

#[derive(Default, Clone, Serialize, Deserialize)]
pub struct GridCell {
    pub npcs: Vec<NpcId>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct NpcLink {
    pub mount: NpcId,
    pub rider: Actor,
    pub is_steering: bool,
}

#[derive(Clone, Default, Serialize, Deserialize)]
struct Riders {
    steerer: Option<MountId>,
    riders: Vec<MountId>,
}

#[derive(Clone, Default, Serialize, Deserialize)]
#[serde(
    from = "HopSlotMap<MountId, NpcLink>",
    into = "HopSlotMap<MountId, NpcLink>"
)]
pub struct NpcLinks {
    links: HopSlotMap<MountId, NpcLink>,
    mount_map: slotmap::SecondaryMap<NpcId, Riders>,
    rider_map: HashMap<Actor, MountId>,
}

impl NpcLinks {
    pub fn remove_mount(&mut self, mount: NpcId) {
        if let Some(riders) = self.mount_map.remove(mount) {
            for link in riders
                .riders
                .into_iter()
                .chain(riders.steerer)
                .filter_map(|link_id| self.links.get(link_id))
            {
                self.rider_map.remove(&link.rider);
            }
        }
    }

    /// Internal function, only removes from `mount_map`.
    fn remove_rider(&mut self, id: MountId, link: &NpcLink) {
        if let Some(riders) = self.mount_map.get_mut(link.mount) {
            if link.is_steering && riders.steerer == Some(id) {
                riders.steerer = None;
            } else if let Some((i, _)) = riders.riders.iter().enumerate().find(|(_, i)| **i == id) {
                riders.riders.remove(i);
            }

            if riders.steerer.is_none() && riders.riders.is_empty() {
                self.mount_map.remove(link.mount);
            }
        }
    }

    pub fn remove_link(&mut self, link_id: MountId) {
        if let Some(link) = self.links.remove(link_id) {
            self.rider_map.remove(&link.rider);
            self.remove_rider(link_id, &link);
        }
    }

    pub fn dismount(&mut self, rider: impl Into<Actor>) {
        if let Some(id) = self.rider_map.remove(&rider.into()) {
            if let Some(link) = self.links.remove(id) {
                self.remove_rider(id, &link);
            }
        }
    }

    // This is the only function to actually add a mount link.
    // And it ensures that there isn't link chaining
    pub fn add_mounting(
        &mut self,
        mount: NpcId,
        rider: impl Into<Actor>,
        steering: bool,
    ) -> Result<MountId, MountingError> {
        let rider = rider.into();
        if Actor::Npc(mount) == rider {
            return Err(MountingError::MountSelf);
        }
        if let Actor::Npc(rider) = rider
            && self.mount_map.contains_key(rider)
        {
            return Err(MountingError::RiderIsMounted);
        }
        if self.rider_map.contains_key(&Actor::Npc(mount)) {
            return Err(MountingError::MountIsRiding);
        }
        if let Some(mount_entry) = self.mount_map.entry(mount) {
            if let hashbrown::hash_map::Entry::Vacant(rider_entry) = self.rider_map.entry(rider) {
                let riders = mount_entry.or_insert(Riders::default());

                if steering {
                    if riders.steerer.is_none() {
                        let id = self.links.insert(NpcLink {
                            mount,
                            rider,
                            is_steering: true,
                        });
                        riders.steerer = Some(id);
                        rider_entry.insert(id);
                        Ok(id)
                    } else {
                        Err(MountingError::HasSteerer)
                    }
                } else {
                    // TODO: Maybe have some limit on the number of riders depending on the mount?
                    let id = self.links.insert(NpcLink {
                        mount,
                        rider,
                        is_steering: false,
                    });
                    riders.riders.push(id);
                    rider_entry.insert(id);
                    Ok(id)
                }
            } else {
                Err(MountingError::AlreadyRiding)
            }
        } else {
            Err(MountingError::MountDead)
        }
    }

    pub fn steer(
        &mut self,
        mount: NpcId,
        rider: impl Into<Actor>,
    ) -> Result<MountId, MountingError> {
        self.add_mounting(mount, rider, true)
    }

    pub fn ride(
        &mut self,
        mount: NpcId,
        rider: impl Into<Actor>,
    ) -> Result<MountId, MountingError> {
        self.add_mounting(mount, rider, false)
    }

    pub fn get_mount_link(&self, rider: impl Into<Actor>) -> Option<&NpcLink> {
        self.rider_map
            .get(&rider.into())
            .and_then(|link| self.links.get(*link))
    }

    pub fn get_steerer_link(&self, mount: NpcId) -> Option<&NpcLink> {
        self.mount_map
            .get(mount)
            .and_then(|mount| self.links.get(mount.steerer?))
    }

    pub fn get(&self, id: MountId) -> Option<&NpcLink> { self.links.get(id) }

    pub fn ids(&self) -> impl Iterator<Item = MountId> + '_ { self.links.keys() }

    pub fn iter(&self) -> impl Iterator<Item = &NpcLink> + '_ { self.links.values() }

    pub fn iter_mounts(&self) -> impl Iterator<Item = NpcId> + '_ { self.mount_map.keys() }
}

impl From<HopSlotMap<MountId, NpcLink>> for NpcLinks {
    fn from(mut value: HopSlotMap<MountId, NpcLink>) -> Self {
        let mut from_map = slotmap::SecondaryMap::new();
        let mut to_map = HashMap::with_capacity(value.len());
        let mut delete = Vec::new();
        for (id, link) in value.iter() {
            if let Some(entry) = from_map.entry(link.mount) {
                let riders = entry.or_insert(Riders::default());
                if link.is_steering {
                    if let Some(old) = riders.steerer.replace(id) {
                        error!("Replaced steerer {old:?} with {id:?}");
                    }
                } else {
                    riders.riders.push(id);
                }
            } else {
                delete.push(id);
            }
            to_map.insert(link.rider, id);
        }
        for id in delete {
            value.remove(id);
        }
        Self {
            links: value,
            mount_map: from_map,
            rider_map: to_map,
        }
    }
}

impl From<NpcLinks> for HopSlotMap<MountId, NpcLink> {
    fn from(other: NpcLinks) -> Self { other.links }
}
slotmap::new_key_type! {
    pub struct MountId;
}

#[derive(Clone, Serialize, Deserialize)]
pub struct MountData {
    is_steering: bool,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Npcs {
    pub uid_counter: u64,
    pub npcs: HopSlotMap<NpcId, Npc>,
    pub mounts: NpcLinks,
    // TODO: This feels like it should be its own rtsim resource
    // TODO: Consider switching to `common::util::SpatialGrid` instead
    #[serde(skip, default = "construct_npc_grid")]
    pub npc_grid: Grid<GridCell>,
    #[serde(skip)]
    pub character_map: HashMap<Vec2<i32>, Vec<(CharacterId, Vec3<f32>)>>,
}

impl Default for Npcs {
    fn default() -> Self {
        Self {
            uid_counter: 0,
            npcs: Default::default(),
            mounts: Default::default(),
            npc_grid: construct_npc_grid(),
            character_map: Default::default(),
        }
    }
}

fn construct_npc_grid() -> Grid<GridCell> { Grid::new(Vec2::zero(), Default::default()) }

#[derive(Debug)]
pub enum MountingError {
    MountDead,
    RiderDead,
    HasSteerer,
    AlreadyRiding,
    MountIsRiding,
    RiderIsMounted,
    MountSelf,
}

impl Npcs {
    pub fn create_npc(&mut self, mut npc: Npc) -> NpcId {
        npc.uid = self.uid_counter;
        self.uid_counter += 1;
        self.npcs.insert(npc)
    }

    /// Queries nearby npcs, not garantueed to work if radius > 32.0
    // TODO: Find a more efficient way to implement this, it's currently
    // (theoretically) O(n^2).
    pub fn nearby(
        &self,
        this_npc: Option<NpcId>,
        wpos: Vec3<f32>,
        radius: f32,
    ) -> impl Iterator<Item = Actor> + '_ {
        let chunk_pos = wpos.xy().as_().wpos_to_cpos();
        let r_sqr = radius * radius;
        LOCALITY
            .into_iter()
            .flat_map(move |neighbor| {
                self.npc_grid.get(chunk_pos + neighbor).map(move |cell| {
                    cell.npcs
                        .iter()
                        .copied()
                        .filter(move |npc| {
                            self.npcs
                                .get(*npc)
                                .map_or(false, |npc| npc.wpos.distance_squared(wpos) < r_sqr)
                                && Some(*npc) != this_npc
                        })
                        .map(Actor::Npc)
                })
            })
            .flatten()
            .chain(
                self.character_map
                    .get(&chunk_pos)
                    .map(|characters| {
                        characters.iter().filter_map(move |(character, c_wpos)| {
                            if c_wpos.distance_squared(wpos) < r_sqr {
                                Some(Actor::Character(*character))
                            } else {
                                None
                            }
                        })
                    })
                    .into_iter()
                    .flatten(),
            )
    }
}

impl Deref for Npcs {
    type Target = HopSlotMap<NpcId, Npc>;

    fn deref(&self) -> &Self::Target { &self.npcs }
}

impl DerefMut for Npcs {
    fn deref_mut(&mut self) -> &mut Self::Target { &mut self.npcs }
}