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
pub use age::Age;
pub use ethnicity::Ethnicity;
pub use gender::Gender;
pub use size::Size;
pub use species::Species;
pub use view::{DescriptionView, DetailsView, SummaryView};

mod age;
mod ethnicity;
mod gender;
mod size;
mod species;
mod view;

use crate::world::place::Place;
use crate::world::{Demographics, Field, Generate};
use rand::Rng;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Npc {
    pub uuid: Uuid,

    #[serde(flatten)]
    pub data: NpcData,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct NpcData {
    pub name: Field<String>,
    pub gender: Field<Gender>,
    pub age: Field<Age>,
    pub age_years: Field<u16>,
    pub size: Field<Size>,
    pub species: Field<Species>,
    pub ethnicity: Field<Ethnicity>,
    pub location_uuid: Field<Uuid>,
    // pub home: Field<Uuid>,
    // pub occupation: Field<Role>,
    // pub languages: Field<Vec<String>>,
    // pub parents: Field<Vec<Uuid>>,
    // pub spouses: Field<Vec<Uuid>>,
    // pub siblings: Field<Vec<Uuid>>,
    // pub children: Field<Vec<Uuid>>,
}

#[derive(Debug, Default)]
pub struct NpcRelations {
    pub location: Option<(Place, Option<Place>)>,
}

impl Npc {
    pub fn display_summary(&self) -> SummaryView {
        self.data.display_summary()
    }

    pub fn display_description(&self) -> DescriptionView {
        self.data.display_description()
    }

    pub fn display_details(&self, relations: NpcRelations) -> DetailsView {
        self.data.display_details(self.uuid, relations)
    }

    pub fn gender(&self) -> Gender {
        self.data.gender()
    }

    pub fn get_words() -> &'static [&'static str] {
        NpcData::get_words()
    }

    pub fn lock_all(&mut self) {
        self.data.lock_all()
    }

    pub fn apply_diff(&mut self, diff: &mut NpcData) {
        self.data.apply_diff(diff)
    }
}

impl NpcData {
    pub fn display_summary(&self) -> SummaryView {
        SummaryView::new(self)
    }

    pub fn display_description(&self) -> DescriptionView {
        DescriptionView::new(self)
    }

    pub fn display_details(&self, uuid: Uuid, relations: NpcRelations) -> DetailsView {
        DetailsView::new(self, uuid, relations)
    }

    pub fn gender(&self) -> Gender {
        self.gender
            .value()
            .copied()
            .unwrap_or(Gender::NonBinaryThey)
    }

    pub fn get_words() -> &'static [&'static str] {
        &["character", "npc"][..]
    }

    pub fn lock_all(&mut self) {
        let NpcData {
            name,
            gender,
            age,
            age_years,
            size,
            species,
            ethnicity,
            location_uuid,
        } = self;

        name.lock();
        gender.lock();
        age.lock();
        age_years.lock();
        size.lock();
        species.lock();
        ethnicity.lock();
        location_uuid.lock();
    }

    pub fn apply_diff(&mut self, diff: &mut Self) {
        let NpcData {
            name,
            gender,
            age,
            age_years,
            size,
            species,
            ethnicity,
            location_uuid,
        } = self;

        name.apply_diff(&mut diff.name);
        gender.apply_diff(&mut diff.gender);
        age.apply_diff(&mut diff.age);
        age_years.apply_diff(&mut diff.age_years);
        size.apply_diff(&mut diff.size);
        species.apply_diff(&mut diff.species);
        ethnicity.apply_diff(&mut diff.ethnicity);
        location_uuid.apply_diff(&mut diff.location_uuid);
    }
}

impl Generate for NpcData {
    fn regenerate(&mut self, rng: &mut impl Rng, demographics: &Demographics) {
        match (self.species.is_locked(), self.ethnicity.is_locked()) {
            (false, false) => {
                let (species, ethnicity) = demographics.gen_species_ethnicity(rng);
                self.ethnicity.replace(ethnicity);
                self.species.replace(species);
            }
            (false, true) => {
                self.species.replace(
                    demographics
                        .only_ethnicity(self.ethnicity.value().unwrap())
                        .gen_species_ethnicity(rng)
                        .0,
                );
            }
            (true, false) => {
                self.ethnicity.replace(
                    demographics
                        .only_species(self.species.value().unwrap())
                        .gen_species_ethnicity(rng)
                        .1,
                );
            }
            (true, true) => {}
        }

        species::regenerate(rng, self);
        ethnicity::regenerate(rng, self);
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use rand::prelude::*;

    #[test]
    fn regenerate_test() {
        let mut rng = SmallRng::seed_from_u64(0);
        let demographics = Demographics::default();

        let npc = NpcData::generate(&mut rng, &demographics);

        assert!(npc.species.is_some());
        assert!(npc.name.is_some());
    }

    #[test]
    fn gender_test() {
        let mut npc = NpcData::default();
        assert_eq!(Gender::NonBinaryThey, npc.gender());

        npc.gender.replace(Gender::Feminine);
        assert_eq!(Gender::Feminine, npc.gender());
    }

    #[test]
    fn serialize_deserialize_test() {
        let npc = gandalf();

        assert_eq!(
            r#"{"uuid":"00000000-0000-0000-0000-000000000000","name":"Gandalf the Grey","gender":"neuter","age":"geriatric","age_years":65535,"size":{"type":"Medium","height":72,"weight":200},"species":"human","ethnicity":"human","location_uuid":null}"#,
            serde_json::to_string(&npc).unwrap()
        );

        let value: Npc = serde_json::from_str(r#"{"uuid":"00000000-0000-0000-0000-000000000000","name":"Gandalf the Grey","gender":"neuter","age":"geriatric","age_years":65535,"size":{"type":"Medium","height":72,"weight":200},"species":"human","ethnicity":"human","location_uuid":null}"#).unwrap();

        assert_eq!(npc, value);
    }

    #[test]
    fn apply_diff_test_no_change() {
        let mut npc = gandalf();
        let mut diff = NpcData::default();

        npc.data.apply_diff(&mut diff);

        assert_eq!(gandalf(), npc);
        assert_eq!(NpcData::default(), diff);
    }

    #[test]
    fn apply_diff_test_from_empty() {
        let gandalf = gandalf();

        let mut npc = NpcData::default();
        let mut diff = gandalf.data.clone();

        npc.apply_diff(&mut diff);

        assert_eq!(gandalf.data, npc);

        let mut empty_locked = NpcData::default();
        empty_locked.lock_all();
        assert_eq!(empty_locked, diff);
    }

    fn gandalf() -> Npc {
        Npc {
            uuid: uuid::Uuid::nil(),
            data: NpcData {
                name: "Gandalf the Grey".into(),
                gender: Gender::Neuter.into(),
                age: Age::Geriatric.into(),
                age_years: u16::MAX.into(),
                size: Size::Medium {
                    height: 72,
                    weight: 200,
                }
                .into(),
                species: Species::Human.into(),
                ethnicity: Ethnicity::Human.into(),
                location_uuid: None.into(),
            },
        }
    }

    #[test]
    fn lock_all_test() {
        let mut npc = NpcData::default();
        npc.lock_all();

        assert_eq!(
            NpcData {
                name: Field::Locked(None),
                gender: Field::Locked(None),
                age: Field::Locked(None),
                age_years: Field::Locked(None),
                size: Field::Locked(None),
                species: Field::Locked(None),
                ethnicity: Field::Locked(None),
                location_uuid: Field::Locked(None),
            },
            npc,
        );
    }
}