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 | sub parse_message($) { |
|
|
14 | map [split /\n/, $_, 2], |
|
|
15 | grep length, |
|
|
16 | split /^\@match /m, |
|
|
17 | $_[0] |
|
|
18 | } |
|
|
19 | |
|
|
20 | my %dialog; # currently active dialogs |
15 | my %dialog; # currently active dialogs |
21 | |
16 | |
22 | sub dialog_tell { |
17 | my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { |
23 | my ($dialog, $msg) = @_; |
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; |
24 | |
21 | |
25 | my $pl = cf::player::find $dialog->{name}; |
22 | $dialog->{ob}->contr->send ("ext $token out_of_range"); |
26 | |
23 | delete $dialog{$token}; |
27 | for my $match (@{ $dialog->{match} }) { |
|
|
28 | for (split /\|/, $match->[0]) { |
|
|
29 | if ($_ eq "*" || 0 <= index $msg, $_) { |
|
|
30 | my $reply = $match->[1]; |
|
|
31 | |
|
|
32 | # combine lines into paragraphs |
|
|
33 | $reply =~ s/(?<=\S)\n(?=\w)/ /g; |
|
|
34 | $reply =~ s/\n\n/\n/g; |
|
|
35 | |
|
|
36 | my @kw; |
|
|
37 | # now mark up all matching keywords |
|
|
38 | for my $match (@{ $dialog->{match} }) { |
|
|
39 | for (sort { (length $b) <=> (length $a) } split /\|/, $match->[0]) { |
|
|
40 | if ($reply =~ /\b\Q$_\E\b/i) { |
|
|
41 | push @kw, $_; |
|
|
42 | last; |
|
|
43 | } |
|
|
44 | } |
|
|
45 | } |
|
|
46 | |
|
|
47 | $pl->send ("ext $dialog->{token} msg " . join "\x00", $reply, @kw); |
|
|
48 | return; |
|
|
49 | } |
|
|
50 | } |
|
|
51 | } |
24 | } |
52 | |
25 | |
53 | $pl->send ("ext $dialog->{token} msg ..."); |
26 | $_[0]->w->stop unless keys %dialog; |
|
|
27 | }); |
|
|
28 | |
|
|
29 | sub dialog_tell { |
|
|
30 | my ($token, $dialog, $msg) = @_; |
|
|
31 | |
|
|
32 | my $pl = $dialog->{ob}->contr; |
|
|
33 | my ($reply, @kw) = $dialog->tell ($msg); |
|
|
34 | $reply = "..." unless $reply; |
|
|
35 | $pl->send ("ext $token msg " . join "\x00", $reply, @kw); |
54 | } |
36 | } |
55 | |
37 | |
56 | # return "interesting" information about the given tile |
38 | # return "interesting" information about the given tile |
57 | # currently only returns the npc_dialog title when a dialog is possible |
39 | # currently only returns the npc_dialog title when a dialog is possible |
58 | cf::register_extcmd lookat => sub { |
40 | cf::register_extcmd lookat => sub { |
59 | my ($pl, $data) = @_; |
41 | my ($pl, $data) = @_; |
60 | |
42 | |
61 | my ($token, $dx, $dy) = split / /, $data; |
43 | my ($token, $dx, $dy) = split / /, $data; |
|
|
44 | my $near = (abs $dx) <= 2 && (abs $dy) <= 2; |
62 | |
45 | |
63 | my %res; |
46 | my %res; |
64 | |
|
|
65 | my $near = (abs $dx) <= 2 && (abs $dy) <= 2; |
|
|
66 | |
47 | |
67 | if ($pl->cell_visible ($dx, $dy)) { |
48 | if ($pl->cell_visible ($dx, $dy)) { |
68 | for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
49 | for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
69 | $res{npc_dialog} = $ob->name |
50 | $res{npc_dialog} = $ob->name |
70 | if $near && $ob->message =~ /^\@match /; |
51 | if $near && NPC_Dialogue::has_dialogue $ob; |
71 | } |
52 | } |
72 | } |
53 | } |
73 | |
54 | |
74 | $pl->send ("ext $token " . join "\x00", %res); |
55 | $pl->send ("ext $token " . join "\x00", %res); |
75 | }; |
56 | }; |
… | |
… | |
81 | |
62 | |
82 | return unless (abs $dx) <= 2 && (abs $dy) <= 2; |
63 | return unless (abs $dx) <= 2 && (abs $dy) <= 2; |
83 | return unless $pl->cell_visible ($dx, $dy); |
64 | return unless $pl->cell_visible ($dx, $dy); |
84 | |
65 | |
85 | for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
66 | for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
86 | if (my @match = parse_message $npc->get_message) { |
67 | if (NPC_Dialogue::has_dialogue $npc) { |
87 | $dialog{$token} = { |
68 | $dialog{$token} = new NPC_Dialogue ob => $pl->ob, npc => $npc; |
88 | name => $pl->ob->name, |
|
|
89 | token => $token, |
|
|
90 | npc => $npc, |
|
|
91 | match => \@match, |
|
|
92 | }; |
|
|
93 | |
|
|
94 | dialog_tell $dialog{$token}, "hi"; |
69 | dialog_tell $token, $dialog{$token}, "hi"; |
|
|
70 | $timer->start; |
95 | return; |
71 | return; |
96 | } |
72 | } |
97 | } |
73 | } |
98 | |
74 | |
99 | $pl->send ("ext $token error"); |
75 | $pl->send ("ext $token error"); |
… | |
… | |
102 | cf::register_extcmd npc_dialog_tell => sub { |
78 | cf::register_extcmd npc_dialog_tell => sub { |
103 | my ($pl, $data) = @_; |
79 | my ($pl, $data) = @_; |
104 | |
80 | |
105 | my ($token, $msg) = split / /, $data, 2; |
81 | my ($token, $msg) = split / /, $data, 2; |
106 | |
82 | |
107 | dialog_tell $dialog{$token}, $msg |
83 | dialog_tell $token, $dialog{$token}, $msg |
108 | if $dialog{$token}; |
84 | if $dialog{$token}; |
109 | }; |
85 | }; |
110 | |
86 | |
111 | cf::register_extcmd npc_dialog_end => sub { |
87 | cf::register_extcmd npc_dialog_end => sub { |
112 | my ($pl, $token) = @_; |
88 | my ($pl, $token) = @_; |
113 | |
89 | |
114 | delete $dialog{$token}; |
90 | delete $dialog{$token}; |
115 | }; |
91 | }; |
116 | |
92 | |
117 | sub on_clock { |
93 | sub on_logout { |
118 | return 0 unless %dialog; |
94 | my ($pl, $host) = @_; |
119 | |
95 | |
120 | while (my ($token, $dialog) = each %dialog) { |
96 | delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog; |
121 | if (my $pl = cf::player::find $dialog->{name}) { |
|
|
122 | my (undef, $dx, $dy) = $pl->ob->rangevector ($dialog->{npc}); |
|
|
123 | next if (abs $dx) <= 2 && (abs $dy) <= 2; |
|
|
124 | |
|
|
125 | $pl->send ("ext $token out_of_range"); |
|
|
126 | } |
|
|
127 | delete $dialog{$token}; |
|
|
128 | } |
|
|
129 | |
97 | |
130 | 0 |
98 | 0 |
131 | } |
99 | } |
132 | |
100 | |
|
|
101 | |