tiger_lib/
effect_validation.rs

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
//! Validators for effects that are generic across multiple games.

use crate::block::{Block, Comparator, Eq::Single, BV};
use crate::context::ScopeContext;
use crate::desc::validate_desc;
use crate::effect::{validate_effect, validate_effect_control};
use crate::everything::Everything;
#[cfg(not(feature = "imperator"))]
use crate::game::Game;
use crate::item::Item;
use crate::lowercase::Lowercase;
use crate::report::{err, warn, ErrorKey, Severity};
use crate::scopes::Scopes;
use crate::script_value::validate_script_value;
use crate::token::Token;
use crate::tooltipped::Tooltipped;
use crate::trigger::{validate_target_ok_this, validate_trigger_key_bv};
use crate::validate::validate_optional_duration;
use crate::validator::{Validator, ValueValidator};

#[allow(dead_code)] // No longer used by CK3
pub fn validate_add_to_list(
    _key: &Token,
    mut vd: ValueValidator,
    sc: &mut ScopeContext,
    _tooltipped: Tooltipped,
) {
    sc.define_or_expect_list(vd.value());
    vd.accept();
}

/// A specific validator for the three `add_to_variable_list` effects (`global`, `local`, and default).
pub fn validate_add_to_variable_list(
    _key: &Token,
    _block: &Block,
    _data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    _tooltipped: Tooltipped,
) {
    vd.req_field("name");
    vd.req_field("target");
    vd.field_value("name");
    vd.field_target_ok_this("target", sc, Scopes::all_but_none());
    #[cfg(feature = "ck3")]
    validate_optional_duration(&mut vd, sc);
}

/// A specific validator for the three `change_variable` effects (`global`, `local`, and default).
pub fn validate_change_variable(
    _key: &Token,
    _block: &Block,
    _data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    _tooltipped: Tooltipped,
) {
    vd.req_field("name");
    vd.field_value("name");
    vd.field_script_value("add", sc);
    vd.field_script_value("subtract", sc);
    vd.field_script_value("multiply", sc);
    vd.field_script_value("divide", sc);
    vd.field_script_value("modulo", sc);
    vd.field_script_value("min", sc);
    vd.field_script_value("max", sc);
}

/// A specific validator for the three `clamp_variable` effects (`global`, `local`, and default).
pub fn validate_clamp_variable(
    _key: &Token,
    _block: &Block,
    _data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    _tooltipped: Tooltipped,
) {
    vd.req_field("name");
    vd.field_value("name");
    vd.field_script_value("min", sc);
    vd.field_script_value("max", sc);
}

/// A specific validator for the `random_list` effect, which has a unique syntax.
pub fn validate_random_list(
    key: &Token,
    _block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    tooltipped: Tooltipped,
) {
    let caller = Lowercase::new(key.as_str());
    vd.field_integer("pick");
    vd.field_bool("unique"); // don't know what this does
    vd.field_validated_sc("desc", sc, validate_desc);
    vd.unknown_block_fields(|key, block| {
        if let Some(n) = key.expect_number() {
            if n < 0.0 {
                let msg = "negative weights make the whole `random_list` fail";
                err(ErrorKey::Range).strong().msg(msg).loc(key).push();
            } else if n > 0.0 && n < 1.0 {
                let msg = "fractional weights are treated as just 0 in `random_list`";
                err(ErrorKey::Range).strong().msg(msg).loc(key).push();
            } else if n.fract() != 0.0 {
                let msg = "fractions are discarded in `random_list` weights";
                warn(ErrorKey::Range).strong().msg(msg).loc(key).push();
            }
            validate_effect_control(&caller, block, data, sc, tooltipped);
        }
    });
}

pub fn validate_remove_from_list(
    _key: &Token,
    mut vd: ValueValidator,
    sc: &mut ScopeContext,
    _tooltipped: Tooltipped,
) {
    sc.expect_list(vd.value());
    vd.accept();
}

/// A specific validator for the three `round_variable` effects (`global`, `local`, and default).
pub fn validate_round_variable(
    _key: &Token,
    _block: &Block,
    _data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    _tooltipped: Tooltipped,
) {
    vd.req_field("name");
    vd.req_field("nearest");
    vd.field_value("name");
    vd.field_script_value("nearest", sc);
}

pub fn validate_save_scope(
    _key: &Token,
    mut vd: ValueValidator,
    sc: &mut ScopeContext,
    _tooltipped: Tooltipped,
) {
    sc.save_current_scope(vd.value().as_str());
    vd.accept();
}

/// A specific validator for the `save_scope_value` effect.
#[cfg(any(feature = "ck3", feature = "vic3"))]
pub fn validate_save_scope_value(
    _key: &Token,
    _block: &Block,
    _data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    _tooltipped: Tooltipped,
) {
    vd.req_field("name");
    vd.req_field("value");
    if let Some(name) = vd.field_value("name") {
        // TODO: examine `value` field to check its real scope type
        sc.define_name_token(name.as_str(), Scopes::primitive(), name);
    }
    vd.field_script_value_or_flag("value", sc);
}

/// A specific validator for the three `set_variable` effects (`global`, `local`, and default).
pub fn validate_set_variable(
    _key: &Token,
    bv: &BV,
    data: &Everything,
    sc: &mut ScopeContext,
    _tooltipped: Tooltipped,
) {
    match bv {
        BV::Value(_token) => (),
        BV::Block(block) => {
            let mut vd = Validator::new(block, data);
            vd.set_case_sensitive(false);
            vd.req_field("name");
            vd.field_value("name");
            vd.field_validated("value", |bv, data| match bv {
                BV::Value(token) => {
                    validate_target_ok_this(token, data, sc, Scopes::all_but_none());
                }
                BV::Block(_) => validate_script_value(bv, data, sc),
            });
            validate_optional_duration(&mut vd, sc);
        }
    }
}

/// A specific validator for the `switch` effect, which has a unique syntax.
pub fn validate_switch(
    key: &Token,
    _block: &Block,
    data: &Everything,
    sc: &mut ScopeContext,
    mut vd: Validator,
    tooltipped: Tooltipped,
) {
    vd.set_case_sensitive(true);
    vd.req_field("trigger");
    if let Some(target) = vd.field_value("trigger") {
        // clone to avoid calling vd again while target is still borrowed
        let target = target.clone();
        let mut count = 0;
        vd.set_allow_questionmark_equals(true);
        vd.unknown_block_fields(|key, block| {
            count += 1;
            if !key.is("fallback") {
                // Pretend the switch was written as a series of trigger = key lines
                let synthetic_bv = BV::Value(key.clone());
                validate_trigger_key_bv(
                    &target,
                    Comparator::Equals(Single),
                    &synthetic_bv,
                    data,
                    sc,
                    tooltipped,
                    false,
                    Severity::Error,
                );
            }

            validate_effect(block, data, sc, tooltipped);
        });
        if count == 0 {
            let msg = "switch with no branches";
            err(ErrorKey::Logic).msg(msg).loc(key).push();
        }
    }
}

pub fn validate_trigger_event(
    _key: &Token,
    bv: &BV,
    data: &Everything,
    sc: &mut ScopeContext,
    _tooltipped: Tooltipped,
) {
    match bv {
        BV::Value(token) => {
            data.verify_exists(Item::Event, token);
            data.events.check_scope(token, sc);
            if let Some(mut event_sc) = sc.root_for_event(token) {
                data.events.validate_call(token, data, &mut event_sc);
            }
        }
        BV::Block(block) => {
            let mut vd = Validator::new(block, data);
            vd.set_case_sensitive(false);
            vd.field_event("id", sc);
            vd.field_action("on_action", sc);
            #[cfg(feature = "ck3")]
            if Game::is_ck3() {
                vd.field_target("saved_event_id", sc, Scopes::Flag);
                vd.field_date("trigger_on_next_date");
                vd.field_bool("delayed");
            }
            #[cfg(feature = "vic3")]
            if Game::is_vic3() {
                vd.field_bool("popup");
            }
            validate_optional_duration(&mut vd, sc);
        }
    }
}