1 |
#! perl |
2 |
|
3 |
# additional support for cfplus client |
4 |
|
5 |
use NPC_Dialogue; |
6 |
|
7 |
cf::register_extcmd cfplus_support => sub { |
8 |
my ($pl, $data) = @_; |
9 |
|
10 |
my ($token, $client_version) = split / /, $data, 2; |
11 |
|
12 |
$pl->send ("ext $token 1"); |
13 |
}; |
14 |
|
15 |
my %dialog; # currently active dialogs |
16 |
|
17 |
my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { |
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; |
21 |
|
22 |
$dialog->{ob}->contr->send ("ext $token out_of_range"); |
23 |
delete $dialog{$token}; |
24 |
} |
25 |
|
26 |
$_[0]->w->stop unless keys %dialog; |
27 |
}); |
28 |
|
29 |
sub dialog_tell { |
30 |
my ($token, $dialog, $msg) = @_; |
31 |
|
32 |
utf8::decode $msg; |
33 |
my $pl = $dialog->{ob}->contr; |
34 |
my ($reply, @kw) = $dialog->tell ($msg); |
35 |
$reply = "..." unless $reply; |
36 |
utf8::encode $_ for ($reply, @kw); |
37 |
$pl->send ("ext $token msg " . join "\x00", $reply, @kw); |
38 |
} |
39 |
|
40 |
# return "interesting" information about the given tile |
41 |
# currently only returns the npc_dialog title when a dialog is possible |
42 |
cf::register_extcmd lookat => sub { |
43 |
my ($pl, $data) = @_; |
44 |
|
45 |
my ($token, $dx, $dy) = split / /, $data; |
46 |
my $near = (abs $dx) <= 2 && (abs $dy) <= 2; |
47 |
|
48 |
my %res; |
49 |
|
50 |
if ($pl->cell_visible ($dx, $dy)) { |
51 |
for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
52 |
$res{npc_dialog} = $ob->name |
53 |
if $near && NPC_Dialogue::has_dialogue $ob; |
54 |
} |
55 |
} |
56 |
|
57 |
$pl->send ("ext $token " . join "\x00", %res); |
58 |
}; |
59 |
|
60 |
cf::register_extcmd npc_dialog_begin => sub { |
61 |
my ($pl, $data) = @_; |
62 |
|
63 |
my ($token, $dx, $dy) = split / /, $data; |
64 |
|
65 |
return unless (abs $dx) <= 2 && (abs $dy) <= 2; |
66 |
return unless $pl->cell_visible ($dx, $dy); |
67 |
|
68 |
for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { |
69 |
if (NPC_Dialogue::has_dialogue $npc) { |
70 |
$dialog{$token} = new NPC_Dialogue ob => $pl->ob, npc => $npc; |
71 |
dialog_tell $token, $dialog{$token}, "hi"; |
72 |
$timer->start; |
73 |
return; |
74 |
} |
75 |
} |
76 |
|
77 |
$pl->send ("ext $token error"); |
78 |
}; |
79 |
|
80 |
cf::register_extcmd npc_dialog_tell => sub { |
81 |
my ($pl, $data) = @_; |
82 |
|
83 |
my ($token, $msg) = split / /, $data, 2; |
84 |
|
85 |
dialog_tell $token, $dialog{$token}, $msg |
86 |
if $dialog{$token}; |
87 |
}; |
88 |
|
89 |
cf::register_extcmd npc_dialog_end => sub { |
90 |
my ($pl, $token) = @_; |
91 |
|
92 |
delete $dialog{$token}; |
93 |
}; |
94 |
|
95 |
sub on_logout { |
96 |
my ($pl, $host) = @_; |
97 |
|
98 |
delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog; |
99 |
|
100 |
0 |
101 |
} |
102 |
|
103 |
sub on_unload { |
104 |
while (my ($token, $dialog) = each %dialog) { |
105 |
$dialog->{ob}->contr->send ("ext $token perl_reload"); |
106 |
} |
107 |
|
108 |
%dialog = (); |
109 |
|
110 |
0 |
111 |
} |
112 |
|
113 |
|