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
use super::utils::*;
use crate::{
    comp::{
        buff::{BuffChange, BuffKind},
        character_state::OutputEvents,
        controller::InputKind,
        inventory::{
            item::{ConsumableKind, ItemKind},
            slot::{InvSlotId, Slot},
        },
        CharacterState, InventoryManip, StateUpdate,
    },
    event::{BuffEvent, InventoryManipEvent},
    states::behavior::{CharacterBehavior, JoinData},
};
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Separated out to condense update portions of character state
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StaticData {
    /// Buildup to item use
    pub buildup_duration: Duration,
    /// Duration of item use
    pub use_duration: Duration,
    /// Recovery after item use
    pub recover_duration: Duration,
    /// Inventory slot to use item from
    pub inv_slot: InvSlotId,
    /// Item hash, used to verify that slot still has the correct item
    pub item_hash: u64,
    /// Kind of item used
    pub item_kind: ItemUseKind,
    /// Had weapon wielded
    pub was_wielded: bool,
    /// Was sneaking
    pub was_sneak: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Data {
    /// Struct containing data that does not change over the course of the
    /// character state
    pub static_data: StaticData,
    /// Timer for each stage
    pub timer: Duration,
    /// What section the character stage is in
    pub stage_section: StageSection,
}

impl CharacterBehavior for Data {
    fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
        let mut update = StateUpdate::from(data);

        match self.static_data.item_kind {
            ItemUseKind::Consumable(
                ConsumableKind::Drink | ConsumableKind::Charm | ConsumableKind::Recipe,
            ) => {
                handle_orientation(data, &mut update, 1.0, None);
                handle_move(data, &mut update, 1.0);
            },
            ItemUseKind::Consumable(ConsumableKind::Food | ConsumableKind::ComplexFood) => {
                handle_orientation(data, &mut update, 0.0, None);
                handle_move(data, &mut update, 0.0);
            },
        }

        let use_point = match self.static_data.item_kind {
            ItemUseKind::Consumable(
                ConsumableKind::Drink | ConsumableKind::Food | ConsumableKind::Recipe,
            ) => UsePoint::BuildupUse,
            ItemUseKind::Consumable(ConsumableKind::ComplexFood | ConsumableKind::Charm) => {
                UsePoint::UseRecover
            },
        };

        match self.stage_section {
            StageSection::Buildup => {
                if self.timer < self.static_data.buildup_duration {
                    // Build up
                    update.character = CharacterState::UseItem(Data {
                        static_data: self.static_data.clone(),
                        timer: tick_attack_or_default(data, self.timer, None),
                        ..*self
                    });
                } else {
                    // Transitions to use section of stage
                    update.character = CharacterState::UseItem(Data {
                        static_data: self.static_data.clone(),
                        timer: Duration::default(),
                        stage_section: StageSection::Action,
                    });
                    if let UsePoint::BuildupUse = use_point {
                        // Create inventory manipulation event
                        use_item(data, output_events, self);
                    }
                }
            },
            StageSection::Action => {
                if self.timer < self.static_data.use_duration {
                    // Item use
                    update.character = CharacterState::UseItem(Data {
                        static_data: self.static_data.clone(),
                        timer: tick_attack_or_default(data, self.timer, None),
                        ..*self
                    });
                } else {
                    // Transitions to recover section of stage
                    update.character = CharacterState::UseItem(Data {
                        static_data: self.static_data.clone(),
                        timer: Duration::default(),
                        stage_section: StageSection::Recover,
                    });
                    if let UsePoint::UseRecover = use_point {
                        // Create inventory manipulation event
                        use_item(data, output_events, self);
                    }
                }
            },
            StageSection::Recover => {
                if self.timer < self.static_data.recover_duration {
                    // Recovery
                    update.character = CharacterState::UseItem(Data {
                        static_data: self.static_data.clone(),
                        timer: tick_attack_or_default(
                            data,
                            self.timer,
                            Some(data.stats.recovery_speed_modifier),
                        ),
                        ..*self
                    });
                } else {
                    // Done
                    end_ability(data, &mut update);
                }
            },
            _ => {
                // If it somehow ends up in an incorrect stage section
                end_ability(data, &mut update);
            },
        }

        // At end of state logic so an interrupt isn't overwritten
        if input_is_pressed(data, InputKind::Roll) {
            handle_input(data, output_events, &mut update, InputKind::Roll);
        }

        if matches!(update.character, CharacterState::Roll(_)) {
            // Remove potion/saturation effect if left the use item state early by rolling
            output_events.emit_server(BuffEvent {
                entity: data.entity,
                buff_change: BuffChange::RemoveByKind(BuffKind::Potion),
            });
            output_events.emit_server(BuffEvent {
                entity: data.entity,
                buff_change: BuffChange::RemoveByKind(BuffKind::Saturation),
            });
        }

        update
    }
}

/// Used to control effects based off of the type of item used
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ItemUseKind {
    Consumable(ConsumableKind),
}

impl From<&ItemKind> for Option<ItemUseKind> {
    fn from(item_kind: &ItemKind) -> Self {
        match item_kind {
            ItemKind::Consumable { kind, .. } => Some(ItemUseKind::Consumable(*kind)),
            _ => None,
        }
    }
}

impl ItemUseKind {
    /// Returns (buildup, use, recover)
    pub fn durations(&self) -> (Duration, Duration, Duration) {
        match self {
            Self::Consumable(ConsumableKind::Drink) => (
                Duration::from_secs_f32(0.1),
                Duration::from_secs_f32(1.1),
                Duration::from_secs_f32(0.1),
            ),
            Self::Consumable(ConsumableKind::Food) => (
                Duration::from_secs_f32(1.0),
                Duration::from_secs_f32(4.0),
                Duration::from_secs_f32(0.5),
            ),
            Self::Consumable(ConsumableKind::ComplexFood) => (
                Duration::from_secs_f32(1.0),
                Duration::from_secs_f32(4.5),
                Duration::from_secs_f32(0.5),
            ),
            Self::Consumable(ConsumableKind::Charm) => (
                Duration::from_secs_f32(0.1),
                Duration::from_secs_f32(0.8),
                Duration::from_secs_f32(0.1),
            ),
            Self::Consumable(ConsumableKind::Recipe) => (
                Duration::from_secs_f32(0.0),
                Duration::from_secs_f32(0.0),
                Duration::from_secs_f32(0.0),
            ),
        }
    }
}

/// Used to control when the item is used in the state
enum UsePoint {
    /// Between buildup and use
    BuildupUse,
    /// Between use and recover
    UseRecover,
}

fn use_item(data: &JoinData, output_events: &mut OutputEvents, state: &Data) {
    // Check if the same item is in the slot
    let item_is_same = data
        .inventory
        .and_then(|inv| inv.get(state.static_data.inv_slot))
        .map_or(false, |item| {
            item.item_hash() == state.static_data.item_hash
        });
    if item_is_same {
        // Create inventory manipulation event
        let inv_manip = InventoryManip::Use(Slot::Inventory(state.static_data.inv_slot));
        output_events.emit_server(InventoryManipEvent(data.entity, inv_manip));
    }
}