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.57 by root, Tue Jul 11 15:30:23 2006 UTC vs.
Revision 1.180 by root, Wed Dec 26 18:09:30 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 Deliantra::Protocol::Constants;
7 7
8use CFClient; 8use CFPlus;
9use CFPlus::DB;
9use CFClient::UI; 10use CFPlus::UI;
11use CFPlus::Pod;
12use CFPlus::Macro;
13use CFPlus::Item;
10 14
11use base 'Crossfire::Protocol::Base'; 15use base 'Deliantra::Protocol::Base';
12
13our %open_logs;
14 16
15sub new { 17sub new {
16 my $class = shift; 18 my ($class, %arg) = @_;
17 19
18 my $self = $class->SUPER::new (@_); 20 my $self = $class->SUPER::new (%arg,
21 setup_req => {
22 extmap => 1,
23 excmd => 1,
24 widget => 1,
25 %{$arg{setup_req} || {}},
26 },
27 );
19 28
20 $self->{map_widget}->clr_commands; 29 $self->{map_widget}->clr_commands;
21 30
22 my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub { 31 my @cmd_help = map {
23 my ($pom) = @_;
24
25 my @cmd_help;
26
27 for my $head2 ($pom->head1->[-2]->head2) {
28 $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 32 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
29 or next; 33 or die "unparseable command help: $_->{kw}[0]";
30 34
31 my $cmd = $1; 35 my $cmd = $1;
32 my @args = split /\|/, $2; 36 my @args = split /\|/, $2;
33 @args = (".*") unless @args; 37 @args = (".*") unless @args;
34 38
39 my (undef, @par) = CFPlus::Pod::section_of $_;
40 my $text = CFPlus::Pod::as_label @par;
41
35 $_ = $_ eq ".*" ? "" : " $_" 42 $_ = $_ eq ".*" ? "" : " $_"
36 for @args; 43 for @args;
37 44
38 my $text = CFClient::pod_to_pango $head2->content; 45 map ["$cmd$_", $text],
39
40 push @cmd_help, ["$cmd$_", $text]
41 for sort { (length $a) <=> (length $b) } 46 sort { (length $a) <=> (length $b) }
42 @args; 47 @args
48 } sort { $a->{par} <=> $b->{par} }
49 CFPlus::Pod::find command => "*";
50
51 $self->{json_coder}
52 ->convert_blessed
53 ->filter_json_single_key_object ("\fw" => sub {
54 $self->{widget}{$_[0]}
43 } 55 })
56 ->filter_json_single_key_object ("\fc" => sub {
57 my ($id) = @_;
58 sub {
59 $self->send_exti_msg (w_e => $id, @_);
60 }
61 });
44 62
63 # destroy widgets on logout
64 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
65 for my $ws (values %{delete $self->{widgetset} || {}}) {
66 $_->destroy
67 for values %{delete $ws->{w} || {}};
68 }
69 });
70
71 $self->{map_widget}->add_command (@$_)
45 \@cmd_help 72 for @cmd_help;
73
74 {
75 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
76 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
77 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
78 }
79
80 {
81 $self->{noface} = my $tex = new_from_file CFPlus::Texture
82 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
83 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
84 }
85
86 $self->{open_container} = 0;
87
88 # per server
89 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
90
91 $self
92}
93
94sub update_fx_want {
95 my ($self) = @_;
96
97 $self->send_exti_msg (fx_want => {
98 3 => !!$::CFG->{bgm_enable}, # FT_MUSIC
99 5 => !!$::CFG->{audio_enable}, # FT_SOUND
100 6 => 1, # FT_RSRC
101 });
102}
103
104sub ext_capabilities {
105 my ($self, %cap) = @_;
106
107 #$self->send ("setup sound 0"); # we use a different protocol
108 $self->update_fx_want;
109
110 $self->send_exti_req (resource => "exp_table", sub {
111 my ($exp_table) = @_;
112
113 $self->register_face_handler ($exp_table, sub {
114 my ($face) = @_;
115
116 $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
117 $_->() for values %{ $self->{on_exp_update} || {} };
118 });
119
120 ()
121 });
122
123 if (my $ts = $cap{tileset}) {
124 if (my ($default) = grep $_->[2] & 1, @$ts) {
125 $self->{tileset} = $default;
126 $self->{tilesize} = $default->[3];
127 $self->setup_req (tileset => $default->[0]);
128
129 my $w = int $self->{mapw} * 32 / $self->{tilesize};
130 my $h = int $self->{maph} * 32 / $self->{tilesize};
131
132 $self->setup_req (mapsize => "${w}x${h}");
133 }
134 }
135}
136
137sub ext_ambient_music {
138 my ($self, $songs) = @_;
139 &::audio_music_set_ambient ($songs);
140}
141
142#############################################################################
143
144sub widget_associate {
145 my ($self, $ws, $id, $widget) = @_;
146
147 $widget ||= new CFPlus::UI::Bin;
148
149 $widget->{s_id} = $id;
150 $self->{widget}{$id} = $widget;
151
152 if ($ws) {
153 $widget->{s_ws} = $ws;
154 $self->{widgetset}{$ws}{w}{$id} = $widget;
155 }
156
157 $widget->connect (on_destroy => sub {
158 my ($widget) = @_;
159
160 delete $self->{widget}{$widget->{s_id}};
161 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}}
162 if exists $widget->{s_ws};
163 });
164}
165
166# widgetset new
167sub ext_ws_n {
168 my ($self, $id) = @_;
169
170 $self->{widgetset}{$id} = {
171 w => {},
46 }; 172 };
47
48 $self->{map_widget}->add_command (@$_)
49 for @$cmd_help;
50
51 $self->{noface} = new_from_file CFClient::Texture
52 CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
53
54 $self->{open_container} = 0;
55
56 # "global"
57 $self->{tilecache} = CFClient::db_table "tilecache";
58 $self->{facemap} = CFClient::db_table "facemap";
59
60 # per server
61 $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
62
63 $self
64} 173}
174
175# widgetset destroy
176sub ext_ws_d {
177 my ($self, $id) = @_;
178
179 my $ws = delete $self->{widgetset}{$id}
180 or return;
181
182 $_->destroy
183 for values %{$ws->{w}};
184}
185
186# widgetset create
187sub ext_ws_c {
188 my ($self, $ws, $id, $class, $args) = @_;
189
190 $self->widget_associate (
191 $ws, $id => scalar eval {
192 local $SIG{__DIE__};
193 "CFPlus::UI::$class"->new (%$args)
194 }
195 );
196}
197
198# widgetset associate
199sub ext_ws_a {
200 my ($self, %ass) = @_;
201
202 # everything that has a name, wether conceivably useful or not
203 my %wkw = (
204 root => $CFPlus::UI::ROOT,
205 tooltip => $CFPlus::UI::TOOLTIP,
206
207 mapwidget => $::MAPWIDGET,
208 buttonbar => $::BUTTONBAR,
209 metaserver => $::METASERVER,
210 buttonbar => $::BUTTONBAR,
211 login_button => $::LOGIN_BUTTON,
212 quit_dialog => $::QUIT_DIALOG,
213 host_entry => $::HOST_ENTRY,
214 metaserver => $::METASERVER,
215 server_info => $::SERVER_INFO,
216
217 setup_dialog => $::SETUP_DIALOG,
218 setup_notebook => $::SETUP_NOTEBOOK,
219 setup_server => $::SETUP_SERVER,
220 setup_keyboard => $::SETUP_KEYBOARD,
221
222 pl_notebook => $::PL_NOTEBOOK,
223 pl_window => $::PL_WINDOW,
224 inventory_page => $::INVENTORY_PAGE,
225 stats_page => $::STATS_PAGE,
226 skill_page => $::SKILL_PAGE,
227 spell_page => $::SPELL_PAGE,
228 spell_list => $::SPELL_LIST,
229
230 floorbox => $::FLOORBOX,
231 help_window => $::HELP_WINDOW,
232 message_window => $::MESSAGE_WINDOW,
233 statusbox => $::SDTATUSBOX,
234
235 inv => $::INV,
236 invr => $::INVR,
237 invr_hb => $::INVR_HB,
238 );
239
240 while (my ($id, $name) = each %ass) {
241 $self->widget_associate (undef, $id => $wkw{$name});
242 }
243}
244
245# widget call
246sub ext_w_c {
247 my ($self, $id, $rcb, $method, @args) = @_;
248
249 my $w = $self->{widget}{$id}
250 or return;
251
252 if ($rcb) {
253 $rcb->($w->$method (@args));
254 } else {
255 $w->$method (@args);
256 }
257}
258
259# widget set
260sub ext_w_s {
261 my ($self, $id, $attr) = @_;
262
263 my $w = $self->{widget}{$id}
264 or return;
265
266 for (my $i = 0; $i < $#$attr; $i += 2) {
267 my ($member, $value) = @$attr[$i, $i+1];
268 if (defined $value) {
269 $w->{$member} = $value;
270 } else {
271 delete $w->{$member};
272 }
273 $w->{parent}->realloc if $member =~ /^c_/ && $w->{visible};
274 }
275}
276
277# widget get
278sub ext_w_g {
279 my ($self, $id, $rid, @attr) = @_;
280
281 my $w = $self->{widget}{$id}
282 or return;
283
284 $self->send_exti_msg (w_e => $rid, map $w->{$_}, @attr);
285}
286
287# message window
288sub ext_channel_info {
289 my ($self, $info) = @_;
290 $self->{channels}->{$info->{id}} = $info;
291 $::MESSAGE_WINDOW->add_channel ($info);
292}
293
294#############################################################################
65 295
66sub logprint { 296sub logprint {
67 my ($self, @a) = @_; 297 my ($self, @a) = @_;
68 my $filename = "$Crossfire::VARDIR/log.$self->{host}";
69 298
70 my $fh = $open_logs{$filename}; 299 CFPlus::DB::logprint "$Deliantra::VARDIR/log.$self->{host}" => (join "", @a), sub { };
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} 300}
87 301
88sub _stat_numdiff { 302sub _stat_numdiff {
89 my ($self, $name, $old, $new) = @_; 303 my ($self, $name, $old, $new) = @_;
90 304
155); 369);
156 370
157sub stats_update { 371sub stats_update {
158 my ($self, $stats) = @_; 372 my ($self, $stats) = @_;
159 373
160 if (my $prev = $self->{prev_stats}) { 374 my $prev = $self->{prev_stats} || { };
161 if (my $diff = $stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) { 375
376 if (my @diffs =
377 (
378 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
379 map {
380 $stats->{$_} && $prev->{$_}
381 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
382 } sort { $a <=> $b } keys %{$self->{skill_info}}
383 )
384 ) {
385 my $msg = join " ", @diffs;
162 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5); 386 $self->{statusbox}->add ($msg, group => "experience $msg", fg => [0.5, 1, 0.5, 0.8], timeout => 5);
163 } 387 }
164 388
165 if ( 389 if (
166 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange 390 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
167 ) { 391 ) {
168 my $msg = "<b>stat change</b>: " . (join " ", @diffs); 392 my $msg = "<b>stat change</b>: " . (join " ", @diffs);
169 $self->{statusbox}->add ($msg, group => $msg, fg => [0.8, 1, 0.2, 1], timeout => 10); 393 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20);
170 }
171 } 394 }
395
396 $self->update_stats_window ($stats, $prev);
172 397
173 $self->{prev_stats} = { %$stats }; 398 $self->{prev_stats} = { %$stats };
399}
174 400
175 ::update_stats_window ($stats); 401my %RES_TBL = (
402 phys => CS_STAT_RES_PHYS,
403 magic => CS_STAT_RES_MAG,
404 fire => CS_STAT_RES_FIRE,
405 elec => CS_STAT_RES_ELEC,
406 cold => CS_STAT_RES_COLD,
407 conf => CS_STAT_RES_CONF,
408 acid => CS_STAT_RES_ACID,
409 drain => CS_STAT_RES_DRAIN,
410 ghit => CS_STAT_RES_GHOSTHIT,
411 pois => CS_STAT_RES_POISON,
412 slow => CS_STAT_RES_SLOW,
413 para => CS_STAT_RES_PARA,
414 tund => CS_STAT_TURN_UNDEAD,
415 fear => CS_STAT_RES_FEAR,
416 depl => CS_STAT_RES_DEPLETE,
417 deat => CS_STAT_RES_DEATH,
418 holyw => CS_STAT_RES_HOLYWORD,
419 blind => CS_STAT_RES_BLIND,
420);
421
422sub update_stats_window {
423 my ($self, $stats, $prev) = @_;
424
425 # I love text protocols...
426
427 my $hp = $stats->{+CS_STAT_HP} * 1;
428 my $hp_m = $stats->{+CS_STAT_MAXHP} * 1;
429 my $sp = $stats->{+CS_STAT_SP} * 1;
430 my $sp_m = $stats->{+CS_STAT_MAXSP} * 1;
431 my $fo = $stats->{+CS_STAT_FOOD} * 1;
432 my $fo_m = 999;
433 my $gr = $stats->{+CS_STAT_GRACE} * 1;
434 my $gr_m = $stats->{+CS_STAT_MAXGRACE} * 1;
435
436 $::GAUGES->{hp} ->set_value ($hp, $hp_m);
437 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
438 $::GAUGES->{food} ->set_value ($fo, $fo_m);
439 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
440 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
441 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
442 $::GAUGES->{prg} ->set_value ($stats->{+CS_STAT_LEVEL}, $stats->{+CS_STAT_EXP64});
443 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
444 my $title = $stats->{+CS_STAT_TITLE};
445 $title =~ s/^Player: //;
446 $::STATWIDS->{title} ->set_text ("Title: " . $title);
447
448 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
449 $::STATWIDS->{st_dex} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DEX});
450 $::STATWIDS->{st_con} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CON});
451 $::STATWIDS->{st_int} ->set_text (sprintf "%d" , $stats->{+CS_STAT_INT});
452 $::STATWIDS->{st_wis} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WIS});
453 $::STATWIDS->{st_pow} ->set_text (sprintf "%d" , $stats->{+CS_STAT_POW});
454 $::STATWIDS->{st_cha} ->set_text (sprintf "%d" , $stats->{+CS_STAT_CHA});
455 $::STATWIDS->{st_wc} ->set_text (sprintf "%d" , $stats->{+CS_STAT_WC});
456 $::STATWIDS->{st_ac} ->set_text (sprintf "%d" , $stats->{+CS_STAT_AC});
457 $::STATWIDS->{st_dam} ->set_text (sprintf "%d" , $stats->{+CS_STAT_DAM});
458 $::STATWIDS->{st_arm} ->set_text (sprintf "%d" , $stats->{+CS_STAT_RES_PHYS});
459 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
460 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
461
462 $self->update_weight;
463
464 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}})
465 for keys %RES_TBL;
466
467 my $sktbl = $::STATWIDS->{skill_tbl};
468 my @skills = keys %{ $self->{skill_info} };
469
470 my @order = sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] }
471 map [$_, $self->{skill_info}{$_}],
472 grep exists $stats->{$_},
473 @skills;
474
475 if ($self->{stat_order} ne join ",", map $_->[0], @order) {
476 $self->{stat_order} = join ",", map $_->[0], @order;
477
478 $sktbl->clear;
479
480 my $sw = $self->{skillwid}{""} ||= [
481 0, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
482 1, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
483 2, 0, (new CFPlus::UI::Label text => "Progress", align => 0),
484 3, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
485 4, 0, (new CFPlus::UI::Label text => "Experience", align => 1),
486 5, 0, (new CFPlus::UI::Label text => "Lvl.", align => 1),
487 6, 0, (new CFPlus::UI::Label text => "Progress", align => 0),
488 7, 0, (new CFPlus::UI::Label text => "Skill", expand => 1),
489 ];
490
491 my @add = @$sw;
492
493 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
494
495 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
496 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
497
498 my ($x, $y) = (0, 1);
499 for (@order) {
500 my ($idx, $name) = @$_;
501
502 my $spell_cb = sub {
503 my ($widget, $ev) = @_;
504
505 if ($ev->{button} == 1) {
506 $::CONN->user_send ("ready_skill $name");
507 } elsif ($ev->{button} == 2) {
508 $::CONN->user_send ("use_skill $name");
509 } elsif ($ev->{button} == 3) {
510 my $shortname = CFPlus::shorten $name, 14;
511 (new CFPlus::UI::Menu
512 items => [
513 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
514 ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
515 ],
516 )->popup ($ev);
517 } else {
518 return 0;
519 }
520
521 1
522 };
523
524 my $sw = $self->{skillwid}{$idx} ||= [
525 # exp
526 (new CFPlus::UI::Label
527 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP),
528
529 # level
530 (new CFPlus::UI::Label
531 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL),
532
533 # progress
534 (new CFPlus::UI::ExperienceProgress),
535
536 # label
537 (new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
538 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL),
539 ];
540
541 push @add,
542 $x * 4 + 0, $y, $sw->[0],
543 $x * 4 + 1, $y, $sw->[1],
544 $x * 4 + 2, $y, $sw->[2],
545 $x * 4 + 3, $y, $sw->[3],
546 ;
547
548 $x++ and ($x, $y) = (0, $y + 1);
549 }
550
551 $sktbl->add_at (@add);
552 }
553
554 for (@order) {
555 my ($idx, $name) = @$_;
556 my $val = $stats->{$idx};
557
558 next if $prev->{$idx}[1] eq $val->[1];
559
560 my $sw = $self->{skillwid}{$idx};
561 $sw->[0]->set_text (::formsep ($val->[1]));
562 $sw->[1]->set_text ($val->[0] * 1);
563 $sw->[2]->set_value (@$val);
564
565 $::GAUGES->{sklprg}->set_label ("$name %d%%");
566 $::GAUGES->{sklprg}->set_value (@$val);
567 }
176} 568}
177 569
178sub user_send { 570sub user_send {
179 my ($self, $command) = @_; 571 my ($self, $command) = @_;
180 572
573 $self->{record}->($command)
181 if ($self->{record}) { 574 if $self->{record};
182 push @{$self->{record}}, $command;
183 }
184 575
185 $self->logprint ("send: ", $command); 576 $self->logprint ("send: ", $command);
186 $self->send_command ($command); 577 $self->send_command ($command);
187 ::status ($command);
188} 578}
189 579
190sub start_record { 580sub record {
191 my ($self) = @_; 581 my ($self, $cb) = @_;
192 582
193 $self->{record} = []; 583 $self->{record} = $cb;
194}
195
196sub stop_record {
197 my ($self) = @_;
198 return delete $self->{record};
199} 584}
200 585
201sub map_scroll { 586sub map_scroll {
202 my ($self, $dx, $dy) = @_; 587 my ($self, $dx, $dy) = @_;
203 588
205} 590}
206 591
207sub feed_map1a { 592sub feed_map1a {
208 my ($self, $data) = @_; 593 my ($self, $data) = @_;
209 594
210 $self->{map}->map1a_update ($data); 595 my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap});
596 my $delay;
597
598 for my $tile (@$missing) {
599 next if $self->{delay}{$tile};
600
601 $delay = 1;
602
603 if (my $tex = $::CONN->{texture}[$tile]) {
604 $tex->upload;
605 } else {
606 $self->{delay}{$tile} = 1;
607
608 # we assume the face is in-flight and will eventually come
609 push @{$self->{tile_cb}{$tile}}, sub {
610 delete $self->{delay}{$tile};
611 $_[0]->upload;
612 };
613 }
614 }
615
616 if ($delay) {
617 # delay the map drawing a tiny bit in the hope of getting the missing fetched
618 EV::once undef, 0, 0.03, sub {
619 $self->{map_widget}->update
620 if $self->{map_widget};
621 };
622 } else {
211 $self->{map_widget}->update; 623 $self->{map_widget}->update;
624 }
212} 625}
213 626
214sub magicmap { 627sub magicmap {
215 my ($self, $w, $h, $x, $y, $data) = @_; 628 my ($self, $w, $h, $x, $y, $data) = @_;
216 629
223 my $map_info = delete $self->{map_info} 636 my $map_info = delete $self->{map_info}
224 or return; 637 or return;
225 638
226 my ($hash, $x, $y, $w, $h) = @$map_info; 639 my ($hash, $x, $y, $w, $h) = @$map_info;
227 640
228 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 641 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h);
229 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 642 $self->{map_cache_new}{$hash} = \$data;
230 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 643 CFPlus::DB::put $self->{mapcache} => $hash => $data, sub { };
231} 644}
232 645
233sub map_clear { 646sub map_clear {
234 my ($self) = @_; 647 my ($self) = @_;
235 648
238 651
239 $self->{map}->clear; 652 $self->{map}->clear;
240 delete $self->{map_widget}{magicmap}; 653 delete $self->{map_widget}{magicmap};
241} 654}
242 655
656sub bg_fetch {
657 my ($self) = @_;
658
659 my $tile;
660
661 do {
662 $tile = pop @{$self->{bg_fetch}}
663 or return;
664 } while $self->{texture}[$tile];
665
666 CFPlus::DB::get tilecache => $tile, sub {
667 my ($data) = @_;
668
669 return unless $self->{map}; # stop when destroyed
670
671 if (defined $data) {
672 $self->have_tile ($tile, $data);
673 $self->{texture}[$tile]->upload;
674 }
675
676 $self->bg_fetch;
677 };
678}
243 679
244sub load_map($$$) { 680sub load_map($$$) {
245 my ($self, $hash, $x, $y) = @_; 681 my ($self, $hash, $x, $y) = @_;
246 682
247 if (defined (my $data = $self->{mapcache}->get ($hash))) { 683 my $gen = $self->{map_change_gen};
684
685 my $cb = sub {
686 return unless $gen == $self->{map_change_gen};
687
688 my ($data) = @_;
689
690 if (defined $data) {
691 $self->{map_cache_new}{$hash} = \$data;
692
248 $data = Compress::LZF::decompress $data; 693 my $data = Compress::LZF::decompress $data;
249 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
250 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
251 my $data = $self->{tilecache}->get ($id)
252 or next;
253 694
254 $self->set_texture ($id => $data); 695 my $inprogress = @{ $self->{bg_fetch} || [] };
696 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
697 $self->bg_fetch unless $inprogress;
255 } 698 }
699 };
700
701 if (my $rdata = $self->{map_cache_old}{$hash}) {
702 $cb->($$rdata);
703 } else {
704 CFPlus::DB::get $self->{mapcache} => $hash, $cb;
256 } 705 }
257} 706}
258 707
259# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 708# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
260# (server resource,s latency, bandwidth), so this hack is warranted. 709# (server resource,s latency, bandwidth), so this hack is warranted.
325 774
326 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 775 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
327 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 776 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
328 777
329 } else { 778 } else {
779 my $gen = $self->{map_change_gen};
330 $self->send_mapinfo ("spatial $path$tile", sub { 780 $self->send_mapinfo ("spatial $path$tile", sub {
781 return unless $gen == $self->{map_change_gen};
782
331 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 783 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
332 784
333 return if $mode ne "spatial"; 785 return if $mode ne "spatial";
334 786
335 $x += $self->{map}->ox; 787 $x += $self->{map}->ox;
349 801
350sub map_change { 802sub map_change {
351 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 803 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
352 804
353 $self->flush_map; 805 $self->flush_map;
806
807 ++$self->{map_change_gen};
808 $self->{map_cache_old} = delete $self->{map_cache_new};
354 809
355 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 810 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
356 811
357 my $mapmapw = $self->{mapmap}->{w}; 812 my $mapmapw = $self->{mapmap}->{w};
358 my $mapmaph = $self->{mapmap}->{h}; 813 my $mapmaph = $self->{mapmap}->{h};
375 $self->load_map ($hash, $x, $y); 830 $self->load_map ($hash, $x, $y);
376 $self->flood_fill (0, 0, 0, "", $hash, $flags); 831 $self->flood_fill (0, 0, 0, "", $hash, $flags);
377} 832}
378 833
379sub face_find { 834sub face_find {
835 my ($self, $facenum, $face, $cb) = @_;
836
837 if ($face->{type} == 0) { # FT_FACE
838 my $id = CFPlus::DB::get_tile_id_sync $face->{name};
839
840 $face->{id} = $id;
841 $self->{map}->set_tileid ($facenum => $id);
842
843 CFPlus::DB::get tilecache => $id, $cb;
844
845 } elsif ($face->{type} & 1) { # with metadata
846 CFPlus::DB::get res_meta => $face->{name}, $cb;
847
848 } else { # no metadata
849 CFPlus::DB::get res_data => $face->{name}, $cb;
850 }
851}
852
853sub face_update {
854 my ($self, $facenum, $face, $changed) = @_;
855
856 if ($face->{type} == 0) {
857 # image, FT_FACE
858 CFPlus::DB::put tilecache => $face->{id} => $face->{data}, sub { }
859 if $changed;
860
861 $self->have_tile ($face->{id}, delete $face->{data});
862
863 } elsif ($face->{type} & 1) {
864 # split metadata case, FT_MUSIC, FT_SOUND
865 if ($changed) { # new data
866 my ($meta, $data) = unpack "(w/a*)*", $face->{data};
867 $face->{data} = $meta;
868
869 # rely on strict ordering here and also on later fetch
870 CFPlus::DB::put res_data => $face->{name} => $data, sub { };
871 CFPlus::DB::put res_meta => $face->{name} => $meta, sub { };
872 }
873
874 $face->{data} = $self->{json_coder}->decode ($face->{data});
875 ::add_license ($face);
876 ::message ({ markup => CFPlus::asxml "downloaded resource '$face->{data}{name}', type $face->{type}." })
877 if $changed;
878
879 if ($face->{type} == 3) { # FT_MUSIC
880 &::audio_music_push ($facenum);
881 } elsif ($face->{type} == 5) { # FT_SOUND
882 &::audio_sound_push ($facenum);
883 }
884
885 } else {
886 # flat resource case, FT_RSRC
887 CFPlus::DB::put res_data => $face->{name} => $face->{data}, sub { }
888 if $changed;
889 }
890
891 if (my $cbs = $self->{face_cb}{$facenum}) {
892 $_->($face, $changed) for @$cbs;
893 }
894}
895
896sub smooth_update {
380 my ($self, $facenum, $face) = @_; 897 my ($self, $facenum, $face) = @_;
381 898
382 my $hash = "$face->{chksum},$face->{name}"; 899 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
900}
383 901
384 my $id = $self->{facemap}->get ($hash); 902sub have_tile {
903 my ($self, $tile, $data) = @_;
385 904
386 unless ($id) { 905 return unless $self->{map};
387 # create new id for face
388 # I love transactions
389 for (1..100) {
390 my $txn = $CFClient::DB_ENV->txn_begin;
391 my $status = $self->{facemap}->db_get (id => $id);
392 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
393 $id = ($id || 64) + 1;
394 if ($self->{facemap}->put (id => $id) == 0
395 && $self->{facemap}->put ($hash => $id) == 0) {
396 $txn->txn_commit;
397 906
398 goto gotid; 907 my $tex = $self->{texture}[$tile] ||=
399 } 908 new CFPlus::Texture
909 tile => $tile,
910 image => $data, delete_image => 1,
911 minify => 1, mipmap => 1;
912
913 if (my $cbs = delete $self->{tile_cb}{$tile}) {
914 $_->($tex) for @$cbs;
915 }
916}
917
918# call in non-void context registers a temporary
919# hook with handle, otherwise its permanent
920sub on_face_change {
921 my ($self, $num, $cb) = @_;
922
923 push @{$self->{face_cb}{$num}}, $cb;
924
925 defined wantarray
926 ? CFPlus::guard {
927 @{$self->{face_cb}{$num}}
928 = grep $_ != $cb,
929 @{$self->{face_cb}{$num}};
400 } 930 }
401 $txn->txn_abort; 931 : ()
402 }
403
404 CFClient::fatal "maximum number of transaction retries reached - database problems?";
405 }
406
407gotid:
408 $face->{id} = $id;
409 $self->{map}->set_face ($facenum => $id);
410 $self->{faceid}[$facenum] = $id;#d#
411
412 my $face = $self->{tilecache}->get ($id);
413
414 if ($face) {
415 #$self->face_prefetch;
416 $face
417 } else {
418 my $tex = $self->{noface};
419 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
420 undef
421 };
422} 932}
423 933
424sub face_update { 934# call in non-void context registers a temporary
935# hook with handle, otherwise its permanent
936sub register_face_handler {
425 my ($self, $facenum, $face) = @_; 937 my ($self, $num, $cb) = @_;
426 938
427 $self->{tilecache}->put ($face->{id} => $face->{image}) 939 return unless $num;
428 if $self->{tilecache}->get ($face->{id}) ne $face->{image};
429 940
430 $self->set_texture ($face->{id} => delete $face->{image}); 941 # invoke if available right now
431} 942 $cb->($self->{face}[$num], 0)
943 unless exists $self->{face}[$num]{loading};
432 944
433sub set_texture { 945 # future changes
434 my ($self, $id, $data) = @_; 946 $self->on_face_change ($num => $cb)
435
436 $self->{texture}[$id] ||= do {
437 my $tex =
438 new_from_image CFClient::Texture
439 $data, minify => 1, mipmap => 1;
440
441 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
442 $self->{map_widget}->update;
443
444 $tex
445 };
446} 947}
447 948
448sub sound_play { 949sub sound_play {
449 my ($self, $x, $y, $soundnum, $type) = @_; 950 my ($self, $type, $face, $dx, $dy, $vol) = @_;
450 951
451 $self->{sound_play}->($x, $y, $soundnum, $type); 952 &::audio_sound_play ($face, $dx, $dy, $vol)
953 unless $type & 1; # odd types are silent for future expansion
452} 954}
453 955
454my $LAST_QUERY; # server is stupid, stupid, stupid 956my $LAST_QUERY; # server is stupid, stupid, stupid
455 957
456sub query { 958sub query {
460 $LAST_QUERY = $prompt; 962 $LAST_QUERY = $prompt;
461 963
462 $self->{query}-> ($self, $flags, $prompt); 964 $self->{query}-> ($self, $flags, $prompt);
463} 965}
464 966
465sub drawinfo { 967sub sanitise_xml($) {
466 my ($self, $color, $text) = @_; 968 local $_ = shift;
467 969
468 my @color = ( 970 # we now weed out all tags we do not support
971 s{ <(?! /?i> | /?u> | /?b> | /?big | /?small | /?s | /?tt | fg\ | /fg>)
972 }{
973 "&lt;"
974 }gex;
975
976 # now all entities
977 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
978
979 # handle some elements
980 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
981 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
982
983 $_
984}
985
986our %NAME_TO_COLOR = (
987 black => 0,
988 white => 1,
989 darkblue => 2,
990 red => 3,
991 orange => 4,
992 lightblue => 5,
993 darkorange => 6,
994 green => 7,
995 darkgreen => 8,
996 grey => 9,
997 brown => 10,
998 yellow => 11,
999 tan => 12,
1000);
1001
1002our @CF_COLOR = (
469 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 1003 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
470 [1.00, 1.00, 1.00], 1004 [1.00, 1.00, 1.00],
471 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 1005 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
472 [1.00, 0.00, 0.00], 1006 [1.00, 0.00, 0.00],
473 [1.00, 0.54, 0.00], 1007 [1.00, 0.54, 0.00],
474 [0.11, 0.56, 1.00], 1008 [0.11, 0.56, 1.00],
475 [0.93, 0.46, 0.00], 1009 [0.93, 0.46, 0.00],
476 [0.18, 0.54, 0.34], 1010 [0.18, 0.54, 0.34],
477 [0.56, 0.73, 0.56], 1011 [0.56, 0.73, 0.56],
478 [0.80, 0.80, 0.80], 1012 [0.80, 0.80, 0.80],
479 [0.55, 0.41, 0.13], 1013 [0.75, 0.61, 0.20],
480 [0.99, 0.77, 0.26], 1014 [0.99, 0.77, 0.26],
481 [0.74, 0.65, 0.41], 1015 [0.74, 0.65, 0.41],
482 ); 1016);
483 1017
1018sub msg {
1019 my ($self, $color, $type, $text, @extra) = @_;
1020
1021 $text = sanitise_xml $text;
1022
1023 if (my $cb = $self->{cb_msg}{$type}) {
1024 $_->($self, $color, $type, $text, @extra) for values %$cb;
1025 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
1026 $type =~ s/-/_/g;
1027 $self->{$type} = $text;
1028 } else {
484 $self->logprint ("info: ", $text); 1029 $self->logprint ("msg: ", $text);
1030 return if $color < 0; # negative color == ignore if not understood
485 1031
486 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 1032 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0];
487 1033
488 # try to create single paragraphs of multiple lines sent by the server 1034 ## try to create single paragraphs of multiple lines sent by the server
1035 # no longer neecssary with TRT servers
489 $text =~ s/(?<=\S)\n(?=\w)/ /g; 1036 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
490 1037
491 $text = CFClient::UI::Label::escape $text; 1038 for (split /\n/, $text) {
492 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 1039 ::message ({
493 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g; 1040 fg => $fg,
1041 markup => $_,
1042 type => $type,
1043 extra => [@extra],
1044 color_flags => $color, #d# ugly, kill
1045 });
494 1046
495 $self->{logview}->add_paragraph ($color[$color], $_) 1047 $color &= ~NDI_CLEAR; # only clear once for multiline messages
496 for map "$time $_", split /\n/, $text; 1048 # actually, this is an ugly design. _we_ should control the channels,
497 $self->{logview}->scroll_to_bottom; 1049 # not some random other widget, as the channels are clearly protocol-specific.
1050 # then we could also react to flags such as CLEAR without resorting to
1051 # hacks such as color_flags, above.
1052 }
498 1053
499 $self->{statusbox}->add ($text, 1054 $self->{statusbox}->add ($text,
500 group => $text, 1055 group => $text,
501 fg => $color[$color], 1056 fg => $fg,
502 timeout => $color >= 2 ? 60 : 10, 1057 timeout => $color >= 2 ? 180 : 10,
503 tooltip_font => $::FONT_FIXED, 1058 tooltip_font => $::FONT_FIXED,
504 ); 1059 ) if $type eq "info";
505} 1060 }
506
507sub drawextinfo {
508 my ($self, $color, $type, $subtype, $message) = @_;
509
510 $self->drawinfo ($color, $message);
511} 1061}
512 1062
513sub spell_add { 1063sub spell_add {
514 my ($self, $spell) = @_; 1064 my ($self, $spell) = @_;
515 1065
516 # try to create single paragraphs of multiple lines sent by the server 1066 # try to create single paragraphs out of the multiple lines sent by the server
517 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 1067 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
518 $spell->{message} =~ s/\n+$//; 1068 $spell->{message} =~ s/\n+$//;
519 $spell->{message} ||= "Server did not provide a description for this spell."; 1069 $spell->{message} ||= "Server did not provide a description for this spell.";
520 1070
521 $::SPELL_PAGE->add_spell ($spell); 1071 $::SPELL_LIST->add_spell ($spell);
522 1072
523 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 1073 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message});
524 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 1074 $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message});
525} 1075}
526 1076
527sub spell_delete { 1077sub spell_delete {
528 my ($self, $spell) = @_; 1078 my ($self, $spell) = @_;
529 1079
530 $::SPELL_PAGE->remove_spell ($spell); 1080 $::SPELL_LIST->remove_spell ($spell);
1081}
1082
1083sub setup {
1084 my ($self, $setup) = @_;
1085
1086 $self->{map_widget}->set_tilesize ($self->{tilesize});
1087 $::MAP->resize ($self->{mapw}, $self->{maph});
531} 1088}
532 1089
533sub addme_success { 1090sub addme_success {
534 my ($self) = @_; 1091 my ($self) = @_;
535 1092
536 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
537 my ($pom) = @_;
538
539 my %skill_help; 1093 my %skill_help;
540 1094
541 for my $head2 ($pom->head1->[3]->head2) { 1095 for my $node (CFPlus::Pod::find skill_description => "*") {
542 $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content; 1096 my (undef, @par) = CFPlus::Pod::section_of $node;
543 } 1097 $skill_help{$node->{kw}[0]} = CFPlus::Pod::as_label @par;
544
545 \%skill_help
546 }; 1098 };
547 1099
548 for my $skill (values %{$self->{skill_info}}) { 1100 for my $skill (values %{$self->{skill_info}}) {
549 $self->{map_widget}->add_command ("ready_skill $skill", 1101 $self->{map_widget}->add_command ("ready_skill $skill",
550 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") 1102 (CFPlus::asxml "Ready the skill '$skill'\n\n")
551 . $skill_help->{$skill}); 1103 . $skill_help{$skill});
552 $self->{map_widget}->add_command ("use_skill $skill", 1104 $self->{map_widget}->add_command ("use_skill $skill",
553 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") 1105 (CFPlus::asxml "Immediately use the skill '$skill'\n\n")
554 . $skill_help->{$skill}); 1106 . $skill_help{$skill});
555 } 1107 }
556} 1108}
557 1109
558sub eof { 1110sub eof {
559 my ($self) = @_; 1111 my ($self) = @_;
561 $self->{map_widget}->clr_commands; 1113 $self->{map_widget}->clr_commands;
562 1114
563 ::stop_game (); 1115 ::stop_game ();
564} 1116}
565 1117
566sub image_info {
567 my ($self, $numfaces) = @_;
568
569 $self->{num_faces} = $numfaces;
570 $self->{face_prefetch} = [1 .. $numfaces];
571 $self->face_prefetch;
572}
573
574sub face_prefetch {
575 my ($self) = @_;
576
577 return unless $::CFG->{face_prefetch};
578
579 if ($self->{num_faces}) {
580 return if @{ $self->{send_queue} || [] };
581 my $todo = @{ $self->{face_prefetch} }
582 or return;
583
584 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
585
586 $self->send ("requestinfo image_sums $face $face");
587
588 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
589 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
590 } elsif (!exists $self->{num_faces}) {
591 $self->send ("requestinfo image_info");
592
593 $self->{num_faces} = 0;
594
595 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
596 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
597 }
598}
599
600sub update_floorbox { 1118sub update_floorbox {
601 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 1119 $CFPlus::UI::ROOT->on_refresh ($::FLOORBOX => sub {
602 return unless $::CONN; 1120 return unless $::CONN;
603 1121
604 $::FLOORBOX->clear; 1122 $::FLOORBOX->clear;
605 1123
1124 my @add;
1125
606 my $row; 1126 my $row;
607 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{0} }) { 1127 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
608 if ($row < 6) { 1128 if ($row < 6) {
609 local $_->{face_widget}; # hack to force recreation of widget 1129 local $_->{face_widget}; # hack to force recreation of widget
610 local $_->{desc_widget}; # hack to force recreation of widget 1130 local $_->{desc_widget}; # hack to force recreation of widget
611 CFClient::Item::update_widgets $_; 1131 CFPlus::Item::update_widgets $_;
612 1132
1133 push @add,
613 $::FLOORBOX->add (0, $row, $_->{face_widget}); 1134 0, $row, $_->{face_widget},
614 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 1135 1, $row, $_->{desc_widget};
615 1136
616 $row++; 1137 $row++;
617 } else { 1138 } else {
618 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 1139 push @add, 1, $row, new CFPlus::UI::Button
619 text => "More...", 1140 text => "More...",
620 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, 1141 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
621 ); 1142 ;
622 last; 1143 last;
623 } 1144 }
624 } 1145 }
1146 if ($::CONN->{open_container}) {
1147 push @add, 1, $row++, new CFPlus::UI::Button
1148 text => "Close container",
1149 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
1150 ;
1151 }
1152
1153 $::FLOORBOX->add_at (@add);
625 }); 1154 });
626 1155
627 $::WANT_REFRESH++; 1156 $::WANT_REFRESH->start;
628} 1157}
629 1158
630sub set_opencont { 1159sub set_opencont {
631 my ($conn, $tag, $name) = @_; 1160 my ($conn, $tag, $name) = @_;
632 $conn->{open_container} = $tag; 1161 $conn->{open_container} = $tag;
1162 update_floorbox;
633 1163
634 $::INV_RIGHT_HB->clear (); 1164 $::INVR_HB->clear ();
635 $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name); 1165 $::INVR_HB->add (new CFPlus::UI::Label align => 0, expand => 1, text => $name);
636 1166
637 if ($tag != 0) { # Floor isn't closable, is it? 1167 if ($tag != 0) { # Floor isn't closable, is it?
638 $::INV_RIGHT_HB->add (new CFClient::UI::Button 1168 $::INVR_HB->add (new CFPlus::UI::Button
639 text => "Close container", 1169 text => "Close container",
640 tooltip => "Close the currently open container (if one is open)", 1170 tooltip => "Close the currently open container (if one is open)",
641 on_activate => sub { 1171 on_activate => sub {
642 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 1172 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
643 if $tag != 0; 1173 if $tag != 0;
644 #if $CONN->{open_container} != 0;
645 0 1174 0
646 }, 1175 },
647 ); 1176 );
648 } 1177 }
649 1178
651} 1180}
652 1181
653sub update_containers { 1182sub update_containers {
654 my ($self) = @_; 1183 my ($self) = @_;
655 1184
656 $CFClient::UI::ROOT->on_refresh ("update_containers_$self" => sub { 1185 $CFPlus::UI::ROOT->on_refresh ("update_containers_$self" => sub {
657 my $todo = delete $self->{update_container} 1186 my $todo = delete $self->{update_container}
658 or return; 1187 or return;
659 1188
660 for my $tag (keys %$todo) { 1189 for my $tag (keys %$todo) {
1190 update_floorbox if $tag == 0 or $tag == $self->{open_container};
661 if ($tag == 0) { 1191 if ($tag == 0) {
662 update_floorbox;
663 $::INVR->set_items ($self->{container}{0}) 1192 $::INVR->set_items ($self->{container}{0})
664 if $tag == $self->{open_container}; 1193 if $tag == $self->{open_container};
665 } elsif ($tag == $self->{player}{tag}) { 1194 } elsif ($tag == $self->{player}{tag}) {
666 $::INV->set_items ($self->{container}{$tag}) 1195 $::INV->set_items ($self->{container}{$tag})
667 } else { 1196 } else {
698sub item_update { 1227sub item_update {
699 my ($self, $item) = @_; 1228 my ($self, $item) = @_;
700 1229
701 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 1230 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
702 1231
703 CFClient::Item::update_widgets $item; 1232 CFPlus::Item::update_widgets $item;
704 1233
705 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1234 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
706 set_opencont ($::CONN, 0, "Floor"); 1235 set_opencont ($::CONN, 0, "Floor");
707 1236
708 } elsif ($item->{flags} & F_OPEN) { 1237 } elsif ($item->{flags} & F_OPEN) {
709 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 1238 set_opencont ($::CONN, $item->{tag}, CFPlus::Item::desc_string $item);
710 1239
711 } else { 1240 } else {
712 $self->{update_container}{$item->{container}}++; 1241 $self->{update_container}{$item->{container}}++;
713 $self->update_containers; 1242 $self->update_containers;
714 } 1243 }
715} 1244}
716 1245
717sub player_update { 1246sub player_update {
718 my ($self, $player) = @_; 1247 my ($self, $player) = @_;
1248
1249 $self->update_weight;
1250}
1251
1252sub update_weight {
1253 my ($self) = @_;
1254
1255 my $weight = .001 * $self->{player}{weight};
1256 my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM};
1257
719 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); 1258 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight);
1259 $::STATWIDS->{m_weight}->set_text (sprintf "%.1fkg", $limit);
1260 $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit);
720} 1261}
721 1262
722sub update_server_info { 1263sub update_server_info {
723 my ($self) = @_; 1264 my ($self) = @_;
724 1265
727 $::SERVER_INFO->set_markup ( 1268 $::SERVER_INFO->set_markup (
728 "server <tt>$self->{host}:$self->{port}</tt>\n" 1269 "server <tt>$self->{host}:$self->{port}</tt>\n"
729 . "protocol version <tt>$self->{version}</tt>\n" 1270 . "protocol version <tt>$self->{version}</tt>\n"
730 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1271 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
731 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 1272 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1273 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
1274 . "editing support $yesno[!!$self->{editor_support}]\n"
1275 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1276 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
732 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1277 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
733 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1278 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
734 . "map size $self->{mapw}×$self->{maph}\n" 1279 . "map size $self->{mapw}×$self->{maph}\n"
735 ); 1280 );
1281
736} 1282}
737 1283
738sub logged_in { 1284sub logged_in {
739 my ($self) = @_; 1285 my ($self) = @_;
740 1286
741 $self->send_ext_req (cfplus_support => "1", sub { 1287 $self->send_ext_req (cfplus_support => version => 2, sub {
1288 my (%msg) = @_;
1289
742 $self->{cfplus_ext} = $_[0]; 1290 $self->{cfplus_ext} = $msg{version};
743 $self->update_server_info; 1291 $self->update_server_info;
1292
1293 if ($self->{cfplus_ext} >= 2) {
1294 $self->send_ext_req ("editor_support", sub {
1295 $self->{editor_support} = { @_ };
1296 $self->update_server_info;
1297
1298 0
1299 });
1300 }
1301
1302 0
744 }); 1303 });
745 1304
746 $self->update_server_info; 1305 $self->update_server_info;
747 1306
748 $self->send_command ("output-sync $::CFG->{output_sync}"); 1307 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
749 $self->send_command ("output-count $::CFG->{output_count}");
750 $self->send_command ("pickup $::CFG->{pickup}"); 1308 $self->send_command ("pickup $::CFG->{pickup}");
751} 1309}
752 1310
753sub lookat { 1311sub lookat {
754 my ($self, $x, $y) = @_; 1312 my ($self, $x, $y) = @_;
755 1313
756 if ($self->{cfplus_ext}) { 1314 if ($self->{cfplus_ext}) {
757 $self->send_ext_req (lookat => "$x $y", sub { 1315 $self->send_ext_req (lookat => $x, $y, sub {
758 my %res = split /\x00/, $_[0]; 1316 my (%msg) = @_;
759 1317
760 if (exists $res{npc_dialog}) { 1318 if (exists $msg{npc_dialog}) {
761 # start npc chat dialog 1319 # start npc chat dialog
762 $self->{npc_dialog} = new CFClient::NPCDialog:: 1320 $self->{npc_dialog} = new CFPlus::NPCDialog::
763 dx => $x, 1321 token => $msg{npc_dialog},
764 dy => $y,
765 title => "$res{npc_dialog} (NPC)", 1322 title => "$msg{npc_dialog}[0] (NPC)",
766 conn => $self, 1323 conn => $self,
767 ; 1324 ;
768 } 1325 }
769 }); 1326 });
770 } 1327 }
773} 1330}
774 1331
775sub destroy { 1332sub destroy {
776 my ($self) = @_; 1333 my ($self) = @_;
777 1334
778 $self->{npc_dialog}->destroy 1335 (delete $self->{npc_dialog})->destroy
779 if $self->{npc_dialog}; 1336 if $self->{npc_dialog};
780 1337
781 $self->SUPER::destroy; 1338 $self->SUPER::destroy;
782}
783 1339
1340 %$self = ();
1341}
1342
784package CFClient::NPCDialog; 1343package CFPlus::NPCDialog;
785 1344
786our @ISA = 'CFClient::UI::FancyFrame'; 1345our @ISA = 'CFPlus::UI::Toplevel';
787 1346
788sub new { 1347sub new {
789 my $class = shift; 1348 my $class = shift;
790 1349
791 my $self = $class->SUPER::new ( 1350 my $self = $class->SUPER::new (
794 name => "npc_dialog", 1353 name => "npc_dialog",
795 force_w => $::WIDTH * 0.7, 1354 force_w => $::WIDTH * 0.7,
796 force_h => $::HEIGHT * 0.7, 1355 force_h => $::HEIGHT * 0.7,
797 title => "NPC Dialog", 1356 title => "NPC Dialog",
798 kw => { hi => 0, yes => 0, no => 0 }, 1357 kw => { hi => 0, yes => 0, no => 0 },
1358 has_close_button => 1,
799 @_, 1359 @_,
800 ); 1360 );
801 1361
802 Scalar::Util::weaken (my $this = $self); 1362 CFPlus::weaken (my $this = $self);
1363
1364 $self->connect (delete => sub { $this->destroy; 1 });
803 1365
804 # better use a pane... 1366 # better use a pane...
805 $self->add (my $hbox = new CFClient::UI::HBox); 1367 $self->add (my $hbox = new CFPlus::UI::HBox);
806 $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1); 1368 $hbox->add ($self->{textview} = new CFPlus::UI::TextScroller expand => 1);
807 1369
808 $hbox->add (my $vbox = new CFClient::UI::VBox); 1370 $hbox->add (my $vbox = new CFPlus::UI::VBox);
809 1371
810 $vbox->add (new CFClient::UI::Label text => "Message Entry:"); 1372 $vbox->add (new CFPlus::UI::Label text => "Message Entry:");
811 $vbox->add ($self->{entry} = new CFClient::UI::Entry 1373 $vbox->add ($self->{entry} = new CFPlus::UI::Entry
812 tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n" 1374 tooltip => "#npc_message_entry",
813 . "Sometimes you have to tell an NPC something you cannot find out during "
814 . "a normal conversation (such as a password). In those cases you have to use "
815 . "this text entry. You can also enter responses manually instead of using the response "
816 . "buttons below.",
817 on_activate => sub { 1375 on_activate => sub {
818 my ($entry, $text) = @_; 1376 my ($entry, $text) = @_;
819 1377
820 return unless $text =~ /\S/; 1378 return unless $text =~ /\S/;
821 1379
824 1382
825 0 1383 0
826 }, 1384 },
827 ); 1385 );
828 1386
829 $vbox->add ($self->{options} = new CFClient::UI::VBox); 1387 $vbox->add ($self->{options} = new CFPlus::UI::VBox);
830 1388
831 $self->{bye_button} = new CFClient::UI::Button 1389 $self->{bye_button} = new CFPlus::UI::Button
832 text => "Bye (close)", 1390 text => "Bye (close)",
833 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 1391 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
834 on_activate => sub { $this->destroy; 0 }, 1392 on_activate => sub { $this->destroy; 1 },
835 ; 1393 ;
836 1394
837 $self->update_options; 1395 $self->update_options;
838 1396
839 $self->{token} = $self->{conn}->ext_token; 1397 $self->{id} = "npc-channel-" . $self->{conn}->token;
840 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 1398 $self->{conn}->connect_ext ($self->{id} => sub {
841 $self->{conn}->send ("ext npc_dialog_begin $self->{token} $self->{dx} $self->{dy}"); 1399 $this->feed (@_) if $this;
1400 });
1401
1402 $self->{conn}->send_ext_msg (npc_dialog_begin => $self->{id}, $self->{token});
842 1403
843 $self->{entry}->grab_focus; 1404 $self->{entry}->grab_focus;
844 1405
845 $self->{textview}->add_paragraph ([1, 1, 0, 1], "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n"); 1406 $self->{textview}->add_paragraph ({
1407 fg => [1, 1, 0, 1],
1408 markup => "<small>[starting conversation with <b>$self->{title}</b>]</small>\n\n",
1409 });
846 1410
847 $self->show; 1411 $self->show;
848 $self 1412 $self
849}; 1413};
850 1414
851sub update_options { 1415sub update_options {
852 my ($self) = @_; 1416 my ($self) = @_;
853 1417
854 Scalar::Util::weaken $self; 1418 CFPlus::weaken $self;
855 1419
856 $self->{options}->clear; 1420 $self->{options}->clear;
857 $self->{options}->add ($self->{bye_button}); 1421 $self->{options}->add ($self->{bye_button});
858 1422
859 for my $kw (sort keys %{ $self->{kw} }) { 1423 for my $kw (sort keys %{ $self->{kw} }) {
860 $self->{options}->add (new CFClient::UI::Button 1424 $self->{options}->add (new CFPlus::UI::Button
861 text => $kw, 1425 text => $kw,
862 on_activate => sub { 1426 on_activate => sub {
863 $self->send ($kw); 1427 $self->send ($kw);
864 0 1428 0
865 }, 1429 },
866 ); 1430 );
867 } 1431 }
868} 1432}
869 1433
870sub feed { 1434sub feed {
871 my ($self, $data) = @_; 1435 my ($self, $type, @arg) = @_;
872 1436
873 Scalar::Util::weaken $self; 1437 CFPlus::weaken $self;
874 1438
875 my ($type, $msg) = split / /, $data, 2;
876
877 if ($type eq "msg") { 1439 if ($type eq "update") {
878 my ($msg, @kw) = split /\x00/, $msg; 1440 my (%info) = @arg;
879 $self->{kw}{$_} = 1 for @kw;
880 1441
881 $msg = "\n" . CFClient::UI::Label::escape $msg; 1442 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
882 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1443 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
883 my @link;
884 $msg =~ s{
885 ($match)
886 }{
887 my $kw = $1;
888
889 push @link, new CFClient::UI::Label
890 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
891 can_hover => 1,
892 can_events => 1,
893 padding_x => 0,
894 padding_y => 0,
895 on_button_up => sub {
896 $self->send ($kw);
897 };
898
899 chr 0xfffc
900 }giex;
901 1444
902 $self->{textview}->add_paragraph ([1, 1, 1, 1], [$msg, @link]); 1445 if (exists $info{msg}) {
1446 my $text = "\n" . CFPlus::Protocol::sanitise_xml $info{msg};
1447 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1448 my @link;
1449 $text =~ s{
1450 ($match)
1451 }{
1452 my $kw = $1;
1453
1454 push @link, new CFPlus::UI::Label
1455 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1456 can_hover => 1,
1457 can_events => 1,
1458 padding_x => 0,
1459 padding_y => 0,
1460 on_button_up => sub {
1461 $self->send ($kw);
1462 };
1463
1464 "\x{fffc}"
1465 }giex;
1466
1467 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
903 $self->{textview}->scroll_to_bottom; 1468 $self->{textview}->scroll_to_bottom;
1469 }
1470
904 $self->update_options; 1471 $self->update_options;
905 } else { 1472 } else {
906 $self->destroy; 1473 $self->destroy;
907 } 1474 }
908 1475
910} 1477}
911 1478
912sub send { 1479sub send {
913 my ($self, $msg) = @_; 1480 my ($self, $msg) = @_;
914 1481
915 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); 1482 $self->{textview}->add_paragraph ({ markup => "\n" . CFPlus::asxml $msg });
916 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
917 $self->{textview}->scroll_to_bottom; 1483 $self->{textview}->scroll_to_bottom;
1484
1485 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
918} 1486}
919 1487
920sub destroy { 1488sub destroy {
921 my ($self) = @_; 1489 my ($self) = @_;
922 1490
923 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1491 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
924 1492
1493 if ($self->{conn}) {
925 $self->{conn}->send ("ext npc_dialog_end $self->{token}") if $self->{token}; 1494 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
926 delete $self->{conn}{npc_dialog}; 1495 delete $self->{conn}{npc_dialog};
927 $self->{conn}->disconnect_ext ($self->{token}); 1496 $self->{conn}->disconnect_ext ($self->{id});
1497 }
928 1498
929 $self->SUPER::destroy; 1499 $self->SUPER::destroy;
930} 1500}
931 1501
9321; 15021
1503

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines