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.96 by root, Fri Apr 6 07:45:35 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
37 for @args; 38 for @args;
38 39
39 map ["$cmd$_", $text], 40 map ["$cmd$_", $text],
40 sort { (length $a) <=> (length $b) } 41 sort { (length $a) <=> (length $b) }
41 @args 42 @args
42 } sort { $a->{par} <=> $b->{par} } 43 } sort { $a->{par} <=> $b->{par} }
43 CFPlus::Pod::find command => "*"; 44 CFPlus::Pod::find command => "*";
44 45
45 $self->connect_ext (event_capabilities => sub { 46 $self->connect_ext (event_capabilities => sub {
46 my ($cap) = @_; 47 my ($cap) = @_;
47 48
48 if (my $ts = $cap->{tileset}) { 49 if (my $ts = $cap->{tileset}) {
49 if (my ($default) = grep $_->[2] & 1, @$ts) { 50 if (my ($default) = grep $_->[2] & 1, @$ts) {
50 $self->{tileset} = $default; 51 $self->{tileset} = $default;
51 $self->{tilesize} = $default->[3]; 52 $self->{tilesize} = $default->[3];
52 $self->setup_req (tileset => $default->[0]); 53 $self->setup_req (tileset => $default->[0]);
53 54
54 my $w = int $self->{mapw} * 32 / $self->{tilesize}; 55 my $w = int $self->{mapw} * 32 / $self->{tilesize};
55 my $h = int $self->{maph} * 32 / $self->{tilesize}; 56 my $h = int $self->{maph} * 32 / $self->{tilesize};
56 57
57 $self->setup_req (mapsize => "${w}x${h}"); 58 $self->setup_req (mapsize => "${w}x${h}");
58 } 59 }
59 } 60 }
60 }); 61 });
61 62
62 $self->{map_widget}->add_command (@$_) 63 $self->{map_widget}->add_command (@$_)
63 for @cmd_help; 64 for @cmd_help;
64
65 $self->{noface} = new_from_file CFPlus::Texture
66 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
67 65
68 { 66 {
69 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 67 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
70 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 68 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
71 $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}});
72 } 70 }
73 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
74 $self->{open_container} = 0; 78 $self->{open_container} = 0;
75 79
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 80 # per server
83 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 81 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
84 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
85 82
86 $self 83 $self
87} 84}
88 85
89sub logprint { 86sub logprint {
391 or return; 388 or return;
392 389
393 my ($hash, $x, $y, $w, $h) = @$map_info; 390 my ($hash, $x, $y, $w, $h) = @$map_info;
394 391
395 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 392 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
396 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 393 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
397 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 394 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
398} 395}
399 396
400sub map_clear { 397sub map_clear {
401 my ($self) = @_; 398 my ($self) = @_;
409 406
410 407
411sub load_map($$$) { 408sub load_map($$$) {
412 my ($self, $hash, $x, $y) = @_; 409 my ($self, $hash, $x, $y) = @_;
413 410
414 if (defined (my $data = $self->{mapcache}->get ($hash))) { 411 CFPlus::DB::get $self->{mapcache} => $hash, sub {
412 my ($data) = @_;
413
414 if (defined $data) {
415 $data = Compress::LZF::decompress $data; 415 $data = Compress::LZF::decompress $data;
416 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 416 #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)) { 417 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
418 my $data = $self->{tilecache}->get ($id) 418 CFPlus::DB::get tilecache => $id, sub {
419 or next; 419 my ($data) = @_;
420 420
421 $self->set_texture ($id => $data); 421 $self->set_texture ($id => $data)
422 if defined $data;
423 };
424 }
422 } 425 }
423 } 426 };
424} 427}
425 428
426# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 429# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
427# (server resource,s latency, bandwidth), so this hack is warranted. 430# (server resource,s latency, bandwidth), so this hack is warranted.
428# the right fix is to make real tiled maps with an overview file 431# the right fix is to make real tiled maps with an overview file
542 $self->load_map ($hash, $x, $y); 545 $self->load_map ($hash, $x, $y);
543 $self->flood_fill (0, 0, 0, "", $hash, $flags); 546 $self->flood_fill (0, 0, 0, "", $hash, $flags);
544} 547}
545 548
546sub face_find { 549sub face_find {
547 my ($self, $facenum, $face) = @_; 550 my ($self, $facenum, $face, $cb) = @_;
548 551
549 my $hash = "$face->{chksum},$face->{name}"; 552 my $hash = "$face->{chksum},$face->{name}";
550 553
551 my $id = $self->{facemap}->get ($hash); 554 my $id = CFPlus::DB::get_tile_id_sync $hash;
552 555
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; 556 $face->{id} = $id;
576 $self->{map}->set_face ($facenum => $id);
577 $self->{faceid}[$facenum] = $id;#d# 557 $self->{faceid}[$facenum] = $id;
578 558
579 my $face = $self->{tilecache}->get ($id); 559 $self->{map}->set_tileid ($facenum => $id);
580 560
581 if ($face) { 561 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} 562}
590 563
591sub face_update { 564sub face_update {
592 my ($self, $facenum, $face, $changed) = @_; 565 my ($self, $facenum, $face, $changed) = @_;
593 566
594 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 567 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
568 if $changed;
595 569
596 $self->set_texture ($face->{id} => delete $face->{image}); 570 $self->set_texture ($face->{id} => delete $face->{image});
597} 571}
598 572
599sub set_texture { 573sub set_texture {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines