ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.37
Committed: Thu Jun 15 15:47:36 2006 UTC (17 years, 11 months ago) by root
Branch: MAIN
Changes since 1.36: +1 -0 lines
Log Message:
implemented preliminary magic mapping support

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