ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.54
Committed: Mon Jul 3 22:58:37 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.53: +10 -9 lines
Log Message:
*** empty log message ***

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