1use crate::block::Block;
2use crate::db::{Db, DbKind};
3use crate::everything::Everything;
4use crate::game::GameFlags;
5use crate::item::{Item, ItemLoader};
6use crate::report::{err, warn, ErrorKey};
7use crate::token::Token;
8use crate::validator::Validator;
9
10#[derive(Clone, Debug)]
11pub struct Accessory {}
12
13inventory::submit! {
14 ItemLoader::Normal(GameFlags::jomini(), Item::Accessory, Accessory::add)
15}
16
17impl Accessory {
18 pub fn add(db: &mut Db, key: Token, block: Block) {
19 db.add(Item::Accessory, key, block, Box::new(Self {}));
20 }
21}
22
23impl DbKind for Accessory {
24 fn add_subitems(&self, _key: &Token, block: &Block, db: &mut Db) {
25 if let Some(token) = block.get_field_value("set_tags") {
26 for tag in token.split(',') {
27 db.add_flag(Item::AccessoryTag, tag);
28 }
29 }
30 #[cfg(feature = "imperator")]
32 for tag in &["no_hair", "fat2_normal", "fat2_max", "fat1_normal", "fat1_max", "no_fat"] {
33 db.add_flag(Item::AccessoryTag, Token::new(tag, block.loc));
34 }
35 }
36
37 fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
38 let mut vd = Validator::new(block, data);
39
40 vd.multi_field_validated_block("entity", |block, data| {
41 let mut vd = Validator::new(block, data);
42 if let Some(token) = vd.field_value("required_tags") {
43 for tag in token.split(',') {
44 if !tag.is("") {
45 data.verify_exists(Item::AccessoryTag, &tag);
46 }
47 }
48 }
49 vd.field_choice("shared_pose_entity", &["head", "torso"]);
50 vd.field_value("node"); vd.field_value("game_entity_override"); vd.field_bool("inherit_rotation");
53 vd.field_item("entity", Item::Entity);
54 });
55
56 for token in vd.multi_field_value("set_tags") {
57 for _tag in token.split(',') {
58 }
60 }
61 }
62}
63
64#[derive(Clone, Debug)]
65pub struct AccessoryVariation {}
66
67inventory::submit! {
68 ItemLoader::Normal(GameFlags::all(), Item::AccessoryVariation, AccessoryVariation::add)
69}
70
71impl AccessoryVariation {
72 pub fn add(db: &mut Db, key: Token, block: Block) {
73 if key.is("variation") {
74 if let Some(name) = block.get_field_value("name") {
75 db.add(Item::AccessoryVariation, name.clone(), block, Box::new(Self {}));
76 } else {
77 err(ErrorKey::FieldMissing).msg("variation without a name").loc(key).push();
78 }
79 } else if key.is("pattern_textures") {
80 if let Some(name) = block.get_field_value("name") {
81 db.add_exact_dup_ok(
82 Item::AccessoryVariationTextures,
83 name.clone(),
84 block,
85 Box::new(AccessoryVariationTextures {}),
86 );
87 } else {
88 let msg = "pattern_textures without a name";
89 err(ErrorKey::FieldMissing).msg(msg).loc(key).push();
90 }
91 } else if key.is("pattern_layout") {
92 if let Some(name) = block.get_field_value("name") {
93 db.add_exact_dup_ok(
94 Item::AccessoryVariationLayout,
95 name.clone(),
96 block,
97 Box::new(AccessoryVariationLayout {}),
98 );
99 } else {
100 err(ErrorKey::FieldMissing).msg("pattern_layout without a name").loc(key).push();
101 }
102 } else {
103 warn(ErrorKey::UnknownField).msg("unknown variation type").loc(key).push();
104 }
105 }
106}
107
108impl DbKind for AccessoryVariation {
109 fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
110 let mut vd = Validator::new(block, data);
111
112 vd.field_value("name");
113 vd.multi_field_validated_block("pattern", |block, data| {
114 let mut vd = Validator::new(block, data);
115 vd.field_numeric("weight");
116 for field in &["r", "g", "b", "a", "properties_r", "normal_b"] {
118 vd.field_validated_block(field, |block, data| {
119 let mut vd = Validator::new(block, data);
120 vd.field_item("textures", Item::AccessoryVariationTextures);
121 vd.field_item("layout", Item::AccessoryVariationLayout);
122 });
123 }
124 });
125 vd.multi_field_validated_block("color_palette", |block, data| {
126 let mut vd = Validator::new(block, data);
127 vd.field_numeric("weight");
128 vd.field_item("texture", Item::File);
129 });
130 }
131}
132
133#[derive(Clone, Debug)]
134pub struct AccessoryVariationTextures {}
135
136impl DbKind for AccessoryVariationTextures {
137 fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
138 let mut vd = Validator::new(block, data);
139 vd.field_value("name");
140 vd.req_field("colormask");
141 vd.field_item("colormask", Item::File);
142 vd.req_field("normal");
143 vd.field_item("normal", Item::File);
144 vd.req_field("properties");
145 vd.field_item("properties", Item::File);
146 }
147}
148
149#[derive(Clone, Debug)]
150pub struct AccessoryVariationLayout {}
151
152impl DbKind for AccessoryVariationLayout {
153 fn validate(&self, _key: &Token, block: &Block, data: &Everything) {
154 let mut vd = Validator::new(block, data);
155 vd.field_value("name");
156 vd.req_field("scale");
157 vd.field_validated_block("scale", validate_minmax);
158 vd.req_field("rotation");
159 vd.field_validated_block("rotation", validate_minmax);
160 vd.req_field("offset");
161 vd.field_validated_block("offset", |block, data| {
162 let mut vd = Validator::new(block, data);
163 vd.field_validated_block("x", validate_minmax);
164 vd.field_validated_block("y", validate_minmax);
165 });
166 }
167}
168
169fn validate_minmax(block: &Block, data: &Everything) {
170 let mut vd = Validator::new(block, data);
171 vd.req_field("min");
172 vd.field_numeric("min");
173 vd.req_field("max");
174 vd.field_numeric("max");
175 }