ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/cfplus.ext
Revision: 1.7
Committed: Wed Jul 19 23:00:50 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.6: +2 -0 lines
Log Message:
properly send and expect utf8 for npc dialogs

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     # additional support for cfplus client
4    
5 root 1.4 use NPC_Dialogue;
6    
7 root 1.1 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 root 1.3 my %dialog; # currently active dialogs
16    
17 root 1.5 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 root 1.3 sub dialog_tell {
30     my ($token, $dialog, $msg) = @_;
31    
32 root 1.7 utf8::decode $msg;
33 root 1.3 my $pl = $dialog->{ob}->contr;
34     my ($reply, @kw) = $dialog->tell ($msg);
35     $reply = "..." unless $reply;
36 root 1.7 utf8::encode $_ for ($reply, @kw);
37 root 1.3 $pl->send ("ext $token msg " . join "\x00", $reply, @kw);
38 root 1.1 }
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 root 1.3 my $near = (abs $dx) <= 2 && (abs $dy) <= 2;
47 root 1.1
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 root 1.3 if $near && NPC_Dialogue::has_dialogue $ob;
54 root 1.1 }
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 root 1.2 return unless (abs $dx) <= 2 && (abs $dy) <= 2;
66 root 1.1 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 root 1.3 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 root 1.5 $timer->start;
73 root 1.1 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 root 1.3 dialog_tell $token, $dialog{$token}, $msg
86 root 1.1 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 root 1.3 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 root 1.6 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 root 1.2