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.153 by root, Thu Nov 26 07:19:12 2009 UTC vs.
Revision 1.166 by root, Sat Nov 10 18:29:32 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 glBlendEquation GL_FUNC_REVERSE_SUBTRACT;
530 glBlendFunc GL_ONE, GL_ZERO;
531 529
532 my $a1 = 0.9; 530 if ($DC::OpenGL::GL_MULTITEX && $self->{prev_fow_texture}) {
533 531 my $d1 = DC::distance $self->{sdx}, $self->{sdy};
534 if (0 && $self->{last_fow_texture}) { 532 my $d2 = (DC::distance $self->{lfdx}, $self->{lfdy}) * $tilesize;
535 my $d1 = ($self->{sdx} ** 2 + $self->{sdy} ** 2) ** 0.5;
536 my $d2 = ($self->{ldx} ** 2 + $self->{ldy} ** 2) ** 0.5;
537 533
538 if ($d1 * $d2) { 534 if ($d1 * $d2) {
535 @prev_fow_params = (
539 my $a2 = $d1 / $d2; 536 (min 1, $d1 / $d2),
540 537 $self->{lmdx} - $dx - $self->{lfdx},
541 $a1 = 1 - $a2; 538 $self->{lmdy} - $dy - $self->{lfdy},
542 539 @{$self->{prev_fow_texture}}{qw(name data)}
543 #$a1 *= 0.9;
544 #$a2 *= 0.9;
545
546 printf "%4g %4g %4g %4g\n", $d1, $d2, $a1, $a2;#d#
547
548 #$a1 = 1 - (1-$a1) * 0.707;
549 #$a2 = 1 - (1-$a2) * 0.707;
550
551 glColor 1,0,0,1;
552 glBegin GL_QUADS;
553 glVertex 10 + $sdx_t, 10;
554 glVertex 11 + $sdx_t, 10;
555 glVertex 11 + $sdx_t, 11;
556 glVertex 10 + $sdx_t, 11;
557 glEnd; 540 );
558
559 glColor +($::CFG->{fow_intensity}) x 3, $a2;
560 glPushMatrix;
561 glTranslate $sdx_t, $sdy_t; # anchor at player
562 glTranslate $self->{ldx} / -$tilesize, $self->{ldy} / -$tilesize;
563 glScale 1/3, 1/3;
564 $self->{last_fow_texture}->draw_quad_alpha (0, 0);
565 glPopMatrix;
566 } else {
567 delete $self->{last_fow_texture};
568 } 541 }
569 } 542 }
570 543
544 DC::Texture::draw_fow_texture
571 glColor +($::CFG->{fow_intensity}) x 3, $a1; 545 $::CFG->{fow_intensity},
572 glPushMatrix; 546 $TEX_HIDDEN[$::CFG->{fow_texture}]{name},
573 #d#glTranslate +(min 0, $sdx_t), (min 0, $sdy_t); # anchor at player 547 @{$self->{fow_texture}}{qw(name data s t w h)},
574 glScale 1/3, 1/3; 548 @prev_fow_params;
575 $self->{fow_texture}->draw_quad_alpha (0, 0);
576 glPopMatrix;
577
578 glBlendEquation GL_FUNC_ADD;
579 glDisable GL_TEXTURE_2D;
580 glDisable GL_BLEND;
581 } 549 }
582 550
583 if ($self->{magicmap}) { 551 if ($self->{magicmap}) {
584 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; 552 my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} };
585 553
586 $x += $::MAP->ox + $self->{dx}; 554 $x += $::MAP->ox + $self->{dx};
587 $y += $::MAP->oy + $self->{dy}; 555 $y += $::MAP->oy + $self->{dy};
588 556
589 glTranslate - $x - 1, - $y - 1; 557 glTranslate - $x - 1, - $y - 1;
590 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name}; 558 glBindTexture GL_TEXTURE_2D, $magicmap_tex->{name};
591 $::MAP->draw_magicmap ($x, $y, $w, $h, $data); 559 $::MAP->draw_magicmap ($w, $h, $data);
592 } 560 }
593 561
594 glPopMatrix; 562 glPopMatrix;
595 glEndList; 563 glEndList;
596 } 564 }
597 } else { 565 } else {
566 delete $self->{last_fow_texture};
567 delete $self->{fow_texture};
568
598 glDeleteList delete $self->{list} 569 glDeleteList delete $self->{list}
599 if $self->{list}; 570 if $self->{list};
600 } 571 }
601} 572}
602 573
638 $self->SUPER::DESTROY; 609 $self->SUPER::DESTROY;
639} 610}
640 611
641package DC::MapWidget::MapMap; 612package DC::MapWidget::MapMap;
642 613
643use strict; 614use common::sense;
644use utf8;
645 615
646our @ISA = DC::UI::Base::; 616our @ISA = DC::UI::Base::;
647 617
648use Time::HiRes qw(time);
649use DC::OpenGL; 618use DC::OpenGL;
650 619
651sub size_request { 620sub size_request {
652 ($::HEIGHT * 0.2, $::HEIGHT * 0.2) 621 ($::HEIGHT * 0.2, $::HEIGHT * 0.2)
653} 622}
654 623
655sub refresh_hook { 624sub refresh_hook {
656 my ($self) = @_; 625 my ($self) = @_;
657 626
658 if ($::MAP && $self->{texture_atime} < time) { 627 if ($::MAP && $self->{texture_atime} < AE::now) {
659 my ($w, $h) = @$self{qw(w h)}; 628 my ($w, $h) = @$self{qw(w h)};
660 629
661 return unless $w && $h; 630 return unless $w && $h;
662 631
663 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; 632 my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99;
678 $self->{sh} = $sh; 647 $self->{sh} = $sh;
679 648
680 $self->{x0} = $x0; 649 $self->{x0} = $x0;
681 $self->{y0} = $y0; 650 $self->{y0} = $y0;
682 651
683 $self->{texture_atime} = time + 1/3; 652 $self->{texture_atime} = AE::now + 1/2;
684 653
685 $self->{texture} = 654 $self->{texture} =
686 new DC::Texture 655 new DC::Texture
687 w => $w, 656 w => $w,
688 h => $h, 657 h => $h,
743 glDisable GL_BLEND; 712 glDisable GL_BLEND;
744} 713}
745 714
746package DC::MapWidget::Command; 715package DC::MapWidget::Command;
747 716
748use strict; 717use common::sense;
749 718
750use DC::OpenGL; 719use DC::OpenGL;
751 720
752our @ISA = DC::UI::Frame::; 721our @ISA = DC::UI::Frame::;
753 722
906 } else { 875 } else {
907 # @match is [command, penalty, command with arguments] until sort 876 # @match is [command, penalty, command with arguments] until sort
908 877
909 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/; 878 my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
910 879
880 my $first_char = substr $cmd, 0, 1;
881
911 my $regexp_abbrev = do { 882 my $regexp_abbrev = do {
912 my ($beg, @chr) = split //, lc $cmd; 883 my ($beg, @chr) = split //, lc $cmd;
913 884
914 # the following regex is used to match our "completion entry" 885 # the following regex is used to match our "completion entry"
915 # to an actual command - the parentheses match kind of "overhead" 886 # to an actual command - the parentheses match kind of "overhead"
916 # - the more characters the parentheses match, the less attractive 887 # - the more characters the parentheses match, the less attractive
917 # is the match. 888 # is the match.
918 my $regexp = "^\Q$beg\E" 889 my $regexp = "^\Q$beg\E"
919 . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr; 890 . join "", map "(?:.*?[ _\-]|(.*?))\Q$_\E", @chr;
920 qr<$regexp> 891 qr<$regexp>
921 }; 892 };
922 893
923 my $regexp_partial = do { 894 my $regexp_partial = do {
924 my $regexp = "^\Q$text\E(.*)"; 895 my $regexp = "^\Q$text\E(.*)";
925 qr<$regexp> 896 qr<$regexp>
926 }; 897 };
927 898
928 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
929 my @scores; 904 my @scores;
930 905
931 # 1. Complete command [with args] 906 # 1. Complete command [with args]
932 # command is a prefix of the text 907 # command is a prefix of the text
933 # score is length of complete command matched 908 # score is length of complete command matched
934 # e.g. "invoke summon pet monster bat" 909 # e.g. "invoke summon pet monster bat"
935 # "invoke" "summon pet monster bat" = 6 910 # "invoke" "summon pet monster bat" = 6
936 # "invoke summon pet monster" "bat" = 25 911 # "invoke summon pet monster" "bat" = 25
937 if ($text =~ /^\Q$_\E(.*)/) { 912 if ((substr $text, 0, length $_) eq $_) {
938 push @scores, [$_, length $_, $text]; 913 push @scores, [$_, length $_, $text];
939 } 914 }
940 915
941 # 2. Partial command 916 # 2. Partial command
942 # 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