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.99 by root, Tue Apr 10 09:39:48 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
16use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
17 18
18sub new { 19sub new {
19 my $class = shift; 20 my $class = shift;
20 21
21 my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); 22 my $self = $class->SUPER::new (@_,
23 setup_req => {
24 extmap => 1,
25 smoothing => 1,
26 },
27 );
22 28
23 $self->{map_widget}->clr_commands; 29 $self->{map_widget}->clr_commands;
24 30
25 my @cmd_help = map { 31 my @cmd_help = map {
26 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 32 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
37 for @args; 43 for @args;
38 44
39 map ["$cmd$_", $text], 45 map ["$cmd$_", $text],
40 sort { (length $a) <=> (length $b) } 46 sort { (length $a) <=> (length $b) }
41 @args 47 @args
42 } sort { $a->{par} <=> $b->{par} } 48 } sort { $a->{par} <=> $b->{par} }
43 CFPlus::Pod::find command => "*"; 49 CFPlus::Pod::find command => "*";
44 50
45 $self->connect_ext (event_capabilities => sub { 51 $self->connect_ext (event_capabilities => sub {
46 my ($cap) = @_; 52 my ($cap) = @_;
47 53
48 if (my $ts = $cap->{tileset}) { 54 if (my $ts = $cap->{tileset}) {
49 if (my ($default) = grep $_->[2] & 1, @$ts) { 55 if (my ($default) = grep $_->[2] & 1, @$ts) {
50 $self->{tileset} = $default; 56 $self->{tileset} = $default;
51 $self->{tilesize} = $default->[3]; 57 $self->{tilesize} = $default->[3];
52 $self->setup_req (tileset => $default->[0]); 58 $self->setup_req (tileset => $default->[0]);
53 59
54 my $w = int $self->{mapw} * 32 / $self->{tilesize}; 60 my $w = int $self->{mapw} * 32 / $self->{tilesize};
55 my $h = int $self->{maph} * 32 / $self->{tilesize}; 61 my $h = int $self->{maph} * 32 / $self->{tilesize};
56 62
57 $self->setup_req (mapsize => "${w}x${h}"); 63 $self->setup_req (mapsize => "${w}x${h}");
58 } 64 }
59 } 65 }
60 }); 66 });
61 67
62 $self->{map_widget}->add_command (@$_) 68 $self->{map_widget}->add_command (@$_)
63 for @cmd_help; 69 for @cmd_help;
64
65 $self->{noface} = new_from_file CFPlus::Texture
66 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
67 70
68 { 71 {
69 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 72 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
70 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 73 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
71 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 74 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
72 } 75 }
73 76
77 {
78 $self->{noface} = my $tex = new_from_file CFPlus::Texture
79 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
80 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
81 }
82
74 $self->{open_container} = 0; 83 $self->{open_container} = 0;
75 84
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 85 # per server
83 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 86 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
84 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
85 87
86 $self 88 $self
87} 89}
88 90
89sub logprint { 91sub logprint {
391 or return; 393 or return;
392 394
393 my ($hash, $x, $y, $w, $h) = @$map_info; 395 my ($hash, $x, $y, $w, $h) = @$map_info;
394 396
395 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 397 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
396 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 398 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
397 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 399 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
398} 400}
399 401
400sub map_clear { 402sub map_clear {
401 my ($self) = @_; 403 my ($self) = @_;
405 407
406 $self->{map}->clear; 408 $self->{map}->clear;
407 delete $self->{map_widget}{magicmap}; 409 delete $self->{map_widget}{magicmap};
408} 410}
409 411
412sub bg_fetch {
413 my ($self) = @_;
414
415 my $id;
416
417 do {
418 $id = pop @{$self->{bg_fetch}}
419 or return;
420 } while $self->{texture}[$id];
421
422 CFPlus::DB::get tilecache => $id, sub {
423 my ($data) = @_;
424
425 return unless $self->{map}; # stop when destroyed
426
427 $self->set_texture ($id => $data)
428 if defined $data;
429
430 $self->bg_fetch;
431 };
432}
410 433
411sub load_map($$$) { 434sub load_map($$$) {
412 my ($self, $hash, $x, $y) = @_; 435 my ($self, $hash, $x, $y) = @_;
413 436
414 if (defined (my $data = $self->{mapcache}->get ($hash))) { 437 CFPlus::DB::get $self->{mapcache} => $hash, sub {
438 my ($data) = @_;
439
440 if (defined $data) {
415 $data = Compress::LZF::decompress $data; 441 $data = Compress::LZF::decompress $data;
416 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 442 #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 443
421 $self->set_texture ($id => $data); 444 my $inprogress = @{ $self->{bg_fetch} || [] };
445 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
446 $self->bg_fetch unless $inprogress;
422 } 447 }
423 } 448 };
424} 449}
425 450
426# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 451# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
427# (server resource,s latency, bandwidth), so this hack is warranted. 452# (server resource,s latency, bandwidth), so this hack is warranted.
428# the right fix is to make real tiled maps with an overview file 453# the right fix is to make real tiled maps with an overview file
542 $self->load_map ($hash, $x, $y); 567 $self->load_map ($hash, $x, $y);
543 $self->flood_fill (0, 0, 0, "", $hash, $flags); 568 $self->flood_fill (0, 0, 0, "", $hash, $flags);
544} 569}
545 570
546sub face_find { 571sub face_find {
547 my ($self, $facenum, $face) = @_; 572 my ($self, $facenum, $face, $cb) = @_;
548 573
549 my $hash = "$face->{chksum},$face->{name}"; 574 my $hash = "$face->{chksum},$face->{name}";
550 575
551 my $id = $self->{facemap}->get ($hash); 576 my $id = CFPlus::DB::get_tile_id_sync $hash;
552 577
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; 578 $face->{id} = $id;
576 $self->{map}->set_face ($facenum => $id);
577 $self->{faceid}[$facenum] = $id;#d# 579 $self->{faceid}[$facenum] = $id;
578 580
579 my $face = $self->{tilecache}->get ($id); 581 $self->{map}->set_tileid ($facenum => $id);
580 582
581 if ($face) { 583 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} 584}
590 585
591sub face_update { 586sub face_update {
592 my ($self, $facenum, $face, $changed) = @_; 587 my ($self, $facenum, $face, $changed) = @_;
593 588
594 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 589 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
590 if $changed;
595 591
596 $self->set_texture ($face->{id} => delete $face->{image}); 592 $self->set_texture ($face->{id} => delete $face->{image});
593}
594
595sub smooth_update {
596 my ($self, $facenum, $face) = @_;
597
598 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
597} 599}
598 600
599sub set_texture { 601sub set_texture {
600 my ($self, $id, $data) = @_; 602 my ($self, $id, $data) = @_;
601 603
602 $self->{texture}[$id] ||= do { 604 $self->{texture}[$id] = my $tex =
603 my $tex =
604 new_from_image CFPlus::Texture 605 new_from_image CFPlus::Texture
605 $data, minify => 1, mipmap => 1; 606 $data, minify => 1, mipmap => 1;
606 607
607 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 608 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
608 $self->{map_widget}->update; 609 $self->{map_widget}->update;
609
610 $tex
611 };
612} 610}
613 611
614sub sound_play { 612sub sound_play {
615 my ($self, $x, $y, $soundnum, $type) = @_; 613 my ($self, $x, $y, $soundnum, $type) = @_;
616 614

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines