… | |
… | |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | |
14 | |
15 | use Crossfire; |
15 | use Crossfire; |
16 | use Crossfire::Protocol; |
16 | use Crossfire::Protocol; |
17 | |
17 | |
|
|
18 | use Compress::LZF; |
|
|
19 | |
18 | use CFClient; |
20 | use CFClient; |
19 | use CFClient::UI; |
21 | use CFClient::UI; |
20 | |
22 | |
21 | our $VERSION = '0.1'; |
23 | our $VERSION = '0.1'; |
22 | |
24 | |
23 | my $MAX_FPS = 60; |
25 | my $MAX_FPS = 60; |
24 | my $MIN_FPS = 5; # unused as of yet |
26 | my $MIN_FPS = 5; # unused as of yet |
25 | |
27 | |
26 | our $META_SERVER = "crossfire.real-time.com:13326"; |
28 | our $META_SERVER = "crossfire.real-time.com:13326"; |
27 | |
29 | |
|
|
30 | our $FACEMAP; |
|
|
31 | our $TILECACHE; |
28 | our $FACECACHE; |
32 | our $MAPCACHE; |
29 | |
33 | |
30 | our $LAST_REFRESH; |
34 | our $LAST_REFRESH; |
31 | our $NOW; |
35 | our $NOW; |
32 | |
36 | |
33 | our $CFG; |
37 | our $CFG; |
… | |
… | |
69 | |
73 | |
70 | sub start_game { |
74 | sub 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 | |
… | |
… | |
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::LineEntry |
|
|
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 ("command $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 ("command $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 | }; |
… | |
… | |
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 |
422 | } |
442 | } |
423 | |
443 | |
424 | sub destroy_screen { |
444 | sub destroy_screen { |
425 | $CFClient::UI::ROOT->{children} = []; |
445 | $CFClient::UI::ROOT->{children} = []; |
426 | undef $SDL_ACTIVE; |
446 | undef $SDL_ACTIVE; |
… | |
… | |
489 | |
509 | |
490 | $self->send ($command); |
510 | $self->send ($command); |
491 | status $command; |
511 | status $command; |
492 | } |
512 | } |
493 | |
513 | |
|
|
514 | sub conn::map_scroll { |
|
|
515 | my ($self, $dx, $dy) = @_; |
|
|
516 | |
|
|
517 | $MAP->scroll ($dx, $dy); |
|
|
518 | } |
|
|
519 | |
494 | sub conn::feed_map1a { |
520 | sub conn::feed_map1a { |
495 | my ($self, $data) = @_; |
521 | my ($self, $data) = @_; |
496 | |
522 | |
497 | # $self->Crossfire::Protocol::feed_map1a ($data); |
523 | # $self->Crossfire::Protocol::feed_map1a ($data); |
498 | |
524 | |
499 | $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y}); |
|
|
500 | $MAP->map1a_update ($data); |
525 | $MAP->map1a_update ($data); |
501 | $MAPWIDGET->update; |
526 | $MAPWIDGET->update; |
502 | } |
527 | } |
503 | |
528 | |
504 | #sub conn::map_update { |
529 | sub conn::flush_map { |
505 | # my ($self, $dirty) = @_; |
530 | my ($self) = @_; |
506 | # |
531 | |
507 | # $MAPWIDGET->update; |
532 | my $map_info = delete $self->{map_info} |
508 | #} |
533 | or return; |
|
|
534 | |
|
|
535 | my ($hash, $x, $y, $w, $h) = @$map_info; |
|
|
536 | |
|
|
537 | my $data = $MAP->get_rect ($x, $y, $w, $h); |
|
|
538 | $MAPCACHE->put ($hash => Compress::LZF::compress $data); |
|
|
539 | |
|
|
540 | warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# |
|
|
541 | |
|
|
542 | } |
509 | |
543 | |
510 | sub conn::map_clear { |
544 | sub conn::map_clear { |
511 | my ($self) = @_; |
545 | my ($self) = @_; |
512 | |
546 | |
|
|
547 | $self->flush_map; |
|
|
548 | delete $self->{neigh}; |
|
|
549 | |
513 | $MAP->clear; |
550 | $MAP->clear; |
|
|
551 | } |
514 | |
552 | |
515 | # refresh; |
553 | |
|
|
554 | sub conn::load_map($$$) { |
|
|
555 | my ($self, $hash, $x, $y) = @_; |
|
|
556 | |
|
|
557 | if (defined (my $data = $MAPCACHE->get ($hash))) { |
|
|
558 | $data = Compress::LZF::decompress $data; |
|
|
559 | warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# |
|
|
560 | for my $id ($MAP->set_rect ($x, $y, $data)) { |
|
|
561 | my $data = $TILECACHE->get ($id) |
|
|
562 | or next; |
|
|
563 | |
|
|
564 | $self->set_texture ($id => $data); |
|
|
565 | } |
|
|
566 | } |
|
|
567 | } |
|
|
568 | |
|
|
569 | sub conn::flood_fill { |
|
|
570 | my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_; |
|
|
571 | |
|
|
572 | # the server doe snot allow map paths > 6 |
|
|
573 | return if 6 <= length $path; |
|
|
574 | |
|
|
575 | for my $tile (1..4) { |
|
|
576 | next if $self->{neigh}{$hash}[$tile]; |
|
|
577 | next unless $flags & (1 << ($tile - 1)); |
|
|
578 | |
|
|
579 | my $neigh = $self->{neigh}{$hash} ||= []; |
|
|
580 | |
|
|
581 | $self->send_mapinfo ("spatial $path$tile", sub { |
|
|
582 | my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; |
|
|
583 | |
|
|
584 | warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d# |
|
|
585 | return if $mode ne "spatial"; |
|
|
586 | |
|
|
587 | $x += $MAP->ox; |
|
|
588 | $y += $MAP->oy; |
|
|
589 | |
|
|
590 | $self->load_map ($hash, $x, $y); |
|
|
591 | |
|
|
592 | $neigh->[$tile] = [$x, $y, $w, $h]; |
|
|
593 | |
|
|
594 | $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1) |
|
|
595 | if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; |
|
|
596 | }); |
|
|
597 | } |
|
|
598 | } |
|
|
599 | |
|
|
600 | sub conn::map_change { |
|
|
601 | my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; |
|
|
602 | |
|
|
603 | $self->flush_map; |
|
|
604 | |
|
|
605 | my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); |
|
|
606 | |
|
|
607 | warn "$ox $oy map_info<$flags, $x, $y, $w, $h, $hash>\n";#d# |
|
|
608 | |
|
|
609 | my $mapmapw = 250; |
|
|
610 | my $mapmaph = 250; |
|
|
611 | |
|
|
612 | $self->flood_fill ("", $hash, $flags, |
|
|
613 | $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5, |
|
|
614 | $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5); |
|
|
615 | |
|
|
616 | $x += $ox; |
|
|
617 | $y += $oy; |
|
|
618 | |
|
|
619 | $self->{map_info} = [$hash, $x, $y, $w, $h]; |
|
|
620 | |
|
|
621 | $self->load_map ($hash, $x, $y); |
516 | } |
622 | } |
517 | |
623 | |
518 | sub conn::face_find { |
624 | sub conn::face_find { |
519 | my ($self, $face) = @_; |
625 | my ($self, $facenum, $face) = @_; |
520 | |
626 | |
521 | my $data; |
627 | my $hash = "$face->{chksum},$face->{name}"; |
522 | |
628 | |
523 | $FACECACHE->get ("$face->{chksum},$face->{name}") |
629 | my $id = $FACEMAP->get ($hash); |
|
|
630 | |
|
|
631 | unless ($id) { |
|
|
632 | # create new id for face |
|
|
633 | # i love transactions |
|
|
634 | for (1..100) { |
|
|
635 | my $txn = $CFClient::DB_ENV->txn_begin; |
|
|
636 | my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW); |
|
|
637 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
|
|
638 | $id++; |
|
|
639 | if ($FACEMAP->put (id => $id) == 0 |
|
|
640 | && $FACEMAP->put ($hash => $id) == 0) { |
|
|
641 | $txn->txn_commit; |
|
|
642 | |
|
|
643 | goto gotid; |
|
|
644 | } |
|
|
645 | } |
|
|
646 | $txn->abort; |
|
|
647 | } |
|
|
648 | |
|
|
649 | CFClient::fatal "maximum number of transaction retries reached - database problems?"; |
|
|
650 | } |
|
|
651 | |
|
|
652 | gotid: |
|
|
653 | $face->{id} = $id; |
|
|
654 | $MAP->set_face ($facenum => $id); |
|
|
655 | $TILECACHE->get ($id) |
524 | } |
656 | } |
525 | |
657 | |
526 | sub conn::face_update { |
658 | sub conn::face_update { |
527 | my ($self, $facenum, $face) = @_; |
659 | my ($self, $facenum, $face) = @_; |
528 | |
660 | |
529 | $FACECACHE->put ("$face->{chksum},$face->{name}" => $face->{image}); |
661 | $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes |
530 | |
662 | |
531 | my $tex = $face->{texture} = |
663 | $self->set_texture ($face->{id} => delete $face->{image}); |
|
|
664 | } |
|
|
665 | |
|
|
666 | sub conn::set_texture { |
|
|
667 | my ($self, $id, $data) = @_; |
|
|
668 | |
|
|
669 | $self->{texture}[$id] ||= do { |
|
|
670 | my $tex = |
532 | new_from_image CFClient::Texture |
671 | new_from_image CFClient::Texture |
533 | delete $face->{image}, minify => 1; |
672 | $data, minify => 1; |
534 | |
673 | |
535 | $MAP->set_texture ($facenum, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
674 | $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); |
536 | $MAPWIDGET->update; |
675 | $MAPWIDGET->update; |
|
|
676 | |
|
|
677 | $tex |
|
|
678 | }; |
537 | } |
679 | } |
538 | |
680 | |
539 | sub conn::query { |
681 | sub conn::query { |
540 | my ($self, $flags, $prompt) = @_; |
682 | my ($self, $flags, $prompt) = @_; |
541 | |
683 | |
… | |
… | |
547 | my ($self, $color, $text) = @_; |
689 | my ($self, $color, $text) = @_; |
548 | |
690 | |
549 | my @color = ( |
691 | my @color = ( |
550 | [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], |
692 | [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], |
551 | [1.00, 1.00, 1.00], |
693 | [1.00, 1.00, 1.00], |
552 | [0.00, 0.00, 0.55], |
694 | [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] |
553 | [1.00, 0.00, 0.00], |
695 | [1.00, 0.00, 0.00], |
554 | [1.00, 0.54, 0.00], |
696 | [1.00, 0.54, 0.00], |
555 | [0.11, 0.56, 1.00], |
697 | [0.11, 0.56, 1.00], |
556 | [0.93, 0.46, 0.00], |
698 | [0.93, 0.46, 0.00], |
557 | [0.18, 0.54, 0.34], |
699 | [0.18, 0.54, 0.34], |
… | |
… | |
601 | }, |
743 | }, |
602 | ); |
744 | ); |
603 | |
745 | |
604 | ############################################################################# |
746 | ############################################################################# |
605 | |
747 | |
606 | $FACECACHE = CFClient::db_table "facecache"; |
748 | $TILECACHE = CFClient::db_table "tilecache"; |
|
|
749 | $FACEMAP = CFClient::db_table "facemap"; |
607 | |
750 | |
608 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
751 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
609 | |
752 | |
610 | my %DEF_CFG = ( |
753 | my %DEF_CFG = ( |
611 | sdl_mode => 0, |
754 | sdl_mode => 0, |