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.65 by root, Sun Jul 23 16:11:12 2006 UTC vs.
Revision 1.119 by root, Sat Jul 14 12:05:53 2007 UTC

1package CFClient::Protocol; 1package CFPlus::Protocol;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Crossfire::Protocol::Constants; 6use Crossfire::Protocol::Constants;
7 7
8use CFClient; 8use CFPlus;
9use CFPlus::DB;
9use CFClient::UI; 10use CFPlus::UI;
10use CFClient::Pod; 11use CFPlus::Pod;
12use CFPlus::Macro;
13use CFPlus::Item;
14
15use Crossfire::Protocol::Base 0.95;
11 16
12use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
13 18
14sub new { 19sub new {
15 my $class = shift; 20 my ($class, %arg) = @_;
16 21
17 my $self = $class->SUPER::new (@_); 22 my $self = $class->SUPER::new (%arg,
23 setup_req => {
24 extmap => 1,
25 excmd => 1,
26 ywidget => 1,
27 %{$arg{setup_req} || {}},
28 },
29 );
18 30
19 $self->{map_widget}->clr_commands; 31 $self->{map_widget}->clr_commands;
20 32
21 my $cmd_help = CFClient::Pod::load CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub { 33 my @cmd_help = map {
22 my ($pom) = @_;
23
24 my @cmd_help;
25
26 for my $head2 ($pom->head1->[-2]->head2) {
27 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 34 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
28 or next; 35 or die "unparseable command help: $_->{kw}[0]";
29 36
30 my $cmd = $1; 37 my $cmd = $1;
31 my @args = split /\|/, $2; 38 my @args = split /\|/, $2;
32 @args = (".*") unless @args; 39 @args = (".*") unless @args;
33 40
41 my (undef, @par) = CFPlus::Pod::section_of $_;
42 my $text = CFPlus::Pod::as_label @par;
43
34 $_ = $_ eq ".*" ? "" : " $_" 44 $_ = $_ eq ".*" ? "" : " $_"
35 for @args; 45 for @args;
36 46
37 my $text = CFClient::Pod::as_xml $head2->content; 47 map ["$cmd$_", $text],
38
39 push @cmd_help, ["$cmd$_", $text]
40 for sort { (length $a) <=> (length $b) } 48 sort { (length $a) <=> (length $b) }
49 @args
50 } sort { $a->{par} <=> $b->{par} }
51 CFPlus::Pod::find command => "*";
52
53 $self->connect_ext (event_music => sub {
54 my ($ev) = @_;
55
56 return unless $::CFG->{bgm_enable};
57
58 my $faces = $ev->{faces};
59 my @songs;
60
61 # request music from server if appropriate
62 if (my $faces = $ev->{faces}) {
63 for (@$faces) {
64 my ($face, $pri, $chksum) = @$_;
65 utf8::downgrade $chksum;
66
67 $chksum = unpack "H*", $chksum;
68 $self->{music_map}{$face} = $chksum;
69
70 # check if already cached
71 CFPlus::DB::get "res-metadata" => $chksum, sub {
72 my ($meta_json) = @_;
73
74 if (defined $meta_json) {
75 my $meta = $self->{json_coder}->decode ($meta_json);
76 $meta->{path} = CFPlus::DB::path_of_res $chksum;
77 $self->{music_meta}{$chksum} = $meta;
78 } else {
79 ::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
80 $self->ask_face ($face, -100 - $pri, undef, sub {
81 my $len = length $_[1];
82 my ($meta_json, $data) = unpack "(w/a*)*", $_[1];
83
84 my $meta = $self->{json_coder}->decode ($meta_json);
85 $meta->{path} = CFPlus::DB::path_of_res $chksum;
86 $self->{music_meta}{$chksum} = $meta;
87
88 CFPlus::DB::write_file $meta->{path}, $data, sub { };
89 CFPlus::DB::put "res-metadata" => $chksum => $meta_json, sub { };
90
91 ::message ({ markup => "downloaded song #$face, size $len octets" });
41 @args; 92 });
93 }
94
95 &::audio_music_set ($self->{songs});
96 }
97 }
98 }
99
100 if (my $play = $ev->{play}) {
101 &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]);
102 }
103 });
104
105 $self->connect_ext (event_capabilities => sub {
106 my ($cap) = @_;
107
108 if (my $ts = $cap->{tileset}) {
109 if (my ($default) = grep $_->[2] & 1, @$ts) {
110 $self->{tileset} = $default;
111 $self->{tilesize} = $default->[3];
112 $self->setup_req (tileset => $default->[0]);
113
114 my $w = int $self->{mapw} * 32 / $self->{tilesize};
115 my $h = int $self->{maph} * 32 / $self->{tilesize};
116
117 $self->setup_req (mapsize => "${w}x${h}");
118 }
119 }
120 });
121
122 $self->{json_coder}
123 ->convert_blessed
124 ->filter_json_single_key_object (__widget_ref__ => sub {
125 $self->{widget}{$_[0]}
126 });
127
128 $self->connect_ext (ws_n => sub {
129 my ($arg) = @_;
130
131 $self->{widgetset}{$arg{id}} = {
132 w => {},
42 } 133 };
43
44 \@cmd_help
45 }; 134 });
135
136 $self->connect_ext (ws_d => sub {
137 my ($arg) = @_;
138
139 my $ws = delete $self->{widgetset}{$arg{id}}
140 or return;
141
142 $_->destroy
143 for values %{$ws->{w}};
144 });
145
146 $self->connect_ext (ws_c => sub {
147 my ($arg) = @_;
148
149 my $args = $arg->{args} || {};
150
151 for my $ev (grep /^on_/, keys %$args) {
152 $args->{$ev} = sub {
153 my $id = shift->{s_id};
154 $self->send_exti_msg (w_e => id => $id, name => $ev, args => \@_);
155
156 1
157 };
158 }
159
160 if (my $widget = eval {
161 local $SIG{__DIE__};
162 "CFPlus::UI::$arg->{class}"->new (
163 %$args,
164 s_ws => $arg->{ws},
165 s_id => $arg->{id},
166 )
167 }
168 ) {
169 $self->{widget}{$arg->{id}}
170 = $self->{widgetset}{$arg->{ws}}{w}{$arg->{id}}
171 = $widget;
172
173 $widget->connect (on_destroy => sub {
174 my ($widget) = @_;
175
176 delete $self->{widget}{$widget->{s_id}};
177 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}};
178 });
179 } else {
180 warn "server failed creating client-side widget " . (CFPlus::to_json $arg) . ": $@\n";
181 $self->send_exti_msg (w_e => id => $arg->{id}, name => "destroy");
182 }
183 });
184
185 $self->connect_ext (w_c => sub {
186 my ($arg) = @_;
187
188 my $w = $self->{widget}{$arg->{id}}
189 or return;
190 my $m = $arg->{name};
191
192 my $a = $arg->{args} || [];
193
194 if (exists $arg->{rid}) {
195 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@$a)]);
196 } else {
197 $w->$m (@$a);
198 }
199 });
200
201 $self->connect_ext (w_s => sub {
202 my ($arg) = @_;
203
204 my $w = $self->{widget}{$arg->{id}}
205 or return;
206
207 $w->{$arg->{name}} = $arg->{value};
208 });
209
210 $self->connect_ext (w_g => sub {
211 my ($arg) = @_;
212
213 my $w = $self->{widget}{$arg->{id}}
214 or return;
215
216 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->{$arg->{name}}]);
217 });
218
219 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
220 for my $ws (values %{delete $self->{widgetset} || {}}) {
221 $_->destroy
222 for values %{delete $ws->{w} || {}};
223 }
224 });
46 225
47 $self->{map_widget}->add_command (@$_) 226 $self->{map_widget}->add_command (@$_)
48 for @$cmd_help; 227 for @cmd_help;
49 228
229 {
230 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
231 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
232 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
233 }
234
235 {
50 $self->{noface} = new_from_file CFClient::Texture 236 $self->{noface} = my $tex = new_from_file CFPlus::Texture
51 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; 237 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
238 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
239 }
52 240
53 $self->{open_container} = 0; 241 $self->{open_container} = 0;
54 242
55 # "global"
56 $self->{tilecache} = CFClient::db_table "tilecache";
57 $self->{facemap} = CFClient::db_table "facemap";
58
59 # per server 243 # per server
60 $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}"; 244 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
61 245
62 $self 246 $self
63} 247}
64 248
65sub logprint { 249sub logprint {
222 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 406 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
223 $::GAUGES->{food} ->set_value ($fo, $fo_m); 407 $::GAUGES->{food} ->set_value ($fo, $fo_m);
224 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 408 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
225 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 409 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
226 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 410 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
227 my $rng = $stats->{+CS_STAT_RANGE}; 411 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
228 $rng =~ s/^Range: //; # thank you so much dear server
229 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
230 my $title = $stats->{+CS_STAT_TITLE}; 412 my $title = $stats->{+CS_STAT_TITLE};
231 $title =~ s/^Player: //; 413 $title =~ s/^Player: //;
232 $::STATWIDS->{title} ->set_text ("Title: " . $title); 414 $::STATWIDS->{title} ->set_text ("Title: " . $title);
233 415
234 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR}); 416 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
254 my @skills = keys %{ $self->{skill_info} }; 436 my @skills = keys %{ $self->{skill_info} };
255 437
256 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) { 438 if (grep +(exists $stats->{$_}) != (exists $prev->{$_}), @skills) {
257 $sktbl->clear; 439 $sktbl->clear;
258 440
259 $sktbl->add (0, 0, new CFClient::UI::Label text => "Experience", align => 1); 441 $sktbl->add (0, 0, new CFPlus::UI::Label text => "Experience", align => 1);
260 $sktbl->add (1, 0, new CFClient::UI::Label text => "Lvl.", align => 1); 442 $sktbl->add (1, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
261 $sktbl->add (2, 0, new CFClient::UI::Label text => "Skill", expand => 1); 443 $sktbl->add (2, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
262 $sktbl->add (3, 0, new CFClient::UI::Label text => "Experience", align => 1); 444 $sktbl->add (3, 0, new CFPlus::UI::Label text => "Experience", align => 1);
263 $sktbl->add (4, 0, new CFClient::UI::Label text => "Lvl.", align => 1); 445 $sktbl->add (4, 0, new CFPlus::UI::Label text => "Lvl.", align => 1);
264 $sktbl->add (5, 0, new CFClient::UI::Label text => "Skill", expand => 1); 446 $sktbl->add (5, 0, new CFPlus::UI::Label text => "Skill", expand => 1);
447
448 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
449
450 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
451 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
265 452
266 my ($x, $y) = (0, 1); 453 my ($x, $y) = (0, 1);
267 for ( 454 for (
268 sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] } 455 sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] }
269 map [$_, $self->{skill_info}{$_}], 456 map [$_, $self->{skill_info}{$_}],
270 grep exists $stats->{$_}, 457 grep exists $stats->{$_},
271 @skills 458 @skills
272 ) { 459 ) {
273 my ($idx, $name) = @$_; 460 my ($idx, $name) = @$_;
274 461
462 my $spell_cb = sub {
463 my ($widget, $ev) = @_;
464
465 if ($ev->{button} == 1) {
466 $::CONN->user_send ("ready_skill $name");
467 } elsif ($ev->{button} == 2) {
468 $::CONN->user_send ("use_skill $name");
469 } elsif ($ev->{button} == 3) {
470 my $shortname = CFPlus::shorten $name, 14;
471 (new CFPlus::UI::Menu
472 items => [
473 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
474 ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
475 ],
476 )->popup ($ev);
477 } else {
478 return 0;
479 }
480
481 1
482 };
483
275 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFClient::UI::Label 484 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
276 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0]); 485 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP);
277 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFClient::UI::Label 486 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
278 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4); 487 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL);
279 $sktbl->add ($x * 3 + 2, $y, new CFClient::UI::Label text => $name); 488 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
489 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL);
280 490
281 $x++ and ($x, $y) = (0, $y + 1); 491 $x++ and ($x, $y) = (0, $y + 1);
282 } 492 }
283 } 493 }
284 494
286 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 496 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
287 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 497 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
288 } 498 }
289} 499}
290 500
501sub macro_send {
502 my ($self, $macro) = @_;
503
504 for my $cmd (@{ $macro->{action} }) {
505 $self->send_command ($cmd);
506 }
507}
508
291sub user_send { 509sub user_send {
292 my ($self, $command) = @_; 510 my ($self, $command) = @_;
293 511
512 $self->{record}->($command)
294 if ($self->{record}) { 513 if $self->{record};
295 push @{$self->{record}}, $command;
296 }
297 514
298 $self->logprint ("send: ", $command); 515 $self->logprint ("send: ", $command);
299 $self->send_command ($command); 516 $self->send_command ($command);
300 ::status ($command); 517 ::status ($command);
301} 518}
302 519
303sub start_record { 520sub record {
304 my ($self) = @_; 521 my ($self, $cb) = @_;
305 522
306 $self->{record} = []; 523 $self->{record} = $cb;
307}
308
309sub stop_record {
310 my ($self) = @_;
311 return delete $self->{record};
312} 524}
313 525
314sub map_scroll { 526sub map_scroll {
315 my ($self, $dx, $dy) = @_; 527 my ($self, $dx, $dy) = @_;
316 528
318} 530}
319 531
320sub feed_map1a { 532sub feed_map1a {
321 my ($self, $data) = @_; 533 my ($self, $data) = @_;
322 534
323 $self->{map}->map1a_update ($data); 535 $self->{map}->map1a_update ($data, $self->{setup}{extmap});
324 $self->{map_widget}->update; 536 $self->{map_widget}->update;
325} 537}
326 538
327sub magicmap { 539sub magicmap {
328 my ($self, $w, $h, $x, $y, $data) = @_; 540 my ($self, $w, $h, $x, $y, $data) = @_;
337 or return; 549 or return;
338 550
339 my ($hash, $x, $y, $w, $h) = @$map_info; 551 my ($hash, $x, $y, $w, $h) = @$map_info;
340 552
341 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 553 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
342 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 554 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
343 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 555 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
344} 556}
345 557
346sub map_clear { 558sub map_clear {
347 my ($self) = @_; 559 my ($self) = @_;
351 563
352 $self->{map}->clear; 564 $self->{map}->clear;
353 delete $self->{map_widget}{magicmap}; 565 delete $self->{map_widget}{magicmap};
354} 566}
355 567
568sub bg_fetch {
569 my ($self) = @_;
570
571 my $id;
572
573 do {
574 $id = pop @{$self->{bg_fetch}}
575 or return;
576 } while $self->{texture}[$id];
577
578 CFPlus::DB::get tilecache => $id, sub {
579 my ($data) = @_;
580
581 return unless $self->{map}; # stop when destroyed
582
583 $self->set_texture ($id => $data)
584 if defined $data;
585
586 $self->bg_fetch;
587 };
588}
356 589
357sub load_map($$$) { 590sub load_map($$$) {
358 my ($self, $hash, $x, $y) = @_; 591 my ($self, $hash, $x, $y) = @_;
359 592
360 if (defined (my $data = $self->{mapcache}->get ($hash))) { 593 my $gen = $self->{map_change_gen};
594
595 CFPlus::DB::get $self->{mapcache} => $hash, sub {
596 return unless $gen == $self->{map_change_gen};
597
598 my ($data) = @_;
599
600 if (defined $data) {
361 $data = Compress::LZF::decompress $data; 601 $data = Compress::LZF::decompress $data;
362 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 602 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
363 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
364 my $data = $self->{tilecache}->get ($id)
365 or next;
366 603
367 $self->set_texture ($id => $data); 604 my $inprogress = @{ $self->{bg_fetch} || [] };
605 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
606 $self->bg_fetch unless $inprogress;
368 } 607 }
369 } 608 };
370} 609}
371 610
372# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 611# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
373# (server resource,s latency, bandwidth), so this hack is warranted. 612# (server resource,s latency, bandwidth), so this hack is warranted.
374# the right fix is to make real tiled maps with an overview file 613# the right fix is to make real tiled maps with an overview file
438 677
439 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 678 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
440 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 679 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
441 680
442 } else { 681 } else {
682 my $gen = $self->{map_change_gen};
443 $self->send_mapinfo ("spatial $path$tile", sub { 683 $self->send_mapinfo ("spatial $path$tile", sub {
684 return unless $gen == $self->{map_change_gen};
685
444 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 686 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
445 687
446 return if $mode ne "spatial"; 688 return if $mode ne "spatial";
447 689
448 $x += $self->{map}->ox; 690 $x += $self->{map}->ox;
462 704
463sub map_change { 705sub map_change {
464 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 706 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
465 707
466 $self->flush_map; 708 $self->flush_map;
709
710 ++$self->{map_change_gen};
467 711
468 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 712 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
469 713
470 my $mapmapw = $self->{mapmap}->{w}; 714 my $mapmapw = $self->{mapmap}->{w};
471 my $mapmaph = $self->{mapmap}->{h}; 715 my $mapmaph = $self->{mapmap}->{h};
488 $self->load_map ($hash, $x, $y); 732 $self->load_map ($hash, $x, $y);
489 $self->flood_fill (0, 0, 0, "", $hash, $flags); 733 $self->flood_fill (0, 0, 0, "", $hash, $flags);
490} 734}
491 735
492sub face_find { 736sub face_find {
493 my ($self, $facenum, $face) = @_; 737 my ($self, $facenum, $face, $cb) = @_;
494 738
495 my $hash = "$face->{chksum},$face->{name}"; 739 my $hash = "$face->{chksum},$face->{name}";
496 740
497 my $id = $self->{facemap}->get ($hash); 741 my $id = CFPlus::DB::get_tile_id_sync $hash;
498 742
499 unless ($id) {
500 # create new id for face
501 # I love transactions
502 for (1..100) {
503 my $txn = $CFClient::DB_ENV->txn_begin;
504 my $status = $self->{facemap}->db_get (id => $id);
505 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
506 $id = ($id || 64) + 1;
507 if ($self->{facemap}->put (id => $id) == 0
508 && $self->{facemap}->put ($hash => $id) == 0) {
509 $txn->txn_commit;
510
511 goto gotid;
512 }
513 }
514 $txn->txn_abort;
515 }
516
517 CFClient::fatal "maximum number of transaction retries reached - database problems?";
518 }
519
520gotid:
521 $face->{id} = $id; 743 $face->{id} = $id;
522 $self->{map}->set_face ($facenum => $id);
523 $self->{faceid}[$facenum] = $id;#d# 744 $self->{faceid}[$facenum] = $id;
524 745
525 my $face = $self->{tilecache}->get ($id); 746 $self->{map}->set_tileid ($facenum => $id);
526 747
527 if ($face) { 748 CFPlus::DB::get tilecache => $id, $cb;
528 #$self->face_prefetch;
529 $face
530 } else {
531 my $tex = $self->{noface};
532 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
533 undef
534 };
535} 749}
536 750
537sub face_update { 751sub face_update {
538 my ($self, $facenum, $face, $changed) = @_; 752 my ($self, $facenum, $face, $changed) = @_;
539 753
540 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 754 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
755 if $changed;
541 756
542 $self->set_texture ($face->{id} => delete $face->{image}); 757 $self->set_texture ($face->{id} => delete $face->{image});
758}
759
760sub smooth_update {
761 my ($self, $facenum, $face) = @_;
762
763 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
543} 764}
544 765
545sub set_texture { 766sub set_texture {
546 my ($self, $id, $data) = @_; 767 my ($self, $id, $data) = @_;
547 768
769 return unless $self->{map};
770
548 $self->{texture}[$id] ||= do { 771 $self->{texture}[$id] = my $tex =
549 my $tex =
550 new_from_image CFClient::Texture 772 new_from_image CFPlus::Texture
551 $data, minify => 1, mipmap => 1; 773 $data, minify => 1, mipmap => 1;
552 774
553 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 775 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
554 $self->{map_widget}->update; 776 $self->{map_widget}->update;
555
556 $tex
557 };
558} 777}
559 778
560sub sound_play { 779sub sound_play {
561 my ($self, $x, $y, $soundnum, $type) = @_; 780 my ($self, $x, $y, $soundnum, $type) = @_;
562 781
572 $LAST_QUERY = $prompt; 791 $LAST_QUERY = $prompt;
573 792
574 $self->{query}-> ($self, $flags, $prompt); 793 $self->{query}-> ($self, $flags, $prompt);
575} 794}
576 795
577sub drawinfo { 796sub sanitise_xml($) {
578 my ($self, $color, $text) = @_; 797 local $_ = shift;
579 798
580 my @color = ( 799 # we now weed out all tags we do not support
800 s%<(?!/?i>|/?u>|/?b>|fg |/fg>)%&lt;%g;
801 # now all entities
802 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
803
804 # handle some elements
805 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
806 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
807
808 $_
809}
810
811our %NAME_TO_COLOR = (
812 black => 0,
813 white => 1,
814 darkblue => 2,
815 red => 3,
816 orange => 4,
817 lightblue => 5,
818 darkorange => 6,
819 green => 7,
820 darkgreen => 8,
821 grey => 9,
822 brown => 10,
823 yellow => 11,
824 tan => 12,
825);
826
827our @CF_COLOR = (
581 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 828 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
582 [1.00, 1.00, 1.00], 829 [1.00, 1.00, 1.00],
583 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 830 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
584 [1.00, 0.00, 0.00], 831 [1.00, 0.00, 0.00],
585 [1.00, 0.54, 0.00], 832 [1.00, 0.54, 0.00],
586 [0.11, 0.56, 1.00], 833 [0.11, 0.56, 1.00],
587 [0.93, 0.46, 0.00], 834 [0.93, 0.46, 0.00],
588 [0.18, 0.54, 0.34], 835 [0.18, 0.54, 0.34],
589 [0.56, 0.73, 0.56], 836 [0.56, 0.73, 0.56],
590 [0.80, 0.80, 0.80], 837 [0.80, 0.80, 0.80],
591 [0.75, 0.61, 0.20], 838 [0.75, 0.61, 0.20],
592 [0.99, 0.77, 0.26], 839 [0.99, 0.77, 0.26],
593 [0.74, 0.65, 0.41], 840 [0.74, 0.65, 0.41],
594 ); 841);
595 842
843sub msg {
844 my ($self, $color, $type, $text, @extra) = @_;
845
846 $text = sanitise_xml $text;
847
848 if (my $cb = $self->{cb_msg}{$type}) {
849 $_->($self, $color, $type, $text, @extra) for values %$cb;
850 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
851 $type =~ s/-/_/g;
852 $self->{$type} = $text;
853 } else {
596 $self->logprint ("info: ", $text); 854 $self->logprint ("msg: ", $text);
855 return if $color < 0; # negative color == ignore if not understood
597 856
598 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 857 my $fg = $CF_COLOR[$color % @CF_COLOR];
599 858
600 # try to create single paragraphs of multiple lines sent by the server 859 ## try to create single paragraphs of multiple lines sent by the server
860 # no longer neecssary with TRT servers
601 $text =~ s/(?<=\S)\n(?=\w)/ /g; 861 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
602 862
603 $text = CFClient::asxml $text; 863 ::message ({ fg => $fg, markup => $_ })
604 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 864 for split /\n/, $text;
605 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
606 865
607 $self->{logview}->add_paragraph ($color[$color], $_)
608 for map "<span foreground='#ffffff'>$time</span> $_", split /\n/, $text;
609 $self->{logview}->scroll_to_bottom;
610
611 $self->{statusbox}->add ($text, 866 $self->{statusbox}->add ($text,
612 group => $text, 867 group => $text,
613 fg => $color[$color], 868 fg => $fg,
614 timeout => $color >= 2 ? 60 : 10, 869 timeout => $color >= 2 ? 180 : 10,
615 tooltip_font => $::FONT_FIXED, 870 tooltip_font => $::FONT_FIXED,
616 ); 871 );
617} 872 }
618
619sub drawextinfo {
620 my ($self, $color, $type, $subtype, $message) = @_;
621
622 $self->drawinfo ($color, $message);
623} 873}
624 874
625sub spell_add { 875sub spell_add {
626 my ($self, $spell) = @_; 876 my ($self, $spell) = @_;
627 877
628 # try to create single paragraphs of multiple lines sent by the server 878 # try to create single paragraphs out of the multiple lines sent by the server
629 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 879 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
630 $spell->{message} =~ s/\n+$//; 880 $spell->{message} =~ s/\n+$//;
631 $spell->{message} ||= "Server did not provide a description for this spell."; 881 $spell->{message} ||= "Server did not provide a description for this spell.";
632 882
633 $::SPELL_PAGE->add_spell ($spell); 883 $::SPELL_LIST->add_spell ($spell);
634 884
635 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::asxml $spell->{message}); 885 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message});
636 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::asxml $spell->{message}); 886 $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message});
637} 887}
638 888
639sub spell_delete { 889sub spell_delete {
640 my ($self, $spell) = @_; 890 my ($self, $spell) = @_;
641 891
642 $::SPELL_PAGE->remove_spell ($spell); 892 $::SPELL_LIST->remove_spell ($spell);
893}
894
895sub setup {
896 my ($self, $setup) = @_;
897
898 $self->{map_widget}->set_tilesize ($self->{tilesize});
899 $::MAP->resize ($self->{mapw}, $self->{maph});
643} 900}
644 901
645sub addme_success { 902sub addme_success {
646 my ($self) = @_; 903 my ($self) = @_;
647 904
648 my $skill_help = CFClient::Pod::load CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
649 my ($pom) = @_;
650
651 my %skill_help; 905 my %skill_help;
652 906
653 for my $head2 ($pom->head1->[3]->head2) { 907 for my $node (CFPlus::Pod::find skill_description => "*") {
654 $skill_help{$head2->title} = CFClient::Pod::as_xml $head2->content; 908 my (undef, @par) = CFPlus::Pod::section_of $node;
655 } 909 $skill_help{$node->{kw}[0]} = CFPlus::Pod::as_label @par;
656
657 \%skill_help
658 }; 910 };
659 911
660 for my $skill (values %{$self->{skill_info}}) { 912 for my $skill (values %{$self->{skill_info}}) {
661 $self->{map_widget}->add_command ("ready_skill $skill", 913 $self->{map_widget}->add_command ("ready_skill $skill",
662 (CFClient::asxml "Ready the skill '$skill'\n\n") 914 (CFPlus::asxml "Ready the skill '$skill'\n\n")
663 . $skill_help->{$skill}); 915 . $skill_help{$skill});
664 $self->{map_widget}->add_command ("use_skill $skill", 916 $self->{map_widget}->add_command ("use_skill $skill",
665 (CFClient::asxml "Immediately use the skill '$skill'\n\n") 917 (CFPlus::asxml "Immediately use the skill '$skill'\n\n")
666 . $skill_help->{$skill}); 918 . $skill_help{$skill});
667 } 919 }
668} 920}
669 921
670sub eof { 922sub eof {
671 my ($self) = @_; 923 my ($self) = @_;
695 947
696 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); 948 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
697 949
698 $self->send ("requestinfo image_sums $face $face"); 950 $self->send ("requestinfo image_sums $face $face");
699 951
700 $self->{statusbox}->add (CFClient::asxml "prefetching $todo", 952 $self->{statusbox}->add (CFPlus::asxml "prefetching $todo",
701 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); 953 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
702 } elsif (!exists $self->{num_faces}) { 954 } elsif (!exists $self->{num_faces}) {
703 $self->send ("requestinfo image_info"); 955 $self->send ("requestinfo image_info");
704 956
705 $self->{num_faces} = 0; 957 $self->{num_faces} = 0;
706 958
707 $self->{statusbox}->add (CFClient::asxml "starting to prefetch", 959 $self->{statusbox}->add (CFPlus::asxml "starting to prefetch",
708 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); 960 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
709 } 961 }
710} 962}
711 963
712sub update_floorbox { 964sub update_floorbox {
713 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 965 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
714 return unless $::CONN; 966 return unless $::CONN;
715 967
716 $::FLOORBOX->clear; 968 $::FLOORBOX->clear;
717 969
718 my $row; 970 my $row;
719 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 971 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
720 if ($row < 6) { 972 if ($row < 6) {
721 local $_->{face_widget}; # hack to force recreation of widget 973 local $_->{face_widget}; # hack to force recreation of widget
722 local $_->{desc_widget}; # hack to force recreation of widget 974 local $_->{desc_widget}; # hack to force recreation of widget
723 CFClient::Item::update_widgets $_; 975 CFPlus::Item::update_widgets $_;
724 976
725 $::FLOORBOX->add (0, $row, $_->{face_widget}); 977 $::FLOORBOX->add (0, $row, $_->{face_widget});
726 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 978 $::FLOORBOX->add (1, $row, $_->{desc_widget});
727 979
728 $row++; 980 $row++;
729 } else { 981 } else {
730 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 982 $::FLOORBOX->add (1, $row, new CFPlus::UI::Button
731 text => "More...", 983 text => "More...",
732 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, 984 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
733 ); 985 );
734 last; 986 last;
735 } 987 }
736 } 988 }
737 if ($::CONN->{open_container}) { 989 if ($::CONN->{open_container}) {
738 $::FLOORBOX->add (1, $row++, new CFClient::UI::Button 990 $::FLOORBOX->add (1, $row++, new CFPlus::UI::Button
739 text => "Close container", 991 text => "Close container",
740 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") } 992 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
741 ); 993 );
742 } 994 }
743 }); 995 });
749 my ($conn, $tag, $name) = @_; 1001 my ($conn, $tag, $name) = @_;
750 $conn->{open_container} = $tag; 1002 $conn->{open_container} = $tag;
751 update_floorbox; 1003 update_floorbox;
752 1004
753 $::INV_RIGHT_HB->clear (); 1005 $::INV_RIGHT_HB->clear ();
754 $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name); 1006 $::INV_RIGHT_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name);
755 1007
756 if ($tag != 0) { # Floor isn't closable, is it? 1008 if ($tag != 0) { # Floor isn't closable, is it?
757 $::INV_RIGHT_HB->add (new CFClient::UI::Button 1009 $::INV_RIGHT_HB->add (new CFPlus::UI::Button
758 text => "Close container", 1010 text => "Close container",
759 tooltip => "Close the currently open container (if one is open)", 1011 tooltip => "Close the currently open container (if one is open)",
760 on_activate => sub { 1012 on_activate => sub {
761 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 1013 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
762 if $tag != 0; 1014 if $tag != 0;
770} 1022}
771 1023
772sub update_containers { 1024sub update_containers {
773 my ($self) = @_; 1025 my ($self) = @_;
774 1026
775 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub { 1027 $CFPlus::UI::ROOT->on_refresh ("update_containers_$self" => sub {
776 my $todo = delete $self->{update_container} 1028 my $todo = delete $self->{update_container}
777 or return; 1029 or return;
778 1030
779 for my $tag (keys %$todo) { 1031 for my $tag (keys %$todo) {
780 update_floorbox if $tag == 0 or $tag == $self->{open_container}; 1032 update_floorbox if $tag == 0 or $tag == $self->{open_container};
817sub item_update { 1069sub item_update {
818 my ($self, $item) = @_; 1070 my ($self, $item) = @_;
819 1071
820 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 1072 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
821 1073
822 CFClient::Item::update_widgets $item; 1074 CFPlus::Item::update_widgets $item;
823 1075
824 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1076 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
825 set_opencont ($::CONN, 0, "Floor"); 1077 set_opencont ($::CONN, 0, "Floor");
826 1078
827 } elsif ($item->{flags} & F_OPEN) { 1079 } elsif ($item->{flags} & F_OPEN) {
828 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 1080 set_opencont ($::CONN, $item->{tag}, CFPlus::Item::desc_string $item);
829 1081
830 } else { 1082 } else {
831 $self->{update_container}{$item->{container}}++; 1083 $self->{update_container}{$item->{container}}++;
832 $self->update_containers; 1084 $self->update_containers;
833 } 1085 }
858 $::SERVER_INFO->set_markup ( 1110 $::SERVER_INFO->set_markup (
859 "server <tt>$self->{host}:$self->{port}</tt>\n" 1111 "server <tt>$self->{host}:$self->{port}</tt>\n"
860 . "protocol version <tt>$self->{version}</tt>\n" 1112 . "protocol version <tt>$self->{version}</tt>\n"
861 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1113 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
862 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 1114 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1115 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
1116 . "editing support $yesno[!!$self->{editor_support}]\n"
1117 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1118 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
863 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1119 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
864 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1120 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
865 . "map size $self->{mapw}×$self->{maph}\n" 1121 . "map size $self->{mapw}×$self->{maph}\n"
866 ); 1122 );
1123
1124 ::setup_build_button ($self->{editor_support}->{builder_ui});
867} 1125}
868 1126
869sub logged_in { 1127sub logged_in {
870 my ($self) = @_; 1128 my ($self) = @_;
871 1129
872 $self->send_ext_req (cfplus_support => "1", sub { 1130 $self->send_ext_req (cfplus_support => version => 1, sub {
873 $self->{cfplus_ext} = $_[0]; 1131 $self->{cfplus_ext} = $_[0]{version};
874 $self->update_server_info; 1132 $self->update_server_info;
1133
1134 if ($self->{cfplus_ext} >= 2) {
1135 $self->send_ext_req ("editor_support", sub {
1136 $self->{editor_support} = $_[0];
1137 $self->update_server_info;
1138
1139 0
1140 });
1141 }
1142
1143 0
875 }); 1144 });
876 1145
877 $self->update_server_info; 1146 $self->update_server_info;
878 1147
879 $self->send_command ("output-sync $::CFG->{output_sync}"); 1148 $self->send_command ("output-sync $::CFG->{output_sync}");
880 $self->send_command ("output-count $::CFG->{output_count}"); 1149 $self->send_command ("output-count $::CFG->{output_count}");
1150 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
881 $self->send_command ("pickup $::CFG->{pickup}"); 1151 $self->send_command ("pickup $::CFG->{pickup}");
1152}
1153
1154sub buildat {
1155 my ($self, $builditem, $x, $y) = @_;
1156
1157 if ($self->{cfplus_ext}) {
1158 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
1159 }
882} 1160}
883 1161
884sub lookat { 1162sub lookat {
885 my ($self, $x, $y) = @_; 1163 my ($self, $x, $y) = @_;
886 1164
887 if ($self->{cfplus_ext}) { 1165 if ($self->{cfplus_ext}) {
888 $self->send_ext_req (lookat => "$x $y", sub { 1166 $self->send_ext_req (lookat => dx => $x, dy => $y, sub {
889 my %res = split /\x00/, $_[0]; 1167 my ($msg) = @_;
890 1168
891 if (exists $res{npc_dialog}) { 1169 if (exists $msg->{npc_dialog}) {
892 # start npc chat dialog 1170 # start npc chat dialog
893 $self->{npc_dialog} = new CFClient::NPCDialog:: 1171 $self->{npc_dialog} = new CFPlus::NPCDialog::
894 dx => $x, 1172 dx => $x,
895 dy => $y, 1173 dy => $y,
896 title => "$res{npc_dialog} (NPC)", 1174 title => "$msg->{npc_dialog} (NPC)",
897 conn => $self, 1175 conn => $self,
898 ; 1176 ;
899 } 1177 }
900 }); 1178 });
901 } 1179 }
904} 1182}
905 1183
906sub destroy { 1184sub destroy {
907 my ($self) = @_; 1185 my ($self) = @_;
908 1186
909 $self->{npc_dialog}->destroy 1187 (delete $self->{npc_dialog})->destroy
910 if $self->{npc_dialog}; 1188 if $self->{npc_dialog};
911 1189
912 $self->SUPER::destroy; 1190 $self->SUPER::destroy;
913}
914 1191
1192 %$self = ();
1193}
1194
915package CFClient::NPCDialog; 1195package CFPlus::NPCDialog;
916 1196
917our @ISA = 'CFClient::UI::FancyFrame'; 1197our @ISA = 'CFPlus::UI::Toplevel';
918 1198
919sub new { 1199sub new {
920 my $class = shift; 1200 my $class = shift;
921 1201
922 my $self = $class->SUPER::new ( 1202 my $self = $class->SUPER::new (
929 kw => { hi => 0, yes => 0, no => 0 }, 1209 kw => { hi => 0, yes => 0, no => 0 },
930 has_close_button => 1, 1210 has_close_button => 1,
931 @_, 1211 @_,
932 ); 1212 );
933 1213
934 Scalar::Util::weaken (my $this = $self); 1214 CFPlus::weaken (my $this = $self);
935 1215
936 $self->connect (delete => sub { $this->destroy; 1 }); 1216 $self->connect (delete => sub { $this->destroy; 1 });
937 1217
938 # better use a pane... 1218 # better use a pane...
939 $self->add (my $hbox = new CFClient::UI::HBox); 1219 $self->add (my $hbox = new CFPlus::UI::HBox);
940 $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1); 1220 $hbox->add ($self->{textview} = new CFPlus::UI::TextScroller expand => 1);
941 1221
942 $hbox->add (my $vbox = new CFClient::UI::VBox); 1222 $hbox->add (my $vbox = new CFPlus::UI::VBox);
943 1223
944 $vbox->add (new CFClient::UI::Label text => "Message Entry:"); 1224 $vbox->add (new CFPlus::UI::Label text => "Message Entry:");
945 $vbox->add ($self->{entry} = new CFClient::UI::Entry 1225 $vbox->add ($self->{entry} = new CFPlus::UI::Entry
946 tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n" 1226 tooltip => "#npc_message_entry",
947 . "Sometimes you have to tell an NPC something you cannot find out during "
948 . "a normal conversation (such as a password). In those cases you have to use "
949 . "this text entry. You can also enter responses manually instead of using the response "
950 . "buttons below.",
951 on_activate => sub { 1227 on_activate => sub {
952 my ($entry, $text) = @_; 1228 my ($entry, $text) = @_;
953 1229
954 return unless $text =~ /\S/; 1230 return unless $text =~ /\S/;
955 1231
958 1234
959 0 1235 0
960 }, 1236 },
961 ); 1237 );
962 1238
963 $vbox->add ($self->{options} = new CFClient::UI::VBox); 1239 $vbox->add ($self->{options} = new CFPlus::UI::VBox);
964 1240
965 $self->{bye_button} = new CFClient::UI::Button 1241 $self->{bye_button} = new CFPlus::UI::Button
966 text => "Bye (close)", 1242 text => "Bye (close)",
967 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 1243 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
968 on_activate => sub { $this->destroy; 1 }, 1244 on_activate => sub { $this->destroy; 1 },
969 ; 1245 ;
970 1246
971 $self->update_options; 1247 $self->update_options;
972 1248
973 $self->{token} = $self->{conn}->ext_token; 1249 $self->{id} = $self->{conn}->send_ext_req (
974 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 1250 npc_dialog_begin => dx => $self->{dx}, dy => $self->{dy},
975 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); 1251 sub { $this && $this->feed (@_) }
1252 );
976 1253
977 $self->{entry}->grab_focus; 1254 $self->{entry}->grab_focus;
978 1255
979 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); 1256 $self->{textview}->add_paragraph ({
1257 fg => [1, 1, 0, 1],
1258 markup => "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n",
1259 });
980 1260
981 $self->show; 1261 $self->show;
982 $self 1262 $self
983}; 1263};
984 1264
985sub update_options { 1265sub update_options {
986 my ($self) = @_; 1266 my ($self) = @_;
987 1267
988 Scalar::Util::weaken $self; 1268 CFPlus::weaken $self;
989 1269
990 $self->{options}->clear; 1270 $self->{options}->clear;
991 $self->{options}->add ($self->{bye_button}); 1271 $self->{options}->add ($self->{bye_button});
992 1272
993 for my $kw (sort keys %{ $self->{kw} }) { 1273 for my $kw (sort keys %{ $self->{kw} }) {
994 $self->{options}->add (new CFClient::UI::Button 1274 $self->{options}->add (new CFPlus::UI::Button
995 text => $kw, 1275 text => $kw,
996 on_activate => sub { 1276 on_activate => sub {
997 $self->send ($kw); 1277 $self->send ($kw);
998 0 1278 0
999 }, 1279 },
1000 ); 1280 );
1001 } 1281 }
1002} 1282}
1003 1283
1004sub feed { 1284sub feed {
1005 my ($self, $data) = @_; 1285 my ($self, $msg) = @_;
1006 1286
1007 Scalar::Util::weaken $self; 1287 CFPlus::weaken $self;
1008 1288
1009 my ($type, $msg) = split / /, $data, 2; 1289 if ($msg->{msgtype} eq "reply") {
1290 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1291 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1010 1292
1011 if ($type eq "msg") { 1293 my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg};
1012 my ($msg, @kw) = split /\x00/, $msg;
1013
1014 utf8::decode $_ for ($msg, @kw);
1015
1016 $self->{kw}{$_} = 1 for @kw;
1017
1018 $msg = "\n" . CFClient::UI::asxml $msg;
1019 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1294 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1020 my @link; 1295 my @link;
1021 $msg =~ s{ 1296 $text =~ s{
1022 ($match) 1297 ($match)
1023 }{ 1298 }{
1024 my $kw = $1; 1299 my $kw = $1;
1025 1300
1026 push @link, new CFClient::UI::Label 1301 push @link, new CFPlus::UI::Label
1027 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", 1302 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1028 can_hover => 1, 1303 can_hover => 1,
1029 can_events => 1, 1304 can_events => 1,
1030 padding_x => 0, 1305 padding_x => 0,
1031 padding_y => 0, 1306 padding_y => 0,
1032 on_button_up => sub { 1307 on_button_up => sub {
1033 $self->send ($kw); 1308 $self->send ($kw);
1034 }; 1309 };
1035 1310
1036 chr 0xfffc 1311 "\x{fffc}"
1037 }giex; 1312 }giex;
1038 1313
1039 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]); 1314 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1040 $self->{textview}->scroll_to_bottom; 1315 $self->{textview}->scroll_to_bottom;
1041 $self->update_options; 1316 $self->update_options;
1042 } else { 1317 } else {
1043 $self->destroy; 1318 $self->destroy;
1044 } 1319 }
1047} 1322}
1048 1323
1049sub send { 1324sub send {
1050 my ($self, $msg) = @_; 1325 my ($self, $msg) = @_;
1051 1326
1052 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::asxml $msg); 1327 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
1053 $self->{textview}->scroll_to_bottom; 1328 $self->{textview}->scroll_to_bottom;
1054 1329
1055 utf8::encode $msg; 1330 $self->{conn}->send_ext_msg (npc_dialog_tell => msgid => $self->{id}, msg => $msg);
1056 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg");
1057} 1331}
1058 1332
1059sub destroy { 1333sub destroy {
1060 my ($self) = @_; 1334 my ($self) = @_;
1061 1335
1062 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1336 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1063 1337
1338 if ($self->{conn}) {
1064 $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token}; 1339 $self->{conn}->send_ext_msg (npc_dialog_end => msgid => $self->{id}) if $self->{id};
1065 delete $self->{conn}{npc_dialog}; 1340 delete $self->{conn}{npc_dialog};
1066 $self->{conn}->disconnect_ext ($self->{token}); 1341 $self->{conn}->disconnect_ext ($self->{id});
1342 }
1067 1343
1068 $self->SUPER::destroy; 1344 $self->SUPER::destroy;
1069} 1345}
1070 1346
10711; 13471
1348

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines