1 | package DC::MapWidget; |
1 | package DC::MapWidget; |
2 | |
2 | |
3 | use strict; |
3 | use common::sense; |
4 | use utf8; |
|
|
5 | |
4 | |
6 | use List::Util qw(min max); |
5 | use List::Util qw(min max); |
|
|
6 | |
|
|
7 | use AnyEvent (); |
7 | |
8 | |
8 | use DC; |
9 | use DC; |
9 | use DC::OpenGL; |
10 | use DC::OpenGL; |
10 | use DC::UI; |
11 | use DC::UI; |
11 | use DC::Macro; |
12 | use DC::Macro; |
12 | |
13 | |
13 | our @ISA = DC::UI::Base::; |
14 | our @ISA = DC::UI::Base::; |
|
|
15 | |
|
|
16 | our @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 | |
15 | my $magicmap_tex = |
21 | my $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 | |
… | |
… | |
27 | ); |
33 | ); |
28 | |
34 | |
29 | $self |
35 | $self |
30 | } |
36 | } |
31 | |
37 | |
32 | sub add_command { |
38 | sub add_command {#d# remove |
33 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
39 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
34 | |
40 | |
35 | (my $data = $command) =~ s/\\//g; |
41 | (my $data = $command) =~ s/\\//g; |
36 | |
42 | |
37 | $tooltip =~ s/^\s+//; |
43 | $tooltip =~ s/^\s+//; |
38 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
44 | $tooltip = "<big>$data</big>\n\n$tooltip"; |
39 | $tooltip =~ s/\s+$//; |
45 | $tooltip =~ s/\s+$//; |
40 | |
46 | |
41 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
47 | $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; |
42 | } |
|
|
43 | |
|
|
44 | sub clr_commands { |
|
|
45 | my ($self) = @_; |
|
|
46 | |
|
|
47 | %{$::COMPLETER->{command}} = (); |
|
|
48 | |
|
|
49 | $::COMPLETER->hide |
|
|
50 | if $::COMPLETER; |
|
|
51 | } |
48 | } |
52 | |
49 | |
53 | sub server_login { |
50 | sub server_login { |
54 | my ($server) = @_; |
51 | my ($server) = @_; |
55 | |
52 | |
… | |
… | |
209 | # push @items, [ |
206 | # push @items, [ |
210 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
207 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
211 | # \&editor_invoke, |
208 | # \&editor_invoke, |
212 | # ]; |
209 | # ]; |
213 | |
210 | |
214 | for my $type (qw(test name)) { |
211 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
215 | $::CONN->{editor_support}{type} ne $type |
212 | $::CONN->{editor_support}{servertype} ne $type |
216 | or next; |
213 | or next; |
217 | my $server = $::CONN->{editor_support}{"${type}server"} |
214 | my $server = $::CONN->{editor_support}{"${type}server"} |
218 | or next; |
215 | or next; |
219 | |
216 | |
220 | push @items, [ |
217 | push @items, [ |
… | |
… | |
402 | |
399 | |
403 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
400 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
404 | $self->{sdy} += $dy * $self->{tilesize}; |
401 | $self->{sdy} += $dy * $self->{tilesize}; |
405 | |
402 | |
406 | # save old fow texture, if applicable |
403 | # save old fow texture, if applicable |
407 | $self->{last_fow_texture} = $self->{fow_texture} |
404 | $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture}; |
408 | if $::CFG->{smooth_transitions}; |
|
|
409 | $self->{lfdx} = $dx; |
405 | $self->{lfdx} = $dx; |
410 | $self->{lfdy} = $dy; |
406 | $self->{lfdy} = $dy; |
411 | $self->{lmdx} = $self->{dx}; |
407 | $self->{lmdx} = $self->{dx}; |
412 | $self->{lmdy} = $self->{dy}; |
408 | $self->{lmdy} = $self->{dy}; |
413 | |
409 | |
… | |
… | |
428 | sub movement_update { |
424 | sub movement_update { |
429 | my ($self) = @_; |
425 | my ($self) = @_; |
430 | |
426 | |
431 | if ($::CFG->{smooth_movement}) { |
427 | if ($::CFG->{smooth_movement}) { |
432 | if ($self->{sdx} || $self->{sdy}) { |
428 | if ($self->{sdx} || $self->{sdy}) { |
433 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
429 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
434 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
430 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
435 | |
431 | |
436 | # the minimum time for a single tile movement |
432 | # the minimum time for a single tile movement |
437 | 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); |
438 | |
434 | |
… | |
… | |
459 | } |
455 | } |
460 | } else { |
456 | } else { |
461 | $self->{sdx} = $self->{sdy} = 0; |
457 | $self->{sdx} = $self->{sdy} = 0; |
462 | } |
458 | } |
463 | |
459 | |
464 | $self->{last_update} = EV::time; |
460 | $self->{last_update} = AE::time; |
465 | } |
461 | } |
466 | |
462 | |
467 | sub refresh_hook { |
463 | sub refresh_hook { |
468 | my ($self) = @_; |
464 | my ($self) = @_; |
469 | |
465 | |
… | |
… | |
511 | glTranslate $sx0, $sy0; |
507 | glTranslate $sx0, $sy0; |
512 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
508 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
513 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
509 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
514 | |
510 | |
515 | $::MAP->draw ($dx, $dy, $sw, $sh, |
511 | $::MAP->draw ($dx, $dy, $sw, $sh, |
516 | $self->{tilesize}, |
512 | ($self->{tilesize}) x 2, |
517 | $::CONN->{player}{tag}, |
513 | $::CONN->{player}{tag}, |
518 | -$self->{sdx}, -$self->{sdy}); |
514 | -$self->{sdx}, -$self->{sdy}); |
519 | |
515 | |
520 | glScale $self->{tilesize}, $self->{tilesize}; |
516 | glScale $self->{tilesize}, $self->{tilesize}; |
521 | |
517 | |
522 | if (my $tex = $self->{fow_texture}) { |
518 | if (my $tex = $self->{fow_texture}) { |
|
|
519 | my @prev_fow_params; |
|
|
520 | |
523 | if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d# |
521 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
524 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
522 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
525 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
523 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
526 | |
524 | |
527 | if ($d1 * $d2) { |
525 | if ($d1 * $d2) { |
528 | DC::Texture::draw_fow_texture |
526 | @prev_fow_params = ( |
529 | $::CFG->{fow_intensity}, |
|
|
530 | $self->{hidden_tex}{name}, |
|
|
531 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
|
|
532 | (min 1, $d1 / $d2), |
527 | (min 1, $d1 / $d2), |
533 | $self->{lmdx} - $dx - $self->{lfdx}, |
528 | $self->{lmdx} - $dx - $self->{lfdx}, |
534 | $self->{lmdy} - $dy - $self->{lfdy}, |
529 | $self->{lmdy} - $dy - $self->{lfdy}, |
535 | @{$self->{last_fow_texture}}{qw(name data)}; |
530 | @{$self->{prev_fow_texture}}{qw(name data)} |
536 | } else { |
531 | ); |
537 | delete $self->{last_fow_texture}; |
|
|
538 | } |
532 | } |
539 | } |
533 | } |
540 | |
534 | |
541 | unless ($self->{last_fow_texture}) { |
|
|
542 | DC::Texture::draw_fow_texture |
535 | DC::Texture::draw_fow_texture |
543 | $::CFG->{fow_intensity}, |
536 | $::CFG->{fow_intensity}, |
544 | $self->{hidden_tex}{name}, |
537 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
545 | @{$self->{fow_texture}}{qw(name data s t w h)}; |
538 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
546 | } |
539 | @prev_fow_params; |
547 | } |
540 | } |
548 | |
541 | |
549 | if ($self->{magicmap}) { |
542 | if ($self->{magicmap}) { |
550 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
543 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
551 | |
544 | |
… | |
… | |
607 | $self->SUPER::DESTROY; |
600 | $self->SUPER::DESTROY; |
608 | } |
601 | } |
609 | |
602 | |
610 | package DC::MapWidget::MapMap; |
603 | package DC::MapWidget::MapMap; |
611 | |
604 | |
612 | use strict; |
605 | use common::sense; |
613 | use utf8; |
|
|
614 | |
606 | |
615 | our @ISA = DC::UI::Base::; |
607 | our @ISA = DC::UI::Base::; |
616 | |
608 | |
617 | use Time::HiRes qw(time); |
|
|
618 | use DC::OpenGL; |
609 | use DC::OpenGL; |
619 | |
610 | |
620 | sub size_request { |
611 | sub size_request { |
621 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
612 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
622 | } |
613 | } |
623 | |
614 | |
624 | sub refresh_hook { |
615 | sub refresh_hook { |
625 | my ($self) = @_; |
616 | my ($self) = @_; |
626 | |
617 | |
627 | if ($::MAP && $self->{texture_atime} < time) { |
618 | if ($::MAP && $self->{texture_atime} < AE::now) { |
628 | my ($w, $h) = @$self{qw(w h)}; |
619 | my ($w, $h) = @$self{qw(w h)}; |
629 | |
620 | |
630 | return unless $w && $h; |
621 | return unless $w && $h; |
631 | |
622 | |
632 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
623 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
… | |
… | |
647 | $self->{sh} = $sh; |
638 | $self->{sh} = $sh; |
648 | |
639 | |
649 | $self->{x0} = $x0; |
640 | $self->{x0} = $x0; |
650 | $self->{y0} = $y0; |
641 | $self->{y0} = $y0; |
651 | |
642 | |
652 | $self->{texture_atime} = time + 1/3; |
643 | $self->{texture_atime} = AE::now + 1/2; |
653 | |
644 | |
654 | $self->{texture} = |
645 | $self->{texture} = |
655 | new DC::Texture |
646 | new DC::Texture |
656 | w => $w, |
647 | w => $w, |
657 | h => $h, |
648 | h => $h, |
… | |
… | |
712 | glDisable GL_BLEND; |
703 | glDisable GL_BLEND; |
713 | } |
704 | } |
714 | |
705 | |
715 | package DC::MapWidget::Command; |
706 | package DC::MapWidget::Command; |
716 | |
707 | |
717 | use strict; |
708 | use common::sense; |
718 | |
709 | |
719 | use DC::OpenGL; |
710 | use DC::OpenGL; |
720 | |
711 | |
721 | our @ISA = DC::UI::Frame::; |
712 | our @ISA = DC::UI::Frame::; |
722 | |
713 | |
… | |
… | |
818 | ); |
809 | ); |
819 | |
810 | |
820 | $self |
811 | $self |
821 | } |
812 | } |
822 | |
813 | |
|
|
814 | sub reset { |
|
|
815 | my ($self) = @_; |
|
|
816 | |
|
|
817 | $self->hide; |
|
|
818 | delete $self->{command_list}; |
|
|
819 | } |
|
|
820 | |
823 | sub set_prefix { |
821 | sub set_prefix { |
824 | my ($self, $prefix) = @_; |
822 | my ($self, $prefix) = @_; |
825 | |
823 | |
826 | $self->{entry}->set_text ($prefix); |
824 | $self->{entry}->set_text ($prefix); |
827 | $self->show; |
825 | $self->show; |
… | |
… | |
863 | |
861 | |
864 | my $text = $self->{entry}->get_text; |
862 | my $text = $self->{entry}->get_text; |
865 | |
863 | |
866 | length $text |
864 | length $text |
867 | 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 | ]; |
868 | |
874 | |
869 | if ($text ne $self->{last_search}) { |
875 | if ($text ne $self->{last_search}) { |
870 | my @match; |
876 | my @match; |
871 | |
877 | |
872 | if ($text =~ /^(.*?)\s+$/) { |
878 | if ($text =~ /^(.*?)\s+$/) { |
… | |
… | |
875 | } else { |
881 | } else { |
876 | # @match is [command, penalty, command with arguments] until sort |
882 | # @match is [command, penalty, command with arguments] until sort |
877 | |
883 | |
878 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
884 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
879 | |
885 | |
|
|
886 | my $first_char = substr $cmd, 0, 1; |
|
|
887 | |
880 | my $regexp_abbrev = do { |
888 | my $regexp_abbrev = do { |
881 | my ($beg, @chr) = split //, lc $cmd; |
889 | my ($beg, @chr) = split //, lc $cmd; |
882 | |
890 | |
883 | # the following regex is used to match our "completion entry" |
891 | # the following regex is used to match our "completion entry" |
884 | # to an actual command - the parentheses match kind of "overhead" |
892 | # to an actual command - the parentheses match kind of "overhead" |
885 | # - the more characters the parentheses match, the less attractive |
893 | # - the more characters the parentheses match, the less attractive |
886 | # is the match. |
894 | # is the match. |
887 | my $regexp = "^\Q$beg\E" |
895 | my $regexp = "^\Q$beg\E" |
888 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
896 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
889 | qr<$regexp> |
897 | qr<$regexp> |
890 | }; |
898 | }; |
891 | |
899 | |
892 | my $regexp_partial = do { |
900 | my $regexp_partial = do { |
893 | my $regexp = "^\Q$text\E(.*)"; |
901 | my $regexp = "^\Q$text\E(.*)"; |
894 | qr<$regexp> |
902 | qr<$regexp> |
895 | }; |
903 | }; |
896 | |
904 | |
897 | 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 | |
898 | my @scores; |
911 | my @scores; |
899 | |
912 | |
900 | # 1. Complete command [with args] |
913 | # 1. Complete command [with args] |
901 | # command is a prefix of the text |
914 | # command is a prefix of the text |
902 | # score is length of complete command matched |
915 | # score is length of complete command matched |
903 | # e.g. "invoke summon pet monster bat" |
916 | # e.g. "invoke summon pet monster bat" |
904 | # "invoke" "summon pet monster bat" = 6 |
917 | # "invoke" "summon pet monster bat" = 6 |
905 | # "invoke summon pet monster" "bat" = 25 |
918 | # "invoke summon pet monster" "bat" = 25 |
906 | if ($text =~ /^\Q$_\E(.*)/) { |
919 | if ((substr $text, 0, length $_) eq $_) { |
907 | 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]; |
908 | } |
946 | } |
909 | |
|
|
910 | # 2. Partial command |
|
|
911 | # text is a prefix of the full command |
|
|
912 | # score is the length of the input text |
|
|
913 | # e.g. "invoke s" |
|
|
914 | # "invoke small fireball" = 8 |
|
|
915 | # "invoke summon pet monster" = 8 |
|
|
916 | |
|
|
917 | if ($_ =~ $regexp_partial) { |
|
|
918 | push @scores, [$_, length $text, $_]; |
|
|
919 | } |
|
|
920 | |
|
|
921 | # 3. Abbreviation match |
|
|
922 | # attempts to use first word of text as an abbreviated command |
|
|
923 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
924 | |
|
|
925 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
926 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
927 | } |
|
|
928 | |
|
|
929 | # Pick the best option for this command |
|
|
930 | push @match, (sort { |
|
|
931 | $b->[1] <=> $a->[1] |
|
|
932 | } @scores)[0]; |
|
|
933 | } |
947 | } |
934 | |
948 | |
935 | # @match is now [command object, command with arguments] |
949 | # @match is now [command object, command with arguments] |
936 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
950 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
937 | sort { |
951 | sort { |