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-10-20 05:19:50 +00:00
|
|
|
use crate::{Server, StateExt};
|
2019-07-26 14:38:31 +00:00
|
|
|
use chrono::{NaiveTime, Timelike};
|
2019-06-01 19:49:34 +00:00
|
|
|
use common::{
|
2019-10-24 21:05:10 +00:00
|
|
|
assets, comp,
|
2019-08-25 16:48:12 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2019-06-01 19:49:34 +00:00
|
|
|
msg::ServerMsg,
|
|
|
|
npc::{get_npc_name, NpcKind},
|
2019-07-03 19:56:54 +00:00
|
|
|
state::TimeOfDay,
|
2019-10-16 11:39:41 +00:00
|
|
|
terrain::TerrainChunkSize,
|
|
|
|
vol::RectVolSize,
|
2019-06-01 19:49:34 +00:00
|
|
|
};
|
2019-07-21 18:22:13 +00:00
|
|
|
use rand::Rng;
|
2019-08-01 23:32:33 +00:00
|
|
|
use specs::{Builder, Entity as EcsEntity, Join};
|
2019-04-16 15:38:01 +00:00
|
|
|
use vek::*;
|
2019-10-16 11:39:41 +00:00
|
|
|
use world::util::Sampler;
|
2019-04-16 15:38:01 +00:00
|
|
|
|
2019-04-16 16:34:41 +00:00
|
|
|
use lazy_static::lazy_static;
|
2019-07-29 13:42:26 +00:00
|
|
|
use scan_fmt::{scan_fmt, scan_fmt_some};
|
2019-07-26 14:38:31 +00:00
|
|
|
|
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-08-15 14:33:00 +00:00
|
|
|
/// A boolean that is used to check whether the command requires administrator permissions or not.
|
|
|
|
needs_admin: bool,
|
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,
|
2019-08-15 14:33:00 +00:00
|
|
|
needs_admin: bool,
|
2019-04-16 15:38:01 +00:00
|
|
|
handler: fn(&mut Server, EcsEntity, String, &ChatCommand),
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
keyword,
|
|
|
|
arg_fmt,
|
|
|
|
help_string,
|
2019-08-15 14:33:00 +00:00
|
|
|
needs_admin,
|
2019-04-16 15:38:01 +00:00
|
|
|
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) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if self.needs_admin {
|
|
|
|
if !server.entity_is_admin(entity) {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
2019-10-12 21:20:37 +00:00
|
|
|
ServerMsg::private(format!(
|
|
|
|
"You don't have permission to use '/{}'.",
|
|
|
|
self.keyword
|
2019-08-18 18:07:21 +00:00
|
|
|
)),
|
2019-08-15 14:33:00 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
(self.handler)(server, entity, args, self);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(self.handler)(server, entity, args, self);
|
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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![
|
2019-10-24 21:05:10 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"giveitem",
|
|
|
|
"{d}",
|
|
|
|
"/giveitem <name> : Give yourself an item.",
|
|
|
|
true,
|
|
|
|
handle_give,),
|
2019-04-16 15:38:01 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"jump",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/jump <dx> <dy> <dz> : Offset your current position",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_jump,
|
2019-04-16 15:38:01 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"goto",
|
|
|
|
"{d} {d} {d}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/goto <x> <y> <z> : Teleport to a position",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_goto,
|
2019-04-16 15:38:01 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"alias",
|
|
|
|
"{}",
|
2019-04-17 13:00:24 +00:00
|
|
|
"/alias <name> : Change your alias",
|
2019-08-15 14:33:00 +00:00
|
|
|
false,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_alias,
|
2019-04-16 15:38:01 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"tp",
|
|
|
|
"{}",
|
2019-05-11 12:43:19 +00:00
|
|
|
"/tp <alias> : Teleport to another player",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_tp,
|
2019-04-16 15:38:01 +00:00
|
|
|
),
|
2019-05-19 21:54:09 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"kill",
|
|
|
|
"{}",
|
|
|
|
"/kill : Kill yourself",
|
2019-08-15 14:33:00 +00:00
|
|
|
false,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_kill,
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"time",
|
|
|
|
"{} {s}",
|
2019-07-29 14:40:46 +00:00
|
|
|
"/time <XY:XY> or [Time of day] : Set the time of day",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_time,
|
2019-05-19 21:54:09 +00:00
|
|
|
),
|
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-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-06-21 08:42:16 +00:00
|
|
|
handle_spawn,
|
2019-05-27 11:18:14 +00:00
|
|
|
),
|
2019-06-11 04:24:35 +00:00
|
|
|
ChatCommand::new(
|
2019-06-29 12:05:30 +00:00
|
|
|
"players",
|
2019-06-11 04:24:35 +00:00
|
|
|
"{}",
|
2019-07-29 14:40:46 +00:00
|
|
|
"/players : Lists players currently online",
|
2019-08-15 14:33:00 +00:00
|
|
|
false,
|
2019-06-29 12:05:30 +00:00
|
|
|
handle_players,
|
2019-06-11 04:24:35 +00:00
|
|
|
),
|
2019-05-17 09:22:32 +00:00
|
|
|
ChatCommand::new(
|
2019-08-15 14:33:00 +00:00
|
|
|
"help", "", "/help: Display this message", false, handle_help),
|
2019-07-01 20:07:30 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"health",
|
|
|
|
"{}",
|
|
|
|
"/health : Set your current health",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-07-01 20:07:30 +00:00
|
|
|
handle_health,
|
2019-07-02 18:19:16 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"build",
|
|
|
|
"",
|
|
|
|
"/build : Toggles build mode on and off",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-07-02 18:19:16 +00:00
|
|
|
handle_build,
|
|
|
|
),
|
2019-07-13 04:25:44 +00:00
|
|
|
ChatCommand::new(
|
2019-07-18 15:19:44 +00:00
|
|
|
"tell",
|
2019-07-13 04:25:44 +00:00
|
|
|
"{}",
|
2019-07-18 15:19:44 +00:00
|
|
|
"/tell <alias> <message>: Send a message to another player",
|
2019-08-15 14:33:00 +00:00
|
|
|
false,
|
2019-07-18 15:19:44 +00:00
|
|
|
handle_tell,
|
2019-07-13 04:25:44 +00:00
|
|
|
),
|
2019-07-17 17:53:10 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"killnpcs",
|
|
|
|
"{}",
|
|
|
|
"/killnpcs : Kill the NPCs",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-07-17 17:53:10 +00:00
|
|
|
handle_killnpcs,
|
2019-07-21 12:42:45 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"object",
|
|
|
|
"{}",
|
2019-07-23 21:43:08 +00:00
|
|
|
"/object [Name]: Spawn an object",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-07-21 12:42:45 +00:00
|
|
|
handle_object,
|
|
|
|
),
|
2019-07-25 20:51:20 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"light",
|
|
|
|
"{} {} {} {} {} {} {}",
|
2019-07-29 14:40:46 +00:00
|
|
|
"/light <opt: <<cr> <cg> <cb>> <<ox> <oy> <oz>> <<strength>>>: Spawn entity with light",
|
2019-08-15 14:33:00 +00:00
|
|
|
true,
|
2019-07-25 20:51:20 +00:00
|
|
|
handle_light,
|
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"lantern",
|
2019-08-07 17:17:04 +00:00
|
|
|
"{}",
|
2019-07-25 20:51:20 +00:00
|
|
|
"/lantern : adds/remove light near player",
|
2019-08-15 14:33:00 +00:00
|
|
|
false,
|
2019-07-25 20:51:20 +00:00
|
|
|
handle_lantern,
|
|
|
|
),
|
2019-08-07 17:17:04 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"explosion",
|
|
|
|
"{}",
|
|
|
|
"/explosion <radius> : Explodes the ground around you",
|
2019-09-25 20:22:39 +00:00
|
|
|
true,
|
2019-08-07 17:17:04 +00:00
|
|
|
handle_explosion,
|
|
|
|
),
|
2019-09-25 20:22:39 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"waypoint",
|
|
|
|
"{}",
|
|
|
|
"/waypoint : Set your waypoint to your current position",
|
2019-10-04 16:09:19 +00:00
|
|
|
false,
|
2019-09-25 20:22:39 +00:00
|
|
|
handle_waypoint,
|
|
|
|
),
|
2019-08-17 18:16:58 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"adminify",
|
|
|
|
"{}",
|
|
|
|
"/adminify <playername> : Temporarily gives a player admin permissions or removes them",
|
2019-10-04 16:09:19 +00:00
|
|
|
true,
|
2019-08-17 18:16:58 +00:00
|
|
|
handle_adminify,
|
2019-08-26 09:49:14 +00:00
|
|
|
),
|
|
|
|
ChatCommand::new(
|
|
|
|
"debug_column",
|
|
|
|
"{} {}",
|
|
|
|
"/debug_column <x> <y> : Prints some debug information about a column",
|
|
|
|
false,
|
|
|
|
handle_debug_column,
|
|
|
|
),
|
2019-10-04 15:48:14 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"give_exp",
|
|
|
|
"{} {}",
|
|
|
|
"/give_exp <playername> <amount> : Give experience to specified player",
|
|
|
|
true,
|
|
|
|
handle_exp,
|
|
|
|
),
|
2019-10-07 03:19:46 +00:00
|
|
|
ChatCommand::new(
|
|
|
|
"removelights",
|
|
|
|
"{}",
|
|
|
|
"/removelights [radius] : Removes all lights spawned by players",
|
|
|
|
true,
|
|
|
|
handle_remove_lights,
|
|
|
|
),
|
2019-04-16 15:38:01 +00:00
|
|
|
];
|
|
|
|
}
|
2019-10-24 21:05:10 +00:00
|
|
|
fn handle_give(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) {
|
|
|
|
if let Ok(item) = assets::load_cloned(&args) {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Inventory>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|inv| inv.push(item));
|
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::InventoryUpdate>()
|
|
|
|
.insert(entity, comp::InventoryUpdate);
|
|
|
|
} else {
|
|
|
|
server.notify_client(entity, ServerMsg::private(String::from("Invalid item!")));
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 15:51:59 +00:00
|
|
|
fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) {
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(entity) {
|
|
|
|
Some(current_pos) => {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.write_component(entity, comp::Pos(current_pos.0 + Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(entity, comp::ForceUpdate);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
2019-08-18 18:07:21 +00:00
|
|
|
ServerMsg::private(String::from("You have no position.")),
|
2019-08-15 14:33:00 +00:00
|
|
|
),
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_goto(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) {
|
|
|
|
if server
|
|
|
|
.state
|
|
|
|
.read_component_cloned::<comp::Pos>(entity)
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
server
|
2019-07-30 08:10:58 +00:00
|
|
|
.state
|
2019-08-15 14:33:00 +00:00
|
|
|
.write_component(entity, comp::Pos(Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(entity, comp::ForceUpdate);
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
2019-08-18 18:07:21 +00:00
|
|
|
ServerMsg::private(String::from("You have no position.")),
|
2019-08-15 14:33:00 +00:00
|
|
|
);
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-30 11:48:28 +00:00
|
|
|
.map(|s| s.health.set_to(0, comp::HealthSource::Suicide));
|
2019-05-19 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 08:42:16 +00:00
|
|
|
fn handle_time(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let time = scan_fmt_some!(&args, action.arg_fmt, String);
|
|
|
|
let new_time = match time.as_ref().map(|s| s.as_str()) {
|
2019-09-01 19:04:03 +00:00
|
|
|
Some("midnight") => NaiveTime::from_hms(0, 0, 0),
|
|
|
|
Some("night") => NaiveTime::from_hms(20, 0, 0),
|
2019-08-15 14:33:00 +00:00
|
|
|
Some("dawn") => NaiveTime::from_hms(5, 0, 0),
|
2019-09-01 19:04:03 +00:00
|
|
|
Some("morning") => NaiveTime::from_hms(8, 0, 0),
|
|
|
|
Some("day") => NaiveTime::from_hms(10, 0, 0),
|
|
|
|
Some("noon") => NaiveTime::from_hms(12, 0, 0),
|
2019-08-15 14:33:00 +00:00
|
|
|
Some("dusk") => NaiveTime::from_hms(17, 0, 0),
|
|
|
|
Some(n) => match n.parse() {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") {
|
|
|
|
Ok(time) => time,
|
|
|
|
Err(_) => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("'{}' is not a valid time.", n)),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-26 14:38:31 +00:00
|
|
|
},
|
2019-08-15 14:33:00 +00:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let time_in_seconds = server.state.ecs_mut().read_resource::<TimeOfDay>().0;
|
|
|
|
|
|
|
|
let current_time = NaiveTime::from_num_seconds_from_midnight_opt(
|
|
|
|
// Wraps around back to 0s if it exceeds 24 hours (24 hours = 86400s)
|
|
|
|
(time_in_seconds as u64 % 86400) as u32,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
let msg = match current_time {
|
2019-08-18 18:07:21 +00:00
|
|
|
Some(time) => format!("It is {}", time.format("%H:%M").to_string()),
|
2019-08-15 14:33:00 +00:00
|
|
|
None => String::from("Unknown Time"),
|
|
|
|
};
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(msg));
|
2019-08-15 14:33:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
server.state.ecs_mut().write_resource::<TimeOfDay>().0 =
|
|
|
|
new_time.num_seconds_from_midnight() as f64;
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Time changed to: {}",
|
|
|
|
new_time.format("%H:%M").to_string()
|
|
|
|
)),
|
|
|
|
);
|
2019-06-21 08:42:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 20:07:30 +00:00
|
|
|
fn handle_health(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if let Ok(hp) = scan_fmt!(&args, action.arg_fmt, u32) {
|
|
|
|
if let Some(stats) = server
|
|
|
|
.state
|
2019-10-15 04:06:14 +00:00
|
|
|
.ecs()
|
2019-08-15 14:33:00 +00:00
|
|
|
.write_storage::<comp::Stats>()
|
|
|
|
.get_mut(entity)
|
|
|
|
{
|
|
|
|
stats.health.set_to(hp, comp::HealthSource::Command);
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-30 08:10:58 +00:00
|
|
|
entity,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(String::from("You have no health.")),
|
2019-07-30 08:10:58 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("You must specify health amount!")),
|
|
|
|
);
|
2019-07-01 20:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:38:01 +00:00
|
|
|
fn handle_alias(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-07-30 08:10:58 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Player>()
|
|
|
|
.get_mut(entity)
|
|
|
|
.map(|player| player.alias = alias);
|
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
|
|
|
|
let ecs = server.state.ecs();
|
|
|
|
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(entity) {
|
|
|
|
Some(_pos) => match opt_player {
|
|
|
|
Some(player) => match server.state.read_component_cloned::<comp::Pos>(player) {
|
|
|
|
Some(pos) => {
|
|
|
|
server.state.write_component(entity, pos);
|
|
|
|
server.state.write_component(entity, comp::ForceUpdate);
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("Unable to teleport to player '{}'!", alias)),
|
|
|
|
),
|
2019-04-16 15:38:01 +00:00
|
|
|
},
|
|
|
|
None => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from(action.help_string)),
|
|
|
|
);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
},
|
|
|
|
None => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(format!("You have no position!")));
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
match scan_fmt_some!(&args, action.arg_fmt, String, NpcKind, String) {
|
|
|
|
(Some(opt_align), Some(id), opt_amount) => {
|
|
|
|
if let Some(agent) = alignment_to_agent(&opt_align, entity) {
|
|
|
|
let amount = opt_amount
|
|
|
|
.and_then(|a| a.parse().ok())
|
|
|
|
.filter(|x| *x > 0)
|
|
|
|
.unwrap_or(1)
|
|
|
|
.min(10);
|
|
|
|
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(entity) {
|
|
|
|
Some(pos) => {
|
|
|
|
for _ in 0..amount {
|
|
|
|
let vel = Vec3::new(
|
|
|
|
rand::thread_rng().gen_range(-2.0, 3.0),
|
|
|
|
rand::thread_rng().gen_range(-2.0, 3.0),
|
|
|
|
10.0,
|
2019-07-29 13:42:26 +00:00
|
|
|
);
|
2019-08-15 14:33:00 +00:00
|
|
|
|
|
|
|
let body = kind_to_body(id);
|
|
|
|
server
|
2019-10-20 05:19:50 +00:00
|
|
|
.state
|
2019-08-27 19:56:46 +00:00
|
|
|
.create_npc(pos, comp::Stats::new(get_npc_name(id), None), body)
|
2019-08-15 14:33:00 +00:00
|
|
|
.with(comp::Vel(vel))
|
2019-09-09 19:11:40 +00:00
|
|
|
.with(comp::MountState::Unmounted)
|
2019-08-15 14:33:00 +00:00
|
|
|
.with(agent)
|
|
|
|
.build();
|
2019-07-29 13:42:26 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-29 13:42:26 +00:00
|
|
|
entity,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(format!("Spawned {} entities", amount).to_owned()),
|
|
|
|
);
|
2019-06-15 07:54:47 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private("You have no position!".to_owned()),
|
|
|
|
),
|
2019-06-15 07:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-07-29 13:42:26 +00:00
|
|
|
}
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 03:01:52 +00:00
|
|
|
|
2019-06-29 12:05:30 +00:00
|
|
|
fn handle_players(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
2019-06-11 04:24:35 +00:00
|
|
|
let ecs = server.state.ecs();
|
|
|
|
let players = ecs.read_storage::<comp::Player>();
|
|
|
|
let count = players.join().count();
|
2019-07-01 16:38:19 +00:00
|
|
|
let header_message: String = format!("{} online players: \n", count);
|
2019-06-11 04:24:35 +00:00
|
|
|
if count > 0 {
|
2019-06-29 22:16:16 +00:00
|
|
|
let mut player_iter = players.join();
|
2019-10-23 18:23:31 +00:00
|
|
|
let first = player_iter
|
|
|
|
.next()
|
|
|
|
.expect("Player iterator returned none.")
|
|
|
|
.alias
|
|
|
|
.to_owned();
|
2019-06-29 23:05:34 +00:00
|
|
|
let player_list = player_iter.fold(first, |mut s, p| {
|
2019-06-29 22:56:10 +00:00
|
|
|
s += ",\n";
|
|
|
|
s += &p.alias;
|
|
|
|
s
|
|
|
|
});
|
2019-06-29 22:16:16 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(header_message + &player_list));
|
2019-06-11 04:24:35 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(header_message));
|
2019-06-11 04:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 18:19:16 +00:00
|
|
|
fn handle_build(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if server
|
|
|
|
.state
|
|
|
|
.read_storage::<comp::CanBuild>()
|
|
|
|
.get(entity)
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::CanBuild>()
|
|
|
|
.remove(entity);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-14 15:51:59 +00:00
|
|
|
entity,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(String::from("Toggled off build mode!")),
|
2019-08-14 15:51:59 +00:00
|
|
|
);
|
|
|
|
} else {
|
2019-08-15 14:33:00 +00:00
|
|
|
let _ = server
|
2019-07-03 20:46:43 +00:00
|
|
|
.state
|
2019-08-15 14:33:00 +00:00
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::CanBuild>()
|
|
|
|
.insert(entity, comp::CanBuild);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("Toggled on build mode!")),
|
|
|
|
);
|
2019-07-02 18:19:16 +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() {
|
2019-10-21 16:36:35 +00:00
|
|
|
if !cmd.needs_admin || server.entity_is_admin(entity) {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(cmd.help_string)));
|
2019-10-21 16:36:35 +00:00
|
|
|
}
|
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 {
|
2019-08-02 18:56:37 +00:00
|
|
|
"hostile" => Some(comp::Agent::enemy()),
|
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 {
|
2019-06-28 23:42:51 +00:00
|
|
|
NpcKind::Humanoid => comp::Body::Humanoid(comp::humanoid::Body::random()),
|
2019-10-25 01:26:32 +00:00
|
|
|
NpcKind::Pig => comp::Body::QuadrupedSmall(comp::quadruped_small::Body::random()),
|
2019-06-28 23:42:51 +00:00
|
|
|
NpcKind::Wolf => comp::Body::QuadrupedMedium(comp::quadruped_medium::Body::random()),
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2019-04-17 13:00:24 +00:00
|
|
|
}
|
2019-07-13 04:25:44 +00:00
|
|
|
|
2019-07-12 21:16:07 +00:00
|
|
|
fn handle_killnpcs(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let ecs = server.state.ecs();
|
|
|
|
let mut stats = ecs.write_storage::<comp::Stats>();
|
|
|
|
let players = ecs.read_storage::<comp::Player>();
|
|
|
|
let mut count = 0;
|
|
|
|
for (stats, ()) in (&mut stats, !&players).join() {
|
|
|
|
count += 1;
|
|
|
|
stats.health.set_to(0, comp::HealthSource::Command);
|
2019-07-12 21:16:07 +00:00
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
let text = if count > 0 {
|
|
|
|
format!("Destroyed {} NPCs.", count)
|
|
|
|
} else {
|
|
|
|
"No NPCs on server.".to_string()
|
|
|
|
};
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(text));
|
2019-07-12 21:16:07 +00:00
|
|
|
}
|
2019-07-17 17:53:10 +00:00
|
|
|
|
2019-07-23 21:43:08 +00:00
|
|
|
fn handle_object(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let obj_type = scan_fmt!(&args, _action.arg_fmt, String);
|
2019-07-28 09:21:17 +00:00
|
|
|
|
2019-08-15 14:33:00 +00:00
|
|
|
let pos = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Pos>()
|
|
|
|
.get(entity)
|
|
|
|
.copied();
|
|
|
|
let ori = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Ori>()
|
|
|
|
.get(entity)
|
|
|
|
.copied();
|
|
|
|
/*let builder = server
|
|
|
|
.create_object(pos, ori, obj_type)
|
|
|
|
.with(ori);*/
|
|
|
|
if let (Some(pos), Some(ori)) = (pos, ori) {
|
2019-07-28 12:58:18 +00:00
|
|
|
let obj_str_res = obj_type.as_ref().map(String::as_str);
|
|
|
|
let obj_type = match obj_str_res {
|
2019-08-15 14:33:00 +00:00
|
|
|
Ok("scarecrow") => comp::object::Body::Scarecrow,
|
|
|
|
Ok("cauldron") => comp::object::Body::Cauldron,
|
|
|
|
Ok("chest_vines") => comp::object::Body::ChestVines,
|
|
|
|
Ok("chest") => comp::object::Body::Chest,
|
|
|
|
Ok("chest_dark") => comp::object::Body::ChestDark,
|
|
|
|
Ok("chest_demon") => comp::object::Body::ChestDemon,
|
|
|
|
Ok("chest_gold") => comp::object::Body::ChestGold,
|
|
|
|
Ok("chest_light") => comp::object::Body::ChestLight,
|
|
|
|
Ok("chest_open") => comp::object::Body::ChestOpen,
|
|
|
|
Ok("chest_skull") => comp::object::Body::ChestSkull,
|
|
|
|
Ok("pumpkin") => comp::object::Body::Pumpkin,
|
|
|
|
Ok("pumpkin_2") => comp::object::Body::Pumpkin2,
|
|
|
|
Ok("pumpkin_3") => comp::object::Body::Pumpkin3,
|
|
|
|
Ok("pumpkin_4") => comp::object::Body::Pumpkin4,
|
|
|
|
Ok("pumpkin_5") => comp::object::Body::Pumpkin5,
|
|
|
|
Ok("campfire") => comp::object::Body::Campfire,
|
|
|
|
Ok("lantern_ground") => comp::object::Body::LanternGround,
|
|
|
|
Ok("lantern_ground_open") => comp::object::Body::LanternGroundOpen,
|
|
|
|
Ok("lantern_2") => comp::object::Body::LanternStanding2,
|
|
|
|
Ok("lantern") => comp::object::Body::LanternStanding,
|
|
|
|
Ok("potion_blue") => comp::object::Body::PotionBlue,
|
|
|
|
Ok("potion_green") => comp::object::Body::PotionGreen,
|
|
|
|
Ok("potion_red") => comp::object::Body::PotionRed,
|
|
|
|
Ok("crate") => comp::object::Body::Crate,
|
|
|
|
Ok("tent") => comp::object::Body::Tent,
|
|
|
|
Ok("bomb") => comp::object::Body::Bomb,
|
|
|
|
Ok("window_spooky") => comp::object::Body::WindowSpooky,
|
|
|
|
Ok("door_spooky") => comp::object::Body::DoorSpooky,
|
|
|
|
Ok("carpet") => comp::object::Body::Carpet,
|
|
|
|
Ok("table_human") => comp::object::Body::Table,
|
|
|
|
Ok("table_human_2") => comp::object::Body::Table2,
|
|
|
|
Ok("table_human_3") => comp::object::Body::Table3,
|
|
|
|
Ok("drawer") => comp::object::Body::Drawer,
|
|
|
|
Ok("bed_human_blue") => comp::object::Body::BedBlue,
|
|
|
|
Ok("anvil") => comp::object::Body::Anvil,
|
|
|
|
Ok("gravestone") => comp::object::Body::Gravestone,
|
|
|
|
Ok("gravestone_2") => comp::object::Body::Gravestone2,
|
|
|
|
Ok("chair") => comp::object::Body::Chair,
|
|
|
|
Ok("chair_2") => comp::object::Body::Chair2,
|
|
|
|
Ok("chair_3") => comp::object::Body::Chair3,
|
|
|
|
Ok("bench_human") => comp::object::Body::Bench,
|
|
|
|
Ok("bedroll") => comp::object::Body::Bedroll,
|
|
|
|
Ok("carpet_human_round") => comp::object::Body::CarpetHumanRound,
|
|
|
|
Ok("carpet_human_square") => comp::object::Body::CarpetHumanSquare,
|
|
|
|
Ok("carpet_human_square_2") => comp::object::Body::CarpetHumanSquare2,
|
|
|
|
Ok("carpet_human_squircle") => comp::object::Body::CarpetHumanSquircle,
|
2019-10-02 10:05:17 +00:00
|
|
|
Ok("crafting_bench") => comp::object::Body::CraftingBench,
|
2019-08-15 14:33:00 +00:00
|
|
|
_ => {
|
2019-10-15 04:06:14 +00:00
|
|
|
return server.notify_client(
|
2019-08-15 14:33:00 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("Object not found!")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
server
|
|
|
|
.create_object(pos, obj_type)
|
|
|
|
.with(comp::Ori(
|
|
|
|
// converts player orientation into a 90° rotation for the object by using the axis with the highest value
|
|
|
|
ori.0
|
|
|
|
.map(|e| {
|
|
|
|
if e.abs() == ori.0.map(|e| e.abs()).reduce_partial_max() {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.normalized(),
|
|
|
|
))
|
|
|
|
.build();
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-28 12:58:18 +00:00
|
|
|
entity,
|
2019-07-28 18:32:13 +00:00
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Spawned: {}",
|
|
|
|
obj_str_res.unwrap_or("<Unknown object>")
|
|
|
|
)),
|
2019-07-28 12:58:18 +00:00
|
|
|
);
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(format!("You have no position!")));
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 20:51:20 +00:00
|
|
|
fn handle_light(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let (opt_r, opt_g, opt_b, opt_x, opt_y, opt_z, opt_s) =
|
|
|
|
scan_fmt_some!(&args, action.arg_fmt, f32, f32, f32, f32, f32, f32, f32);
|
|
|
|
|
|
|
|
let mut light_emitter = comp::LightEmitter::default();
|
|
|
|
|
|
|
|
if let (Some(r), Some(g), Some(b)) = (opt_r, opt_g, opt_b) {
|
|
|
|
let r = r.max(0.0).min(1.0);
|
|
|
|
let g = g.max(0.0).min(1.0);
|
|
|
|
let b = b.max(0.0).min(1.0);
|
|
|
|
light_emitter.col = Rgb::new(r, g, b)
|
|
|
|
};
|
|
|
|
if let (Some(x), Some(y), Some(z)) = (opt_x, opt_y, opt_z) {
|
|
|
|
light_emitter.offset = Vec3::new(x, y, z)
|
|
|
|
};
|
|
|
|
if let Some(s) = opt_s {
|
|
|
|
light_emitter.strength = s.max(0.0)
|
|
|
|
};
|
|
|
|
let pos = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Pos>()
|
|
|
|
.get(entity)
|
|
|
|
.copied();
|
|
|
|
if let Some(pos) = pos {
|
|
|
|
server
|
2019-07-25 20:51:20 +00:00
|
|
|
.state
|
2019-08-15 14:33:00 +00:00
|
|
|
.ecs_mut()
|
|
|
|
.create_entity_synced()
|
|
|
|
.with(pos)
|
|
|
|
.with(comp::ForceUpdate)
|
|
|
|
.with(light_emitter)
|
|
|
|
.build();
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(format!("Spawned object.")));
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(format!("You have no position!")));
|
2019-07-25 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-26 14:38:31 +00:00
|
|
|
|
2019-07-28 11:36:35 +00:00
|
|
|
fn handle_lantern(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-12 14:05:58 +00:00
|
|
|
let opt_s = scan_fmt_some!(&args, action.arg_fmt, f32);
|
2019-07-28 11:36:35 +00:00
|
|
|
|
2019-08-12 14:05:58 +00:00
|
|
|
if server
|
|
|
|
.state
|
|
|
|
.read_storage::<comp::LightEmitter>()
|
|
|
|
.get(entity)
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
if let Some(s) = opt_s {
|
|
|
|
if let Some(light) = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::LightEmitter>()
|
|
|
|
.get_mut(entity)
|
|
|
|
{
|
2019-09-25 13:16:02 +00:00
|
|
|
light.strength = s.max(0.1).min(10.0);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-28 11:36:35 +00:00
|
|
|
entity,
|
2019-10-04 18:27:12 +00:00
|
|
|
ServerMsg::private(String::from("You adjusted flame strength.")),
|
2019-07-28 11:36:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-12 14:05:58 +00:00
|
|
|
server
|
2019-07-28 11:36:35 +00:00
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::LightEmitter>()
|
2019-08-12 14:05:58 +00:00
|
|
|
.remove(entity);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-28 11:36:35 +00:00
|
|
|
entity,
|
2019-08-12 14:05:58 +00:00
|
|
|
ServerMsg::private(String::from("You put out the lantern.")),
|
2019-07-28 11:36:35 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-12 14:05:58 +00:00
|
|
|
} else {
|
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::LightEmitter>()
|
|
|
|
.insert(
|
|
|
|
entity,
|
|
|
|
comp::LightEmitter {
|
|
|
|
offset: Vec3::new(0.5, 0.2, 0.8),
|
|
|
|
col: Rgb::new(1.0, 0.75, 0.3),
|
|
|
|
strength: if let Some(s) = opt_s {
|
2019-09-25 13:16:02 +00:00
|
|
|
s.max(0.0).min(10.0)
|
2019-08-12 14:05:58 +00:00
|
|
|
} else {
|
2019-09-25 13:16:02 +00:00
|
|
|
3.0
|
2019-08-12 14:05:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-12 14:05:58 +00:00
|
|
|
entity,
|
2019-10-04 18:27:12 +00:00
|
|
|
ServerMsg::private(String::from("You lit your lantern.")),
|
2019-08-12 14:05:58 +00:00
|
|
|
);
|
|
|
|
}
|
2019-07-25 20:51:20 +00:00
|
|
|
}
|
2019-07-26 14:38:31 +00:00
|
|
|
|
2019-08-07 17:17:04 +00:00
|
|
|
fn handle_explosion(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-08-07 17:18:32 +00:00
|
|
|
let radius = scan_fmt!(&args, action.arg_fmt, f32).unwrap_or(8.0);
|
|
|
|
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(entity) {
|
|
|
|
Some(pos) => server
|
|
|
|
.state
|
|
|
|
.ecs()
|
2019-08-25 14:49:54 +00:00
|
|
|
.read_resource::<EventBus<ServerEvent>>()
|
|
|
|
.emit(ServerEvent::Explosion { pos: pos.0, radius }),
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-08-07 17:18:32 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("You have no position!")),
|
|
|
|
),
|
2019-08-07 17:17:04 +00:00
|
|
|
}
|
2019-09-25 20:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_waypoint(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(entity) {
|
|
|
|
Some(pos) => {
|
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Waypoint>()
|
|
|
|
.insert(entity, comp::Waypoint::new(pos.0));
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from("Waypoint set!")));
|
2019-09-25 20:22:39 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-09-25 20:22:39 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("You have no position!")),
|
|
|
|
),
|
|
|
|
}
|
2019-08-07 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 18:16:58 +00:00
|
|
|
fn handle_adminify(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
|
|
|
|
let ecs = server.state.ecs();
|
|
|
|
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
match opt_player {
|
|
|
|
Some(player) => match server.state.read_component_cloned::<comp::Admin>(player) {
|
|
|
|
Some(_admin) => {
|
|
|
|
ecs.write_storage::<comp::Admin>().remove(player);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
server.state.write_component(player, comp::Admin);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-17 18:16:58 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-08-17 18:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-08-17 18:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:19:44 +00:00
|
|
|
fn handle_tell(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
2019-07-30 08:10:58 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, action.arg_fmt, String) {
|
|
|
|
let ecs = server.state.ecs();
|
|
|
|
let msg = &args[alias.len()..args.len()];
|
|
|
|
if let Some(player) = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity)
|
|
|
|
{
|
|
|
|
if player != entity {
|
|
|
|
if msg.len() > 1 {
|
|
|
|
if let Some(name) = ecs
|
|
|
|
.read_storage::<comp::Player>()
|
|
|
|
.get(entity)
|
|
|
|
.map(|s| s.alias.clone())
|
|
|
|
{
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
|
|
|
player,
|
|
|
|
ServerMsg::tell(format!("[{}] tells:{}", name, msg)),
|
|
|
|
);
|
|
|
|
server.notify_client(
|
|
|
|
entity,
|
|
|
|
ServerMsg::tell(format!("To [{}]:{}", alias, msg)),
|
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-28 09:21:17 +00:00
|
|
|
entity,
|
2019-07-30 08:10:58 +00:00
|
|
|
ServerMsg::private(String::from("Failed to send message.")),
|
2019-07-28 09:21:17 +00:00
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-13 04:25:44 +00:00
|
|
|
entity,
|
2019-07-30 08:10:58 +00:00
|
|
|
ServerMsg::private(format!("[{}] wants to talk to you.", alias)),
|
2019-07-13 04:25:44 +00:00
|
|
|
);
|
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-30 08:10:58 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("You can't /tell yourself.")),
|
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-07-30 08:10:58 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-26 09:49:14 +00:00
|
|
|
|
|
|
|
fn handle_debug_column(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let sim = server.world.sim();
|
2019-10-16 11:39:41 +00:00
|
|
|
let sampler = server.world.sample_columns();
|
2019-08-26 09:49:14 +00:00
|
|
|
if let Ok((x, y)) = scan_fmt!(&args, action.arg_fmt, i32, i32) {
|
|
|
|
let wpos = Vec2::new(x, y);
|
common: Rework volume API
See the doc comments in `common/src/vol.rs` for more information on
the API itself.
The changes include:
* Consistent `Err`/`Error` naming.
* Types are named `...Error`.
* `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
to an upcoming change where a “map” in the game related sense will
be added.
* Add volume iterators. There are two types of them:
* _Position_ iterators obtained from the trait `IntoPosIterator`
using the method
`fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `Vec3<i32>`.
* _Volume_ iterators obtained from the trait `IntoVolIterator`
using the method
`fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
Those traits will usually be implemented by references to volume
types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
type which usually implements several volume traits, such as `Chunk`).
* _Position_ iterators iterate over the positions valid for that
volume.
* _Volume_ iterators do the same but return not only the position
but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
`Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
* `RasterableVol` represents a volume that is compile-time sized and has
its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
because such a volume can be used with `VolGrid3d`.
* `RectRasterableVol` represents a volume that is compile-time sized at
least in x and y direction and has its lower bound at `(0, 0, z)`.
There's no requirement on he lower bound or size in z direction.
The name `RectRasterableVol` was chosen because such a volume can be
used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
|
|
|
/* let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| {
|
2019-08-26 09:49:14 +00:00
|
|
|
e / sz as i32
|
|
|
|
}); */
|
|
|
|
|
|
|
|
let foo = || {
|
|
|
|
// let sim_chunk = sim.get(chunk_pos)?;
|
|
|
|
let alt = sim.get_interpolated(wpos, |chunk| chunk.alt)?;
|
2019-10-16 11:39:41 +00:00
|
|
|
let water_alt = sim.get_interpolated(wpos, |chunk| chunk.water_alt)?;
|
2019-08-26 09:49:14 +00:00
|
|
|
let chaos = sim.get_interpolated(wpos, |chunk| chunk.chaos)?;
|
|
|
|
let temp = sim.get_interpolated(wpos, |chunk| chunk.temp)?;
|
|
|
|
let humidity = sim.get_interpolated(wpos, |chunk| chunk.humidity)?;
|
|
|
|
let rockiness = sim.get_interpolated(wpos, |chunk| chunk.rockiness)?;
|
|
|
|
let tree_density = sim.get_interpolated(wpos, |chunk| chunk.tree_density)?;
|
|
|
|
let spawn_rate = sim.get_interpolated(wpos, |chunk| chunk.spawn_rate)?;
|
2019-10-16 11:39:41 +00:00
|
|
|
let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32);
|
|
|
|
let chunk = sim.get(chunk_pos)?;
|
|
|
|
let col = sampler.get(wpos)?;
|
|
|
|
let downhill = chunk.downhill;
|
|
|
|
let river = &chunk.river;
|
|
|
|
let flux = chunk.flux;
|
2019-08-26 09:49:14 +00:00
|
|
|
|
|
|
|
Some(format!(
|
|
|
|
r#"wpos: {:?}
|
2019-10-16 11:39:41 +00:00
|
|
|
alt {:?} ({:?})
|
|
|
|
water_alt {:?} ({:?})
|
|
|
|
river {:?}
|
|
|
|
downhill {:?}
|
2019-08-26 09:49:14 +00:00
|
|
|
chaos {:?}
|
2019-10-16 11:39:41 +00:00
|
|
|
flux {:?}
|
2019-08-26 09:49:14 +00:00
|
|
|
temp {:?}
|
|
|
|
humidity {:?}
|
|
|
|
rockiness {:?}
|
|
|
|
tree_density {:?}
|
|
|
|
spawn_rate {:?} "#,
|
2019-10-16 11:39:41 +00:00
|
|
|
wpos,
|
|
|
|
alt,
|
|
|
|
col.alt,
|
|
|
|
water_alt,
|
|
|
|
col.water_level,
|
|
|
|
river,
|
|
|
|
downhill,
|
|
|
|
chaos,
|
|
|
|
flux,
|
|
|
|
temp,
|
|
|
|
humidity,
|
|
|
|
rockiness,
|
|
|
|
tree_density,
|
|
|
|
spawn_rate
|
2019-08-26 09:49:14 +00:00
|
|
|
))
|
|
|
|
};
|
|
|
|
if let Some(s) = foo() {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(s));
|
2019-08-26 09:49:14 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-08-26 09:49:14 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("Not a pregenerated chunk.")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(entity, ServerMsg::private(String::from(action.help_string)));
|
2019-08-26 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 15:48:14 +00:00
|
|
|
|
|
|
|
fn handle_exp(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
|
|
|
|
let (a_alias, a_exp) = scan_fmt_some!(&args, action.arg_fmt, String, i64);
|
|
|
|
if let (Some(alias), Some(exp)) = (a_alias, a_exp) {
|
|
|
|
let ecs = server.state.ecs_mut();
|
|
|
|
let opt_player = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
|
|
|
.map(|(entity, _)| entity);
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
let mut error_msg = None;
|
|
|
|
|
2019-10-04 15:48:14 +00:00
|
|
|
match opt_player {
|
|
|
|
Some(_alias) => {
|
|
|
|
if let Some(stats) = ecs.write_storage::<comp::Stats>().get_mut(entity) {
|
|
|
|
stats.exp.change_by(exp);
|
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
error_msg = Some(ServerMsg::private(String::from("Player has no stats!")));
|
2019-10-04 15:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2019-10-15 04:06:14 +00:00
|
|
|
error_msg = Some(ServerMsg::private(format!("Player '{}' not found!", alias)));
|
2019-10-04 15:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
|
|
|
|
if let Some(msg) = error_msg {
|
|
|
|
server.notify_client(entity, msg);
|
|
|
|
}
|
2019-10-04 15:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 03:19:46 +00:00
|
|
|
|
2019-10-07 04:38:28 +00:00
|
|
|
fn handle_remove_lights(
|
|
|
|
server: &mut Server,
|
|
|
|
entity: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2019-10-07 03:19:46 +00:00
|
|
|
let opt_radius = scan_fmt_some!(&args, action.arg_fmt, f32);
|
2019-10-07 04:38:28 +00:00
|
|
|
let opt_player_pos = server.state.read_component_cloned::<comp::Pos>(entity);
|
2019-10-07 03:19:46 +00:00
|
|
|
let mut to_delete = vec![];
|
|
|
|
|
|
|
|
match opt_player_pos {
|
|
|
|
Some(player_pos) => {
|
|
|
|
let ecs = server.state.ecs();
|
2019-10-07 04:03:53 +00:00
|
|
|
for (entity, pos, _, _) in (
|
|
|
|
&ecs.entities(),
|
|
|
|
&ecs.read_storage::<comp::Pos>(),
|
|
|
|
&ecs.read_storage::<comp::LightEmitter>(),
|
2019-10-07 04:38:28 +00:00
|
|
|
!&ecs.read_storage::<comp::Player>(),
|
|
|
|
)
|
|
|
|
.join()
|
|
|
|
{
|
|
|
|
if opt_radius
|
|
|
|
.map(|r| pos.0.distance(player_pos.0) < r)
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
2019-10-07 04:03:53 +00:00
|
|
|
to_delete.push(entity);
|
2019-10-07 03:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 04:38:28 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2019-10-07 04:38:28 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from("You have no position.")),
|
2019-10-07 03:19:46 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = to_delete.len();
|
|
|
|
|
2019-10-07 04:49:56 +00:00
|
|
|
for entity in to_delete {
|
|
|
|
let _ = server.state.ecs_mut().delete_entity_synced(entity);
|
2019-10-07 03:19:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2019-10-07 04:38:28 +00:00
|
|
|
entity,
|
|
|
|
ServerMsg::private(String::from(format!("Removed {} lights!", size))),
|
2019-10-07 03:19:46 +00:00
|
|
|
);
|
|
|
|
}
|