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 | |
… | |
… | |
209 | # push @items, [ |
215 | # push @items, [ |
210 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
216 | # "Edit this map <span size='xx-small'>(" . (DC::asxml $::CONN->{map_info}[0]) . ")</span>", |
211 | # \&editor_invoke, |
217 | # \&editor_invoke, |
212 | # ]; |
218 | # ]; |
213 | |
219 | |
214 | for my $type (qw(test name)) { |
220 | for my $type (@{ $::CONN->{editor_support}{servertypes} }) { |
215 | $::CONN->{editor_support}{type} ne $type |
221 | $::CONN->{editor_support}{servertype} ne $type |
216 | or next; |
222 | or next; |
217 | my $server = $::CONN->{editor_support}{"${type}server"} |
223 | my $server = $::CONN->{editor_support}{"${type}server"} |
218 | or next; |
224 | or next; |
219 | |
225 | |
220 | push @items, [ |
226 | push @items, [ |
… | |
… | |
402 | |
408 | |
403 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
409 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
404 | $self->{sdy} += $dy * $self->{tilesize}; |
410 | $self->{sdy} += $dy * $self->{tilesize}; |
405 | |
411 | |
406 | # save old fow texture, if applicable |
412 | # save old fow texture, if applicable |
407 | $self->{last_fow_texture} = $self->{fow_texture} |
413 | $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture}; |
408 | if $::CFG->{smooth_transitions}; |
|
|
409 | $self->{lfdx} = $dx; |
414 | $self->{lfdx} = $dx; |
410 | $self->{lfdy} = $dy; |
415 | $self->{lfdy} = $dy; |
411 | $self->{lmdx} = $self->{dx}; |
416 | $self->{lmdx} = $self->{dx}; |
412 | $self->{lmdy} = $self->{dy}; |
417 | $self->{lmdy} = $self->{dy}; |
413 | |
418 | |
… | |
… | |
428 | sub movement_update { |
433 | sub movement_update { |
429 | my ($self) = @_; |
434 | my ($self) = @_; |
430 | |
435 | |
431 | if ($::CFG->{smooth_movement}) { |
436 | if ($::CFG->{smooth_movement}) { |
432 | if ($self->{sdx} || $self->{sdy}) { |
437 | if ($self->{sdx} || $self->{sdy}) { |
433 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
438 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
434 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
439 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
435 | |
440 | |
436 | # the minimum time for a single tile movement |
441 | # the minimum time for a single tile movement |
437 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
442 | my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1); |
438 | |
443 | |
… | |
… | |
459 | } |
464 | } |
460 | } else { |
465 | } else { |
461 | $self->{sdx} = $self->{sdy} = 0; |
466 | $self->{sdx} = $self->{sdy} = 0; |
462 | } |
467 | } |
463 | |
468 | |
464 | $self->{last_update} = EV::time; |
469 | $self->{last_update} = AE::time; |
465 | } |
470 | } |
466 | |
471 | |
467 | sub refresh_hook { |
472 | sub refresh_hook { |
468 | my ($self) = @_; |
473 | my ($self) = @_; |
469 | |
474 | |
… | |
… | |
490 | |
495 | |
491 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
496 | my $dx = $self->{dx} = DC::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
492 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
497 | my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
493 | |
498 | |
494 | if ($::CFG->{fow_enable}) { |
499 | if ($::CFG->{fow_enable}) { |
|
|
500 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
495 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
501 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
496 | |
502 | |
497 | $self->{fow_texture} = new DC::Texture |
503 | $self->{fow_texture} = new DC::Texture |
498 | w => $w, |
504 | w => $w, |
499 | h => $h, |
505 | h => $h, |
… | |
… | |
510 | glTranslate $sx0, $sy0; |
516 | glTranslate $sx0, $sy0; |
511 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
517 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
512 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
518 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
513 | |
519 | |
514 | $::MAP->draw ($dx, $dy, $sw, $sh, |
520 | $::MAP->draw ($dx, $dy, $sw, $sh, |
515 | $self->{tilesize}, |
521 | ($self->{tilesize}) x 2, |
516 | $::CONN->{player}{tag}, |
522 | $::CONN->{player}{tag}, |
517 | -$self->{sdx}, -$self->{sdy}); |
523 | -$self->{sdx}, -$self->{sdy}); |
518 | |
524 | |
519 | glScale $self->{tilesize}, $self->{tilesize}; |
525 | glScale $self->{tilesize}, $self->{tilesize}; |
520 | |
526 | |
521 | if (my $tex = $self->{fow_texture}) { |
527 | if (my $tex = $self->{fow_texture}) { |
|
|
528 | my @prev_fow_params; |
|
|
529 | |
522 | if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d# |
530 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
523 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
531 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
524 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
532 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
525 | |
533 | |
526 | if ($d1 * $d2) { |
534 | if ($d1 * $d2) { |
527 | DC::Texture::draw_fow_texture |
535 | @prev_fow_params = ( |
528 | $::CFG->{fow_intensity}, |
|
|
529 | @{$self->{fow_texture}}{qw(name s t w h)}, |
|
|
530 | (min 1, $d1 / $d2), |
536 | (min 1, $d1 / $d2), |
531 | $self->{lmdx} - $dx - $self->{lfdx}, |
537 | $self->{lmdx} - $dx - $self->{lfdx}, |
532 | $self->{lmdy} - $dy - $self->{lfdy}, |
538 | $self->{lmdy} - $dy - $self->{lfdy}, |
533 | @{$self->{last_fow_texture}}{qw(name s t w h)}; |
539 | @{$self->{prev_fow_texture}}{qw(name data)} |
534 | } else { |
540 | ); |
535 | delete $self->{last_fow_texture}; |
|
|
536 | } |
541 | } |
537 | } |
542 | } |
538 | |
543 | |
539 | unless ($self->{last_fow_texture}) { |
|
|
540 | DC::Texture::draw_fow_texture |
544 | DC::Texture::draw_fow_texture |
541 | $::CFG->{fow_intensity}, |
545 | $::CFG->{fow_intensity}, |
|
|
546 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
542 | @{$self->{fow_texture}}{qw(name s t w h)}; |
547 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
543 | } |
548 | @prev_fow_params; |
544 | } |
549 | } |
545 | |
550 | |
546 | if ($self->{magicmap}) { |
551 | if ($self->{magicmap}) { |
547 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
552 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
548 | |
553 | |
… | |
… | |
556 | |
561 | |
557 | glPopMatrix; |
562 | glPopMatrix; |
558 | glEndList; |
563 | glEndList; |
559 | } |
564 | } |
560 | } else { |
565 | } else { |
|
|
566 | delete $self->{last_fow_texture}; |
|
|
567 | delete $self->{fow_texture}; |
|
|
568 | |
561 | glDeleteList delete $self->{list} |
569 | glDeleteList delete $self->{list} |
562 | if $self->{list}; |
570 | if $self->{list}; |
563 | } |
571 | } |
564 | } |
572 | } |
565 | |
573 | |
… | |
… | |
601 | $self->SUPER::DESTROY; |
609 | $self->SUPER::DESTROY; |
602 | } |
610 | } |
603 | |
611 | |
604 | package DC::MapWidget::MapMap; |
612 | package DC::MapWidget::MapMap; |
605 | |
613 | |
606 | use strict; |
614 | use common::sense; |
607 | use utf8; |
|
|
608 | |
615 | |
609 | our @ISA = DC::UI::Base::; |
616 | our @ISA = DC::UI::Base::; |
610 | |
617 | |
611 | use Time::HiRes qw(time); |
|
|
612 | use DC::OpenGL; |
618 | use DC::OpenGL; |
613 | |
619 | |
614 | sub size_request { |
620 | sub size_request { |
615 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
621 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
616 | } |
622 | } |
617 | |
623 | |
618 | sub refresh_hook { |
624 | sub refresh_hook { |
619 | my ($self) = @_; |
625 | my ($self) = @_; |
620 | |
626 | |
621 | if ($::MAP && $self->{texture_atime} < time) { |
627 | if ($::MAP && $self->{texture_atime} < AE::now) { |
622 | my ($w, $h) = @$self{qw(w h)}; |
628 | my ($w, $h) = @$self{qw(w h)}; |
623 | |
629 | |
624 | return unless $w && $h; |
630 | return unless $w && $h; |
625 | |
631 | |
626 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
632 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
… | |
… | |
641 | $self->{sh} = $sh; |
647 | $self->{sh} = $sh; |
642 | |
648 | |
643 | $self->{x0} = $x0; |
649 | $self->{x0} = $x0; |
644 | $self->{y0} = $y0; |
650 | $self->{y0} = $y0; |
645 | |
651 | |
646 | $self->{texture_atime} = time + 1/3; |
652 | $self->{texture_atime} = AE::now + 1/2; |
647 | |
653 | |
648 | $self->{texture} = |
654 | $self->{texture} = |
649 | new DC::Texture |
655 | new DC::Texture |
650 | w => $w, |
656 | w => $w, |
651 | h => $h, |
657 | h => $h, |
… | |
… | |
706 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
707 | } |
713 | } |
708 | |
714 | |
709 | package DC::MapWidget::Command; |
715 | package DC::MapWidget::Command; |
710 | |
716 | |
711 | use strict; |
717 | use common::sense; |
712 | |
718 | |
713 | use DC::OpenGL; |
719 | use DC::OpenGL; |
714 | |
720 | |
715 | our @ISA = DC::UI::Frame::; |
721 | our @ISA = DC::UI::Frame::; |
716 | |
722 | |
… | |
… | |
869 | } else { |
875 | } else { |
870 | # @match is [command, penalty, command with arguments] until sort |
876 | # @match is [command, penalty, command with arguments] until sort |
871 | |
877 | |
872 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
878 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
873 | |
879 | |
|
|
880 | my $first_char = substr $cmd, 0, 1; |
|
|
881 | |
874 | my $regexp_abbrev = do { |
882 | my $regexp_abbrev = do { |
875 | my ($beg, @chr) = split //, lc $cmd; |
883 | my ($beg, @chr) = split //, lc $cmd; |
876 | |
884 | |
877 | # the following regex is used to match our "completion entry" |
885 | # the following regex is used to match our "completion entry" |
878 | # to an actual command - the parentheses match kind of "overhead" |
886 | # to an actual command - the parentheses match kind of "overhead" |
879 | # - the more characters the parentheses match, the less attractive |
887 | # - the more characters the parentheses match, the less attractive |
880 | # is the match. |
888 | # is the match. |
881 | my $regexp = "^\Q$beg\E" |
889 | my $regexp = "^\Q$beg\E" |
882 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
890 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
883 | qr<$regexp> |
891 | qr<$regexp> |
884 | }; |
892 | }; |
885 | |
893 | |
886 | my $regexp_partial = do { |
894 | my $regexp_partial = do { |
887 | my $regexp = "^\Q$text\E(.*)"; |
895 | my $regexp = "^\Q$text\E(.*)"; |
888 | qr<$regexp> |
896 | qr<$regexp> |
889 | }; |
897 | }; |
890 | |
898 | |
891 | for (keys %{$self->{command}}) { |
899 | for (keys %{$self->{command}}) { |
|
|
900 | # we only match and score if the first character matches, |
|
|
901 | # so quickly rule out all others first. |
|
|
902 | next unless $first_char = substr $_, 0, 1; |
|
|
903 | |
892 | my @scores; |
904 | my @scores; |
893 | |
905 | |
894 | # 1. Complete command [with args] |
906 | # 1. Complete command [with args] |
895 | # command is a prefix of the text |
907 | # command is a prefix of the text |
896 | # score is length of complete command matched |
908 | # score is length of complete command matched |
897 | # e.g. "invoke summon pet monster bat" |
909 | # e.g. "invoke summon pet monster bat" |
898 | # "invoke" "summon pet monster bat" = 6 |
910 | # "invoke" "summon pet monster bat" = 6 |
899 | # "invoke summon pet monster" "bat" = 25 |
911 | # "invoke summon pet monster" "bat" = 25 |
900 | if ($text =~ /^\Q$_\E(.*)/) { |
912 | if ((substr $text, 0, length $_) eq $_) { |
901 | push @scores, [$_, length $_, $text]; |
913 | push @scores, [$_, length $_, $text]; |
902 | } |
914 | } |
903 | |
915 | |
904 | # 2. Partial command |
916 | # 2. Partial command |
905 | # text is a prefix of the full command |
917 | # text is a prefix of the full command |