1
use crate::craft::Craft;
2
use crate::specs::{Buff, Success};
3
use lazy_static::lazy_static;
4
#[allow(dead_code)]
5
use std::fmt::Debug;
6

            
7
pub struct Action {
8
    pub name: String,
9
    pub dur: u32,
10
    pub cp: u32,
11
    pub progress: u32,
12
    pub quality: u32,
13
    pub buff: Option<(Buff, u8)>,
14
    pub short_name: String,
15
}
16

            
17
pub struct ActionBuilder {
18
    action: Action,
19
}
20

            
21
impl ActionBuilder {
22
    pub fn new(name: &str) -> Self {
23
        let mut short_name = name.to_string();
24
        short_name = short_name.replace("II", "2");
25
        short_name.retain(|c| !c.is_whitespace() && c != '\'');
26
        short_name[0..1].make_ascii_lowercase();
27
        if name == "Basic Synthesis" {
28
            short_name = "basicSynth2".to_string();
29
        }
30
        if name == "Groundwork" {
31
            short_name = "groundwork".to_string();
32
        }
33
        if name == "Careful Synthesis" {
34
            short_name = "carefulSynthesis".to_string();
35
        }
36

            
37
        Self {
38
            action: Action {
39
                name: name.to_string(),
40
                dur: 10,
41
                cp: 0,
42
                progress: 0,
43
                quality: 0,
44
                buff: None,
45
                short_name,
46
            },
47
        }
48
    }
49
    pub fn dur(mut self, dur: u32) -> Self {
50
        self.action.dur = dur;
51
        self
52
    }
53
    pub fn cp(mut self, cp: u32) -> Self {
54
        self.action.cp = cp;
55
        self
56
    }
57
    pub fn progress(mut self, progress: u32) -> Self {
58
        self.action.progress = progress;
59
        self
60
    }
61
    pub fn quality(mut self, quality: u32) -> Self {
62
        self.action.quality = quality;
63
        self
64
    }
65
    pub fn buff(mut self, buff: Option<(Buff, u8)>) -> Self {
66
        self.action.buff = buff;
67
        self
68
    }
69
    pub fn build(self) -> Action {
70
        self.action
71
    }
72
}
73

            
74
impl PartialEq for Action {
75
    fn eq(&self, other: &Self) -> bool {
76
        self.cp == other.cp
77
            && self.dur == other.dur
78
            && self.progress == other.progress
79
            && self.quality == other.quality
80
    }
81
}
82

            
83
impl Action {
84
    pub fn get_name(&self) -> String {
85
        self.name.clone()
86
    }
87
    pub fn get_cp_cost(&self, _craft: &Craft) -> u32 {
88
        if self == &ACTIONS.standard_touch && _craft.buffs.basic_touch > 0 {
89
            return 18;
90
        };
91
        if self == &ACTIONS.advanced_touch && _craft.buffs.standard_touch > 0 {
92
            return 18;
93
        };
94
        self.cp
95
    }
96
    pub fn get_durability_cost(&self, _craft: &Craft) -> u32 {
97
        if self == &ACTIONS.masters_mend {
98
            return 30;
99
        }
100
        if self.dur == 0 || self.progress == 0 && self.quality == 0 {
101
            return 0;
102
        }
103
        let mut dur = self.dur;
104
        if _craft.buffs.waste_not > 0 {
105
            dur /= 2;
106
        }
107
        if dur < 5 {
108
            dur = 5;
109
        }
110
        dur
111
    }
112
    pub fn get_progress(&self, _craft: &Craft) -> u32 {
113
        let mut prog = self.progress as f64;
114
        if self.dur == 20 && _craft.durability < self.get_durability_cost(_craft) as i32 {
115
            prog /= 2.0;
116
        }
117
        let mut mult = 1.0;
118
        if _craft.buffs.muscle_memory > 0 {
119
            mult += 1.0;
120
        }
121
        if _craft.buffs.veneration > 0 {
122
            mult += 0.5;
123
        }
124
        (prog * mult).floor() as u32
125
    }
126
    pub fn get_quality(&self, _craft: &Craft) -> u32 {
127
        let mut qual = self.quality as f64;
128
        if self == &ACTIONS.byregot_blessing {
129
            qual = (100 + 20 * _craft.buffs.inner_quiet as u32) as f64;
130
        }
131
        if self.dur == 20 && _craft.durability < self.get_durability_cost(_craft) as i32 {
132
            qual /= 2.0;
133
        }
134
        let mut mult = 1.0;
135
        if _craft.buffs.great_strides > 0 {
136
            mult += 1.0;
137
        }
138
        if _craft.buffs.innovation > 0 {
139
            mult += 0.5;
140
        }
141
        let iq_mult = (_craft.buffs.inner_quiet as f64 * 0.1) + 1.0;
142
        (qual * mult * iq_mult).floor() as u32
143
    }
144
    pub fn get_buff(&self) -> Option<(Buff, u8)> {
145
        self.buff
146
    }
147
    pub fn can_use(&self, _craft: &Craft) -> bool {
148
        if self.get_cp_cost(_craft) > _craft.cp as u32 {
149
            return false;
150
        }
151
        if _craft.success != Success::Pending {
152
            return false;
153
        }
154
        if self == &ACTIONS.byregot_blessing {
155
            return _craft.buffs.inner_quiet > 0;
156
        }
157
        if self == &ACTIONS.muscle_memory || self == &ACTIONS.reflect {
158
            return _craft.step_count == 0;
159
        }
160
        if self == &ACTIONS.trained_finesse {
161
            return _craft.buffs.inner_quiet >= 10;
162
        }
163
        true
164
    }
165
}
166

            
167
impl Debug for Action {
168
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169
        f.write_str(&self.name)
170
    }
171
}
172

            
173
pub struct ActionList {
174
    pub muscle_memory: Action,
175
    pub reflect: Action,
176
    pub basic_synthesis: Action,
177
    pub careful_synthesis: Action,
178
    pub groundwork: Action,
179
    pub prudent_synthesis: Action,
180
    pub delicate_synthesis: Action,
181
    pub basic_touch: Action,
182
    pub standard_touch: Action,
183
    pub byregot_blessing: Action,
184
    pub prudent_touch: Action,
185
    pub preparatory_touch: Action,
186
    pub advanced_touch: Action,
187
    pub trained_finesse: Action,
188
    pub masters_mend: Action,
189
    pub waste_not: Action,
190
    pub waste_not_ii: Action,
191
    pub manipulation: Action,
192
    pub veneration: Action,
193
    pub great_strides: Action,
194
    pub innovation: Action,
195
    //pub observe: Action,
196
    //pub focused_synthesis: Action,
197
}
198

            
199
impl Default for ActionList {
200
    fn default() -> Self {
201
        Self {
202
            muscle_memory: ActionBuilder::new("Muscle Memory")
203
                .cp(6)
204
                .progress(300)
205
                .buff(Some((Buff::MuscleMemory, 5)))
206
                .build(),
207
            reflect: ActionBuilder::new("Reflect")
208
                .cp(6)
209
                .quality(100)
210
                .buff(Some((Buff::InnerQuiet, 1)))
211
                .build(),
212

            
213
            basic_synthesis: ActionBuilder::new("Basic Synthesis").progress(120).build(),
214
            careful_synthesis: ActionBuilder::new("Careful Synthesis")
215
                .cp(7)
216
                .progress(180)
217
                .build(),
218
            groundwork: ActionBuilder::new("Groundwork")
219
                .cp(18)
220
                .dur(20)
221
                .progress(360)
222
                .build(),
223
            prudent_synthesis: ActionBuilder::new("Prudent Synthesis")
224
                .cp(18)
225
                .dur(5)
226
                .progress(180)
227
                .build(),
228
            delicate_synthesis: ActionBuilder::new("Delicate Synthesis")
229
                .cp(32)
230
                .progress(100)
231
                .quality(100)
232
                .build(),
233

            
234
            basic_touch: ActionBuilder::new("Basic Touch")
235
                .cp(18)
236
                .quality(100)
237
                .buff(Some((Buff::BasicTouch, 1)))
238
                .build(),
239
            standard_touch: ActionBuilder::new("Standard Touch")
240
                .quality(125)
241
                .cp(32)
242
                .buff(Some((Buff::StandardTouch, 1)))
243
                .build(),
244
            byregot_blessing: ActionBuilder::new("Byregot's Blessing")
245
                .cp(24)
246
                .quality(100)
247
                .buff(Some((Buff::InnerQuiet, 0)))
248
                .build(),
249
            prudent_touch: ActionBuilder::new("Prudent Touch")
250
                .cp(25)
251
                .dur(5)
252
                .quality(100)
253
                .build(),
254
            preparatory_touch: ActionBuilder::new("Preparatory Touch")
255
                .cp(40)
256
                .dur(20)
257
                .quality(200)
258
                .buff(Some((Buff::InnerQuiet, 1)))
259
                .build(),
260
            advanced_touch: ActionBuilder::new("Advanced Touch")
261
                .quality(150)
262
                .cp(46)
263
                .build(),
264
            trained_finesse: ActionBuilder::new("Trained Finesse")
265
                .cp(32)
266
                .quality(100)
267
                .dur(0)
268
                .build(),
269

            
270
            masters_mend: ActionBuilder::new("Master's Mend").cp(88).build(),
271
            waste_not: ActionBuilder::new("Waste Not")
272
                .cp(56)
273
                .buff(Some((Buff::WasteNot, 4)))
274
                .build(),
275
            waste_not_ii: ActionBuilder::new("Waste Not II")
276
                .cp(98)
277
                .buff(Some((Buff::WasteNot, 8)))
278
                .build(),
279
            manipulation: ActionBuilder::new("Manipulation")
280
                .cp(96)
281
                .buff(Some((Buff::Manipulation, 8)))
282
                .build(),
283
            veneration: ActionBuilder::new("Veneration")
284
                .cp(18)
285
                .buff(Some((Buff::Veneration, 4)))
286
                .build(),
287
            great_strides: ActionBuilder::new("Great Strides")
288
                .cp(32)
289
                .buff(Some((Buff::GreatStrides, 3)))
290
                .build(),
291
            innovation: ActionBuilder::new("Innovation")
292
                .cp(18)
293
                .buff(Some((Buff::Innovation, 4)))
294
                .build(),
295
            // Todo: Add back
296
            //observe: ActionBuilder::new("observe").cp(18).buff(Some((Buff::Observe, 1))).build(),
297
            //focused_synthesis: ActionBuilder::new("focused_synthesis").cp(18).quality(100).build(),
298
        }
299
    }
300
}
301

            
302
lazy_static! {
303
    pub static ref ACTIONS: ActionList = ActionList::default();
304
}