2019-05-17 09:22:32 +00:00
|
|
|
//! # Implementing new commands.
|
2019-04-16 16:08:36 +00:00
|
|
|
//! To implement a new command, add an instance of `ChatCommand` to `CHAT_COMMANDS`
|
2019-04-16 16:22:44 +00:00
|
|
|
//! and provide a handler function.
|
2019-04-16 16:08:36 +00:00
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
use crate::Server;
|
2019-06-01 19:49:34 +00:00
|
|
|
use common::{
|
|
|
|
comp,
|
|
|
|
msg::ServerMsg,
|
|
|
|
npc::{get_npc_name, NpcKind},
|
|
|
|
};
|
2019-05-12 19:57:39 +00:00
|
|
|
use specs::{Builder, Entity as EcsEntity, Join};
|
2019-04-16 15:38:01 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-04-16 16:34:41 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use scan_fmt::scan_fmt;
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Struct representing a command that a user can run from server chat.
|
2019-04-16 15:38:01 +00:00
|
|
|
pub struct ChatCommand {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// The keyword used to invoke the command, omitting the leading '/'.
|
2019-04-16 15:38:01 +00:00
|
|
|
pub keyword: &'static str,
|
2019-05-17 09:22:32 +00:00
|
|
|
/// A format string for parsing arguments.
|
2019-04-16 15:38:01 +00:00
|
|
|
arg_fmt: &'static str,
|
2019-05-17 09:22:32 +00:00
|
|
|
/// A message that explains how the command is used.
|
2019-04-16 15:38:01 +00:00
|
|
|
help_string: &'static str,
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Handler function called when the command is executed.
|
2019-04-16 16:22:44 +00:00
|
|
|
/// # Arguments
|
2019-05-17 09:22:32 +00:00
|
|
|
/// * `&mut Server` - the `Server` instance executing the command.
|
|
|
|
/// * `EcsEntity` - an `Entity` corresponding to the player that invoked the command.
|
|
|
|
/// * `String` - a `String` containing the part of the command after the keyword.
|
|
|
|
/// * `&ChatCommand` - the command to execute with the above arguments.
|
|
|
|
/// Handler functions must parse arguments from the the given `String` (`scan_fmt!` is included for this purpose).
|
2019-04-16 15:38:01 +00:00
|
|
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatCommand {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Creates a new chat command.
|
2019-04-16 15:38:01 +00:00
|
|
|
pub fn new(
|
|
|
|
keyword: &'static str,
|
|
|
|
arg_fmt: &'static str,
|
|
|
|
help_string: &'static str,
|
|
|
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
keyword,
|
|
|
|
arg_fmt,
|
|
|
|
help_string,
|
|
|
|
handler,
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Calls the contained handler function, passing `&self` as the last argument.
|
2019-04-16 15:38:01 +00:00
|
|
|
pub fn execute(&self, server: &mut Server, entity: EcsEntity, args: String) {
|
|
|
|
(self.handler)(server, entity, args, self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Static list of chat commands available to the server.
|
2019-04-16 15:38:01 +00:00
|
|
|
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
|
|
|
|
ChatCommand::new(
|
|
|
|
"jump",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/jump <dx> <dy> <dz> : Offset your current position",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_jump
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"goto",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/goto <x> <y> <z> : Teleport to a position",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_goto
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"alias",
|
|
|
|
"{}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/alias <name> : Change your alias",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_alias
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"tp",
|
|
|
|
"{}",
|
2019-05-11 12:43:19 +00:00
|
|
|
"/tp <alias> : Teleport to another player",
|
2019-04-16 15:38:01 +00:00
|
|
|
handle_tp
|
|
|
|
),
|
2019-05-19 21:54:09 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"kill",
|
|
|
|
"{}",
|
|
|
|
"/kill : Kill yourself",
|
|
|
|
handle_kill
|
|
|
|
),
|
2019-05-11 12:43:19 +00:00
|
|
|
ChatCommand::new(
|
2019-06-15 07:54:47 +00:00
|
|
|
"spawn",
|
|
|
|
"{} {} {d}",
|
2019-06-15 08:15:04 +00:00
|
|
|
"/spawn <alignment> <entity> [amount] : Spawn a test entity",
|
2019-06-15 07:54:47 +00:00
|
|
|
handle_spawn
|
2019-05-27 11:18:14 +00:00
|
|
|
),
|
2019-05-17 09:22:32 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"help", "", "/help: Display this message", handle_help)
|
2019-04-16 15:38:01 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
|
|
|
(Some(x), Some(y), Some(z)) => {
|
2019-04-16 16:34:41 +00:00
|
|
|
match server
|
2019-04-16 15:38:01 +00:00
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(entity)
|
|
|
|
{
|
2019-04-17 13:00:24 +00:00
|
|
|
Some(current_pos) => {
|
2019-04-29 20:37:19 +00:00
|
|
|
server.state.write_component(
|
|
|
|
entity,
|
|
|
|
comp::phys::Pos(current_pos.0 + Vec3::new(x, y, z)),
|
|
|
|
);
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
}
|
2019-04-16 16:34:41 +00:00
|
|
|
None => server.clients.notify(
|
2019-04-16 15:38:01 +00:00
|
|
|
entity,
|
2019-05-17 09:22:32 +00:00
|
|
|
ServerMsg::Chat(String::from("Command 'jump' invalid in current state.")),
|
2019-04-16 16:34:41 +00:00
|
|
|
),
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let (opt_x, opt_y, opt_z) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32);
|
|
|
|
match (opt_x, opt_y, opt_z) {
|
2019-04-17 13:00:24 +00:00
|
|
|
(Some(x), Some(y), Some(z)) => {
|
2019-04-29 20:37:19 +00:00
|
|
|
server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::phys::Pos(Vec3::new(x, y, z)));
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
_ => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:48:41 +00:00
|
|
|
fn handle_kill(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
2019-05-19 21:54:09 +00:00
|
|
|
server
|
|
|
|
.state
|
2019-05-25 21:13:38 +00:00
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Stats>()
|
|
|
|
.get_mut(entity)
|
2019-05-27 19:41:24 +00:00
|
|
|
.map(|s| s.hp.set_to(0, comp::HealthSource::Suicide));
|
2019-05-19 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
|
|
|
|
match opt_alias {
|
2019-05-19 00:45:02 +00:00
|
|
|
Some(alias) => {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Player>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|player| player.alias = alias);
|
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let opt_alias = scan_fmt!(&args, action.arg_fmt, String);
|
|
|
|
match opt_alias {
|
|
|
|
Some(alias) => {
|
2019-04-22 08:20:25 +00:00
|
|
|
let ecs = server.state.ecs();
|
2019-04-16 15:38:01 +00:00
|
|
|
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::player::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
match opt_player {
|
|
|
|
Some(player) => match server
|
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(player)
|
|
|
|
{
|
2019-04-17 13:00:24 +00:00
|
|
|
Some(pos) => {
|
|
|
|
server.state.write_component(entity, pos);
|
2019-04-29 20:37:19 +00:00
|
|
|
server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::phys::ForceUpdate);
|
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
None => server.clients.notify(
|
|
|
|
entity,
|
2019-05-17 09:22:32 +00:00
|
|
|
ServerMsg::Chat(format!("Unable to teleport to player '{}'!", alias)),
|
2019-04-16 15:38:01 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
server.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
|
|
|
server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-06-15 08:15:04 +00:00
|
|
|
let (opt_align, opt_id, opt_amount) = scan_fmt!(&args, action.arg_fmt, String, NpcKind, String);
|
|
|
|
// This should be just an enum and be handled with scan_fmt!
|
2019-06-15 07:54:47 +00:00
|
|
|
let opt_agent = alignment_to_agent(&opt_align.unwrap_or(String::new()), entity);
|
2019-06-15 08:15:04 +00:00
|
|
|
|
|
|
|
// Make sure the amount is either not provided or a valid value
|
2019-06-15 13:42:39 +00:00
|
|
|
let opt_amount = opt_amount
|
|
|
|
.and_then(|a| a.parse().ok())
|
|
|
|
.or(Some(1))
|
|
|
|
.and_then(|a| if a > 0 { Some(a) } else { None });
|
2019-06-15 08:15:04 +00:00
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
match (opt_agent, opt_id, opt_amount) {
|
|
|
|
(Some(agent), Some(id), Some(amount)) => {
|
|
|
|
match server
|
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::phys::Pos>(entity)
|
|
|
|
{
|
|
|
|
Some(mut pos) => {
|
|
|
|
pos.0.x += 1.0; // Temp fix TODO: Solve NaN issue with positions of pets
|
|
|
|
let body = kind_to_body(id);
|
|
|
|
for _ in 0..amount {
|
|
|
|
server
|
2019-06-15 11:48:14 +00:00
|
|
|
.create_npc(pos, get_npc_name(id), body)
|
2019-06-15 07:54:47 +00:00
|
|
|
.with(agent)
|
|
|
|
.build();
|
|
|
|
}
|
2019-06-15 11:48:14 +00:00
|
|
|
server.clients.notify(
|
|
|
|
entity,
|
|
|
|
ServerMsg::Chat(format!("Spawned {} entities", amount).to_owned()),
|
|
|
|
);
|
2019-06-15 07:54:47 +00:00
|
|
|
}
|
|
|
|
None => server
|
|
|
|
.clients
|
|
|
|
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())),
|
|
|
|
}
|
2019-06-15 11:48:14 +00:00
|
|
|
}
|
2019-06-15 07:54:47 +00:00
|
|
|
_ => server
|
2019-05-12 21:34:20 +00:00
|
|
|
.clients
|
2019-06-15 07:54:47 +00:00
|
|
|
.notify(entity, ServerMsg::Chat(String::from(action.help_string))),
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 03:01:52 +00:00
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
|
|
|
for cmd in CHAT_COMMANDS.iter() {
|
|
|
|
server
|
2019-05-26 03:31:41 +00:00
|
|
|
.clients
|
2019-06-15 07:54:47 +00:00
|
|
|
.notify(entity, ServerMsg::Chat(String::from(cmd.help_string)));
|
2019-05-26 03:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 03:01:52 +00:00
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
fn alignment_to_agent(alignment: &str, target: EcsEntity) -> Option<comp::Agent> {
|
|
|
|
match alignment {
|
|
|
|
"hostile" => Some(comp::Agent::Enemy { target: None }),
|
2019-06-15 11:48:14 +00:00
|
|
|
"friendly" => Some(comp::Agent::Pet {
|
|
|
|
target,
|
|
|
|
offset: Vec2::zero(),
|
|
|
|
}),
|
2019-06-15 09:32:07 +00:00
|
|
|
// passive?
|
2019-06-15 11:48:14 +00:00
|
|
|
_ => None,
|
2019-05-27 11:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 03:01:52 +00:00
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
fn kind_to_body(kind: NpcKind) -> comp::Body {
|
|
|
|
match kind {
|
|
|
|
NpcKind::Humanoid => comp::Body::Humanoid(comp::HumanoidBody::random()),
|
|
|
|
NpcKind::Pig => comp::Body::Quadruped(comp::QuadrupedBody::random()),
|
|
|
|
NpcKind::Wolf => comp::Body::QuadrupedMedium(comp::QuadrupedMediumBody::random()),
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2019-04-17 13:00:24 +00:00
|
|
|
}
|