1
use crate::action::{Action, ACTIONS};
2
use crate::craft::Craft;
3
use crate::specs::Success;
4
use std::collections::VecDeque;
5
use std::ops::Not;
6

            
7
macro_rules! action_vec {
8
    ($($tt:expr),*) => { vec![ $(Some(&$tt),)*]};
9
}
10

            
11
/// Find the next authorised action for this step of the craft
12
pub fn next_action_picker_1<'a>(craft: &Craft<'a>) -> Vec<Option<&'a Action>> {
13
    if craft.success != Success::Pending {
14
        return vec![None];
15
    }
16
    let mut available_actions = Vec::<Option<&'a Action>>::new();
17
    let mut forbidden_actions = Vec::<Option<&'a Action>>::new();
18

            
19
    // Optimize the first three steps for massive time save
20
    if craft.step_count == 0 {
21
        return action_vec![ACTIONS.muscle_memory];
22
    }
23
    if craft.step_count == 1 {
24
        return action_vec![ACTIONS.manipulation];
25
    }
26

            
27
    // Prune some actions if not requested by --long
28
    if craft.step_count == 2 {
29
        return action_vec![/*ACTIONS.waste_not, ACTIONS.waste_not_ii, */ ACTIONS.veneration];
30
    }
31
    if craft.step_count == 3 {
32
        available_actions.append(&mut action_vec![
33
            ACTIONS.waste_not_ii /*,ACTIONS.waste_not*/
34
        ])
35
    }
36

            
37
    // Groundwork mostly for wn / mm
38
    if craft.buffs.waste_not > 0 || craft.buffs.muscle_memory > 0 {
39
        available_actions.append(&mut action_vec![ACTIONS.groundwork])
40
    }
41

            
42
    // Forbidding actions depending on buffs
43
    if craft.buffs.muscle_memory > 0 {
44
        forbidden_actions.append(&mut action_vec![
45
            ACTIONS.basic_synthesis,
46
            ACTIONS.careful_synthesis,
47
            ACTIONS.prudent_synthesis,
48
            ACTIONS.delicate_synthesis
49
        ])
50
    }
51
    if craft.buffs.waste_not > 0 {
52
        forbidden_actions.append(&mut action_vec![ACTIONS.prudent_synthesis])
53
    }
54
    available_actions.append(&mut action_vec![
55
        ACTIONS.basic_synthesis,
56
        ACTIONS.careful_synthesis,
57
        ACTIONS.prudent_synthesis,
58
        ACTIONS.delicate_synthesis
59
    ]);
60

            
61
    // ACTIONS.groundwork for the --pls
62
    if craft.args.desperate {
63
        available_actions.append(&mut action_vec![ACTIONS.groundwork]);
64
    }
65

            
66
    // For long first run
67
    if craft.step_count > 8 {
68
        available_actions.append(&mut action_vec![ACTIONS.waste_not_ii, ACTIONS.waste_not])
69
    };
70

            
71
    // Pruning the actions with the forbidden ones
72
    let mut result_actions = Vec::<Option<&'a Action>>::new();
73
    for action in available_actions {
74
        if !forbidden_actions.contains(&action)
75
            && action.unwrap().can_use(&craft)
76
            && result_actions
77
                .iter()
78
                .any(|x| x.unwrap() == action.unwrap())
79
                .not()
80
        {
81
            result_actions.push(action);
82
        }
83
    }
84

            
85
    result_actions
86
}
87

            
88
/// Find all routes that can finish in one more action
89
pub fn generate_routes_phase1<'a>(craft: Craft<'a>) -> Vec<Craft<'a>> {
90
    let mut queue = Vec::new();
91
    queue.push(craft);
92
    let mut routes = Vec::new();
93
    while !queue.is_empty() {
94
        let craft = queue.pop().unwrap();
95
        for action in next_action_picker_1(&craft) {
96
            let mut craft = craft.clone();
97

            
98
            match action {
99
                Some(a) => craft.run_action(a),
100
                None => break,
101
            };
102

            
103
            let remaining_prog = (craft.recipe.progress as f32 - craft.progression as f32)
104
                / craft.get_base_progression() as f32;
105
            if remaining_prog <= 2.0 {
106
                if remaining_prog <= 0.0 {
107
                    continue;
108
                } else if 0.0 < remaining_prog && remaining_prog <= 1.2 {
109
                    // pass         // Will finish with basicSynth2 (free)
110
                } else if 1.2 < remaining_prog && remaining_prog <= 1.2 {
111
                    craft.cp -= 7; // Will finish with carefulSynthesis (7cp)
112
                } else if 1.2 < remaining_prog && remaining_prog <= 2.0 {
113
                    craft.cp -= 12; // Will finish with observe+focusedSynthesis (12cp)
114
                }
115
                craft.durability -= 10; // Save the final step, since we always aim to end with -5dur, it doesn't matter
116
                routes.push(craft);
117
                continue;
118
            }
119

            
120
            // Keep adding initial routes with several pass, we must be able to finish in one step
121
            // 8 Seemed to be a good initial guess with enouth to use a good amount of WN / WNII to get close
122
            // A higher value will yield more initial guesses with minimal benefits
123
            // A lover value will yield less initial guesses and might spent too much CP to get to the required progression
124
            if craft.step_count < craft.args.depth {
125
                queue.push(craft);
126
            }
127
        }
128
    }
129
    routes
130
}
131

            
132
/// Find the list of authorised actions for this step
133
pub fn next_action_phase_2<'a>(craft: &Craft<'a>) -> Vec<Option<&'a Action>> {
134
    let mut available_actions = vec![
135
        &ACTIONS.basic_touch,
136
        &ACTIONS.prudent_touch,
137
        &ACTIONS.preparatory_touch,
138
    ];
139
    let mut forbidden_actions = Vec::new();
140
    if craft.success != Success::Pending {
141
        return vec![None];
142
    }
143
    if craft.buffs.innovation > 0 {
144
        forbidden_actions.push(&ACTIONS.innovation);
145
    } else {
146
        if craft.buffs.inner_quiet >= 2 {
147
            forbidden_actions.append(&mut vec![
148
                &ACTIONS.basic_touch,
149
                &ACTIONS.standard_touch,
150
                &ACTIONS.advanced_touch,
151
                &ACTIONS.trained_finesse,
152
                &ACTIONS.prudent_touch,
153
                &ACTIONS.preparatory_touch,
154
                &ACTIONS.byregot_blessing,
155
            ]);
156
        }
157
        available_actions.push(&ACTIONS.innovation);
158
    }
159
    if craft.buffs.manipulation == 0
160
        && craft.buffs.basic_touch == 0
161
        && craft.buffs.standard_touch == 0
162
        && craft.buffs.inner_quiet < 8
163
    {
164
        available_actions.push(&ACTIONS.manipulation);
165
    }
166
    if craft.buffs.waste_not > 0 {
167
        available_actions.push(&ACTIONS.preparatory_touch);
168
        forbidden_actions.push(&ACTIONS.prudent_touch);
169
    } else {
170
        available_actions.push(&ACTIONS.prudent_touch);
171
        forbidden_actions.push(&ACTIONS.preparatory_touch);
172
    }
173
    if craft.buffs.basic_touch > 0 {
174
        available_actions.push(&ACTIONS.standard_touch);
175
        forbidden_actions.push(&ACTIONS.basic_touch);
176
    }
177
    if craft.buffs.standard_touch > 0 {
178
        available_actions.push(&ACTIONS.advanced_touch);
179
        forbidden_actions.push(&ACTIONS.basic_touch);
180
    }
181

            
182
    if craft.buffs.inner_quiet >= craft.args.byregot_step {
183
        // 10
184
        available_actions.push(&ACTIONS.trained_finesse);
185
        available_actions.push(&ACTIONS.great_strides);
186
    }
187
    if craft.buffs.great_strides > 0 {
188
        forbidden_actions.push(&ACTIONS.trained_finesse);
189
        forbidden_actions.push(&ACTIONS.great_strides);
190
        if craft.buffs.innovation > 0 {
191
            available_actions.push(&ACTIONS.byregot_blessing);
192
        }
193
    }
194
    let mut final_actions: Vec<Option<&Action>> = Vec::new();
195

            
196
    for action in available_actions {
197
        if !forbidden_actions.contains(&action)
198
            && !final_actions.iter().any(|a| a.unwrap() == action)
199
        {
200
            final_actions.push(Some(action));
201
        }
202
    }
203
    final_actions
204
}
205

            
206
/// Apply all actions to the current route
207
pub fn generate_routes_phase2<'a>(craft: Craft<'a>) -> Option<Vec<Craft<'a>>> {
208
    let mut queue = VecDeque::new();
209

            
210
    let mut top_route: Craft<'a> = craft.clone(); // Default route, no hq on that one
211
    let mut top_routes: Vec<Craft<'a>> = vec![];
212

            
213
    queue.push_back(craft);
214

            
215
    while !queue.is_empty() {
216
        let _craft = queue.pop_front().unwrap();
217
        for action in next_action_phase_2(&_craft) {
218
            if _craft.success != Success::Pending
219
                || action.is_none()
220
                || !action.unwrap().can_use(&_craft)
221
            {
222
                continue;
223
            }
224
            let mut craft = _craft.clone();
225
            craft.run_action(action.unwrap());
226
            if action.unwrap() == &ACTIONS.byregot_blessing {
227
                #[cfg(not(feature = "fast"))]
228
                if top_route.quality >= craft.recipe.quality {
229
                    top_routes.push(craft.clone()); // Me memory
230
                }
231
                if top_route.quality < craft.quality {
232
                    #[cfg(feature = "fast")]
233
                    top_routes.push(craft.clone());
234
                    top_route = craft;
235
                }
236
            } else {
237
                queue.push_back(craft);
238
            }
239
        }
240
    }
241
    // Let's not forget the best result
242
    top_routes.push(top_route);
243
    Some(top_routes)
244
}