ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Protocol.pm (file contents):
Revision 1.96 by root, Fri Apr 6 07:45:35 2007 UTC vs.
Revision 1.147 by root, Thu Aug 9 03:40:01 2007 UTC

15use Crossfire::Protocol::Base 0.95; 15use Crossfire::Protocol::Base 0.95;
16 16
17use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
18 18
19sub new { 19sub new {
20 my $class = shift; 20 my ($class, %arg) = @_;
21 21
22 my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); 22 my $self = $class->SUPER::new (%arg,
23 setup_req => {
24 extmap => 1,
25 excmd => 1,
26 ywidget => 1,#d#
27 %{$arg{setup_req} || {}},
28 },
29 );
23 30
24 $self->{map_widget}->clr_commands; 31 $self->{map_widget}->clr_commands;
25 32
26 my @cmd_help = map { 33 my @cmd_help = map {
27 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 34 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
41 sort { (length $a) <=> (length $b) } 48 sort { (length $a) <=> (length $b) }
42 @args 49 @args
43 } sort { $a->{par} <=> $b->{par} } 50 } sort { $a->{par} <=> $b->{par} }
44 CFPlus::Pod::find command => "*"; 51 CFPlus::Pod::find command => "*";
45 52
53 $self->connect_ext (ambient_music => sub {
54 my ($songs) = @_;
55 &::audio_music_set_ambient ($songs);
56 });
57
46 $self->connect_ext (event_capabilities => sub { 58 $self->connect_ext (capabilities => sub {
47 my ($cap) = @_; 59 my (%cap) = @_;
48 60
61 #$self->send ("setup sound 0"); # we use a different protocol
62 $self->update_fx_want;
63
49 if (my $ts = $cap->{tileset}) { 64 if (my $ts = $cap{tileset}) {
50 if (my ($default) = grep $_->[2] & 1, @$ts) { 65 if (my ($default) = grep $_->[2] & 1, @$ts) {
51 $self->{tileset} = $default; 66 $self->{tileset} = $default;
52 $self->{tilesize} = $default->[3]; 67 $self->{tilesize} = $default->[3];
53 $self->setup_req (tileset => $default->[0]); 68 $self->setup_req (tileset => $default->[0]);
54 69
58 $self->setup_req (mapsize => "${w}x${h}"); 73 $self->setup_req (mapsize => "${w}x${h}");
59 } 74 }
60 } 75 }
61 }); 76 });
62 77
78 $self->{json_coder}
79 ->convert_blessed
80 ->filter_json_single_key_object (__w_ => sub {
81 $self->{widget}{$_[0]}
82 });
83
84 # widgetset new
85 $self->connect_ext (ws_n => sub {
86 my ($id) = @_;
87
88 $self->{widgetset}{$id} = {
89 w => {},
90 };
91 });
92
93 # widgetset destroy
94 $self->connect_ext (ws_d => sub {
95 my ($id) = @_;
96
97 my $ws = delete $self->{widgetset}{$id}
98 or return;
99
100 $_->destroy
101 for values %{$ws->{w}};
102 });
103
104 # widgetset create
105 $self->connect_ext (ws_c => sub {
106 my ($ws, $id, $class, $args) = @_;
107
108 for my $ev (grep /^on_/, keys %$args) {
109 $args->{$ev} = sub {
110 my $id = shift->{s_id};
111 $self->send_exti_msg (w_e => $id, $ev, \@_);
112
113 1
114 };
115 }
116
117 if (my $widget = eval {
118 local $SIG{__DIE__};
119 "CFPlus::UI::$class"->new (
120 %$args,
121 s_ws => $ws,
122 s_id => $id,
123 )
124 }
125 ) {
126 $self->{widget}{$id}
127 = $self->{widgetset}{$ws}{w}{$id}
128 = $widget;
129
130 $widget->connect (on_destroy => sub {
131 my ($widget) = @_;
132
133 delete $self->{widget}{$widget->{s_id}};
134 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}};
135 });
136 } else {
137 warn "server failed creating client-side widget " . (CFPlus::to_json $class) . ": $@\n";
138 $self->send_exti_msg (w_e => $id, "destroy");
139 }
140 });
141
142 # widget call
143 $self->connect_ext (w_c => sub {
144 my ($id, $rid, $method, $args) = @_;
145
146 my $w = $self->{widget}{$id}
147 or return;
148
149 $args ||= [];
150
151 if ($rid) {
152 $self->send_exti_msg (w_r => $rid, [$w->$method (@$args)]);
153 } else {
154 $w->$method (@$args);
155 }
156 });
157
158 # widget set
159 $self->connect_ext (w_s => sub {
160 my ($id, $attr) = @_;
161
162 my $w = $self->{widget}{$id}
163 or return;
164
165 for (my $i = 0; $i < $#$attr; $i += 2) {
166 my ($member, $value) = @$attr[$i, $i+1];
167 if (defined $value) {
168 $w->{$member} = $value;
169 } else {
170 delete $w->{$member};
171 }
172 $w->{parent}->realloc if $member =~ /^c_/ && $w->{visible};
173 }
174 });
175
176 # widget get
177 $self->connect_ext (w_g => sub {
178 my ($id, $rid, $attr) = @_;
179
180 my $w = $self->{widget}{$id}
181 or return;
182
183 $self->send_exti_msg (w_r => $rid, [map $w->{$_}, @$attr]);
184 });
185
186 # message window
187 $self->connect_ext (channel_info => sub {
188 my ($info) = @_;
189 $self->{channels}->{$info->{id}} = $info;
190 $::MESSAGE_WINDOW->add_channel ($info);
191 });
192
193 # destroy widgets on logout
194 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
195 for my $ws (values %{delete $self->{widgetset} || {}}) {
196 $_->destroy
197 for values %{delete $ws->{w} || {}};
198 }
199 });
200
63 $self->{map_widget}->add_command (@$_) 201 $self->{map_widget}->add_command (@$_)
64 for @cmd_help; 202 for @cmd_help;
65 203
66 { 204 {
67 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 205 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
81 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 219 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
82 220
83 $self 221 $self
84} 222}
85 223
224sub update_fx_want {
225 my ($self) = @_;
226
227 $self->send_exti_msg (fx_want => {
228 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
229 5 => !!$::CFG->{audio_enable}, # FT_SOUND
230 });
231}
232
86sub logprint { 233sub logprint {
87 my ($self, @a) = @_; 234 my ($self, @a) = @_;
88 235
89 $self->{log_fh} ||= do { 236 CFPlus::DB::logprint "$Crossfire::VARDIR/log.$self->{host}" => (join "", @a), sub { };
90 my $path = "$Crossfire::VARDIR/log.$self->{host}";
91
92 open my $fh, ">>:utf8", $path
93 or die "Couldn't open logfile $path: $!";
94
95 $fh->autoflush (1);
96
97 $fh;
98 };
99
100 my ($sec, $min, $hour, $mday, $mon, $year) = localtime time;
101
102 my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d",
103 $year + 1900, $mon + 1, $mday, $hour, $min, $sec;
104
105 print {$self->{log_fh}} "$ts ", @a, "\n";
106} 237}
107 238
108sub _stat_numdiff { 239sub _stat_numdiff {
109 my ($self, $name, $old, $new) = @_; 240 my ($self, $name, $old, $new) = @_;
110 241
243 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 374 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
244 $::GAUGES->{food} ->set_value ($fo, $fo_m); 375 $::GAUGES->{food} ->set_value ($fo, $fo_m);
245 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 376 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
246 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 377 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
247 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 378 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
248 my $rng = $stats->{+CS_STAT_RANGE}; 379 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
249 $rng =~ s/^Range: //; # thank you so much dear server
250 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
251 my $title = $stats->{+CS_STAT_TITLE}; 380 my $title = $stats->{+CS_STAT_TITLE};
252 $title =~ s/^Player: //; 381 $title =~ s/^Player: //;
253 $::STATWIDS->{title} ->set_text ("Title: " . $title); 382 $::STATWIDS->{title} ->set_text ("Title: " . $title);
254 383
255 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR}); 384 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
275 my @skills = keys %{ $self->{skill_info} }; 404 my @skills = keys %{ $self->{skill_info} };
276 405
277 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) { 406 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
278 $sktbl->clear; 407 $sktbl->clear;
279 408
409 my @add;
410
411 push @add,
280 $sktbl->add (0, 0, new CFPlus::UI::Label text => "Experience", align => 1); 412 0, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
281 $sktbl->add (1, 0, new CFPlus::UI::Label text => "Lvl.", align => 1); 413 1, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
282 $sktbl->add (2, 0, new CFPlus::UI::Label text => "Skill", expand => 1); 414 2, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
283 $sktbl->add (3, 0, new CFPlus::UI::Label text => "Experience", align => 1); 415 3, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
284 $sktbl->add (4, 0, new CFPlus::UI::Label text => "Lvl.", align => 1); 416 4, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
285 $sktbl->add (5, 0, new CFPlus::UI::Label text => "Skill", expand => 1); 417 5, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
418 ;
286 419
287 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 420 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
288 421
289 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 422 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
290 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 423 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
318 } 451 }
319 452
320 1 453 1
321 }; 454 };
322 455
456 push @add,
323 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label 457 $x * 3 + 0, $y, ($self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
324 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP); 458 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP),
325 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label 459 $x * 3 + 1, $y, ($self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
326 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL); 460 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL),
327 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb, 461 $x * 3 + 2, $y, (new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
328 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL); 462 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL),
463 ;
329 464
330 $x++ and ($x, $y) = (0, $y + 1); 465 $x++ and ($x, $y) = (0, $y + 1);
331 } 466 }
467
468 $sktbl->add_at (@add);
332 } 469 }
333 470
334 for (grep exists $stats->{$_}, @skills) { 471 for (grep exists $stats->{$_}, @skills) {
335 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 472 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
336 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 473 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
402 539
403 $self->{map}->clear; 540 $self->{map}->clear;
404 delete $self->{map_widget}{magicmap}; 541 delete $self->{map_widget}{magicmap};
405} 542}
406 543
544sub bg_fetch {
545 my ($self) = @_;
546
547 my $id;
548
549 do {
550 $id = pop @{$self->{bg_fetch}}
551 or return;
552 } while $self->{texture}[$id];
553
554 CFPlus::DB::exists tilecache => $id, sub {
555 my ($exists) = @_;
556
557 return unless $self->{map}; # stop when destroyed
558
559 $self->have_tile ($id)
560 if $exists;
561
562 $self->bg_fetch;
563 };
564}
407 565
408sub load_map($$$) { 566sub load_map($$$) {
409 my ($self, $hash, $x, $y) = @_; 567 my ($self, $hash, $x, $y) = @_;
410 568
569 my $gen = $self->{map_change_gen};
570
411 CFPlus::DB::get $self->{mapcache} => $hash, sub { 571 CFPlus::DB::get $self->{mapcache} => $hash, sub {
572 return unless $gen == $self->{map_change_gen};
573
412 my ($data) = @_; 574 my ($data) = @_;
413 575
414 if (defined $data) { 576 if (defined $data) {
415 $data = Compress::LZF::decompress $data; 577 $data = Compress::LZF::decompress $data;
416 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 578 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
417 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
418 CFPlus::DB::get tilecache => $id, sub {
419 my ($data) = @_;
420 579
421 $self->set_texture ($id => $data) 580 my $inprogress = @{ $self->{bg_fetch} || [] };
422 if defined $data; 581 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
423 }; 582 $self->bg_fetch unless $inprogress;
424 }
425 } 583 }
426 }; 584 };
427} 585}
428 586
429# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 587# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
495 653
496 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 654 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
497 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 655 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
498 656
499 } else { 657 } else {
658 my $gen = $self->{map_change_gen};
500 $self->send_mapinfo ("spatial $path$tile", sub { 659 $self->send_mapinfo ("spatial $path$tile", sub {
660 return unless $gen == $self->{map_change_gen};
661
501 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 662 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
502 663
503 return if $mode ne "spatial"; 664 return if $mode ne "spatial";
504 665
505 $x += $self->{map}->ox; 666 $x += $self->{map}->ox;
519 680
520sub map_change { 681sub map_change {
521 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 682 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
522 683
523 $self->flush_map; 684 $self->flush_map;
685
686 ++$self->{map_change_gen};
524 687
525 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 688 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
526 689
527 my $mapmapw = $self->{mapmap}->{w}; 690 my $mapmapw = $self->{mapmap}->{w};
528 my $mapmaph = $self->{mapmap}->{h}; 691 my $mapmaph = $self->{mapmap}->{h};
547} 710}
548 711
549sub face_find { 712sub face_find {
550 my ($self, $facenum, $face, $cb) = @_; 713 my ($self, $facenum, $face, $cb) = @_;
551 714
552 my $hash = "$face->{chksum},$face->{name}"; 715 if ($face->{type} == 0) { # FT_FACE
553
554 my $id = CFPlus::DB::get_tile_id_sync $hash; 716 my $id = CFPlus::DB::get_tile_id_sync $face->{name};
555 717
556 $face->{id} = $id; 718 $face->{id} = $id;
557 $self->{faceid}[$facenum] = $id; 719 $self->{faceid}[$facenum] = $id;
558 720
559 $self->{map}->set_tileid ($facenum => $id); 721 $self->{map}->set_tileid ($facenum => $id);
560 722
561 CFPlus::DB::get tilecache => $id, $cb; 723 CFPlus::DB::get tilecache => $id, $cb;
724
725 } elsif ($face->{type} & 1) { # with metadata
726 #::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
727 CFPlus::DB::get res_meta => $face->{name}, $cb;
728 }
562} 729}
563 730
564sub face_update { 731sub face_update {
565 my ($self, $facenum, $face, $changed) = @_; 732 my ($self, $facenum, $face, $changed) = @_;
566 733
734 if ($face->{type} == 0) { # FT_FACE
567 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { } 735 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
568 if $changed; 736 if $changed;
569 737
570 $self->set_texture ($face->{id} => delete $face->{image}); 738 $self->have_tile ($face->{id});
571}
572 739
573sub set_texture { 740 } elsif ($face->{type} & 1) { # split metadata
741 if ($changed) { # new data
742 my ($meta_json, $data) = unpack "(w/a*)*", delete $face->{data};
743 $face->{meta} = $self->{json_coder}->decode ($meta_json);
744
745 CFPlus::DB::put res_data => $face->{name} => $data, sub { };
746 CFPlus::DB::put res_meta => $face->{name} => $meta_json, sub { };
747 } else {
748 $face->{meta} = $self->{json_coder}->decode (delete $face->{data});
749 }
750
751 ::add_license ($face);
752
753 if ($face->{type} == 3) { # FT_MUSIC
754 $self->{music_meta}{$facenum} = $face;
755
756 ::message ({ markup => "downloaded song #$facenum" })
757 if $changed;
758
759 &::audio_music_push ($facenum);
760 } elsif ($face->{type} == 5) { # FT_SOUND
761 $self->{sound_meta}{$facenum} = $face;
762
763 ::message ({ markup => "downloaded sound #$facenum" })
764 if $changed;
765
766 &::audio_sound_push ($facenum);
767 }
768
769 }
770}
771
772sub smooth_update {
773 my ($self, $facenum, $face) = @_;
774
775 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
776}
777
778sub have_tile {
779 my ($self, $tile) = @_;
780
781 return unless $self->{map};
782
783 $self->{texture}[$tile] ||= my $tex =
784 new CFPlus::Texture
785 tile => $tile, minify => 1, mipmap => 1, delay => 1;
786
787 $_->() for @{(delete $self->{face_cb}{$tile}) || []};
788}
789
790sub connect_face_update {
574 my ($self, $id, $data) = @_; 791 my ($self, $id, $cb) = @_;
575 792
576 $self->{texture}[$id] ||= do { 793 push @{$self->{face_cb}{$id}}, $cb;
577 my $tex =
578 new_from_image CFPlus::Texture
579 $data, minify => 1, mipmap => 1;
580 794
581 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 795 CFPlus::guard {
582 $self->{map_widget}->update; 796 @{$self->{face_cb}{$id}}
583 797 = grep $_ != $cb,
584 $tex 798 @{$self->{face_cb}{$id}};
585 }; 799 }
586} 800}
587 801
588sub sound_play { 802sub sound_play {
589 my ($self, $x, $y, $soundnum, $type) = @_; 803 my ($self, $type, $face, $dx, $dy, $vol) = @_;
590 804
591 $self->{sound_play}->($x, $y, $soundnum, $type); 805 &::audio_sound_play ($face, $dx, $dy, $vol)
806 unless $type & 1; # odd types are silent for future expansion
592} 807}
593 808
594my $LAST_QUERY; # server is stupid, stupid, stupid 809my $LAST_QUERY; # server is stupid, stupid, stupid
595 810
596sub query { 811sub query {
600 $LAST_QUERY = $prompt; 815 $LAST_QUERY = $prompt;
601 816
602 $self->{query}-> ($self, $flags, $prompt); 817 $self->{query}-> ($self, $flags, $prompt);
603} 818}
604 819
605sub drawinfo { 820sub sanitise_xml($) {
606 my ($self, $color, $text) = @_; 821 local $_ = shift;
607 822
608 my @color = ( 823 # we now weed out all tags we do not support
824 s%<(?!/?i>|/?u>|/?b>|fg |/fg>)%&lt;%g;
825 # now all entities
826 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
827
828 # handle some elements
829 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
830 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
831
832 $_
833}
834
835our %NAME_TO_COLOR = (
836 black => 0,
837 white => 1,
838 darkblue => 2,
839 red => 3,
840 orange => 4,
841 lightblue => 5,
842 darkorange => 6,
843 green => 7,
844 darkgreen => 8,
845 grey => 9,
846 brown => 10,
847 yellow => 11,
848 tan => 12,
849);
850
851our @CF_COLOR = (
609 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 852 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
610 [1.00, 1.00, 1.00], 853 [1.00, 1.00, 1.00],
611 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 854 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
612 [1.00, 0.00, 0.00], 855 [1.00, 0.00, 0.00],
613 [1.00, 0.54, 0.00], 856 [1.00, 0.54, 0.00],
614 [0.11, 0.56, 1.00], 857 [0.11, 0.56, 1.00],
615 [0.93, 0.46, 0.00], 858 [0.93, 0.46, 0.00],
616 [0.18, 0.54, 0.34], 859 [0.18, 0.54, 0.34],
617 [0.56, 0.73, 0.56], 860 [0.56, 0.73, 0.56],
618 [0.80, 0.80, 0.80], 861 [0.80, 0.80, 0.80],
619 [0.75, 0.61, 0.20], 862 [0.75, 0.61, 0.20],
620 [0.99, 0.77, 0.26], 863 [0.99, 0.77, 0.26],
621 [0.74, 0.65, 0.41], 864 [0.74, 0.65, 0.41],
622 ); 865);
623 866
624 my $fg = $color[$color % @color]; 867sub msg {
868 my ($self, $color, $type, $text, @extra) = @_;
625 869
870 $text = sanitise_xml $text;
871
872 if (my $cb = $self->{cb_msg}{$type}) {
873 $_->($self, $color, $type, $text, @extra) for values %$cb;
874 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
875 $type =~ s/-/_/g;
876 $self->{$type} = $text;
877 } else {
626 $self->logprint ("info: ", $text); 878 $self->logprint ("msg: ", $text);
879 return if $color < 0; # negative color == ignore if not understood
627 880
881 my $fg = $CF_COLOR[$color & 0x1f] || [1, 0, 0];
882
628 # try to create single paragraphs of multiple lines sent by the server 883 ## try to create single paragraphs of multiple lines sent by the server
884 # no longer neecssary with TRT servers
629 $text =~ s/(?<=\S)\n(?=\w)/ /g; 885 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
630 886
631 $text = CFPlus::asxml $text; 887 ::message ({ fg => $fg, markup => $_, type => $type, extra => [@extra] })
632 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
633 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
634
635 ::message ({ fg => $fg, markup => $_ })
636 for split /\n/, $text; 888 for split /\n/, $text;
637 889
638 $self->{statusbox}->add ($text, 890 $self->{statusbox}->add ($text,
639 group => $text, 891 group => $text,
640 fg => $fg, 892 fg => $fg,
641 timeout => $color >= 2 ? 180 : 10, 893 timeout => $color >= 2 ? 180 : 10,
642 tooltip_font => $::FONT_FIXED, 894 tooltip_font => $::FONT_FIXED,
643 ); 895 );
644} 896 }
645
646sub drawextinfo {
647 my ($self, $color, $type, $subtype, $message) = @_;
648
649 $self->drawinfo ($color, $message);
650} 897}
651 898
652sub spell_add { 899sub spell_add {
653 my ($self, $spell) = @_; 900 my ($self, $spell) = @_;
654 901
702 $self->{map_widget}->clr_commands; 949 $self->{map_widget}->clr_commands;
703 950
704 ::stop_game (); 951 ::stop_game ();
705} 952}
706 953
707sub image_info {
708 my ($self, $numfaces) = @_;
709
710 $self->{num_faces} = $numfaces;
711 $self->{face_prefetch} = [1 .. $numfaces];
712 $self->face_prefetch;
713}
714
715sub face_prefetch {
716 my ($self) = @_;
717
718 return unless $::CFG->{face_prefetch};
719
720 if ($self->{num_faces}) {
721 return if @{ $self->{send_queue} || [] };
722 my $todo = @{ $self->{face_prefetch} }
723 or return;
724
725 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
726
727 $self->send ("requestinfo image_sums $face $face");
728
729 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
730 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
731 } elsif (!exists $self->{num_faces}) {
732 $self->send ("requestinfo image_info");
733
734 $self->{num_faces} = 0;
735
736 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
737 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
738 }
739}
740
741sub update_floorbox { 954sub update_floorbox {
742 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub { 955 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
743 return unless $::CONN; 956 return unless $::CONN;
744 957
745 $::FLOORBOX->clear; 958 $::FLOORBOX->clear;
959
960 my @add;
746 961
747 my $row; 962 my $row;
748 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 963 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
749 if ($row < 6) { 964 if ($row < 6) {
750 local $_->{face_widget}; # hack to force recreation of widget 965 local $_->{face_widget}; # hack to force recreation of widget
751 local $_->{desc_widget}; # hack to force recreation of widget 966 local $_->{desc_widget}; # hack to force recreation of widget
752 CFPlus::Item::update_widgets $_; 967 CFPlus::Item::update_widgets $_;
753 968
969 push @add,
754 $::FLOORBOX->add (0, $row, $_->{face_widget}); 970 0, $row, $_->{face_widget},
755 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 971 1, $row, $_->{desc_widget};
756 972
757 $row++; 973 $row++;
758 } else { 974 } else {
759 $::FLOORBOX->add (1, $row, new CFPlus::UI::Button 975 push @add, 1, $row, new CFPlus::UI::Button
760 text => "More...", 976 text => "More...",
761 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, 977 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
762 ); 978 ;
763 last; 979 last;
764 } 980 }
765 } 981 }
766 if ($::CONN->{open_container}) { 982 if ($::CONN->{open_container}) {
767 $::FLOORBOX->add (1, $row++, new CFPlus::UI::Button 983 push @add, 1, $row++, new CFPlus::UI::Button
768 text => "Close container", 984 text => "Close container",
769 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") } 985 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
770 ); 986 ;
771 } 987 }
988
989 $::FLOORBOX->add_at (@add);
772 }); 990 });
773 991
774 $::WANT_REFRESH++; 992 $::WANT_REFRESH++;
775} 993}
776 994
887 $::SERVER_INFO->set_markup ( 1105 $::SERVER_INFO->set_markup (
888 "server <tt>$self->{host}:$self->{port}</tt>\n" 1106 "server <tt>$self->{host}:$self->{port}</tt>\n"
889 . "protocol version <tt>$self->{version}</tt>\n" 1107 . "protocol version <tt>$self->{version}</tt>\n"
890 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1108 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
891 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 1109 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1110 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
892 . "editing support $yesno[!!$self->{editor_support}]\n" 1111 . "editing support $yesno[!!$self->{editor_support}]\n"
893 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n" 1112 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1113 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
894 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1114 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
895 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1115 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
896 . "map size $self->{mapw}×$self->{maph}\n" 1116 . "map size $self->{mapw}×$self->{maph}\n"
897 ); 1117 );
898 1118
899 ::setup_build_button ($self->{editor_support}->{builder_ui});
900} 1119}
901 1120
902sub logged_in { 1121sub logged_in {
903 my ($self) = @_; 1122 my ($self) = @_;
904 1123
905 $self->send_ext_req (cfplus_support => version => 1, sub { 1124 $self->send_ext_req (cfplus_support => version => 2, sub {
1125 my (%msg) = @_;
1126
906 $self->{cfplus_ext} = $_[0]{version}; 1127 $self->{cfplus_ext} = $msg{version};
907 $self->update_server_info; 1128 $self->update_server_info;
908 1129
909 if ($self->{cfplus_ext} >= 2) { 1130 if ($self->{cfplus_ext} >= 2) {
910 $self->send_ext_req ("editor_support", sub { 1131 $self->send_ext_req ("editor_support", sub {
911 $self->{editor_support} = $_[0]; 1132 $self->{editor_support} = { @_ };
912 $self->update_server_info; 1133 $self->update_server_info;
913 1134
914 0 1135 0
915 }); 1136 });
916 } 1137 }
924 $self->send_command ("output-count $::CFG->{output_count}"); 1145 $self->send_command ("output-count $::CFG->{output_count}");
925 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0; 1146 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
926 $self->send_command ("pickup $::CFG->{pickup}"); 1147 $self->send_command ("pickup $::CFG->{pickup}");
927} 1148}
928 1149
929sub buildat {
930 my ($self, $builditem, $x, $y) = @_;
931
932 if ($self->{cfplus_ext}) {
933 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
934 }
935}
936
937sub lookat { 1150sub lookat {
938 my ($self, $x, $y) = @_; 1151 my ($self, $x, $y) = @_;
939 1152
940 if ($self->{cfplus_ext}) { 1153 if ($self->{cfplus_ext}) {
941 $self->send_ext_req (lookat => dx => $x, dy => $y, sub { 1154 $self->send_ext_req (lookat => $x, $y, sub {
942 my ($msg) = @_; 1155 my (%msg) = @_;
943 1156
944 if (exists $msg->{npc_dialog}) { 1157 if (exists $msg{npc_dialog}) {
945 # start npc chat dialog 1158 # start npc chat dialog
946 $self->{npc_dialog} = new CFPlus::NPCDialog:: 1159 $self->{npc_dialog} = new CFPlus::NPCDialog::
947 dx => $x, 1160 token => $msg{npc_dialog},
948 dy => $y,
949 title => "$msg->{npc_dialog} (NPC)", 1161 title => "$msg{npc_dialog}[0] (NPC)",
950 conn => $self, 1162 conn => $self,
951 ; 1163 ;
952 } 1164 }
953 }); 1165 });
954 } 1166 }
961 1173
962 (delete $self->{npc_dialog})->destroy 1174 (delete $self->{npc_dialog})->destroy
963 if $self->{npc_dialog}; 1175 if $self->{npc_dialog};
964 1176
965 $self->SUPER::destroy; 1177 $self->SUPER::destroy;
1178
1179 %$self = ();
966} 1180}
967 1181
968package CFPlus::NPCDialog; 1182package CFPlus::NPCDialog;
969 1183
970our @ISA = 'CFPlus::UI::Toplevel'; 1184our @ISA = 'CFPlus::UI::Toplevel';
1017 on_activate => sub { $this->destroy; 1 }, 1231 on_activate => sub { $this->destroy; 1 },
1018 ; 1232 ;
1019 1233
1020 $self->update_options; 1234 $self->update_options;
1021 1235
1022 $self->{id} = $self->{conn}->send_ext_req ( 1236 $self->{id} = "npc-channel-" . $self->{conn}->token;
1023 npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy}, 1237 $self->{conn}->connect_ext ($self->{id} => sub {
1024 sub { $this && $this->feed (@_) } 1238 $this->feed (@_) if $this;
1025 ); 1239 });
1240
1241 $self->{conn}->send_ext_msg (npc_dialog_begin => $self->{id}, $self->{token});
1026 1242
1027 $self->{entry}->grab_focus; 1243 $self->{entry}->grab_focus;
1028 1244
1029 $self->{textview}->add_paragraph ({ 1245 $self->{textview}->add_paragraph ({
1030 fg => [1, 1, 0, 1], 1246 fg => [1, 1, 0, 1],
1053 ); 1269 );
1054 } 1270 }
1055} 1271}
1056 1272
1057sub feed { 1273sub feed {
1058 my ($self, $msg) = @_; 1274 my ($self, $type, @arg) = @_;
1059 1275
1060 CFPlus::weaken $self; 1276 CFPlus::weaken $self;
1061 1277
1062 if ($msg->{msgtype} eq "reply") { 1278 if ($type eq "update") {
1279 my (%info) = @arg;
1280
1063 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1281 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
1064 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1282 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
1065
1066 my $text = "\n" . CFPlus::asxml $msg->{msg};
1067 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1068 my @link;
1069 $text =~ s{
1070 ($match)
1071 }{
1072 my $kw = $1;
1073
1074 push @link, new CFPlus::UI::Label
1075 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1076 can_hover => 1,
1077 can_events => 1,
1078 padding_x => 0,
1079 padding_y => 0,
1080 on_button_up => sub {
1081 $self->send ($kw);
1082 };
1083
1084 "\x{fffc}"
1085 }giex;
1086 1283
1284 if (exists $info{msg}) {
1285 my $text = "\n" . CFPlus::Protocol::sanitise_xml $info{msg};
1286 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1287 my @link;
1288 $text =~ s{
1289 ($match)
1290 }{
1291 my $kw = $1;
1292
1293 push @link, new CFPlus::UI::Label
1294 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1295 can_hover => 1,
1296 can_events => 1,
1297 padding_x => 0,
1298 padding_y => 0,
1299 on_button_up => sub {
1300 $self->send ($kw);
1301 };
1302
1303 "\x{fffc}"
1304 }giex;
1305
1087 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); 1306 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1088 $self->{textview}->scroll_to_bottom; 1307 $self->{textview}->scroll_to_bottom;
1308 }
1309
1089 $self->update_options; 1310 $self->update_options;
1090 } else { 1311 } else {
1091 $self->destroy; 1312 $self->destroy;
1092 } 1313 }
1093 1314
1098 my ($self, $msg) = @_; 1319 my ($self, $msg) = @_;
1099 1320
1100 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg }); 1321 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
1101 $self->{textview}->scroll_to_bottom; 1322 $self->{textview}->scroll_to_bottom;
1102 1323
1103 $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg); 1324 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
1104} 1325}
1105 1326
1106sub destroy { 1327sub destroy {
1107 my ($self) = @_; 1328 my ($self) = @_;
1108 1329
1109 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1330 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1110 1331
1111 if ($self->{conn}) { 1332 if ($self->{conn}) {
1112 $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id}; 1333 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
1113 delete $self->{conn}{npc_dialog}; 1334 delete $self->{conn}{npc_dialog};
1114 $self->{conn}->disconnect_ext ($self->{id}); 1335 $self->{conn}->disconnect_ext ($self->{id});
1115 } 1336 }
1116 1337
1117 $self->SUPER::destroy; 1338 $self->SUPER::destroy;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines