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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::{
    comp::{character_state::OutputEvents, CharacterState, StateUpdate},
    event::{CreateSpriteEvent, LocalEvent},
    outcome::Outcome,
    spiral::Spiral2d,
    states::{
        behavior::{CharacterBehavior, JoinData},
        utils::*,
    },
    terrain::{Block, SpriteKind},
    vol::ReadVol,
};
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use vek::*;

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub enum SpriteSummonAnchor {
    #[default]
    Summoner,
    Target,
}

/// Separated out to condense update portions of character state
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StaticData {
    /// How long the state builds up for
    pub buildup_duration: Duration,
    /// How long the state is casting for
    pub cast_duration: Duration,
    /// How long the state recovers for
    pub recover_duration: Duration,
    /// What kind of sprite is created by this state
    pub sprite: SpriteKind,
    /// Duration until sprite-delete begins (in sec), randomization-range of
    /// sprite-delete-time (in sec)
    pub del_timeout: Option<(f32, f32)>,
    /// Range that sprites are created relative to the summonner
    pub summon_distance: (f32, f32),
    /// Relative to what should the sprites be summoned?
    pub anchor: SpriteSummonAnchor,
    /// Chance that sprite is not created on a particular square
    pub sparseness: f64,
    /// Angle of total coverage, centered on the forward-facing orientation
    pub angle: f32,
    /// How much we can move
    pub move_efficiency: f32,
    /// Miscellaneous information about the ability
    pub ability_info: AbilityInfo,
}

#[derive(Copy, Clone, Debug, PartialEq, 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,
    /// What radius of sprites have already been summoned
    pub achieved_radius: i32,
}

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

        handle_orientation(data, &mut update, 1.0, None);
        handle_move(data, &mut update, self.static_data.move_efficiency);

        let target_pos = || {
            data.controller
                .queued_inputs
                .get(&self.static_data.ability_info.input)
                .or(self.static_data.ability_info.input_attr.as_ref())
                .and_then(|input| input.select_pos)
        };

        match self.stage_section {
            StageSection::Buildup => {
                if self.timer < self.static_data.buildup_duration {
                    // Build up
                    update.character = CharacterState::SpriteSummon(Data {
                        timer: tick_attack_or_default(data, self.timer, None),
                        ..*self
                    });
                    // Send local event used for frontend shenanigans
                    match self.static_data.sprite {
                        SpriteKind::Empty => {
                            output_events.emit_local(LocalEvent::CreateOutcome(
                                Outcome::TerracottaStatueCharge {
                                    pos: data.pos.0
                                        + *data.ori.look_dir() * (data.body.max_radius()),
                                },
                            ));
                        },
                        SpriteKind::FireBlock => {
                            output_events.emit_local(LocalEvent::CreateOutcome(Outcome::Charge {
                                pos: data.pos.0 + *data.ori.look_dir() * (data.body.max_radius()),
                            }));
                        },
                        _ => {},
                    }
                } else {
                    // Transitions to recover section of stage
                    update.character = CharacterState::SpriteSummon(Data {
                        timer: Duration::default(),
                        stage_section: StageSection::Action,
                        ..*self
                    });
                }
            },
            StageSection::Action => {
                if self.timer < self.static_data.cast_duration {
                    let timer_frac =
                        self.timer.as_secs_f32() / self.static_data.cast_duration.as_secs_f32();

                    let anchor_pos = match self.static_data.anchor {
                        SpriteSummonAnchor::Summoner => data.pos.0,
                        // Use the selected target position, falling back to the
                        // summoner position
                        SpriteSummonAnchor::Target => target_pos().unwrap_or(data.pos.0),
                    };
                    let achieved_radius = create_sprites(
                        data,
                        output_events,
                        self.static_data.sprite,
                        timer_frac,
                        self.static_data.summon_distance,
                        self.achieved_radius,
                        self.static_data.angle,
                        self.static_data.sparseness,
                        anchor_pos,
                        matches!(self.static_data.anchor, SpriteSummonAnchor::Target),
                        self.static_data.del_timeout,
                    );

                    update.character = CharacterState::SpriteSummon(Data {
                        timer: tick_attack_or_default(data, self.timer, None),
                        achieved_radius,
                        ..*self
                    });
                    // Send local event used for frontend shenanigans
                    match self.static_data.sprite {
                        SpriteKind::IceSpike => {
                            let summoner_pos =
                                data.pos.0 + *data.ori.look_dir() * data.body.max_radius();
                            output_events.emit_local(LocalEvent::CreateOutcome(
                                Outcome::IceCrack {
                                    pos: match self.static_data.anchor {
                                        SpriteSummonAnchor::Summoner => summoner_pos,
                                        SpriteSummonAnchor::Target => {
                                            target_pos().unwrap_or(summoner_pos)
                                        },
                                    },
                                },
                            ));
                        },
                        SpriteKind::IronSpike => {
                            output_events.emit_local(LocalEvent::CreateOutcome(Outcome::Whoosh {
                                pos: data.pos.0,
                            }));
                        },
                        SpriteKind::FireBlock => {
                            output_events.emit_local(LocalEvent::CreateOutcome(Outcome::Bleep {
                                pos: data.pos.0 + *data.ori.look_dir() * (data.body.max_radius()),
                            }));
                        },
                        _ => {},
                    }
                } else {
                    // Transitions to recover section of stage
                    update.character = CharacterState::SpriteSummon(Data {
                        timer: Duration::default(),
                        stage_section: StageSection::Recover,
                        ..*self
                    });
                }
            },
            StageSection::Recover => {
                if self.timer < self.static_data.recover_duration {
                    // Recovery
                    update.character = CharacterState::SpriteSummon(Data {
                        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);
            },
        }

        update
    }
}

/// Returns achieved radius
pub fn create_sprites(
    data: &JoinData,
    output_events: &mut OutputEvents,
    sprite: SpriteKind,
    timer_frac: f32,
    summon_distance: (f32, f32),
    achieved_radius: i32,
    angle: f32,
    sparseness: f64,
    anchor_pos: Vec3<f32>,
    stack_sprites: bool,
    del_timeout: Option<(f32, f32)>,
) -> i32 {
    // Determines distance from summoner sprites should be created. Goes outward
    // with time.
    let summon_distance = timer_frac * (summon_distance.1 - summon_distance.0) + summon_distance.0;
    let summon_distance = summon_distance.round() as i32;

    // Only summons sprites if summon distance is greater than achieved radius
    for radius in achieved_radius..=summon_distance {
        // 1 added to make range correct, too lazy to add 1 to both variables above
        let radius = radius + 1;
        // Creates a spiral iterator for the newly achieved radius
        let spiral = Spiral2d::with_edge_radius(radius);
        for point in spiral {
            // If square is in the angle and is not sparse, generate sprite
            if data
                .ori
                .look_vec()
                .xy()
                .angle_between(point.as_())
                .to_degrees()
                <= (angle / 2.0)
                && !thread_rng().gen_bool(sparseness)
            {
                // The coordinates of where the sprite is created
                let sprite_pos = Vec3::new(
                    anchor_pos.x.floor() as i32 + point.x,
                    anchor_pos.y.floor() as i32 + point.y,
                    anchor_pos.z.floor() as i32,
                );

                // Check for collision in z up to 10 blocks up or down
                let (obstacle_z, obstacle_z_result) = data
                    .terrain
                    .ray(
                        sprite_pos.map(|x| x as f32 + 0.5) + Vec3::unit_z() * 10.0,
                        sprite_pos.map(|x| x as f32 + 0.5) - Vec3::unit_z() * 10.0,
                    )
                    .until(|b| {
                        // Until reaching a solid block that is not the created
                        // sprite
                        Block::is_solid(b) && b.get_sprite() != Some(sprite)
                    })
                    .cast();

                let z = match sprite {
                    // z height - 1 to delete sprite layer below caster
                    SpriteKind::Empty => sprite_pos.z + (10.5 - obstacle_z).ceil() as i32 - 1,
                    _ => {
                        sprite_pos.z
                            + if let (true, Ok(None)) = (stack_sprites, obstacle_z_result) {
                                0
                            } else {
                                (10.5 - obstacle_z).ceil() as i32
                            }
                    },
                };

                // Location sprite will be created
                let sprite_pos = Vec3::new(sprite_pos.x, sprite_pos.y, z);
                // Layers of sprites
                let layers = match sprite {
                    SpriteKind::SeaUrchin => 2,
                    _ => 1,
                };
                for i in 0..layers {
                    // Send server event to create sprite
                    output_events.emit_server(CreateSpriteEvent {
                        pos: Vec3::new(sprite_pos.x, sprite_pos.y, z + i),
                        sprite,
                        del_timeout,
                    });
                }
            }
        }
    }
    summon_distance
}