ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/cfplus.ext
(Generate patch)

Comparing deliantra/server/ext/cfplus.ext (file contents):
Revision 1.1 by root, Fri Dec 15 19:29:18 2006 UTC vs.
Revision 1.7 by elmex, Tue Jun 19 18:14:36 2007 UTC

23cf::register_extcmd cfplus_support => sub { 23cf::register_extcmd cfplus_support => sub {
24 my ($pl, $msg) = @_; 24 my ($pl, $msg) = @_;
25 25
26 # $msg->{version} 26 # $msg->{version}
27 27
28 (
28 (version => 2) 29 version => 2,
29};
30
31my %dialog; # currently active dialogs
32
33my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub {
34 while (my ($id, $dialog) = each %dialog) {
35 my (undef, $dx, $dy) = $dialog->{ob}->rangevector ($dialog->{npc});
36 next if (abs $dx) <= 2 && (abs $dy) <= 2;
37
38 $dialog->{ob}->contr->ext_reply ($id => msgtype => "error", msg => "out of range");
39 delete $dialog{$id};
40 } 30 )
41
42 $_[0]->w->stop unless keys %dialog;
43}); 31};
44 32
45sub dialog_tell { 33sub dialog_tell {
46 my ($id, $dialog, $msg) = @_; 34 my ($id, $dialog, $msg) = @_;
47 35
48 my $pl = $dialog->{ob}->contr; 36 my $pl = $dialog->{pl};
49 my ($reply, @kw) = $dialog->tell ($msg); 37 my ($reply, @kw) = $dialog->tell ($msg);
38
50 $reply = "..." unless $reply; 39 $reply = "..." unless defined $reply;
40 return if $reply eq ""; # NPC doesn't want to say, or wants to say something later
51 41
52 $pl->ext_reply ($id => msgtype => "reply", msg => $reply, add_topics => \@kw); 42 $pl->ext_reply ($id => msgtype => "reply", msg => $reply, add_topics => \@kw);
53} 43}
54 44
55=item ... = extcmd lookat { dx => $dx, dy => $dy } 45=item ... = extcmd lookat { dx => $dx, dy => $dy }
65=cut 55=cut
66 56
67cf::register_extcmd lookat => sub { 57cf::register_extcmd lookat => sub {
68 my ($pl, $msg) = @_; 58 my ($pl, $msg) = @_;
69 my ($dx, $dy) = @$msg{qw(dx dy)}; 59 my ($dx, $dy) = @$msg{qw(dx dy)};
60
61 return unless $pl->ob && $pl->ob->map;
70 62
71 my $near = (abs $dx) <= 2 && (abs $dy) <= 2; 63 my $near = (abs $dx) <= 2 && (abs $dy) <= 2;
72 64
73 my %res; 65 my %res;
74 66
75 if ($pl->cell_visible ($dx, $dy)) { 67 if ($pl->cell_visible ($dx, $dy)) {
76 for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { 68 for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) {
77 $res{npc_dialog} = $ob->name 69 $res{npc_dialog} = $ob->name
78 if $near && NPC_Dialogue::has_dialogue $ob; 70 if $near && NPC_Dialogue::has_dialogue $ob && !$pl->{npc_dialog};
79 } 71 }
80 } 72 }
81 73
82 %res 74 %res
83}; 75};
87Tries to start a dialogue with the mapspace specified by $dx and $dy (see 79Tries to start a dialogue with the mapspace specified by $dx and $dy (see
88C<extcmd lookat>). The $msgid will be used as a handle for all future 80C<extcmd lookat>). The $msgid will be used as a handle for all future
89messages related to this dialog interaction. 81messages related to this dialog interaction.
90 82
91It either replies with an error reply or starts a dialog by telling 83It either replies with an error reply or starts a dialog by telling
92the npc "hi" and returning a reply strcuture as with C<extcmd 84the npc "hi" and returning a reply structure as with C<extcmd
93npc_dialog_tell>. 85npc_dialog_tell>.
94 86
95=cut 87=cut
96 88
97cf::register_extcmd npc_dialog_begin => sub { 89cf::register_extcmd npc_dialog_begin => sub {
98 my ($pl, $msg) = @_; 90 my ($pl, $msg) = @_;
99 my ($id, $dx, $dy) = @$msg{qw(msgid dx dy)}; 91 my ($id, $dx, $dy) = @$msg{qw(msgid dx dy)};
100 92
93 return unless $pl->ob && $pl->ob->map;
101 return unless (abs $dx) <= 2 && (abs $dy) <= 2; 94 return unless (abs $dx) <= 2 && (abs $dy) <= 2;
102 return unless $pl->cell_visible ($dx, $dy); 95 return unless $pl->cell_visible ($dx, $dy);
96 return if $pl->{npc_dialog}; # only one dialog at a time
103 97
104 for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { 98 for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) {
105 if (NPC_Dialogue::has_dialogue $npc) { 99 if (NPC_Dialogue::has_dialogue $npc) {
100 $pl->attach ("npc_dialog_active");
106 $dialog{$id} = new NPC_Dialogue ob => $pl->ob, npc => $npc; 101 $pl->{npc_dialog} = new NPC_Dialogue pl => $pl, npc => $npc, id => $id;
107 dialog_tell $id, $dialog{$id}, "hi"; 102 dialog_tell $id, $pl->{npc_dialog}, "hi";
108 $timer->start;
109 return; 103 return;
110 } 104 }
111 } 105 }
112 106
113 (msgtype => "error", msg => "nothing to talk to found") 107 (msgtype => "error", msg => "nothing to talk to found")
126=cut 120=cut
127 121
128cf::register_extcmd npc_dialog_tell => sub { 122cf::register_extcmd npc_dialog_tell => sub {
129 my ($pl, $msg) = @_; 123 my ($pl, $msg) = @_;
130 124
125 if (my $dialog = $pl->{npc_dialog}) {
131 dialog_tell $msg->{msgid}, $dialog{$msg->{msgid}}, $msg->{msg} 126 dialog_tell $msg->{msgid}, $dialog, $msg->{msg};
132 if $dialog{$msg->{msgid}}; 127 }
133 128
134 () 129 ()
135}; 130};
136 131
137=item extcmd npc_dialog_end { msgid => $id } 132=item extcmd npc_dialog_end { msgid => $id }
141=cut 136=cut
142 137
143cf::register_extcmd npc_dialog_end => sub { 138cf::register_extcmd npc_dialog_end => sub {
144 my ($pl, $msg) = @_; 139 my ($pl, $msg) = @_;
145 140
146 delete $dialog{$msg->{msgid}}; 141 if (my $dialog = delete $pl->{npc_dialog}) {
142 $pl->detach ("ncpa_dialog_active");
143 }
147 144
148 () 145 ()
149}; 146};
150
151cf::attach_to_players
152 on_logout => sub {
153 my ($pl) = @_;
154
155 delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog;
156 },
157;
158 147
159=item ... = extcmd editor_support 148=item ... = extcmd editor_support
160 149
161Returns the value required by clients that have an editor to download and 150Returns the value required by clients that have an editor to download and
162upload maps from/to the server. 151upload maps from/to the server.
189 178
190 map +($_ => $cf::CFG{"editor_$_"}), qw(servertype gameserver testserver cvs_root lib_root builder_ui) 179 map +($_ => $cf::CFG{"editor_$_"}), qw(servertype gameserver testserver cvs_root lib_root builder_ui)
191}; 180};
192 181
193sub unload { 182sub unload {
194 while (my ($id, $dialog) = each %dialog) { 183 for my $pl (cf::player::list) {
184 if (my $dialog = delete $pl->{npc_dialog}) {
185 $pl->detach ("npc_dialog_active");
195 $dialog->{ob}->contr->ext_reply ($id => msgtype => "error", msg => "npc dialogue module was reloaded"); 186 $pl->ext_reply ($dialog->{id} => msgtype => "error", msg => "npc dialogue module was reloaded");
187 }
196 } 188 }
197
198 %dialog = ();
199} 189}
200 190
191cf::player::attachment npc_dialog_active =>
192 on_logout => sub {
193 my ($pl) = @_;
194
195 delete $pl->{npc_dialog};
196 $pl->detach ("npc_dialog_active");
197 },
198 on_move => sub {
199 my ($pl, $dir) = @_;
200
201 if (my $dialog = $pl->{npc_dialog}) {
202 warn "on_move<@_>\n";#d#
203 warn $pl->ob;
204 warn $dialog;
205 warn $dialog->{npc};
206 warn $dialog->{npc}->is_valid;
207
208 my (undef, $dx, $dy) = $pl->ob->rangevector ($dialog->{npc});
209
210 return if (abs $dx) <= 2 && (abs $dy) <= 2;
211
212 $pl->ext_reply ($dialog->{id} => msgtype => "error", msg => "out of range");
213 }
214
215 delete $pl->{npc_dialog};
216 $pl->detach ("npc_dialog_active");
217 },
218;
219
220cf::player->attach (
221 on_login => sub {
222 my ($pl) = @_;
223
224 delete $pl->{npc_dialog};
225 },
226);
227
201=back 228=back
202 229
203=cut 230=cut
204 231

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines