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 |
root |
1.5 |
use strict; |
14 |
|
|
|
15 |
root |
1.4 |
sub has_dialogue($) { |
16 |
root |
1.1 |
my ($ob) = @_; |
17 |
|
|
|
18 |
|
|
$ob->get_message =~ /^\@match /; |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
sub parse_message($) { |
22 |
|
|
map [split /\n/, $_, 2], |
23 |
|
|
grep length, |
24 |
|
|
split /^\@match /m, |
25 |
|
|
$_[0] |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
sub new { |
29 |
|
|
my ($class, %arg) = @_; |
30 |
|
|
|
31 |
|
|
my $self = bless { |
32 |
|
|
%arg, |
33 |
|
|
}, $class; |
34 |
|
|
|
35 |
|
|
$self->{match} ||= [parse_message $self->{npc}->get_message]; |
36 |
|
|
|
37 |
|
|
$self; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
sub greet { |
41 |
|
|
my ($self) = @_; |
42 |
|
|
|
43 |
|
|
$self->tell ("hi") |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
sub tell { |
47 |
|
|
my ($self, $msg) = @_; |
48 |
|
|
|
49 |
root |
1.5 |
my $lcmsg = lc $msg; |
50 |
root |
1.3 |
|
51 |
root |
1.5 |
match: |
52 |
root |
1.1 |
for my $match (@{ $self->{match} }) { |
53 |
|
|
for (split /\|/, $match->[0]) { |
54 |
root |
1.5 |
if ($_ eq "*" || $lcmsg eq lc) { |
55 |
root |
1.1 |
my $reply = $match->[1]; |
56 |
|
|
|
57 |
root |
1.5 |
my @match; # @match/@parse command results |
58 |
|
|
|
59 |
|
|
# now execute @-commands (which can result in a no-match) |
60 |
|
|
while ($reply =~ s/^\@(\w+)\s*([^\n]*)\n?//) { |
61 |
|
|
my ($cmd, $args) = ($1, $2); |
62 |
|
|
|
63 |
|
|
if ($cmd eq "parse" || $cmd eq "match") { # match is future rename |
64 |
|
|
no re 'eval'; # default, but make sure |
65 |
|
|
@match = $msg =~ /$args/i |
66 |
|
|
or next match; |
67 |
|
|
|
68 |
|
|
} elsif ($cmd eq "cond") { |
69 |
|
|
cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg |
70 |
|
|
or next match; |
71 |
|
|
|
72 |
|
|
} elsif ($cmd eq "eval") { |
73 |
|
|
cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg; |
74 |
|
|
warn "\@eval evaluation error: $@\n" if $@; |
75 |
|
|
|
76 |
|
|
} else { |
77 |
|
|
warn "unknown dialogue command <$cmd,$args> used (from " . $self->{npc}->get_message . ")"; |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
root |
1.1 |
# combine lines into paragraphs |
82 |
|
|
$reply =~ s/(?<=\S)\n(?=\w)/ /g; |
83 |
|
|
$reply =~ s/\n\n/\n/g; |
84 |
|
|
|
85 |
|
|
my @kw; |
86 |
|
|
# now mark up all matching keywords |
87 |
|
|
for my $match (@{ $self->{match} }) { |
88 |
|
|
for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { |
89 |
|
|
if ($reply =~ /\b\Q$_\E\b/i) { |
90 |
|
|
push @kw, $_; |
91 |
|
|
last; |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
return wantarray ? ($reply, @kw) : $reply; |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
() |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
1 |