1 | package DC::MapWidget; |
1 | package DC::MapWidget; |
2 | |
2 | |
3 | use common::sense; |
3 | use common::sense; |
4 | |
4 | |
5 | use List::Util qw(min max); |
5 | use List::Util qw(min max); |
|
|
6 | |
|
|
7 | use AnyEvent (); |
6 | |
8 | |
7 | use DC; |
9 | use DC; |
8 | use DC::OpenGL; |
10 | use DC::OpenGL; |
9 | use DC::UI; |
11 | use DC::UI; |
10 | use DC::Macro; |
12 | use DC::Macro; |
… | |
… | |
31 | ); |
33 | ); |
32 | |
34 | |
33 | $self |
35 | $self |
34 | } |
36 | } |
35 | |
37 | |
36 | sub add_command { |
38 | sub add_command {#d# remove |
37 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
39 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
38 | |
40 | |
39 | (my $data = $command) =~ s/\\//g; |
41 | (my $data = $command) =~ s/\\//g; |
40 | |
42 | |
41 | $tooltip =~ s/^\s+//; |
43 | $tooltip =~ s/^\s+//; |
42 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
44 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
43 | $tooltip =~ s/\s+$//; |
45 | $tooltip =~ s/\s+$//; |
44 | |
46 | |
45 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
47 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
46 | } |
|
|
47 | |
|
|
48 | sub clr_commands { |
|
|
49 | my ($self) = @_; |
|
|
50 | |
|
|
51 | %{$::COMPLETER->{command}} = (); |
|
|
52 | |
|
|
53 | $::COMPLETER->hide |
|
|
54 | if $::COMPLETER; |
|
|
55 | } |
48 | } |
56 | |
49 | |
57 | sub server_login { |
50 | sub server_login { |
58 | my ($server) = @_; |
51 | my ($server) = @_; |
59 | |
52 | |
… | |
… | |
431 | sub movement_update { |
424 | sub movement_update { |
432 | my ($self) = @_; |
425 | my ($self) = @_; |
433 | |
426 | |
434 | if ($::CFG->{smooth_movement}) { |
427 | if ($::CFG->{smooth_movement}) { |
435 | if ($self->{sdx} || $self->{sdy}) { |
428 | if ($self->{sdx} || $self->{sdy}) { |
436 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
429 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
437 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
430 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
438 | |
431 | |
439 | # the minimum time for a single tile movement |
432 | # the minimum time for a single tile movement |
440 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
433 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
441 | |
434 | |
… | |
… | |
462 | } |
455 | } |
463 | } else { |
456 | } else { |
464 | $self->{sdx} = $self->{sdy} = 0; |
457 | $self->{sdx} = $self->{sdy} = 0; |
465 | } |
458 | } |
466 | |
459 | |
467 | $self->{last_update} = EV::time; |
460 | $self->{last_update} = AE::time; |
468 | } |
461 | } |
469 | |
462 | |
470 | sub refresh_hook { |
463 | sub refresh_hook { |
471 | my ($self) = @_; |
464 | my ($self) = @_; |
472 | |
465 | |
… | |
… | |
514 | glTranslate $sx0, $sy0; |
507 | glTranslate $sx0, $sy0; |
515 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
508 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
516 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
509 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
517 | |
510 | |
518 | $::MAP->draw ($dx, $dy, $sw, $sh, |
511 | $::MAP->draw ($dx, $dy, $sw, $sh, |
519 | $self->{tilesize}, |
512 | ($self->{tilesize}) x 2, |
520 | $::CONN->{player}{tag}, |
513 | $::CONN->{player}{tag}, |
521 | -$self->{sdx}, -$self->{sdy}); |
514 | -$self->{sdx}, -$self->{sdy}); |
522 | |
515 | |
523 | glScale $self->{tilesize}, $self->{tilesize}; |
516 | glScale $self->{tilesize}, $self->{tilesize}; |
524 | |
517 | |
… | |
… | |
611 | |
604 | |
612 | use common::sense; |
605 | use common::sense; |
613 | |
606 | |
614 | our @ISA = DC::UI::Base::; |
607 | our @ISA = DC::UI::Base::; |
615 | |
608 | |
616 | use Time::HiRes qw(time); |
|
|
617 | use DC::OpenGL; |
609 | use DC::OpenGL; |
618 | |
610 | |
619 | sub size_request { |
611 | sub size_request { |
620 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
612 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
621 | } |
613 | } |
622 | |
614 | |
623 | sub refresh_hook { |
615 | sub refresh_hook { |
624 | my ($self) = @_; |
616 | my ($self) = @_; |
625 | |
617 | |
626 | if ($::MAP && $self->{texture_atime} < time) { |
618 | if ($::MAP && $self->{texture_atime} < AE::now) { |
627 | my ($w, $h) = @$self{qw(w h)}; |
619 | my ($w, $h) = @$self{qw(w h)}; |
628 | |
620 | |
629 | return unless $w && $h; |
621 | return unless $w && $h; |
630 | |
622 | |
631 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
623 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
… | |
… | |
646 | $self->{sh} = $sh; |
638 | $self->{sh} = $sh; |
647 | |
639 | |
648 | $self->{x0} = $x0; |
640 | $self->{x0} = $x0; |
649 | $self->{y0} = $y0; |
641 | $self->{y0} = $y0; |
650 | |
642 | |
651 | $self->{texture_atime} = time + 1/3; |
643 | $self->{texture_atime} = AE::now + 1/2; |
652 | |
644 | |
653 | $self->{texture} = |
645 | $self->{texture} = |
654 | new DC::Texture |
646 | new DC::Texture |
655 | w => $w, |
647 | w => $w, |
656 | h => $h, |
648 | h => $h, |
… | |
… | |
817 | ); |
809 | ); |
818 | |
810 | |
819 | $self |
811 | $self |
820 | } |
812 | } |
821 | |
813 | |
|
|
814 | sub reset { |
|
|
815 | my ($self) = @_; |
|
|
816 | |
|
|
817 | $self->hide; |
|
|
818 | delete $self->{command_list}; |
|
|
819 | } |
|
|
820 | |
822 | sub set_prefix { |
821 | sub set_prefix { |
823 | my ($self, $prefix) = @_; |
822 | my ($self, $prefix) = @_; |
824 | |
823 | |
825 | $self->{entry}->set_text ($prefix); |
824 | $self->{entry}->set_text ($prefix); |
826 | $self->show; |
825 | $self->show; |
… | |
… | |
862 | |
861 | |
863 | my $text = $self->{entry}->get_text; |
862 | my $text = $self->{entry}->get_text; |
864 | |
863 | |
865 | length $text |
864 | length $text |
866 | or return $self->hide; |
865 | or return $self->hide; |
|
|
866 | |
|
|
867 | return unless $::CONN; |
|
|
868 | |
|
|
869 | # regenerate spell list if necessary |
|
|
870 | $self->{command_list}{spells} ||= [ |
|
|
871 | map { ("cast $_->{name}", "invoke $_->{name}") } |
|
|
872 | values %{ $::CONN->{spell} } |
|
|
873 | ]; |
867 | |
874 | |
868 | if ($text ne $self->{last_search}) { |
875 | if ($text ne $self->{last_search}) { |
869 | my @match; |
876 | my @match; |
870 | |
877 | |
871 | if ($text =~ /^(.*?)\s+$/) { |
878 | if ($text =~ /^(.*?)\s+$/) { |
… | |
… | |
874 | } else { |
881 | } else { |
875 | # @match is [command, penalty, command with arguments] until sort |
882 | # @match is [command, penalty, command with arguments] until sort |
876 | |
883 | |
877 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
884 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
878 | |
885 | |
|
|
886 | my $first_char = substr $cmd, 0, 1; |
|
|
887 | |
879 | my $regexp_abbrev = do { |
888 | my $regexp_abbrev = do { |
880 | my ($beg, @chr) = split //, lc $cmd; |
889 | my ($beg, @chr) = split //, lc $cmd; |
881 | |
890 | |
882 | # the following regex is used to match our "completion entry" |
891 | # the following regex is used to match our "completion entry" |
883 | # to an actual command - the parentheses match kind of "overhead" |
892 | # to an actual command - the parentheses match kind of "overhead" |
884 | # - the more characters the parentheses match, the less attractive |
893 | # - the more characters the parentheses match, the less attractive |
885 | # is the match. |
894 | # is the match. |
886 | my $regexp = "^\Q$beg\E" |
895 | my $regexp = "^\Q$beg\E" |
887 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
896 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
888 | qr<$regexp> |
897 | qr<$regexp> |
889 | }; |
898 | }; |
890 | |
899 | |
891 | my $regexp_partial = do { |
900 | my $regexp_partial = do { |
892 | my $regexp = "^\Q$text\E(.*)"; |
901 | my $regexp = "^\Q$text\E(.*)"; |
893 | qr<$regexp> |
902 | qr<$regexp> |
894 | }; |
903 | }; |
895 | |
904 | |
896 | for (keys %{$self->{command}}) { |
905 | for my $list (values %{ $self->{command_list} }) { |
|
|
906 | for (@$list) { |
|
|
907 | # we only match and score if the first character matches, |
|
|
908 | # so quickly rule out all others first. |
|
|
909 | next unless $first_char = substr $_, 0, 1; |
|
|
910 | |
897 | my @scores; |
911 | my @scores; |
898 | |
912 | |
899 | # 1. Complete command [with args] |
913 | # 1. Complete command [with args] |
900 | # command is a prefix of the text |
914 | # command is a prefix of the text |
901 | # score is length of complete command matched |
915 | # score is length of complete command matched |
902 | # e.g. "invoke summon pet monster bat" |
916 | # e.g. "invoke summon pet monster bat" |
903 | # "invoke" "summon pet monster bat" = 6 |
917 | # "invoke" "summon pet monster bat" = 6 |
904 | # "invoke summon pet monster" "bat" = 25 |
918 | # "invoke summon pet monster" "bat" = 25 |
905 | if ($text =~ /^\Q$_\E(.*)/) { |
919 | if ((substr $text, 0, length $_) eq $_) { |
906 | push @scores, [$_, length $_, $text]; |
920 | push @scores, [$_, length $_, $text]; |
|
|
921 | } |
|
|
922 | |
|
|
923 | # 2. Partial command |
|
|
924 | # text is a prefix of the full command |
|
|
925 | # score is the length of the input text |
|
|
926 | # e.g. "invoke s" |
|
|
927 | # "invoke small fireball" = 8 |
|
|
928 | # "invoke summon pet monster" = 8 |
|
|
929 | |
|
|
930 | if ($_ =~ $regexp_partial) { |
|
|
931 | push @scores, [$_, length $text, $_]; |
|
|
932 | } |
|
|
933 | |
|
|
934 | # 3. Abbreviation match |
|
|
935 | # attempts to use first word of text as an abbreviated command |
|
|
936 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
937 | |
|
|
938 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
939 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
940 | } |
|
|
941 | |
|
|
942 | # Pick the best option for this command |
|
|
943 | push @match, (sort { |
|
|
944 | $b->[1] <=> $a->[1] |
|
|
945 | } @scores)[0]; |
907 | } |
946 | } |
908 | |
|
|
909 | # 2. Partial command |
|
|
910 | # text is a prefix of the full command |
|
|
911 | # score is the length of the input text |
|
|
912 | # e.g. "invoke s" |
|
|
913 | # "invoke small fireball" = 8 |
|
|
914 | # "invoke summon pet monster" = 8 |
|
|
915 | |
|
|
916 | if ($_ =~ $regexp_partial) { |
|
|
917 | push @scores, [$_, length $text, $_]; |
|
|
918 | } |
|
|
919 | |
|
|
920 | # 3. Abbreviation match |
|
|
921 | # attempts to use first word of text as an abbreviated command |
|
|
922 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
923 | |
|
|
924 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
925 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
926 | } |
|
|
927 | |
|
|
928 | # Pick the best option for this command |
|
|
929 | push @match, (sort { |
|
|
930 | $b->[1] <=> $a->[1] |
|
|
931 | } @scores)[0]; |
|
|
932 | } |
947 | } |
933 | |
948 | |
934 | # @match is now [command object, command with arguments] |
949 | # @match is now [command object, command with arguments] |
935 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
950 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
936 | sort { |
951 | sort { |