ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.53
Committed: Mon Jul 3 22:32:52 2006 UTC (17 years, 11 months ago) by root
Branch: MAIN
Changes since 1.52: +11 -12 lines
Log Message:
more stats change code

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