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