--- deliantra/Deliantra-Client/DC/MapWidget.pm 2007/12/29 16:29:00 1.127
+++ 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,8 +13,13 @@
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_file DC::Texture DC::find_rcfile "magicmap.png",
+ new_from_resource DC::Texture "magicmap.png",
mipmap => 1, wrap => 0, internalformat => GL_ALPHA;
sub new {
@@ -29,13 +35,7 @@
$self
}
-sub set_tilesize {
- my ($self, $tilesize) = @_;
-
- $self->{tilesize} = $tilesize;
-}
-
-sub add_command {
+sub add_command {#d# remove
my ($self, $command, $tooltip, $widget, $cb) = @_;
(my $data = $command) =~ s/\\//g;
@@ -47,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) = @_;
@@ -171,7 +162,7 @@
if ($ev->{button} == 1) {
$self->grab_focus;
- return unless $::CONN;
+ return unless $::CONN && $self->{ctilesize};
my $x = $self->{dx} + DC::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize};
my $y = $self->{dy} + DC::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize};
@@ -208,23 +199,17 @@
["Spells…\tF4", sub { ::toggle_player_page ($::SPELL_PAGE) }],
["Inventory…\tF5", sub { ::toggle_player_page ($::INVENTORY_PAGE) }],
["Setup… \tF9", sub { $::SETUP_DIALOG->toggle_visibility }],
- ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
- [
- $::PICKUP_ENABLE->{state}
- ? "Disable automatic pickup"
- : "Enable automatic pickup",
- sub { $::PICKUP_ENABLE->toggle }
- ],
+# ["Server Messages…", sub { $::MESSAGE_WINDOW->toggle_visibility }],
);
if ($::CONN && $::CONN->{editor_support}) {
- push @items, [
- "Edit this map (" . (DC::asxml $::CONN->{map_info}[0]) . ")",
- \&editor_invoke,
- ];
+# push @items, [
+# "Edit this map (" . (DC::asxml $::CONN->{map_info}[0]) . ")",
+# \&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;
@@ -293,24 +278,29 @@
}
my %DIR = (
- ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
- ( "," . DC::SDLK_KP8 ), [1, "north"],
- ( "," . DC::SDLK_KP9 ), [2, "northeast"],
- ( "," . DC::SDLK_KP6 ), [3, "east"],
- ( "," . DC::SDLK_KP3 ), [4, "southeast"],
- ( "," . DC::SDLK_KP2 ), [5, "south"],
- ( "," . DC::SDLK_KP1 ), [6, "southwest"],
- ( "," . DC::SDLK_KP4 ), [7, "west"],
- ( "," . DC::SDLK_KP7 ), [8, "northwest"],
-
- ( "," . DC::SDLK_UP ), [1, "north"],
- ("1," . DC::SDLK_UP ), [2, "northeast"],
- ( "," . DC::SDLK_RIGHT), [3, "east"],
- ("1," . DC::SDLK_RIGHT), [4, "southeast"],
- ( "," . DC::SDLK_DOWN ), [5, "south"],
- ("1," . DC::SDLK_DOWN ), [6, "southwest"],
- ( "," . DC::SDLK_LEFT ), [7, "west"],
- ("1," . DC::SDLK_LEFT ), [8, "northwest"],
+ ( "," . DC::SDLK_KP5 ), [0, "stay fire"],
+ ( "," . DC::SDLK_KP8 ), [1, "north"],
+ ( "," . DC::SDLK_KP9 ), [2, "northeast"],
+ ( "," . DC::SDLK_KP6 ), [3, "east"],
+ ( "," . DC::SDLK_KP3 ), [4, "southeast"],
+ ( "," . DC::SDLK_KP2 ), [5, "south"],
+ ( "," . DC::SDLK_KP1 ), [6, "southwest"],
+ ( "," . DC::SDLK_KP4 ), [7, "west"],
+ ( "," . DC::SDLK_KP7 ), [8, "northwest"],
+
+ ( "," . DC::SDLK_PAGEUP ), [2, "northeast"],
+ ( "," . DC::SDLK_PAGEDOWN ), [4, "southeast"],
+ ( "," . DC::SDLK_END ), [6, "southwest"],
+ ( "," . DC::SDLK_HOME ), [8, "northwest"],
+
+ ( "," . DC::SDLK_UP ), [1, "north"],
+ ("1," . DC::SDLK_UP ), [2, "northeast"],
+ ( "," . DC::SDLK_RIGHT ), [3, "east"],
+ ("1," . DC::SDLK_RIGHT ), [4, "southeast"],
+ ( "," . DC::SDLK_DOWN ), [5, "south"],
+ ("1," . DC::SDLK_DOWN ), [6, "southwest"],
+ ( "," . DC::SDLK_LEFT ), [7, "west"],
+ ("1," . DC::SDLK_LEFT ), [8, "northwest"],
);
sub invoke_key_down {
@@ -320,9 +310,16 @@
my $sym = $ev->{sym};
my $uni = $ev->{unicode};
- $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_SHIFT;
+ $mod &= DC::KMOD_CTRL | DC::KMOD_ALT | DC::KMOD_META | DC::KMOD_SHIFT;
+
+ # ignore repeated keypresses
+ return if $self->{last_mod} == $mod && $self->{last_sym} == $sym;
+ $self->{last_mod} = $mod;
+ $self->{last_sym} = $sym;
- if ($::CONN && (my $dir = $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"})) {
+ my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
+
+ if ($::CONN && $dir) {
if ($mod & DC::KMOD_SHIFT) {
$self->{shft}++;
if ($dir->[0] != $self->{fire_dir}) {
@@ -349,6 +346,9 @@
my $mod = $ev->{mod};
my $sym = $ev->{sym};
+ delete $self->{last_mod};
+ delete $self->{last_sym};
+
if ($::CFG->{shift_fire_stop}) {
if (!($mod & DC::KMOD_SHIFT) && delete $self->{shft}) {
$::CONN->user_send ("fire_stop");
@@ -356,7 +356,9 @@
$res = 1;
}
} else {
- if (exists $DIR{(!!($mod & DC::KMOD_ALT)) . ",$sym"} && delete $self->{shft}) {
+ my $dir = $DIR{ (!!($mod & (DC::KMOD_ALT | DC::KMOD_META))) . ",$sym" };
+
+ if ($dir && delete $self->{shft}) {
$::CONN->user_send ("fire_stop");
delete $self->{fire_dir};
$res = 1;
@@ -368,7 +370,7 @@
}
}
- if (!($mod & DC::KMOD_CTRL ) && delete $self->{ctrl}) {
+ if (!($mod & DC::KMOD_CTRL) && delete $self->{ctrl}) {
$::CONN->user_send ("run_stop");
$res = 1;
}
@@ -384,6 +386,30 @@
0
}
+sub set_tilesize {
+ my ($self, $tilesize) = @_;
+
+ $self->{tilesize} = $tilesize;
+}
+
+sub scroll {
+ my ($self, $dx, $dy) = @_;
+
+ $self->movement_update;
+
+ $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
+ $self->{sdy} += $dy * $self->{tilesize};
+
+ # save old fow texture, if applicable
+ $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
+ $self->{lfdx} = $dx;
+ $self->{lfdy} = $dy;
+ $self->{lmdx} = $self->{dx};
+ $self->{lmdy} = $self->{dy};
+
+ $::MAP->scroll ($dx, $dy);
+}
+
sub set_magicmap {
my ($self, $w, $h, $x, $y, $data) = @_;
@@ -395,19 +421,66 @@
$self->update;
}
+sub movement_update {
+ my ($self) = @_;
+
+ if ($::CFG->{smooth_movement}) {
+ if ($self->{sdx} || $self->{sdy}) {
+ 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
+ my $mintime = DC::Protocol::TICK * DC::ceil 1 / ($spd * DC::Protocol::TICK || 1);
+
+ $spd *= $self->{tilesize};
+
+ # jump if "impossibly high" speed
+ if (
+ (max abs $self->{sdx}, abs $self->{sdy})
+ > $spd * $mintime * 2.1
+ ) {
+ #warn "jump ", (max abs $self->{sdx}, abs $self->{sdy}), " ", $spd * $mintime * 2.1;#d#
+ $self->{sdx} = $self->{sdy} = 0;
+ } else {
+ $spd *= $diff * 1.0001; # 1.0001 so that we don't accumulate rounding errors the wrong direction
+
+ my $dx = $self->{sdx} < 0 ? -$spd : $spd;
+ my $dy = $self->{sdy} < 0 ? -$spd : $spd;
+
+ if ($self->{sdx} * ($self->{sdx} - $dx) <= 0) { $self->{sdx} = 0 } else { $self->{sdx} -= $dx }
+ if ($self->{sdy} * ($self->{sdy} - $dy) <= 0) { $self->{sdy} = 0 } else { $self->{sdy} -= $dy }
+ }
+
+ $self->update;
+ }
+ } else {
+ $self->{sdx} = $self->{sdy} = 0;
+ }
+
+ $self->{last_update} = AE::time;
+}
+
sub refresh_hook {
my ($self) = @_;
if ($::MAP && $::CONN) {
if (delete $self->{need_update}) {
+ $self->movement_update;
+
my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1;
- my $sw = $self->{sw} = 1 + DC::ceil $self->{w} / $tilesize;
- my $sh = $self->{sh} = 1 + DC::ceil $self->{h} / $tilesize;
+ my $sdx_t = DC::ceil $self->{sdx} / $tilesize;
+ my $sdy_t = DC::ceil $self->{sdy} / $tilesize;
- my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize;
- my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize;
+ # width/height of map, in tiles
+ my $sw = $self->{sw} = 2 + DC::ceil $self->{w} / $tilesize;
+ my $sh = $self->{sh} = 2 + DC::ceil $self->{h} / $tilesize;
+
+ # the map displacement, in tiles
+ my $sx = DC::ceil $::CFG->{map_shift_x} / $tilesize + $sdx_t;
+ my $sy = DC::ceil $::CFG->{map_shift_y} / $tilesize + $sdy_t;
+ # the upper left "visible" corner, in pixels
my $sx0 = $self->{sx0} = $::CFG->{map_shift_x} - $sx * $tilesize;
my $sy0 = $self->{sy0} = $::CFG->{map_shift_y} - $sy * $tilesize;
@@ -415,6 +488,7 @@
my $dy = $self->{dy} = DC::ceil 0.5 * ($::MAP->h - $sh) - $sy;
if ($::CFG->{fow_enable}) {
+ # draw_fow_texture REQUIRES the fow texture to stay the same size.
my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
$self->{fow_texture} = new DC::Texture
@@ -432,22 +506,37 @@
glPushMatrix;
glTranslate $sx0, $sy0;
glScale $::CFG->{map_scale}, $::CFG->{map_scale};
+ glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
- $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize});
+ $::MAP->draw ($dx, $dy, $sw, $sh,
+ ($self->{tilesize}) x 2,
+ $::CONN->{player}{tag},
+ -$self->{sdx}, -$self->{sdy});
glScale $self->{tilesize}, $self->{tilesize};
if (my $tex = $self->{fow_texture}) {
- glPushMatrix;
- glScale 1/3, 1/3;
- glEnable GL_TEXTURE_2D;
- glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
+ my @prev_fow_params;
- glColor +($::CFG->{fow_intensity}) x 3, 0.9;
- $self->{fow_texture}->draw_quad_alpha (0, 0);
+ 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) {
+ @prev_fow_params = (
+ (min 1, $d1 / $d2),
+ $self->{lmdx} - $dx - $self->{lfdx},
+ $self->{lmdy} - $dy - $self->{lfdy},
+ @{$self->{prev_fow_texture}}{qw(name data)}
+ );
+ }
+ }
- glDisable GL_TEXTURE_2D;
- glPopMatrix;
+ 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}) {
@@ -458,13 +547,16 @@
glTranslate - $x - 1, - $y - 1;
glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
- $::MAP->draw_magicmap ($x, $y, $w, $h, $data);
+ $::MAP->draw_magicmap ($w, $h, $data);
}
glPopMatrix;
glEndList;
}
} else {
+ delete $self->{last_fow_texture};
+ delete $self->{fow_texture};
+
glDeleteList delete $self->{list}
if $self->{list};
}
@@ -510,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 {
@@ -525,9 +615,11 @@
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;
+
my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
@@ -548,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
@@ -613,7 +705,7 @@
package DC::MapWidget::Command;
-use strict;
+use common::sense;
use DC::OpenGL;
@@ -719,6 +811,13 @@
$self
}
+sub reset {
+ my ($self) = @_;
+
+ $self->hide;
+ delete $self->{command_list};
+}
+
sub set_prefix {
my ($self, $prefix) = @_;
@@ -765,15 +864,28 @@
length $text
or return $self->hide;
- my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
+ 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;
if ($text =~ /^(.*?)\s+$/) {
- @match = [$cmd, "(appended whitespace suppresses completion)"];
+ my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
+ @match = ([[$cmd,'(appended whitespace suppresses completion)'],$text]);
} else {
- my $regexp = do {
+ # @match is [command, penalty, command with arguments] until sort
+
+ my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
+
+ my $first_char = substr $cmd, 0, 1;
+
+ my $regexp_abbrev = do {
my ($beg, @chr) = split //, lc $cmd;
# the following regex is used to match our "completion entry"
@@ -781,21 +893,63 @@
# - 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>
};
- my @penalty;
+ my $regexp_partial = do {
+ my $regexp = "^\Q$text\E(.*)";
+ qr<$regexp>
+ };
+
+ 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];
+ }
+
+ # 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, $_];
+ }
+
+ # 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"];
+ }
- for (keys %{$self->{command}}) {
- if (@penalty = $_ =~ $regexp) {
- push @match, [$_, length join "", map "::$_", grep defined, @penalty];
+ # Pick the best option for this command
+ push @match, (sort {
+ $b->[1] <=> $a->[1]
+ } @scores)[0];
}
}
- @match = map $self->{command}{$_->[0]},
+ # @match is now [command object, command with arguments]
+ @match = map [$self->{command}{$_->[0]}, $_->[2]],
sort {
- $a->[1] <=> $b->[1]
+ $b->[1] <=> $a->[1]
or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
or (length $b->[0]) <=> (length $a->[0])
} @match;
@@ -824,20 +978,20 @@
}
if (@matches) {
- $self->{select} = "$matches[0][0]$arg";
+ $self->{select} = "$matches[0][1]";
$labels[0]->{fg} = [0, 0, 0, 1];
$labels[0]->{bg} = [1, 1, 1, 0.8];
} else {
- $self->{select} = "$cmd$arg";
+ $self->{select} = "$text";
}
for my $match (@matches) {
my $label = shift @labels;
if (@labels) {
- $label->set_text ("$match->[0]$arg");
- $label->set_tooltip ($match->[1]);
+ $label->set_text ("$match->[1]");
+ $label->set_tooltip ("$match->[0][1]");
} else {
$label->set_text ("...");
$label->set_tooltip ("Use Cursor-Down to view more matches");