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 | |
14 | |
13 | { |
15 | my %dialog; # currently active dialogs |
14 | package NPC_Dialogue; |
|
|
15 | |
16 | |
16 | sub has_dialogue { |
17 | my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { |
17 | my ($ob) = @_; |
18 | while (my ($token, $dialog) = each %dialog) { |
|
|
19 | my (undef, $dx, $dy) = $dialog->{ob}->rangevector ($dialog->{npc}); |
|
|
20 | next if (abs $dx) <= 2 && (abs $dy) <= 2; |
18 | |
21 | |
19 | $ob->get_message =~ /^\@match /; |
22 | $dialog->{ob}->contr->send ("ext $token out_of_range"); |
|
|
23 | delete $dialog{$token}; |
20 | } |
24 | } |
21 | |
25 | |
22 | sub parse_message($) { |
26 | $_[0]->w->stop unless keys %dialog; |
23 | map [split /\n/, $_, 2], |
27 | }); |
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 | |
|
|
79 | my %dialog; # currently active dialogs |
|
|
80 | |
28 | |
81 | sub dialog_tell { |
29 | sub dialog_tell { |
82 | my ($token, $dialog, $msg) = @_; |
30 | my ($token, $dialog, $msg) = @_; |
83 | |
31 | |
|
|
32 | utf8::decode $msg; |
84 | my $pl = $dialog->{ob}->contr; |
33 | my $pl = $dialog->{ob}->contr; |
85 | my ($reply, @kw) = $dialog->tell ($msg); |
34 | my ($reply, @kw) = $dialog->tell ($msg); |
86 | $reply = "..." unless $reply; |
35 | $reply = "..." unless $reply; |
|
|
36 | utf8::encode $_ for ($reply, @kw); |
87 | $pl->send ("ext $token msg " . join "\x00", $reply, @kw); |
37 | $pl->send ("ext $token msg " . join "\x00", $reply, @kw); |
88 | } |
38 | } |
89 | |
39 | |
90 | # return "interesting" information about the given tile |
40 | # return "interesting" information about the given tile |
91 | # currently only returns the npc_dialog title when a dialog is possible |
41 | # currently only returns the npc_dialog title when a dialog is possible |
… | |
… | |
117 | |
67 | |
118 | for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
68 | for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
119 | if (NPC_Dialogue::has_dialogue $npc) { |
69 | if (NPC_Dialogue::has_dialogue $npc) { |
120 | $dialog{$token} = new NPC_Dialogue ob => $pl->ob, npc => $npc; |
70 | $dialog{$token} = new NPC_Dialogue ob => $pl->ob, npc => $npc; |
121 | dialog_tell $token, $dialog{$token}, "hi"; |
71 | dialog_tell $token, $dialog{$token}, "hi"; |
|
|
72 | $timer->start; |
122 | return; |
73 | return; |
123 | } |
74 | } |
124 | } |
75 | } |
125 | |
76 | |
126 | $pl->send ("ext $token error"); |
77 | $pl->send ("ext $token error"); |
… | |
… | |
147 | delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog; |
98 | delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog; |
148 | |
99 | |
149 | 0 |
100 | 0 |
150 | } |
101 | } |
151 | |
102 | |
152 | sub on_clock { |
103 | sub on_unload { |
153 | return 0 unless %dialog; |
104 | while (my ($token, $dialog) = each %dialog) { |
|
|
105 | $dialog->{ob}->contr->send ("ext $token perl_reload"); |
|
|
106 | } |
154 | |
107 | |
155 | while (my ($token, $dialog) = each %dialog) { |
108 | %dialog = (); |
156 | my (undef, $dx, $dy) = $dialog->{ob}->rangevector ($dialog->{npc}); |
|
|
157 | next if (abs $dx) <= 2 && (abs $dy) <= 2; |
|
|
158 | |
|
|
159 | $dialog->{ob}->contr->send ("ext $token out_of_range"); |
|
|
160 | delete $dialog{$token}; |
|
|
161 | } |
|
|
162 | |
109 | |
163 | 0 |
110 | 0 |
164 | } |
111 | } |
165 | |
112 | |
|
|
113 | |