Made /rtsim_npc order NPCs by distance

This commit is contained in:
Joshua Barretto 2023-04-06 21:27:34 +01:00
parent 70538dae66
commit 326aa3f8ff
8 changed files with 36 additions and 18 deletions

View File

@ -695,8 +695,9 @@ impl ServerChatCommand {
Some(Moderator),
),
ServerChatCommand::RtsimNpc => cmd(
vec![Any("query", Required)],
"List rtsim NPCs that fit a given query (e.g: simulated,merchant)",
vec![Any("query", Required), Integer("max number", 20, Optional)],
"List rtsim NPCs that fit a given query (e.g: simulated,merchant) in order of \
distance",
Some(Moderator),
),
ServerChatCommand::RtsimPurge => cmd(

View File

@ -2,8 +2,8 @@
mod tests {
use common::{
comp::{
item::MaterialStatManifest, skills::GeneralSkill, tool::AbilityMap, CharacterState,
Controller, Energy, Ori, PhysicsState, Poise, Pos, Skill, Stats, Vel,
item::MaterialStatManifest, skills::GeneralSkill, tool::AbilityMap, CharacterActivity,
CharacterState, Controller, Energy, Ori, PhysicsState, Poise, Pos, Skill, Stats, Vel,
},
resources::{DeltaTime, GameMode, Time},
shared_server_config::ServerConstants,
@ -53,6 +53,7 @@ mod tests {
.ecs_mut()
.create_entity()
.with(CharacterState::Idle(common::states::idle::Data::default()))
.with(CharacterActivity::default())
.with(Pos(Vec3::zero()))
.with(Vel::default())
.with(ori)
@ -116,8 +117,12 @@ mod tests {
for i in 0..TESTCASES {
if let Some(e) = entities[i] {
let result = Dir::from(*results.get(e).expect("Ori missing"));
assert!(result.abs_diff_eq(&testcases[i].1, 0.0005));
// println!("{:?}", result);
assert!(
result.abs_diff_eq(&testcases[i].1, 0.0005),
"{:?} != {:?}",
result,
testcases[i].1
);
}
}
}

View File

@ -128,11 +128,11 @@ fn fall_dt_speed_diff() -> Result<(), Box<dyn Error>> {
assert_relative_eq!(svel.0.z, -4.9847627, epsilon = EPSILON);
assert_relative_eq!(fpos.0.x, 16.0);
assert_relative_eq!(fpos.0.y, 16.0);
assert_relative_eq!(fpos.0.z, 264.25073, epsilon = EPSILON);
assert_relative_eq!(fpos.0.z, 264.25067, epsilon = EPSILON);
assert_relative_eq!(fvel.0.z, -4.9930925, epsilon = EPSILON);
// Diff after 200ms
assert_relative_eq!((spos.0.z - fpos.0.z).abs(), 0.2253418, epsilon = EPSILON);
assert_relative_eq!((spos.0.z - fpos.0.z).abs(), 0.22540283, epsilon = EPSILON);
assert_relative_eq!((svel.0.z - fvel.0.z).abs(), 0.008329868, epsilon = EPSILON);
Ok(())

View File

@ -3,8 +3,8 @@ use common::{
inventory::item::MaterialStatManifest,
skills::{GeneralSkill, Skill},
tool::AbilityMap,
Auras, Buffs, CharacterState, Collider, Combo, Controller, Energy, Health, Ori, Pos, Stats,
Vel,
Auras, Buffs, CharacterActivity, CharacterState, Collider, Combo, Controller, Energy,
Health, Ori, Pos, Stats, Vel,
},
resources::{DeltaTime, GameMode, Time},
shared_server_config::ServerConstants,
@ -123,6 +123,7 @@ pub fn create_player(state: &mut State) -> Entity {
.with(body)
.with(Controller::default())
.with(CharacterState::default())
.with(CharacterActivity::default())
.with(Buffs::default())
.with(Combo::default())
.with(Auras::default())

View File

@ -226,6 +226,7 @@ impl Npc {
// Clear reports that have been forgotten
self.known_reports
.retain(|report| reports.contains_key(*report));
// TODO: Limit number of reports
// TODO: Clear old inbox items
}
}

View File

@ -58,6 +58,7 @@ impl Reports {
// Forget reports that are too old
self.reports
.retain(|_, report| (current_time.0 - report.at.0).max(0.0) < report.remember_for());
// TODO: Limit global number of reports
}
}

View File

@ -51,6 +51,7 @@ impl Site {
// Clear reports that have been forgotten
self.known_reports
.retain(|report| reports.contains_key(*report));
// TODO: Limit number of reports
}
}

View File

@ -1268,12 +1268,12 @@ fn handle_rtsim_info(
fn handle_rtsim_npc(
server: &mut Server,
client: EcsEntity,
_target: EcsEntity,
target: EcsEntity,
args: Vec<String>,
action: &ServerChatCommand,
) -> CmdResult<()> {
use crate::rtsim::RtSim;
if let Some(query) = parse_cmd_args!(args, String) {
if let (Some(query), count) = parse_cmd_args!(args, String, u32) {
let terms = query
.split(',')
.map(|s| s.trim().to_lowercase())
@ -1281,7 +1281,7 @@ fn handle_rtsim_npc(
let rtsim = server.state.ecs().read_resource::<RtSim>();
let data = rtsim.state().data();
let npcs = data
let mut npcs = data
.npcs
.values()
.enumerate()
@ -1294,20 +1294,28 @@ fn handle_rtsim_npc(
format!("{:?}", npc.mode),
format!("{}", idx),
];
terms
.iter()
.all(|term| tags.iter().any(|tag| term.eq_ignore_ascii_case(tag.trim())))
terms.iter().all(|term| {
term == "" || tags.iter().any(|tag| term.eq_ignore_ascii_case(tag.trim()))
})
})
.collect::<Vec<_>>();
if let Ok(pos) = position(server, target, "target") {
npcs.sort_by_key(|(_, npc)| (npc.wpos.distance_squared(pos.0) * 10.0) as u64);
}
let mut info = String::new();
let _ = writeln!(&mut info, "-- NPCs matching [{}] --", terms.join(", "));
for (idx, _) in &npcs {
for (idx, _) in npcs.iter().take(count.unwrap_or(!0) as usize) {
let _ = write!(&mut info, "{}, ", idx);
}
let _ = writeln!(&mut info);
let _ = writeln!(&mut info, "Matched {} NPCs.", npcs.len());
let _ = writeln!(
&mut info,
"Showing {}/{} matching NPCs.",
count.unwrap_or(npcs.len() as u32),
npcs.len()
);
server.notify_client(
client,