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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines