limit range of npc interactions

This commit is contained in:
Maxicarlos08 2023-10-02 21:40:59 +02:00
parent 8116e5be2c
commit 58a379ee47
No known key found for this signature in database
3 changed files with 25 additions and 20 deletions

View File

@ -3,6 +3,7 @@ pub const MAX_PICKUP_RANGE: f32 = 5.0;
pub const MAX_MOUNT_RANGE: f32 = 5.0;
pub const MAX_SPRITE_MOUNT_RANGE: f32 = 2.0;
pub const MAX_TRADE_RANGE: f32 = 20.0;
pub const MAX_NPCINTERACT_RANGE: f32 = 30.0;
pub const GRAVITY: f32 = 25.0;
pub const FRIC_GROUND: f32 = 0.15;

View File

@ -14,7 +14,10 @@ use common::{
tool::{AbilityMap, ToolKind},
Inventory, LootOwner, Pos, SkillGroupKind,
},
consts::{MAX_MOUNT_RANGE, MAX_SPRITE_MOUNT_RANGE, SOUND_TRAVEL_DIST_PER_VOLUME},
consts::{
MAX_MOUNT_RANGE, MAX_NPCINTERACT_RANGE, MAX_SPRITE_MOUNT_RANGE,
SOUND_TRAVEL_DIST_PER_VOLUME,
},
event::EventBus,
link::Is,
mounting::{Mount, Mounting, Rider, VolumeMounting, VolumePos, VolumeRider},
@ -86,17 +89,25 @@ pub fn handle_npc_interaction(
subject: Subject,
) {
let state = server.state_mut();
if let Some(agent) = state
let within_range = {
let positions = state.ecs().read_storage::<Pos>();
positions
.get(interactor)
.zip(positions.get(npc_entity))
.map_or(false, |(interactor_pos, npc_pos)| {
interactor_pos.0.distance_squared(npc_pos.0) <= MAX_NPCINTERACT_RANGE.powi(2)
})
};
if within_range && let Some(agent) = state
.ecs()
.write_storage::<comp::Agent>()
.get_mut(npc_entity)
.get_mut(npc_entity) && agent.target.is_none()
{
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));
}
if let Some(interactor_uid) = state.ecs().uid_from_entity(interactor) {
agent
.inbox
.push_back(AgentEvent::Talk(interactor_uid, subject));
}
}
}

View File

@ -557,7 +557,7 @@ pub fn handle_inbox_cancel_interactions(bdata: &mut BehaviorData) -> bool {
} = bdata;
if let Some(msg) = agent.inbox.front() {
let used = match msg {
match msg {
AgentEvent::Talk(by, _) | AgentEvent::TradeAccepted(by) => {
if agent
.target
@ -571,8 +571,6 @@ pub fn handle_inbox_cancel_interactions(bdata: &mut BehaviorData) -> bool {
event_emitter,
);
}
false
},
AgentEvent::TradeInvite(by) => {
controller.push_invite_response(InviteResponse::Decline);
@ -596,7 +594,6 @@ pub fn handle_inbox_cancel_interactions(bdata: &mut BehaviorData) -> bool {
}
}
}
true
},
AgentEvent::FinishedTrade(result) => {
// copy pasted from recv_interaction
@ -621,7 +618,6 @@ pub fn handle_inbox_cancel_interactions(bdata: &mut BehaviorData) -> bool {
agent.behavior.unset(BehaviorState::TRADING);
agent.target = None;
}
true
},
AgentEvent::UpdatePendingTrade(boxval) => {
// immediately cancel the trade
@ -638,14 +634,11 @@ pub fn handle_inbox_cancel_interactions(bdata: &mut BehaviorData) -> bool {
agent,
event_emitter,
);
true
},
AgentEvent::ServerSound(_) | AgentEvent::Hurt => false,
AgentEvent::ServerSound(_) | AgentEvent::Hurt => return false,
};
if used {
agent.inbox.pop_front();
}
return used;
agent.inbox.pop_front();
}
false
}