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.88 by root, Sun Nov 19 20:31:29 2006 UTC vs.
Revision 1.102 by root, Wed Apr 18 18:16:16 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 {
288 if ($ev->{button} == 1) { 309 if ($ev->{button} == 1) {
289 $::CONN->user_send ("ready_skill $name"); 310 $::CONN->user_send ("ready_skill $name");
290 } elsif ($ev->{button} == 2) { 311 } elsif ($ev->{button} == 2) {
291 $::CONN->user_send ("use_skill $name"); 312 $::CONN->user_send ("use_skill $name");
292 } elsif ($ev->{button} == 3) { 313 } elsif ($ev->{button} == 3) {
314 my $shortname = CFPlus::shorten $name, 14;
293 (new CFPlus::UI::Menu 315 (new CFPlus::UI::Menu
294 items => [ 316 items => [
295 ["bind <i>ready_skill $name</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"] }],
296 ["bind <i>use_skill $name</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"] }],
297 ], 319 ],
298 )->popup ($ev); 320 )->popup ($ev);
299 } else { 321 } else {
300 return 0; 322 return 0;
301 } 323 }
318 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 340 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
319 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 341 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
320 } 342 }
321} 343}
322 344
345sub macro_send {
346 my ($self, $macro) = @_;
347
348 for my $cmd (@{ $macro->{action} }) {
349 $self->send_command ($cmd);
350 }
351}
352
323sub user_send { 353sub user_send {
324 my ($self, $command) = @_; 354 my ($self, $command) = @_;
325 355
326 push @{$self->{record}}, $command 356 $self->{record}->($command)
327 if $self->{record}; 357 if $self->{record};
328 358
329 $self->logprint ("send: ", $command); 359 $self->logprint ("send: ", $command);
330 $self->send_command ($command); 360 $self->send_command ($command);
331 ::status ($command); 361 ::status ($command);
332} 362}
333 363
334sub start_record { 364sub record {
335 my ($self) = @_; 365 my ($self, $cb) = @_;
336 366
337 $self->{record} = []; 367 $self->{record} = $cb;
338}
339
340sub stop_record {
341 my ($self) = @_;
342 return delete $self->{record};
343} 368}
344 369
345sub map_scroll { 370sub map_scroll {
346 my ($self, $dx, $dy) = @_; 371 my ($self, $dx, $dy) = @_;
347 372
368 or return; 393 or return;
369 394
370 my ($hash, $x, $y, $w, $h) = @$map_info; 395 my ($hash, $x, $y, $w, $h) = @$map_info;
371 396
372 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 397 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
373 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 398 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
374 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 399 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
375} 400}
376 401
377sub map_clear { 402sub map_clear {
378 my ($self) = @_; 403 my ($self) = @_;
382 407
383 $self->{map}->clear; 408 $self->{map}->clear;
384 delete $self->{map_widget}{magicmap}; 409 delete $self->{map_widget}{magicmap};
385} 410}
386 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}
387 433
388sub load_map($$$) { 434sub load_map($$$) {
389 my ($self, $hash, $x, $y) = @_; 435 my ($self, $hash, $x, $y) = @_;
390 436
391 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) {
392 $data = Compress::LZF::decompress $data; 445 $data = Compress::LZF::decompress $data;
393 #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#
394 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
395 my $data = $self->{tilecache}->get ($id)
396 or next;
397 447
398 $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;
399 } 451 }
400 } 452 };
401} 453}
402 454
403# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 455# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
404# (server resource,s latency, bandwidth), so this hack is warranted. 456# (server resource,s latency, bandwidth), so this hack is warranted.
405# 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
469 521
470 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 522 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
471 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 523 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
472 524
473 } else { 525 } else {
526 my $gen = $self->{map_change_gen};
474 $self->send_mapinfo ("spatial $path$tile", sub { 527 $self->send_mapinfo ("spatial $path$tile", sub {
528 return unless $gen == $self->{map_change_gen};
529
475 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 530 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
476 531
477 return if $mode ne "spatial"; 532 return if $mode ne "spatial";
478 533
479 $x += $self->{map}->ox; 534 $x += $self->{map}->ox;
493 548
494sub map_change { 549sub map_change {
495 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 550 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
496 551
497 $self->flush_map; 552 $self->flush_map;
553
554 ++$self->{map_change_gen};
498 555
499 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 556 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
500 557
501 my $mapmapw = $self->{mapmap}->{w}; 558 my $mapmapw = $self->{mapmap}->{w};
502 my $mapmaph = $self->{mapmap}->{h}; 559 my $mapmaph = $self->{mapmap}->{h};
519 $self->load_map ($hash, $x, $y); 576 $self->load_map ($hash, $x, $y);
520 $self->flood_fill (0, 0, 0, "", $hash, $flags); 577 $self->flood_fill (0, 0, 0, "", $hash, $flags);
521} 578}
522 579
523sub face_find { 580sub face_find {
524 my ($self, $facenum, $face) = @_; 581 my ($self, $facenum, $face, $cb) = @_;
525 582
526 my $hash = "$face->{chksum},$face->{name}"; 583 my $hash = "$face->{chksum},$face->{name}";
527 584
528 my $id = $self->{facemap}->get ($hash); 585 my $id = CFPlus::DB::get_tile_id_sync $hash;
529 586
530 unless ($id) {
531 # create new id for face
532 # I love transactions
533 for (1..100) {
534 my $txn = $CFPlus::DB_ENV->txn_begin;
535 my $status = $self->{facemap}->db_get (id => $id);
536 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
537 $id = ($id || 64) + 1;
538 if ($self->{facemap}->put (id => $id) == 0
539 && $self->{facemap}->put ($hash => $id) == 0) {
540 $txn->txn_commit;
541
542 goto gotid;
543 }
544 }
545 $txn->txn_abort;
546 }
547
548 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
549 }
550
551gotid:
552 $face->{id} = $id; 587 $face->{id} = $id;
553 $self->{map}->set_face ($facenum => $id);
554 $self->{faceid}[$facenum] = $id;#d# 588 $self->{faceid}[$facenum] = $id;
555 589
556 my $face = $self->{tilecache}->get ($id); 590 $self->{map}->set_tileid ($facenum => $id);
557 591
558 if ($face) { 592 CFPlus::DB::get tilecache => $id, $cb;
559 #$self->face_prefetch;
560 $face
561 } else {
562 my $tex = $self->{noface};
563 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
564 undef
565 };
566} 593}
567 594
568sub face_update { 595sub face_update {
569 my ($self, $facenum, $face, $changed) = @_; 596 my ($self, $facenum, $face, $changed) = @_;
570 597
571 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 598 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
599 if $changed;
572 600
573 $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});
574} 608}
575 609
576sub set_texture { 610sub set_texture {
577 my ($self, $id, $data) = @_; 611 my ($self, $id, $data) = @_;
578 612
579 $self->{texture}[$id] ||= do { 613 $self->{texture}[$id] = my $tex =
580 my $tex =
581 new_from_image CFPlus::Texture 614 new_from_image CFPlus::Texture
582 $data, minify => 1, mipmap => 1; 615 $data, minify => 1, mipmap => 1;
583 616
584 $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}});
585 $self->{map_widget}->update; 618 $self->{map_widget}->update;
586
587 $tex
588 };
589} 619}
590 620
591sub sound_play { 621sub sound_play {
592 my ($self, $x, $y, $soundnum, $type) = @_; 622 my ($self, $x, $y, $soundnum, $type) = @_;
593 623
622 [0.75, 0.61, 0.20], 652 [0.75, 0.61, 0.20],
623 [0.99, 0.77, 0.26], 653 [0.99, 0.77, 0.26],
624 [0.74, 0.65, 0.41], 654 [0.74, 0.65, 0.41],
625 ); 655 );
626 656
657 my $fg = $color[$color % @color];
658
627 $self->logprint ("info: ", $text); 659 $self->logprint ("info: ", $text);
628 660
629 # 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
662 # no longer neecssary with TRT servers
630 $text =~ s/(?<=\S)\n(?=\w)/ /g; 663 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
631 664
632 $text = CFPlus::asxml $text; 665 $text = CFPlus::asxml $text;
633 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 666 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
634 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 667 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
635 668
636 ::message ({ fg => $color[$color], markup => $_ }) 669 ::message ({ fg => $fg, markup => $_ })
637 for split /\n/, $text; 670 for split /\n/, $text;
638 671
639 $self->{statusbox}->add ($text, 672 $self->{statusbox}->add ($text,
640 group => $text, 673 group => $text,
641 fg => $color[$color], 674 fg => $fg,
642 timeout => $color >= 2 ? 180 : 10, 675 timeout => $color >= 2 ? 180 : 10,
643 tooltip_font => $::FONT_FIXED, 676 tooltip_font => $::FONT_FIXED,
644 ); 677 );
645} 678}
646 679
671} 704}
672 705
673sub setup { 706sub setup {
674 my ($self, $setup) = @_; 707 my ($self, $setup) = @_;
675 708
709 $self->{map_widget}->set_tilesize ($self->{tilesize});
676 $::MAP->resize ($self->{mapw}, $self->{maph}); 710 $::MAP->resize ($self->{mapw}, $self->{maph});
677} 711}
678 712
679sub addme_success { 713sub addme_success {
680 my ($self) = @_; 714 my ($self) = @_;
893 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n" 927 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
894 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 928 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
895 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 929 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
896 . "map size $self->{mapw}×$self->{maph}\n" 930 . "map size $self->{mapw}×$self->{maph}\n"
897 ); 931 );
932
933 ::setup_build_button ($self->{editor_support}->{builder_ui});
898} 934}
899 935
900sub logged_in { 936sub logged_in {
901 my ($self) = @_; 937 my ($self) = @_;
902 938
918 954
919 $self->update_server_info; 955 $self->update_server_info;
920 956
921 $self->send_command ("output-sync $::CFG->{output_sync}"); 957 $self->send_command ("output-sync $::CFG->{output_sync}");
922 $self->send_command ("output-count $::CFG->{output_count}"); 958 $self->send_command ("output-count $::CFG->{output_count}");
959 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
923 $self->send_command ("pickup $::CFG->{pickup}"); 960 $self->send_command ("pickup $::CFG->{pickup}");
961}
962
963sub buildat {
964 my ($self, $builditem, $x, $y) = @_;
965
966 if ($self->{cfplus_ext}) {
967 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
968 }
924} 969}
925 970
926sub lookat { 971sub lookat {
927 my ($self, $x, $y) = @_; 972 my ($self, $x, $y) = @_;
928 973
971 kw => { hi => 0, yes => 0, no => 0 }, 1016 kw => { hi => 0, yes => 0, no => 0 },
972 has_close_button => 1, 1017 has_close_button => 1,
973 @_, 1018 @_,
974 ); 1019 );
975 1020
976 Scalar::Util::weaken (my $this = $self); 1021 CFPlus::weaken (my $this = $self);
977 1022
978 $self->connect (delete => sub { $this->destroy; 1 }); 1023 $self->connect (delete => sub { $this->destroy; 1 });
979 1024
980 # better use a pane... 1025 # better use a pane...
981 $self->add (my $hbox = new CFPlus::UI::HBox); 1026 $self->add (my $hbox = new CFPlus::UI::HBox);
1025}; 1070};
1026 1071
1027sub update_options { 1072sub update_options {
1028 my ($self) = @_; 1073 my ($self) = @_;
1029 1074
1030 Scalar::Util::weaken $self; 1075 CFPlus::weaken $self;
1031 1076
1032 $self->{options}->clear; 1077 $self->{options}->clear;
1033 $self->{options}->add ($self->{bye_button}); 1078 $self->{options}->add ($self->{bye_button});
1034 1079
1035 for my $kw (sort keys %{ $self->{kw} }) { 1080 for my $kw (sort keys %{ $self->{kw} }) {
1044} 1089}
1045 1090
1046sub feed { 1091sub feed {
1047 my ($self, $msg) = @_; 1092 my ($self, $msg) = @_;
1048 1093
1049 Scalar::Util::weaken $self; 1094 CFPlus::weaken $self;
1050 1095
1051 if ($msg->{msgtype} eq "reply") { 1096 if ($msg->{msgtype} eq "reply") {
1052 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1097 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1053 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1098 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1054 1099

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines