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.90 by root, Wed Dec 6 00:15:12 2006 UTC vs.
Revision 1.101 by root, Thu Apr 12 07:53:31 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;
12use CFPlus::Macro;
13use CFPlus::Item;
11 14
12use Crossfire::Protocol::Base 0.95; 15use Crossfire::Protocol::Base 0.95;
13 16
14use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
15 18
16sub new { 19sub new {
17 my $class = shift; 20 my ($class, %arg) = @_;
18 21
19 my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); 22 my $self = $class->SUPER::new (%arg,
23 setup_req => {
24 extmap => 1,
25 %{$arg{setup_req} || {}},
26 },
27 );
20 28
21 $self->{map_widget}->clr_commands; 29 $self->{map_widget}->clr_commands;
22 30
23 my @cmd_help = map { 31 my @cmd_help = map {
24 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 32 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
35 for @args; 43 for @args;
36 44
37 map ["$cmd$_", $text], 45 map ["$cmd$_", $text],
38 sort { (length $a) <=> (length $b) } 46 sort { (length $a) <=> (length $b) }
39 @args 47 @args
40 } sort { $a->{par} <=> $b->{par} } 48 } sort { $a->{par} <=> $b->{par} }
41 CFPlus::Pod::find command => "*"; 49 CFPlus::Pod::find command => "*";
42 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
43 $self->{map_widget}->add_command (@$_) 68 $self->{map_widget}->add_command (@$_)
44 for @cmd_help; 69 for @cmd_help;
45
46 $self->{noface} = new_from_file CFPlus::Texture
47 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
48 70
49 { 71 {
50 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 72 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
51 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 73 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
52 $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}});
53 } 75 }
54 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
55 $self->{open_container} = 0; 83 $self->{open_container} = 0;
56 84
57 # "global"
58 $self->{tilecache} = CFPlus::db_table "tilecache"
59 or die "tilecache: unable to open database table";
60 $self->{facemap} = CFPlus::db_table "facemap"
61 or die "facemap: unable to open database table";
62
63 # per server 85 # per server
64 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 86 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
65 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
66 87
67 $self 88 $self
68} 89}
69 90
70sub logprint { 91sub logprint {
291 $::CONN->user_send ("use_skill $name"); 312 $::CONN->user_send ("use_skill $name");
292 } elsif ($ev->{button} == 3) { 313 } elsif ($ev->{button} == 3) {
293 my $shortname = CFPlus::shorten $name, 14; 314 my $shortname = CFPlus::shorten $name, 14;
294 (new CFPlus::UI::Menu 315 (new CFPlus::UI::Menu
295 items => [ 316 items => [
296 ["bind <i>ready_skill $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["ready_skill $name"]) }], 317 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
297 ["bind <i>use_skill $shortname</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["use_skill $name"]) }], 318 ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
298 ], 319 ],
299 )->popup ($ev); 320 )->popup ($ev);
300 } else { 321 } else {
301 return 0; 322 return 0;
302 } 323 }
319 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 340 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
320 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 341 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
321 } 342 }
322} 343}
323 344
345sub macro_send {
346 my ($self, $macro) = @_;
347
348 for my $cmd (@{ $macro->{action} }) {
349 $self->send_command ($cmd);
350 }
351}
352
324sub user_send { 353sub user_send {
325 my ($self, $command) = @_; 354 my ($self, $command) = @_;
326 355
327 push @{$self->{record}}, $command 356 $self->{record}->($command)
328 if $self->{record}; 357 if $self->{record};
329 358
330 $self->logprint ("send: ", $command); 359 $self->logprint ("send: ", $command);
331 $self->send_command ($command); 360 $self->send_command ($command);
332 ::status ($command); 361 ::status ($command);
333} 362}
334 363
335sub start_record { 364sub record {
336 my ($self) = @_; 365 my ($self, $cb) = @_;
337 366
338 $self->{record} = []; 367 $self->{record} = $cb;
339}
340
341sub stop_record {
342 my ($self) = @_;
343 return delete $self->{record};
344} 368}
345 369
346sub map_scroll { 370sub map_scroll {
347 my ($self, $dx, $dy) = @_; 371 my ($self, $dx, $dy) = @_;
348 372
369 or return; 393 or return;
370 394
371 my ($hash, $x, $y, $w, $h) = @$map_info; 395 my ($hash, $x, $y, $w, $h) = @$map_info;
372 396
373 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 397 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
374 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 398 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
375 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 399 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
376} 400}
377 401
378sub map_clear { 402sub map_clear {
379 my ($self) = @_; 403 my ($self) = @_;
383 407
384 $self->{map}->clear; 408 $self->{map}->clear;
385 delete $self->{map_widget}{magicmap}; 409 delete $self->{map_widget}{magicmap};
386} 410}
387 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}
388 433
389sub load_map($$$) { 434sub load_map($$$) {
390 my ($self, $hash, $x, $y) = @_; 435 my ($self, $hash, $x, $y) = @_;
391 436
392 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) {
393 $data = Compress::LZF::decompress $data; 445 $data = Compress::LZF::decompress $data;
394 #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#
395 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
396 my $data = $self->{tilecache}->get ($id)
397 or next;
398 447
399 $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;
400 } 451 }
401 } 452 };
402} 453}
403 454
404# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 455# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
405# (server resource,s latency, bandwidth), so this hack is warranted. 456# (server resource,s latency, bandwidth), so this hack is warranted.
406# 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
470 521
471 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 522 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
472 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 523 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
473 524
474 } else { 525 } else {
526 my $gen = $self->{map_change_gen};
475 $self->send_mapinfo ("spatial $path$tile", sub { 527 $self->send_mapinfo ("spatial $path$tile", sub {
528 return unless $gen == $self->{map_change_gen};
529
476 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 530 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
477 531
478 return if $mode ne "spatial"; 532 return if $mode ne "spatial";
479 533
480 $x += $self->{map}->ox; 534 $x += $self->{map}->ox;
494 548
495sub map_change { 549sub map_change {
496 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 550 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
497 551
498 $self->flush_map; 552 $self->flush_map;
553
554 ++$self->{map_change_gen};
499 555
500 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 556 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
501 557
502 my $mapmapw = $self->{mapmap}->{w}; 558 my $mapmapw = $self->{mapmap}->{w};
503 my $mapmaph = $self->{mapmap}->{h}; 559 my $mapmaph = $self->{mapmap}->{h};
520 $self->load_map ($hash, $x, $y); 576 $self->load_map ($hash, $x, $y);
521 $self->flood_fill (0, 0, 0, "", $hash, $flags); 577 $self->flood_fill (0, 0, 0, "", $hash, $flags);
522} 578}
523 579
524sub face_find { 580sub face_find {
525 my ($self, $facenum, $face) = @_; 581 my ($self, $facenum, $face, $cb) = @_;
526 582
527 my $hash = "$face->{chksum},$face->{name}"; 583 my $hash = "$face->{chksum},$face->{name}";
528 584
529 my $id = $self->{facemap}->get ($hash); 585 my $id = CFPlus::DB::get_tile_id_sync $hash;
530 586
531 unless ($id) {
532 # create new id for face
533 # I love transactions
534 for (1..100) {
535 my $txn = $CFPlus::DB_ENV->txn_begin;
536 my $status = $self->{facemap}->db_get (id => $id);
537 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
538 $id = ($id || 64) + 1;
539 if ($self->{facemap}->put (id => $id) == 0
540 && $self->{facemap}->put ($hash => $id) == 0) {
541 $txn->txn_commit;
542
543 goto gotid;
544 }
545 }
546 $txn->txn_abort;
547 }
548
549 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
550 }
551
552gotid:
553 $face->{id} = $id; 587 $face->{id} = $id;
554 $self->{map}->set_face ($facenum => $id);
555 $self->{faceid}[$facenum] = $id;#d# 588 $self->{faceid}[$facenum] = $id;
556 589
557 my $face = $self->{tilecache}->get ($id); 590 $self->{map}->set_tileid ($facenum => $id);
558 591
559 if ($face) { 592 CFPlus::DB::get tilecache => $id, $cb;
560 #$self->face_prefetch;
561 $face
562 } else {
563 my $tex = $self->{noface};
564 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
565 undef
566 };
567} 593}
568 594
569sub face_update { 595sub face_update {
570 my ($self, $facenum, $face, $changed) = @_; 596 my ($self, $facenum, $face, $changed) = @_;
571 597
572 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 598 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
599 if $changed;
573 600
574 $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});
575} 608}
576 609
577sub set_texture { 610sub set_texture {
578 my ($self, $id, $data) = @_; 611 my ($self, $id, $data) = @_;
579 612
580 $self->{texture}[$id] ||= do { 613 $self->{texture}[$id] = my $tex =
581 my $tex =
582 new_from_image CFPlus::Texture 614 new_from_image CFPlus::Texture
583 $data, minify => 1, mipmap => 1; 615 $data, minify => 1, mipmap => 1;
584 616
585 $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}});
586 $self->{map_widget}->update; 618 $self->{map_widget}->update;
587
588 $tex
589 };
590} 619}
591 620
592sub sound_play { 621sub sound_play {
593 my ($self, $x, $y, $soundnum, $type) = @_; 622 my ($self, $x, $y, $soundnum, $type) = @_;
594 623
623 [0.75, 0.61, 0.20], 652 [0.75, 0.61, 0.20],
624 [0.99, 0.77, 0.26], 653 [0.99, 0.77, 0.26],
625 [0.74, 0.65, 0.41], 654 [0.74, 0.65, 0.41],
626 ); 655 );
627 656
657 my $fg = $color[$color % @color];
658
628 $self->logprint ("info: ", $text); 659 $self->logprint ("info: ", $text);
629 660
630 # try to create single paragraphs of multiple lines sent by the server 661 # try to create single paragraphs of multiple lines sent by the server
631 $text =~ s/(?<=\S)\n(?=\w)/ /g; 662 $text =~ s/(?<=\S)\n(?=\w)/ /g;
632 663
633 $text = CFPlus::asxml $text; 664 $text = CFPlus::asxml $text;
634 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 665 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
635 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 666 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
636 667
637 ::message ({ fg => $color[$color], markup => $_ }) 668 ::message ({ fg => $fg, markup => $_ })
638 for split /\n/, $text; 669 for split /\n/, $text;
639 670
640 $self->{statusbox}->add ($text, 671 $self->{statusbox}->add ($text,
641 group => $text, 672 group => $text,
642 fg => $color[$color], 673 fg => $fg,
643 timeout => $color >= 2 ? 180 : 10, 674 timeout => $color >= 2 ? 180 : 10,
644 tooltip_font => $::FONT_FIXED, 675 tooltip_font => $::FONT_FIXED,
645 ); 676 );
646} 677}
647 678
672} 703}
673 704
674sub setup { 705sub setup {
675 my ($self, $setup) = @_; 706 my ($self, $setup) = @_;
676 707
708 $self->{map_widget}->set_tilesize ($self->{tilesize});
677 $::MAP->resize ($self->{mapw}, $self->{maph}); 709 $::MAP->resize ($self->{mapw}, $self->{maph});
678} 710}
679 711
680sub addme_success { 712sub addme_success {
681 my ($self) = @_; 713 my ($self) = @_;
921 953
922 $self->update_server_info; 954 $self->update_server_info;
923 955
924 $self->send_command ("output-sync $::CFG->{output_sync}"); 956 $self->send_command ("output-sync $::CFG->{output_sync}");
925 $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;
926 $self->send_command ("pickup $::CFG->{pickup}"); 959 $self->send_command ("pickup $::CFG->{pickup}");
927} 960}
928 961
929sub buildat { 962sub buildat {
930 my ($self, $builditem, $x, $y) = @_; 963 my ($self, $builditem, $x, $y) = @_;
982 kw => { hi => 0, yes => 0, no => 0 }, 1015 kw => { hi => 0, yes => 0, no => 0 },
983 has_close_button => 1, 1016 has_close_button => 1,
984 @_, 1017 @_,
985 ); 1018 );
986 1019
987 Scalar::Util::weaken (my $this = $self); 1020 CFPlus::weaken (my $this = $self);
988 1021
989 $self->connect (delete => sub { $this->destroy; 1 }); 1022 $self->connect (delete => sub { $this->destroy; 1 });
990 1023
991 # better use a pane... 1024 # better use a pane...
992 $self->add (my $hbox = new CFPlus::UI::HBox); 1025 $self->add (my $hbox = new CFPlus::UI::HBox);
1036}; 1069};
1037 1070
1038sub update_options { 1071sub update_options {
1039 my ($self) = @_; 1072 my ($self) = @_;
1040 1073
1041 Scalar::Util::weaken $self; 1074 CFPlus::weaken $self;
1042 1075
1043 $self->{options}->clear; 1076 $self->{options}->clear;
1044 $self->{options}->add ($self->{bye_button}); 1077 $self->{options}->add ($self->{bye_button});
1045 1078
1046 for my $kw (sort keys %{ $self->{kw} }) { 1079 for my $kw (sort keys %{ $self->{kw} }) {
1055} 1088}
1056 1089
1057sub feed { 1090sub feed {
1058 my ($self, $msg) = @_; 1091 my ($self, $msg) = @_;
1059 1092
1060 Scalar::Util::weaken $self; 1093 CFPlus::weaken $self;
1061 1094
1062 if ($msg->{msgtype} eq "reply") { 1095 if ($msg->{msgtype} eq "reply") {
1063 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1096 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1064 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1097 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1065 1098

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines