veloren_rtsim/data/
report.rs

1use common::{
2    resources::TimeOfDay,
3    rtsim::{Actor, SiteId},
4    terrain::SpriteKind,
5};
6use serde::{Deserialize, Serialize};
7use slotmap::HopSlotMap;
8use std::ops::Deref;
9use vek::*;
10
11pub use common::rtsim::ReportId;
12
13/// Represents a single piece of information known by an rtsim entity.
14///
15/// Reports are the medium through which rtsim represents information sharing
16/// between NPCs, factions, and sites. They can represent deaths, attacks,
17/// changes in diplomacy, or any other piece of information representing a
18/// singular event that might be communicated.
19///
20/// Note that they should not be used to communicate sentiments like 'this actor
21/// is friendly': the [`crate::data::Sentiment`] system should be used for that.
22/// Some events might generate both a report and a change in sentiment. For
23/// example, the murder of an NPC might generate both a murder report and highly
24/// negative sentiments.
25#[derive(Clone, Serialize, Deserialize)]
26pub struct Report {
27    pub kind: ReportKind,
28    pub at_tod: TimeOfDay,
29}
30
31impl Report {
32    /// The time, in in-game seconds, for which the report will be remembered
33    fn remember_for(&self) -> f64 {
34        const DAYS: f64 = 60.0 * 60.0 * 24.0;
35        match &self.kind {
36            ReportKind::Death { killer, .. } => {
37                if killer.is_some() {
38                    // Murder is less easy to forget
39                    DAYS * 15.0
40                } else {
41                    DAYS * 5.0
42                }
43            },
44            // TODO: Could consider what was stolen here
45            ReportKind::Theft { .. } => DAYS * 1.5,
46        }
47    }
48}
49
50#[derive(Copy, Clone, Serialize, Deserialize)]
51pub enum ReportKind {
52    Death {
53        actor: Actor,
54        killer: Option<Actor>,
55    },
56    Theft {
57        thief: Actor,
58        /// Where the theft happened.
59        site: Option<SiteId>,
60        /// What was stolen.
61        sprite: SpriteKind,
62    },
63}
64
65#[derive(Clone, Default, Serialize, Deserialize)]
66pub struct Reports {
67    pub reports: HopSlotMap<ReportId, Report>,
68}
69
70impl Reports {
71    pub fn create(&mut self, report: Report) -> ReportId { self.reports.insert(report) }
72
73    pub fn cleanup(&mut self, current_time: TimeOfDay) {
74        // Forget reports that are too old
75        self.reports.retain(|_, report| {
76            (current_time.0 - report.at_tod.0).max(0.0) < report.remember_for()
77        });
78        // TODO: Limit global number of reports
79    }
80}
81
82impl Deref for Reports {
83    type Target = HopSlotMap<ReportId, Report>;
84
85    fn deref(&self) -> &Self::Target { &self.reports }
86}