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