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.90 by root, Wed Dec 6 00:15:12 2006 UTC vs.
Revision 1.97 by root, Fri Apr 6 08:09:43 2007 UTC

4use strict; 4use strict;
5 5
6use Crossfire::Protocol::Constants; 6use Crossfire::Protocol::Constants;
7 7
8use CFPlus; 8use CFPlus;
9use CFPlus::DB;
9use CFPlus::UI; 10use CFPlus::UI;
10use CFPlus::Pod; 11use CFPlus::Pod;
12use CFPlus::Macro;
13use CFPlus::Item;
11 14
12use Crossfire::Protocol::Base 0.95; 15use Crossfire::Protocol::Base 0.95;
13 16
14use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
15 18
35 for @args; 38 for @args;
36 39
37 map ["$cmd$_", $text], 40 map ["$cmd$_", $text],
38 sort { (length $a) <=> (length $b) } 41 sort { (length $a) <=> (length $b) }
39 @args 42 @args
40 } sort { $a->{par} <=> $b->{par} } 43 } sort { $a->{par} <=> $b->{par} }
41 CFPlus::Pod::find command => "*"; 44 CFPlus::Pod::find command => "*";
42 45
46 $self->connect_ext (event_capabilities => sub {
47 my ($cap) = @_;
48
49 if (my $ts = $cap->{tileset}) {
50 if (my ($default) = grep $_->[2] & 1, @$ts) {
51 $self->{tileset} = $default;
52 $self->{tilesize} = $default->[3];
53 $self->setup_req (tileset => $default->[0]);
54
55 my $w = int $self->{mapw} * 32 / $self->{tilesize};
56 my $h = int $self->{maph} * 32 / $self->{tilesize};
57
58 $self->setup_req (mapsize => "${w}x${h}");
59 }
60 }
61 });
62
43 $self->{map_widget}->add_command (@$_) 63 $self->{map_widget}->add_command (@$_)
44 for @cmd_help; 64 for @cmd_help;
45
46 $self->{noface} = new_from_file CFPlus::Texture
47 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
48 65
49 { 66 {
50 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 67 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
51 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 68 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
52 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 69 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
53 } 70 }
54 71
72 {
73 $self->{noface} = my $tex = new_from_file CFPlus::Texture
74 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
75 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
76 }
77
55 $self->{open_container} = 0; 78 $self->{open_container} = 0;
56 79
57 # "global"
58 $self->{tilecache} = CFPlus::db_table "tilecache"
59 or die "tilecache: unable to open database table";
60 $self->{facemap} = CFPlus::db_table "facemap"
61 or die "facemap: unable to open database table";
62
63 # per server 80 # per server
64 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 81 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
65 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
66 82
67 $self 83 $self
68} 84}
69 85
70sub logprint { 86sub logprint {
291 $::CONN->user_send ("use_skill $name"); 307 $::CONN->user_send ("use_skill $name");
292 } elsif ($ev->{button} == 3) { 308 } elsif ($ev->{button} == 3) {
293 my $shortname = CFPlus::shorten $name, 14; 309 my $shortname = CFPlus::shorten $name, 14;
294 (new CFPlus::UI::Menu 310 (new CFPlus::UI::Menu
295 items => [ 311 items => [
296 ["bind <i>ready_skill $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["ready_skill $name"]) }], 312 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
297 ["bind <i>use_skill $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["use_skill $name"]) }], 313 ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
298 ], 314 ],
299 )->popup ($ev); 315 )->popup ($ev);
300 } else { 316 } else {
301 return 0; 317 return 0;
302 } 318 }
319 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 335 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
320 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 336 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
321 } 337 }
322} 338}
323 339
340sub macro_send {
341 my ($self, $macro) = @_;
342
343 for my $cmd (@{ $macro->{action} }) {
344 $self->send_command ($cmd);
345 }
346}
347
324sub user_send { 348sub user_send {
325 my ($self, $command) = @_; 349 my ($self, $command) = @_;
326 350
327 push @{$self->{record}}, $command 351 $self->{record}->($command)
328 if $self->{record}; 352 if $self->{record};
329 353
330 $self->logprint ("send: ", $command); 354 $self->logprint ("send: ", $command);
331 $self->send_command ($command); 355 $self->send_command ($command);
332 ::status ($command); 356 ::status ($command);
333} 357}
334 358
335sub start_record { 359sub record {
336 my ($self) = @_; 360 my ($self, $cb) = @_;
337 361
338 $self->{record} = []; 362 $self->{record} = $cb;
339}
340
341sub stop_record {
342 my ($self) = @_;
343 return delete $self->{record};
344} 363}
345 364
346sub map_scroll { 365sub map_scroll {
347 my ($self, $dx, $dy) = @_; 366 my ($self, $dx, $dy) = @_;
348 367
369 or return; 388 or return;
370 389
371 my ($hash, $x, $y, $w, $h) = @$map_info; 390 my ($hash, $x, $y, $w, $h) = @$map_info;
372 391
373 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 392 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
374 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 393 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
375 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 394 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
376} 395}
377 396
378sub map_clear { 397sub map_clear {
379 my ($self) = @_; 398 my ($self) = @_;
383 402
384 $self->{map}->clear; 403 $self->{map}->clear;
385 delete $self->{map_widget}{magicmap}; 404 delete $self->{map_widget}{magicmap};
386} 405}
387 406
407sub bg_fetch {
408 my ($self) = @_;
409
410 my $id;
411
412 do {
413 $id = pop @{$self->{bg_fetch}}
414 or return;
415 } while $self->{texture}[$id];
416
417 CFPlus::DB::get tilecache => $id, sub {
418 my ($data) = @_;
419
420 $self->set_texture ($id => $data)
421 if defined $data;
422
423 $self->bg_fetch;
424 };
425}
388 426
389sub load_map($$$) { 427sub load_map($$$) {
390 my ($self, $hash, $x, $y) = @_; 428 my ($self, $hash, $x, $y) = @_;
391 429
392 if (defined (my $data = $self->{mapcache}->get ($hash))) { 430 CFPlus::DB::get $self->{mapcache} => $hash, sub {
431 my ($data) = @_;
432
433 if (defined $data) {
393 $data = Compress::LZF::decompress $data; 434 $data = Compress::LZF::decompress $data;
394 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 435 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
395 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
396 my $data = $self->{tilecache}->get ($id)
397 or next;
398 436
399 $self->set_texture ($id => $data); 437 my $inprogress = @{ $self->{bg_fetch} || [] };
438 push @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
439 $self->bg_fetch unless $inprogress;
400 } 440 }
401 } 441 };
402} 442}
403 443
404# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 444# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
405# (server resource,s latency, bandwidth), so this hack is warranted. 445# (server resource,s latency, bandwidth), so this hack is warranted.
406# the right fix is to make real tiled maps with an overview file 446# the right fix is to make real tiled maps with an overview file
520 $self->load_map ($hash, $x, $y); 560 $self->load_map ($hash, $x, $y);
521 $self->flood_fill (0, 0, 0, "", $hash, $flags); 561 $self->flood_fill (0, 0, 0, "", $hash, $flags);
522} 562}
523 563
524sub face_find { 564sub face_find {
525 my ($self, $facenum, $face) = @_; 565 my ($self, $facenum, $face, $cb) = @_;
526 566
527 my $hash = "$face->{chksum},$face->{name}"; 567 my $hash = "$face->{chksum},$face->{name}";
528 568
529 my $id = $self->{facemap}->get ($hash); 569 my $id = CFPlus::DB::get_tile_id_sync $hash;
530 570
531 unless ($id) {
532 # create new id for face
533 # I love transactions
534 for (1..100) {
535 my $txn = $CFPlus::DB_ENV->txn_begin;
536 my $status = $self->{facemap}->db_get (id => $id);
537 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
538 $id = ($id || 64) + 1;
539 if ($self->{facemap}->put (id => $id) == 0
540 && $self->{facemap}->put ($hash => $id) == 0) {
541 $txn->txn_commit;
542
543 goto gotid;
544 }
545 }
546 $txn->txn_abort;
547 }
548
549 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
550 }
551
552gotid:
553 $face->{id} = $id; 571 $face->{id} = $id;
554 $self->{map}->set_face ($facenum => $id);
555 $self->{faceid}[$facenum] = $id;#d# 572 $self->{faceid}[$facenum] = $id;
556 573
557 my $face = $self->{tilecache}->get ($id); 574 $self->{map}->set_tileid ($facenum => $id);
558 575
559 if ($face) { 576 CFPlus::DB::get tilecache => $id, $cb;
560 #$self->face_prefetch;
561 $face
562 } else {
563 my $tex = $self->{noface};
564 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
565 undef
566 };
567} 577}
568 578
569sub face_update { 579sub face_update {
570 my ($self, $facenum, $face, $changed) = @_; 580 my ($self, $facenum, $face, $changed) = @_;
571 581
572 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 582 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
583 if $changed;
573 584
574 $self->set_texture ($face->{id} => delete $face->{image}); 585 $self->set_texture ($face->{id} => delete $face->{image});
575} 586}
576 587
577sub set_texture { 588sub set_texture {
623 [0.75, 0.61, 0.20], 634 [0.75, 0.61, 0.20],
624 [0.99, 0.77, 0.26], 635 [0.99, 0.77, 0.26],
625 [0.74, 0.65, 0.41], 636 [0.74, 0.65, 0.41],
626 ); 637 );
627 638
639 my $fg = $color[$color % @color];
640
628 $self->logprint ("info: ", $text); 641 $self->logprint ("info: ", $text);
629 642
630 # try to create single paragraphs of multiple lines sent by the server 643 # try to create single paragraphs of multiple lines sent by the server
631 $text =~ s/(?<=\S)\n(?=\w)/ /g; 644 $text =~ s/(?<=\S)\n(?=\w)/ /g;
632 645
633 $text = CFPlus::asxml $text; 646 $text = CFPlus::asxml $text;
634 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 647 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
635 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 648 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
636 649
637 ::message ({ fg => $color[$color], markup => $_ }) 650 ::message ({ fg => $fg, markup => $_ })
638 for split /\n/, $text; 651 for split /\n/, $text;
639 652
640 $self->{statusbox}->add ($text, 653 $self->{statusbox}->add ($text,
641 group => $text, 654 group => $text,
642 fg => $color[$color], 655 fg => $fg,
643 timeout => $color >= 2 ? 180 : 10, 656 timeout => $color >= 2 ? 180 : 10,
644 tooltip_font => $::FONT_FIXED, 657 tooltip_font => $::FONT_FIXED,
645 ); 658 );
646} 659}
647 660
672} 685}
673 686
674sub setup { 687sub setup {
675 my ($self, $setup) = @_; 688 my ($self, $setup) = @_;
676 689
690 $self->{map_widget}->set_tilesize ($self->{tilesize});
677 $::MAP->resize ($self->{mapw}, $self->{maph}); 691 $::MAP->resize ($self->{mapw}, $self->{maph});
678} 692}
679 693
680sub addme_success { 694sub addme_success {
681 my ($self) = @_; 695 my ($self) = @_;
921 935
922 $self->update_server_info; 936 $self->update_server_info;
923 937
924 $self->send_command ("output-sync $::CFG->{output_sync}"); 938 $self->send_command ("output-sync $::CFG->{output_sync}");
925 $self->send_command ("output-count $::CFG->{output_count}"); 939 $self->send_command ("output-count $::CFG->{output_count}");
940 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
926 $self->send_command ("pickup $::CFG->{pickup}"); 941 $self->send_command ("pickup $::CFG->{pickup}");
927} 942}
928 943
929sub buildat { 944sub buildat {
930 my ($self, $builditem, $x, $y) = @_; 945 my ($self, $builditem, $x, $y) = @_;
982 kw => { hi => 0, yes => 0, no => 0 }, 997 kw => { hi => 0, yes => 0, no => 0 },
983 has_close_button => 1, 998 has_close_button => 1,
984 @_, 999 @_,
985 ); 1000 );
986 1001
987 Scalar::Util::weaken (my $this = $self); 1002 CFPlus::weaken (my $this = $self);
988 1003
989 $self->connect (delete => sub { $this->destroy; 1 }); 1004 $self->connect (delete => sub { $this->destroy; 1 });
990 1005
991 # better use a pane... 1006 # better use a pane...
992 $self->add (my $hbox = new CFPlus::UI::HBox); 1007 $self->add (my $hbox = new CFPlus::UI::HBox);
1036}; 1051};
1037 1052
1038sub update_options { 1053sub update_options {
1039 my ($self) = @_; 1054 my ($self) = @_;
1040 1055
1041 Scalar::Util::weaken $self; 1056 CFPlus::weaken $self;
1042 1057
1043 $self->{options}->clear; 1058 $self->{options}->clear;
1044 $self->{options}->add ($self->{bye_button}); 1059 $self->{options}->add ($self->{bye_button});
1045 1060
1046 for my $kw (sort keys %{ $self->{kw} }) { 1061 for my $kw (sort keys %{ $self->{kw} }) {
1055} 1070}
1056 1071
1057sub feed { 1072sub feed {
1058 my ($self, $msg) = @_; 1073 my ($self, $msg) = @_;
1059 1074
1060 Scalar::Util::weaken $self; 1075 CFPlus::weaken $self;
1061 1076
1062 if ($msg->{msgtype} eq "reply") { 1077 if ($msg->{msgtype} eq "reply") {
1063 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1078 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1064 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1079 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1065 1080

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines