ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.6
Committed: Sat May 27 19:49:07 2006 UTC (17 years, 11 months ago) by root
Branch: MAIN
Changes since 1.5: +6 -5 lines
Log Message:
improved and tuned completer

File Contents

# Content
1 package CFClient::Protocol;
2
3 use utf8;
4 use strict;
5
6 use Crossfire::Protocol::Constants;
7
8 use CFClient::UI;
9
10 use base 'Crossfire::Protocol::Base';
11
12 sub new {
13 my $class = shift;
14
15 my $self = $class->SUPER::new (@_);
16
17 $self->{map_widget}->clr_commands;
18
19 my $parser = new Pod::POM;
20 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod");
21
22 for my $head2 ($pod->head1->[-2]->head2) {
23 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
24 or next;
25
26 my $cmd = $1;
27 my @args = split /\|/, $2;
28 @args = (".*") unless @args;
29
30 $_ = $_ eq ".*" ? "" : " $_"
31 for @args;
32
33 my $text = CFClient::pod_to_pango $head2->content;
34
35 $self->{map_widget}->add_command ("$cmd$_", $text)
36 for sort { (length $a) <=> (length $b) }
37 @args;
38 }
39
40 $self->{noface} = new_from_file CFClient::Texture
41 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
42
43 $self->{open_container} = 0;
44
45 # "global"
46 $self->{tilecache} = CFClient::db_table "tilecache";
47 $self->{facemap} = CFClient::db_table "facemap";
48
49 # per server
50 $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
51
52 $self
53 }
54
55 sub stats_update {
56 my ($self, $stats) = @_;
57
58 if (my $exp = $stats->{+CS_STAT_EXP64}) {
59 my $diff = $exp - $self->{prev_exp};
60 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5)
61 if exists $self->{prev_exp} && $diff;
62 $self->{prev_exp} = $exp;
63 }
64
65 ::update_stats_window ($stats);
66 }
67
68 sub user_send {
69 my ($self, $command) = @_;
70
71 $self->send_command ($command);
72 ::status $command;
73 }
74
75 sub map_scroll {
76 my ($self, $dx, $dy) = @_;
77
78 $self->{map}->scroll ($dx, $dy);
79 }
80
81 sub feed_map1a {
82 my ($self, $data) = @_;
83
84 $self->{map}->map1a_update ($data);
85 $self->{map_widget}->update;
86 }
87
88 sub flush_map {
89 my ($self) = @_;
90
91 my $map_info = delete $self->{map_info}
92 or return;
93
94 my ($hash, $x, $y, $w, $h) = @$map_info;
95
96 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
97 $self->{mapcache}->put ($hash => Compress::LZF::compress $data);
98 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
99 }
100
101 sub map_clear {
102 my ($self) = @_;
103
104 $self->flush_map;
105 delete $self->{neigh_map};
106
107 $self->{map}->clear;
108 }
109
110
111 sub load_map($$$) {
112 my ($self, $hash, $x, $y) = @_;
113
114 if (defined (my $data = $self->{mapcache}->get ($hash))) {
115 $data = Compress::LZF::decompress $data;
116 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
117 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
118 my $data = $self->{tilecache}->get ($id)
119 or next;
120
121 $self->set_texture ($id => $data);
122 }
123 }
124 }
125
126 # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
127 # (server resource,s latency, bandwidth), so this hack is warranted.
128 # the right fix is to make real tiled maps with an overview file
129 sub send_mapinfo {
130 my ($self, $data, $cb) = @_;
131
132 if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
133 my ($wx, $wy) = ($1, $2);
134
135 if ($data =~ /^spatial ([1-4]+)$/) {
136 my @dx = (0, 0, 1, 0, -1);
137 my @dy = (0, -1, 0, 1, 0);
138 my ($dx, $dy);
139
140 for (split //, $1) {
141 $dx += $dx[$_];
142 $dy += $dy[$_];
143 }
144
145 $cb->(spatial => 15,
146 $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
147 $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
148 50, 50,
149 sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
150 );
151
152 return;
153 }
154 }
155
156 $self->SUPER::send_mapinfo ($data, $cb);
157 }
158
159 # this method does a "flood fill" into every tile direction
160 # it assumes that tiles are arranged in a rectangular grid,
161 # i.e. a map is the same as the left of the right map etc.
162 # failure to comply are harmless and result in display errors
163 # at worst.
164 sub flood_fill {
165 my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
166
167 # the server does not allow map paths > 6
168 return if 7 <= length $path;
169
170 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
171
172 for (
173 [1, 3, 0, -1],
174 [2, 4, 1, 0],
175 [3, 1, 0, 1],
176 [4, 2, -1, 0],
177 ) {
178 my ($tile, $tile2, $dx, $dy) = @$_;
179
180 next if $block & (1 << $tile);
181 my $block = $block | (1 << $tile2);
182
183 my $gx = $gx + $dx;
184 my $gy = $gy + $dy;
185
186 next unless $flags & (1 << ($tile - 1));
187 next if $self->{neigh_grid}{$gx, $gy}++;
188
189 my $neigh = $self->{neigh_map}{$hash} ||= [];
190 if (my $info = $neigh->[$tile]) {
191 my ($flags, $x, $y, $w, $h, $hash) = @$info;
192
193 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
194 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
195
196 } else {
197 $self->send_mapinfo ("spatial $path$tile", sub {
198 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
199
200 return if $mode ne "spatial";
201
202 $x += $self->{map}->ox;
203 $y += $self->{map}->oy;
204
205 $self->load_map ($hash, $x, $y)
206 unless $self->{neigh_map}{$hash}[5]++;#d#
207
208 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
209
210 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
211 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
212 });
213 }
214 }
215 }
216
217 sub map_change {
218 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
219
220 $self->flush_map;
221
222 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
223
224 my $mapmapw = $self->{mapmap}->{w};
225 my $mapmaph = $self->{mapmap}->{h};
226
227 $self->{neigh_rect} = [
228 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
229 $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
230 ];
231
232 delete $self->{neigh_grid};
233
234 $x += $ox;
235 $y += $oy;
236
237 $self->{map_info} = [$hash, $x, $y, $w, $h];
238
239 (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
240 $::STATWIDS->{map}->set_text ("Map: " . $map);
241
242 $self->load_map ($hash, $x, $y);
243 $self->flood_fill (0, 0, 0, "", $hash, $flags);
244 }
245
246 sub face_find {
247 my ($self, $facenum, $face) = @_;
248
249 my $hash = "$face->{chksum},$face->{name}";
250
251 my $id = $self->{facemap}->get ($hash);
252
253 unless ($id) {
254 # create new id for face
255 # I love transactions
256 for (1..100) {
257 my $txn = $CFClient::DB_ENV->txn_begin;
258 my $status = $self->{facemap}->db_get (id => $id, BerkeleyDB::DB_RMW);
259 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
260 $id = ($id || 16) + 1;
261 if ($self->{facemap}->put (id => $id) == 0
262 && $self->{facemap}->put ($hash => $id) == 0) {
263 $txn->txn_commit;
264
265 goto gotid;
266 }
267 }
268 $txn->abort;
269 }
270
271 CFClient::fatal "maximum number of transaction retries reached - database problems?";
272 }
273
274 gotid:
275 $face->{id} = $id;
276 $self->{map}->set_face ($facenum => $id);
277 $self->{faceid}[$facenum] = $id;#d#
278
279 my $face = $self->{tilecache}->get ($id);
280
281 if ($face) {
282 #$self->face_prefetch;
283 $face
284 } else {
285 my $tex = $self->{noface};
286 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
287 undef
288 };
289 }
290
291 sub face_update {
292 my ($self, $facenum, $face) = @_;
293
294 $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
295
296 $self->set_texture ($face->{id} => delete $face->{image});
297 }
298
299 sub set_texture {
300 my ($self, $id, $data) = @_;
301
302 $self->{texture}[$id] ||= do {
303 my $tex =
304 new_from_image CFClient::Texture
305 $data, minify => 1, mipmap => 1;
306
307 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
308 $self->{map_widget}->update;
309
310 $tex
311 };
312 }
313
314 sub sound_play {
315 my ($self, $x, $y, $soundnum, $type) = @_;
316
317 $self->{sound_play}->($x, $y, $soundnum, $type);
318 }
319
320 my $LAST_QUERY; # server is stupid, stupid, stupid
321
322 sub query {
323 my ($self, $flags, $prompt) = @_;
324
325 $prompt = $LAST_QUERY unless length $prompt;
326 $LAST_QUERY = $prompt;
327
328 my $dialog = new CFClient::UI::FancyFrame
329 title => "Query",
330 child => my $vbox = new CFClient::UI::VBox;
331
332 $vbox->add (new CFClient::UI::Label
333 max_w => $::WIDTH * 0.4,
334 ellipsise => 0,
335 text => $prompt);
336
337 if ($flags & CS_QUERY_YESNO) {
338 $vbox->add (my $hbox = new CFClient::UI::HBox);
339 $hbox->add (new CFClient::UI::Button
340 text => "No",
341 connect_activate => sub {
342 $self->send ("reply n");
343 $dialog->destroy;
344 $self->{map_widget}->focus_in;
345 }
346 );
347 $hbox->add (new CFClient::UI::Button
348 text => "Yes",
349 connect_activate => sub {
350 $self->send ("reply y");
351 $dialog->destroy;
352 },
353 );
354
355 $dialog->focus_in;
356
357 } elsif ($flags & CS_QUERY_SINGLECHAR) {
358 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
359 $vbox->add (my $entry = new CFClient::UI::Entry
360 connect_changed => sub {
361 $self->send ("reply $_[1]");
362 $dialog->destroy;
363 },
364 );
365
366 $entry->focus_in;
367
368 } else {
369 $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
370
371 $vbox->add (my $entry = new CFClient::UI::Entry
372 $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
373 connect_activate => sub {
374 $self->send ("reply $_[1]");
375 $dialog->destroy;
376 },
377 );
378
379 $entry->focus_in;
380 }
381
382 $dialog->show_centered;
383 }
384
385 sub drawinfo {
386 my ($self, $color, $text) = @_;
387
388 my @color = (
389 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
390 [1.00, 1.00, 1.00],
391 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
392 [1.00, 0.00, 0.00],
393 [1.00, 0.54, 0.00],
394 [0.11, 0.56, 1.00],
395 [0.93, 0.46, 0.00],
396 [0.18, 0.54, 0.34],
397 [0.56, 0.73, 0.56],
398 [0.80, 0.80, 0.80],
399 [0.55, 0.41, 0.13],
400 [0.99, 0.77, 0.26],
401 [0.74, 0.65, 0.41],
402 );
403
404 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
405
406 $text = CFClient::UI::Label::escape $text;
407 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
408 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
409
410 $self->{logview}->add_paragraph ($color[$color],
411 join "\n", map "$time $_", split /\n/, $text);
412
413 $self->{statusbox}->add ($text,
414 group => $text,
415 fg => $color[$color],
416 timeout => 10,
417 tooltip_font => $::FONT_FIXED,
418 );
419 }
420
421 sub drawextinfo {
422 my ($self, $color, $type, $subtype, $message) = @_;
423
424 $self->drawinfo ($color, $message);
425 }
426
427 sub spell_add {
428 my ($self, $spell) = @_;
429
430 # TODO
431 # create a widget dynamically, using spell face (CF::Protocol downloads them)
432 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
433 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
434 }
435
436 sub spell_delete {
437 my ($self, $spell) = @_;
438 }
439
440 sub addme_success {
441 my ($self) = @_;
442
443 $self->send ("command output-sync $::CFG->{output_sync}");
444 $self->send ("command output-count $::CFG->{output_count}");
445
446 my $parser = new Pod::POM;
447 my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod");
448
449 my %skill_tooltip;
450
451 for my $head2 ($pod->head1->[-2]->head2) {
452 $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content;
453 }
454
455 for my $skill (values %{$self->{skill_info}}) {
456 $self->{map_widget}->add_command ("ready_skill $skill",
457 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
458 . $skill_tooltip{$skill});
459 $self->{map_widget}->add_command ("use_skill $skill",
460 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
461 . $skill_tooltip{$skill});
462 }
463 }
464
465 sub eof {
466 my ($self) = @_;
467
468 $self->{map_widget}->clr_commands;
469
470 ::stop_game ();
471 }
472
473 sub image_info {
474 my ($self, $numfaces) = @_;
475
476 $self->{num_faces} = $numfaces;
477 $self->{face_prefetch} = [1 .. $numfaces];
478 $self->face_prefetch;
479 }
480
481 sub face_prefetch {
482 my ($self) = @_;
483
484 return unless $::CFG->{face_prefetch};
485
486 if ($self->{num_faces}) {
487 return if @{ $self->{send_queue} || [] };
488 my $todo = @{ $self->{face_prefetch} }
489 or return;
490
491 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
492
493 $self->send ("requestinfo image_sums $face $face");
494
495 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
496 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
497 } elsif (!exists $self->{num_faces}) {
498 $self->send ("requestinfo image_info");
499
500 $self->{num_faces} = 0;
501
502 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
503 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
504 }
505 }
506
507 sub update_floorbox {
508 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
509 return unless $::CONN;
510
511 $::FLOORBOX->clear;
512
513 my $row;
514 for (@{ $::CONN->{container}{0} }) {
515 if ($row < 7) {
516 local $_->{face_widget}; # hack to force recreation of widget
517 local $_->{desc_widget}; # hack to force recreation of widget
518 CFClient::Item::update_widgets $_;
519
520 $::FLOORBOX->add (0, $row, $_->{face_widget});
521 $::FLOORBOX->add (1, $row, $_->{desc_widget});
522
523 $row++;
524 } else {
525 $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More...");
526 last;
527 }
528 }
529 });
530
531 $::WANT_REFRESH++;
532 }
533
534 sub set_opencont {
535 my ($conn, $tag, $name) = @_;
536 $conn->{open_container} = $tag;
537 $::INVR_LBL->set_text ($name);
538 $::INVR->set_items ($conn->{container}{$tag});
539 }
540
541 sub update_container {
542 my ($tag) = @_;
543
544 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
545 if $tag == $::CONN->{open_container};
546 }
547
548 sub container_add {
549 my ($self, $tag, $items) = @_;
550
551 #d# print "container_add: container $tag ($self->{player}{tag})\n";
552
553 if ($tag == 0) {
554 update_floorbox;
555 update_container (0);
556 } elsif ($tag == $self->{player}{tag}) {
557 $::INV->set_items ($self->{container}{$self->{player}{tag}})
558 } else {
559 update_container ($tag);
560 }
561
562 # $self-<{player}{tag} => player inv
563 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
564 }
565
566 sub container_clear {
567 my ($self, $tag) = @_;
568
569 #d# print "container_clear: container $tag ($self->{player}{tag})\n";
570
571 if ($tag == 0) {
572 update_floorbox;
573 update_container (0);
574 } elsif ($tag == $self->{player}{tag}) {
575 $::INV->set_items ($self->{container}{$tag})
576 }
577
578 # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
579 }
580
581 sub item_delete {
582 my ($self, @items) = @_;
583
584 for (@items) {
585 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
586
587 if ($_->{container} == 0) {
588 update_floorbox;
589 update_container ($_->{tag});
590 } elsif ($_->{container} == $self->{player}{tag}) {
591 $::INV->set_items ($self->{container}{$self->{player}{tag}})
592 } else {
593 update_container ($_->{tag});
594 }
595 }
596 }
597
598 sub item_update {
599 my ($self, $item) = @_;
600
601 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
602
603 if ($item->{tag} == $self->{player}{tag}) {
604 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
605 return;
606 }
607
608 CFClient::Item::update_widgets $item;
609
610 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
611 set_opencont ($::CONN, 0, "Floor");
612
613 } elsif ($item->{flags} & F_OPEN) {
614 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
615 } else {
616 if ($item->{container} == 0) {
617 update_floorbox;
618 update_container (0);
619 } elsif ($item->{container} == $self->{player}{tag}) {
620 $::INV->set_items ($self->{container}{$item->{container}})
621 }
622 }
623 }
624
625 sub player_update {
626 my ($self, $player) = @_;
627 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
628 }
629
630 1;