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.93 by root, Fri Dec 22 03:43:05 2006 UTC vs.
Revision 1.100 by root, Tue Apr 10 11:12:53 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
51 $self->connect_ext (event_capabilities => sub {
52 my ($cap) = @_;
53
54 if (my $ts = $cap->{tileset}) {
55 if (my ($default) = grep $_->[2] & 1, @$ts) {
56 $self->{tileset} = $default;
57 $self->{tilesize} = $default->[3];
58 $self->setup_req (tileset => $default->[0]);
59
60 my $w = int $self->{mapw} * 32 / $self->{tilesize};
61 my $h = int $self->{maph} * 32 / $self->{tilesize};
62
63 $self->setup_req (mapsize => "${w}x${h}");
64 }
65 }
66 });
67
45 $self->{map_widget}->add_command (@$_) 68 $self->{map_widget}->add_command (@$_)
46 for @cmd_help; 69 for @cmd_help;
47
48 $self->{noface} = new_from_file CFPlus::Texture
49 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
50 70
51 { 71 {
52 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 72 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
53 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 73 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
54 $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}});
55 } 75 }
56 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
57 $self->{open_container} = 0; 83 $self->{open_container} = 0;
58 84
59 # "global"
60 $self->{tilecache} = CFPlus::db_table "tilecache"
61 or die "tilecache: unable to open database table";
62 $self->{facemap} = CFPlus::db_table "facemap"
63 or die "facemap: unable to open database table";
64
65 # per server 85 # per server
66 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 86 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
67 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
68 87
69 $self 88 $self
70} 89}
71 90
72sub logprint { 91sub logprint {
374 or return; 393 or return;
375 394
376 my ($hash, $x, $y, $w, $h) = @$map_info; 395 my ($hash, $x, $y, $w, $h) = @$map_info;
377 396
378 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 397 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
379 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 398 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
380 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 399 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
381} 400}
382 401
383sub map_clear { 402sub map_clear {
384 my ($self) = @_; 403 my ($self) = @_;
388 407
389 $self->{map}->clear; 408 $self->{map}->clear;
390 delete $self->{map_widget}{magicmap}; 409 delete $self->{map_widget}{magicmap};
391} 410}
392 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}
393 433
394sub load_map($$$) { 434sub load_map($$$) {
395 my ($self, $hash, $x, $y) = @_; 435 my ($self, $hash, $x, $y) = @_;
396 436
397 if (defined (my $data = $self->{mapcache}->get ($hash))) { 437 my $gen = $self->{map_change_gen};
438
439 CFPlus::DB::get $self->{mapcache} => $hash, sub {
440 return unless $gen == $self->{map_change_gen};
441
442 my ($data) = @_;
443
444 if (defined $data) {
398 $data = Compress::LZF::decompress $data; 445 $data = Compress::LZF::decompress $data;
399 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 446 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
400 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
401 my $data = $self->{tilecache}->get ($id)
402 or next;
403 447
404 $self->set_texture ($id => $data); 448 my $inprogress = @{ $self->{bg_fetch} || [] };
449 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
450 $self->bg_fetch unless $inprogress;
405 } 451 }
406 } 452 };
407} 453}
408 454
409# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 455# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
410# (server resource,s latency, bandwidth), so this hack is warranted. 456# (server resource,s latency, bandwidth), so this hack is warranted.
411# the right fix is to make real tiled maps with an overview file 457# the right fix is to make real tiled maps with an overview file
475 521
476 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 522 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
477 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 523 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
478 524
479 } else { 525 } else {
526 my $gen = $self->{map_change_gen};
480 $self->send_mapinfo ("spatial $path$tile", sub { 527 $self->send_mapinfo ("spatial $path$tile", sub {
528 return unless $gen == $self->{map_change_gen};
529
481 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 530 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
482 531
483 return if $mode ne "spatial"; 532 return if $mode ne "spatial";
484 533
485 $x += $self->{map}->ox; 534 $x += $self->{map}->ox;
499 548
500sub map_change { 549sub map_change {
501 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 550 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
502 551
503 $self->flush_map; 552 $self->flush_map;
553
554 ++$self->{map_change_gen};
504 555
505 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 556 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
506 557
507 my $mapmapw = $self->{mapmap}->{w}; 558 my $mapmapw = $self->{mapmap}->{w};
508 my $mapmaph = $self->{mapmap}->{h}; 559 my $mapmaph = $self->{mapmap}->{h};
525 $self->load_map ($hash, $x, $y); 576 $self->load_map ($hash, $x, $y);
526 $self->flood_fill (0, 0, 0, "", $hash, $flags); 577 $self->flood_fill (0, 0, 0, "", $hash, $flags);
527} 578}
528 579
529sub face_find { 580sub face_find {
530 my ($self, $facenum, $face) = @_; 581 my ($self, $facenum, $face, $cb) = @_;
531 582
532 my $hash = "$face->{chksum},$face->{name}"; 583 my $hash = "$face->{chksum},$face->{name}";
533 584
534 my $id = $self->{facemap}->get ($hash); 585 my $id = CFPlus::DB::get_tile_id_sync $hash;
535 586
536 unless ($id) {
537 # create new id for face
538 # I love transactions
539 for (1..100) {
540 my $txn = $CFPlus::DB_ENV->txn_begin;
541 my $status = $self->{facemap}->db_get (id => $id);
542 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
543 $id = ($id || 64) + 1;
544 if ($self->{facemap}->put (id => $id) == 0
545 && $self->{facemap}->put ($hash => $id) == 0) {
546 $txn->txn_commit;
547
548 goto gotid;
549 }
550 }
551 $txn->txn_abort;
552 }
553
554 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
555 }
556
557gotid:
558 $face->{id} = $id; 587 $face->{id} = $id;
559 $self->{map}->set_face ($facenum => $id);
560 $self->{faceid}[$facenum] = $id;#d# 588 $self->{faceid}[$facenum] = $id;
561 589
562 my $face = $self->{tilecache}->get ($id); 590 $self->{map}->set_tileid ($facenum => $id);
563 591
564 if ($face) { 592 CFPlus::DB::get tilecache => $id, $cb;
565 #$self->face_prefetch;
566 $face
567 } else {
568 my $tex = $self->{noface};
569 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
570 undef
571 };
572} 593}
573 594
574sub face_update { 595sub face_update {
575 my ($self, $facenum, $face, $changed) = @_; 596 my ($self, $facenum, $face, $changed) = @_;
576 597
577 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 598 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
599 if $changed;
578 600
579 $self->set_texture ($face->{id} => delete $face->{image}); 601 $self->set_texture ($face->{id} => delete $face->{image});
602}
603
604sub smooth_update {
605 my ($self, $facenum, $face) = @_;
606
607 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
580} 608}
581 609
582sub set_texture { 610sub set_texture {
583 my ($self, $id, $data) = @_; 611 my ($self, $id, $data) = @_;
584 612
585 $self->{texture}[$id] ||= do { 613 $self->{texture}[$id] = my $tex =
586 my $tex =
587 new_from_image CFPlus::Texture 614 new_from_image CFPlus::Texture
588 $data, minify => 1, mipmap => 1; 615 $data, minify => 1, mipmap => 1;
589 616
590 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 617 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
591 $self->{map_widget}->update; 618 $self->{map_widget}->update;
592
593 $tex
594 };
595} 619}
596 620
597sub sound_play { 621sub sound_play {
598 my ($self, $x, $y, $soundnum, $type) = @_; 622 my ($self, $x, $y, $soundnum, $type) = @_;
599 623
679} 703}
680 704
681sub setup { 705sub setup {
682 my ($self, $setup) = @_; 706 my ($self, $setup) = @_;
683 707
708 $self->{map_widget}->set_tilesize ($self->{tilesize});
684 $::MAP->resize ($self->{mapw}, $self->{maph}); 709 $::MAP->resize ($self->{mapw}, $self->{maph});
685} 710}
686 711
687sub addme_success { 712sub addme_success {
688 my ($self) = @_; 713 my ($self) = @_;
928 953
929 $self->update_server_info; 954 $self->update_server_info;
930 955
931 $self->send_command ("output-sync $::CFG->{output_sync}"); 956 $self->send_command ("output-sync $::CFG->{output_sync}");
932 $self->send_command ("output-count $::CFG->{output_count}"); 957 $self->send_command ("output-count $::CFG->{output_count}");
958 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
933 $self->send_command ("pickup $::CFG->{pickup}"); 959 $self->send_command ("pickup $::CFG->{pickup}");
934} 960}
935 961
936sub buildat { 962sub buildat {
937 my ($self, $builditem, $x, $y) = @_; 963 my ($self, $builditem, $x, $y) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines