ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Protocol.pm (file contents):
Revision 1.5 by elmex, Sat May 27 10:32:35 2006 UTC vs.
Revision 1.62 by elmex, Sat Jul 22 12:18:02 2006 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Crossfire::Protocol::Constants; 6use Crossfire::Protocol::Constants;
7 7
8use CFClient;
8use CFClient::UI; 9use CFClient::UI;
9 10
10use base 'Crossfire::Protocol::Base'; 11use base 'Crossfire::Protocol::Base';
11 12
12sub new { 13sub new {
14 15
15 my $self = $class->SUPER::new (@_); 16 my $self = $class->SUPER::new (@_);
16 17
17 $self->{map_widget}->clr_commands; 18 $self->{map_widget}->clr_commands;
18 19
19 my $parser = new Pod::POM; 20 my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub {
20 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod"); 21 my ($pom) = @_;
21 22
23 my @cmd_help;
24
22 for my $head2 ($pod->head1->[-2]->head2) { 25 for my $head2 ($pom->head1->[-2]->head2) {
23 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 26 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
24 or next; 27 or next;
25 28
26 my $cmd = $1; 29 my $cmd = $1;
27 my @args = split /\|/, $2; 30 my @args = split /\|/, $2;
28 @args = (".*") unless @args; 31 @args = (".*") unless @args;
29 32
33 $_ = $_ eq ".*" ? "" : " $_"
34 for @args;
35
30 my $text = CFClient::pod_to_pango $head2->content; 36 my $text = CFClient::pod_to_pango $head2->content;
31 37
32 for my $arg (@args) { 38 push @cmd_help, ["$cmd$_", $text]
33 $arg = $arg eq ".*" ? "" : " $arg"; 39 for sort { (length $a) <=> (length $b) }
34 40 @args;
35 $self->{map_widget}->add_command ("$cmd$arg", $text);
36 } 41 }
42
43 \@cmd_help
37 } 44 };
45
46 $self->{map_widget}->add_command (@$_)
47 for @$cmd_help;
38 48
39 $self->{noface} = new_from_file CFClient::Texture 49 $self->{noface} = new_from_file CFClient::Texture
40 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; 50 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
41 51
42 $self->{open_container} = 0; 52 $self->{open_container} = 0;
49 $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; 59 $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
50 60
51 $self 61 $self
52} 62}
53 63
64sub logprint {
65 my ($self, @a) = @_;
66
67 $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
73 $fh->autoflush (1);
74
75 $fh;
76 };
77
78 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
79
80 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
81 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
82
83 print {$self->{log_fh}} "$ts ", @a, "\n";
84}
85
86sub _stat_numdiff {
87 my ($self, $name, $old, $new) = @_;
88
89 my $diff = $new - $old;
90
91 $diff = 0.01 * int $diff * 100;
92
93 0.1 >= abs $diff ? ()
94 : $diff < 0 ? "$name$diff" : "$name+$diff"
95}
96
97sub _stat_skillmaskdiff {
98 my ($self, $name, $old, $new) = @_;
99
100 my $diff = $old ^ $new
101 or return;
102
103 my @diff = map
104 {
105 $diff & $_
106 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
107 : ()
108 }
109 sort { $a <=> $b } keys %{$self->{spell_paths}};
110
111 join "", @diff
112}
113
114# all stats that are chacked against changes
115my @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
54sub stats_update { 155sub stats_update {
55 my ($self, $stats) = @_; 156 my ($self, $stats) = @_;
56 157
57 if (my $exp = $stats->{+CS_STAT_EXP64}) { 158 my $prev = $self->{prev_stats} || { };
58 my $diff = $exp - $self->{prev_exp}; 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;
59 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5) 170 $self->{statusbox}->add ($msg, group => "experience $msg", fg => [0.5, 1, 0.5, 0.8], timeout => 5);
60 if exists $self->{prev_exp} && $diff;
61 $self->{prev_exp} = $exp;
62 } 171 }
63 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
64 ::update_stats_window ($stats); 180 $self->update_stats_window ($stats, $prev);
181
182 $self->{prev_stats} = { %$stats };
183}
184
185my %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
206sub update_stats_window {
207 my ($self, $stats, $prev) = @_;
208
209 # I love text protocols...
210
211 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 $::STATWIDS->{m_weight}->set_text (sprintf "Max weight: %.1fkg", $stats->{+CS_STAT_WEIGHT_LIM} / 1000);
248
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 my ($x, $y) = (1, 0);
266 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 ) {
272 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 }
282 }
283
284 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 }
65} 288}
66 289
67sub user_send { 290sub user_send {
68 my ($self, $command) = @_; 291 my ($self, $command) = @_;
69 292
293 if ($self->{record}) {
294 push @{$self->{record}}, $command;
295 }
296
297 $self->logprint ("send: ", $command);
70 $self->send_command ($command); 298 $self->send_command ($command);
71 ::status $command; 299 ::status ($command);
300}
301
302sub start_record {
303 my ($self) = @_;
304
305 $self->{record} = [];
306}
307
308sub stop_record {
309 my ($self) = @_;
310 return delete $self->{record};
72} 311}
73 312
74sub map_scroll { 313sub map_scroll {
75 my ($self, $dx, $dy) = @_; 314 my ($self, $dx, $dy) = @_;
76 315
80sub feed_map1a { 319sub feed_map1a {
81 my ($self, $data) = @_; 320 my ($self, $data) = @_;
82 321
83 $self->{map}->map1a_update ($data); 322 $self->{map}->map1a_update ($data);
84 $self->{map_widget}->update; 323 $self->{map_widget}->update;
324}
325
326sub magicmap {
327 my ($self, $w, $h, $x, $y, $data) = @_;
328
329 $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data);
85} 330}
86 331
87sub flush_map { 332sub flush_map {
88 my ($self) = @_; 333 my ($self) = @_;
89 334
102 347
103 $self->flush_map; 348 $self->flush_map;
104 delete $self->{neigh_map}; 349 delete $self->{neigh_map};
105 350
106 $self->{map}->clear; 351 $self->{map}->clear;
352 delete $self->{map_widget}{magicmap};
107} 353}
108 354
109 355
110sub load_map($$$) { 356sub load_map($$$) {
111 my ($self, $hash, $x, $y) = @_; 357 my ($self, $hash, $x, $y) = @_;
252 unless ($id) { 498 unless ($id) {
253 # create new id for face 499 # create new id for face
254 # I love transactions 500 # I love transactions
255 for (1..100) { 501 for (1..100) {
256 my $txn = $CFClient::DB_ENV->txn_begin; 502 my $txn = $CFClient::DB_ENV->txn_begin;
257 my $status = $self->{facemap}->db_get (id => $id, BerkeleyDB::DB_RMW); 503 my $status = $self->{facemap}->db_get (id => $id);
258 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { 504 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
259 $id = ($id || 16) + 1; 505 $id = ($id || 64) + 1;
260 if ($self->{facemap}->put (id => $id) == 0 506 if ($self->{facemap}->put (id => $id) == 0
261 && $self->{facemap}->put ($hash => $id) == 0) { 507 && $self->{facemap}->put ($hash => $id) == 0) {
262 $txn->txn_commit; 508 $txn->txn_commit;
263 509
264 goto gotid; 510 goto gotid;
265 } 511 }
266 } 512 }
267 $txn->abort; 513 $txn->txn_abort;
268 } 514 }
269 515
270 CFClient::fatal "maximum number of transaction retries reached - database problems?"; 516 CFClient::fatal "maximum number of transaction retries reached - database problems?";
271 } 517 }
272 518
286 undef 532 undef
287 }; 533 };
288} 534}
289 535
290sub face_update { 536sub face_update {
291 my ($self, $facenum, $face) = @_; 537 my ($self, $facenum, $face, $changed) = @_;
292 538
293 $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes 539 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed;
294 540
295 $self->set_texture ($face->{id} => delete $face->{image}); 541 $self->set_texture ($face->{id} => delete $face->{image});
296} 542}
297 543
298sub set_texture { 544sub set_texture {
322 my ($self, $flags, $prompt) = @_; 568 my ($self, $flags, $prompt) = @_;
323 569
324 $prompt = $LAST_QUERY unless length $prompt; 570 $prompt = $LAST_QUERY unless length $prompt;
325 $LAST_QUERY = $prompt; 571 $LAST_QUERY = $prompt;
326 572
327 my $dialog = new CFClient::UI::FancyFrame 573 $self->{query}-> ($self, $flags, $prompt);
328 title => "Query",
329 child => my $vbox = new CFClient::UI::VBox;
330
331 $vbox->add (new CFClient::UI::Label
332 max_w => $::WIDTH * 0.4,
333 ellipsise => 0,
334 text => $prompt);
335
336 if ($flags & CS_QUERY_YESNO) {
337 $vbox->add (my $hbox = new CFClient::UI::HBox);
338 $hbox->add (new CFClient::UI::Button
339 text => "No",
340 connect_activate => sub {
341 $self->send ("reply n");
342 $dialog->destroy;
343 $self->{map_widget}->focus_in;
344 }
345 );
346 $hbox->add (new CFClient::UI::Button
347 text => "Yes",
348 connect_activate => sub {
349 $self->send ("reply y");
350 $dialog->destroy;
351 },
352 );
353
354 $dialog->focus_in;
355
356 } elsif ($flags & CS_QUERY_SINGLECHAR) {
357 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
358 $vbox->add (my $entry = new CFClient::UI::Entry
359 connect_changed => sub {
360 $self->send ("reply $_[1]");
361 $dialog->destroy;
362 },
363 );
364
365 $entry->focus_in;
366
367 } else {
368 $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
369
370 $vbox->add (my $entry = new CFClient::UI::Entry
371 $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
372 connect_activate => sub {
373 $self->send ("reply $_[1]");
374 $dialog->destroy;
375 },
376 );
377
378 $entry->focus_in;
379 }
380
381 $dialog->show_centered;
382} 574}
383 575
384sub drawinfo { 576sub drawinfo {
385 my ($self, $color, $text) = @_; 577 my ($self, $color, $text) = @_;
386 578
393 [0.11, 0.56, 1.00], 585 [0.11, 0.56, 1.00],
394 [0.93, 0.46, 0.00], 586 [0.93, 0.46, 0.00],
395 [0.18, 0.54, 0.34], 587 [0.18, 0.54, 0.34],
396 [0.56, 0.73, 0.56], 588 [0.56, 0.73, 0.56],
397 [0.80, 0.80, 0.80], 589 [0.80, 0.80, 0.80],
398 [0.55, 0.41, 0.13], 590 [0.75, 0.61, 0.20],
399 [0.99, 0.77, 0.26], 591 [0.99, 0.77, 0.26],
400 [0.74, 0.65, 0.41], 592 [0.74, 0.65, 0.41],
401 ); 593 );
402 594
595 $self->logprint ("info: ", $text);
596
403 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 597 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
598
599 # try to create single paragraphs of multiple lines sent by the server
600 $text =~ s/(?<=\S)\n(?=\w)/ /g;
404 601
405 $text = CFClient::UI::Label::escape $text; 602 $text = CFClient::UI::Label::escape $text;
406 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 603 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
407 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 604 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
408 605
409 $self->{logview}->add_paragraph ($color[$color], 606 $self->{logview}->add_paragraph ($color[$color], $_)
410 join "\n", map "$time $_", split /\n/, $text); 607 for map "$time $_", split /\n/, $text;
608 $self->{logview}->scroll_to_bottom;
411 609
412 $self->{statusbox}->add ($text, 610 $self->{statusbox}->add ($text,
413 group => $text, 611 group => $text,
414 fg => $color[$color], 612 fg => $color[$color],
415 timeout => 10, 613 timeout => $color >= 2 ? 60 : 10,
416 tooltip_font => $::FONT_FIXED, 614 tooltip_font => $::FONT_FIXED,
417 ); 615 );
418} 616}
419 617
420sub drawextinfo { 618sub drawextinfo {
424} 622}
425 623
426sub spell_add { 624sub spell_add {
427 my ($self, $spell) = @_; 625 my ($self, $spell) = @_;
428 626
429 # TODO 627 # try to create single paragraphs of multiple lines sent by the server
430 # create a widget dynamically, using spell face (CF::Protocol downloads them) 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 $::SPELL_PAGE->add_spell ($spell);
633
431 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 634 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
432 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 635 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
433} 636}
434 637
435sub spell_delete { 638sub spell_delete {
436 my ($self, $spell) = @_; 639 my ($self, $spell) = @_;
640
641 $::SPELL_PAGE->remove_spell ($spell);
437} 642}
438 643
439sub addme_success { 644sub addme_success {
440 my ($self) = @_; 645 my ($self) = @_;
441 646
442 $self->send ("command output-sync $::CFG->{output_sync}"); 647 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
443 $self->send ("command output-count $::CFG->{output_count}"); 648 my ($pom) = @_;
444 649
445 my $parser = new Pod::POM;
446 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod");
447
448 my %skill_tooltip; 650 my %skill_help;
449 651
450 for my $head2 ($pod->head1->[-2]->head2) { 652 for my $head2 ($pom->head1->[3]->head2) {
451 $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content; 653 $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content;
654 }
655
656 \%skill_help
452 } 657 };
453 658
454 for my $skill (values %{$self->{skill_info}}) { 659 for my $skill (values %{$self->{skill_info}}) {
455 $self->{map_widget}->add_command ("ready_skill $skill", 660 $self->{map_widget}->add_command ("ready_skill $skill",
456 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") 661 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
457 . $skill_tooltip{$skill}); 662 . $skill_help->{$skill});
458 $self->{map_widget}->add_command ("use_skill $skill", 663 $self->{map_widget}->add_command ("use_skill $skill",
459 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") 664 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
460 . $skill_tooltip{$skill}); 665 . $skill_help->{$skill});
461 } 666 }
462} 667}
463 668
464sub eof { 669sub eof {
465 my ($self) = @_; 670 my ($self) = @_;
490 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); 695 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
491 696
492 $self->send ("requestinfo image_sums $face $face"); 697 $self->send ("requestinfo image_sums $face $face");
493 698
494 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", 699 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
495 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); 700 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
496 } elsif (!exists $self->{num_faces}) { 701 } elsif (!exists $self->{num_faces}) {
497 $self->send ("requestinfo image_info"); 702 $self->send ("requestinfo image_info");
498 703
499 $self->{num_faces} = 0; 704 $self->{num_faces} = 0;
500 705
501 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", 706 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
502 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); 707 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
503 } 708 }
504} 709}
505 710
506sub update_floorbox { 711sub update_floorbox {
507 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 712 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
508 return unless $::CONN; 713 return unless $::CONN;
509 714
510 $::FLOORBOX->clear; 715 $::FLOORBOX->clear;
511 716
512 my $row; 717 my $row;
513 for (@{ $::CONN->{container}{0} }) { 718 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
514 if ($row < 7) { 719 if ($row < 6) {
515 local $_->{face_widget}; # hack to force recreation of widget 720 local $_->{face_widget}; # hack to force recreation of widget
516 local $_->{desc_widget}; # hack to force recreation of widget 721 local $_->{desc_widget}; # hack to force recreation of widget
517 CFClient::Item::update_widgets $_; 722 CFClient::Item::update_widgets $_;
518 723
519 $::FLOORBOX->add (0, $row, $_->{face_widget}); 724 $::FLOORBOX->add (0, $row, $_->{face_widget});
520 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 725 $::FLOORBOX->add (1, $row, $_->{desc_widget});
521 726
522 $row++; 727 $row++;
523 } else { 728 } else {
524 $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More..."); 729 $::FLOORBOX->add (1, $row, new CFClient::UI::Button
730 text => "More...",
731 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
732 );
525 last; 733 last;
526 } 734 }
527 } 735 }
736 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 }
528 }); 742 });
529 743
530 $::WANT_REFRESH++; 744 $::WANT_REFRESH++;
531} 745}
532 746
533sub set_opencont { 747sub set_opencont {
534 my ($conn, $tag, $name) = @_; 748 my ($conn, $tag, $name) = @_;
535 $conn->{open_container} = $tag; 749 $conn->{open_container} = $tag;
536 $::INVR_LBL->set_text ($name); 750 update_floorbox;
751
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 0
764 },
765 );
766 }
767
537 $::INVR->set_items ($conn->{container}{$tag}); 768 $::INVR->set_items ($conn->{container}{$tag});
538} 769}
539 770
540sub update_container { 771sub update_containers {
541 my ($tag) = @_; 772 my ($self) = @_;
542 773
543 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) 774 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub {
775 my $todo = delete $self->{update_container}
776 or return;
777
778 for my $tag (keys %$todo) {
779 update_floorbox if $tag == 0 or $tag == $self->{open_container};
780 if ($tag == 0) {
781 $::INVR->set_items ($self->{container}{0})
544 if $tag == $::CONN->{open_container}; 782 if $tag == $self->{open_container};
783 } elsif ($tag == $self->{player}{tag}) {
784 $::INV->set_items ($self->{container}{$tag})
785 } else {
786 $::INVR->set_items ($self->{container}{$tag})
787 if $tag == $self->{open_container};
788 }
789 }
790 });
545} 791}
546 792
547sub container_add { 793sub container_add {
548 my ($self, $tag, $items) = @_; 794 my ($self, $tag, $items) = @_;
549 795
550 #d# print "container_add: container $tag ($self->{player}{tag})\n"; 796 $self->{update_container}{$tag}++;
551 797 $self->update_containers;
552 if ($tag == 0) {
553 update_floorbox;
554 update_container (0);
555 } elsif ($tag == $self->{player}{tag}) {
556 $::INV->set_items ($self->{container}{$self->{player}{tag}})
557 } else {
558 update_container ($tag);
559 }
560
561 # $self-<{player}{tag} => player inv
562 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
563} 798}
564 799
565sub container_clear { 800sub container_clear {
566 my ($self, $tag) = @_; 801 my ($self, $tag) = @_;
567 802
568 #d# print "container_clear: container $tag ($self->{player}{tag})\n"; 803 $self->{update_container}{$tag}++;
569 804 $self->update_containers;
570 if ($tag == 0) {
571 update_floorbox;
572 update_container (0);
573 } elsif ($tag == $self->{player}{tag}) {
574 $::INV->set_items ($self->{container}{$tag})
575 }
576
577# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
578} 805}
579 806
580sub item_delete { 807sub item_delete {
581 my ($self, @items) = @_; 808 my ($self, @items) = @_;
582 809
810 $self->{update_container}{$_->{container}}++
583 for (@items) { 811 for @items;
584 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
585
586 if ($_->{container} == 0) {
587 update_floorbox;
588 update_container ($_->{tag});
589 } elsif ($_->{container} == $self->{player}{tag}) {
590 $::INV->set_items ($self->{container}{$self->{player}{tag}})
591 } else {
592 update_container ($_->{tag});
593 }
594 } 812
813 $self->update_containers;
595} 814}
596 815
597sub item_update { 816sub item_update {
598 my ($self, $item) = @_; 817 my ($self, $item) = @_;
599 818
600 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 819 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
601 820
602 if ($item->{tag} == $self->{player}{tag}) {
603 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
604 return;
605 }
606
607 CFClient::Item::update_widgets $item; 821 CFClient::Item::update_widgets $item;
608 822
609 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 823 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
610 set_opencont ($::CONN, 0, "Floor"); 824 set_opencont ($::CONN, 0, "Floor");
611 825
612 } elsif ($item->{flags} & F_OPEN) { 826 } elsif ($item->{flags} & F_OPEN) {
613 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 827 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
828
614 } else { 829 } else {
615 if ($item->{container} == 0) { 830 $self->{update_container}{$item->{container}}++;
616 update_floorbox;
617 update_container (0); 831 $self->update_containers;
618 } elsif ($item->{container} == $self->{player}{tag}) {
619 $::INV->set_items ($self->{container}{$item->{container}})
620 }
621 } 832 }
622} 833}
623 834
624sub player_update { 835sub player_update {
625 my ($self, $player) = @_; 836 my ($self, $player) = @_;
626 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 837 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
627} 838}
628 839
840sub update_server_info {
841 my ($self) = @_;
842
843 my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
844
845 $::SERVER_INFO->set_markup (
846 "server <tt>$self->{host}:$self->{port}</tt>\n"
847 . "protocol version <tt>$self->{version}</tt>\n"
848 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
849 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
850 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
851 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
852 . "map size $self->{mapw}×$self->{maph}\n"
853 );
854}
855
856sub logged_in {
857 my ($self) = @_;
858
859 $self->send_ext_req (cfplus_support => "1", sub {
860 $self->{cfplus_ext} = $_[0];
861 $self->update_server_info;
862 });
863
864 $self->update_server_info;
865
866 $self->send_command ("output-sync $::CFG->{output_sync}");
867 $self->send_command ("output-count $::CFG->{output_count}");
868 $self->send_command ("pickup $::CFG->{pickup}");
869}
870
871sub lookat {
872 my ($self, $x, $y) = @_;
873
874 if ($self->{cfplus_ext}) {
875 $self->send_ext_req (lookat => "$x $y", sub {
876 my %res = split /\x00/, $_[0];
877
878 if (exists $res{npc_dialog}) {
879 # start npc chat dialog
880 $self->{npc_dialog} = new CFClient::NPCDialog::
881 dx => $x,
882 dy => $y,
883 title => "$res{npc_dialog} (NPC)",
884 conn => $self,
885 ;
886 }
887 });
888 }
889
890 $self->send ("lookat $x $y");
891}
892
893sub destroy {
894 my ($self) = @_;
895
896 $self->{npc_dialog}->destroy
897 if $self->{npc_dialog};
898
899 $self->SUPER::destroy;
900}
901
902package CFClient::NPCDialog;
903
904our @ISA = 'CFClient::UI::FancyFrame';
905
906sub new {
907 my $class = shift;
908
909 my $self = $class->SUPER::new (
910 x => 'center',
911 y => 'center',
912 name => "npc_dialog",
913 force_w => $::WIDTH * 0.7,
914 force_h => $::HEIGHT * 0.7,
915 title => "NPC Dialog",
916 kw => { hi => 0, yes => 0, no => 0 },
917 @_,
918 );
919
920 Scalar::Util::weaken (my $this = $self);
921
922 # better use a pane...
923 $self->add (my $hbox = new CFClient::UI::HBox);
924 $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1);
925
926 $hbox->add (my $vbox = new CFClient::UI::VBox);
927
928 $vbox->add (new CFClient::UI::Label text => "Message Entry:");
929 $vbox->add ($self->{entry} = new CFClient::UI::Entry
930 tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n"
931 . "Sometimes you have to tell an NPC something you cannot find out during "
932 . "a normal conversation (such as a password). In those cases you have to use "
933 . "this text entry. You can also enter responses manually instead of using the response "
934 . "buttons below.",
935 on_activate => sub {
936 my ($entry, $text) = @_;
937
938 return unless $text =~ /\S/;
939
940 $entry->set_text ("");
941 $this->send ($text);
942
943 0
944 },
945 );
946
947 $vbox->add ($self->{options} = new CFClient::UI::VBox);
948
949 $self->{bye_button} = new CFClient::UI::Button
950 text => "Bye (close)",
951 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
952 on_activate => sub { $this->destroy; 0 },
953 ;
954
955 $self->update_options;
956
957 $self->{token} = $self->{conn}->ext_token;
958 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) });
959 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}");
960
961 $self->{entry}->grab_focus;
962
963 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n");
964
965 $self->show;
966 $self
967};
968
969sub update_options {
970 my ($self) = @_;
971
972 Scalar::Util::weaken $self;
973
974 $self->{options}->clear;
975 $self->{options}->add ($self->{bye_button});
976
977 for my $kw (sort keys %{ $self->{kw} }) {
978 $self->{options}->add (new CFClient::UI::Button
979 text => $kw,
980 on_activate => sub {
981 $self->send ($kw);
982 0
983 },
984 );
985 }
986}
987
988sub feed {
989 my ($self, $data) = @_;
990
991 Scalar::Util::weaken $self;
992
993 my ($type, $msg) = split / /, $data, 2;
994
995 if ($type eq "msg") {
996 my ($msg, @kw) = split /\x00/, $msg;
997
998 utf8::decode $_ for ($msg, @kw);
999
1000 $self->{kw}{$_} = 1 for @kw;
1001
1002 $msg = "\n" . CFClient::UI::Label::escape $msg;
1003 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1004 my @link;
1005 $msg =~ s{
1006 ($match)
1007 }{
1008 my $kw = $1;
1009
1010 push @link, new CFClient::UI::Label
1011 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1012 can_hover => 1,
1013 can_events => 1,
1014 padding_x => 0,
1015 padding_y => 0,
1016 on_button_up => sub {
1017 $self->send ($kw);
1018 };
1019
1020 chr 0xfffc
1021 }giex;
1022
1023 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]);
1024 $self->{textview}->scroll_to_bottom;
1025 $self->update_options;
1026 } else {
1027 $self->destroy;
1028 }
1029
1030 1
1031}
1032
1033sub send {
1034 my ($self, $msg) = @_;
1035
1036 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
1037 $self->{textview}->scroll_to_bottom;
1038
1039 utf8::encode $msg;
1040 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
1041}
1042
1043sub destroy {
1044 my ($self) = @_;
1045
1046 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1047
1048 $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token};
1049 delete $self->{conn}{npc_dialog};
1050 $self->{conn}->disconnect_ext ($self->{token});
1051
1052 $self->SUPER::destroy;
1053}
1054
6291; 10551;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines