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.95 by root, Wed Apr 4 02:43:30 2007 UTC vs.
Revision 1.106 by root, Sun Apr 29 20:03:08 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;
11use CFPlus::Macro; 12use CFPlus::Macro;
12use CFPlus::Item; 13use CFPlus::Item;
13 14
14use Crossfire::Protocol::Base 0.95; 15use Crossfire::Protocol::Base 0.95;
15 16
16use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
17 18
18sub new { 19sub new {
19 my $class = shift; 20 my ($class, %arg) = @_;
20 21
21 my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); 22 my $self = $class->SUPER::new (%arg,
23 setup_req => {
24 extmap => 1,
25 excmd => 1,
26 %{$arg{setup_req} || {}},
27 },
28 );
22 29
23 $self->{map_widget}->clr_commands; 30 $self->{map_widget}->clr_commands;
24 31
25 my @cmd_help = map { 32 my @cmd_help = map {
26 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 33 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
37 for @args; 44 for @args;
38 45
39 map ["$cmd$_", $text], 46 map ["$cmd$_", $text],
40 sort { (length $a) <=> (length $b) } 47 sort { (length $a) <=> (length $b) }
41 @args 48 @args
42 } sort { $a->{par} <=> $b->{par} } 49 } sort { $a->{par} <=> $b->{par} }
43 CFPlus::Pod::find command => "*"; 50 CFPlus::Pod::find command => "*";
44 51
45 $self->connect_ext (event_capabilities => sub { 52 $self->connect_ext (event_capabilities => sub {
46 my ($cap) = @_; 53 my ($cap) = @_;
47 54
48 if (my $ts = $cap->{tileset}) { 55 if (my $ts = $cap->{tileset}) {
49 if (my ($default) = grep $_->[2] & 1, @$ts) { 56 if (my ($default) = grep $_->[2] & 1, @$ts) {
50 $self->{tileset} = $default; 57 $self->{tileset} = $default;
51 $self->{tilesize} = $default->[3]; 58 $self->{tilesize} = $default->[3];
52 $self->setup_req (tileset => $default->[0]); 59 $self->setup_req (tileset => $default->[0]);
53 60
54 my $w = int $self->{mapw} * 32 / $self->{tilesize}; 61 my $w = int $self->{mapw} * 32 / $self->{tilesize};
55 my $h = int $self->{maph} * 32 / $self->{tilesize}; 62 my $h = int $self->{maph} * 32 / $self->{tilesize};
56 63
57 $self->setup_req (mapsize => "${w}x${h}"); 64 $self->setup_req (mapsize => "${w}x${h}");
58 } 65 }
59 } 66 }
60 }); 67 });
61 68
62 $self->{map_widget}->add_command (@$_) 69 $self->{map_widget}->add_command (@$_)
63 for @cmd_help; 70 for @cmd_help;
64
65 $self->{noface} = new_from_file CFPlus::Texture
66 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
67 71
68 { 72 {
69 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 73 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
70 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 74 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
71 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 75 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
72 } 76 }
73 77
78 {
79 $self->{noface} = my $tex = new_from_file CFPlus::Texture
80 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
81 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
82 }
83
74 $self->{open_container} = 0; 84 $self->{open_container} = 0;
75 85
76 # "global"
77 $self->{tilecache} = CFPlus::db_table "tilecache"
78 or die "tilecache: unable to open database table";
79 $self->{facemap} = CFPlus::db_table "facemap"
80 or die "facemap: unable to open database table";
81
82 # per server 86 # per server
83 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 87 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
84 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
85 88
86 $self 89 $self
87} 90}
88 91
89sub logprint { 92sub logprint {
246 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 249 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
247 $::GAUGES->{food} ->set_value ($fo, $fo_m); 250 $::GAUGES->{food} ->set_value ($fo, $fo_m);
248 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 251 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
249 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 252 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
250 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 253 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
251 my $rng = $stats->{+CS_STAT_RANGE}; 254 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
252 $rng =~ s/^Range: //; # thank you so much dear server
253 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
254 my $title = $stats->{+CS_STAT_TITLE}; 255 my $title = $stats->{+CS_STAT_TITLE};
255 $title =~ s/^Player: //; 256 $title =~ s/^Player: //;
256 $::STATWIDS->{title} ->set_text ("Title: " . $title); 257 $::STATWIDS->{title} ->set_text ("Title: " . $title);
257 258
258 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR}); 259 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
391 or return; 392 or return;
392 393
393 my ($hash, $x, $y, $w, $h) = @$map_info; 394 my ($hash, $x, $y, $w, $h) = @$map_info;
394 395
395 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 396 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
396 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 397 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
397 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 398 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
398} 399}
399 400
400sub map_clear { 401sub map_clear {
401 my ($self) = @_; 402 my ($self) = @_;
405 406
406 $self->{map}->clear; 407 $self->{map}->clear;
407 delete $self->{map_widget}{magicmap}; 408 delete $self->{map_widget}{magicmap};
408} 409}
409 410
411sub bg_fetch {
412 my ($self) = @_;
413
414 my $id;
415
416 do {
417 $id = pop @{$self->{bg_fetch}}
418 or return;
419 } while $self->{texture}[$id];
420
421 CFPlus::DB::get tilecache => $id, sub {
422 my ($data) = @_;
423
424 return unless $self->{map}; # stop when destroyed
425
426 $self->set_texture ($id => $data)
427 if defined $data;
428
429 $self->bg_fetch;
430 };
431}
410 432
411sub load_map($$$) { 433sub load_map($$$) {
412 my ($self, $hash, $x, $y) = @_; 434 my ($self, $hash, $x, $y) = @_;
413 435
414 if (defined (my $data = $self->{mapcache}->get ($hash))) { 436 my $gen = $self->{map_change_gen};
437
438 CFPlus::DB::get $self->{mapcache} => $hash, sub {
439 return unless $gen == $self->{map_change_gen};
440
441 my ($data) = @_;
442
443 if (defined $data) {
415 $data = Compress::LZF::decompress $data; 444 $data = Compress::LZF::decompress $data;
416 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 445 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
417 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
418 my $data = $self->{tilecache}->get ($id)
419 or next;
420 446
421 $self->set_texture ($id => $data); 447 my $inprogress = @{ $self->{bg_fetch} || [] };
448 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
449 $self->bg_fetch unless $inprogress;
422 } 450 }
423 } 451 };
424} 452}
425 453
426# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 454# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
427# (server resource,s latency, bandwidth), so this hack is warranted. 455# (server resource,s latency, bandwidth), so this hack is warranted.
428# the right fix is to make real tiled maps with an overview file 456# the right fix is to make real tiled maps with an overview file
492 520
493 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 521 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
494 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 522 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
495 523
496 } else { 524 } else {
525 my $gen = $self->{map_change_gen};
497 $self->send_mapinfo ("spatial $path$tile", sub { 526 $self->send_mapinfo ("spatial $path$tile", sub {
527 return unless $gen == $self->{map_change_gen};
528
498 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 529 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
499 530
500 return if $mode ne "spatial"; 531 return if $mode ne "spatial";
501 532
502 $x += $self->{map}->ox; 533 $x += $self->{map}->ox;
516 547
517sub map_change { 548sub map_change {
518 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 549 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
519 550
520 $self->flush_map; 551 $self->flush_map;
552
553 ++$self->{map_change_gen};
521 554
522 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 555 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
523 556
524 my $mapmapw = $self->{mapmap}->{w}; 557 my $mapmapw = $self->{mapmap}->{w};
525 my $mapmaph = $self->{mapmap}->{h}; 558 my $mapmaph = $self->{mapmap}->{h};
542 $self->load_map ($hash, $x, $y); 575 $self->load_map ($hash, $x, $y);
543 $self->flood_fill (0, 0, 0, "", $hash, $flags); 576 $self->flood_fill (0, 0, 0, "", $hash, $flags);
544} 577}
545 578
546sub face_find { 579sub face_find {
547 my ($self, $facenum, $face) = @_; 580 my ($self, $facenum, $face, $cb) = @_;
548 581
549 my $hash = "$face->{chksum},$face->{name}"; 582 my $hash = "$face->{chksum},$face->{name}";
550 583
551 my $id = $self->{facemap}->get ($hash); 584 my $id = CFPlus::DB::get_tile_id_sync $hash;
552 585
553 unless ($id) {
554 # create new id for face
555 # I love transactions
556 for (1..100) {
557 my $txn = $CFPlus::DB_ENV->txn_begin;
558 my $status = $self->{facemap}->db_get (id => $id);
559 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
560 $id = ($id || 64) + 1;
561 if ($self->{facemap}->put (id => $id) == 0
562 && $self->{facemap}->put ($hash => $id) == 0) {
563 $txn->txn_commit;
564
565 goto gotid;
566 }
567 }
568 $txn->txn_abort;
569 }
570
571 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
572 }
573
574gotid:
575 $face->{id} = $id; 586 $face->{id} = $id;
576 $self->{map}->set_face ($facenum => $id);
577 $self->{faceid}[$facenum] = $id;#d# 587 $self->{faceid}[$facenum] = $id;
578 588
579 my $face = $self->{tilecache}->get ($id); 589 $self->{map}->set_tileid ($facenum => $id);
580 590
581 if ($face) { 591 CFPlus::DB::get tilecache => $id, $cb;
582 #$self->face_prefetch;
583 $face
584 } else {
585 my $tex = $self->{noface};
586 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
587 undef
588 };
589} 592}
590 593
591sub face_update { 594sub face_update {
592 my ($self, $facenum, $face, $changed) = @_; 595 my ($self, $facenum, $face, $changed) = @_;
593 596
594 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 597 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
598 if $changed;
595 599
596 $self->set_texture ($face->{id} => delete $face->{image}); 600 $self->set_texture ($face->{id} => delete $face->{image});
601}
602
603sub smooth_update {
604 my ($self, $facenum, $face) = @_;
605
606 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
597} 607}
598 608
599sub set_texture { 609sub set_texture {
600 my ($self, $id, $data) = @_; 610 my ($self, $id, $data) = @_;
601 611
602 $self->{texture}[$id] ||= do { 612 $self->{texture}[$id] = my $tex =
603 my $tex =
604 new_from_image CFPlus::Texture 613 new_from_image CFPlus::Texture
605 $data, minify => 1, mipmap => 1; 614 $data, minify => 1, mipmap => 1;
606 615
607 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 616 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
608 $self->{map_widget}->update; 617 $self->{map_widget}->update;
609
610 $tex
611 };
612} 618}
613 619
614sub sound_play { 620sub sound_play {
615 my ($self, $x, $y, $soundnum, $type) = @_; 621 my ($self, $x, $y, $soundnum, $type) = @_;
616 622
626 $LAST_QUERY = $prompt; 632 $LAST_QUERY = $prompt;
627 633
628 $self->{query}-> ($self, $flags, $prompt); 634 $self->{query}-> ($self, $flags, $prompt);
629} 635}
630 636
631sub drawinfo { 637our %NAME_TO_COLOR = (
632 my ($self, $color, $text) = @_; 638 black => 0,
639 white => 1,
640 darkblue => 2,
641 red => 3,
642 orange => 4,
643 lightblue => 5,
644 darkorange => 6,
645 green => 7,
646 darkgreen => 8,
647 grey => 9,
648 brown => 10,
649 yellow => 11,
650 tan => 12,
651);
633 652
634 my @color = ( 653our @CF_COLOR = (
635 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 654 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
636 [1.00, 1.00, 1.00], 655 [1.00, 1.00, 1.00],
637 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 656 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
638 [1.00, 0.00, 0.00], 657 [1.00, 0.00, 0.00],
639 [1.00, 0.54, 0.00], 658 [1.00, 0.54, 0.00],
640 [0.11, 0.56, 1.00], 659 [0.11, 0.56, 1.00],
641 [0.93, 0.46, 0.00], 660 [0.93, 0.46, 0.00],
642 [0.18, 0.54, 0.34], 661 [0.18, 0.54, 0.34],
643 [0.56, 0.73, 0.56], 662 [0.56, 0.73, 0.56],
644 [0.80, 0.80, 0.80], 663 [0.80, 0.80, 0.80],
645 [0.75, 0.61, 0.20], 664 [0.75, 0.61, 0.20],
646 [0.99, 0.77, 0.26], 665 [0.99, 0.77, 0.26],
647 [0.74, 0.65, 0.41], 666 [0.74, 0.65, 0.41],
648 ); 667);
649 668
650 my $fg = $color[$color % @color]; 669sub msg {
670 my ($self, $color, $type, $text, @extra) = @_;
651 671
672 if (my $cb = $self->{cb_msg}{$type}) {
673 $_->($self, $color, $type, $text, @extra) for values %$cb;
674 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
675 $type =~ s/-/_/g;
676 $self->{$type} = $text;
677 } else {
652 $self->logprint ("info: ", $text); 678 $self->logprint ("msg: ", $text);
679 return if $color < 0; # negative color == ignore if not understood
653 680
681 my $fg = $CF_COLOR[$color % @CF_COLOR];
682
654 # try to create single paragraphs of multiple lines sent by the server 683 ## try to create single paragraphs of multiple lines sent by the server
684 # no longer neecssary with TRT servers
655 $text =~ s/(?<=\S)\n(?=\w)/ /g; 685 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
656 686
657 $text = CFPlus::asxml $text;
658 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
659 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
660
661 ::message ({ fg => $fg, markup => $_ }) 687 ::message ({ fg => $fg, markup => $_ })
662 for split /\n/, $text; 688 for split /\n/, $text;
663 689
664 $self->{statusbox}->add ($text, 690 $self->{statusbox}->add ($text,
665 group => $text, 691 group => $text,
666 fg => $fg, 692 fg => $fg,
667 timeout => $color >= 2 ? 180 : 10, 693 timeout => $color >= 2 ? 180 : 10,
668 tooltip_font => $::FONT_FIXED, 694 tooltip_font => $::FONT_FIXED,
669 ); 695 );
670} 696 }
671
672sub drawextinfo {
673 my ($self, $color, $type, $subtype, $message) = @_;
674
675 $self->drawinfo ($color, $message);
676} 697}
677 698
678sub spell_add { 699sub spell_add {
679 my ($self, $spell) = @_; 700 my ($self, $spell) = @_;
680 701
913 $::SERVER_INFO->set_markup ( 934 $::SERVER_INFO->set_markup (
914 "server <tt>$self->{host}:$self->{port}</tt>\n" 935 "server <tt>$self->{host}:$self->{port}</tt>\n"
915 . "protocol version <tt>$self->{version}</tt>\n" 936 . "protocol version <tt>$self->{version}</tt>\n"
916 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 937 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
917 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 938 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
939 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
918 . "editing support $yesno[!!$self->{editor_support}]\n" 940 . "editing support $yesno[!!$self->{editor_support}]\n"
919 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n" 941 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
942 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
920 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 943 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
921 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 944 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
922 . "map size $self->{mapw}×$self->{maph}\n" 945 . "map size $self->{mapw}×$self->{maph}\n"
923 ); 946 );
924 947

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines