From d53b344c237ba72af595cd928d570df5dcaa55c6 Mon Sep 17 00:00:00 2001 From: Isse Date: Sat, 1 Apr 2023 14:09:41 +0200 Subject: [PATCH] make merchants use tell, and general cleanup --- common/src/event.rs | 2 +- common/src/trade.rs | 25 +++---- rtsim/src/ai/mod.rs | 2 +- rtsim/src/gen/mod.rs | 4 +- rtsim/src/lib.rs | 6 +- rtsim/src/rule/npc_ai.rs | 12 +-- rtsim/src/rule/simulate_npcs.rs | 6 +- server/src/cmd.rs | 2 +- server/src/rtsim/mod.rs | 2 +- .../sys/agent/behavior_tree/interaction.rs | 72 ++++++------------ voxygen/src/hud/mod.rs | 74 ++++++++++--------- 11 files changed, 90 insertions(+), 117 deletions(-) diff --git a/common/src/event.rs b/common/src/event.rs index 629746b934..80d1c952a2 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -64,7 +64,7 @@ impl NpcBuilder { stats, skill_set: comp::SkillSet::default(), health: None, - poise: comp::Poise::new(body.clone()), + poise: comp::Poise::new(body), inventory: comp::Inventory::with_empty(), body, agent: None, diff --git a/common/src/trade.rs b/common/src/trade.rs index a3408d09a2..3b3ca0f7eb 100644 --- a/common/src/trade.rs +++ b/common/src/trade.rs @@ -389,20 +389,17 @@ impl SitePrices { .as_ref() .map(|ri| { let item = ri.inventory.get(slot)?; - if let Some(vec) = TradePricing::get_materials(&item.name.as_ref()) { - Some( - vec.iter() - .map(|(amount2, material)| { - self.values.get(material).copied().unwrap_or_default() - * *amount2 - * (if reduce { material.trade_margin() } else { 1.0 }) - }) - .sum::() - * (*amount as f32), - ) - } else { - None - } + let vec = TradePricing::get_materials(&item.name.as_ref())?; + Some( + vec.iter() + .map(|(amount2, material)| { + self.values.get(material).copied().unwrap_or_default() + * *amount2 + * (if reduce { material.trade_margin() } else { 1.0 }) + }) + .sum::() + * (*amount as f32), + ) }) .unwrap_or(Some(0.0)) }) diff --git a/rtsim/src/ai/mod.rs b/rtsim/src/ai/mod.rs index 1232935a62..2d27002298 100644 --- a/rtsim/src/ai/mod.rs +++ b/rtsim/src/ai/mod.rs @@ -469,7 +469,7 @@ impl Node + Send + Sync + 'static, R: 'static> Actio }; match prev.0.tick(ctx) { - ControlFlow::Continue(()) => return ControlFlow::Continue(()), + ControlFlow::Continue(()) => ControlFlow::Continue(()), ControlFlow::Break(r) => { self.prev = None; ControlFlow::Break(r) diff --git a/rtsim/src/gen/mod.rs b/rtsim/src/gen/mod.rs index 757cc68ef0..19ba5e692b 100644 --- a/rtsim/src/gen/mod.rs +++ b/rtsim/src/gen/mod.rs @@ -150,9 +150,7 @@ impl Data { .with_personality(Personality::random_evil(&mut rng)) .with_faction(site.faction) .with_home(site_id) - .with_profession(match rng.gen_range(0..20) { - _ => Profession::Cultist, - }), + .with_profession(Profession::Cultist), ); } } diff --git a/rtsim/src/lib.rs b/rtsim/src/lib.rs index f24a449f4a..3643dc458e 100644 --- a/rtsim/src/lib.rs +++ b/rtsim/src/lib.rs @@ -136,9 +136,9 @@ impl RtState { } pub fn emit(&mut self, e: E, world: &World, index: IndexRef) { - self.event_handlers - .get::>() - .map(|handlers| handlers.iter().for_each(|f| f(self, world, index, &e))); + if let Some(handlers) = self.event_handlers.get::>() { + handlers.iter().for_each(|f| f(self, world, index, &e)); + } } pub fn tick( diff --git a/rtsim/src/rule/npc_ai.rs b/rtsim/src/rule/npc_ai.rs index 81bbde6949..2050f42fd7 100644 --- a/rtsim/src/rule/npc_ai.rs +++ b/rtsim/src/rule/npc_ai.rs @@ -45,7 +45,7 @@ fn path_in_site(start: Vec2, end: Vec2, site: &site2::Site) -> PathRes let mut astar = Astar::new( 1000, start, - &heuristic, + heuristic, BuildHasherDefault::::default(), ); @@ -76,12 +76,12 @@ fn path_in_site(start: Vec2, end: Vec2, site: &site2::Site) -> PathRes let building = if a_tile.is_building() && b_tile.is_road() { a_tile .plot - .and_then(|plot| is_door_tile(plot, *a).then(|| 1.0)) + .and_then(|plot| is_door_tile(plot, *a).then_some(1.0)) .unwrap_or(10000.0) } else if b_tile.is_building() && a_tile.is_road() { b_tile .plot - .and_then(|plot| is_door_tile(plot, *b).then(|| 1.0)) + .and_then(|plot| is_door_tile(plot, *b).then_some(1.0)) .unwrap_or(10000.0) } else if (a_tile.is_building() || b_tile.is_building()) && a_tile.plot != b_tile.plot { 10000.0 @@ -493,7 +493,7 @@ fn adventure() -> impl Action { casual(finish().boxed()) } }) - .debug(move || format!("adventure")) + .debug(move || "adventure") } fn villager(visiting_site: SiteId) -> impl Action { @@ -513,9 +513,9 @@ fn villager(visiting_site: SiteId) -> impl Action { // Travel to the site we're supposed to be in urgent(travel_to_site(visiting_site).debug(move || { if npc_home == Some(visiting_site) { - format!("travel home") + "travel home".to_string() } else { - format!("travel to visiting site") + "travel to visiting site".to_string() } })) } else if DayPeriod::from(ctx.time_of_day.0).is_dark() diff --git a/rtsim/src/rule/simulate_npcs.rs b/rtsim/src/rule/simulate_npcs.rs index 2703c1d8b9..620825ac2c 100644 --- a/rtsim/src/rule/simulate_npcs.rs +++ b/rtsim/src/rule/simulate_npcs.rs @@ -27,10 +27,8 @@ impl Rule for SimulateNpcs { if let Some(vehicle) = data.npcs.vehicles.get_mut(ride.vehicle) { let actor = crate::data::Actor::Npc(npc_id); vehicle.riders.push(actor); - if ride.steering { - if vehicle.driver.replace(actor).is_some() { - panic!("Replaced driver"); - } + if ride.steering && vehicle.driver.replace(actor).is_some() { + panic!("Replaced driver"); } } } diff --git a/server/src/cmd.rs b/server/src/cmd.rs index e2b0857dd4..acc753ce1a 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -1290,7 +1290,7 @@ fn handle_rtsim_purge( ); Ok(()) } else { - return Err(action.help_string()); + Err(action.help_string()) } } diff --git a/server/src/rtsim/mod.rs b/server/src/rtsim/mod.rs index 1bda5169b4..d9f5d8db63 100644 --- a/server/src/rtsim/mod.rs +++ b/server/src/rtsim/mod.rs @@ -95,7 +95,7 @@ impl RtSim { ); } - let data = Data::generate(settings, &world, index); + let data = Data::generate(settings, world, index); info!("Rtsim data generated."); data }; diff --git a/server/src/sys/agent/behavior_tree/interaction.rs b/server/src/sys/agent/behavior_tree/interaction.rs index a64ff80afa..45b05a843f 100644 --- a/server/src/sys/agent/behavior_tree/interaction.rs +++ b/server/src/sys/agent/behavior_tree/interaction.rs @@ -165,25 +165,7 @@ pub fn handle_inbox_talk(bdata: &mut BehaviorData) -> bool { standard_response_msg() }; agent_data.chat_npc(msg, event_emitter); - } - /*else if agent.behavior.can_trade(agent_data.alignment.copied(), by) { - if !agent.behavior.is(BehaviorState::TRADING) { - controller.push_initiate_invite(by, InviteKind::Trade); - agent_data.chat_npc( - "npc-speech-merchant_advertisement", - event_emitter, - ); - } else { - let default_msg = "npc-speech-merchant_busy"; - let msg = if agent.rtsim_controller.personality.is(PersonalityTrait::Disagreeable) { - "npc-speech-merchant_busy_rude" - } else { - default_msg - }; - agent_data.chat_npc(msg, event_emitter); - } - }*/ - else { + } else { let mut rng = thread_rng(); if let Some(extreme_trait) = agent.rtsim_controller.personality.chat_trait(&mut rng) @@ -522,22 +504,36 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool { let (tradeid, pending, prices, inventories) = *boxval; if agent.behavior.is(BehaviorState::TRADING) { let who = usize::from(!agent.behavior.is(BehaviorState::TRADING_ISSUER)); + let mut message = |msg| { + if let Some(with) = agent + .target + .as_ref() + .and_then(|tgt_data| read_data.uids.get(tgt_data.target)) + { + event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_tell( + *agent_data.uid, + *with, + msg, + ))); + } else { + event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say( + *agent_data.uid, + msg, + ))); + } + }; match agent.behavior.trading_behavior { TradingBehavior::RequireBalanced { .. } => { let balance0 = prices.balance(&pending.offers, &inventories, 1 - who, true); let balance1 = prices.balance(&pending.offers, &inventories, who, false); match (balance0, balance1) { (_, None) => { - event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say( - *agent_data.uid, - format!("I'm not willing to sell that item"), - ))) + let msg = "I'm not willing to sell that item".to_string(); + message(msg); }, (None, _) => { - event_emitter.emit(ServerEvent::Chat(UnresolvedChatMsg::npc_say( - *agent_data.uid, - format!("I'm not willing to buy that item"), - ))) + let msg = "I'm not willing to buy that item".to_string(); + message(msg); }, (Some(balance0), Some(balance1)) => { if balance0 >= balance1 { @@ -567,27 +563,7 @@ pub fn handle_inbox_update_pending_trade(bdata: &mut BehaviorData) -> bool { "That only covers {:.0}% of my costs!", (balance0 / balance1 * 100.0).floor() ); - if let Some(tgt_data) = &agent.target { - // If talking with someone in particular, "tell" it only to - // them - if let Some(with) = read_data.uids.get(tgt_data.target) { - event_emitter.emit(ServerEvent::Chat( - UnresolvedChatMsg::npc_tell( - *agent_data.uid, - *with, - msg, - ), - )); - } else { - event_emitter.emit(ServerEvent::Chat( - UnresolvedChatMsg::npc_say(*agent_data.uid, msg), - )); - } - } else { - event_emitter.emit(ServerEvent::Chat( - UnresolvedChatMsg::npc_say(*agent_data.uid, msg), - )); - } + message(msg); } if pending.phase != TradePhase::Mutate { // we got into the review phase but without balanced goods, diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index f3102fed6e..342d470dc9 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -4035,44 +4035,48 @@ impl Hud { remove, quantity: &mut u32| { if let Some(prices) = prices { - let balance0 = prices + if let Some((balance0, balance1)) = prices .balance(&trade.offers, &r_inventories, who, true) - .unwrap_or(0.0); // TODO: Don't default to 0 here? - let balance1 = prices - .balance(&trade.offers, &r_inventories, 1 - who, false) - .unwrap_or(0.0); // TODO: Don't default to 0 here? - if let Some(item) = inventory.get(slot) { - if let Some(materials) = - TradePricing::get_materials(&item.item_definition_id()) - { - let unit_price: f32 = materials - .iter() - .map(|e| { - prices - .values - .get(&e.1) - .cloned() - .unwrap_or_default() - * e.0 - * (if ours { - e.1.trade_margin() - } else { - 1.0 - }) - }) - .sum(); + .zip(prices.balance( + &trade.offers, + &r_inventories, + 1 - who, + false, + )) + { + if let Some(item) = inventory.get(slot) { + if let Some(materials) = TradePricing::get_materials( + &item.item_definition_id(), + ) { + let unit_price: f32 = materials + .iter() + .map(|e| { + prices + .values + .get(&e.1) + .cloned() + .unwrap_or_default() + * e.0 + * (if ours { + e.1.trade_margin() + } else { + 1.0 + }) + }) + .sum(); - let mut float_delta = if ours ^ remove { - (balance1 - balance0) / unit_price - } else { - (balance0 - balance1) / unit_price - }; - if ours ^ remove { - float_delta = float_delta.ceil(); - } else { - float_delta = float_delta.floor(); + let mut float_delta = if ours ^ remove { + (balance1 - balance0) / unit_price + } else { + (balance0 - balance1) / unit_price + }; + if ours ^ remove { + float_delta = float_delta.ceil(); + } else { + float_delta = float_delta.floor(); + } + *quantity = float_delta.max(0.0) as u32; } - *quantity = float_delta.max(0.0) as u32; } } }