ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.154 by root, Fri Dec 4 15:04:56 2009 UTC vs.
Revision 1.165 by root, Fri Nov 9 22:53:57 2012 UTC

1package DC::MapWidget; 1package DC::MapWidget;
2 2
3use strict; 3use common::sense;
4use utf8;
5 4
6use List::Util qw(min max); 5use List::Util qw(min max);
6
7use AnyEvent ();
7 8
8use DC; 9use DC;
9use DC::OpenGL; 10use DC::OpenGL;
10use DC::UI; 11use DC::UI;
11use DC::Macro; 12use DC::Macro;
12 13
13our @ISA = DC::UI::Base::; 14our @ISA = DC::UI::Base::;
15
16our @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
15my $magicmap_tex = 21my $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, [
396} 402}
397 403
398sub scroll { 404sub scroll {
399 my ($self, $dx, $dy) = @_; 405 my ($self, $dx, $dy) = @_;
400 406
401 $::MAP->scroll ($dx, $dy);
402
403 $self->{last_fow_texture} = $self->{fow_texture};
404 $self->{ldx} = $self->{sdx} + $dx * $self->{tilesize}; # smooth displacement
405 $self->{ldy} = $self->{sdy} + $dy * $self->{tilesize};
406
407 $self->movement_update; 407 $self->movement_update;
408 408
409 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement 409 $self->{sdx} += $dx * $self->{tilesize}; # smooth displacement
410 $self->{sdy} += $dy * $self->{tilesize}; 410 $self->{sdy} += $dy * $self->{tilesize};
411
412 # save old fow texture, if applicable
413 $self->{prev_fow_texture} = $::CFG->{smooth_transitions} && $self->{fow_texture};
414 $self->{lfdx} = $dx;
415 $self->{lfdy} = $dy;
416 $self->{lmdx} = $self->{dx};
417 $self->{lmdy} = $self->{dy};
418
419 $::MAP->scroll ($dx, $dy);
411} 420}
412 421
413sub set_magicmap { 422sub set_magicmap {
414 my ($self, $w, $h, $x, $y, $data) = @_; 423 my ($self, $w, $h, $x, $y, $data) = @_;
415 424
424sub movement_update { 433sub movement_update {
425 my ($self) = @_; 434 my ($self) = @_;
426 435
427 if ($::CFG->{smooth_movement}) { 436 if ($::CFG->{smooth_movement}) {
428 if ($self->{sdx} || $self->{sdy}) { 437 if ($self->{sdx} || $self->{sdy}) {
429 my $diff = EV::time - ($self->{last_update} || $::LAST_REFRESH); 438 my $diff = AE::time - ($self->{last_update} || $::LAST_REFRESH);
430 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED}; 439 my $spd = $::CONN->{stat}{DC::Protocol::CS_STAT_SPEED};
431 440
432 # the minimum time for a single tile movement 441 # the minimum time for a single tile movement
433 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);
434 443
455 } 464 }
456 } else { 465 } else {
457 $self->{sdx} = $self->{sdy} = 0; 466 $self->{sdx} = $self->{sdy} = 0;
458 } 467 }
459 468
460 $self->{last_update} = EV::time; 469 $self->{last_update} = AE::time;
461} 470}
462 471
463sub refresh_hook { 472sub refresh_hook {
464 my ($self) = @_; 473 my ($self) = @_;
465 474
486 495
487 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;
488 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;
489 498
490 if ($::CFG->{fow_enable}) { 499 if ($::CFG->{fow_enable}) {
491 my ($sdx_t, $sdy_t);#d# do not anchor at player 500 # draw_fow_texture REQUIRES the fow texture to stay the same size.
492
493 my ($w, $h, $data) = $::MAP->fow_texture ( 501 my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh);
494 $dx + (min 0, $sdx_t),
495 $dy + (min 0, $sdy_t),
496 $sw + abs $sdx_t,
497 $sh + abs $sdy_t
498 );
499 502
500 $self->{fow_texture} = new DC::Texture 503 $self->{fow_texture} = new DC::Texture
501 w => $w, 504 w => $w,
502 h => $h, 505 h => $h,
503 data => $data, 506 data => $data,
513 glTranslate $sx0, $sy0; 516 glTranslate $sx0, $sy0;
514 glScale $::CFG->{map_scale}, $::CFG->{map_scale}; 517 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
515 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy}; 518 glTranslate DC::ceil $self->{sdx}, DC::ceil $self->{sdy};
516 519
517 $::MAP->draw ($dx, $dy, $sw, $sh, 520 $::MAP->draw ($dx, $dy, $sw, $sh,
518 $self->{tilesize}, 521 ($self->{tilesize}) x 2,
519 $::CONN->{player}{tag}, 522 $::CONN->{player}{tag},
520 -$self->{sdx}, -$self->{sdy}); 523 -$self->{sdx}, -$self->{sdy});
521 524
522 #d#glTranslate -$self->{sdx}, -$self->{sdy}; # anchor fow at player
523 glScale $self->{tilesize}, $self->{tilesize}; 525 glScale $self->{tilesize}, $self->{tilesize};
524 526
525 if (my $tex = $self->{fow_texture}) { 527 if (my $tex = $self->{fow_texture}) {
526 glEnable GL_TEXTURE_2D; 528 my @prev_fow_params;
527 glEnable GL_BLEND;
528 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
529 glBlendFunc GL_ONE, GL_ZERO;
530 529
531 my $a1 = 0.9; 530 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
532 531 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
533 if (0 && $self->{last_fow_texture}) { 532 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
534 my $d1 = ($self->{sdx} ** 2 + $self->{sdy} ** 2) ** 0.5;
535 my $d2 = ($self->{ldx} ** 2 + $self->{ldy} ** 2) ** 0.5;
536 533
537 if ($d1 * $d2) { 534 if ($d1 * $d2) {
535 @prev_fow_params = (
538 my $a2 = $d1 / $d2; 536 (min 1, $d1 / $d2),
539 537 $self->{lmdx} - $dx - $self->{lfdx},
540 $a1 = 1 - $a2; 538 $self->{lmdy} - $dy - $self->{lfdy},
541 539 @{$self->{prev_fow_texture}}{qw(name data)}
542 #$a1 *= 0.9;
543 #$a2 *= 0.9;
544
545 printf "%4g %4g %4g %4g\n", $d1, $d2, $a1, $a2;#d#
546
547 #$a1 = 1 - (1-$a1) * 0.707;
548 #$a2 = 1 - (1-$a2) * 0.707;
549
550 glColor 1,0,0,1;
551 glBegin GL_QUADS;
552 glVertex 10 + $sdx_t, 10;
553 glVertex 11 + $sdx_t, 10;
554 glVertex 11 + $sdx_t, 11;
555 glVertex 10 + $sdx_t, 11;
556 glEnd; 540 );
557
558 glColor +($::CFG->{fow_intensity}) x 3, $a2;
559 glPushMatrix;
560 glTranslate $sdx_t, $sdy_t; # anchor at player
561 glTranslate $self->{ldx} / -$tilesize, $self->{ldy} / -$tilesize;
562 glScale 1/3, 1/3;
563 $self->{last_fow_texture}->draw_quad_alpha (0, 0);
564 glPopMatrix;
565 } else {
566 delete $self->{last_fow_texture};
567 } 541 }
568 } 542 }
569 543
544 DC::Texture::draw_fow_texture
570 glColor +($::CFG->{fow_intensity}) x 3, $a1; 545 $::CFG->{fow_intensity},
571 glPushMatrix; 546 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
572 #d#glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); # anchor at player 547 @{$self->{fow_texture}}{qw(name data s t w h)},
573 glScale 1/3, 1/3; 548 @prev_fow_params;
574 $self->{fow_texture}->draw_quad_alpha (0, 0);
575 glPopMatrix;
576
577 glDisable GL_TEXTURE_2D;
578 glDisable GL_BLEND;
579 } 549 }
580 550
581 if ($self->{magicmap}) { 551 if ($self->{magicmap}) {
582 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 552 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
583 553
584 $x += $::MAP->ox + $self->{dx}; 554 $x += $::MAP->ox + $self->{dx};
585 $y += $::MAP->oy + $self->{dy}; 555 $y += $::MAP->oy + $self->{dy};
586 556
587 glTranslate - $x - 1, - $y - 1; 557 glTranslate - $x - 1, - $y - 1;
588 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 558 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
589 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 559 $::MAP->draw_magicmap ($w, $h, $data);
590 } 560 }
591 561
592 glPopMatrix; 562 glPopMatrix;
593 glEndList; 563 glEndList;
594 } 564 }
595 } else { 565 } else {
566 delete $self->{last_fow_texture};
567 delete $self->{fow_texture};
568
596 glDeleteList delete $self->{list} 569 glDeleteList delete $self->{list}
597 if $self->{list}; 570 if $self->{list};
598 } 571 }
599} 572}
600 573
636 $self->SUPER::DESTROY; 609 $self->SUPER::DESTROY;
637} 610}
638 611
639package DC::MapWidget::MapMap; 612package DC::MapWidget::MapMap;
640 613
641use strict; 614use common::sense;
642use utf8;
643 615
644our @ISA = DC::UI::Base::; 616our @ISA = DC::UI::Base::;
645 617
646use Time::HiRes qw(time);
647use DC::OpenGL; 618use DC::OpenGL;
648 619
649sub size_request { 620sub size_request {
650 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 621 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
651} 622}
652 623
653sub refresh_hook { 624sub refresh_hook {
654 my ($self) = @_; 625 my ($self) = @_;
655 626
656 if ($::MAP && $self->{texture_atime} < time) { 627 if ($::MAP && $self->{texture_atime} < AE::now) {
657 my ($w, $h) = @$self{qw(w h)}; 628 my ($w, $h) = @$self{qw(w h)};
658 629
659 return unless $w && $h; 630 return unless $w && $h;
660 631
661 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 632 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
676 $self->{sh} = $sh; 647 $self->{sh} = $sh;
677 648
678 $self->{x0} = $x0; 649 $self->{x0} = $x0;
679 $self->{y0} = $y0; 650 $self->{y0} = $y0;
680 651
681 $self->{texture_atime} = time + 1/3; 652 $self->{texture_atime} = AE::now + 1/2;
682 653
683 $self->{texture} = 654 $self->{texture} =
684 new DC::Texture 655 new DC::Texture
685 w => $w, 656 w => $w,
686 h => $h, 657 h => $h,
741 glDisable GL_BLEND; 712 glDisable GL_BLEND;
742} 713}
743 714
744package DC::MapWidget::Command; 715package DC::MapWidget::Command;
745 716
746use strict; 717use common::sense;
747 718
748use DC::OpenGL; 719use DC::OpenGL;
749 720
750our @ISA = DC::UI::Frame::; 721our @ISA = DC::UI::Frame::;
751 722
904 } else { 875 } else {
905 # @match is [command, penalty, command with arguments] until sort 876 # @match is [command, penalty, command with arguments] until sort
906 877
907 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 878 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
908 879
880 my $first_char = substr $cmd, 0, 1;
881
909 my $regexp_abbrev = do { 882 my $regexp_abbrev = do {
910 my ($beg, @chr) = split //, lc $cmd; 883 my ($beg, @chr) = split //, lc $cmd;
911 884
912 # the following regex is used to match our "completion entry" 885 # the following regex is used to match our "completion entry"
913 # to an actual command - the parentheses match kind of "overhead" 886 # to an actual command - the parentheses match kind of "overhead"
914 # - the more characters the parentheses match, the less attractive 887 # - the more characters the parentheses match, the less attractive
915 # is the match. 888 # is the match.
916 my $regexp = "^\Q$beg\E" 889 my $regexp = "^\Q$beg\E"
917 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 890 . join "", map "(?:.*?[ \\\\]|(.*?))\Q$_\E", @chr;
918 qr<$regexp> 891 qr<$regexp>
919 }; 892 };
920 893
921 my $regexp_partial = do { 894 my $regexp_partial = do {
922 my $regexp = "^\Q$text\E(.*)"; 895 my $regexp = "^\Q$text\E(.*)";
923 qr<$regexp> 896 qr<$regexp>
924 }; 897 };
925 898
926 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
927 my @scores; 904 my @scores;
928 905
929 # 1. Complete command [with args] 906 # 1. Complete command [with args]
930 # command is a prefix of the text 907 # command is a prefix of the text
931 # score is length of complete command matched 908 # score is length of complete command matched
932 # e.g. "invoke summon pet monster bat" 909 # e.g. "invoke summon pet monster bat"
933 # "invoke" "summon pet monster bat" = 6 910 # "invoke" "summon pet monster bat" = 6
934 # "invoke summon pet monster" "bat" = 25 911 # "invoke summon pet monster" "bat" = 25
935 if ($text =~ /^\Q$_\E(.*)/) { 912 if ((substr $text, 0, length $_) eq $_) {
936 push @scores, [$_, length $_, $text]; 913 push @scores, [$_, length $_, $text];
937 } 914 }
938 915
939 # 2. Partial command 916 # 2. Partial command
940 # text is a prefix of the full command 917 # text is a prefix of the full command

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines