--- deliantra/Deliantra-Client/DC/MapWidget.pm 2009/12/21 23:52:34 1.158 +++ deliantra/Deliantra-Client/DC/MapWidget.pm 2012/11/20 14:32:12 1.167 @@ -1,10 +1,11 @@ package DC::MapWidget; -use strict; -use utf8; +use common::sense; use List::Util qw(min max); +use AnyEvent (); + use DC; use DC::OpenGL; use DC::UI; @@ -12,6 +13,11 @@ our @ISA = DC::UI::Base::; +our @TEX_HIDDEN = map { + new_from_resource DC::Texture # MUST be POT + "hidden-$_.png", mipmap => 1, wrap => 1 + } 0, 1, 2; + my $magicmap_tex = new_from_resource DC::Texture "magicmap.png", mipmap => 1, wrap => 0, internalformat => GL_ALPHA; @@ -29,7 +35,7 @@ $self } -sub add_command { +sub add_command {#d# remove my ($self, $command, $tooltip, $widget, $cb) = @_; (my $data = $command) =~ s/\\//g; @@ -41,15 +47,6 @@ $::COMPLETER->{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}]; } -sub clr_commands { - my ($self) = @_; - - %{$::COMPLETER->{command}} = (); - - $::COMPLETER->hide - if $::COMPLETER; -} - sub server_login { my ($server) = @_; @@ -211,8 +208,8 @@ # \&editor_invoke, # ]; - for my $type (qw(test name)) { - $::CONN->{editor_support}{type} ne $type + for my $type (@{ $::CONN->{editor_support}{servertypes} }) { + $::CONN->{editor_support}{servertype} ne $type or next; my $server = $::CONN->{editor_support}{"${type}server"} or next; @@ -404,8 +401,7 @@ $self->{sdy} += $dy * $self->{tilesize}; # save old fow texture, if applicable - $self->{last_fow_texture} = $self->{fow_texture} - if $::CFG->{smooth_transitions}; + $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture}; $self->{lfdx} = $dx; $self->{lfdy} = $dy; $self->{lmdx} = $self->{dx}; @@ -430,7 +426,7 @@ if ($::CFG->{smooth_movement}) { if ($self->{sdx} || $self->{sdy}) { - my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); + my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH); my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; # the minimum time for a single tile movement @@ -461,7 +457,7 @@ $self->{sdx} = $self->{sdy} = 0; } - $self->{last_update} = EV::time; + $self->{last_update} = AE::time; } sub refresh_hook { @@ -513,37 +509,34 @@ glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; $::MAP->draw ($dx, $dy, $sw, $sh, - $self->{tilesize}, + ($self->{tilesize}) x 2, $::CONN->{player}{tag}, -$self->{sdx}, -$self->{sdy}); glScale $self->{tilesize}, $self->{tilesize}; if (my $tex = $self->{fow_texture}) { - if ($DC::OpenGL::GL_MULTITEX && $self->{last_fow_texture}) {#d# + my @prev_fow_params; + + if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) { my $d1 = DC::distance $self->{sdx}, $self->{sdy}; my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize; if ($d1 * $d2) { - DC::Texture::draw_fow_texture - $::CFG->{fow_intensity}, - $self->{hidden_tex}{name}, - @{$self->{fow_texture}}{qw(name data s t w h)}, + @prev_fow_params = ( (min 1, $d1 / $d2), $self->{lmdx} - $dx - $self->{lfdx}, $self->{lmdy} - $dy - $self->{lfdy}, - @{$self->{last_fow_texture}}{qw(name data)}; - } else { - delete $self->{last_fow_texture}; + @{$self->{prev_fow_texture}}{qw(name data)} + ); } } - unless ($self->{last_fow_texture}) { - DC::Texture::draw_fow_texture - $::CFG->{fow_intensity}, - $self->{hidden_tex}{name}, - @{$self->{fow_texture}}{qw(name data s t w h)}; - } + DC::Texture::draw_fow_texture + $::CFG->{fow_intensity}, + $TEX_HIDDEN[$::CFG->{fow_texture}]{name}, + @{$self->{fow_texture}}{qw(name data s t w h)}, + @prev_fow_params; } if ($self->{magicmap}) { @@ -609,12 +602,10 @@ package DC::MapWidget::MapMap; -use strict; -use utf8; +use common::sense; our @ISA = DC::UI::Base::; -use Time::HiRes qw(time); use DC::OpenGL; sub size_request { @@ -624,7 +615,7 @@ sub refresh_hook { my ($self) = @_; - if ($::MAP && $self->{texture_atime} < time) { + if ($::MAP && $self->{texture_atime} < AE::now) { my ($w, $h) = @$self{qw(w h)}; return unless $w && $h; @@ -649,7 +640,7 @@ $self->{x0} = $x0; $self->{y0} = $y0; - $self->{texture_atime} = time + 1/3; + $self->{texture_atime} = AE::now + 1/2; $self->{texture} = new DC::Texture @@ -714,7 +705,7 @@ package DC::MapWidget::Command; -use strict; +use common::sense; use DC::OpenGL; @@ -820,6 +811,13 @@ $self } +sub reset { + my ($self) = @_; + + $self->hide; + delete $self->{command_list}; +} + sub set_prefix { my ($self, $prefix) = @_; @@ -866,6 +864,14 @@ length $text or return $self->hide; + return unless $::CONN; + + # regenerate spell list if necessary + $self->{command_list}{spells} ||= [ + map { ("cast $_->{name}", "invoke $_->{name}") } + values %{ $::CONN->{spell} } + ]; + if ($text ne $self->{last_search}) { my @match; @@ -877,6 +883,8 @@ my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; + my $first_char = substr $cmd, 0, 1; + my $regexp_abbrev = do { my ($beg, @chr) = split //, lc $cmd; @@ -885,7 +893,7 @@ # - the more characters the parentheses match, the less attractive # is the match. my $regexp = "^\Q$beg\E" - . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; + . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr; qr<$regexp> }; @@ -894,42 +902,48 @@ qr<$regexp> }; - for (keys %{$self->{command}}) { - my @scores; + for my $list (values %{ $self->{command_list} }) { + for (@$list) { + # we only match and score if the first character matches, + # so quickly rule out all others first. + next unless $first_char = substr $_, 0, 1; + + my @scores; + + # 1. Complete command [with args] + # command is a prefix of the text + # score is length of complete command matched + # e.g. "invoke summon pet monster bat" + # "invoke" "summon pet monster bat" = 6 + # "invoke summon pet monster" "bat" = 25 + if ((substr $text, 0, length $_) eq $_) { + push @scores, [$_, length $_, $text]; + } - # 1. Complete command [with args] - # command is a prefix of the text - # score is length of complete command matched - # e.g. "invoke summon pet monster bat" - # "invoke" "summon pet monster bat" = 6 - # "invoke summon pet monster" "bat" = 25 - if ($text =~ /^\Q$_\E(.*)/) { - push @scores, [$_, length $_, $text]; - } + # 2. Partial command + # text is a prefix of the full command + # score is the length of the input text + # e.g. "invoke s" + # "invoke small fireball" = 8 + # "invoke summon pet monster" = 8 - # 2. Partial command - # text is a prefix of the full command - # score is the length of the input text - # e.g. "invoke s" - # "invoke small fireball" = 8 - # "invoke summon pet monster" = 8 + if ($_ =~ $regexp_partial) { + push @scores, [$_, length $text, $_]; + } - if ($_ =~ $regexp_partial) { - push @scores, [$_, length $text, $_]; - } + # 3. Abbreviation match + # attempts to use first word of text as an abbreviated command + # score is length of word + 1 - 3 per non-word-initial character - # 3. Abbreviation match - # attempts to use first word of text as an abbreviated command - # score is length of word + 1 - 3 per non-word-initial character + if (my @penalty = $_ =~ $regexp_abbrev) { + push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; + } - if (my @penalty = $_ =~ $regexp_abbrev) { - push @scores, [$_, (length $cmd) + 1 - (length join "", map "::$_", grep defined, @penalty), "$_$arg"]; + # Pick the best option for this command + push @match, (sort { + $b->[1] <=> $a->[1] + } @scores)[0]; } - - # Pick the best option for this command - push @match, (sort { - $b->[1] <=> $a->[1] - } @scores)[0]; } # @match is now [command object, command with arguments]