… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | use CFClient::UI; |
9 | use CFClient::UI; |
10 | |
10 | |
11 | use base 'Crossfire::Protocol::Base'; |
11 | use base 'Crossfire::Protocol::Base'; |
|
|
12 | |
|
|
13 | our %open_logs; |
12 | |
14 | |
13 | sub new { |
15 | sub new { |
14 | my $class = shift; |
16 | my $class = shift; |
15 | |
17 | |
16 | my $self = $class->SUPER::new (@_); |
18 | my $self = $class->SUPER::new (@_); |
… | |
… | |
58 | # per server |
60 | # per server |
59 | $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; |
61 | $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; |
60 | |
62 | |
61 | $self |
63 | $self |
62 | } |
64 | } |
|
|
65 | |
|
|
66 | sub logprint { |
|
|
67 | my ($self, @a) = @_; |
|
|
68 | my $filename = "$Crossfire::VARDIR/log.$self->{host}"; |
|
|
69 | |
|
|
70 | my $fh = $open_logs{$filename}; |
|
|
71 | unless ($fh) { |
|
|
72 | # FIXME: handle this more gracefully? |
|
|
73 | open $fh, ">>", $filename |
|
|
74 | or die "Couldn't open logfile: log.$self->{host}: $!"; |
|
|
75 | |
|
|
76 | $open_logs{$filename} = $fh; |
|
|
77 | } |
|
|
78 | |
|
|
79 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime (time); |
|
|
80 | |
|
|
81 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
82 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
83 | |
|
|
84 | print $fh "$ts ", @a, "\n"; |
|
|
85 | $fh->flush; |
|
|
86 | } |
|
|
87 | |
|
|
88 | |
63 | |
89 | |
64 | sub stats_update { |
90 | sub stats_update { |
65 | my ($self, $stats) = @_; |
91 | my ($self, $stats) = @_; |
66 | |
92 | |
67 | if (my $exp = $stats->{+CS_STAT_EXP64}) { |
93 | if (my $exp = $stats->{+CS_STAT_EXP64}) { |
… | |
… | |
79 | |
105 | |
80 | if ($self->{record}) { |
106 | if ($self->{record}) { |
81 | push @{$self->{record}}, $command; |
107 | push @{$self->{record}}, $command; |
82 | } |
108 | } |
83 | |
109 | |
|
|
110 | $self->logprint ("send: ", $command); |
84 | $self->send_command ($command); |
111 | $self->send_command ($command); |
85 | ::status $command; |
112 | ::status $command; |
86 | } |
113 | } |
87 | |
114 | |
88 | sub start_record { |
115 | sub start_record { |
… | |
… | |
105 | sub feed_map1a { |
132 | sub feed_map1a { |
106 | my ($self, $data) = @_; |
133 | my ($self, $data) = @_; |
107 | |
134 | |
108 | $self->{map}->map1a_update ($data); |
135 | $self->{map}->map1a_update ($data); |
109 | $self->{map_widget}->update; |
136 | $self->{map_widget}->update; |
|
|
137 | } |
|
|
138 | |
|
|
139 | sub magicmap { |
|
|
140 | my ($self, $w, $h, $x, $y, $data) = @_; |
|
|
141 | |
|
|
142 | $self->{map_widget}->set_magicmap ($w, $h, $x, $y, $data); |
110 | } |
143 | } |
111 | |
144 | |
112 | sub flush_map { |
145 | sub flush_map { |
113 | my ($self) = @_; |
146 | my ($self) = @_; |
114 | |
147 | |
… | |
… | |
279 | # I love transactions |
312 | # I love transactions |
280 | for (1..100) { |
313 | for (1..100) { |
281 | my $txn = $CFClient::DB_ENV->txn_begin; |
314 | my $txn = $CFClient::DB_ENV->txn_begin; |
282 | my $status = $self->{facemap}->db_get (id => $id); |
315 | my $status = $self->{facemap}->db_get (id => $id); |
283 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
316 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
284 | $id = ($id || 16) + 1; |
317 | $id = ($id || 64) + 1; |
285 | if ($self->{facemap}->put (id => $id) == 0 |
318 | if ($self->{facemap}->put (id => $id) == 0 |
286 | && $self->{facemap}->put ($hash => $id) == 0) { |
319 | && $self->{facemap}->put ($hash => $id) == 0) { |
287 | $txn->txn_commit; |
320 | $txn->txn_commit; |
288 | |
321 | |
289 | goto gotid; |
322 | goto gotid; |
… | |
… | |
369 | [0.55, 0.41, 0.13], |
402 | [0.55, 0.41, 0.13], |
370 | [0.99, 0.77, 0.26], |
403 | [0.99, 0.77, 0.26], |
371 | [0.74, 0.65, 0.41], |
404 | [0.74, 0.65, 0.41], |
372 | ); |
405 | ); |
373 | |
406 | |
|
|
407 | $self->logprint ("info: ", $text); |
|
|
408 | |
374 | my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
409 | my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; |
|
|
410 | |
|
|
411 | # try to create single paragraphs of multiple lines sent by the server |
|
|
412 | $text =~ s/(?<=\S)\n(?=\w)/ /g; |
375 | |
413 | |
376 | $text = CFClient::UI::Label::escape $text; |
414 | $text = CFClient::UI::Label::escape $text; |
377 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
415 | $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; |
378 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
416 | $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; |
379 | |
417 | |
… | |
… | |
395 | } |
433 | } |
396 | |
434 | |
397 | sub spell_add { |
435 | sub spell_add { |
398 | my ($self, $spell) = @_; |
436 | my ($self, $spell) = @_; |
399 | |
437 | |
400 | # TODO |
438 | # try to create single paragraphs of multiple lines sent by the server |
401 | # create a widget dynamically, using spell face (CF::Protocol downloads them) |
439 | $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; |
|
|
440 | $spell->{message} =~ s/\n+$//; |
|
|
441 | $spell->{message} ||= "Server did not provide a description for this spell."; |
|
|
442 | |
402 | $::SETUP_SPELLS->add_spell ($spell); |
443 | $::SETUP_SPELLS->add_spell ($spell); |
403 | |
444 | |
404 | $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
445 | $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
405 | $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
446 | $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
406 | } |
447 | } |
… | |
… | |
483 | |
524 | |
484 | $::FLOORBOX->clear; |
525 | $::FLOORBOX->clear; |
485 | |
526 | |
486 | my $row; |
527 | my $row; |
487 | for (@{ $::CONN->{container}{0} }) { |
528 | for (@{ $::CONN->{container}{0} }) { |
488 | if ($row < 7) { |
529 | if ($row < 6) { |
489 | local $_->{face_widget}; # hack to force recreation of widget |
530 | local $_->{face_widget}; # hack to force recreation of widget |
490 | local $_->{desc_widget}; # hack to force recreation of widget |
531 | local $_->{desc_widget}; # hack to force recreation of widget |
491 | CFClient::Item::update_widgets $_; |
532 | CFClient::Item::update_widgets $_; |
492 | |
533 | |
493 | $::FLOORBOX->add (0, $row, $_->{face_widget}); |
534 | $::FLOORBOX->add (0, $row, $_->{face_widget}); |
494 | $::FLOORBOX->add (1, $row, $_->{desc_widget}); |
535 | $::FLOORBOX->add (1, $row, $_->{desc_widget}); |
495 | |
536 | |
496 | $row++; |
537 | $row++; |
497 | } else { |
538 | } else { |
498 | $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More..."); |
539 | $::FLOORBOX->add (1, $row, new CFClient::UI::Button |
|
|
540 | text => "More...", |
|
|
541 | on_activate => sub { $::INV_WINDOW->toggle_visibility }, |
|
|
542 | ); |
499 | last; |
543 | last; |
500 | } |
544 | } |
501 | } |
545 | } |
502 | }); |
546 | }); |
503 | |
547 | |
… | |
… | |
612 | } |
656 | } |
613 | } |
657 | } |
614 | |
658 | |
615 | sub player_update { |
659 | sub player_update { |
616 | my ($self, $player) = @_; |
660 | my ($self, $player) = @_; |
|
|
661 | |
617 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
662 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
|
|
663 | } |
618 | |
664 | |
619 | # do it here because it is ignored earlier, and there is no "login" event |
665 | sub update_server_info { |
|
|
666 | my ($self) = @_; |
|
|
667 | |
|
|
668 | my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>"); |
|
|
669 | |
|
|
670 | $::SERVER_INFO->set_markup ( |
|
|
671 | "server <tt>$self->{host}:$self->{port}</tt>\n" |
|
|
672 | . "protocol version <tt>$self->{version}</tt>\n" |
|
|
673 | . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" |
|
|
674 | . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" |
|
|
675 | . "cfplus support $yesno[$self->{cfplus_ext} > 0]" |
|
|
676 | . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" |
|
|
677 | . "map size $self->{mapw}×$self->{maph}\n" |
|
|
678 | ); |
|
|
679 | } |
|
|
680 | |
|
|
681 | sub logged_in { |
|
|
682 | my ($self) = @_; |
|
|
683 | |
|
|
684 | $self->send_ext_req (cfplus_support => "1", sub { |
|
|
685 | $self->{cfplus_ext} = $_[0]; |
|
|
686 | $self->update_server_info; |
|
|
687 | }); |
|
|
688 | |
|
|
689 | $self->update_server_info; |
|
|
690 | |
620 | $self->send_command ("output-sync $::CFG->{output_sync}"); |
691 | $self->send_command ("output-sync $::CFG->{output_sync}"); |
621 | $self->send_command ("output-count $::CFG->{output_count}"); |
692 | $self->send_command ("output-count $::CFG->{output_count}"); |
|
|
693 | $self->send_command ("pickup $::CFG->{pickup}"); |
|
|
694 | } |
|
|
695 | |
|
|
696 | sub lookat { |
|
|
697 | my ($self, $x, $y) = @_; |
|
|
698 | |
|
|
699 | if ($self->{cfplus_ext}) { |
|
|
700 | $self->send_ext_req (lookat => "$x $y", sub { |
|
|
701 | my %res = split /\x00/, $_[0]; |
|
|
702 | |
|
|
703 | if (exists $res{npc_dialog}) { |
|
|
704 | # start npc chat dialog |
|
|
705 | $self->{npc_dialog} = new CFClient::NPCDialog:: |
|
|
706 | dx => $x, |
|
|
707 | dy => $y, |
|
|
708 | title => "$res{npc_dialog} (NPC)", |
|
|
709 | conn => $self, |
|
|
710 | ; |
|
|
711 | } |
|
|
712 | }); |
|
|
713 | } |
|
|
714 | |
|
|
715 | $self->send ("lookat $x $y"); |
|
|
716 | } |
|
|
717 | |
|
|
718 | sub destroy { |
|
|
719 | my ($self) = @_; |
|
|
720 | |
|
|
721 | $self->{npc_dialog}->destroy |
|
|
722 | if $self->{npc_dialog}; |
|
|
723 | |
|
|
724 | $self->SUPER::destroy; |
|
|
725 | } |
|
|
726 | |
|
|
727 | package CFClient::NPCDialog; |
|
|
728 | |
|
|
729 | our @ISA = 'CFClient::UI::FancyFrame'; |
|
|
730 | |
|
|
731 | sub new { |
|
|
732 | my $class = shift; |
|
|
733 | |
|
|
734 | my $self = $class->SUPER::new ( |
|
|
735 | x => 'center', |
|
|
736 | y => 'center', |
|
|
737 | name => "npc_dialog", |
|
|
738 | force_w => $::WIDTH * 0.7, |
|
|
739 | force_h => $::HEIGHT * 0.7, |
|
|
740 | title => "NPC Dialog", |
|
|
741 | kw => { hi => 0, yes => 0, no => 0 }, |
|
|
742 | @_, |
|
|
743 | ); |
|
|
744 | |
|
|
745 | Scalar::Util::weaken (my $this = $self); |
|
|
746 | |
|
|
747 | # better use a pane... |
|
|
748 | $self->add (my $hbox = new CFClient::UI::HBox); |
|
|
749 | $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1); |
|
|
750 | |
|
|
751 | $hbox->add (my $vbox = new CFClient::UI::VBox); |
|
|
752 | |
|
|
753 | $vbox->add (new CFClient::UI::Label text => "Message Entry:"); |
|
|
754 | $vbox->add ($self->{entry} = new CFClient::UI::Entry |
|
|
755 | tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n" |
|
|
756 | . "Sometimes you have to tell an NPC something you cannot find out during " |
|
|
757 | . "a normal conversation (such as a password). In those cases you have to use " |
|
|
758 | . "this text entry. You can also enter responses manually instead of using the response " |
|
|
759 | . "buttons below.", |
|
|
760 | on_activate => sub { |
|
|
761 | my ($entry, $text) = @_; |
|
|
762 | |
|
|
763 | return unless $text =~ /\S/; |
|
|
764 | |
|
|
765 | $entry->set_text (""); |
|
|
766 | $this->send ($text); |
|
|
767 | }, |
|
|
768 | ); |
|
|
769 | |
|
|
770 | $vbox->add ($self->{options} = new CFClient::UI::VBox); |
|
|
771 | |
|
|
772 | $self->{close_button} = new CFClient::UI::Button |
|
|
773 | text => "Bye (close)", |
|
|
774 | tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", |
|
|
775 | on_activate => sub { $this->destroy }, |
|
|
776 | ; |
|
|
777 | |
|
|
778 | $self->update_options; |
|
|
779 | |
|
|
780 | $self->{token} = $self->{conn}->ext_token; |
|
|
781 | $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); |
|
|
782 | $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); |
|
|
783 | |
|
|
784 | $self->{entry}->focus_in; |
|
|
785 | |
|
|
786 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); |
|
|
787 | |
|
|
788 | $self->show; |
|
|
789 | $self |
|
|
790 | }; |
|
|
791 | |
|
|
792 | sub update_options { |
|
|
793 | my ($self) = @_; |
|
|
794 | |
|
|
795 | Scalar::Util::weaken $self; |
|
|
796 | |
|
|
797 | $self->{options}->clear; |
|
|
798 | $self->{options}->add ($self->{close_button}); |
|
|
799 | |
|
|
800 | for my $kw (sort keys %{ $self->{kw} }) { |
|
|
801 | $self->{options}->add (new CFClient::UI::Button |
|
|
802 | text => $kw, |
|
|
803 | on_activate => sub { |
|
|
804 | $self->send ($kw); |
|
|
805 | }, |
|
|
806 | ); |
|
|
807 | } |
|
|
808 | } |
|
|
809 | |
|
|
810 | sub feed { |
|
|
811 | my ($self, $data) = @_; |
|
|
812 | |
|
|
813 | my ($type, $msg) = split / /, $data, 2; |
|
|
814 | |
|
|
815 | if ($type eq "msg") { |
|
|
816 | my ($msg, @kw) = split /\x00/, $msg; |
|
|
817 | $self->{kw}{$_} = 1 for @kw; |
|
|
818 | |
|
|
819 | $msg = CFClient::UI::Label::escape $msg; |
|
|
820 | my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; |
|
|
821 | $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh. |
|
|
822 | |
|
|
823 | $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); |
|
|
824 | $self->update_options; |
|
|
825 | } else { |
|
|
826 | $self->destroy; |
|
|
827 | } |
|
|
828 | |
|
|
829 | 1 |
|
|
830 | } |
|
|
831 | |
|
|
832 | sub send { |
|
|
833 | my ($self, $msg) = @_; |
|
|
834 | |
|
|
835 | $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); |
|
|
836 | $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg); |
|
|
837 | } |
|
|
838 | |
|
|
839 | sub destroy { |
|
|
840 | my ($self) = @_; |
|
|
841 | |
|
|
842 | #Carp::cluck "debug\n";#d# #todo# enable: destroyx gets called twice because scalar keys {} is 1 |
|
|
843 | |
|
|
844 | delete $self->{conn}{npc_dialog}; |
|
|
845 | $self->{conn}->disconnect_ext ($self->{token}); |
|
|
846 | |
|
|
847 | $self->SUPER::destroy; |
622 | } |
848 | } |
623 | |
849 | |
624 | 1; |
850 | 1; |