ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.114 by root, Sat Apr 15 21:16:51 2006 UTC vs.
Revision 1.126 by root, Mon Apr 17 20:59:20 2006 UTC

13use SDL::OpenGL; 13use SDL::OpenGL;
14 14
15use Crossfire; 15use Crossfire;
16use Crossfire::Protocol; 16use Crossfire::Protocol;
17 17
18use Compress::LZF;
19
18use CFClient; 20use CFClient;
19use CFClient::UI; 21use CFClient::UI;
20 22
21our $VERSION = '0.1'; 23our $VERSION = '0.1';
22 24
23my $MAX_FPS = 60; 25my $MAX_FPS = 60;
24my $MIN_FPS = 5; # unused as of yet 26my $MIN_FPS = 5; # unused as of yet
25 27
26our $META_SERVER = "crossfire.real-time.com:13326"; 28our $META_SERVER = "crossfire.real-time.com:13326";
27 29
30our $FACEMAP;
31our $TILECACHE;
28our $FACECACHE; 32our $MAPCACHE;
29 33
30our $LAST_REFRESH; 34our $LAST_REFRESH;
31our $NOW; 35our $NOW;
32 36
33our $CFG; 37our $CFG;
45our $BUTTONBAR; 49our $BUTTONBAR;
46our $LOGVIEW; 50our $LOGVIEW;
47our $CONSOLE; 51our $CONSOLE;
48our $METASERVER; 52our $METASERVER;
49 53
54our $GAUGES;
55
50our $SDL_ACTIVE; 56our $SDL_ACTIVE;
51our $SDL_EV; 57our $SDL_EV;
52our %SDL_CB; 58our %SDL_CB;
53 59
54our $ALT_ENTER_MESSAGE; 60our $ALT_ENTER_MESSAGE;
69 75
70sub start_game { 76sub start_game {
71 status "logging in..."; 77 status "logging in...";
72 78
73 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; 79 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
80
81 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
74 82
75 $MAP = new CFClient::Map $mapsize, $mapsize; 83 $MAP = new CFClient::Map $mapsize, $mapsize;
76 84
77 my ($host, $port) = split /:/, $CFG->{host}; 85 my ($host, $port) = split /:/, $CFG->{host};
78 86
294 302
295sub message_window { 303sub message_window {
296 my $window = new CFClient::UI::FancyFrame 304 my $window = new CFClient::UI::FancyFrame
297 border_bg => [1, 1, 1, 0.5], 305 border_bg => [1, 1, 1, 0.5],
298 bg => [0.3, 0.3, 0.3, 0.8], 306 bg => [0.3, 0.3, 0.3, 0.8],
299 user_w => $::WIDTH/4, 307 user_w => int $::WIDTH / 3,
300 user_h => $::HEIGHT, 308 user_h => int $::HEIGHT / 5,
301 child => (my $vbox = new CFClient::UI::VBox); 309 child => (my $vbox = new CFClient::UI::VBox);
302 310
303 $vbox->add ($LOGVIEW = new CFClient::UI::TextView 311 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
304 expand => 1, 312 expand => 1,
305 fontsize => $::CFG->{log_fontsize}, 313 fontsize => $::CFG->{log_fontsize},
306 ); 314 );
307 315
308 $vbox->add (my $input = new CFClient::UI::LineEntry); 316 $vbox->add (my $input = new CFClient::UI::Entry
317 connect_focus_in => sub {
318 my ($input, $prev_focus) = @_;
319
320 delete $input->{refocus_map};
321
322 if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
323 $input->{refocus_map} = 1;
324 }
325 delete $input->{auto_activated};
326 },
309 $input->connect (activate => sub { 327 connect_activate => sub {
310 my ($input, $text) = @_; 328 my ($input, $text) = @_;
311 $input->set_text (''); 329 $input->set_text ('');
312 330
313 if ($text =~ /^\/(.*)/) { 331 if ($text =~ /^\/(.*)/) {
314 $::CONN->user_send ("command $1"); 332 $::CONN->user_send ($1);
315 } else { 333 } else {
316 my $say_cmd = $::CFG->{say_command} || 'say'; 334 my $say_cmd = $::CFG->{say_command} || 'say';
317 $::CONN->user_send ("command $say_cmd $text"); 335 $::CONN->user_send ("$say_cmd $text");
318 } 336 }
337 if ($input->{refocus_map}) {
338 delete $input->{refocus_map};
339 $MAPWIDGET->focus_in
319 1 340 }
320 }); 341 },
321 $input->connect (escape => sub { 342 connect_escape => sub {
322 $MAPWIDGET->focus_in 343 $MAPWIDGET->focus_in
323 }); 344 },
324 $input->focus_in; 345 );
325 346
326 $CONSOLE = { 347 $CONSOLE = {
327 window => $window, 348 window => $window,
328 input => $input 349 input => $input
329 }; 350 };
385 padding => 0, 406 padding => 0,
386 y => $HEIGHT * 44 / 45 - $FONTSIZE; 407 y => $HEIGHT * 44 / 45 - $FONTSIZE;
387 $CFClient::UI::ROOT->add ($STATUS_LINE); 408 $CFClient::UI::ROOT->add ($STATUS_LINE);
388 409
389 $ALT_ENTER_MESSAGE = new CFClient::UI::Label 410 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
390 padding => 0, 411 padding => 0,
391 y => $HEIGHT * 44 / 45, 412 y => $HEIGHT * 44 / 45,
392 height => $HEIGHT / 45, 413 fontsize => $HEIGHT / 45,
393 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 414 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
394 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE); 415 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE);
395 416
396 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget); 417 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget);
397 $MAPWIDGET->focus_in; 418 $MAPWIDGET->focus_in;
398 $MAPWIDGET->connect (activate_console => sub { 419 $MAPWIDGET->connect (activate_console => sub {
399 my ($mapwidget, $preset) = @_; 420 my ($mapwidget, $preset) = @_;
400 421
401 if ($CONSOLE) { 422 if ($CONSOLE) {
423 $CONSOLE->{input}->{auto_activated} = 1;
402 $CONSOLE->{input}->focus_in; 424 $CONSOLE->{input}->focus_in;
403 425
404 if ($preset && $CONSOLE->{input}->get_text eq '') { 426 if ($preset && $CONSOLE->{input}->get_text eq '') {
405 $CONSOLE->{input}->set_text ($preset); 427 $CONSOLE->{input}->set_text ($preset);
406 } 428 }
416 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub { 438 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
417 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; 439 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
418 status "Configuration Saved"; 440 status "Configuration Saved";
419 }); 441 });
420 442
421 $BUTTONBAR->{children}[0]->emit ("activate"); 443 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
444
445 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 150, y => 0);
446 $tgw->add (my $hbox = new CFClient::UI::HBox (req_w => 60, req_h => 150));
447
448 $hbox->add (my $fg = new CFClient::UI::VGauge (w => 30, h => 150, gauge => 'food', expand => 1));
449 $hbox->add (my $mg = new CFClient::UI::VGauge (w => 30, h => 150, gauge => 'mana', expand => 1));
450 $hbox->add (my $hg = new CFClient::UI::VGauge (w => 30, h => 150, gauge => 'hp', expand => 1));
451 $hbox->add (my $gg = new CFClient::UI::VGauge (w => 30, h => 150, gauge => 'grace', expand => 1));
452
453 $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
454 $CFClient::UI::ROOT->add ($tgw);
422} 455}
423 456
424sub destroy_screen { 457sub destroy_screen {
425 $CFClient::UI::ROOT->{children} = []; 458 $CFClient::UI::ROOT->{children} = [];
426 undef $SDL_ACTIVE; 459 undef $SDL_ACTIVE;
482 delete $animate_object{$widget}; 515 delete $animate_object{$widget};
483} 516}
484 517
485@conn::ISA = Crossfire::Protocol::; 518@conn::ISA = Crossfire::Protocol::;
486 519
520sub conn::stats_update {
521 my ($self, $stats) = @_;
522
523 # i love text protocols!!!
524 # FIXME: the stats are somehow weird
525 my $hp = $stats->{1};
526 my $hp_m = $stats->{2};
527 my $sp = $stats->{3};
528 my $sp_m = $stats->{4};
529 my $fo = $stats->{18};
530 my $fo_m = 1000;
531 my $gr = $stats->{23};
532 my $gr_m = $stats->{24};
533
534 #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
535 $GAUGES->{hp}->set_value ($hp, $hp_m);
536 $GAUGES->{mana}->set_value ($sp, $sp_m);
537 $GAUGES->{food}->set_value ($fo, $fo_m);
538 $GAUGES->{grace}->set_value ($gr, $gr_m);
539}
540
487sub conn::user_send { 541sub conn::user_send {
488 my ($self, $command) = @_; 542 my ($self, $command) = @_;
489 543
490 $self->send ($command); 544 $self->send_command ($command);
491 status $command; 545 status $command;
546}
547
548sub conn::map_scroll {
549 my ($self, $dx, $dy) = @_;
550
551 $MAP->scroll ($dx, $dy);
492} 552}
493 553
494sub conn::feed_map1a { 554sub conn::feed_map1a {
495 my ($self, $data) = @_; 555 my ($self, $data) = @_;
496 556
497# $self->Crossfire::Protocol::feed_map1a ($data); 557# $self->Crossfire::Protocol::feed_map1a ($data);
498 558
499 $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y});
500 $MAP->map1a_update ($data); 559 $MAP->map1a_update ($data);
501 $MAPWIDGET->update; 560 $MAPWIDGET->update;
502} 561}
503 562
504#sub conn::map_update { 563sub conn::flush_map {
505# my ($self, $dirty) = @_; 564 my ($self) = @_;
506# 565
507# $MAPWIDGET->update; 566 my $map_info = delete $self->{map_info}
508#} 567 or return;
568
569 my ($hash, $x, $y, $w, $h) = @$map_info;
570
571 my $data = $MAP->get_rect ($x, $y, $w, $h);
572 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
573
574 warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
575
576}
509 577
510sub conn::map_clear { 578sub conn::map_clear {
511 my ($self) = @_; 579 my ($self) = @_;
512 580
581 $self->flush_map;
582 delete $self->{neigh};
583
513 $MAP->clear; 584 $MAP->clear;
585}
514 586
515# refresh; 587
588sub conn::load_map($$$) {
589 my ($self, $hash, $x, $y) = @_;
590
591 if (defined (my $data = $MAPCACHE->get ($hash))) {
592 $data = Compress::LZF::decompress $data;
593 warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
594 for my $id ($MAP->set_rect ($x, $y, $data)) {
595 my $data = $TILECACHE->get ($id)
596 or next;
597
598 $self->set_texture ($id => $data);
599 }
600 }
601}
602
603sub conn::flood_fill {
604 my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_;
605
606 # the server does not allow map paths > 6
607 return if 6 <= length $path;
608
609 for my $tile (1..4) {
610 next if $self->{neigh}{$hash}[$tile];
611 next unless $flags & (1 << ($tile - 1));
612
613 my $neigh = $self->{neigh}{$hash} ||= [];
614
615 $self->send_mapinfo ("spatial $path$tile", sub {
616 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
617
618 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
619 return if $mode ne "spatial";
620
621 $x += $MAP->ox;
622 $y += $MAP->oy;
623
624 $self->load_map ($hash, $x, $y)
625 unless $self->{neigh}{$hash}[5]++;#d#
626
627 $neigh->[$tile] = [$x, $y, $w, $h];
628
629 $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1)
630 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
631 });
632 }
633}
634
635sub conn::map_change {
636 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
637
638 $self->flush_map;
639
640 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
641
642 warn "$ox $oy map_info<$flags, $x, $y, $w, $h, $hash>\n";#d#
643
644 my $mapmapw = 250;
645 my $mapmaph = 250;
646
647 $self->flood_fill ("", $hash, $flags,
648 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
649 $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5);
650
651 $x += $ox;
652 $y += $oy;
653
654 $self->{map_info} = [$hash, $x, $y, $w, $h];
655
656 $self->load_map ($hash, $x, $y);
516} 657}
517 658
518sub conn::face_find { 659sub conn::face_find {
519 my ($self, $face) = @_; 660 my ($self, $facenum, $face) = @_;
520 661
521 my $data; 662 my $hash = "$face->{chksum},$face->{name}";
522 663
523 $FACECACHE->get ("$face->{chksum},$face->{name}") 664 my $id = $FACEMAP->get ($hash);
665
666 unless ($id) {
667 # create new id for face
668 # i love transactions
669 for (1..100) {
670 my $txn = $CFClient::DB_ENV->txn_begin;
671 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
672 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
673 $id++;
674 if ($FACEMAP->put (id => $id) == 0
675 && $FACEMAP->put ($hash => $id) == 0) {
676 $txn->txn_commit;
677
678 goto gotid;
679 }
680 }
681 $txn->abort;
682 }
683
684 CFClient::fatal "maximum number of transaction retries reached - database problems?";
685 }
686
687gotid:
688 $face->{id} = $id;
689 $MAP->set_face ($facenum => $id);
690 $TILECACHE->get ($id)
524} 691}
525 692
526sub conn::face_update { 693sub conn::face_update {
527 my ($self, $facenum, $face) = @_; 694 my ($self, $facenum, $face) = @_;
528 695
529 $FACECACHE->put ("$face->{chksum},$face->{name}" => $face->{image}); 696 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
530 697
531 my $tex = $face->{texture} = 698 $self->set_texture ($face->{id} => delete $face->{image});
699}
700
701sub conn::set_texture {
702 my ($self, $id, $data) = @_;
703
704 $self->{texture}[$id] ||= do {
705 my $tex =
532 new_from_image CFClient::Texture 706 new_from_image CFClient::Texture
533 delete $face->{image}, minify => 1; 707 $data, minify => 1;
534 708
535 $MAP->set_texture ($facenum, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 709 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
536 $MAPWIDGET->update; 710 $MAPWIDGET->update;
711
712 $tex
713 };
537} 714}
538 715
539sub conn::query { 716sub conn::query {
540 my ($self, $flags, $prompt) = @_; 717 my ($self, $flags, $prompt) = @_;
541 718
547 my ($self, $color, $text) = @_; 724 my ($self, $color, $text) = @_;
548 725
549 my @color = ( 726 my @color = (
550 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 727 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
551 [1.00, 1.00, 1.00], 728 [1.00, 1.00, 1.00],
552 [0.00, 0.00, 0.55], 729 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
553 [1.00, 0.00, 0.00], 730 [1.00, 0.00, 0.00],
554 [1.00, 0.54, 0.00], 731 [1.00, 0.54, 0.00],
555 [0.11, 0.56, 1.00], 732 [0.11, 0.56, 1.00],
556 [0.93, 0.46, 0.00], 733 [0.93, 0.46, 0.00],
557 [0.18, 0.54, 0.34], 734 [0.18, 0.54, 0.34],
601 }, 778 },
602); 779);
603 780
604############################################################################# 781#############################################################################
605 782
606$FACECACHE = CFClient::db_table "facecache"; 783$TILECACHE = CFClient::db_table "tilecache";
784$FACEMAP = CFClient::db_table "facemap";
607 785
608CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 786CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
609 787
610my %DEF_CFG = ( 788my %DEF_CFG = (
611 sdl_mode => 0, 789 sdl_mode => 0,
635 813
636@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; 814@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
637 815
638$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; 816$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
639 817
640init_screen;
641
642{ 818{
643 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 819 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
644 820
645 CFClient::add_font $_ for @fonts; 821 CFClient::add_font $_ for @fonts;
646 CFClient::set_font $fonts[0]; 822 CFClient::set_font $fonts[0];
647} 823}
648 824
825init_screen;
826
649Event::loop; 827Event::loop;
650 828
651 829

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines