ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.153 by root, Thu Nov 26 07:19:12 2009 UTC vs.
Revision 1.169 by root, Sun Nov 25 14:37:18 2012 UTC

1package DC::MapWidget; 1package DC::MapWidget;
2 2
3use strict; 3use common::sense;
4use utf8;
5 4
6use List::Util qw(min max); 5use List::Util qw(min max);
6
7use AnyEvent ();
7 8
8use DC; 9use DC;
9use DC::OpenGL; 10use DC::OpenGL;
10use DC::UI; 11use DC::UI;
11use DC::Macro; 12use DC::Macro;
12 13
13our @ISA = DC::UI::Base::; 14our @ISA = DC::UI::Base::;
15
16our @TEX_HIDDEN = map {
17 new_from_resource DC::Texture # MUST be POT
18 "hidden-$_.png", mipmap => 1, wrap => 1
19 } 0, 1, 2;
14 20
15my $magicmap_tex = 21my $magicmap_tex =
16 new_from_resource DC::Texture "magicmap.png", 22 new_from_resource DC::Texture "magicmap.png",
17 mipmap => 1, wrap => 0, internalformat => GL_ALPHA; 23 mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
18 24
25 tilesize => 32, 31 tilesize => 32,
26 @_ 32 @_
27 ); 33 );
28 34
29 $self 35 $self
30}
31
32sub add_command {
33 my ($self, $command, $tooltip, $widget, $cb) = @_;
34
35 (my $data = $command) =~ s/\\//g;
36
37 $tooltip =~ s/^\s+//;
38 $tooltip = "<big>$data</big>\n\n$tooltip";
39 $tooltip =~ s/\s+$//;
40
41 $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
42}
43
44sub clr_commands {
45 my ($self) = @_;
46
47 %{$::COMPLETER->{command}} = ();
48
49 $::COMPLETER->hide
50 if $::COMPLETER;
51} 36}
52 37
53sub server_login { 38sub server_login {
54 my ($server) = @_; 39 my ($server) = @_;
55 40
209# push @items, [ 194# push @items, [
210# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", 195# "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>",
211# \&editor_invoke, 196# \&editor_invoke,
212# ]; 197# ];
213 198
214 for my $type (qw(test name)) { 199 for my $type (@{ $::CONN->{editor_support}{servertypes} }) {
215 $::CONN->{editor_support}{type} ne $type 200 $::CONN->{editor_support}{servertype} ne $type
216 or next; 201 or next;
217 my $server = $::CONN->{editor_support}{"${type}server"} 202 my $server = $::CONN->{editor_support}{"${type}server"}
218 or next; 203 or next;
219 204
220 push @items, [ 205 push @items, [
396} 381}
397 382
398sub scroll { 383sub scroll {
399 my ($self, $dx, $dy) = @_; 384 my ($self, $dx, $dy) = @_;
400 385
401 $::MAP->scroll ($dx, $dy);
402
403 $self->{last_fow_texture} = $self->{fow_texture};
404 $self->{ldx} = $self->{sdx} + $dx * $self->{tilesize}; # smooth displacement
405 $self->{ldy} = $self->{sdy} + $dy * $self->{tilesize};
406
407 $self->movement_update; 386 $self->movement_update;
408 387
409 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 388 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
410 $self->{sdy} += $dy * $self->{tilesize}; 389 $self->{sdy} += $dy * $self->{tilesize};
390
391 # save old fow texture, if applicable
392 $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
393 $self->{lfdx} = $dx;
394 $self->{lfdy} = $dy;
395 $self->{lmdx} = $self->{dx};
396 $self->{lmdy} = $self->{dy};
397
398 $::MAP->scroll ($dx, $dy);
411} 399}
412 400
413sub set_magicmap { 401sub set_magicmap {
414 my ($self, $w, $h, $x, $y, $data) = @_; 402 my ($self, $w, $h, $x, $y, $data) = @_;
415 403
424sub movement_update { 412sub movement_update {
425 my ($self) = @_; 413 my ($self) = @_;
426 414
427 if ($::CFG->{smooth_movement}) { 415 if ($::CFG->{smooth_movement}) {
428 if ($self->{sdx} || $self->{sdy}) { 416 if ($self->{sdx} || $self->{sdy}) {
429 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 417 my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH);
430 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 418 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
431 419
432 # the minimum time for a single tile movement 420 # the minimum time for a single tile movement
433 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); 421 my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
434 422
455 } 443 }
456 } else { 444 } else {
457 $self->{sdx} = $self->{sdy} = 0; 445 $self->{sdx} = $self->{sdy} = 0;
458 } 446 }
459 447
460 $self->{last_update} = EV::time; 448 $self->{last_update} = AE::time;
461} 449}
462 450
463sub refresh_hook { 451sub refresh_hook {
464 my ($self) = @_; 452 my ($self) = @_;
465 453
486 474
487 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; 475 my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx;
488 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; 476 my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
489 477
490 if ($::CFG->{fow_enable}) { 478 if ($::CFG->{fow_enable}) {
491 my ($sdx_t, $sdy_t);#d# do not anchor at player 479 # draw_fow_texture REQUIRES the fow texture to stay the same size.
492
493 my ($w, $h, $data) = $::MAP->fow_texture ( 480 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
494 $dx + (min 0, $sdx_t),
495 $dy + (min 0, $sdy_t),
496 $sw + abs $sdx_t,
497 $sh + abs $sdy_t
498 );
499 481
500 $self->{fow_texture} = new DC::Texture 482 $self->{fow_texture} = new DC::Texture
501 w => $w, 483 w => $w,
502 h => $h, 484 h => $h,
503 data => $data, 485 data => $data,
513 glTranslate $sx0, $sy0; 495 glTranslate $sx0, $sy0;
514 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 496 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
515 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; 497 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
516 498
517 $::MAP->draw ($dx, $dy, $sw, $sh, 499 $::MAP->draw ($dx, $dy, $sw, $sh,
518 $self->{tilesize}, 500 ($self->{tilesize}) x 2,
519 $::CONN->{player}{tag}, 501 $::CONN->{player}{tag},
520 -$self->{sdx}, -$self->{sdy}); 502 -$self->{sdx}, -$self->{sdy});
521 503
522 #d#glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player
523 glScale $self->{tilesize}, $self->{tilesize}; 504 glScale $self->{tilesize}, $self->{tilesize};
524 505
525 if (my $tex = $self->{fow_texture}) { 506 if (my $tex = $self->{fow_texture}) {
526 glEnable GL_TEXTURE_2D; 507 my @prev_fow_params;
527 glEnable GL_BLEND;
528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
529 glBlendEquation GL_FUNC_REVERSE_SUBTRACT;
530 glBlendFunc GL_ONE, GL_ZERO;
531 508
532 my $a1 = 0.9; 509 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
533 510 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
534 if (0 && $self->{last_fow_texture}) { 511 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
535 my $d1 = ($self->{sdx} ** 2 + $self->{sdy} ** 2) ** 0.5;
536 my $d2 = ($self->{ldx} ** 2 + $self->{ldy} ** 2) ** 0.5;
537 512
538 if ($d1 * $d2) { 513 if ($d1 * $d2) {
514 @prev_fow_params = (
539 my $a2 = $d1 / $d2; 515 (min 1, $d1 / $d2),
540 516 $self->{lmdx} - $dx - $self->{lfdx},
541 $a1 = 1 - $a2; 517 $self->{lmdy} - $dy - $self->{lfdy},
542 518 @{$self->{prev_fow_texture}}{qw(name data)}
543 #$a1 *= 0.9;
544 #$a2 *= 0.9;
545
546 printf "%4g %4g %4g %4g\n", $d1, $d2, $a1, $a2;#d#
547
548 #$a1 = 1 - (1-$a1) * 0.707;
549 #$a2 = 1 - (1-$a2) * 0.707;
550
551 glColor 1,0,0,1;
552 glBegin GL_QUADS;
553 glVertex 10 + $sdx_t, 10;
554 glVertex 11 + $sdx_t, 10;
555 glVertex 11 + $sdx_t, 11;
556 glVertex 10 + $sdx_t, 11;
557 glEnd; 519 );
558
559 glColor +($::CFG->{fow_intensity}) x 3, $a2;
560 glPushMatrix;
561 glTranslate $sdx_t, $sdy_t; # anchor at player
562 glTranslate $self->{ldx} / -$tilesize, $self->{ldy} / -$tilesize;
563 glScale 1/3, 1/3;
564 $self->{last_fow_texture}->draw_quad_alpha (0, 0);
565 glPopMatrix;
566 } else {
567 delete $self->{last_fow_texture};
568 } 520 }
569 } 521 }
570 522
523 DC::Texture::draw_fow_texture
571 glColor +($::CFG->{fow_intensity}) x 3, $a1; 524 $::CFG->{fow_intensity},
572 glPushMatrix; 525 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
573 #d#glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); # anchor at player 526 @{$self->{fow_texture}}{qw(name data s t w h)},
574 glScale 1/3, 1/3; 527 @prev_fow_params;
575 $self->{fow_texture}->draw_quad_alpha (0, 0);
576 glPopMatrix;
577
578 glBlendEquation GL_FUNC_ADD;
579 glDisable GL_TEXTURE_2D;
580 glDisable GL_BLEND;
581 } 528 }
582 529
583 if ($self->{magicmap}) { 530 if ($self->{magicmap}) {
584 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 531 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
585 532
586 $x += $::MAP->ox + $self->{dx}; 533 $x += $::MAP->ox + $self->{dx};
587 $y += $::MAP->oy + $self->{dy}; 534 $y += $::MAP->oy + $self->{dy};
588 535
589 glTranslate - $x - 1, - $y - 1; 536 glTranslate - $x - 1, - $y - 1;
590 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 537 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
591 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 538 $::MAP->draw_magicmap ($w, $h, $data);
592 } 539 }
593 540
594 glPopMatrix; 541 glPopMatrix;
595 glEndList; 542 glEndList;
596 } 543 }
597 } else { 544 } else {
545 delete $self->{last_fow_texture};
546 delete $self->{fow_texture};
547
598 glDeleteList delete $self->{list} 548 glDeleteList delete $self->{list}
599 if $self->{list}; 549 if $self->{list};
600 } 550 }
601} 551}
602 552
638 $self->SUPER::DESTROY; 588 $self->SUPER::DESTROY;
639} 589}
640 590
641package DC::MapWidget::MapMap; 591package DC::MapWidget::MapMap;
642 592
643use strict; 593use common::sense;
644use utf8;
645 594
646our @ISA = DC::UI::Base::; 595our @ISA = DC::UI::Base::;
647 596
648use Time::HiRes qw(time);
649use DC::OpenGL; 597use DC::OpenGL;
650 598
651sub size_request { 599sub size_request {
652 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 600 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
653} 601}
654 602
655sub refresh_hook { 603sub refresh_hook {
656 my ($self) = @_; 604 my ($self) = @_;
657 605
658 if ($::MAP && $self->{texture_atime} < time) { 606 if ($::MAP && $self->{texture_atime} < AE::now) {
659 my ($w, $h) = @$self{qw(w h)}; 607 my ($w, $h) = @$self{qw(w h)};
660 608
661 return unless $w && $h; 609 return unless $w && $h;
662 610
663 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 611 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
678 $self->{sh} = $sh; 626 $self->{sh} = $sh;
679 627
680 $self->{x0} = $x0; 628 $self->{x0} = $x0;
681 $self->{y0} = $y0; 629 $self->{y0} = $y0;
682 630
683 $self->{texture_atime} = time + 1/3; 631 $self->{texture_atime} = AE::now + 1/2;
684 632
685 $self->{texture} = 633 $self->{texture} =
686 new DC::Texture 634 new DC::Texture
687 w => $w, 635 w => $w,
688 h => $h, 636 h => $h,
743 glDisable GL_BLEND; 691 glDisable GL_BLEND;
744} 692}
745 693
746package DC::MapWidget::Command; 694package DC::MapWidget::Command;
747 695
748use strict; 696use common::sense;
749 697
750use DC::OpenGL; 698use DC::OpenGL;
751 699
752our @ISA = DC::UI::Frame::; 700our @ISA = DC::UI::Frame::;
753 701
754sub new { 702sub new {
755 my $class = shift; 703 my $class = shift;
756 704
757 my $self = $class->SUPER::new ( 705 my $self = $class->SUPER::new (
706 min_w => $::WIDTH * 0.25, # workaround for layout problems #d#
758 bg => [0, 0, 0, 0.8], 707 bg => [0, 0, 0, 0.8],
759 @_, 708 @_,
760 ); 709 );
761 710
762 $self->add ($self->{vbox} = new DC::UI::VBox); 711 $self->add ($self->{vbox} = new DC::UI::VBox);
849 ); 798 );
850 799
851 $self 800 $self
852} 801}
853 802
803sub reset {
804 my ($self) = @_;
805
806 $self->hide;
807 delete $self->{command_list};
808}
809
854sub set_prefix { 810sub set_prefix {
855 my ($self, $prefix) = @_; 811 my ($self, $prefix) = @_;
856 812
857 $self->{entry}->set_text ($prefix); 813 $self->{entry}->set_text ($prefix);
858 $self->show; 814 $self->show;
894 850
895 my $text = $self->{entry}->get_text; 851 my $text = $self->{entry}->get_text;
896 852
897 length $text 853 length $text
898 or return $self->hide; 854 or return $self->hide;
855
856 return unless $::CONN;
857
858 # regenerate spell list if necessary
859 $self->{command_list}{spells} ||= [
860 map { ("cast $_->{name}", "invoke $_->{name}") }
861 values %{ $::CONN->{spell} }
862 ];
899 863
900 if ($text ne $self->{last_search}) { 864 if ($text ne $self->{last_search}) {
901 my @match; 865 my @match;
902 866
903 if ($text =~ /^(.*?)\s+$/) { 867 if ($text =~ /^(.*?)\s+$/) {
906 } else { 870 } else {
907 # @match is [command, penalty, command with arguments] until sort 871 # @match is [command, penalty, command with arguments] until sort
908 872
909 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 873 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
910 874
875 my $first_char = substr $cmd, 0, 1;
876
911 my $regexp_abbrev = do { 877 my $regexp_abbrev = do {
912 my ($beg, @chr) = split //, lc $cmd; 878 my ($beg, @chr) = split //, lc $cmd;
913 879
914 # the following regex is used to match our "completion entry" 880 # the following regex is used to match our "completion entry"
915 # to an actual command - the parentheses match kind of "overhead" 881 # to an actual command - the parentheses match kind of "overhead"
916 # - the more characters the parentheses match, the less attractive 882 # - the more characters the parentheses match, the less attractive
917 # is the match. 883 # is the match.
918 my $regexp = "^\Q$beg\E" 884 my $regexp = "^\Q$beg\E"
919 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 885 . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr;
920 qr<$regexp> 886 qr<$regexp>
921 }; 887 };
922 888
923 my $regexp_partial = do { 889 my $regexp_partial = do {
924 my $regexp = "^\Q$text\E(.*)"; 890 my $regexp = "^\Q$text\E(.*)";
925 qr<$regexp> 891 qr<$regexp>
926 }; 892 };
927 893
928 for (keys %{$self->{command}}) { 894 for my $list (values %{ $self->{command_list} }) {
895 for (@$list) {
896 # we only match and score if the first character matches,
897 # so quickly rule out all others first.
898 next unless $first_char = substr $_, 0, 1;
899
929 my @scores; 900 my @scores;
930 901
931 # 1. Complete command [with args] 902 # 1. Complete command [with args]
932 # command is a prefix of the text 903 # command is a prefix of the text
933 # score is length of complete command matched 904 # score is length of complete command matched
934 # e.g. "invoke summon pet monster bat" 905 # e.g. "invoke summon pet monster bat"
935 # "invoke" "summon pet monster bat" = 6 906 # "invoke" "summon pet monster bat" = 6
936 # "invoke summon pet monster" "bat" = 25 907 # "invoke summon pet monster" "bat" = 25
937 if ($text =~ /^\Q$_\E(.*)/) { 908 if ((substr $text, 0, length $_) eq $_) {
938 push @scores, [$_, length $_, $text]; 909 push @scores, [$_, length $_, $text];
910 }
911
912 # 2. Partial command
913 # text is a prefix of the full command
914 # score is the length of the input text
915 # e.g. "invoke s"
916 # "invoke small fireball" = 8
917 # "invoke summon pet monster" = 8
918
919 if ($_ =~ $regexp_partial) {
920 push @scores, [$_, length $text, $_];
921 }
922
923 # 3. Abbreviation match
924 # attempts to use first word of text as an abbreviated command
925 # score is length of word + 1 - 3 per non-word-initial character
926
927 if (my @penalty = $_ =~ $regexp_abbrev) {
928 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
929 }
930
931 # Pick the best option for this command
932 push @match, (sort {
933 $b->[1] <=> $a->[1]
934 } @scores)[0];
939 } 935 }
940
941 # 2. Partial command
942 # text is a prefix of the full command
943 # score is the length of the input text
944 # e.g. "invoke s"
945 # "invoke small fireball" = 8
946 # "invoke summon pet monster" = 8
947
948 if ($_ =~ $regexp_partial) {
949 push @scores, [$_, length $text, $_];
950 }
951
952 # 3. Abbreviation match
953 # attempts to use first word of text as an abbreviated command
954 # score is length of word + 1 - 3 per non-word-initial character
955
956 if (my @penalty = $_ =~ $regexp_abbrev) {
957 push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"];
958 }
959
960 # Pick the best option for this command
961 push @match, (sort {
962 $b->[1] <=> $a->[1]
963 } @scores)[0];
964 } 936 }
965 937
966 # @match is now [command object, command with arguments] 938 # @match is now [command object, command with arguments]
967 @match = map [$self->{command}{$_->[0]}, $_->[2]], 939 @match = map [$self->{command}{$_->[0]}, $_->[2]],
968 sort { 940 sort {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines