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 | |
… | |
… | |
25 | tilesize => 32, |
31 | tilesize => 32, |
26 | @_ |
32 | @_ |
27 | ); |
33 | ); |
28 | |
34 | |
29 | $self |
35 | $self |
30 | } |
|
|
31 | |
|
|
32 | sub 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 | |
|
|
44 | sub clr_commands { |
|
|
45 | my ($self) = @_; |
|
|
46 | |
|
|
47 | %{$::COMPLETER->{command}} = (); |
|
|
48 | |
|
|
49 | $::COMPLETER->hide |
|
|
50 | if $::COMPLETER; |
|
|
51 | } |
36 | } |
52 | |
37 | |
53 | sub server_login { |
38 | sub 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 | |
398 | sub scroll { |
383 | sub scroll { |
399 | my ($self, $dx, $dy) = @_; |
384 | my ($self, $dx, $dy) = @_; |
400 | |
385 | |
401 | $::MAP->scroll ($dx, $dy); |
|
|
402 | |
|
|
403 | $self->movement_update; |
386 | $self->movement_update; |
404 | |
387 | |
405 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
388 | $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement |
406 | $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); |
407 | } |
399 | } |
408 | |
400 | |
409 | sub set_magicmap { |
401 | sub set_magicmap { |
410 | my ($self, $w, $h, $x, $y, $data) = @_; |
402 | my ($self, $w, $h, $x, $y, $data) = @_; |
411 | |
403 | |
… | |
… | |
420 | sub movement_update { |
412 | sub movement_update { |
421 | my ($self) = @_; |
413 | my ($self) = @_; |
422 | |
414 | |
423 | if ($::CFG->{smooth_movement}) { |
415 | if ($::CFG->{smooth_movement}) { |
424 | if ($self->{sdx} || $self->{sdy}) { |
416 | if ($self->{sdx} || $self->{sdy}) { |
425 | my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); |
417 | my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); |
426 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
418 | my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; |
427 | |
419 | |
428 | # the minimum time for a single tile movement |
420 | # the minimum time for a single tile movement |
429 | 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); |
430 | |
422 | |
… | |
… | |
451 | } |
443 | } |
452 | } else { |
444 | } else { |
453 | $self->{sdx} = $self->{sdy} = 0; |
445 | $self->{sdx} = $self->{sdy} = 0; |
454 | } |
446 | } |
455 | |
447 | |
456 | $self->{last_update} = EV::time; |
448 | $self->{last_update} = AE::time; |
457 | } |
449 | } |
458 | |
450 | |
459 | sub refresh_hook { |
451 | sub refresh_hook { |
460 | my ($self) = @_; |
452 | my ($self) = @_; |
461 | |
453 | |
… | |
… | |
482 | |
474 | |
483 | 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; |
484 | 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; |
485 | |
477 | |
486 | if ($::CFG->{fow_enable}) { |
478 | if ($::CFG->{fow_enable}) { |
487 | $sdx_t = $sdy_t = 0;#d# |
479 | # draw_fow_texture REQUIRES the fow texture to stay the same size. |
488 | my ($w, $h, $data) = $::MAP->fow_texture ( |
480 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
489 | $dx + (min 0, $sdx_t), |
|
|
490 | $dy + (min 0, $sdy_t), |
|
|
491 | $sw + abs $sdx_t, |
|
|
492 | $sh + abs $sdy_t |
|
|
493 | ); |
|
|
494 | |
481 | |
495 | $self->{fow_texture} = new DC::Texture |
482 | $self->{fow_texture} = new DC::Texture |
496 | w => $w, |
483 | w => $w, |
497 | h => $h, |
484 | h => $h, |
498 | data => $data, |
485 | data => $data, |
… | |
… | |
508 | glTranslate $sx0, $sy0; |
495 | glTranslate $sx0, $sy0; |
509 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
496 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
510 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
497 | glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; |
511 | |
498 | |
512 | $::MAP->draw ($dx, $dy, $sw, $sh, |
499 | $::MAP->draw ($dx, $dy, $sw, $sh, |
513 | $self->{tilesize}, |
500 | ($self->{tilesize}) x 2, |
514 | $::CONN->{player}{tag}, |
501 | $::CONN->{player}{tag}, |
515 | -$self->{sdx}, -$self->{sdy}); |
502 | -$self->{sdx}, -$self->{sdy}); |
516 | |
503 | |
517 | #glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player |
|
|
518 | glScale $self->{tilesize}, $self->{tilesize}; |
504 | glScale $self->{tilesize}, $self->{tilesize}; |
519 | |
505 | |
520 | if (my $tex = $self->{fow_texture}) { |
506 | if (my $tex = $self->{fow_texture}) { |
521 | glPushMatrix; |
507 | my @prev_fow_params; |
522 | glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); |
|
|
523 | glScale 1/3, 1/3; |
|
|
524 | glEnable GL_TEXTURE_2D; |
|
|
525 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
526 | |
508 | |
|
|
509 | if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { |
|
|
510 | my $d1 = DC::distance $self->{sdx}, $self->{sdy}; |
|
|
511 | my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; |
|
|
512 | |
|
|
513 | if ($d1 * $d2) { |
|
|
514 | @prev_fow_params = ( |
|
|
515 | (min 1, $d1 / $d2), |
|
|
516 | $self->{lmdx} - $dx - $self->{lfdx}, |
|
|
517 | $self->{lmdy} - $dy - $self->{lfdy}, |
|
|
518 | @{$self->{prev_fow_texture}}{qw(name data)} |
|
|
519 | ); |
|
|
520 | } |
|
|
521 | } |
|
|
522 | |
|
|
523 | DC::Texture::draw_fow_texture |
527 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
524 | $::CFG->{fow_intensity}, |
528 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
525 | $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, |
529 | |
526 | @{$self->{fow_texture}}{qw(name data s t w h)}, |
530 | glDisable GL_TEXTURE_2D; |
527 | @prev_fow_params; |
531 | glPopMatrix; |
|
|
532 | } |
528 | } |
533 | |
529 | |
534 | if ($self->{magicmap}) { |
530 | if ($self->{magicmap}) { |
535 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
531 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
536 | |
532 | |
537 | $x += $::MAP->ox + $self->{dx}; |
533 | $x += $::MAP->ox + $self->{dx}; |
538 | $y += $::MAP->oy + $self->{dy}; |
534 | $y += $::MAP->oy + $self->{dy}; |
539 | |
535 | |
540 | glTranslate - $x - 1, - $y - 1; |
536 | glTranslate - $x - 1, - $y - 1; |
541 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
537 | glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; |
542 | $::MAP->draw_magicmap ($x, $y, $w, $h, $data); |
538 | $::MAP->draw_magicmap ($w, $h, $data); |
543 | } |
539 | } |
544 | |
540 | |
545 | glPopMatrix; |
541 | glPopMatrix; |
546 | glEndList; |
542 | glEndList; |
547 | } |
543 | } |
548 | } else { |
544 | } else { |
|
|
545 | delete $self->{last_fow_texture}; |
|
|
546 | delete $self->{fow_texture}; |
|
|
547 | |
549 | glDeleteList delete $self->{list} |
548 | glDeleteList delete $self->{list} |
550 | if $self->{list}; |
549 | if $self->{list}; |
551 | } |
550 | } |
552 | } |
551 | } |
553 | |
552 | |
… | |
… | |
589 | $self->SUPER::DESTROY; |
588 | $self->SUPER::DESTROY; |
590 | } |
589 | } |
591 | |
590 | |
592 | package DC::MapWidget::MapMap; |
591 | package DC::MapWidget::MapMap; |
593 | |
592 | |
594 | use strict; |
593 | use common::sense; |
595 | use utf8; |
|
|
596 | |
594 | |
597 | our @ISA = DC::UI::Base::; |
595 | our @ISA = DC::UI::Base::; |
598 | |
596 | |
599 | use Time::HiRes qw(time); |
|
|
600 | use DC::OpenGL; |
597 | use DC::OpenGL; |
601 | |
598 | |
602 | sub size_request { |
599 | sub size_request { |
603 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
600 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
604 | } |
601 | } |
605 | |
602 | |
606 | sub refresh_hook { |
603 | sub refresh_hook { |
607 | my ($self) = @_; |
604 | my ($self) = @_; |
608 | |
605 | |
609 | if ($::MAP && $self->{texture_atime} < time) { |
606 | if ($::MAP && $self->{texture_atime} < AE::now) { |
610 | my ($w, $h) = @$self{qw(w h)}; |
607 | my ($w, $h) = @$self{qw(w h)}; |
611 | |
608 | |
612 | return unless $w && $h; |
609 | return unless $w && $h; |
613 | |
610 | |
614 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
611 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
… | |
… | |
629 | $self->{sh} = $sh; |
626 | $self->{sh} = $sh; |
630 | |
627 | |
631 | $self->{x0} = $x0; |
628 | $self->{x0} = $x0; |
632 | $self->{y0} = $y0; |
629 | $self->{y0} = $y0; |
633 | |
630 | |
634 | $self->{texture_atime} = time + 1/3; |
631 | $self->{texture_atime} = AE::now + 1/2; |
635 | |
632 | |
636 | $self->{texture} = |
633 | $self->{texture} = |
637 | new DC::Texture |
634 | new DC::Texture |
638 | w => $w, |
635 | w => $w, |
639 | h => $h, |
636 | h => $h, |
… | |
… | |
694 | glDisable GL_BLEND; |
691 | glDisable GL_BLEND; |
695 | } |
692 | } |
696 | |
693 | |
697 | package DC::MapWidget::Command; |
694 | package DC::MapWidget::Command; |
698 | |
695 | |
699 | use strict; |
696 | use common::sense; |
700 | |
697 | |
701 | use DC::OpenGL; |
698 | use DC::OpenGL; |
702 | |
699 | |
703 | our @ISA = DC::UI::Frame::; |
700 | our @ISA = DC::UI::Frame::; |
704 | |
701 | |
705 | sub new { |
702 | sub new { |
706 | my $class = shift; |
703 | my $class = shift; |
707 | |
704 | |
708 | my $self = $class->SUPER::new ( |
705 | my $self = $class->SUPER::new ( |
|
|
706 | min_w => $::WIDTH * 0.25, # workaround for layout problems #d# |
709 | bg => [0, 0, 0, 0.8], |
707 | bg => [0, 0, 0, 0.8], |
710 | @_, |
708 | @_, |
711 | ); |
709 | ); |
712 | |
710 | |
713 | $self->add ($self->{vbox} = new DC::UI::VBox); |
711 | $self->add ($self->{vbox} = new DC::UI::VBox); |
… | |
… | |
800 | ); |
798 | ); |
801 | |
799 | |
802 | $self |
800 | $self |
803 | } |
801 | } |
804 | |
802 | |
|
|
803 | sub reset { |
|
|
804 | my ($self) = @_; |
|
|
805 | |
|
|
806 | $self->hide; |
|
|
807 | delete $self->{command_list}; |
|
|
808 | } |
|
|
809 | |
805 | sub set_prefix { |
810 | sub set_prefix { |
806 | my ($self, $prefix) = @_; |
811 | my ($self, $prefix) = @_; |
807 | |
812 | |
808 | $self->{entry}->set_text ($prefix); |
813 | $self->{entry}->set_text ($prefix); |
809 | $self->show; |
814 | $self->show; |
… | |
… | |
845 | |
850 | |
846 | my $text = $self->{entry}->get_text; |
851 | my $text = $self->{entry}->get_text; |
847 | |
852 | |
848 | length $text |
853 | length $text |
849 | 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 | ]; |
850 | |
863 | |
851 | if ($text ne $self->{last_search}) { |
864 | if ($text ne $self->{last_search}) { |
852 | my @match; |
865 | my @match; |
853 | |
866 | |
854 | if ($text =~ /^(.*?)\s+$/) { |
867 | if ($text =~ /^(.*?)\s+$/) { |
… | |
… | |
857 | } else { |
870 | } else { |
858 | # @match is [command, penalty, command with arguments] until sort |
871 | # @match is [command, penalty, command with arguments] until sort |
859 | |
872 | |
860 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
873 | my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; |
861 | |
874 | |
|
|
875 | my $first_char = substr $cmd, 0, 1; |
|
|
876 | |
862 | my $regexp_abbrev = do { |
877 | my $regexp_abbrev = do { |
863 | my ($beg, @chr) = split //, lc $cmd; |
878 | my ($beg, @chr) = split //, lc $cmd; |
864 | |
879 | |
865 | # the following regex is used to match our "completion entry" |
880 | # the following regex is used to match our "completion entry" |
866 | # to an actual command - the parentheses match kind of "overhead" |
881 | # to an actual command - the parentheses match kind of "overhead" |
867 | # - the more characters the parentheses match, the less attractive |
882 | # - the more characters the parentheses match, the less attractive |
868 | # is the match. |
883 | # is the match. |
869 | my $regexp = "^\Q$beg\E" |
884 | my $regexp = "^\Q$beg\E" |
870 | . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; |
885 | . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; |
871 | qr<$regexp> |
886 | qr<$regexp> |
872 | }; |
887 | }; |
873 | |
888 | |
874 | my $regexp_partial = do { |
889 | my $regexp_partial = do { |
875 | my $regexp = "^\Q$text\E(.*)"; |
890 | my $regexp = "^\Q$text\E(.*)"; |
876 | qr<$regexp> |
891 | qr<$regexp> |
877 | }; |
892 | }; |
878 | |
893 | |
879 | 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 | |
880 | my @scores; |
900 | my @scores; |
881 | |
901 | |
882 | # 1. Complete command [with args] |
902 | # 1. Complete command [with args] |
883 | # command is a prefix of the text |
903 | # command is a prefix of the text |
884 | # score is length of complete command matched |
904 | # score is length of complete command matched |
885 | # e.g. "invoke summon pet monster bat" |
905 | # e.g. "invoke summon pet monster bat" |
886 | # "invoke" "summon pet monster bat" = 6 |
906 | # "invoke" "summon pet monster bat" = 6 |
887 | # "invoke summon pet monster" "bat" = 25 |
907 | # "invoke summon pet monster" "bat" = 25 |
888 | if ($text =~ /^\Q$_\E(.*)/) { |
908 | if ((substr $text, 0, length $_) eq $_) { |
889 | 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]; |
890 | } |
935 | } |
891 | |
|
|
892 | # 2. Partial command |
|
|
893 | # text is a prefix of the full command |
|
|
894 | # score is the length of the input text |
|
|
895 | # e.g. "invoke s" |
|
|
896 | # "invoke small fireball" = 8 |
|
|
897 | # "invoke summon pet monster" = 8 |
|
|
898 | |
|
|
899 | if ($_ =~ $regexp_partial) { |
|
|
900 | push @scores, [$_, length $text, $_]; |
|
|
901 | } |
|
|
902 | |
|
|
903 | # 3. Abbreviation match |
|
|
904 | # attempts to use first word of text as an abbreviated command |
|
|
905 | # score is length of word + 1 - 3 per non-word-initial character |
|
|
906 | |
|
|
907 | if (my @penalty = $_ =~ $regexp_abbrev) { |
|
|
908 | push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; |
|
|
909 | } |
|
|
910 | |
|
|
911 | # Pick the best option for this command |
|
|
912 | push @match, (sort { |
|
|
913 | $b->[1] <=> $a->[1] |
|
|
914 | } @scores)[0]; |
|
|
915 | } |
936 | } |
916 | |
937 | |
917 | # @match is now [command object, command with arguments] |
938 | # @match is now [command object, command with arguments] |
918 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
939 | @match = map [$self->{command}{$_->[0]}, $_->[2]], |
919 | sort { |
940 | sort { |