… | |
… | |
7 | NPC dialogue support module. |
7 | NPC dialogue support module. |
8 | |
8 | |
9 | =cut |
9 | =cut |
10 | |
10 | |
11 | package NPC_Dialogue; |
11 | package NPC_Dialogue; |
|
|
12 | |
|
|
13 | use strict; |
12 | |
14 | |
13 | sub has_dialogue($) { |
15 | sub has_dialogue($) { |
14 | my ($ob) = @_; |
16 | my ($ob) = @_; |
15 | |
17 | |
16 | $ob->get_message =~ /^\@match /; |
18 | $ob->get_message =~ /^\@match /; |
… | |
… | |
42 | } |
44 | } |
43 | |
45 | |
44 | sub tell { |
46 | sub tell { |
45 | my ($self, $msg) = @_; |
47 | my ($self, $msg) = @_; |
46 | |
48 | |
47 | $msg = lc $msg; |
49 | my $lcmsg = lc $msg; |
48 | |
50 | |
|
|
51 | match: |
49 | for my $match (@{ $self->{match} }) { |
52 | for my $match (@{ $self->{match} }) { |
50 | for (split /\|/, $match->[0]) { |
53 | for (split /\|/, $match->[0]) { |
51 | if ($_ eq "*" || $msg eq lc) { |
54 | if ($_ eq "*" || $lcmsg eq lc) { |
52 | my $reply = $match->[1]; |
55 | my $reply = $match->[1]; |
|
|
56 | |
|
|
57 | 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 | } |
53 | |
80 | |
54 | # combine lines into paragraphs |
81 | # combine lines into paragraphs |
55 | $reply =~ s/(?<=\S)\n(?=\w)/ /g; |
82 | $reply =~ s/(?<=\S)\n(?=\w)/ /g; |
56 | $reply =~ s/\n\n/\n/g; |
83 | $reply =~ s/\n\n/\n/g; |
57 | |
84 | |