From 75bbe619fdfdf07f357c12c5306abf0efe5d4f22 Mon Sep 17 00:00:00 2001 From: Aleksandr Nariadchikov Date: Mon, 6 Sep 2021 18:21:32 +0300 Subject: [PATCH 1/3] [#1251] /kill_npcs admin command does not kill player's pets now without additional argument "--also-pets" --- server/src/cmd.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index e17ef01967..bb2a88da91 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -56,6 +56,7 @@ use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement}; use world::util::Sampler; use tracing::{error, info, warn}; +use common::comp::Alignment; pub trait ChatCommandExt { fn execute(&self, server: &mut Server, entity: EcsEntity, args: Vec); @@ -617,7 +618,6 @@ fn handle_make_npc( // Some would say it's a hack, some would say it's incomplete // simulation. But this is what we do to avoid PvP between npc. - use comp::Alignment; let npc_group = match alignment { Alignment::Enemy => Some(comp::group::ENEMY), Alignment::Npc | Alignment::Tame => Some(comp::group::NPC), @@ -1667,22 +1667,49 @@ fn handle_kill_npcs( server: &mut Server, client: EcsEntity, _target: EcsEntity, - _args: Vec, + args: Vec, _action: &ChatCommand, ) -> CmdResult<()> { + let mut kill_pets = false; + + if let Some(kill_option) = parse_args!(args, String) { + kill_pets = kill_option.contains("--also-pets"); + } + let ecs = server.state.ecs(); let mut healths = ecs.write_storage::(); let players = ecs.read_storage::(); + let alignments = ecs.read_storage::(); let mut count = 0; - for (mut health, ()) in (&mut healths, !&players).join() { - count += 1; - health.set_to(0, comp::HealthSource::Command); + + for (mut health, (), alignment) in (&mut healths, !&players, &alignments).join() { + let mut should_kill = true; + + if !kill_pets { + match alignment { + Alignment::Owned(uid) => { + if let Some(owner) = ecs.entity_from_uid(uid.0) { + if players.contains(owner) { + should_kill = false; + } + } + } + _ => (), + } + } + + if should_kill { + count += 1; + health.set_to(0, comp::HealthSource::Command); + } } + let text = if count > 0 { format!("Destroyed {} NPCs.", count) } else { "No NPCs on server.".to_string() }; + server.notify_client( client, ServerGeneral::server_msg(ChatType::CommandInfo, text), From c22104ea56974c6f569a4fa90f24d2a62b9c725b Mon Sep 17 00:00:00 2001 From: Aleksandr Nariadchikov Date: Mon, 6 Sep 2021 19:31:12 +0300 Subject: [PATCH 2/3] [#1251] Mutable variables are removed. For killing checking non players without alignments as well as with it. --- server/src/cmd.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index bb2a88da91..db6c23c485 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -1670,11 +1670,11 @@ fn handle_kill_npcs( args: Vec, _action: &ChatCommand, ) -> CmdResult<()> { - let mut kill_pets = false; - - if let Some(kill_option) = parse_args!(args, String) { - kill_pets = kill_option.contains("--also-pets"); - } + let kill_pets = if let Some(kill_option) = parse_args!(args, String) { + kill_option.contains("--also-pets") + } else { + false + }; let ecs = server.state.ecs(); let mut healths = ecs.write_storage::(); @@ -1682,21 +1682,15 @@ fn handle_kill_npcs( let alignments = ecs.read_storage::(); let mut count = 0; - for (mut health, (), alignment) in (&mut healths, !&players, &alignments).join() { - let mut should_kill = true; - - if !kill_pets { - match alignment { - Alignment::Owned(uid) => { - if let Some(owner) = ecs.entity_from_uid(uid.0) { - if players.contains(owner) { - should_kill = false; - } - } - } - _ => (), - } - } + for (mut health, (), alignment) + in (&mut healths, !&players, alignments.maybe()).join() { + let should_kill = kill_pets || + if let Some(Alignment::Owned(owned)) = alignment { + ecs.entity_from_uid(owned.0) + .map_or(true, |owner| !players.contains(owner)) + } else { + true + }; if should_kill { count += 1; From 3f31f07cc29efd3b6512857e4cc5e742f3126359 Mon Sep 17 00:00:00 2001 From: Aleksandr Nariadchikov Date: Mon, 6 Sep 2021 20:33:08 +0300 Subject: [PATCH 3/3] [#1251] Code style refactoring --- server/src/cmd.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/server/src/cmd.rs b/server/src/cmd.rs index db6c23c485..2f395fb284 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -55,8 +55,8 @@ use vek::*; use wiring::{Circuit, Wire, WiringAction, WiringActionEffect, WiringElement}; use world::util::Sampler; -use tracing::{error, info, warn}; use common::comp::Alignment; +use tracing::{error, info, warn}; pub trait ChatCommandExt { fn execute(&self, server: &mut Server, entity: EcsEntity, args: Vec); @@ -1682,15 +1682,14 @@ fn handle_kill_npcs( let alignments = ecs.read_storage::(); let mut count = 0; - for (mut health, (), alignment) - in (&mut healths, !&players, alignments.maybe()).join() { - let should_kill = kill_pets || - if let Some(Alignment::Owned(owned)) = alignment { - ecs.entity_from_uid(owned.0) - .map_or(true, |owner| !players.contains(owner)) - } else { - true - }; + for (mut health, (), alignment) in (&mut healths, !&players, alignments.maybe()).join() { + let should_kill = kill_pets + || if let Some(Alignment::Owned(owned)) = alignment { + ecs.entity_from_uid(owned.0) + .map_or(true, |owner| !players.contains(owner)) + } else { + true + }; if should_kill { count += 1;