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.39 by root, Mon Jun 19 06:50:45 2006 UTC vs.
Revision 1.183 by root, Wed Dec 26 21:03:21 2007 UTC

1package CFClient::Protocol; 1package DC::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 DC;
9use CFClient::UI; 9use DC::DB;
10use DC::UI;
11use DC::Pod;
12use DC::Macro;
13use DC::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) = DC::Pod::section_of $_;
40 my $text = DC::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 DC::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 DC::Texture
76 DC::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 DC::Texture
82 DC::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 DC::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 "DC::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 => $DC::UI::ROOT,
205 tooltip => $DC::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 DC::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}
301
302sub _stat_numdiff {
303 my ($self, $name, $old, $new) = @_;
304
305 my $diff = $new - $old;
306
307 $diff = 0.01 * int $diff * 100;
308
309 0.1 >= abs $diff ? ()
310 : $diff < 0 ? "$name$diff" : "$name+$diff"
311}
312
313sub _stat_skillmaskdiff {
314 my ($self, $name, $old, $new) = @_;
315
316 my $diff = $old ^ $new
317 or return;
318
319 my @diff = map
320 {
321 $diff & $_
322 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
323 : ()
324 }
325 sort { $a <=> $b } keys %{$self->{spell_paths}};
326
327 join "", @diff
328}
329
330# all stats that are chacked against changes
331my @statchange = (
332 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
333 [&CS_STAT_INT => \&_stat_numdiff, "Int"],
334 [&CS_STAT_WIS => \&_stat_numdiff, "Wis"],
335 [&CS_STAT_DEX => \&_stat_numdiff, "Dex"],
336 [&CS_STAT_CON => \&_stat_numdiff, "Con"],
337 [&CS_STAT_CHA => \&_stat_numdiff, "Cha"],
338 [&CS_STAT_POW => \&_stat_numdiff, "Pow"],
339 [&CS_STAT_WC => \&_stat_numdiff, "Wc"],
340 [&CS_STAT_AC => \&_stat_numdiff, "Ac"],
341 [&CS_STAT_DAM => \&_stat_numdiff, "Dam"],
342 [&CS_STAT_SPEED => \&_stat_numdiff, "Speed"],
343 [&CS_STAT_WEAP_SP => \&_stat_numdiff, "WSp"],
344 [&CS_STAT_MAXHP => \&_stat_numdiff, "HP"],
345 [&CS_STAT_MAXSP => \&_stat_numdiff, "Mana"],
346 [&CS_STAT_MAXGRACE => \&_stat_numdiff, "Grace"],
347 [&CS_STAT_WEIGHT_LIM => \&_stat_numdiff, "Weight"],
348 [&CS_STAT_SPELL_ATTUNE => \&_stat_skillmaskdiff, "attuned"],
349 [&CS_STAT_SPELL_REPEL => \&_stat_skillmaskdiff, "repelled"],
350 [&CS_STAT_SPELL_DENY => \&_stat_skillmaskdiff, "denied"],
351 [&CS_STAT_RES_PHYS => \&_stat_numdiff, "phys"],
352 [&CS_STAT_RES_MAG => \&_stat_numdiff, "magic"],
353 [&CS_STAT_RES_FIRE => \&_stat_numdiff, "fire"],
354 [&CS_STAT_RES_ELEC => \&_stat_numdiff, "electricity"],
355 [&CS_STAT_RES_COLD => \&_stat_numdiff, "cold"],
356 [&CS_STAT_RES_CONF => \&_stat_numdiff, "confusion"],
357 [&CS_STAT_RES_ACID => \&_stat_numdiff, "acid"],
358 [&CS_STAT_RES_DRAIN => \&_stat_numdiff, "drain"],
359 [&CS_STAT_RES_GHOSTHIT => \&_stat_numdiff, "ghosthit"],
360 [&CS_STAT_RES_POISON => \&_stat_numdiff, "poison"],
361 [&CS_STAT_RES_SLOW => \&_stat_numdiff, "slow"],
362 [&CS_STAT_RES_PARA => \&_stat_numdiff, "paralyse"],
363 [&CS_STAT_TURN_UNDEAD => \&_stat_numdiff, "turnundead"],
364 [&CS_STAT_RES_FEAR => \&_stat_numdiff, "fear"],
365 [&CS_STAT_RES_DEPLETE => \&_stat_numdiff, "depletion"],
366 [&CS_STAT_RES_DEATH => \&_stat_numdiff, "death"],
367 [&CS_STAT_RES_HOLYWORD => \&_stat_numdiff, "godpower"],
368 [&CS_STAT_RES_BLIND => \&_stat_numdiff, "blind"],
369);
87 370
88sub stats_update { 371sub stats_update {
89 my ($self, $stats) = @_; 372 my ($self, $stats) = @_;
90 373
91 if (my $exp = $stats->{+CS_STAT_EXP64}) { 374 my $prev = $self->{prev_stats} || { };
92 my $diff = $exp - $self->{prev_exp}; 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;
93 $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);
94 if exists $self->{prev_exp} && $diff;
95 $self->{prev_exp} = $exp;
96 } 387 }
97 388
389 if (
390 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
391 ) {
392 my $msg = "<b>stat change</b>: " . (join " ", @diffs);
393 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20);
394 }
395
98 ::update_stats_window ($stats); 396 $self->update_stats_window ($stats, $prev);
397
398 $self->{prev_stats} = { %$stats };
399}
400
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 DC::UI::Label text => "Experience", align => 1),
482 1, 0, (new DC::UI::Label text => "Lvl.", align => 1),
483 2, 0, (new DC::UI::Label text => "Progress", align => 0),
484 3, 0, (new DC::UI::Label text => "Skill", expand => 1),
485 4, 0, (new DC::UI::Label text => "Experience", align => 1),
486 5, 0, (new DC::UI::Label text => "Lvl.", align => 1),
487 6, 0, (new DC::UI::Label text => "Progress", align => 0),
488 7, 0, (new DC::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 = DC::shorten $name, 14;
511 (new DC::UI::Menu
512 items => [
513 ["bind <i>ready_skill $shortname</i> to a key" => sub { DC::Macro::quick_macro ["ready_skill $name"] }],
514 ["bind <i>use_skill $shortname</i> to a key" => sub { DC::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 DC::UI::Label
527 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP),
528
529 # level
530 (new DC::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 DC::UI::ExperienceProgress),
535
536 # label
537 (new DC::UI::Label text => $name, on_button_down => $spell_cb,
538 can_events => 1, can_hover => 1, tooltip => (DC::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 }
99} 568}
100 569
101sub user_send { 570sub user_send {
102 my ($self, $command) = @_; 571 my ($self, $command) = @_;
103 572
573 $self->{record}->($command)
104 if ($self->{record}) { 574 if $self->{record};
105 push @{$self->{record}}, $command;
106 }
107 575
108 $self->logprint ("send: ", $command); 576 $self->logprint ("send: ", $command);
109 $self->send_command ($command); 577 $self->send_command ($command);
110 ::status $command;
111} 578}
112 579
113sub start_record { 580sub record {
114 my ($self) = @_; 581 my ($self, $cb) = @_;
115 582
116 $self->{record} = []; 583 $self->{record} = $cb;
117}
118
119sub stop_record {
120 my ($self) = @_;
121 return delete $self->{record};
122} 584}
123 585
124sub map_scroll { 586sub map_scroll {
125 my ($self, $dx, $dy) = @_; 587 my ($self, $dx, $dy) = @_;
126 588
128} 590}
129 591
130sub feed_map1a { 592sub feed_map1a {
131 my ($self, $data) = @_; 593 my ($self, $data) = @_;
132 594
133 $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 {
134 $self->{map_widget}->update; 623 $self->{map_widget}->update;
624 }
135} 625}
136 626
137sub magicmap { 627sub magicmap {
138 my ($self, $w, $h, $x, $y, $data) = @_; 628 my ($self, $w, $h, $x, $y, $data) = @_;
139 629
146 my $map_info = delete $self->{map_info} 636 my $map_info = delete $self->{map_info}
147 or return; 637 or return;
148 638
149 my ($hash, $x, $y, $w, $h) = @$map_info; 639 my ($hash, $x, $y, $w, $h) = @$map_info;
150 640
151 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 641 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h);
152 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 642 $self->{map_cache_new}{$hash} = \$data;
153 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 643 DC::DB::put $self->{mapcache} => $hash => $data, sub { };
154} 644}
155 645
156sub map_clear { 646sub map_clear {
157 my ($self) = @_; 647 my ($self) = @_;
158 648
161 651
162 $self->{map}->clear; 652 $self->{map}->clear;
163 delete $self->{map_widget}{magicmap}; 653 delete $self->{map_widget}{magicmap};
164} 654}
165 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 DC::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}
166 679
167sub load_map($$$) { 680sub load_map($$$) {
168 my ($self, $hash, $x, $y) = @_; 681 my ($self, $hash, $x, $y) = @_;
169 682
170 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
171 $data = Compress::LZF::decompress $data; 693 my $data = Compress::LZF::decompress $data;
172 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
173 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
174 my $data = $self->{tilecache}->get ($id)
175 or next;
176 694
177 $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;
178 } 698 }
699 };
700
701 if (my $rdata = $self->{map_cache_old}{$hash}) {
702 $cb->($$rdata);
703 } else {
704 DC::DB::get $self->{mapcache} => $hash, $cb;
179 } 705 }
180} 706}
181 707
182# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 708# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
183# (server resource,s latency, bandwidth), so this hack is warranted. 709# (server resource,s latency, bandwidth), so this hack is warranted.
248 774
249 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 775 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
250 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 776 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
251 777
252 } else { 778 } else {
779 my $gen = $self->{map_change_gen};
253 $self->send_mapinfo ("spatial $path$tile", sub { 780 $self->send_mapinfo ("spatial $path$tile", sub {
781 return unless $gen == $self->{map_change_gen};
782
254 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 783 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
255 784
256 return if $mode ne "spatial"; 785 return if $mode ne "spatial";
257 786
258 $x += $self->{map}->ox; 787 $x += $self->{map}->ox;
272 801
273sub map_change { 802sub map_change {
274 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 803 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
275 804
276 $self->flush_map; 805 $self->flush_map;
806
807 ++$self->{map_change_gen};
808 $self->{map_cache_old} = delete $self->{map_cache_new};
277 809
278 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 810 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
279 811
280 my $mapmapw = $self->{mapmap}->{w}; 812 my $mapmapw = $self->{mapmap}->{w};
281 my $mapmaph = $self->{mapmap}->{h}; 813 my $mapmaph = $self->{mapmap}->{h};
298 $self->load_map ($hash, $x, $y); 830 $self->load_map ($hash, $x, $y);
299 $self->flood_fill (0, 0, 0, "", $hash, $flags); 831 $self->flood_fill (0, 0, 0, "", $hash, $flags);
300} 832}
301 833
302sub face_find { 834sub face_find {
835 my ($self, $facenum, $face, $cb) = @_;
836
837 if ($face->{type} == 0) { # FT_FACE
838 my $id = DC::DB::get_tile_id_sync $face->{name};
839
840 $face->{id} = $id;
841 $self->{map}->set_tileid ($facenum => $id);
842
843 DC::DB::get tilecache => $id, $cb;
844
845 } elsif ($face->{type} & 1) { # with metadata
846 DC::DB::get res_meta => $face->{name}, $cb;
847
848 } else { # no metadata
849 DC::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 DC::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 DC::DB::put res_data => $face->{name} => $data, sub { };
871 DC::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 => DC::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 DC::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 {
303 my ($self, $facenum, $face) = @_; 897 my ($self, $facenum, $face) = @_;
304 898
305 my $hash = "$face->{chksum},$face->{name}"; 899 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
900}
306 901
307 my $id = $self->{facemap}->get ($hash); 902sub have_tile {
903 my ($self, $tile, $data) = @_;
308 904
309 unless ($id) { 905 return unless $self->{map};
310 # create new id for face
311 # I love transactions
312 for (1..100) {
313 my $txn = $CFClient::DB_ENV->txn_begin;
314 my $status = $self->{facemap}->db_get (id => $id);
315 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
316 $id = ($id || 64) + 1;
317 if ($self->{facemap}->put (id => $id) == 0
318 && $self->{facemap}->put ($hash => $id) == 0) {
319 $txn->txn_commit;
320 906
321 goto gotid; 907 my $tex = $self->{texture}[$tile] ||=
322 } 908 new DC::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 ? DC::guard {
927 @{$self->{face_cb}{$num}}
928 = grep $_ != $cb,
929 @{$self->{face_cb}{$num}};
323 } 930 }
324 $txn->txn_abort; 931 : ()
325 }
326
327 CFClient::fatal "maximum number of transaction retries reached - database problems?";
328 }
329
330gotid:
331 $face->{id} = $id;
332 $self->{map}->set_face ($facenum => $id);
333 $self->{faceid}[$facenum] = $id;#d#
334
335 my $face = $self->{tilecache}->get ($id);
336
337 if ($face) {
338 #$self->face_prefetch;
339 $face
340 } else {
341 my $tex = $self->{noface};
342 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
343 undef
344 };
345} 932}
346 933
347sub face_update { 934# call in non-void context registers a temporary
935# hook with handle, otherwise its permanent
936sub register_face_handler {
348 my ($self, $facenum, $face) = @_; 937 my ($self, $num, $cb) = @_;
349 938
350 $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes 939 return unless $num;
351 940
352 $self->set_texture ($face->{id} => delete $face->{image}); 941 # invoke if available right now
353} 942 $cb->($self->{face}[$num], 0)
943 unless exists $self->{face}[$num]{loading};
354 944
355sub set_texture { 945 # future changes
356 my ($self, $id, $data) = @_; 946 $self->on_face_change ($num => $cb)
357
358 $self->{texture}[$id] ||= do {
359 my $tex =
360 new_from_image CFClient::Texture
361 $data, minify => 1, mipmap => 1;
362
363 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
364 $self->{map_widget}->update;
365
366 $tex
367 };
368} 947}
369 948
370sub sound_play { 949sub sound_play {
371 my ($self, $x, $y, $soundnum, $type) = @_; 950 my ($self, $type, $face, $dx, $dy, $vol) = @_;
372 951
373 $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
374} 954}
375 955
376my $LAST_QUERY; # server is stupid, stupid, stupid 956my $LAST_QUERY; # server is stupid, stupid, stupid
377 957
378sub query { 958sub query {
382 $LAST_QUERY = $prompt; 962 $LAST_QUERY = $prompt;
383 963
384 $self->{query}-> ($self, $flags, $prompt); 964 $self->{query}-> ($self, $flags, $prompt);
385} 965}
386 966
387sub drawinfo { 967sub sanitise_xml($) {
388 my ($self, $color, $text) = @_; 968 local $_ = shift;
389 969
390 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 = (
391 [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],
392 [1.00, 1.00, 1.00], 1004 [1.00, 1.00, 1.00],
393 [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]
394 [1.00, 0.00, 0.00], 1006 [1.00, 0.00, 0.00],
395 [1.00, 0.54, 0.00], 1007 [1.00, 0.54, 0.00],
396 [0.11, 0.56, 1.00], 1008 [0.11, 0.56, 1.00],
397 [0.93, 0.46, 0.00], 1009 [0.93, 0.46, 0.00],
398 [0.18, 0.54, 0.34], 1010 [0.18, 0.54, 0.34],
399 [0.56, 0.73, 0.56], 1011 [0.56, 0.73, 0.56],
400 [0.80, 0.80, 0.80], 1012 [0.80, 0.80, 0.80],
401 [0.55, 0.41, 0.13], 1013 [0.75, 0.61, 0.20],
402 [0.99, 0.77, 0.26], 1014 [0.99, 0.77, 0.26],
403 [0.74, 0.65, 0.41], 1015 [0.74, 0.65, 0.41],
404 ); 1016);
405 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 {
406 $self->logprint ("info: ", $text); 1029 $self->logprint ("msg: ", $text);
1030 return if $color < 0; # negative color == ignore if not understood
407 1031
408 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 1032 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0];
409 1033
410 # 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
411 $text =~ s/(?<=\S)\n(?=\w)/ /g; 1036 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
412 1037
413 $text = CFClient::UI::Label::escape $text; 1038 for (split /\n/, $text) {
414 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 1039 ::message ({
415 $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 });
416 1046
417 $self->{logview}->add_paragraph ($color[$color], 1047 $color &= ~NDI_CLEAR; # only clear once for multiline messages
418 join "\n", map "$time $_", split /\n/, $text); 1048 # actually, this is an ugly design. _we_ should control the channels,
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 }
419 1053
420 $self->{statusbox}->add ($text, 1054 $self->{statusbox}->add ($text,
421 group => $text, 1055 group => $text,
422 fg => $color[$color], 1056 fg => $fg,
423 timeout => $color >= 2 ? 60 : 10, 1057 timeout => $color >= 2 ? 180 : 10,
424 tooltip_font => $::FONT_FIXED, 1058 tooltip_font => $::FONT_FIXED,
425 ); 1059 ) if $type eq "info";
426} 1060 }
427
428sub drawextinfo {
429 my ($self, $color, $type, $subtype, $message) = @_;
430
431 $self->drawinfo ($color, $message);
432} 1061}
433 1062
434sub spell_add { 1063sub spell_add {
435 my ($self, $spell) = @_; 1064 my ($self, $spell) = @_;
436 1065
437 # 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
438 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 1067 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
439 $spell->{message} =~ s/\n+$//; 1068 $spell->{message} =~ s/\n+$//;
440 $spell->{message} ||= "Server did not provide a description for this spell."; 1069 $spell->{message} ||= "Server did not provide a description for this spell.";
441 1070
442 $::SETUP_SPELLS->add_spell ($spell); 1071 $::SPELL_LIST->add_spell ($spell);
443 1072
444 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 1073 $self->{map_widget}->add_command ("invoke $spell->{name}", DC::asxml $spell->{message});
445 $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); 1074 $self->{map_widget}->add_command ("cast $spell->{name}", DC::asxml $spell->{message});
446} 1075}
447 1076
448sub spell_delete { 1077sub spell_delete {
449 my ($self, $spell) = @_; 1078 my ($self, $spell) = @_;
1079
450 $::SETUP_SPELLS->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});
451} 1088}
452 1089
453sub addme_success { 1090sub addme_success {
454 my ($self) = @_; 1091 my ($self) = @_;
455 1092
456 my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub {
457 my ($pom) = @_;
458
459 my %skill_help; 1093 my %skill_help;
460 1094
461 for my $head2 ($pom->head1->[3]->head2) { 1095 for my $node (DC::Pod::find skill_description => "*") {
462 $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content; 1096 my (undef, @par) = DC::Pod::section_of $node;
463 } 1097 $skill_help{$node->{kw}[0]} = DC::Pod::as_label @par;
464
465 \%skill_help
466 }; 1098 };
467 1099
468 for my $skill (values %{$self->{skill_info}}) { 1100 for my $skill (values %{$self->{skill_info}}) {
469 $self->{map_widget}->add_command ("ready_skill $skill", 1101 $self->{map_widget}->add_command ("ready_skill $skill",
470 (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") 1102 (DC::asxml "Ready the skill '$skill'\n\n")
471 . $skill_help->{$skill}); 1103 . $skill_help{$skill});
472 $self->{map_widget}->add_command ("use_skill $skill", 1104 $self->{map_widget}->add_command ("use_skill $skill",
473 (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") 1105 (DC::asxml "Immediately use the skill '$skill'\n\n")
474 . $skill_help->{$skill}); 1106 . $skill_help{$skill});
475 } 1107 }
476} 1108}
477 1109
478sub eof { 1110sub eof {
479 my ($self) = @_; 1111 my ($self) = @_;
481 $self->{map_widget}->clr_commands; 1113 $self->{map_widget}->clr_commands;
482 1114
483 ::stop_game (); 1115 ::stop_game ();
484} 1116}
485 1117
486sub image_info {
487 my ($self, $numfaces) = @_;
488
489 $self->{num_faces} = $numfaces;
490 $self->{face_prefetch} = [1 .. $numfaces];
491 $self->face_prefetch;
492}
493
494sub face_prefetch {
495 my ($self) = @_;
496
497 return unless $::CFG->{face_prefetch};
498
499 if ($self->{num_faces}) {
500 return if @{ $self->{send_queue} || [] };
501 my $todo = @{ $self->{face_prefetch} }
502 or return;
503
504 my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
505
506 $self->send ("requestinfo image_sums $face $face");
507
508 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
509 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
510 } elsif (!exists $self->{num_faces}) {
511 $self->send ("requestinfo image_info");
512
513 $self->{num_faces} = 0;
514
515 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
516 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
517 }
518}
519
520sub update_floorbox { 1118sub update_floorbox {
521 $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { 1119 $DC::UI::ROOT->on_refresh ($::FLOORBOX => sub {
522 return unless $::CONN; 1120 return unless $::CONN;
523 1121
524 $::FLOORBOX->clear; 1122 $::FLOORBOX->clear;
525 1123
1124 my @add;
1125
526 my $row; 1126 my $row;
527 for (@{ $::CONN->{container}{0} }) { 1127 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
528 if ($row < 6) { 1128 if ($row < 6) {
529 local $_->{face_widget}; # hack to force recreation of widget 1129 local $_->{face_widget}; # hack to force recreation of widget
530 local $_->{desc_widget}; # hack to force recreation of widget 1130 local $_->{desc_widget}; # hack to force recreation of widget
531 CFClient::Item::update_widgets $_; 1131 DC::Item::update_widgets $_;
532 1132
1133 push @add,
533 $::FLOORBOX->add (0, $row, $_->{face_widget}); 1134 0, $row, $_->{face_widget},
534 $::FLOORBOX->add (1, $row, $_->{desc_widget}); 1135 1, $row, $_->{desc_widget};
535 1136
536 $row++; 1137 $row++;
537 } else { 1138 } else {
538 $::FLOORBOX->add (1, $row, new CFClient::UI::Button 1139 push @add, 1, $row, new DC::UI::Button
539 text => "More...", 1140 text => "More...",
540 on_activate => sub { $::INV_WINDOW->toggle_visibility; 0 }, 1141 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
541 ); 1142 ;
542 last; 1143 last;
543 } 1144 }
544 } 1145 }
1146 if ($::CONN->{open_container}) {
1147 push @add, 1, $row++, new DC::UI::Button
1148 text => "Close container",
1149 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
1150 ;
1151 }
1152
1153 $::FLOORBOX->add_at (@add);
545 }); 1154 });
546 1155
547 $::WANT_REFRESH++; 1156 $::WANT_REFRESH = 1;
548} 1157}
549 1158
550sub set_opencont { 1159sub set_opencont {
551 my ($conn, $tag, $name) = @_; 1160 my ($conn, $tag, $name) = @_;
552 $conn->{open_container} = $tag; 1161 $conn->{open_container} = $tag;
1162 update_floorbox;
553 1163
554 $::INV_RIGHT_HB->clear (); 1164 $::INVR_HB->clear ();
555 $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name); 1165 $::INVR_HB->add (new DC::UI::Label align => 0, expand => 1, text => $name);
556 1166
557 if ($tag != 0) { # Floor isn't closable, is it? 1167 if ($tag != 0) { # Floor isn't closable, is it?
558 $::INV_RIGHT_HB->add (new CFClient::UI::Button 1168 $::INVR_HB->add (new DC::UI::Button
559 text => "Close container", 1169 text => "Close container",
560 tooltip => "Close the currently open container (if one is open)", 1170 tooltip => "Close the currently open container (if one is open)",
561 on_activate => sub { 1171 on_activate => sub {
562 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 1172 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
563 if $tag != 0; 1173 if $tag != 0;
564 #if $CONN->{open_container} != 0;
565 0 1174 0
566 }, 1175 },
567 ); 1176 );
568 } 1177 }
569 1178
570 $::INVR->set_items ($conn->{container}{$tag}); 1179 $::INVR->set_items ($conn->{container}{$tag});
571} 1180}
572 1181
573sub update_container { 1182sub update_containers {
574 my ($tag) = @_; 1183 my ($self) = @_;
575 1184
576 $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}}) 1185 $DC::UI::ROOT->on_refresh ("update_containers_$self" => sub {
1186 my $todo = delete $self->{update_container}
1187 or return;
1188
1189 for my $tag (keys %$todo) {
1190 update_floorbox if $tag == 0 or $tag == $self->{open_container};
1191 if ($tag == 0) {
1192 $::INVR->set_items ($self->{container}{0})
577 if $tag == $::CONN->{open_container}; 1193 if $tag == $self->{open_container};
1194 } elsif ($tag == $self->{player}{tag}) {
1195 $::INV->set_items ($self->{container}{$tag})
1196 } else {
1197 $::INVR->set_items ($self->{container}{$tag})
1198 if $tag == $self->{open_container};
1199 }
1200 }
1201 });
578} 1202}
579 1203
580sub container_add { 1204sub container_add {
581 my ($self, $tag, $items) = @_; 1205 my ($self, $tag, $items) = @_;
582 1206
583 #d# print "container_add: container $tag ($self->{player}{tag})\n"; 1207 $self->{update_container}{$tag}++;
584 1208 $self->update_containers;
585 if ($tag == 0) {
586 update_floorbox;
587 update_container (0);
588 } elsif ($tag == $self->{player}{tag}) {
589 $::INV->set_items ($self->{container}{$self->{player}{tag}})
590 } else {
591 update_container ($tag);
592 }
593
594 # $self-<{player}{tag} => player inv
595 #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
596} 1209}
597 1210
598sub container_clear { 1211sub container_clear {
599 my ($self, $tag) = @_; 1212 my ($self, $tag) = @_;
600 1213
601 #d# print "container_clear: container $tag ($self->{player}{tag})\n"; 1214 $self->{update_container}{$tag}++;
602 1215 $self->update_containers;
603 if ($tag == 0) {
604 update_floorbox;
605 update_container (0);
606 } elsif ($tag == $self->{player}{tag}) {
607 $::INV->set_items ($self->{container}{$tag})
608 } else {
609 update_container ($tag);
610 }
611
612# use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
613} 1216}
614 1217
615sub item_delete { 1218sub item_delete {
616 my ($self, @items) = @_; 1219 my ($self, @items) = @_;
617 1220
1221 $self->{update_container}{$_->{container}}++
618 for (@items) { 1222 for @items;
619 #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
620
621 if ($_->{container} == 0) {
622 update_floorbox;
623 update_container ($_->{tag});
624 } elsif ($_->{container} == $self->{player}{tag}) {
625 $::INV->set_items ($self->{container}{$self->{player}{tag}})
626 } else {
627 update_container ($_->{container});
628 }
629 } 1223
1224 $self->update_containers;
630} 1225}
631 1226
632sub item_update { 1227sub item_update {
633 my ($self, $item) = @_; 1228 my ($self, $item) = @_;
634 1229
635 #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";
636 1231
637 if ($item->{tag} == $self->{player}{tag}) {
638 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
639 return;
640 }
641
642 CFClient::Item::update_widgets $item; 1232 DC::Item::update_widgets $item;
643 1233
644 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1234 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
645 set_opencont ($::CONN, 0, "Floor"); 1235 set_opencont ($::CONN, 0, "Floor");
646 1236
647 } elsif ($item->{flags} & F_OPEN) { 1237 } elsif ($item->{flags} & F_OPEN) {
648 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); 1238 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item);
1239
649 } else { 1240 } else {
650 if ($item->{container} == 0) { 1241 $self->{update_container}{$item->{container}}++;
651 update_floorbox;
652 update_container (0); 1242 $self->update_containers;
653 } elsif ($item->{container} == $self->{player}{tag}) {
654 $::INV->set_items ($self->{container}{$item->{container}})
655 }
656 } 1243 }
657} 1244}
658 1245
659sub player_update { 1246sub player_update {
660 my ($self, $player) = @_; 1247 my ($self, $player) = @_;
661 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
662 $::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);
663} 1261}
664 1262
665sub update_server_info { 1263sub update_server_info {
666 my ($self) = @_; 1264 my ($self) = @_;
667 1265
670 $::SERVER_INFO->set_markup ( 1268 $::SERVER_INFO->set_markup (
671 "server <tt>$self->{host}:$self->{port}</tt>\n" 1269 "server <tt>$self->{host}:$self->{port}</tt>\n"
672 . "protocol version <tt>$self->{version}</tt>\n" 1270 . "protocol version <tt>$self->{version}</tt>\n"
673 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1271 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
674 . "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"
675 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1277 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
676 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1278 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
677 . "map size $self->{mapw}×$self->{maph}\n" 1279 . "map size $self->{mapw}×$self->{maph}\n"
678 ); 1280 );
1281
679} 1282}
680 1283
681sub logged_in { 1284sub logged_in {
682 my ($self) = @_; 1285 my ($self) = @_;
683 1286
684 $self->send_ext_req (cfplus_support => "1", sub { 1287 $self->send_ext_req (cfplus_support => version => 2, sub {
1288 my (%msg) = @_;
1289
685 $self->{cfplus_ext} = $_[0]; 1290 $self->{cfplus_ext} = $msg{version};
686 $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
687 }); 1303 });
688 1304
689 $self->update_server_info; 1305 $self->update_server_info;
690 1306
691 $self->send_command ("output-sync $::CFG->{output_sync}"); 1307 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
692 $self->send_command ("output-count $::CFG->{output_count}");
693 $self->send_command ("pickup $::CFG->{pickup}"); 1308 $self->send_command ("pickup $::CFG->{pickup}");
694} 1309}
695 1310
696sub lookat { 1311sub lookat {
697 my ($self, $x, $y) = @_; 1312 my ($self, $x, $y) = @_;
698 1313
699 if ($self->{cfplus_ext}) { 1314 if ($self->{cfplus_ext}) {
700 $self->send_ext_req (lookat => "$x $y", sub { 1315 $self->send_ext_req (lookat => $x, $y, sub {
701 my %res = split /\x00/, $_[0]; 1316 my (%msg) = @_;
702 1317
703 if (exists $res{npc_dialog}) { 1318 if (exists $msg{npc_dialog}) {
704 # start npc chat dialog 1319 # start npc chat dialog
705 $self->{npc_dialog} = new CFClient::NPCDialog:: 1320 $self->{npc_dialog} = new DC::NPCDialog::
706 dx => $x, 1321 token => $msg{npc_dialog},
707 dy => $y,
708 title => "$res{npc_dialog} (NPC)", 1322 title => "$msg{npc_dialog}[0] (NPC)",
709 conn => $self, 1323 conn => $self,
710 ; 1324 ;
711 } 1325 }
712 }); 1326 });
713 } 1327 }
716} 1330}
717 1331
718sub destroy { 1332sub destroy {
719 my ($self) = @_; 1333 my ($self) = @_;
720 1334
721 $self->{npc_dialog}->destroy 1335 (delete $self->{npc_dialog})->destroy
722 if $self->{npc_dialog}; 1336 if $self->{npc_dialog};
723 1337
724 $self->SUPER::destroy; 1338 $self->SUPER::destroy;
725}
726 1339
1340 %$self = ();
1341}
1342
727package CFClient::NPCDialog; 1343package DC::NPCDialog;
728 1344
729our @ISA = 'CFClient::UI::FancyFrame'; 1345our @ISA = 'DC::UI::Toplevel';
730 1346
731sub new { 1347sub new {
732 my $class = shift; 1348 my $class = shift;
733 1349
734 my $self = $class->SUPER::new ( 1350 my $self = $class->SUPER::new (
737 name => "npc_dialog", 1353 name => "npc_dialog",
738 force_w => $::WIDTH * 0.7, 1354 force_w => $::WIDTH * 0.7,
739 force_h => $::HEIGHT * 0.7, 1355 force_h => $::HEIGHT * 0.7,
740 title => "NPC Dialog", 1356 title => "NPC Dialog",
741 kw => { hi => 0, yes => 0, no => 0 }, 1357 kw => { hi => 0, yes => 0, no => 0 },
1358 has_close_button => 1,
742 @_, 1359 @_,
743 ); 1360 );
744 1361
745 Scalar::Util::weaken (my $this = $self); 1362 DC::weaken (my $this = $self);
1363
1364 $self->connect (delete => sub { $this->destroy; 1 });
746 1365
747 # better use a pane... 1366 # better use a pane...
748 $self->add (my $hbox = new CFClient::UI::HBox); 1367 $self->add (my $hbox = new DC::UI::HBox);
749 $hbox->add ($self->{textview} = new CFClient::UI::TextScroller expand => 1); 1368 $hbox->add ($self->{textview} = new DC::UI::TextScroller expand => 1);
750 1369
751 $hbox->add (my $vbox = new CFClient::UI::VBox); 1370 $hbox->add (my $vbox = new DC::UI::VBox);
752 1371
753 $vbox->add (new CFClient::UI::Label text => "Message Entry:"); 1372 $vbox->add (new DC::UI::Label text => "Message Entry:");
754 $vbox->add ($self->{entry} = new CFClient::UI::Entry 1373 $vbox->add ($self->{entry} = new DC::UI::Entry
755 tooltip => "Enter a message you want to tell the NPC and press <b>return</b>.\n\n" 1374 tooltip => "#npc_message_entry",
756 . "Sometimes you have to tell an NPC something you cannot find out during "
757 . "a normal conversation (such as a password). In those cases you have to use "
758 . "this text entry. You can also enter responses manually instead of using the response "
759 . "buttons below.",
760 on_activate => sub { 1375 on_activate => sub {
761 my ($entry, $text) = @_; 1376 my ($entry, $text) = @_;
762 1377
763 return unless $text =~ /\S/; 1378 return unless $text =~ /\S/;
764 1379
767 1382
768 0 1383 0
769 }, 1384 },
770 ); 1385 );
771 1386
772 $vbox->add ($self->{options} = new CFClient::UI::VBox); 1387 $vbox->add ($self->{options} = new DC::UI::VBox);
773 1388
774 $self->{bye_button} = new CFClient::UI::Button 1389 $self->{bye_button} = new DC::UI::Button
775 text => "Bye (close)", 1390 text => "Bye (close)",
776 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.",
777 on_activate => sub { $this->destroy; 0 }, 1392 on_activate => sub { $this->destroy; 1 },
778 ; 1393 ;
779 1394
780 $self->update_options; 1395 $self->update_options;
781 1396
782 $self->{token} = $self->{conn}->ext_token; 1397 $self->{id} = "npc-channel-" . $self->{conn}->token;
783 $self->{conn}->connect_ext ($self->{token} => sub { $this->feed (@_) }); 1398 $self->{conn}->connect_ext ($self->{id} => sub {
784 $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});
785 1403
786 $self->{entry}->grab_focus; 1404 $self->{entry}->grab_focus;
787 1405
788 $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 });
789 1410
790 $self->show; 1411 $self->show;
791 $self 1412 $self
792}; 1413};
793 1414
794sub update_options { 1415sub update_options {
795 my ($self) = @_; 1416 my ($self) = @_;
796 1417
797 Scalar::Util::weaken $self; 1418 DC::weaken $self;
798 1419
799 $self->{options}->clear; 1420 $self->{options}->clear;
800 $self->{options}->add ($self->{bye_button}); 1421 $self->{options}->add ($self->{bye_button});
801 1422
802 for my $kw (sort keys %{ $self->{kw} }) { 1423 for my $kw (sort keys %{ $self->{kw} }) {
803 $self->{options}->add (new CFClient::UI::Button 1424 $self->{options}->add (new DC::UI::Button
804 text => $kw, 1425 text => $kw,
805 on_activate => sub { 1426 on_activate => sub {
806 $self->send ($kw); 1427 $self->send ($kw);
807 0 1428 0
808 }, 1429 },
809 ); 1430 );
810 } 1431 }
811} 1432}
812 1433
813sub feed { 1434sub feed {
814 my ($self, $data) = @_; 1435 my ($self, $type, @arg) = @_;
815 1436
816 my ($type, $msg) = split / /, $data, 2; 1437 DC::weaken $self;
817 1438
818 if ($type eq "msg") { 1439 if ($type eq "update") {
819 my ($msg, @kw) = split /\x00/, $msg; 1440 my (%info) = @arg;
820 $self->{kw}{$_} = 1 for @kw;
821 1441
822 $msg = CFClient::UI::Label::escape $msg; 1442 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
823 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1443 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
824 $msg =~ s/($match)/<span foreground='#c0c0ff' underline='none'>$1<\/span>/gi; # underline when http-ready, huh.
825 1444
826 $self->{textview}->add_paragraph ([1, 1, 1, 1], "\n$msg"); 1445 if (exists $info{msg}) {
1446 my $text = "\n" . DC::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 DC::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 });
1468 $self->{textview}->scroll_to_bottom;
1469 }
1470
827 $self->update_options; 1471 $self->update_options;
828 } else { 1472 } else {
829 $self->destroy; 1473 $self->destroy;
830 } 1474 }
831 1475
833} 1477}
834 1478
835sub send { 1479sub send {
836 my ($self, $msg) = @_; 1480 my ($self, $msg) = @_;
837 1481
1482 $self->{textview}->add_paragraph ({ markup => "\n" . DC::asxml $msg });
1483 $self->{textview}->scroll_to_bottom;
1484
838 $self->{conn}->send ("ext npc_dialog_tell $self->{token} $msg"); 1485 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
839 $self->{textview}->add_paragraph ([1, 1, 0, 1], "\n" . CFClient::UI::Label::escape $msg);
840} 1486}
841 1487
842sub destroy { 1488sub destroy {
843 my ($self) = @_; 1489 my ($self) = @_;
844 1490
845 #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
846 1492
1493 if ($self->{conn}) {
1494 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
847 delete $self->{conn}{npc_dialog}; 1495 delete $self->{conn}{npc_dialog};
848 $self->{conn}->disconnect_ext ($self->{token}); 1496 $self->{conn}->disconnect_ext ($self->{id});
1497 }
849 1498
850 $self->SUPER::destroy; 1499 $self->SUPER::destroy;
851} 1500}
852 1501
8531; 15021
1503

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines