ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.64
Committed: Sun Jul 23 08:58:44 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.63: +18 -3 lines
Log Message:
preliminary inventory sorting

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