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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use std::{cmp::Reverse, collections::HashSet};

use specs::{Join, LendJoin, ReadStorage, WorldExt};
use vek::*;

use super::target::{self, Target};
use client::Client;
use common::{
    comp::{
        self, pet, ship::figuredata::VOXEL_COLLIDER_MANIFEST, tool::ToolKind, Alignment, Collider,
        Content,
    },
    consts::{
        self, MAX_INTERACT_RANGE, MAX_PICKUP_RANGE, MAX_SPRITE_MOUNT_RANGE, TELEPORTER_RADIUS,
    },
    link::Is,
    mounting::{Mount, Volume, VolumePos, VolumeRider},
    states::utils::can_perform_pet,
    terrain::{Block, TerrainGrid, UnlockKind},
    uid::{IdMaps, Uid},
    CachedSpatialGrid,
};
use common_base::span;
use hashbrown::HashMap;

use crate::{
    game_input::GameInput,
    hud::CraftingTab,
    scene::{terrain::Interaction, Scene},
};

#[derive(Debug, Default)]
pub struct Interactables {
    pub input_map: HashMap<GameInput, (f32, Interactable)>,
    /// Set of all nearby interactable entities, stored separately for fast
    /// access in scene
    pub entities: HashSet<specs::Entity>,
}

#[derive(Clone, Debug)]
pub enum BlockInteraction {
    Collect { steal: bool },
    Unlock(UnlockKind),
    Craft(CraftingTab),
    // TODO: mining blocks don't use the interaction key, so it might not be the best abstraction
    // to have them here, will see how things turn out
    Mine(ToolKind),
    Mount,
    Read(Content),
    LightToggle(bool),
}

#[derive(Debug, Clone)]
pub enum Interactable {
    Block {
        block: Block,
        volume_pos: VolumePos,
        interaction: BlockInteraction,
    },
    Entity {
        entity: specs::Entity,
        interaction: EntityInteraction,
    },
}

/// The type of interaction an entity has
#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_map::Enum)]
pub enum EntityInteraction {
    HelpDowned,
    PickupItem,
    ActivatePortal,
    Pet,
    Talk,
    CampfireSit,
    Trade,
    StayFollow,
    Mount,
}

impl BlockInteraction {
    fn from_block_pos(
        terrain: &TerrainGrid,
        id_maps: &IdMaps,
        colliders: &ReadStorage<Collider>,
        volume_pos: VolumePos,
        interaction: Interaction,
    ) -> Option<(Block, Self)> {
        let block = volume_pos.get_block(terrain, id_maps, colliders)?;
        let block_interaction = match interaction {
            Interaction::Collect => {
                // Check if this is an unlockable sprite
                let unlock = match volume_pos.kind {
                    Volume::Terrain => block.get_sprite().and_then(|sprite| {
                        let chunk = terrain.pos_chunk(volume_pos.pos)?;
                        let sprite_chunk_pos = TerrainGrid::chunk_offs(volume_pos.pos);
                        let sprite_cfg = chunk.meta().sprite_cfg_at(sprite_chunk_pos);
                        let unlock_condition = sprite.unlock_condition(sprite_cfg.cloned());
                        // HACK: No other way to distinguish between things that should be
                        // unlockable and regular sprites with the current
                        // unlock_condition method so we hack around that by
                        // saying that it is a regular collectible sprite if
                        // `unlock_condition` returns UnlockKind::Free and the cfg was `None`.
                        if sprite_cfg.is_some() || !matches!(&unlock_condition, UnlockKind::Free) {
                            Some(unlock_condition)
                        } else {
                            None
                        }
                    }),
                    Volume::Entity(_) => None,
                };

                if let Some(unlock) = unlock {
                    BlockInteraction::Unlock(unlock)
                } else if let Some(mine_tool) = block.mine_tool() {
                    BlockInteraction::Mine(mine_tool)
                } else {
                    BlockInteraction::Collect {
                        steal: block.is_owned(),
                    }
                }
            },
            Interaction::Read => match volume_pos.kind {
                common::mounting::Volume::Terrain => block.get_sprite().and_then(|sprite| {
                    let chunk = terrain.pos_chunk(volume_pos.pos)?;
                    let sprite_chunk_pos = TerrainGrid::chunk_offs(volume_pos.pos);
                    let sprite_cfg = chunk.meta().sprite_cfg_at(sprite_chunk_pos);
                    sprite
                        .content(sprite_cfg.cloned())
                        .map(BlockInteraction::Read)
                })?,
                // Signs on volume entities are not currently supported
                common::mounting::Volume::Entity(_) => return None,
            },
            Interaction::Craft(tab) => BlockInteraction::Craft(tab),
            Interaction::Mount => BlockInteraction::Mount,
            Interaction::LightToggle(enable) => BlockInteraction::LightToggle(enable),
        };

        Some((block, block_interaction))
    }

    pub fn game_input(&self) -> GameInput {
        match self {
            BlockInteraction::Collect { .. }
            | BlockInteraction::Read(_)
            | BlockInteraction::LightToggle(_)
            | BlockInteraction::Craft(_)
            | BlockInteraction::Unlock(_) => GameInput::Interact,
            BlockInteraction::Mine(_) => GameInput::Primary,
            BlockInteraction::Mount => GameInput::Mount,
        }
    }

    pub fn range(&self) -> f32 {
        match self {
            BlockInteraction::Collect { .. }
            | BlockInteraction::Unlock(_)
            | BlockInteraction::Mine(_)
            | BlockInteraction::Craft(_) => consts::MAX_PICKUP_RANGE,
            BlockInteraction::Mount => consts::MAX_MOUNT_RANGE,
            BlockInteraction::LightToggle(_) | BlockInteraction::Read(_) => {
                consts::MAX_INTERACT_RANGE
            },
        }
    }
}

#[derive(Debug)]
pub enum GetInteractablesError {
    ClientMissingPosition,
    ClientMissingUid,
}

/// Scan for any nearby interactables, including further ones if directly
/// targetted.
pub(super) fn get_interactables(
    client: &Client,
    collect_target: Option<Target<target::Collectable>>,
    entity_target: Option<Target<target::Entity>>,
    mine_target: Option<Target<target::Mine>>,
    scene: &Scene,
) -> Result<HashMap<GameInput, (f32, Interactable)>, GetInteractablesError> {
    span!(_guard, "select_interactable");
    use common::{spiral::Spiral2d, terrain::TerrainChunk, vol::RectRasterableVol};

    let voxel_colliders_manifest = VOXEL_COLLIDER_MANIFEST.read();

    let ecs = client.state().ecs();
    let id_maps = ecs.read_resource::<IdMaps>();
    let scene_terrain = scene.terrain();
    let terrain = client.state().terrain();
    let player_entity = client.entity();
    let positions = ecs.read_storage::<comp::Pos>();
    let player_pos = positions
        .get(player_entity)
        .ok_or(GetInteractablesError::ClientMissingPosition)?
        .0;

    let uids = ecs.read_storage::<Uid>();
    let healths = ecs.read_storage::<comp::Health>();
    let colliders = ecs.read_storage::<comp::Collider>();
    let char_states = ecs.read_storage::<comp::CharacterState>();
    let is_mounts = ecs.read_storage::<Is<Mount>>();
    let bodies = ecs.read_storage::<comp::Body>();
    let masses = ecs.read_storage::<comp::Mass>();
    let items = ecs.read_storage::<comp::PickupItem>();
    let alignments = ecs.read_storage::<comp::Alignment>();
    let is_volume_rider = ecs.read_storage::<Is<VolumeRider>>();

    let player_chunk = player_pos.xy().map2(TerrainChunk::RECT_SIZE, |e, sz| {
        (e.floor() as i32).div_euclid(sz as i32)
    });
    let player_body = bodies.get(player_entity);
    let player_mass = masses.get(player_entity);
    let Some(player_uid) = uids.get(player_entity).copied() else {
        tracing::error!("Client has no Uid component! Not scanning for any interactables.");
        return Err(GetInteractablesError::ClientMissingUid);
    };

    let spacial_grid = ecs.read_resource::<CachedSpatialGrid>();

    let entities = ecs.entities();
    let mut entity_data = (
        &entities,
        !&is_mounts,
        &uids,
        &positions,
        &bodies,
        &masses,
        char_states.maybe(),
        healths.maybe(),
        alignments.maybe(),
        items.mask().maybe(),
    )
        .lend_join();

    let interactable_entities = spacial_grid
        .0
        .in_circle_aabr(player_pos.xy(), MAX_PICKUP_RANGE)
        .chain(entity_target.map(|t| t.kind.0))
        .filter(|&entity| entity != player_entity)
        .filter_map(|entity| entity_data.get(entity, &entities))
        .flat_map(
            |(entity, _, uid, pos, body, mass, char_state, health, alignment, has_item)| {
                // If an entity is downed, the only allowed interaction is HelpDowned
                let is_downed = comp::is_downed(health, char_state);

                // Interactions using [`GameInput::Interact`]
                let interaction = if is_downed {
                    Some(EntityInteraction::HelpDowned)
                } else if has_item.is_some() {
                    Some(EntityInteraction::PickupItem)
                } else if body.is_portal()
                    && pos.0.distance_squared(player_pos) <= TELEPORTER_RADIUS.powi(2)
                {
                    Some(EntityInteraction::ActivatePortal)
                } else if alignment.is_some_and(|alignment| {
                    can_perform_pet(comp::Pos(player_pos), *pos, *alignment)
                }) {
                    Some(EntityInteraction::Pet)
                } else if alignment.is_some_and(|alignment| matches!(alignment, Alignment::Npc)) {
                    Some(EntityInteraction::Talk)
                } else {
                    None
                };

                // Interaction using [`GameInput::Sit`]
                let sit =
                    (body.is_campfire() && !is_downed).then_some(EntityInteraction::CampfireSit);

                // Interaction using [`GameInput::Trade`]
                // TODO: Remove this once we have a better way do determine whether an entity
                // can be traded with not based on alignment.
                let trade = (!is_downed
                    && alignment.is_some_and(|alignment| match alignment {
                        Alignment::Npc => true,
                        Alignment::Owned(other_uid) => other_uid == uid || player_uid == *other_uid,
                        _ => false,
                    }))
                .then_some(EntityInteraction::Trade);

                // Interaction using [`GameInput::Mount`]
                let mount = (matches!(alignment, Some(Alignment::Owned(other_uid)) if player_uid == *other_uid)
                    && pet::is_mountable(body, mass, player_body, player_mass)
                    && !is_downed
                    && !client.is_riding())
                .then_some(EntityInteraction::Mount);

                // Interaction using [`GameInput::StayFollow`]
                let stayfollow = alignment
                    .filter(|alignment| {
                        matches!(alignment,
                            Alignment::Owned(other_uid) if player_uid == *other_uid)
                            && !is_downed
                    })
                    .map(|_| EntityInteraction::StayFollow);

                // Roughly filter out entities farther than interaction distance
                let distance_squared = player_pos.distance_squared(pos.0);

                Some(
                    interaction
                        .into_iter()
                        .chain(sit)
                        .chain(trade)
                        .chain(mount)
                        .chain(stayfollow)
                        .filter_map(move |interaction| {
                            (distance_squared <= interaction.range().powi(2)).then_some((
                                interaction,
                                entity,
                                distance_squared,
                            ))
                        }),
                )
            },
        )
        .flatten();

    let volumes_data = (
        &entities,
        &ecs.read_storage::<Uid>(),
        &ecs.read_storage::<comp::Body>(),
        &ecs.read_storage::<crate::ecs::comp::Interpolated>(),
        &ecs.read_storage::<comp::Collider>(),
    );

    let mut volumes_data = volumes_data.lend_join();

    let volume_interactables = spacial_grid
        .0
        .in_circle_aabr(player_pos.xy(), MAX_PICKUP_RANGE)
        .filter(|&e| e != player_entity)
        .filter_map(|e| volumes_data.get(e, &entities))
        .filter_map(|(entity, uid, body, interpolated, collider)| {
            let vol = collider.get_vol(&voxel_colliders_manifest)?;
            let (blocks_of_interest, offset) =
                scene
                    .figure_mgr()
                    .get_blocks_of_interest(entity, body, Some(collider))?;

            let mat = Mat4::from(interpolated.ori.to_quat()).translated_3d(interpolated.pos)
                * Mat4::translation_3d(offset);

            let p = mat.inverted().mul_point(player_pos);
            let aabb = Aabb {
                min: Vec3::zero(),
                max: vol.volume().sz.as_(),
            };
            if aabb.contains_point(p) || aabb.distance_to_point(p) < MAX_PICKUP_RANGE {
                Some(blocks_of_interest.interactables.iter().map(
                    move |(block_offset, interaction)| {
                        let wpos = mat.mul_point(block_offset.as_() + 0.5);
                        (wpos, VolumePos::entity(*block_offset, *uid), *interaction)
                    },
                ))
            } else {
                None
            }
        })
        .flatten();

    // TODO: this formula for the number to take was guessed
    // Note: assumes RECT_SIZE.x == RECT_SIZE.y
    let interactable_blocks = Spiral2d::new()
        .take(
            ((MAX_PICKUP_RANGE / TerrainChunk::RECT_SIZE.x as f32).ceil() as usize * 2 + 1).pow(2),
        )
        .flat_map(|offset| {
            let chunk_pos = player_chunk + offset;
            let chunk_voxel_pos =
                Vec3::<i32>::from(chunk_pos * TerrainChunk::RECT_SIZE.map(|e| e as i32));
            scene_terrain
                .get(chunk_pos)
                .map(|data| (data, chunk_voxel_pos))
        })
        .flat_map(|(chunk_data, chunk_pos)| {
            // TODO: maybe we could make this more efficient by putting the
            // interactables is some sort of spatial structure
            chunk_data
                .blocks_of_interest
                .interactables
                .iter()
                .map(move |(block_offset, interaction)| (chunk_pos + block_offset, interaction))
                .map(|(pos, interaction)| {
                    (
                        pos.as_::<f32>() + 0.5,
                        VolumePos::terrain(pos),
                        *interaction,
                    )
                })
        })
        .chain(volume_interactables)
        .filter(|(wpos, volume_pos, interaction)| match interaction {
            Interaction::Mount => {
                !is_volume_rider.contains(player_entity)
                        && wpos.distance_squared(player_pos) < MAX_SPRITE_MOUNT_RANGE.powi(2)
                        // TODO: Use shared volume riders component here
                        && (volume_pos.is_entity()
                            || !is_volume_rider
                                .join()
                                .any(|is_volume_rider| is_volume_rider.pos == *volume_pos))
            },
            Interaction::LightToggle(_) => {
                wpos.distance_squared(player_pos) < MAX_INTERACT_RANGE.powi(2)
            },
            _ => true,
        })
        .chain(
            mine_target
                .map(|t| t.position_int())
                .into_iter()
                .chain(collect_target.map(|t| t.position_int()))
                .map(|pos| (pos.as_(), VolumePos::terrain(pos), Interaction::Collect)),
        )
        .filter_map(|(wpos, volume_pos, interaction)| {
            let (block, interaction) = BlockInteraction::from_block_pos(
                &terrain,
                &id_maps,
                &colliders,
                volume_pos,
                interaction,
            )?;

            let distance = wpos.distance_squared(player_pos);
            (distance <= interaction.range().powi(2)).then_some((
                block,
                volume_pos,
                interaction,
                distance,
            ))
        });

    // Helper to check if an interactable is directly targetted by the player, and
    // should thus be prioritized over non-directly targetted ones.
    let is_direct_target = |interactable: &Interactable| match interactable {
        Interactable::Block {
            volume_pos,
            interaction,
            ..
        } => {
            matches!(
                (mine_target, volume_pos, interaction),
                (Some(target), VolumePos { kind: Volume::Terrain, pos }, BlockInteraction::Mine(_))
                    if target.position_int() == *pos)
                || matches!(
                        (collect_target, volume_pos, interaction),
                        (Some(target), VolumePos { kind: Volume::Terrain, pos }, BlockInteraction::Collect { .. } | BlockInteraction::Unlock { .. })
                            if target.position_int() == *pos)
        },
        Interactable::Entity { entity, .. } => {
            entity_target.is_some_and(|target| target.kind.0 == *entity)
        },
    };

    Ok(interactable_entities
        .map(|(interaction, entity, distance)| {
            (distance.powi(2), Interactable::Entity {
                entity,
                interaction,
            })
        })
        .chain(
            interactable_blocks.map(|(block, volume_pos, interaction, distance_squared)| {
                (distance_squared, Interactable::Block {
                    block,
                    volume_pos,
                    interaction,
                })
            }),
        )
        .fold(HashMap::new(), |mut map, (distance, interaction)| {
            let input = interaction.game_input();

            if map
                .get(&input)
                .map_or(true, |(other_distance, other_interaction)| {
                    (
                        // Prioritize direct targets
                        is_direct_target(other_interaction),
                        other_interaction.priority(),
                        Reverse(*other_distance),
                    ) < (
                        is_direct_target(&interaction),
                        interaction.priority(),
                        Reverse(distance),
                    )
                })
            {
                map.insert(input, (distance, interaction));
            }

            map
        }))
}

impl Interactable {
    pub fn game_input(&self) -> GameInput {
        match self {
            Interactable::Block { interaction, .. } => interaction.game_input(),
            Interactable::Entity { interaction, .. } => interaction.game_input(),
        }
    }

    /// Priorities for different interactions. Note: Priorities are grouped by
    /// GameInput
    #[rustfmt::skip]
    pub fn priority(&self) -> usize {
        match self {
            // GameInput::Interact
            Self::Entity { interaction: EntityInteraction::ActivatePortal, .. }  => 4,
            Self::Entity { interaction: EntityInteraction::PickupItem, .. }      => 3,
            Self::Block  { interaction: BlockInteraction::Craft(_), .. }         => 3,
            Self::Block  { interaction: BlockInteraction::Collect { .. }, .. }   => 3,
            Self::Entity { interaction: EntityInteraction::HelpDowned, .. }      => 2,
            Self::Block  { interaction: BlockInteraction::Unlock(_), .. }        => 1,
            Self::Block  { interaction: BlockInteraction::Read(_), .. }          => 1,
            Self::Block  { interaction: BlockInteraction::LightToggle(_), .. }   => 1,
            Self::Entity { interaction: EntityInteraction::Pet, .. }             => 0,
            Self::Entity { interaction: EntityInteraction::Talk , .. }           => 0,

            // GameInput::Mount
            Self::Entity { interaction: EntityInteraction::Mount, .. }           => 1,
            Self::Block  { interaction: BlockInteraction::Mount, .. }            => 0,

            // These interactions have dedicated keybinds already, no need to prioritize them
            Self::Block  { interaction: BlockInteraction::Mine(_), .. }          => 0,
            Self::Entity { interaction: EntityInteraction::StayFollow, .. }      => 0,
            Self::Entity { interaction: EntityInteraction::Trade, .. }           => 0,
            Self::Entity { interaction: EntityInteraction::CampfireSit, .. }     => 0,
        }
    }
}

impl EntityInteraction {
    pub fn game_input(&self) -> GameInput {
        match self {
            EntityInteraction::HelpDowned
            | EntityInteraction::PickupItem
            | EntityInteraction::ActivatePortal
            | EntityInteraction::Pet
            | EntityInteraction::Talk => GameInput::Interact,
            EntityInteraction::StayFollow => GameInput::StayFollow,
            EntityInteraction::Trade => GameInput::Trade,
            EntityInteraction::Mount => GameInput::Mount,
            EntityInteraction::CampfireSit => GameInput::Sit,
        }
    }

    pub fn range(&self) -> f32 {
        match self {
            Self::Trade => consts::MAX_TRADE_RANGE,
            Self::Mount | Self::Pet => consts::MAX_MOUNT_RANGE,
            Self::PickupItem => consts::MAX_PICKUP_RANGE,
            Self::Talk => consts::MAX_NPCINTERACT_RANGE,
            _ => consts::MAX_INTERACT_RANGE,
        }
    }
}

impl Interactables {
    /// Maps the interaction targets to all their available interactions
    pub fn inverted_map(
        &self,
    ) -> (
        HashMap<specs::Entity, Vec<EntityInteraction>>,
        HashMap<VolumePos, (Block, Vec<&BlockInteraction>)>,
    ) {
        let (mut entity_map, block_map) = self.input_map.iter().fold(
            (HashMap::new(), HashMap::new()),
            |(mut entity_map, mut block_map), (_input, (_, interactable))| {
                match interactable {
                    Interactable::Entity {
                        entity,
                        interaction,
                    } => {
                        entity_map
                            .entry(*entity)
                            .and_modify(|i: &mut Vec<_>| i.push(*interaction))
                            .or_insert_with(|| vec![*interaction]);
                    },
                    Interactable::Block {
                        block,
                        volume_pos,
                        interaction,
                    } => {
                        block_map
                            .entry(*volume_pos)
                            .and_modify(|(_, i): &mut (_, Vec<_>)| i.push(interaction))
                            .or_insert_with(|| (*block, vec![interaction]));
                    },
                }

                (entity_map, block_map)
            },
        );

        // Ensure interactions are ordered in a stable way
        // TODO: Once blocks can have more than one interaction, do the same for blocks
        // here too
        for v in entity_map.values_mut() {
            v.sort_unstable_by_key(|i| i.game_input())
        }

        (entity_map, block_map)
    }
}