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.6 |
# combine lines into paragraphs |
58 |
|
|
$reply =~ s/(?<=\S)\n(?=\w)/ /g; |
59 |
|
|
$reply =~ s/\n\n/\n/g; |
60 |
|
|
|
61 |
|
|
my @replies; |
62 |
root |
1.5 |
my @match; # @match/@parse command results |
63 |
root |
1.6 |
local $self->{ob}{record_replies} = \@replies; |
64 |
root |
1.5 |
|
65 |
|
|
# now execute @-commands (which can result in a no-match) |
66 |
|
|
while ($reply =~ s/^\@(\w+)\s*([^\n]*)\n?//) { |
67 |
|
|
my ($cmd, $args) = ($1, $2); |
68 |
|
|
|
69 |
|
|
if ($cmd eq "parse" || $cmd eq "match") { # match is future rename |
70 |
|
|
no re 'eval'; # default, but make sure |
71 |
|
|
@match = $msg =~ /$args/i |
72 |
|
|
or next match; |
73 |
|
|
|
74 |
|
|
} elsif ($cmd eq "cond") { |
75 |
|
|
cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg |
76 |
|
|
or next match; |
77 |
|
|
|
78 |
|
|
} elsif ($cmd eq "eval") { |
79 |
|
|
cf::safe_eval $args, who => $self->{ob}, npc => $self->{npc}, msg => $msg; |
80 |
|
|
warn "\@eval evaluation error: $@\n" if $@; |
81 |
|
|
|
82 |
|
|
} else { |
83 |
|
|
warn "unknown dialogue command <$cmd,$args> used (from " . $self->{npc}->get_message . ")"; |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
|
87 |
root |
1.6 |
# ignores flags and npc from replies |
88 |
|
|
$reply = join "\n", (map $_->[1], @replies), $reply; |
89 |
root |
1.1 |
|
90 |
|
|
my @kw; |
91 |
|
|
# now mark up all matching keywords |
92 |
|
|
for my $match (@{ $self->{match} }) { |
93 |
|
|
for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { |
94 |
|
|
if ($reply =~ /\b\Q$_\E\b/i) { |
95 |
|
|
push @kw, $_; |
96 |
|
|
last; |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
return wantarray ? ($reply, @kw) : $reply; |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
() |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
1 |