Address comments

This commit is contained in:
Vincent Foulon 2021-04-08 18:33:00 +02:00
parent a30cbaf735
commit bc1797a240
5 changed files with 64 additions and 50 deletions

View File

@ -136,6 +136,30 @@ impl From<BehaviorCapability> for Behavior {
}
impl Behavior {
/// Builder function
/// Set capabilities if Option is Some
pub fn maybe_with_capabilities(
mut self,
maybe_capabilities: Option<BehaviorCapability>,
) -> Self {
if let Some(capabilities) = maybe_capabilities {
self.allow(capabilities)
}
self
}
/// Builder function
/// Set trade_site and TRADE capability if Option is Some
pub fn with_trade_site(mut self, trade_site: Option<SiteId>) -> Self {
self.trade_site = trade_site;
if trade_site.is_some() {
self.allow(BehaviorCapability::TRADE);
} else {
self.deny(BehaviorCapability::TRADE);
}
self
}
/// Set capabilities to the Behavior
pub fn allow(&mut self, capabilities: BehaviorCapability) {
self.capabilities.set(capabilities, true)
@ -281,18 +305,16 @@ pub struct Agent {
}
impl Agent {
pub fn set_patrol_origin(mut self, origin: Vec3<f32>) -> Self {
pub fn with_patrol_origin(mut self, origin: Vec3<f32>) -> Self {
self.patrol_origin = Some(origin);
self
}
pub fn with_destination(behavior: Behavior, pos: Vec3<f32>) -> Self {
Self {
psyche: Psyche { aggro: 1.0 },
rtsim_controller: RtSimController::with_destination(pos),
behavior,
..Default::default()
}
pub fn with_destination(mut self, pos: Vec3<f32>) -> Self {
self.psyche = Psyche { aggro: 1.0 };
self.rtsim_controller = RtSimController::with_destination(pos);
self.behavior.allow(BehaviorCapability::SPEAK);
self
}
pub fn new(

View File

@ -15,7 +15,7 @@ use common::{
buff::{BuffCategory, BuffData, BuffKind, BuffSource},
inventory::item::MaterialStatManifest,
invite::InviteKind,
BehaviorCapability, ChatType, Inventory, Item, LightEmitter, WaypointArea,
ChatType, Inventory, Item, LightEmitter, WaypointArea,
},
effect::Effect,
event::{EventBus, ServerEvent},
@ -31,7 +31,6 @@ use common_net::{
sync::WorldSyncExt,
};
use common_sys::state::BuildAreas;
use comp::Behavior;
use rand::Rng;
use specs::{Builder, Entity as EcsEntity, Join, WorldExt};
use std::{
@ -889,7 +888,7 @@ fn handle_spawn(
if let comp::Alignment::Owned(_) | comp::Alignment::Npc = alignment {
comp::Agent::default()
} else {
comp::Agent::default().set_patrol_origin(pos.0)
comp::Agent::default().with_patrol_origin(pos.0)
};
for _ in 0..amount {
@ -1075,10 +1074,7 @@ fn handle_spawn_airship(
animated: true,
});
if let Some(pos) = destination {
builder = builder.with(comp::Agent::with_destination(
Behavior::from(BehaviorCapability::SPEAK),
pos,
));
builder = builder.with(comp::Agent::default().with_destination(pos))
}
builder.build();

View File

@ -225,13 +225,18 @@ pub fn handle_invite_accept(server: &mut Server, entity: specs::Entity) {
}
let pricing = agents
.get(inviter)
.and_then(|a| a.behavior.trade_site.map(|id| index.get_site_prices(id)))
.and_then(|a| {
a.behavior
.trade_site
.and_then(|id| index.get_site_prices(id))
})
.or_else(|| {
agents.get(entity).and_then(|a| {
a.behavior.trade_site.map(|id| index.get_site_prices(id))
a.behavior
.trade_site
.and_then(|id| index.get_site_prices(id))
})
})
.flatten();
});
clients.get(inviter).map(|c| {
c.send(ServerGeneral::UpdatePendingTrade(
id,

View File

@ -32,22 +32,21 @@ fn notify_agent_prices(
entity: EcsEntity,
event: AgentEvent,
) {
if let Some(agent) = agents.get_mut(entity) {
if let Some(site_id) = agent.behavior.trade_site {
let prices = index.get_site_prices(site_id);
if let AgentEvent::UpdatePendingTrade(boxval) = event {
// Box<(tid, pend, _, inventories)>) = event {
agent
.inbox
.push_front(AgentEvent::UpdatePendingTrade(Box::new((
// Prefer using this Agent's price data, but use the counterparty's price
// data if we don't have price data
boxval.0,
boxval.1,
prices.unwrap_or(boxval.2),
boxval.3,
))));
}
if let Some((Some(site_id), agent)) = agents.get_mut(entity).map(|a| (a.behavior.trade_site, a))
{
let prices = index.get_site_prices(site_id);
if let AgentEvent::UpdatePendingTrade(boxval) = event {
// Box<(tid, pend, _, inventories)>) = event {
agent
.inbox
.push_front(AgentEvent::UpdatePendingTrade(Box::new((
// Prefer using this Agent's price data, but use the counterparty's price
// data if we don't have price data
boxval.0,
boxval.1,
prices.unwrap_or(boxval.2),
boxval.3,
))));
}
}
}
@ -122,12 +121,8 @@ pub fn handle_process_trade_action(
prices = prices.or_else(|| {
agents
.get(e)
.and_then(|a| {
a.behavior
.trade_site
.map(|id| server.index.get_site_prices(id))
})
.flatten()
.and_then(|a| a.behavior.trade_site)
.and_then(|id| server.index.get_site_prices(id))
});
}
}

View File

@ -193,18 +193,14 @@ impl<'a> System<'a> for Sys {
poise,
loadout,
agent: if entity.has_agency {
let mut behavior = Behavior::default();
if can_speak {
behavior.allow(BehaviorCapability::SPEAK);
}
if trade_for_site.is_some() {
behavior.allow(BehaviorCapability::TRADE);
behavior.trade_site = trade_for_site
}
Some(comp::Agent::new(
Some(entity.pos),
&body,
behavior,
Behavior::default()
.maybe_with_capabilities(
can_speak.then(|| BehaviorCapability::SPEAK),
)
.with_trade_site(trade_for_site),
matches!(
loadout_config,
Some(comp::inventory::loadout_builder::LoadoutConfig::Guard)