tiger_lib/ck3/data/
memories.rs1use crate::block::Block;
2use crate::context::ScopeContext;
3use crate::db::{Db, DbKind};
4use crate::desc::validate_desc;
5use crate::everything::Everything;
6use crate::game::GameFlags;
7use crate::item::{Item, ItemLoader};
8use crate::report::{ErrorKey, err};
9use crate::scopes::Scopes;
10use crate::token::Token;
11use crate::validate::validate_duration;
12use crate::validator::Validator;
13
14#[derive(Clone, Debug)]
15pub struct MemoryType {}
16
17inventory::submit! {
18 ItemLoader::Normal(GameFlags::Ck3, Item::MemoryType, MemoryType::add)
19}
20
21impl MemoryType {
22 pub fn add(db: &mut Db, key: Token, block: Block) {
23 db.add(Item::MemoryType, key, block, Box::new(Self {}));
24 }
25}
26
27impl DbKind for MemoryType {
28 fn add_subitems(&self, _key: &Token, block: &Block, db: &mut Db) {
29 if let Some(vec) = block.get_field_list("categories") {
30 for token in vec {
31 db.add_flag(Item::MemoryCategory, token.clone());
32 }
33 }
34 }
35
36 fn validate(&self, key: &Token, block: &Block, data: &Everything) {
37 let mut vd = Validator::new(block, data);
38 let mut sc = ScopeContext::new(Scopes::CharacterMemory, key);
39 sc.define_name("owner", Scopes::Character, key);
40
41 if let Some(icon) = vd.field_value("icon") {
43 let pathname = format!("gfx/interface/icons/memory_types/{icon}");
44 data.verify_exists_implied(Item::File, &pathname, icon);
45 }
46
47 vd.field_list("categories");
48 vd.field_validated_list("participants", |token, _data| {
49 sc.define_name(token.as_str(), Scopes::Character, token);
50 });
51
52 data.verify_exists(Item::Localization, key);
53 vd.field_validated_sc("description", &mut sc, validate_desc);
54 vd.field_validated_sc("second_perspective_description", &mut sc, validate_desc);
55 vd.field_validated_sc("third_perspective_description", &mut sc, validate_desc);
56
57 if !block.has_key("duration") {
58 let msg = "field `duration` missing";
59 let info = "without a duration field, the duration is only 1 day";
60 err(ErrorKey::FieldMissing).msg(msg).info(info).loc(block).push();
61 }
62 vd.field_validated_block_rooted("duration", Scopes::Character, validate_duration);
63 }
64
65 fn has_property(
66 &self,
67 _key: &Token,
68 block: &Block,
69 property: &str,
70 _data: &Everything,
71 ) -> bool {
72 if let Some(vec) = block.get_field_list("participants") {
73 for token in vec {
74 if token.is(property) {
75 return true;
76 }
77 }
78 }
79 false
80 }
81}