ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.34
Committed: Tue Jun 13 14:35:17 2006 UTC (17 years, 11 months ago) by elmex
Branch: MAIN
Changes since 1.33: +29 -0 lines
Log Message:
changed the fireing logic a little bit and made a checkbox for the old
one. and implemented todo item:
- log messages received and commands sent to ~/.crossfire/log.$ip

File Contents

# User Rev Content
1 root 1.1 package CFClient::Protocol;
2    
3     use utf8;
4     use strict;
5    
6 root 1.2 use Crossfire::Protocol::Constants;
7    
8 root 1.20 use CFClient;
9 root 1.1 use CFClient::UI;
10    
11     use base 'Crossfire::Protocol::Base';
12    
13 elmex 1.34 our %open_logs;
14    
15 root 1.1 sub new {
16     my $class = shift;
17    
18     my $self = $class->SUPER::new (@_);
19    
20     $self->{map_widget}->clr_commands;
21    
22 root 1.21 my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub {
23     my ($pom) = @_;
24 root 1.1
25 root 1.21 my @cmd_help;
26 root 1.1
27 root 1.21 for my $head2 ($pom->head1->[-2]->head2) {
28     $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
29     or next;
30 root 1.1
31 root 1.21 my $cmd = $1;
32     my @args = split /\|/, $2;
33     @args = (".*") unless @args;
34    
35     $_ = $_ eq ".*" ? "" : " $_"
36     for @args;
37    
38     my $text = CFClient::pod_to_pango $head2->content;
39    
40     push @cmd_help, ["$cmd$_", $text]
41     for sort { (length $a) <=> (length $b) }
42     @args;
43     }
44 root 1.6
45 root 1.21 \@cmd_help
46     };
47 root 1.1
48 root 1.21 $self->{map_widget}->add_command (@$_)
49     for @$cmd_help;
50 root 1.1
51     $self->{noface} = new_from_file CFClient::Texture
52     CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
53    
54     $self->{open_container} = 0;
55    
56     # "global"
57     $self->{tilecache} = CFClient::db_table "tilecache";
58     $self->{facemap} = CFClient::db_table "facemap";
59    
60     # per server
61     $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
62    
63     $self
64     }
65    
66 elmex 1.34 sub logprint {
67     my ($self, @a) = @_;
68     my $filename = "$Crossfire::VARDIR/log.$self->{host}";
69    
70     my $fh = $open_logs{$filename};
71     unless ($fh) {
72     # FIXME: handle this more gracefully?
73     open $fh, ">>", $filename
74     or die "Couldn't open logfile: log.$self->{host}: $!";
75    
76     $open_logs{$filename} = $fh;
77     }
78    
79     my ($sec, $min, $hour, $mday, $mon, $year) = localtime (time);
80    
81     my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
82     $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
83    
84     print $fh "$ts ", @a, "\n";
85     $fh->flush;
86     }
87    
88    
89    
90 root 1.1 sub stats_update {
91     my ($self, $stats) = @_;
92    
93 elmex 1.4 if (my $exp = $stats->{+CS_STAT_EXP64}) {
94 root 1.1 my $diff = $exp - $self->{prev_exp};
95 elmex 1.5 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5)
96 root 1.1 if exists $self->{prev_exp} && $diff;
97     $self->{prev_exp} = $exp;
98     }
99    
100     ::update_stats_window ($stats);
101     }
102    
103     sub user_send {
104     my ($self, $command) = @_;
105    
106 elmex 1.9 if ($self->{record}) {
107     push @{$self->{record}}, $command;
108     }
109    
110 elmex 1.34 $self->logprint ("send: ", $command);
111 root 1.1 $self->send_command ($command);
112 root 1.2 ::status $command;
113 root 1.1 }
114    
115 elmex 1.9 sub start_record {
116     my ($self) = @_;
117    
118     $self->{record} = [];
119     }
120    
121     sub stop_record {
122     my ($self) = @_;
123     return delete $self->{record};
124     }
125    
126 root 1.1 sub map_scroll {
127     my ($self, $dx, $dy) = @_;
128    
129     $self->{map}->scroll ($dx, $dy);
130     }
131    
132     sub feed_map1a {
133     my ($self, $data) = @_;
134    
135     $self->{map}->map1a_update ($data);
136     $self->{map_widget}->update;
137     }
138    
139     sub flush_map {
140     my ($self) = @_;
141    
142     my $map_info = delete $self->{map_info}
143     or return;
144    
145     my ($hash, $x, $y, $w, $h) = @$map_info;
146    
147     my $data = $self->{map}->get_rect ($x, $y, $w, $h);
148     $self->{mapcache}->put ($hash => Compress::LZF::compress $data);
149     #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
150     }
151    
152     sub map_clear {
153     my ($self) = @_;
154    
155     $self->flush_map;
156     delete $self->{neigh_map};
157    
158     $self->{map}->clear;
159     }
160    
161    
162     sub load_map($$$) {
163     my ($self, $hash, $x, $y) = @_;
164    
165     if (defined (my $data = $self->{mapcache}->get ($hash))) {
166     $data = Compress::LZF::decompress $data;
167     #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
168     for my $id ($self->{map}->set_rect ($x, $y, $data)) {
169     my $data = $self->{tilecache}->get ($id)
170     or next;
171    
172     $self->set_texture ($id => $data);
173     }
174     }
175     }
176    
177     # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
178     # (server resource,s latency, bandwidth), so this hack is warranted.
179     # the right fix is to make real tiled maps with an overview file
180     sub send_mapinfo {
181     my ($self, $data, $cb) = @_;
182    
183     if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
184     my ($wx, $wy) = ($1, $2);
185    
186     if ($data =~ /^spatial ([1-4]+)$/) {
187     my @dx = (0, 0, 1, 0, -1);
188     my @dy = (0, -1, 0, 1, 0);
189     my ($dx, $dy);
190    
191     for (split //, $1) {
192     $dx += $dx[$_];
193     $dy += $dy[$_];
194     }
195    
196     $cb->(spatial => 15,
197     $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
198     $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
199     50, 50,
200     sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
201     );
202    
203     return;
204     }
205     }
206    
207     $self->SUPER::send_mapinfo ($data, $cb);
208     }
209    
210     # this method does a "flood fill" into every tile direction
211     # it assumes that tiles are arranged in a rectangular grid,
212     # i.e. a map is the same as the left of the right map etc.
213     # failure to comply are harmless and result in display errors
214     # at worst.
215     sub flood_fill {
216     my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
217    
218     # the server does not allow map paths > 6
219     return if 7 <= length $path;
220    
221     my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
222    
223     for (
224     [1, 3, 0, -1],
225     [2, 4, 1, 0],
226     [3, 1, 0, 1],
227     [4, 2, -1, 0],
228     ) {
229     my ($tile, $tile2, $dx, $dy) = @$_;
230    
231     next if $block & (1 << $tile);
232     my $block = $block | (1 << $tile2);
233    
234     my $gx = $gx + $dx;
235     my $gy = $gy + $dy;
236    
237     next unless $flags & (1 << ($tile - 1));
238     next if $self->{neigh_grid}{$gx, $gy}++;
239    
240     my $neigh = $self->{neigh_map}{$hash} ||= [];
241     if (my $info = $neigh->[$tile]) {
242     my ($flags, $x, $y, $w, $h, $hash) = @$info;
243    
244     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
245     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
246    
247     } else {
248     $self->send_mapinfo ("spatial $path$tile", sub {
249     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
250    
251     return if $mode ne "spatial";
252    
253     $x += $self->{map}->ox;
254     $y += $self->{map}->oy;
255    
256     $self->load_map ($hash, $x, $y)
257     unless $self->{neigh_map}{$hash}[5]++;#d#
258    
259     $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
260    
261     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
262     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
263     });
264     }
265     }
266     }
267    
268     sub map_change {
269     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
270    
271     $self->flush_map;
272    
273     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
274    
275     my $mapmapw = $self->{mapmap}->{w};
276     my $mapmaph = $self->{mapmap}->{h};
277    
278     $self->{neigh_rect} = [
279     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
280     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
281     ];
282    
283     delete $self->{neigh_grid};
284    
285     $x += $ox;
286     $y += $oy;
287    
288     $self->{map_info} = [$hash, $x, $y, $w, $h];
289    
290     (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
291     $::STATWIDS->{map}->set_text ("Map: " . $map);
292    
293     $self->load_map ($hash, $x, $y);
294     $self->flood_fill (0, 0, 0, "", $hash, $flags);
295     }
296    
297     sub face_find {
298     my ($self, $facenum, $face) = @_;
299    
300     my $hash = "$face->{chksum},$face->{name}";
301    
302     my $id = $self->{facemap}->get ($hash);
303    
304     unless ($id) {
305     # create new id for face
306     # I love transactions
307     for (1..100) {
308     my $txn = $CFClient::DB_ENV->txn_begin;
309 root 1.15 my $status = $self->{facemap}->db_get (id => $id);
310 root 1.1 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
311     $id = ($id || 16) + 1;
312     if ($self->{facemap}->put (id => $id) == 0
313     && $self->{facemap}->put ($hash => $id) == 0) {
314     $txn->txn_commit;
315    
316     goto gotid;
317     }
318     }
319 root 1.15 $txn->txn_abort;
320 root 1.1 }
321    
322     CFClient::fatal "maximum number of transaction retries reached - database problems?";
323     }
324    
325     gotid:
326     $face->{id} = $id;
327     $self->{map}->set_face ($facenum => $id);
328     $self->{faceid}[$facenum] = $id;#d#
329    
330     my $face = $self->{tilecache}->get ($id);
331    
332     if ($face) {
333     #$self->face_prefetch;
334     $face
335     } else {
336     my $tex = $self->{noface};
337     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
338     undef
339     };
340     }
341    
342     sub face_update {
343     my ($self, $facenum, $face) = @_;
344    
345     $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
346    
347     $self->set_texture ($face->{id} => delete $face->{image});
348     }
349    
350     sub set_texture {
351     my ($self, $id, $data) = @_;
352    
353     $self->{texture}[$id] ||= do {
354     my $tex =
355     new_from_image CFClient::Texture
356     $data, minify => 1, mipmap => 1;
357    
358     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
359     $self->{map_widget}->update;
360    
361     $tex
362     };
363     }
364    
365     sub sound_play {
366     my ($self, $x, $y, $soundnum, $type) = @_;
367    
368     $self->{sound_play}->($x, $y, $soundnum, $type);
369     }
370    
371     my $LAST_QUERY; # server is stupid, stupid, stupid
372    
373     sub query {
374     my ($self, $flags, $prompt) = @_;
375    
376     $prompt = $LAST_QUERY unless length $prompt;
377     $LAST_QUERY = $prompt;
378    
379 root 1.22 $self->{query}-> ($self, $flags, $prompt);
380 root 1.1 }
381    
382     sub drawinfo {
383     my ($self, $color, $text) = @_;
384    
385     my @color = (
386     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
387     [1.00, 1.00, 1.00],
388     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
389     [1.00, 0.00, 0.00],
390     [1.00, 0.54, 0.00],
391     [0.11, 0.56, 1.00],
392     [0.93, 0.46, 0.00],
393     [0.18, 0.54, 0.34],
394     [0.56, 0.73, 0.56],
395     [0.80, 0.80, 0.80],
396     [0.55, 0.41, 0.13],
397     [0.99, 0.77, 0.26],
398     [0.74, 0.65, 0.41],
399     );
400    
401 elmex 1.34 $self->logprint ("info: ", $text);
402    
403 root 1.1 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
404    
405 root 1.23 # try to create single paragraphs of multiple lines sent by the server
406 root 1.24 $text =~ s/(?<=\S)\n(?=\w)/ /g;
407 root 1.23
408 root 1.1 $text = CFClient::UI::Label::escape $text;
409     $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
410     $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
411    
412     $self->{logview}->add_paragraph ($color[$color],
413     join "\n", map "$time $_", split /\n/, $text);
414    
415 elmex 1.5 $self->{statusbox}->add ($text,
416 root 1.1 group => $text,
417     fg => $color[$color],
418 root 1.18 timeout => $color >= 2 ? 60 : 10,
419 root 1.1 tooltip_font => $::FONT_FIXED,
420     );
421     }
422    
423     sub drawextinfo {
424     my ($self, $color, $type, $subtype, $message) = @_;
425    
426     $self->drawinfo ($color, $message);
427     }
428    
429     sub spell_add {
430     my ($self, $spell) = @_;
431    
432 root 1.27 # try to create single paragraphs of multiple lines sent by the server
433     $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
434     $spell->{message} =~ s/\n+$//;
435     $spell->{message} ||= "Server did not provide a description for this spell.";
436    
437 root 1.16 $::SETUP_SPELLS->add_spell ($spell);
438 elmex 1.14
439 root 1.1 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
440     $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
441     }
442    
443     sub spell_delete {
444     my ($self, $spell) = @_;
445 root 1.17 $::SETUP_SPELLS->remove_spell ($spell);
446 root 1.1 }
447    
448     sub addme_success {
449     my ($self) = @_;
450    
451 root 1.21 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
452     my ($pom) = @_;
453 root 1.1
454 root 1.21 my %skill_help;
455 root 1.1
456 root 1.21 for my $head2 ($pom->head1->[3]->head2) {
457     $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content;
458     }
459    
460     \%skill_help
461     };
462 root 1.1
463     for my $skill (values %{$self->{skill_info}}) {
464     $self->{map_widget}->add_command ("ready_skill $skill",
465     (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
466 root 1.21 . $skill_help->{$skill});
467 root 1.1 $self->{map_widget}->add_command ("use_skill $skill",
468     (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
469 root 1.21 . $skill_help->{$skill});
470 root 1.1 }
471     }
472    
473     sub eof {
474     my ($self) = @_;
475    
476     $self->{map_widget}->clr_commands;
477    
478     ::stop_game ();
479     }
480    
481     sub image_info {
482     my ($self, $numfaces) = @_;
483    
484     $self->{num_faces} = $numfaces;
485     $self->{face_prefetch} = [1 .. $numfaces];
486     $self->face_prefetch;
487     }
488    
489     sub face_prefetch {
490     my ($self) = @_;
491    
492     return unless $::CFG->{face_prefetch};
493    
494     if ($self->{num_faces}) {
495     return if @{ $self->{send_queue} || [] };
496     my $todo = @{ $self->{face_prefetch} }
497     or return;
498    
499     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
500    
501     $self->send ("requestinfo image_sums $face $face");
502    
503 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
504 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
505 root 1.1 } elsif (!exists $self->{num_faces}) {
506     $self->send ("requestinfo image_info");
507    
508     $self->{num_faces} = 0;
509    
510 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
511 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
512 root 1.1 }
513     }
514    
515     sub update_floorbox {
516     $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
517     return unless $::CONN;
518    
519     $::FLOORBOX->clear;
520    
521     my $row;
522     for (@{ $::CONN->{container}{0} }) {
523 root 1.25 if ($row < 6) {
524 root 1.1 local $_->{face_widget}; # hack to force recreation of widget
525     local $_->{desc_widget}; # hack to force recreation of widget
526     CFClient::Item::update_widgets $_;
527    
528     $::FLOORBOX->add (0, $row, $_->{face_widget});
529     $::FLOORBOX->add (1, $row, $_->{desc_widget});
530    
531     $row++;
532     } else {
533 root 1.25 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
534 root 1.30 text => "More...",
535 root 1.25 on_activate => sub { $::INV_WINDOW->toggle_visibility },
536     );
537 root 1.1 last;
538     }
539     }
540     });
541    
542     $::WANT_REFRESH++;
543     }
544    
545     sub set_opencont {
546     my ($conn, $tag, $name) = @_;
547     $conn->{open_container} = $tag;
548 elmex 1.10
549     $::INV_RIGHT_HB->clear ();
550     $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
551    
552     if ($tag != 0) { # Floor isn't closable, is it?
553     $::INV_RIGHT_HB->add (new CFClient::UI::Button
554     text => "Close container",
555     tooltip => "Close the currently open container (if one is open)",
556     on_activate => sub {
557     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
558     if $tag != 0;
559     #if $CONN->{open_container} != 0;
560     },
561     );
562     }
563    
564 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
565     }
566    
567     sub update_container {
568     my ($tag) = @_;
569    
570     $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
571     if $tag == $::CONN->{open_container};
572     }
573    
574     sub container_add {
575     my ($self, $tag, $items) = @_;
576    
577     #d# print "container_add: container $tag ($self->{player}{tag})\n";
578    
579     if ($tag == 0) {
580     update_floorbox;
581     update_container (0);
582     } elsif ($tag == $self->{player}{tag}) {
583     $::INV->set_items ($self->{container}{$self->{player}{tag}})
584     } else {
585     update_container ($tag);
586     }
587    
588     # $self-<{player}{tag} => player inv
589     #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
590     }
591    
592     sub container_clear {
593     my ($self, $tag) = @_;
594    
595     #d# print "container_clear: container $tag ($self->{player}{tag})\n";
596    
597     if ($tag == 0) {
598     update_floorbox;
599     update_container (0);
600     } elsif ($tag == $self->{player}{tag}) {
601     $::INV->set_items ($self->{container}{$tag})
602 elmex 1.12 } else {
603     update_container ($tag);
604 root 1.1 }
605    
606     # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
607     }
608    
609     sub item_delete {
610     my ($self, @items) = @_;
611    
612     for (@items) {
613     #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
614    
615     if ($_->{container} == 0) {
616     update_floorbox;
617     update_container ($_->{tag});
618     } elsif ($_->{container} == $self->{player}{tag}) {
619     $::INV->set_items ($self->{container}{$self->{player}{tag}})
620     } else {
621 elmex 1.12 update_container ($_->{container});
622 root 1.1 }
623     }
624     }
625    
626     sub item_update {
627     my ($self, $item) = @_;
628    
629     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
630    
631     if ($item->{tag} == $self->{player}{tag}) {
632     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
633     return;
634     }
635    
636     CFClient::Item::update_widgets $item;
637    
638 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
639 root 1.1 set_opencont ($::CONN, 0, "Floor");
640    
641 root 1.2 } elsif ($item->{flags} & F_OPEN) {
642 root 1.1 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
643     } else {
644     if ($item->{container} == 0) {
645     update_floorbox;
646     update_container (0);
647     } elsif ($item->{container} == $self->{player}{tag}) {
648     $::INV->set_items ($self->{container}{$item->{container}})
649     }
650     }
651     }
652    
653 elmex 1.3 sub player_update {
654     my ($self, $player) = @_;
655 root 1.26
656 elmex 1.3 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
657 root 1.28 }
658    
659 root 1.32 sub update_server_info {
660     my ($self) = @_;
661    
662     my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
663    
664     $::SERVER_INFO->set_markup (
665     "server <tt>$self->{host}:$self->{port}</tt>\n"
666     . "protocol version <tt>$self->{version}</tt>\n"
667     . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
668     . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
669     . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
670     . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
671     . "map size $self->{mapw}×$self->{maph}\n"
672     );
673     }
674    
675 root 1.28 sub logged_in {
676     my ($self) = @_;
677    
678 root 1.32 $self->send_ext_req (cfplus_support => "1", sub {
679 root 1.28 $self->{cfplus_ext} = $_[0];
680 root 1.32 $self->update_server_info;
681     });
682 root 1.31
683 root 1.32 $self->update_server_info;
684 root 1.13
685     $self->send_command ("output-sync $::CFG->{output_sync}");
686     $self->send_command ("output-count $::CFG->{output_count}");
687 root 1.29 $self->send_command ("pickup $::CFG->{pickup}");
688 elmex 1.3 }
689    
690 root 1.32 sub lookat {
691     my ($self, $x, $y) = @_;
692    
693     if ($self->{cfplus_ext}) {
694     $self->send_ext_req (lookat => "$x $y", sub {
695     my %res = split /\x00/, $_[0];
696    
697     if (exists $res{npc_dialog}) {
698     # start npc chat dialog
699     $self->{npc_dialog} = new CFClient::NPCDialog::
700     dx => $x,
701     dy => $y,
702     title => "$res{npc_dialog} (NPC)",
703     conn => $self,
704     ;
705     }
706     });
707     }
708    
709     $self->send ("lookat $x $y");
710     }
711    
712     sub destroy {
713     my ($self) = @_;
714    
715     $self->{npc_dialog}->destroy
716     if $self->{npc_dialog};
717    
718     $self->SUPER::destroy;
719     }
720    
721     package CFClient::NPCDialog;
722    
723     our @ISA = 'CFClient::UI::FancyFrame';
724    
725     sub new {
726     my $class = shift;
727    
728     my $self = $class->SUPER::new (
729     x => 'center',
730     y => 'center',
731     name => "npc_dialog",
732     force_w => $::WIDTH * 0.7,
733     force_h => $::HEIGHT * 0.7,
734     title => "NPC Dialog",
735     kw => { hi => 0, yes => 0, no => 0 },
736     @_,
737     );
738    
739     Scalar::Util::weaken (my $this = $self);
740    
741     # better use a pane...
742     $self->add (my $hbox = new CFClient::UI::HBox);
743     $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1);
744    
745     $hbox->add (my $vbox = new CFClient::UI::VBox);
746    
747     $vbox->add (new CFClient::UI::Label text => "Message Entry:");
748     $vbox->add ($self->{entry} = new CFClient::UI::Entry
749     tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n"
750     . "Sometimes you have to tell an NPC something you cannot find out during "
751     . "a normal conversation (such as a password). In those cases you have to use "
752     . "this text entry. You can also enter responses manually instead of using the response "
753     . "buttons below.",
754     on_activate => sub {
755     my ($entry, $text) = @_;
756    
757     return unless $text =~ /\S/;
758    
759     $entry->set_text ("");
760     $this->send ($text);
761     },
762     );
763    
764     $vbox->add ($self->{options} = new CFClient::UI::VBox);
765    
766     $self->{close_button} = new CFClient::UI::Button
767     text => "Bye (close)",
768     tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
769     on_activate => sub { $this->destroy },
770     ;
771    
772     $self->update_options;
773    
774     $self->{token} = $self->{conn}->ext_token;
775     $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
776     $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
777    
778     $self->{entry}->focus_in;
779    
780     $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
781    
782     $self->show;
783     $self
784     };
785    
786     sub update_options {
787     my ($self) = @_;
788    
789     Scalar::Util::weaken $self;
790    
791     $self->{options}->clear;
792     $self->{options}->add ($self->{close_button});
793    
794     for my $kw (sort keys %{ $self->{kw} }) {
795     $self->{options}->add (new CFClient::UI::Button
796     text => $kw,
797     on_activate => sub {
798     $self->send ($kw);
799     },
800     );
801     }
802     }
803    
804     sub feed {
805     my ($self, $data) = @_;
806    
807     my ($type, $msg) = split / /, $data, 2;
808    
809     if ($type eq "msg") {
810     my ($msg, @kw) = split /\x00/, $msg;
811     $self->{kw}{$_} = 1 for @kw;
812    
813     $msg = CFClient::UI::Label::escape $msg;
814     my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
815     $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh.
816    
817     $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg");
818     $self->update_options;
819     } else {
820     $self->destroy;
821     }
822    
823     1
824     }
825    
826     sub send {
827     my ($self, $msg) = @_;
828    
829     $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
830     $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
831     }
832    
833     sub destroy {
834     my ($self) = @_;
835    
836     #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1
837    
838     delete $self->{conn}{npc_dialog};
839     $self->{conn}->disconnect_ext ($self->{token});
840    
841     $self->SUPER::destroy;
842     }
843    
844 root 1.1 1;