diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 9ed396771b..f8536da0fd 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -118,6 +118,12 @@ lazy_static! { "/build : Toggles build mode on and off", handle_build, ), + ChatCommand::new( + "killnpcs", + "{}", + "/killnpcs : Kill the NPCs", + handle_killnpcs, + ), ]; } @@ -407,3 +413,26 @@ fn kind_to_body(kind: NpcKind) -> comp::Body { NpcKind::Wolf => comp::Body::QuadrupedMedium(comp::quadruped_medium::Body::random()), } } + +fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { + let ecs = server.state.ecs(); + let mut npclist=Vec::new(); + { + // get the npc list, scope read access to prevent + // 'Already borrowed: InvalidBorrow' error when setting health stat + let entities = ecs.entities(); + let stats = ecs.read_storage::(); + let players = ecs.read_storage::(); + for (npc,stat,()) in (&entities,&stats,!&players).join() { + npclist.push((npc,stat.name.clone())); + } + } + for npc in npclist { + let mut text = npc.1.clone(); + text += " is no more."; + server.clients.notify(entity, ServerMsg::Chat(text)); + ecs.write_storage::() + .get_mut(npc.0) + .map(|s| s.health.set_to(0, comp::HealthSource::Command)); + } +}