2021-05-15 19:36:27 +00:00
|
|
|
use specs::{world::WorldExt, Builder, Entity as EcsEntity, Join};
|
2021-03-21 16:09:16 +00:00
|
|
|
use vek::*;
|
2021-01-08 19:12:09 +00:00
|
|
|
|
2020-02-16 20:04:06 +00:00
|
|
|
use common::{
|
2021-06-09 05:14:20 +00:00
|
|
|
assets,
|
2021-03-21 17:45:01 +00:00
|
|
|
comp::{
|
2021-05-15 19:36:27 +00:00
|
|
|
self,
|
2021-07-31 19:33:28 +00:00
|
|
|
agent::{AgentEvent, Sound, SoundKind},
|
2021-05-15 19:36:27 +00:00
|
|
|
dialogue::Subject,
|
|
|
|
inventory::slot::EquipSlot,
|
2022-06-12 17:56:59 +00:00
|
|
|
loot_owner::LootOwnerKind,
|
2021-05-15 19:36:27 +00:00
|
|
|
tool::ToolKind,
|
2022-06-12 17:56:59 +00:00
|
|
|
Inventory, LootOwner, Pos, SkillGroupKind,
|
2021-03-21 17:45:01 +00:00
|
|
|
},
|
2021-05-15 19:36:27 +00:00
|
|
|
consts::{MAX_MOUNT_RANGE, SOUND_TRAVEL_DIST_PER_VOLUME},
|
2022-05-09 19:58:13 +00:00
|
|
|
event::EventBus,
|
2022-01-16 17:06:35 +00:00
|
|
|
link::Is,
|
|
|
|
mounting::{Mount, Mounting, Rider},
|
2021-03-21 20:09:59 +00:00
|
|
|
outcome::Outcome,
|
2021-06-19 18:53:23 +00:00
|
|
|
terrain::{Block, SpriteKind},
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::Uid,
|
2021-03-21 16:09:16 +00:00
|
|
|
vol::ReadVol,
|
2020-02-16 20:04:06 +00:00
|
|
|
};
|
2022-02-23 03:26:15 +00:00
|
|
|
use common_net::sync::WorldSyncExt;
|
2021-01-08 19:12:09 +00:00
|
|
|
|
2022-02-23 03:26:15 +00:00
|
|
|
use crate::{state_ext::StateExt, Server};
|
2020-02-16 20:04:06 +00:00
|
|
|
|
2021-07-28 22:36:41 +00:00
|
|
|
use crate::pet::tame_pet;
|
2021-06-09 20:03:25 +00:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
2021-06-09 05:14:20 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use serde::Deserialize;
|
2021-06-09 20:03:25 +00:00
|
|
|
use std::iter::FromIterator;
|
2021-06-09 05:14:20 +00:00
|
|
|
|
2020-10-07 02:23:20 +00:00
|
|
|
pub fn handle_lantern(server: &mut Server, entity: EcsEntity, enable: bool) {
|
2020-05-04 15:15:31 +00:00
|
|
|
let ecs = server.state_mut().ecs();
|
2020-10-07 02:23:20 +00:00
|
|
|
|
|
|
|
let lantern_exists = ecs
|
2020-05-04 15:15:31 +00:00
|
|
|
.read_storage::<comp::LightEmitter>()
|
|
|
|
.get(entity)
|
2020-10-07 02:23:20 +00:00
|
|
|
.map_or(false, |light| light.strength > 0.0);
|
|
|
|
|
|
|
|
if lantern_exists != enable {
|
|
|
|
if !enable {
|
|
|
|
server
|
|
|
|
.state_mut()
|
|
|
|
.ecs()
|
2020-05-04 15:15:31 +00:00
|
|
|
.write_storage::<comp::LightEmitter>()
|
2020-10-07 02:23:20 +00:00
|
|
|
.remove(entity);
|
2021-07-06 21:35:35 +00:00
|
|
|
} else if ecs // Only enable lantern if entity is alive
|
|
|
|
.read_storage::<comp::Health>()
|
|
|
|
.get(entity)
|
|
|
|
.map_or(true, |h| !h.is_dead)
|
|
|
|
{
|
2021-01-08 19:12:09 +00:00
|
|
|
let inventory_storage = ecs.read_storage::<Inventory>();
|
2021-11-18 15:41:08 +00:00
|
|
|
let lantern_info = inventory_storage
|
2020-10-07 02:23:20 +00:00
|
|
|
.get(entity)
|
2021-01-08 19:12:09 +00:00
|
|
|
.and_then(|inventory| inventory.equipped(EquipSlot::Lantern))
|
2020-10-07 02:23:20 +00:00
|
|
|
.and_then(|item| {
|
2021-11-18 15:41:08 +00:00
|
|
|
if let comp::item::ItemKind::Lantern(l) = &*item.kind() {
|
|
|
|
Some((l.color(), l.strength()))
|
2020-10-07 02:23:20 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-05-04 15:15:31 +00:00
|
|
|
});
|
2021-11-18 15:41:08 +00:00
|
|
|
if let Some((col, strength)) = lantern_info {
|
2020-10-07 02:23:20 +00:00
|
|
|
let _ =
|
|
|
|
ecs.write_storage::<comp::LightEmitter>()
|
|
|
|
.insert(entity, comp::LightEmitter {
|
2021-11-18 15:41:08 +00:00
|
|
|
col,
|
|
|
|
strength,
|
2020-10-07 02:23:20 +00:00
|
|
|
flicker: 0.35,
|
|
|
|
animated: true,
|
|
|
|
});
|
|
|
|
}
|
2020-05-04 15:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 20:29:50 +00:00
|
|
|
pub fn handle_npc_interaction(server: &mut Server, interactor: EcsEntity, npc_entity: EcsEntity) {
|
|
|
|
let state = server.state_mut();
|
|
|
|
if let Some(agent) = state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::Agent>()
|
|
|
|
.get_mut(npc_entity)
|
|
|
|
{
|
2021-08-22 03:28:26 +00:00
|
|
|
if agent.target.is_none() {
|
|
|
|
if let Some(interactor_uid) = state.ecs().uid_from_entity(interactor) {
|
|
|
|
agent
|
|
|
|
.inbox
|
|
|
|
.push_back(AgentEvent::Talk(interactor_uid, Subject::Regular));
|
|
|
|
}
|
2021-01-31 20:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 18:22:28 +00:00
|
|
|
pub fn handle_mount(server: &mut Server, rider: EcsEntity, mount: EcsEntity) {
|
2020-02-16 20:04:06 +00:00
|
|
|
let state = server.state_mut();
|
|
|
|
|
2022-01-16 17:06:35 +00:00
|
|
|
if state.ecs().read_storage::<Is<Rider>>().get(rider).is_none() {
|
|
|
|
let not_mounting_yet = state.ecs().read_storage::<Is<Mount>>().get(mount).is_none();
|
2020-02-16 20:04:06 +00:00
|
|
|
|
2021-07-03 04:58:11 +00:00
|
|
|
let within_range = || {
|
|
|
|
let positions = state.ecs().read_storage::<comp::Pos>();
|
2022-01-15 18:22:28 +00:00
|
|
|
within_mounting_range(positions.get(rider), positions.get(mount))
|
2021-05-15 23:48:14 +00:00
|
|
|
};
|
2021-07-03 04:58:11 +00:00
|
|
|
let healths = state.ecs().read_storage::<comp::Health>();
|
|
|
|
let alive = |e| healths.get(e).map_or(true, |h| !h.is_dead);
|
2020-10-31 02:34:44 +00:00
|
|
|
|
2022-01-15 18:22:28 +00:00
|
|
|
if not_mounting_yet && within_range() && alive(rider) && alive(mount) {
|
2021-07-03 04:58:11 +00:00
|
|
|
let uids = state.ecs().read_storage::<Uid>();
|
2022-01-15 18:22:28 +00:00
|
|
|
if let (Some(rider_uid), Some(mount_uid)) =
|
|
|
|
(uids.get(rider).copied(), uids.get(mount).copied())
|
2021-07-03 04:58:11 +00:00
|
|
|
{
|
2022-01-16 17:06:35 +00:00
|
|
|
let is_pet = matches!(
|
|
|
|
state
|
|
|
|
.ecs()
|
|
|
|
.read_storage::<comp::Alignment>()
|
|
|
|
.get(mount),
|
|
|
|
Some(comp::Alignment::Owned(owner)) if *owner == rider_uid,
|
|
|
|
);
|
2022-01-15 21:43:20 +00:00
|
|
|
|
|
|
|
if is_pet {
|
|
|
|
drop(uids);
|
|
|
|
drop(healths);
|
|
|
|
let _ = state.link(Mounting {
|
|
|
|
mount: mount_uid,
|
|
|
|
rider: rider_uid,
|
|
|
|
});
|
|
|
|
}
|
2020-02-16 20:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 18:22:28 +00:00
|
|
|
pub fn handle_unmount(server: &mut Server, rider: EcsEntity) {
|
2020-02-16 20:04:06 +00:00
|
|
|
let state = server.state_mut();
|
2022-01-16 17:06:35 +00:00
|
|
|
state.ecs().write_storage::<Is<Rider>>().remove(rider);
|
2020-02-16 20:04:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 02:34:44 +00:00
|
|
|
fn within_mounting_range(player_position: Option<&Pos>, mount_position: Option<&Pos>) -> bool {
|
|
|
|
match (player_position, mount_position) {
|
|
|
|
(Some(ppos), Some(ipos)) => ppos.0.distance_squared(ipos.0) < MAX_MOUNT_RANGE.powi(2),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 16:09:16 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
#[derive(Deserialize)]
|
2021-10-13 14:44:28 +00:00
|
|
|
struct ResourceExperienceManifest(HashMap<String, u32>);
|
2021-06-09 05:14:20 +00:00
|
|
|
|
|
|
|
impl assets::Asset for ResourceExperienceManifest {
|
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref RESOURCE_EXPERIENCE_MANIFEST: assets::AssetHandle<ResourceExperienceManifest> =
|
|
|
|
assets::AssetExt::load_expect("server.manifests.resource_experience_manifest");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_mine_block(
|
|
|
|
server: &mut Server,
|
|
|
|
entity: EcsEntity,
|
|
|
|
pos: Vec3<i32>,
|
|
|
|
tool: Option<ToolKind>,
|
|
|
|
) {
|
2021-03-21 16:09:16 +00:00
|
|
|
let state = server.state_mut();
|
|
|
|
if state.can_set_block(pos) {
|
|
|
|
let block = state.terrain().get(pos).ok().copied();
|
2021-03-21 20:38:08 +00:00
|
|
|
if let Some(block) = block.filter(|b| b.mine_tool().map_or(false, |t| Some(t) == tool)) {
|
2021-03-21 20:09:59 +00:00
|
|
|
// Drop item if one is recoverable from the block
|
2021-06-09 05:14:20 +00:00
|
|
|
if let Some(mut item) = comp::Item::try_reclaim_from_block(block) {
|
2022-06-12 17:56:59 +00:00
|
|
|
let maybe_uid = state.ecs().uid_from_entity(entity);
|
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
if let Some(mut skillset) = state
|
|
|
|
.ecs()
|
|
|
|
.write_storage::<comp::SkillSet>()
|
|
|
|
.get_mut(entity)
|
|
|
|
{
|
|
|
|
if let (Some(tool), Some(uid), Some(exp_reward)) = (
|
|
|
|
tool,
|
2022-06-12 17:56:59 +00:00
|
|
|
maybe_uid,
|
2022-05-05 20:42:38 +00:00
|
|
|
item.item_definition_id()
|
2022-05-09 03:30:56 +00:00
|
|
|
.itemdef_id()
|
2022-05-05 20:42:38 +00:00
|
|
|
.and_then(|id| RESOURCE_EXPERIENCE_MANIFEST.read().0.get(id).copied()),
|
2021-06-09 05:14:20 +00:00
|
|
|
) {
|
2021-12-02 06:31:20 +00:00
|
|
|
let skill_group = SkillGroupKind::Weapon(tool);
|
2022-05-09 19:58:13 +00:00
|
|
|
let outcome_bus = state.ecs().read_resource::<EventBus<Outcome>>();
|
2022-03-15 18:04:21 +00:00
|
|
|
if let Some(level_outcome) =
|
|
|
|
skillset.add_experience(skill_group, exp_reward)
|
|
|
|
{
|
2022-05-09 19:58:13 +00:00
|
|
|
outcome_bus.emit_now(Outcome::SkillPointGain {
|
2021-06-09 05:14:20 +00:00
|
|
|
uid,
|
2021-12-02 06:31:20 +00:00
|
|
|
skill_tree: skill_group,
|
|
|
|
total_points: level_outcome,
|
2021-06-09 05:14:20 +00:00
|
|
|
});
|
2021-12-02 06:31:20 +00:00
|
|
|
}
|
2022-05-09 19:58:13 +00:00
|
|
|
outcome_bus.emit_now(Outcome::ExpChange {
|
2021-12-02 06:31:20 +00:00
|
|
|
uid,
|
2022-05-05 20:42:38 +00:00
|
|
|
exp: exp_reward,
|
2021-12-02 06:31:20 +00:00
|
|
|
xp_pools: HashSet::from_iter(vec![skill_group]),
|
|
|
|
});
|
2021-06-09 05:14:20 +00:00
|
|
|
}
|
2021-08-22 10:50:01 +00:00
|
|
|
use common::comp::skills::{MiningSkill, Skill, SKILL_MODIFIERS};
|
2021-06-09 05:14:20 +00:00
|
|
|
use rand::Rng;
|
|
|
|
let mut rng = rand::thread_rng();
|
2021-08-22 10:50:01 +00:00
|
|
|
|
|
|
|
let need_double_ore = |rng: &mut rand::rngs::ThreadRng| {
|
2021-08-24 20:00:03 +00:00
|
|
|
let chance_mod = f64::from(SKILL_MODIFIERS.mining_tree.ore_gain);
|
2021-12-02 04:38:33 +00:00
|
|
|
let skill_level = skillset
|
|
|
|
.skill_level(Skill::Pick(MiningSkill::OreGain))
|
|
|
|
.unwrap_or(0);
|
2021-08-22 10:50:01 +00:00
|
|
|
|
|
|
|
rng.gen_bool(chance_mod * f64::from(skill_level))
|
|
|
|
};
|
|
|
|
let need_double_gem = |rng: &mut rand::rngs::ThreadRng| {
|
2021-08-24 20:00:03 +00:00
|
|
|
let chance_mod = f64::from(SKILL_MODIFIERS.mining_tree.gem_gain);
|
2021-12-02 04:38:33 +00:00
|
|
|
let skill_level = skillset
|
|
|
|
.skill_level(Skill::Pick(MiningSkill::GemGain))
|
|
|
|
.unwrap_or(0);
|
2021-08-22 10:50:01 +00:00
|
|
|
|
|
|
|
rng.gen_bool(chance_mod * f64::from(skill_level))
|
|
|
|
};
|
|
|
|
|
2022-05-09 03:30:56 +00:00
|
|
|
let double_gain = item.item_definition_id().itemdef_id().map_or(false, |id| {
|
2022-05-05 20:42:38 +00:00
|
|
|
(id.contains("mineral.ore.") && need_double_ore(&mut rng))
|
|
|
|
|| (id.contains("mineral.gem.") && need_double_gem(&mut rng))
|
|
|
|
});
|
2021-08-22 10:50:01 +00:00
|
|
|
|
|
|
|
if double_gain {
|
|
|
|
// Ignore non-stackable errors
|
2021-06-09 05:14:20 +00:00
|
|
|
let _ = item.increase_amount(1);
|
|
|
|
}
|
|
|
|
}
|
2022-06-12 17:56:59 +00:00
|
|
|
let item_drop = state
|
2022-05-28 12:06:49 +00:00
|
|
|
.create_item_drop(Default::default(), item)
|
2022-06-12 17:56:59 +00:00
|
|
|
.with(comp::Pos(pos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0)));
|
|
|
|
if let Some(uid) = maybe_uid {
|
|
|
|
item_drop.with(LootOwner::new(LootOwnerKind::Player(uid)))
|
|
|
|
} else {
|
|
|
|
item_drop
|
|
|
|
}
|
|
|
|
.build();
|
2021-03-21 16:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state.set_block(pos, block.into_vacant());
|
2021-03-21 20:09:59 +00:00
|
|
|
state
|
|
|
|
.ecs()
|
2022-05-09 19:58:13 +00:00
|
|
|
.read_resource::<EventBus<Outcome>>()
|
|
|
|
.emit_now(Outcome::BreakBlock {
|
2021-03-21 20:09:59 +00:00
|
|
|
pos,
|
|
|
|
color: block.get_color(),
|
|
|
|
});
|
2021-03-21 16:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 19:36:27 +00:00
|
|
|
|
|
|
|
pub fn handle_sound(server: &mut Server, sound: &Sound) {
|
|
|
|
let ecs = &server.state.ecs();
|
|
|
|
let positions = &ecs.read_storage::<comp::Pos>();
|
|
|
|
let agents = &mut ecs.write_storage::<comp::Agent>();
|
|
|
|
|
2021-06-15 16:57:52 +00:00
|
|
|
// TODO: Reduce the complexity of this problem by using spatial partitioning
|
|
|
|
// system
|
2021-05-15 19:36:27 +00:00
|
|
|
for (agent, agent_pos) in (agents, positions).join() {
|
|
|
|
// TODO: Use pathfinding for more dropoff around obstacles
|
|
|
|
let agent_dist_sqrd = agent_pos.0.distance_squared(sound.pos);
|
|
|
|
let sound_travel_dist_sqrd = (sound.vol * SOUND_TRAVEL_DIST_PER_VOLUME).powi(2);
|
|
|
|
|
|
|
|
let vol_dropoff = agent_dist_sqrd / sound_travel_dist_sqrd * sound.vol;
|
|
|
|
let propagated_sound = sound.with_new_vol(sound.vol - vol_dropoff);
|
|
|
|
|
|
|
|
let can_hear_sound = propagated_sound.vol > 0.00;
|
2021-07-31 19:33:28 +00:00
|
|
|
let should_hear_sound = agent_dist_sqrd < agent.psyche.listen_dist.powi(2);
|
2021-05-15 19:36:27 +00:00
|
|
|
|
|
|
|
if can_hear_sound && should_hear_sound {
|
|
|
|
agent
|
|
|
|
.inbox
|
|
|
|
.push_back(AgentEvent::ServerSound(propagated_sound));
|
|
|
|
}
|
2021-06-16 16:16:05 +00:00
|
|
|
}
|
2021-06-15 16:15:58 +00:00
|
|
|
|
2021-06-16 16:16:05 +00:00
|
|
|
// Attempt to turn this sound into an outcome to be received by frontends.
|
|
|
|
if let Some(outcome) = match sound.kind {
|
|
|
|
SoundKind::Utterance(kind, body) => Some(Outcome::Utterance {
|
|
|
|
kind,
|
|
|
|
pos: sound.pos,
|
|
|
|
body,
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
} {
|
2022-05-09 19:58:13 +00:00
|
|
|
ecs.read_resource::<EventBus<Outcome>>().emit_now(outcome);
|
2021-05-15 19:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 18:53:23 +00:00
|
|
|
|
|
|
|
pub fn handle_create_sprite(server: &mut Server, pos: Vec3<i32>, sprite: SpriteKind) {
|
|
|
|
let state = server.state_mut();
|
|
|
|
if state.can_set_block(pos) {
|
|
|
|
let block = state.terrain().get(pos).ok().copied();
|
|
|
|
if block.map_or(false, |b| (*b).is_air()) {
|
|
|
|
let new_block = state
|
|
|
|
.get_block(pos)
|
|
|
|
.unwrap_or_else(|| Block::air(SpriteKind::Empty))
|
|
|
|
.with_sprite(sprite);
|
|
|
|
server.state.set_block(pos, new_block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 22:36:41 +00:00
|
|
|
|
|
|
|
pub fn handle_tame_pet(server: &mut Server, pet_entity: EcsEntity, owner_entity: EcsEntity) {
|
|
|
|
// TODO: Raise outcome to send to clients to play sound/render an indicator
|
|
|
|
// showing taming success?
|
|
|
|
tame_pet(server.state.ecs(), pet_entity, owner_entity);
|
|
|
|
}
|