1 |
root |
1.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 |
root |
1.3 |
$msg = lc $msg; |
48 |
|
|
|
49 |
root |
1.1 |
for my $match (@{ $self->{match} }) { |
50 |
|
|
for (split /\|/, $match->[0]) { |
51 |
root |
1.3 |
if ($_ eq "*" || $msg eq lc) { |
52 |
root |
1.1 |
my $reply = $match->[1]; |
53 |
|
|
|
54 |
|
|
# combine lines into paragraphs |
55 |
|
|
$reply =~ s/(?<=\S)\n(?=\w)/ /g; |
56 |
|
|
$reply =~ s/\n\n/\n/g; |
57 |
|
|
|
58 |
|
|
my @kw; |
59 |
|
|
# now mark up all matching keywords |
60 |
|
|
for my $match (@{ $self->{match} }) { |
61 |
|
|
for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { |
62 |
|
|
if ($reply =~ /\b\Q$_\E\b/i) { |
63 |
|
|
push @kw, $_; |
64 |
|
|
last; |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
return wantarray ? ($reply, @kw) : $reply; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
() |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
1 |