=head1 NAME NPC_Dialogue =head1 DESCRIPTION NPC dialogue support module. =cut package NPC_Dialogue; sub has_dialogue($) { my ($ob) = @_; $ob->get_message =~ /^\@match /; } sub parse_message($) { map [split /\n/, $_, 2], grep length, split /^\@match /m, $_[0] } sub new { my ($class, %arg) = @_; my $self = bless { %arg, }, $class; $self->{match} ||= [parse_message $self->{npc}->get_message]; $self; } sub greet { my ($self) = @_; $self->tell ("hi") } sub tell { my ($self, $msg) = @_; $msg = lc $msg; for my $match (@{ $self->{match} }) { for (split /\|/, $match->[0]) { if ($_ eq "*" || $msg eq lc) { my $reply = $match->[1]; # combine lines into paragraphs $reply =~ s/(?<=\S)\n(?=\w)/ /g; $reply =~ s/\n\n/\n/g; my @kw; # now mark up all matching keywords for my $match (@{ $self->{match} }) { for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { if ($reply =~ /\b\Q$_\E\b/i) { push @kw, $_; last; } } } return wantarray ? ($reply, @kw) : $reply; } } } () } 1