ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.44
Committed: Wed Jun 28 10:37:19 2006 UTC (17 years, 10 months ago) by elmex
Branch: MAIN
Changes since 1.43: +1 -0 lines
Log Message:
fixed player weight update problems.
fixed Arm stat display (still displayed, but with the resist-phys value)

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