2019-05-17 09:22:32 +00:00
|
|
|
//! # Implementing new commands.
|
2020-02-01 20:39:39 +00:00
|
|
|
//! To implement a new command, add an instance of `ChatCommand` to
|
|
|
|
//! `CHAT_COMMANDS` 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::{
|
2020-05-05 22:33:16 +00:00
|
|
|
assets,
|
|
|
|
cmd::{ChatCommand, CHAT_COMMANDS},
|
|
|
|
comp,
|
2019-08-25 16:48:12 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-05-14 16:56:10 +00:00
|
|
|
msg::{Notification, PlayerListUpdate, ServerMsg},
|
2020-01-29 16:56:28 +00:00
|
|
|
npc::{self, get_npc_name},
|
2019-07-03 19:56:54 +00:00
|
|
|
state::TimeOfDay,
|
2019-12-23 06:02:00 +00:00
|
|
|
sync::{Uid, WorldSyncExt},
|
2020-01-25 19:03:25 +00:00
|
|
|
terrain::TerrainChunkSize,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2019-10-16 11:39:41 +00:00
|
|
|
vol::RectVolSize,
|
2019-06-01 19:49:34 +00:00
|
|
|
};
|
2019-07-21 18:22:13 +00:00
|
|
|
use rand::Rng;
|
2019-11-30 06:41:20 +00:00
|
|
|
use specs::{Builder, Entity as EcsEntity, Join, WorldExt};
|
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-11-29 06:04:37 +00:00
|
|
|
use log::error;
|
2019-07-29 13:42:26 +00:00
|
|
|
use scan_fmt::{scan_fmt, scan_fmt_some};
|
2019-07-26 14:38:31 +00:00
|
|
|
|
2020-05-05 22:33:16 +00:00
|
|
|
pub trait ChatCommandExt {
|
|
|
|
fn execute(&self, server: &mut Server, entity: EcsEntity, args: String);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2020-05-05 22:33:16 +00:00
|
|
|
impl ChatCommandExt for ChatCommand {
|
|
|
|
fn execute(&self, server: &mut Server, entity: EcsEntity, args: String) {
|
|
|
|
let cmd_data = self.data();
|
|
|
|
if cmd_data.needs_admin && !server.entity_is_admin(entity) {
|
|
|
|
server.notify_client(
|
|
|
|
entity,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"You don't have permission to use '/{}'.",
|
|
|
|
self.keyword()
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
return;
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
get_handler(self)(server, entity, entity, args, &self);
|
2019-08-15 14:33:00 +00:00
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:02:21 +00:00
|
|
|
/// Handler function called when the command is executed.
|
|
|
|
/// # Arguments
|
|
|
|
/// * `&mut Server` - the `Server` instance executing the command.
|
|
|
|
/// * `EcsEntity` - an `Entity` corresponding to the player that invoked the
|
|
|
|
/// command.
|
|
|
|
/// * `EcsEntity` - an `Entity` for the player on whom the command is invoked.
|
|
|
|
/// This differs from the previous argument when using /sudo
|
|
|
|
/// * `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).
|
2020-05-05 22:33:16 +00:00
|
|
|
type CommandHandler = fn(&mut Server, EcsEntity, EcsEntity, String, &ChatCommand);
|
|
|
|
fn get_handler(cmd: &ChatCommand) -> CommandHandler {
|
|
|
|
match cmd {
|
|
|
|
ChatCommand::Adminify => handle_adminify,
|
|
|
|
ChatCommand::Alias => handle_alias,
|
|
|
|
ChatCommand::Build => handle_build,
|
|
|
|
ChatCommand::Debug => handle_debug,
|
|
|
|
ChatCommand::DebugColumn => handle_debug_column,
|
|
|
|
ChatCommand::Explosion => handle_explosion,
|
|
|
|
ChatCommand::GiveExp => handle_give_exp,
|
|
|
|
ChatCommand::GiveItem => handle_give_item,
|
|
|
|
ChatCommand::Goto => handle_goto,
|
|
|
|
ChatCommand::Health => handle_health,
|
|
|
|
ChatCommand::Help => handle_help,
|
|
|
|
ChatCommand::Jump => handle_jump,
|
|
|
|
ChatCommand::Kill => handle_kill,
|
|
|
|
ChatCommand::KillNpcs => handle_kill_npcs,
|
|
|
|
ChatCommand::Lantern => handle_lantern,
|
|
|
|
ChatCommand::Light => handle_light,
|
|
|
|
ChatCommand::Object => handle_object,
|
|
|
|
ChatCommand::Players => handle_players,
|
|
|
|
ChatCommand::RemoveLights => handle_remove_lights,
|
|
|
|
ChatCommand::SetLevel => handle_set_level,
|
|
|
|
ChatCommand::Spawn => handle_spawn,
|
|
|
|
ChatCommand::Sudo => handle_sudo,
|
|
|
|
ChatCommand::Tell => handle_tell,
|
|
|
|
ChatCommand::Time => handle_time,
|
|
|
|
ChatCommand::Tp => handle_tp,
|
|
|
|
ChatCommand::Version => handle_version,
|
|
|
|
ChatCommand::Waypoint => handle_waypoint,
|
|
|
|
}
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2020-05-09 02:42:51 +00:00
|
|
|
|
2020-05-05 22:33:16 +00:00
|
|
|
fn handle_give_item(
|
2020-04-24 02:36:19 +00:00
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
2020-05-07 18:39:48 +00:00
|
|
|
action: &ChatCommand,
|
2020-04-24 02:36:19 +00:00
|
|
|
) {
|
2020-05-10 01:17:03 +00:00
|
|
|
if let (Some(item_name), give_amount_opt) =
|
|
|
|
scan_fmt_some!(&args, &action.arg_fmt(), String, u32)
|
|
|
|
{
|
2020-05-07 18:39:48 +00:00
|
|
|
let give_amount = give_amount_opt.unwrap_or(1);
|
|
|
|
if let Ok(item) = assets::load_cloned(&item_name) {
|
|
|
|
let mut item: Item = item;
|
|
|
|
if let Ok(()) = item.set_amount(give_amount.min(2000)) {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Inventory>()
|
|
|
|
.get_mut(target)
|
|
|
|
.map(|inv| {
|
|
|
|
if inv.push(item).is_some() {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Player inventory full. Gave 0 of {} items.",
|
|
|
|
give_amount
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// This item can't stack. Give each item in a loop.
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Inventory>()
|
|
|
|
.get_mut(target)
|
|
|
|
.map(|inv| {
|
|
|
|
for i in 0..give_amount {
|
|
|
|
if inv.push(item.clone()).is_some() {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Player inventory full. Gave {} of {} items.",
|
|
|
|
i, give_amount
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::InventoryUpdate>()
|
|
|
|
.insert(
|
|
|
|
target,
|
|
|
|
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Given),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!("Invalid item: {}", item_name)),
|
2020-03-04 10:09:48 +00:00
|
|
|
);
|
2020-05-07 18:39:48 +00:00
|
|
|
}
|
2019-10-24 21:05:10 +00:00
|
|
|
} else {
|
2020-05-10 01:17:03 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-10-24 21:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 04:48:57 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_jump(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok((x, y, z)) = scan_fmt!(&args, &action.arg_fmt(), f32, f32, f32) {
|
2020-04-24 02:36:19 +00:00
|
|
|
match server.state.read_component_cloned::<comp::Pos>(target) {
|
2019-08-15 14:33:00 +00:00
|
|
|
Some(current_pos) => {
|
|
|
|
server
|
|
|
|
.state
|
2020-04-24 02:36:19 +00:00
|
|
|
.write_component(target, comp::Pos(current_pos.0 + Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(target, comp::ForceUpdate);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_goto(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok((x, y, z)) = scan_fmt!(&args, &action.arg_fmt(), f32, f32, f32) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if server
|
|
|
|
.state
|
2020-04-24 02:36:19 +00:00
|
|
|
.read_component_cloned::<comp::Pos>(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
server
|
2019-07-30 08:10:58 +00:00
|
|
|
.state
|
2020-04-24 02:36:19 +00:00
|
|
|
.write_component(target, comp::Pos(Vec3::new(x, y, z)));
|
|
|
|
server.state.write_component(target, comp::ForceUpdate);
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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 {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_kill(
|
|
|
|
server: &mut Server,
|
2020-04-24 16:17:56 +00:00
|
|
|
client: EcsEntity,
|
2020-04-24 02:36:19 +00:00
|
|
|
target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
2020-04-24 16:17:56 +00:00
|
|
|
let reason = if client == target {
|
|
|
|
comp::HealthSource::Suicide
|
|
|
|
} else {
|
|
|
|
if let Some(uid) = server.state.read_storage::<Uid>().get(client) {
|
|
|
|
comp::HealthSource::Attack { by: *uid }
|
|
|
|
} else {
|
|
|
|
comp::HealthSource::Command
|
|
|
|
}
|
|
|
|
};
|
2019-05-19 21:54:09 +00:00
|
|
|
server
|
|
|
|
.state
|
2019-05-25 21:13:38 +00:00
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Stats>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get_mut(target)
|
2020-04-24 16:17:56 +00:00
|
|
|
.map(|s| s.health.set_to(0, reason));
|
2019-05-19 21:54:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_time(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
let time = scan_fmt_some!(&args, &action.arg_fmt(), String);
|
2019-08-15 14:33:00 +00:00
|
|
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(format!("'{}' is not a valid time.", n)),
|
|
|
|
);
|
|
|
|
return;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
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"),
|
|
|
|
};
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(msg));
|
2019-08-15 14:33:00 +00:00
|
|
|
return;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-15 14:33:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Time changed to: {}",
|
|
|
|
new_time.format("%H:%M").to_string()
|
|
|
|
)),
|
|
|
|
);
|
2019-06-21 08:42:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_health(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok(hp) = scan_fmt!(&args, &action.arg_fmt(), u32) {
|
2019-08-15 14:33:00 +00:00
|
|
|
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>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get_mut(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
{
|
|
|
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(String::from("You must specify health amount!")),
|
|
|
|
);
|
2019-07-01 20:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_alias(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) {
|
2020-05-23 07:13:51 +00:00
|
|
|
let old_alias_optional = server
|
2019-07-30 08:10:58 +00:00
|
|
|
.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Player>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get_mut(target)
|
2020-05-23 07:13:51 +00:00
|
|
|
.map(|player| std::mem::replace(&mut player.alias, alias));
|
2019-12-23 06:02:00 +00:00
|
|
|
|
|
|
|
// Update name on client player lists
|
|
|
|
let ecs = server.state.ecs();
|
2020-05-23 07:13:51 +00:00
|
|
|
if let (Some(uid), Some(player), Some(old_alias)) = (
|
2020-04-24 02:36:19 +00:00
|
|
|
ecs.read_storage::<Uid>().get(target),
|
|
|
|
ecs.read_storage::<comp::Player>().get(target),
|
2020-05-23 07:13:51 +00:00
|
|
|
old_alias_optional,
|
2019-12-23 06:02:00 +00:00
|
|
|
) {
|
|
|
|
let msg = ServerMsg::PlayerListUpdate(PlayerListUpdate::Alias(
|
|
|
|
(*uid).into(),
|
|
|
|
player.alias.clone(),
|
|
|
|
));
|
|
|
|
server.state.notify_registered_clients(msg);
|
2020-05-30 01:36:52 +00:00
|
|
|
|
|
|
|
// Announce alias change if target has a Body.
|
|
|
|
if ecs.read_storage::<comp::Body>().get(target).is_some() {
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.notify_registered_clients(ServerMsg::broadcast(format!(
|
|
|
|
"{} is now known as {}.",
|
|
|
|
old_alias, player.alias
|
|
|
|
)));
|
|
|
|
}
|
2019-12-23 06:02:00 +00:00
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_tp(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-10 01:17:03 +00:00
|
|
|
let opt_player = if let Some(alias) = scan_fmt_some!(&args, &action.arg_fmt(), String) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let ecs = server.state.ecs();
|
2020-05-10 01:17:03 +00:00
|
|
|
(&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
2019-08-15 14:33:00 +00:00
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
2020-05-10 01:17:03 +00:00
|
|
|
.map(|(entity, _)| entity)
|
|
|
|
} else {
|
|
|
|
if client != target {
|
|
|
|
Some(client)
|
|
|
|
} else {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private("You must specify a player name".to_string()),
|
|
|
|
);
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(_pos) = server.state.read_component_cloned::<comp::Pos>(target) {
|
|
|
|
if let Some(player) = opt_player {
|
|
|
|
if let Some(pos) = server.state.read_component_cloned::<comp::Pos>(player) {
|
|
|
|
server.state.write_component(target, pos);
|
|
|
|
server.state.write_component(target, comp::ForceUpdate);
|
|
|
|
} else {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!("Unable to teleport to player!")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server.notify_client(client, ServerMsg::private(format!("Player not found!")));
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2020-05-10 01:17:03 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(format!("You have no position!")));
|
2019-04-16 15:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_spawn(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
match scan_fmt_some!(&args, &action.arg_fmt(), String, npc::NpcBody, String) {
|
2020-01-29 16:56:28 +00:00
|
|
|
(Some(opt_align), Some(npc::NpcBody(id, mut body)), opt_amount) => {
|
2020-04-24 02:36:19 +00:00
|
|
|
if let Some(alignment) = parse_alignment(target, &opt_align) {
|
2019-08-15 14:33:00 +00:00
|
|
|
let amount = opt_amount
|
|
|
|
.and_then(|a| a.parse().ok())
|
|
|
|
.filter(|x| *x > 0)
|
|
|
|
.unwrap_or(1)
|
|
|
|
.min(10);
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
match server.state.read_component_cloned::<comp::Pos>(target) {
|
2019-08-15 14:33:00 +00:00
|
|
|
Some(pos) => {
|
2020-01-27 15:51:07 +00:00
|
|
|
let agent =
|
|
|
|
if let comp::Alignment::Owned(_) | comp::Alignment::Npc = alignment {
|
|
|
|
comp::Agent::default()
|
|
|
|
} else {
|
|
|
|
comp::Agent::default().with_patrol_origin(pos.0)
|
|
|
|
};
|
2020-01-25 18:49:47 +00:00
|
|
|
|
2019-08-15 14:33:00 +00:00
|
|
|
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
|
|
|
|
2020-01-29 16:56:28 +00:00
|
|
|
let body = body();
|
2020-01-13 22:49:46 +00:00
|
|
|
|
2019-12-11 05:28:45 +00:00
|
|
|
let new_entity = server
|
2019-10-20 05:19:50 +00:00
|
|
|
.state
|
2020-01-13 22:49:46 +00:00
|
|
|
.create_npc(
|
|
|
|
pos,
|
2020-03-15 14:27:06 +00:00
|
|
|
comp::Stats::new(get_npc_name(id).into(), body),
|
2020-02-26 17:04:43 +00:00
|
|
|
comp::Loadout::default(),
|
2020-01-13 22:49:46 +00:00
|
|
|
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-12-11 05:28:45 +00:00
|
|
|
.with(agent.clone())
|
2020-01-24 21:24:57 +00:00
|
|
|
.with(alignment)
|
2019-08-15 14:33:00 +00:00
|
|
|
.build();
|
2019-12-11 05:28:45 +00:00
|
|
|
|
|
|
|
if let Some(uid) = server.state.ecs().uid_from_entity(new_entity) {
|
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-12-11 05:28:45 +00:00
|
|
|
ServerMsg::private(
|
|
|
|
format!("Spawned entity with ID: {}", uid).to_owned(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2019-07-29 13:42:26 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(format!("Spawned {} entities", amount).to_owned()),
|
|
|
|
);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private("You have no position!".to_owned()),
|
|
|
|
),
|
2019-06-15 07:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-15 14:33:00 +00:00
|
|
|
_ => {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 03:01:52 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_players(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
2019-06-11 04:24:35 +00:00
|
|
|
let ecs = server.state.ecs();
|
2019-06-29 22:16:16 +00:00
|
|
|
|
2020-06-01 13:01:10 +00:00
|
|
|
let entity_tuples = (
|
|
|
|
&ecs.entities(),
|
|
|
|
&ecs.read_storage::<comp::Player>(),
|
|
|
|
&ecs.read_storage::<comp::Stats>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(entity_tuples.join().fold(
|
|
|
|
format!("{} online players:", entity_tuples.join().count()),
|
|
|
|
|s, (_, player, stat)| {
|
|
|
|
format!(
|
|
|
|
"{}\n[{}]{} Lvl {}",
|
|
|
|
s,
|
|
|
|
player.alias,
|
|
|
|
stat.name,
|
|
|
|
stat.level.level()
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
);
|
2019-06-11 04:24:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_build(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
2019-08-15 14:33:00 +00:00
|
|
|
if server
|
|
|
|
.state
|
|
|
|
.read_storage::<comp::CanBuild>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::CanBuild>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.remove(target);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.insert(target, comp::CanBuild);
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-15 14:33:00 +00:00
|
|
|
ServerMsg::private(String::from("Toggled on build mode!")),
|
|
|
|
);
|
2019-07-02 18:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_help(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
2020-05-05 22:33:16 +00:00
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
2020-04-24 02:36:19 +00:00
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Some(cmd) = scan_fmt_some!(&args, &action.arg_fmt(), ChatCommand) {
|
|
|
|
server.notify_client(client, ServerMsg::private(String::from(cmd.help_string())));
|
|
|
|
} else {
|
|
|
|
for cmd in CHAT_COMMANDS.iter() {
|
|
|
|
if !cmd.needs_admin() || server.entity_is_admin(client) {
|
|
|
|
server.notify_client(client, 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
|
|
|
|
2020-01-27 15:51:07 +00:00
|
|
|
fn parse_alignment(owner: EcsEntity, alignment: &str) -> Option<comp::Alignment> {
|
2019-06-15 07:54:47 +00:00
|
|
|
match alignment {
|
2020-01-24 21:24:57 +00:00
|
|
|
"wild" => Some(comp::Alignment::Wild),
|
|
|
|
"enemy" => Some(comp::Alignment::Enemy),
|
|
|
|
"npc" => Some(comp::Alignment::Npc),
|
2020-01-27 15:51:07 +00:00
|
|
|
"pet" => Some(comp::Alignment::Owned(owner)),
|
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
|
|
|
|
2020-05-05 22:33:16 +00:00
|
|
|
fn handle_kill_npcs(
|
2020-04-24 02:36:19 +00:00
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: 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()
|
|
|
|
};
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(text));
|
2019-07-12 21:16:07 +00:00
|
|
|
}
|
2019-07-17 17:53:10 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_object(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
2020-05-05 22:33:16 +00:00
|
|
|
action: &ChatCommand,
|
2020-04-24 02:36:19 +00:00
|
|
|
) {
|
2020-05-05 22:33:16 +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>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
.copied();
|
|
|
|
let ori = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Ori>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
.copied();
|
2020-03-10 02:27:32 +00:00
|
|
|
/*let builder = server.state
|
2019-08-15 14:33:00 +00:00
|
|
|
.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);
|
2020-05-09 02:42:51 +00:00
|
|
|
if let Some(obj_type) = comp::object::ALL_OBJECTS
|
|
|
|
.iter()
|
|
|
|
.find(|o| Ok(o.to_string()) == obj_str_res)
|
|
|
|
{
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.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
|
|
|
|
Dir::from_unnormalized(ori.0.map(|e| {
|
|
|
|
if e.abs() == ori.0.map(|e| e.abs()).reduce_partial_max() {
|
|
|
|
e
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
))
|
|
|
|
.build();
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Spawned: {}",
|
|
|
|
obj_str_res.unwrap_or("<Unknown object>")
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from("Object not found!")),
|
|
|
|
);
|
|
|
|
}
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(format!("You have no position!")));
|
2019-07-21 12:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_light(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: 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) =
|
2020-05-05 22:33:16 +00:00
|
|
|
scan_fmt_some!(&args, &action.arg_fmt(), f32, f32, f32, f32, f32, f32, f32);
|
2019-08-15 14:33:00 +00:00
|
|
|
|
|
|
|
let mut light_emitter = comp::LightEmitter::default();
|
2020-05-04 15:15:31 +00:00
|
|
|
let mut light_offset_opt = None;
|
2019-08-15 14:33:00 +00:00
|
|
|
|
|
|
|
if let (Some(r), Some(g), Some(b)) = (opt_r, opt_g, opt_b) {
|
2020-05-04 09:50:58 +00:00
|
|
|
if r < 0.0 || g < 0.0 || b < 0.0 {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from("cr, cg and cb values mustn't be negative.")),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:33:00 +00:00
|
|
|
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) {
|
2020-05-04 15:15:31 +00:00
|
|
|
light_offset_opt = Some(comp::LightAnimation {
|
|
|
|
offset: Vec3::new(x, y, z),
|
|
|
|
col: light_emitter.col,
|
|
|
|
strength: 0.0,
|
|
|
|
})
|
2019-08-15 14:33:00 +00:00
|
|
|
};
|
|
|
|
if let Some(s) = opt_s {
|
|
|
|
light_emitter.strength = s.max(0.0)
|
|
|
|
};
|
|
|
|
let pos = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Pos>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get(target)
|
2019-08-15 14:33:00 +00:00
|
|
|
.copied();
|
|
|
|
if let Some(pos) = pos {
|
2020-05-04 15:15:31 +00:00
|
|
|
let builder = 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)
|
2020-05-04 15:15:31 +00:00
|
|
|
.with(light_emitter);
|
|
|
|
if let Some(light_offset) = light_offset_opt {
|
|
|
|
builder.with(light_offset).build();
|
|
|
|
} else {
|
|
|
|
builder.build();
|
|
|
|
}
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(format!("Spawned object.")));
|
2019-08-15 14:33:00 +00:00
|
|
|
} else {
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(format!("You have no position!")));
|
2019-07-25 20:51:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-26 14:38:31 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_lantern(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let (Some(s), r, g, b) = scan_fmt_some!(&args, &action.arg_fmt(), f32, f32, f32, f32) {
|
2020-05-04 15:15:31 +00:00
|
|
|
if let Some(light) = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::LightEmitter>()
|
|
|
|
.get_mut(target)
|
|
|
|
{
|
|
|
|
light.strength = s.max(0.1).min(10.0);
|
|
|
|
if let (Some(r), Some(g), Some(b)) = (r, g, b) {
|
|
|
|
light.col = (
|
|
|
|
r.max(0.0).min(1.0),
|
|
|
|
g.max(0.0).min(1.0),
|
|
|
|
b.max(0.0).min(1.0),
|
|
|
|
)
|
|
|
|
.into();
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from("You adjusted flame strength and color.")),
|
|
|
|
);
|
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2020-05-04 15:15:31 +00:00
|
|
|
ServerMsg::private(String::from("Please equip a lantern first")),
|
2019-07-28 11:36:35 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-12 14:05:58 +00:00
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-08-12 14:05:58 +00:00
|
|
|
}
|
2019-07-25 20:51:20 +00:00
|
|
|
}
|
2019-07-26 14:38:31 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_explosion(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
let power = scan_fmt!(&args, &action.arg_fmt(), f32).unwrap_or(8.0);
|
2020-05-04 09:50:58 +00:00
|
|
|
|
|
|
|
if power > 512.0 {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from("Explosion power mustn't be more than 512.")),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} else if power <= 0.0 {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from("Explosion power must be more than 0.")),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-22 19:39:50 +00:00
|
|
|
let ecs = server.state.ecs();
|
2019-08-07 17:18:32 +00:00
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
match server.state.read_component_cloned::<comp::Pos>(target) {
|
2020-03-22 19:39:50 +00:00
|
|
|
Some(pos) => {
|
|
|
|
ecs.read_resource::<EventBus<ServerEvent>>()
|
|
|
|
.emit_now(ServerEvent::Explosion {
|
|
|
|
pos: pos.0,
|
|
|
|
power,
|
2020-04-24 02:36:19 +00:00
|
|
|
owner: ecs.read_storage::<Uid>().get(target).copied(),
|
2020-03-22 19:39:50 +00:00
|
|
|
})
|
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-07 17:18:32 +00:00
|
|
|
ServerMsg::private(String::from("You have no position!")),
|
|
|
|
),
|
2019-08-07 17:17:04 +00:00
|
|
|
}
|
2019-09-25 20:22:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_waypoint(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
|
|
|
match server.state.read_component_cloned::<comp::Pos>(target) {
|
2019-09-25 20:22:39 +00:00
|
|
|
Some(pos) => {
|
2020-05-24 04:10:08 +00:00
|
|
|
let time = server.state.ecs().read_resource();
|
2019-09-25 20:22:39 +00:00
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Waypoint>()
|
2020-05-24 04:10:08 +00:00
|
|
|
.insert(target, comp::Waypoint::new(pos.0, *time));
|
2020-05-14 16:56:10 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(String::from("Waypoint saved!")));
|
|
|
|
server.notify_client(client, ServerMsg::Notification(Notification::WaypointSaved));
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-09-25 20:22:39 +00:00
|
|
|
ServerMsg::private(String::from("You have no position!")),
|
|
|
|
),
|
|
|
|
}
|
2019-08-07 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_adminify(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) {
|
2019-08-17 18:16:58 +00:00
|
|
|
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);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-17 18:16:58 +00:00
|
|
|
None => {
|
|
|
|
server.state.write_component(player, comp::Admin);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-17 18:16:58 +00:00
|
|
|
},
|
|
|
|
None => {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-17 18:16:58 +00:00
|
|
|
ServerMsg::private(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-08-17 18:16:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-08-17 18:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_tell(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
|
|
|
if client != target {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::tell(String::from("It's rude to impersonate people")),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) {
|
2019-07-30 08:10:58 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-04-24 02:36:19 +00:00
|
|
|
if player != target {
|
2019-07-30 08:10:58 +00:00
|
|
|
if msg.len() > 1 {
|
|
|
|
if let Some(name) = ecs
|
|
|
|
.read_storage::<comp::Player>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get(target)
|
2019-07-30 08:10:58 +00:00
|
|
|
.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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-10-15 04:06:14 +00:00
|
|
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-07-30 08:10:58 +00:00
|
|
|
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(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-07-30 08:10:58 +00:00
|
|
|
ServerMsg::private(format!("Player '{}' not found!", alias)),
|
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
2019-07-30 08:10:58 +00:00
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-07-13 04:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-26 09:49:14 +00:00
|
|
|
|
2020-01-22 03:12:17 +00:00
|
|
|
#[cfg(not(feature = "worldgen"))]
|
|
|
|
fn handle_debug_column(
|
|
|
|
server: &mut Server,
|
2020-04-24 02:36:19 +00:00
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
2020-01-22 03:12:17 +00:00
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2020-01-22 03:12:17 +00:00
|
|
|
ServerMsg::private(String::from("Unsupported without worldgen enabled")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "worldgen")]
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_debug_column(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2019-08-26 09:49:14 +00:00
|
|
|
let sim = server.world.sim();
|
2019-10-16 11:39:41 +00:00
|
|
|
let sampler = server.world.sample_columns();
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Ok((x, y)) = scan_fmt!(&args, &action.arg_fmt(), i32, i32) {
|
2019-08-26 09:49:14 +00:00
|
|
|
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-11-19 18:34:52 +00:00
|
|
|
let basement = sim.get_interpolated(wpos, |chunk| chunk.basement)?;
|
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 {:?} ({:?})
|
2019-11-19 18:34:52 +00:00
|
|
|
basement {:?}
|
2019-10-16 11:39:41 +00:00
|
|
|
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,
|
2019-11-19 18:34:52 +00:00
|
|
|
basement,
|
2019-10-16 11:39:41 +00:00
|
|
|
river,
|
|
|
|
downhill,
|
|
|
|
chaos,
|
|
|
|
flux,
|
|
|
|
temp,
|
|
|
|
humidity,
|
|
|
|
rockiness,
|
|
|
|
tree_density,
|
|
|
|
spawn_rate
|
2019-08-26 09:49:14 +00:00
|
|
|
))
|
|
|
|
};
|
|
|
|
if let Some(s) = foo() {
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, ServerMsg::private(s));
|
2019-08-26 09:49:14 +00:00
|
|
|
} else {
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-08-26 09:49:14 +00:00
|
|
|
ServerMsg::private(String::from("Not a pregenerated chunk.")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2019-08-26 09:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 15:48:14 +00:00
|
|
|
|
2020-02-13 20:32:24 +00:00
|
|
|
fn find_target(
|
|
|
|
ecs: &specs::World,
|
|
|
|
opt_alias: Option<String>,
|
|
|
|
fallback: EcsEntity,
|
|
|
|
) -> Result<EcsEntity, ServerMsg> {
|
|
|
|
if let Some(alias) = opt_alias {
|
|
|
|
(&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
2019-10-04 15:48:14 +00:00
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == alias)
|
2020-02-13 20:32:24 +00:00
|
|
|
.map(|(entity, _)| entity)
|
|
|
|
.ok_or(ServerMsg::private(format!("Player '{}' not found!", alias)))
|
|
|
|
} else {
|
|
|
|
Ok(fallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 22:33:16 +00:00
|
|
|
fn handle_give_exp(
|
2020-04-24 02:36:19 +00:00
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
let (a_exp, a_alias) = scan_fmt_some!(&args, &action.arg_fmt(), i64, String);
|
2020-02-13 20:32:24 +00:00
|
|
|
|
|
|
|
if let Some(exp) = a_exp {
|
|
|
|
let ecs = server.state.ecs_mut();
|
2020-04-24 02:36:19 +00:00
|
|
|
let target = find_target(&ecs, a_alias, target);
|
2019-10-04 15:48:14 +00:00
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
let mut error_msg = None;
|
|
|
|
|
2020-02-13 20:32:24 +00:00
|
|
|
match target {
|
|
|
|
Ok(player) => {
|
2020-02-10 09:38:45 +00:00
|
|
|
if let Some(stats) = ecs.write_storage::<comp::Stats>().get_mut(player) {
|
2019-10-04 15:48:14 +00:00
|
|
|
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
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-02-13 20:32:24 +00:00
|
|
|
Err(e) => {
|
|
|
|
error_msg = Some(e);
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-04 15:48:14 +00:00
|
|
|
}
|
2019-10-15 04:06:14 +00:00
|
|
|
|
|
|
|
if let Some(msg) = error_msg {
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, msg);
|
2019-10-15 04:06:14 +00:00
|
|
|
}
|
2019-10-04 15:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 03:19:46 +00:00
|
|
|
|
2020-05-05 22:33:16 +00:00
|
|
|
fn handle_set_level(
|
2020-04-24 02:36:19 +00:00
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
let (a_lvl, a_alias) = scan_fmt_some!(&args, &action.arg_fmt(), u32, String);
|
2020-02-13 21:15:54 +00:00
|
|
|
|
|
|
|
if let Some(lvl) = a_lvl {
|
2020-06-01 10:06:30 +00:00
|
|
|
let target = find_target(&server.state.ecs(), a_alias, target);
|
2020-02-13 21:15:54 +00:00
|
|
|
|
|
|
|
let mut error_msg = None;
|
|
|
|
|
|
|
|
match target {
|
|
|
|
Ok(player) => {
|
2020-06-01 10:06:30 +00:00
|
|
|
let uid = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<Uid>()
|
|
|
|
.get(player)
|
|
|
|
.expect("Failed to get uid for player")
|
|
|
|
.0;
|
|
|
|
server
|
|
|
|
.state
|
|
|
|
.notify_registered_clients(ServerMsg::PlayerListUpdate(
|
|
|
|
PlayerListUpdate::LevelChange(uid, lvl),
|
|
|
|
));
|
|
|
|
|
|
|
|
if let Some(stats) = server
|
|
|
|
.state
|
|
|
|
.ecs_mut()
|
|
|
|
.write_storage::<comp::Stats>()
|
|
|
|
.get_mut(player)
|
|
|
|
{
|
2020-02-13 21:15:54 +00:00
|
|
|
stats.level.set_level(lvl);
|
2020-02-23 00:08:30 +00:00
|
|
|
|
|
|
|
stats.update_max_hp();
|
|
|
|
stats
|
|
|
|
.health
|
|
|
|
.set_to(stats.health.maximum(), comp::HealthSource::LevelUp);
|
2020-02-13 21:15:54 +00:00
|
|
|
} else {
|
|
|
|
error_msg = Some(ServerMsg::private(String::from("Player has no stats!")));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
error_msg = Some(e);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(msg) = error_msg {
|
2020-04-24 02:36:19 +00:00
|
|
|
server.notify_client(client, msg);
|
2020-02-13 21:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 04:48:57 +00:00
|
|
|
use common::comp::Item;
|
2020-04-24 02:36:19 +00:00
|
|
|
fn handle_debug(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
2019-10-31 04:48:57 +00:00
|
|
|
if let Ok(items) = assets::load_glob::<Item>("common.items.debug.*") {
|
2019-10-31 14:26:25 +00:00
|
|
|
server
|
|
|
|
.state()
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Inventory>()
|
2020-04-24 02:36:19 +00:00
|
|
|
.get_mut(target)
|
2019-10-31 14:26:25 +00:00
|
|
|
// TODO: Consider writing a `load_glob_cloned` in `assets` and using that here
|
|
|
|
.map(|inv| inv.push_all_unique(items.iter().map(|item| item.as_ref().clone())));
|
2019-10-31 04:48:57 +00:00
|
|
|
let _ = server
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::InventoryUpdate>()
|
2020-03-04 10:09:48 +00:00
|
|
|
.insert(
|
2020-04-24 02:36:19 +00:00
|
|
|
target,
|
2020-03-04 10:09:48 +00:00
|
|
|
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Debug),
|
|
|
|
);
|
2019-10-31 04:48:57 +00:00
|
|
|
} else {
|
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-10-31 14:26:25 +00:00
|
|
|
ServerMsg::private(String::from(
|
|
|
|
"Debug items not found? Something is very broken.",
|
|
|
|
)),
|
2019-10-31 04:48:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 04:38:28 +00:00
|
|
|
fn handle_remove_lights(
|
|
|
|
server: &mut Server,
|
2020-04-24 02:36:19 +00:00
|
|
|
client: EcsEntity,
|
|
|
|
target: EcsEntity,
|
2019-10-07 04:38:28 +00:00
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-05-05 22:33:16 +00:00
|
|
|
let opt_radius = scan_fmt_some!(&args, &action.arg_fmt(), f32);
|
2020-04-24 02:36:19 +00:00
|
|
|
let opt_player_pos = server.state.read_component_cloned::<comp::Pos>(target);
|
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();
|
2020-01-26 12:47:41 +00:00
|
|
|
for (entity, pos, _, _, _) in (
|
2019-10-07 04:03:53 +00:00
|
|
|
&ecs.entities(),
|
|
|
|
&ecs.read_storage::<comp::Pos>(),
|
|
|
|
&ecs.read_storage::<comp::LightEmitter>(),
|
2020-01-26 12:47:41 +00:00
|
|
|
!&ecs.read_storage::<comp::WaypointArea>(),
|
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
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-10-15 04:06:14 +00:00
|
|
|
None => server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-10-07 04:38:28 +00:00
|
|
|
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 {
|
2019-11-29 06:04:37 +00:00
|
|
|
if let Err(err) = server.state.delete_entity_recorded(entity) {
|
|
|
|
error!("Failed to delete light: {:?}", err);
|
|
|
|
}
|
2019-10-07 03:19:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 04:06:14 +00:00
|
|
|
server.notify_client(
|
2020-04-24 02:36:19 +00:00
|
|
|
client,
|
2019-10-07 04:38:28 +00:00
|
|
|
ServerMsg::private(String::from(format!("Removed {} lights!", size))),
|
2019-10-07 03:19:46 +00:00
|
|
|
);
|
|
|
|
}
|
2020-04-24 02:36:19 +00:00
|
|
|
|
|
|
|
fn handle_sudo(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
args: String,
|
|
|
|
action: &ChatCommand,
|
|
|
|
) {
|
2020-04-25 17:18:29 +00:00
|
|
|
if let (Some(player_alias), Some(cmd), cmd_args) =
|
2020-05-05 22:33:16 +00:00
|
|
|
scan_fmt_some!(&args, &action.arg_fmt(), String, String, String)
|
2020-04-24 02:36:19 +00:00
|
|
|
{
|
|
|
|
let cmd_args = cmd_args.unwrap_or(String::from(""));
|
2020-04-25 17:18:29 +00:00
|
|
|
let cmd = if cmd.chars().next() == Some('/') {
|
|
|
|
cmd.chars().skip(1).collect()
|
|
|
|
} else {
|
|
|
|
cmd
|
|
|
|
};
|
2020-05-05 22:33:16 +00:00
|
|
|
if let Some(action) = CHAT_COMMANDS.iter().find(|c| c.keyword() == cmd) {
|
2020-04-24 02:36:19 +00:00
|
|
|
let ecs = server.state.ecs();
|
2020-04-25 17:18:29 +00:00
|
|
|
let entity_opt = (&ecs.entities(), &ecs.read_storage::<comp::Player>())
|
|
|
|
.join()
|
|
|
|
.find(|(_, player)| player.alias == player_alias)
|
|
|
|
.map(|(entity, _)| entity);
|
2020-04-24 02:36:19 +00:00
|
|
|
if let Some(entity) = entity_opt {
|
2020-05-05 22:33:16 +00:00
|
|
|
get_handler(action)(server, client, entity, cmd_args, action);
|
2020-04-24 02:36:19 +00:00
|
|
|
} else {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!("Could not find that player")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!("Unknown command: /{}", cmd)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 22:33:16 +00:00
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(String::from(action.help_string())),
|
|
|
|
);
|
2020-04-24 02:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 15:59:53 +00:00
|
|
|
|
|
|
|
fn handle_version(
|
|
|
|
server: &mut Server,
|
|
|
|
client: EcsEntity,
|
|
|
|
_target: EcsEntity,
|
|
|
|
_args: String,
|
|
|
|
_action: &ChatCommand,
|
|
|
|
) {
|
|
|
|
server.notify_client(
|
|
|
|
client,
|
|
|
|
ServerMsg::private(format!(
|
|
|
|
"Server is running {}[{}]",
|
|
|
|
common::util::GIT_HASH.to_string(),
|
|
|
|
common::util::GIT_DATE.to_string(),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|