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.103 by root, Mon Apr 23 19:17:41 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 {
391 or return; 394 or return;
392 395
393 my ($hash, $x, $y, $w, $h) = @$map_info; 396 my ($hash, $x, $y, $w, $h) = @$map_info;
394 397
395 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 398 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
396 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 399 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
397 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 400 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
398} 401}
399 402
400sub map_clear { 403sub map_clear {
401 my ($self) = @_; 404 my ($self) = @_;
405 408
406 $self->{map}->clear; 409 $self->{map}->clear;
407 delete $self->{map_widget}{magicmap}; 410 delete $self->{map_widget}{magicmap};
408} 411}
409 412
413sub bg_fetch {
414 my ($self) = @_;
415
416 my $id;
417
418 do {
419 $id = pop @{$self->{bg_fetch}}
420 or return;
421 } while $self->{texture}[$id];
422
423 CFPlus::DB::get tilecache => $id, sub {
424 my ($data) = @_;
425
426 return unless $self->{map}; # stop when destroyed
427
428 $self->set_texture ($id => $data)
429 if defined $data;
430
431 $self->bg_fetch;
432 };
433}
410 434
411sub load_map($$$) { 435sub load_map($$$) {
412 my ($self, $hash, $x, $y) = @_; 436 my ($self, $hash, $x, $y) = @_;
413 437
414 if (defined (my $data = $self->{mapcache}->get ($hash))) { 438 my $gen = $self->{map_change_gen};
439
440 CFPlus::DB::get $self->{mapcache} => $hash, sub {
441 return unless $gen == $self->{map_change_gen};
442
443 my ($data) = @_;
444
445 if (defined $data) {
415 $data = Compress::LZF::decompress $data; 446 $data = Compress::LZF::decompress $data;
416 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 447 #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 448
421 $self->set_texture ($id => $data); 449 my $inprogress = @{ $self->{bg_fetch} || [] };
450 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
451 $self->bg_fetch unless $inprogress;
422 } 452 }
423 } 453 };
424} 454}
425 455
426# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 456# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
427# (server resource,s latency, bandwidth), so this hack is warranted. 457# (server resource,s latency, bandwidth), so this hack is warranted.
428# the right fix is to make real tiled maps with an overview file 458# the right fix is to make real tiled maps with an overview file
492 522
493 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 523 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
494 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 524 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
495 525
496 } else { 526 } else {
527 my $gen = $self->{map_change_gen};
497 $self->send_mapinfo ("spatial $path$tile", sub { 528 $self->send_mapinfo ("spatial $path$tile", sub {
529 return unless $gen == $self->{map_change_gen};
530
498 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 531 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
499 532
500 return if $mode ne "spatial"; 533 return if $mode ne "spatial";
501 534
502 $x += $self->{map}->ox; 535 $x += $self->{map}->ox;
516 549
517sub map_change { 550sub map_change {
518 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 551 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
519 552
520 $self->flush_map; 553 $self->flush_map;
554
555 ++$self->{map_change_gen};
521 556
522 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 557 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
523 558
524 my $mapmapw = $self->{mapmap}->{w}; 559 my $mapmapw = $self->{mapmap}->{w};
525 my $mapmaph = $self->{mapmap}->{h}; 560 my $mapmaph = $self->{mapmap}->{h};
542 $self->load_map ($hash, $x, $y); 577 $self->load_map ($hash, $x, $y);
543 $self->flood_fill (0, 0, 0, "", $hash, $flags); 578 $self->flood_fill (0, 0, 0, "", $hash, $flags);
544} 579}
545 580
546sub face_find { 581sub face_find {
547 my ($self, $facenum, $face) = @_; 582 my ($self, $facenum, $face, $cb) = @_;
548 583
549 my $hash = "$face->{chksum},$face->{name}"; 584 my $hash = "$face->{chksum},$face->{name}";
550 585
551 my $id = $self->{facemap}->get ($hash); 586 my $id = CFPlus::DB::get_tile_id_sync $hash;
552 587
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; 588 $face->{id} = $id;
576 $self->{map}->set_face ($facenum => $id);
577 $self->{faceid}[$facenum] = $id;#d# 589 $self->{faceid}[$facenum] = $id;
578 590
579 my $face = $self->{tilecache}->get ($id); 591 $self->{map}->set_tileid ($facenum => $id);
580 592
581 if ($face) { 593 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} 594}
590 595
591sub face_update { 596sub face_update {
592 my ($self, $facenum, $face, $changed) = @_; 597 my ($self, $facenum, $face, $changed) = @_;
593 598
594 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 599 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
600 if $changed;
595 601
596 $self->set_texture ($face->{id} => delete $face->{image}); 602 $self->set_texture ($face->{id} => delete $face->{image});
603}
604
605sub smooth_update {
606 my ($self, $facenum, $face) = @_;
607
608 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
597} 609}
598 610
599sub set_texture { 611sub set_texture {
600 my ($self, $id, $data) = @_; 612 my ($self, $id, $data) = @_;
601 613
602 $self->{texture}[$id] ||= do { 614 $self->{texture}[$id] = my $tex =
603 my $tex =
604 new_from_image CFPlus::Texture 615 new_from_image CFPlus::Texture
605 $data, minify => 1, mipmap => 1; 616 $data, minify => 1, mipmap => 1;
606 617
607 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 618 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
608 $self->{map_widget}->update; 619 $self->{map_widget}->update;
609
610 $tex
611 };
612} 620}
613 621
614sub sound_play { 622sub sound_play {
615 my ($self, $x, $y, $soundnum, $type) = @_; 623 my ($self, $x, $y, $soundnum, $type) = @_;
616 624
626 $LAST_QUERY = $prompt; 634 $LAST_QUERY = $prompt;
627 635
628 $self->{query}-> ($self, $flags, $prompt); 636 $self->{query}-> ($self, $flags, $prompt);
629} 637}
630 638
639our @CF_COLOR = (
640 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
641 [1.00, 1.00, 1.00],
642 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
643 [1.00, 0.00, 0.00],
644 [1.00, 0.54, 0.00],
645 [0.11, 0.56, 1.00],
646 [0.93, 0.46, 0.00],
647 [0.18, 0.54, 0.34],
648 [0.56, 0.73, 0.56],
649 [0.80, 0.80, 0.80],
650 [0.75, 0.61, 0.20],
651 [0.99, 0.77, 0.26],
652 [0.74, 0.65, 0.41],
653);
654
631sub drawinfo { 655sub drawinfo {
632 my ($self, $color, $text) = @_; 656 my ($self, $color, $text) = @_;
633 657
634 my @color = ( 658 my $fg = $CF_COLOR[$color % @CF_COLOR];
635 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
636 [1.00, 1.00, 1.00],
637 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
638 [1.00, 0.00, 0.00],
639 [1.00, 0.54, 0.00],
640 [0.11, 0.56, 1.00],
641 [0.93, 0.46, 0.00],
642 [0.18, 0.54, 0.34],
643 [0.56, 0.73, 0.56],
644 [0.80, 0.80, 0.80],
645 [0.75, 0.61, 0.20],
646 [0.99, 0.77, 0.26],
647 [0.74, 0.65, 0.41],
648 );
649
650 my $fg = $color[$color % @color];
651 659
652 $self->logprint ("info: ", $text); 660 $self->logprint ("info: ", $text);
653 661
654 # try to create single paragraphs of multiple lines sent by the server 662 ## try to create single paragraphs of multiple lines sent by the server
663 # no longer neecssary with TRT servers
655 $text =~ s/(?<=\S)\n(?=\w)/ /g; 664 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
656 665
657 $text = CFPlus::asxml $text; 666 $text = CFPlus::asxml $text;
658 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 667 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
659 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 668 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
660 669

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines