1 |
=head1 NAME |
2 |
|
3 |
NPC_Dialogue |
4 |
|
5 |
=head1 DESCRIPTION |
6 |
|
7 |
NPC dialogue support module. |
8 |
|
9 |
=cut |
10 |
|
11 |
package NPC_Dialogue; |
12 |
|
13 |
sub has_dialogue { |
14 |
my ($ob) = @_; |
15 |
|
16 |
$ob->get_message =~ /^\@match /; |
17 |
} |
18 |
|
19 |
sub parse_message($) { |
20 |
map [split /\n/, $_, 2], |
21 |
grep length, |
22 |
split /^\@match /m, |
23 |
$_[0] |
24 |
} |
25 |
|
26 |
sub new { |
27 |
my ($class, %arg) = @_; |
28 |
|
29 |
my $self = bless { |
30 |
%arg, |
31 |
}, $class; |
32 |
|
33 |
$self->{match} ||= [parse_message $self->{npc}->get_message]; |
34 |
|
35 |
$self; |
36 |
} |
37 |
|
38 |
sub greet { |
39 |
my ($self) = @_; |
40 |
|
41 |
$self->tell ("hi") |
42 |
} |
43 |
|
44 |
sub tell { |
45 |
my ($self, $msg) = @_; |
46 |
|
47 |
for my $match (@{ $self->{match} }) { |
48 |
for (split /\|/, $match->[0]) { |
49 |
if ($_ eq "*" || 0 <= index $msg, $_) { |
50 |
my $reply = $match->[1]; |
51 |
|
52 |
# combine lines into paragraphs |
53 |
$reply =~ s/(?<=\S)\n(?=\w)/ /g; |
54 |
$reply =~ s/\n\n/\n/g; |
55 |
|
56 |
my @kw; |
57 |
# now mark up all matching keywords |
58 |
for my $match (@{ $self->{match} }) { |
59 |
for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { |
60 |
if ($reply =~ /\b\Q$_\E\b/i) { |
61 |
push @kw, $_; |
62 |
last; |
63 |
} |
64 |
} |
65 |
} |
66 |
|
67 |
return wantarray ? ($reply, @kw) : $reply; |
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
() |
73 |
} |
74 |
|
75 |
1 |