Skip to content

Commit

Permalink
Implement auto pickup timed event
Browse files Browse the repository at this point in the history
  • Loading branch information
sorokya committed Sep 11, 2024
1 parent e8f19d0 commit 54822e1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/map/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub enum Command {
TimedEvacuate,
TimedDropProtection,
TimedGhost,
TimedAutoPickup,
ToggleHidden {
player_id: i32,
},
Expand Down
2 changes: 2 additions & 0 deletions src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ impl Map {

Command::TimedGhost => self.timed_ghost(),

Command::TimedAutoPickup => self.timed_auto_pickup(),

Command::ToggleHidden { player_id } => self.toggle_hidden(player_id),

Command::ActNpcs => self.act_npcs(),
Expand Down
1 change: 1 addition & 0 deletions src/map/map/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod recover_players;
mod spawn_items;
mod spawn_npcs;
mod timed_arena;
mod timed_auto_pickup;
mod timed_door_close;
mod timed_drain;
mod timed_drop_protection;
Expand Down
34 changes: 34 additions & 0 deletions src/map/map/events/timed_auto_pickup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{utils::get_distance, SETTINGS};

use super::super::Map;

impl Map {
pub fn timed_auto_pickup(&mut self) {
let mut matched: Vec<(i32, i32)> = Vec::new();

for (item_index, item) in self.items.iter() {
if let Some(player_id) = self
.characters
.iter()
.filter(|(_, character)| {
let distance = get_distance(&item.coords, &character.coords);
!character.captcha_open
&& character.auto_pickup_items.contains(&item.id)
&& distance <= SETTINGS.world.drop_distance
})
.min_by(|(_, a), (_, b)| {
let distance_a = get_distance(&item.coords, &a.coords);
let distance_b = get_distance(&item.coords, &b.coords);
distance_a.cmp(&distance_b)
})
.map(|(player_id, _)| *player_id)
{
matched.push((*item_index, player_id));
}
}

for (item_index, player_id) in matched {
self.get_item(player_id, item_index);
}
}
}
4 changes: 4 additions & 0 deletions src/map/map_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,10 @@ impl MapHandle {
let _ = self.tx.send(Command::TimedGhost);
}

pub fn timed_auto_pickup(&self) {
let _ = self.tx.send(Command::TimedAutoPickup);
}

pub fn toggle_hidden(&self, player_id: i32) {
let _ = self.tx.send(Command::ToggleHidden { player_id });
}
Expand Down
2 changes: 2 additions & 0 deletions src/world/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct World {
spike_ticks: i32,
drain_ticks: i32,
second_ticks: i32,
auto_pickup_ticks: i32,
global_locked: bool,
connection_log: ConnectionLog,
}
Expand Down Expand Up @@ -66,6 +67,7 @@ impl World {
spike_ticks: 0,
drain_ticks: 0,
second_ticks: 0,
auto_pickup_ticks: 0,
global_locked: false,
connection_log: ConnectionLog::new(),
}
Expand Down
12 changes: 12 additions & 0 deletions src/world/world/tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ impl World {
None => return,
};

if SETTINGS.auto_pickup.enabled {
self.auto_pickup_ticks += 1;
}

self.second_ticks += 1;
self.npc_act_ticks += 1;
self.item_spawn_ticks += 1;
Expand All @@ -31,6 +35,10 @@ impl World {
map.act_npcs();
}

if self.auto_pickup_ticks >= SETTINGS.auto_pickup.rate && SETTINGS.auto_pickup.enabled {
map.timed_auto_pickup();
}

if self.second_ticks >= ONE_SECOND {
map.spawn_npcs();
map.timed_warp_suck();
Expand Down Expand Up @@ -74,6 +82,10 @@ impl World {
self.second_ticks = 0;
}

if self.auto_pickup_ticks >= SETTINGS.auto_pickup.rate && SETTINGS.auto_pickup.enabled {
self.auto_pickup_ticks = 0;
}

if self.npc_act_ticks >= SETTINGS.npcs.act_rate {
self.npc_act_ticks = 0;
}
Expand Down

0 comments on commit 54822e1

Please sign in to comment.