Made merchants advertise wares

This commit is contained in:
Joshua Barretto 2023-04-03 18:36:33 +01:00
parent b72d8f3192
commit 1fcb46ae0c
4 changed files with 36 additions and 7 deletions

View File

@ -222,7 +222,8 @@ pub enum NpcActivity {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum NpcAction { pub enum NpcAction {
Greet(Actor), Greet(Actor),
// TODO: Use some sort of structured, language-independent value that frontends can translate instead // TODO: Use some sort of structured, language-independent value that frontends can translate
// instead
Say(Cow<'static, str>), Say(Cow<'static, str>),
} }

View File

@ -124,11 +124,11 @@ pub trait Action<R = ()>: Any + Send + Sync {
/// go_on_an_adventure().repeat().stop_if(|ctx| ctx.npc.age > 111.0) /// go_on_an_adventure().repeat().stop_if(|ctx| ctx.npc.age > 111.0)
/// ``` /// ```
#[must_use] #[must_use]
fn stop_if<F: FnMut(&mut NpcCtx) -> bool>(self, f: F) -> StopIf<Self, F> fn stop_if<F: FnMut(&mut NpcCtx) -> bool + Clone>(self, f: F) -> StopIf<Self, F>
where where
Self: Sized, Self: Sized,
{ {
StopIf(self, f) StopIf(self, f.clone(), f)
} }
/// Map the completion value of this action to something else. /// Map the completion value of this action to something else.
@ -704,10 +704,10 @@ where
/// See [`Action::stop_if`]. /// See [`Action::stop_if`].
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct StopIf<A, F>(A, F); pub struct StopIf<A, F>(A, F, F);
impl<A: Action<R>, F: FnMut(&mut NpcCtx) -> bool + Send + Sync + 'static, R> Action<Option<R>> impl<A: Action<R>, F: FnMut(&mut NpcCtx) -> bool + Clone + Send + Sync + 'static, R>
for StopIf<A, F> Action<Option<R>> for StopIf<A, F>
{ {
fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) } fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) }
@ -715,7 +715,10 @@ impl<A: Action<R>, F: FnMut(&mut NpcCtx) -> bool + Send + Sync + 'static, R> Act
fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt); } fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt); }
fn reset(&mut self) { self.0.reset(); } fn reset(&mut self) {
self.0.reset();
self.1 = self.2.clone();
}
fn tick(&mut self, ctx: &mut NpcCtx) -> ControlFlow<Option<R>> { fn tick(&mut self, ctx: &mut NpcCtx) -> ControlFlow<Option<R>> {
if (self.1)(ctx) { if (self.1)(ctx) {

View File

@ -469,6 +469,7 @@ fn socialize() -> impl Action {
just(|ctx| ctx.controller.do_dance()) just(|ctx| ctx.controller.do_dance())
.repeat() .repeat()
.stop_if(timeout(6.0)) .stop_if(timeout(6.0))
.debug(|| "dancing")
.map(|_| ()) .map(|_| ())
.boxed() .boxed()
} else { } else {
@ -644,6 +645,29 @@ fn villager(visiting_site: SiteId) -> impl Action {
.map(|_| ()), .map(|_| ()),
); );
} }
} else if matches!(ctx.npc.profession, Some(Profession::Merchant))
&& thread_rng().gen_bool(0.8)
{
return casual(
just(|ctx| {
ctx.controller.say(
*[
"All my goods are of the highest quality!",
"Does anybody want to buy my wares?",
"I've got the best offers in town.",
"Looking for supplies? I've got you covered.",
]
.iter()
.choose(&mut thread_rng())
.unwrap(),
) // Can't fail
})
.then(idle().repeat().stop_if(timeout(8.0)))
.repeat()
.stop_if(timeout(60.0))
.debug(|| "sell wares")
.map(|_| ()),
);
} }
// If nothing else needs doing, walk between plazas and socialize // If nothing else needs doing, walk between plazas and socialize

View File

@ -506,6 +506,7 @@ fn handle_rtsim_actions(bdata: &mut BehaviorData) -> bool {
} }
}, },
NpcAction::Say(msg) => { NpcAction::Say(msg) => {
bdata.controller.push_utterance(UtteranceKind::Greeting);
bdata.agent_data.chat_npc(msg, &mut bdata.event_emitter); bdata.agent_data.chat_npc(msg, &mut bdata.event_emitter);
}, },
} }