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.183 by root, Wed Dec 26 21:03:21 2007 UTC vs.
Revision 1.222 by root, Sun Nov 4 02:14:11 2012 UTC

1package DC::Protocol; 1package DC::Protocol;
2 2
3use utf8; 3use common::sense;
4use strict; 4
5use Guard ();
5 6
6use Deliantra::Protocol::Constants; 7use Deliantra::Protocol::Constants;
7 8
8use DC; 9use DC;
9use DC::DB; 10use DC::DB;
12use DC::Macro; 13use DC::Macro;
13use DC::Item; 14use DC::Item;
14 15
15use base 'Deliantra::Protocol::Base'; 16use base 'Deliantra::Protocol::Base';
16 17
18our $TEX_DIALOGUE = new_from_resource DC::Texture
19 "dialogue.png", minify => 1, mipmap => 1;
20
21our $TEX_NOFACE = new_from_resource DC::Texture
22 "noface.png", minify => 1, mipmap => 1, wrap => 1;
23
24sub MIN_TEXTURE_UNUSED() { 1 }#d#
25
17sub new { 26sub new {
18 my ($class, %arg) = @_; 27 my ($class, %arg) = @_;
19 28
20 my $self = $class->SUPER::new (%arg, 29 my $self = $class->SUPER::new (%arg,
21 setup_req => { 30 setup_req => {
22 extmap => 1, 31 extmap => 1,
23 excmd => 1,
24 widget => 1, 32 widget => 2,
25 %{$arg{setup_req} || {}}, 33 %{$arg{setup_req} || {}},
26 }, 34 },
27 ); 35 );
28 36
37 $self->update_fx_want;
38
39 my $guard = $self->addme_guard;
40 $self->send_exti_req (resource => "exp_table", sub {
41 my ($idx) = @_;
42
43 $self->register_face_handler ($idx, sub {
44 my ($face) = @_;
45
46 undef $guard;
47 $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
48 $_->() for values %{ $self->{on_exp_update} };
49 });
50
51 ()
52 });
53
54 my $guard = $self->addme_guard;
55 $self->send_exti_req (resource => "skill_info", sub {
56 my ($idx) = @_;
57
58 $self->register_face_handler ($idx, sub {
59 my ($face) = @_;
60
61 undef $guard;
62 my $info = $self->{json_coder}->decode (delete $face->{data});
63 $self->{skill_info} = { map { CS_STAT_SKILLINFO + $_ => $info->[$_][0] } 0 .. $#$info };
64 });
65
66 ()
67 });
68
69 my $guard = $self->addme_guard;
70 $self->send_exti_req (resource => "spell_paths", sub {
71 my ($idx) = @_;
72
73 $self->register_face_handler ($idx, sub {
74 my ($face) = @_;
75
76 undef $guard;
77 my $info = $self->{json_coder}->decode (delete $face->{data});
78 $self->{spell_paths} = { map { (1 << $_) => $info->[$_][0] } 0 .. $#$info };
79 });
80
81 ()
82 });
83
29 $self->{map_widget}->clr_commands; 84 $self->{map_widget}->clr_commands;
30 85
31 my @cmd_help = map { 86 my @cmd_help = map {
32 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 87 $_->[DC::Pod::N_KW][0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
33 or die "unparseable command help: $_->{kw}[0]"; 88 or die "unparseable command help: $_->[DC::Pod::N_KW][0]";
34 89
35 my $cmd = $1; 90 my $cmd = $1;
36 my @args = split /\|/, $2; 91 my @args = split /\|/, $2;
37 @args = (".*") unless @args; 92 @args = (".*") unless @args;
38 93
43 for @args; 98 for @args;
44 99
45 map ["$cmd$_", $text], 100 map ["$cmd$_", $text],
46 sort { (length $a) <=> (length $b) } 101 sort { (length $a) <=> (length $b) }
47 @args 102 @args
48 } sort { $a->{par} <=> $b->{par} } 103 } sort { $a->[DC::Pod::N_PAR] <=> $b->[DC::Pod::N_PAR] }
49 DC::Pod::find command => "*"; 104 DC::Pod::find command => "*";
50 105
51 $self->{json_coder} 106 $self->{json_coder}
52 ->convert_blessed 107 ->convert_blessed
53 ->filter_json_single_key_object ("\fw" => sub { 108 ->filter_json_single_key_object ("\fw" => sub {
64 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { 119 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
65 for my $ws (values %{delete $self->{widgetset} || {}}) { 120 for my $ws (values %{delete $self->{widgetset} || {}}) {
66 $_->destroy 121 $_->destroy
67 for values %{delete $ws->{w} || {}}; 122 for values %{delete $ws->{w} || {}};
68 } 123 }
124
125 delete $self->{items};
126 $::INV->clear;
127 $::INVR->clear;
128 $::INVR_HB->clear;
129 $::FLOORBOX->clear;
69 }); 130 });
70 131
71 $self->{map_widget}->add_command (@$_) 132 $self->{map_widget}->add_command (@$_)
72 for @cmd_help; 133 for @cmd_help;
73 134
74 { 135 {
75 $self->{dialogue} = my $tex = new_from_file DC::Texture 136 $self->{dialogue} = my $tex = $TEX_DIALOGUE;
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}}); 137 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
78 } 138 }
79 139
80 { 140 {
81 $self->{noface} = my $tex = new_from_file DC::Texture 141 $self->{noface} = my $tex = $TEX_NOFACE;
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}}); 142 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
84 } 143 }
144
145# $self->{expire_count} = DC::DB::FIRST_TILE_ID; # minimum non-fixed tile id
146# $self->{expire_w} = AE::timer 1, 1, sub {
147# my $count = (int @{ $self->{texture} } / MIN_TEXTURE_UNUSED) || 1;
148#
149# for ($self->{map}->expire_textures ($self->{expire_count}, $count)) {
150# warn DC::SvREFCNT $self->{texture}[$_];
151# $self->{texture}[$_]->unload;
152# warn "expire texture $_\n";#d#
153# }
154#
155# ($self->{expire_count} += $count) < @{ $self->{texture} }
156# or $self->{expire_count} = DC::DB::FIRST_TILE_ID;
157# warn "count is $count\n";#d#
158# };
85 159
86 $self->{open_container} = 0; 160 $self->{open_container} = 0;
87 161
88 # per server 162 # per server
89 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 163 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
102} 176}
103 177
104sub ext_capabilities { 178sub ext_capabilities {
105 my ($self, %cap) = @_; 179 my ($self, %cap) = @_;
106 180
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}) { 181 if (my $ts = $cap{tileset}) {
124 if (my ($default) = grep $_->[2] & 1, @$ts) { 182 if (my ($default) = grep $_->[2] & 1, @$ts) {
125 $self->{tileset} = $default; 183 $self->{tileset} = $default;
126 $self->{tilesize} = $default->[3]; 184 $self->{tilesize} = $default->[3];
127 $self->setup_req (tileset => $default->[0]); 185 $self->setup_req (tileset => $default->[0]);
193 "DC::UI::$class"->new (%$args) 251 "DC::UI::$class"->new (%$args)
194 } 252 }
195 ); 253 );
196} 254}
197 255
256# widgetset create template
257sub ext_ws_ct {
258 my ($self, $ws, $type, $template, $done_cb, $cfg) = @_;
259
260 $done_cb ||= sub { };
261
262 my $parse_list; $parse_list = sub {
263 my ($list) = @_;
264 my @w;
265
266 while (@$list) {
267 my ($class, $args) = splice @$list, 0, 2;
268 my $name = delete $args->{s_id};
269 my $cl = delete $args->{s_cl};
270 my $cfg = delete $cfg->{$name};
271 my $id = delete $cfg->{id};
272 my $w = eval { "DC::UI::$class"->new (%$args, %{ $cfg || {} }) }
273 or next;
274
275 $self->widget_associate ($ws, $id, $w)
276 if $id;
277
278 $w->add ($parse_list->($cl))
279 if $cl;
280
281 push @w, $w;
282 }
283
284 @w
285 };
286
287 # either array reference, or face #
288 if ($type eq "inline") {
289 $done_cb->();
290 $parse_list->($template);
291 } elsif ($type eq "face") {
292 my $handler; $handler = $self->register_face_handler ($template, sub {
293 my ($face) = @_;
294
295 undef $handler;
296 $done_cb->();
297 $parse_list->($self->{json_coder}->decode ($face->{data}));
298 });
299 } else {
300 $done_cb->(0);
301 }
302}
303
198# widgetset associate 304# widgetset associate
199sub ext_ws_a { 305sub ext_ws_a {
200 my ($self, %ass) = @_; 306 my ($self, %ass) = @_;
201 307
202 # everything that has a name, wether conceivably useful or not 308 # everything that has a name, wether conceivably useful or not
203 my %wkw = ( 309 my %wkw = (
204 root => $DC::UI::ROOT, 310 root => $DC::UI::ROOT,
205 tooltip => $DC::UI::TOOLTIP, 311 tooltip => $DC::UI::TOOLTIP,
206 312
207 mapwidget => $::MAPWIDGET, 313 mapwidget => $::MAPWIDGET,
314 menubar => $::MENUBAR,
315 menupopup => $::MENUPOPUP,
316 pickup_enable => $::PICKUP_ENABLE,
208 buttonbar => $::BUTTONBAR, 317 buttonbar => $::BUTTONBAR,
209 metaserver => $::METASERVER, 318 metaserver => $::METASERVER,
210 buttonbar => $::BUTTONBAR, 319 buttonbar => $::BUTTONBAR,
211 login_button => $::LOGIN_BUTTON, 320 login_button => $::LOGIN_BUTTON,
212 quit_dialog => $::QUIT_DIALOG, 321 quit_dialog => $::QUIT_DIALOG,
228 spell_list => $::SPELL_LIST, 337 spell_list => $::SPELL_LIST,
229 338
230 floorbox => $::FLOORBOX, 339 floorbox => $::FLOORBOX,
231 help_window => $::HELP_WINDOW, 340 help_window => $::HELP_WINDOW,
232 message_window => $::MESSAGE_WINDOW, 341 message_window => $::MESSAGE_WINDOW,
342 message_dist => $::MESSAGE_DIST,
233 statusbox => $::SDTATUSBOX, 343 statusbox => $::STATUSBOX,
234 344
235 inv => $::INV, 345 inv => $::INV,
236 invr => $::INVR, 346 invr => $::INVR,
237 invr_hb => $::INVR_HB, 347 invr_hb => $::INVR_HB,
238 ); 348 );
286 396
287# message window 397# message window
288sub ext_channel_info { 398sub ext_channel_info {
289 my ($self, $info) = @_; 399 my ($self, $info) = @_;
290 $self->{channels}->{$info->{id}} = $info; 400 $self->{channels}->{$info->{id}} = $info;
291 $::MESSAGE_WINDOW->add_channel ($info); 401 $::MESSAGE_DIST->add_channel ($info);
292} 402}
293 403
294############################################################################# 404#############################################################################
295 405
296sub logprint { 406sub logprint {
322 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_}) 432 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
323 : () 433 : ()
324 } 434 }
325 sort { $a <=> $b } keys %{$self->{spell_paths}}; 435 sort { $a <=> $b } keys %{$self->{spell_paths}};
326 436
327 join "", @diff 437 "\u$name: " . (join ", ", @diff)
328} 438}
329 439
330# all stats that are chacked against changes 440# all stats that are chacked against changes
331my @statchange = ( 441my @statchange = (
332 [&CS_STAT_STR => \&_stat_numdiff, "Str"], 442 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
375 485
376 if (my @diffs = 486 if (my @diffs =
377 ( 487 (
378 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()), 488 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
379 map { 489 map {
380 $stats->{$_} && $prev->{$_} 490 $stats->{$_} && $prev->{$_}
381 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : () 491 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
382 } sort { $a <=> $b } keys %{$self->{skill_info}} 492 } sort { $a <=> $b } keys %{$self->{skill_info}}
383 ) 493 )
384 ) { 494 ) {
385 my $msg = join " ", @diffs; 495 my $msg = join " ", @diffs;
387 } 497 }
388 498
389 if ( 499 if (
390 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange 500 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
391 ) { 501 ) {
392 my $msg = "<b>stat change</b>: " . (join " ", @diffs); 502 my $msg = "<b>stat change</b>: " . (join " ", map "($_)", @diffs);
393 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20); 503 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20);
394 } 504 }
395 505
396 $self->update_stats_window ($stats, $prev); 506 $self->update_stats_window ($stats, $prev);
397 507
435 545
436 $::GAUGES->{hp} ->set_value ($hp, $hp_m); 546 $::GAUGES->{hp} ->set_value ($hp, $hp_m);
437 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 547 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
438 $::GAUGES->{food} ->set_value ($fo, $fo_m); 548 $::GAUGES->{food} ->set_value ($fo, $fo_m);
439 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 549 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
440 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 550 $::GAUGES->{exp} ->set_label ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))#d#
441 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 551 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
442 $::GAUGES->{prg} ->set_value ($stats->{+CS_STAT_LEVEL}, $stats->{+CS_STAT_EXP64}); 552 $::GAUGES->{exp} ->set_value ($stats->{+CS_STAT_LEVEL}, $stats->{+CS_STAT_EXP64});
443 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE}); 553 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
444 my $title = $stats->{+CS_STAT_TITLE}; 554 my $title = $stats->{+CS_STAT_TITLE};
445 $title =~ s/^Player: //; 555 $title =~ s/^Player: //;
446 $::STATWIDS->{title} ->set_text ("Title: " . $title); 556 $::STATWIDS->{title} ->set_text ("Title: " . $title);
447 557
459 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED}); 569 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
460 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP}); 570 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
461 571
462 $self->update_weight; 572 $self->update_weight;
463 573
464 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}}) 574 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%%", $stats->{$RES_TBL{$_}})
465 for keys %RES_TBL; 575 for keys %RES_TBL;
466 576
467 my $sktbl = $::STATWIDS->{skill_tbl}; 577 my $sktbl = $::STATWIDS->{skill_tbl};
468 my @skills = keys %{ $self->{skill_info} }; 578 my @skills = keys %{ $self->{skill_info} };
469 579
478 $sktbl->clear; 588 $sktbl->clear;
479 589
480 my $sw = $self->{skillwid}{""} ||= [ 590 my $sw = $self->{skillwid}{""} ||= [
481 0, 0, (new DC::UI::Label text => "Experience", align => 1), 591 0, 0, (new DC::UI::Label text => "Experience", align => 1),
482 1, 0, (new DC::UI::Label text => "Lvl.", align => 1), 592 1, 0, (new DC::UI::Label text => "Lvl.", align => 1),
483 2, 0, (new DC::UI::Label text => "Progress", align => 0), 593 2, 0, (new DC::UI::Label text => "Progress"),
484 3, 0, (new DC::UI::Label text => "Skill", expand => 1), 594 3, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0),
485 4, 0, (new DC::UI::Label text => "Experience", align => 1), 595 4, 0, (new DC::UI::Label text => "Experience", align => 1),
486 5, 0, (new DC::UI::Label text => "Lvl.", align => 1), 596 5, 0, (new DC::UI::Label text => "Lvl.", align => 1),
487 6, 0, (new DC::UI::Label text => "Progress", align => 0), 597 6, 0, (new DC::UI::Label text => "Progress"),
488 7, 0, (new DC::UI::Label text => "Skill", expand => 1), 598 7, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0),
489 ]; 599 ];
490 600
491 my @add = @$sw; 601 my @add = @$sw;
492 602
493 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 603 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use skill\nRight click - further options</small>";
494 604
495 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 605 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); 606 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
497 607
498 my ($x, $y) = (0, 1); 608 my ($x, $y) = (0, 1);
532 642
533 # progress 643 # progress
534 (new DC::UI::ExperienceProgress), 644 (new DC::UI::ExperienceProgress),
535 645
536 # label 646 # label
537 (new DC::UI::Label text => $name, on_button_down => $spell_cb, 647 (new DC::UI::Label text => $name, on_button_down => $spell_cb, align => 0,
538 can_events => 1, can_hover => 1, tooltip => (DC::Pod::section_label skill_description => $name) . $TOOLTIP_ALL), 648 can_events => 1, can_hover => 1, tooltip => (DC::Pod::section_label skill_description => $name) . $TOOLTIP_ALL),
539 ]; 649 ];
540 650
541 push @add, 651 push @add,
542 $x * 4 + 0, $y, $sw->[0], 652 $x * 4 + 0, $y, $sw->[0],
560 my $sw = $self->{skillwid}{$idx}; 670 my $sw = $self->{skillwid}{$idx};
561 $sw->[0]->set_text (::formsep ($val->[1])); 671 $sw->[0]->set_text (::formsep ($val->[1]));
562 $sw->[1]->set_text ($val->[0] * 1); 672 $sw->[1]->set_text ($val->[0] * 1);
563 $sw->[2]->set_value (@$val); 673 $sw->[2]->set_value (@$val);
564 674
565 $::GAUGES->{sklprg}->set_label ("$name %d%%"); 675 $::GAUGES->{skillexp}->set_label ("$name %d%%");
566 $::GAUGES->{sklprg}->set_value (@$val); 676 $::GAUGES->{skillexp}->set_value (@$val);
567 } 677 }
568} 678}
569 679
570sub user_send { 680sub user_send {
571 my ($self, $command) = @_; 681 my ($self, $command) = @_;
584} 694}
585 695
586sub map_scroll { 696sub map_scroll {
587 my ($self, $dx, $dy) = @_; 697 my ($self, $dx, $dy) = @_;
588 698
589 $self->{map}->scroll ($dx, $dy); 699 $self->{map_widget}->scroll ($dx, $dy);
590} 700}
591 701
592sub feed_map1a { 702sub feed_map1a {
593 my ($self, $data) = @_; 703 my ($self, $data) = @_;
594 704
595 my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap}); 705 my $missing = $self->{map}->map1a_update ($data);
596 my $delay; 706 my $delay;
597 707
598 for my $tile (@$missing) { 708 for my $tile (@$missing) {
599 next if $self->{delay}{$tile}; 709 next if $self->{delay}{$tile};
600 710
601 $delay = 1; 711 $delay = 1;
602 712
603 if (my $tex = $::CONN->{texture}[$tile]) { 713 if (my $tex = $self->{texture}[$tile]) {
604 $tex->upload; 714 $tex->upload;
605 } else { 715 } else {
606 $self->{delay}{$tile} = 1; 716 $self->{delay}{$tile} = 1;
607 717
608 # we assume the face is in-flight and will eventually come 718 # we assume the face is in-flight and will eventually arrive
609 push @{$self->{tile_cb}{$tile}}, sub { 719 push @{$self->{tile_cb}{$tile}}, sub {
610 delete $self->{delay}{$tile}; 720 delete $self->{delay}{$tile};
611 $_[0]->upload; 721 $_[0]->upload;
612 }; 722 };
613 } 723 }
614 } 724 }
615 725
616 if ($delay) { 726 if ($delay) {
617 # delay the map drawing a tiny bit in the hope of getting the missing fetched 727 # delay the map drawing a tiny bit in the hope of getting the missing tiles fetched
618 EV::once undef, 0, 0.03, sub { 728 EV::once undef, 0, 0.03, sub {
619 $self->{map_widget}->update 729 $self->{map_widget}->update
620 if $self->{map_widget}; 730 if $self->{map_widget};
621 }; 731 };
622 } else { 732 } else {
631} 741}
632 742
633sub flush_map { 743sub flush_map {
634 my ($self) = @_; 744 my ($self) = @_;
635 745
636 my $map_info = delete $self->{map_info} 746 return unless $self->{map_info};
637 or return;
638 747
748 for my $map_info (values %{ $self->{map_cache} || {} }) {
639 my ($hash, $x, $y, $w, $h) = @$map_info; 749 my ($hash, $rdata, $x, $y, $w, $h) = @$map_info;
640 750
641 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h); 751 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
642 $self->{map_cache_new}{$hash} = \$data; 752
753 if ($data ne $$rdata) {
754 $map_info->[1] = \$data;
755 my $cdata = Compress::LZF::compress $data;
643 DC::DB::put $self->{mapcache} => $hash => $data, sub { }; 756 DC::DB::put $self->{mapcache} => $hash => $cdata, sub { };
757 }
758 }
644} 759}
645 760
646sub map_clear { 761sub map_clear {
647 my ($self) = @_; 762 my ($self) = @_;
648 763
649 $self->flush_map; 764 $self->flush_map;
765 delete $self->{map_info};
650 delete $self->{neigh_map}; 766 delete $self->{neigh_map};
651 767
652 $self->{map}->clear; 768 $self->{map}->clear;
653 delete $self->{map_widget}{magicmap}; 769 delete $self->{map_widget}{magicmap};
654} 770}
675 791
676 $self->bg_fetch; 792 $self->bg_fetch;
677 }; 793 };
678} 794}
679 795
680sub load_map($$$) { 796sub load_map($$$$$$) {
681 my ($self, $hash, $x, $y) = @_; 797 my ($self, $hash, $x, $y, $w, $h) = @_;
682 798
683 my $gen = $self->{map_change_gen}; 799 my $map_info = $self->{map_cache}{$hash} = [$hash, \"", $x, $y, $w, $h];
684 800
685 my $cb = sub { 801 my $cb = sub {
686 return unless $gen == $self->{map_change_gen}; 802 $map_info->[1] = \$_[0];
687 803
688 my ($data) = @_;
689
690 if (defined $data) {
691 $self->{map_cache_new}{$hash} = \$data;
692
693 my $data = Compress::LZF::decompress $data;
694
695 my $inprogress = @{ $self->{bg_fetch} || [] }; 804 my $inprogress = @{ $self->{bg_fetch} || [] };
696 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); 805 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $_[0]);
697 $self->bg_fetch unless $inprogress; 806 $self->bg_fetch unless $inprogress;
698 }
699 }; 807 };
700 808
701 if (my $rdata = $self->{map_cache_old}{$hash}) { 809 if (my $map_info = $self->{map_cache_old}{$hash}) {
702 $cb->($$rdata); 810 $cb->(${ $map_info->[1] });
703 } else { 811 } else {
812 my $gen = $self->{map_change_gen};
813
704 DC::DB::get $self->{mapcache} => $hash, $cb; 814 DC::DB::get $self->{mapcache} => $hash, sub {
815 return unless $gen == $self->{map_change_gen};
816 return unless defined $_[0];
817 $cb->(Compress::LZF::decompress $_[0]);
818 };
705 } 819 }
706} 820}
707 821
708# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 822# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
709# (server resource,s latency, bandwidth), so this hack is warranted. 823# (server resources, latency, bandwidth), so this hack is warranted.
710# the right fix is to make real tiled maps with an overview file 824# the right fix is to make real tiled maps with an overview file
711sub send_mapinfo { 825sub send_mapinfo {
712 my ($self, $data, $cb) = @_; 826 my ($self, $data, $cb) = @_;
713 827
714 if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) { 828 if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
739} 853}
740 854
741# this method does a "flood fill" into every tile direction 855# this method does a "flood fill" into every tile direction
742# it assumes that tiles are arranged in a rectangular grid, 856# it assumes that tiles are arranged in a rectangular grid,
743# i.e. a map is the same as the left of the right map etc. 857# i.e. a map is the same as the left of the right map etc.
744# failure to comply are harmless and result in display errors 858# failure to comply is harmless and results in display errors
745# at worst. 859# at worst.
746sub flood_fill { 860sub flood_fill {
747 my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_; 861 my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
748 862
749 # the server does not allow map paths > 6 863 # the server does not allow map paths > 6
785 return if $mode ne "spatial"; 899 return if $mode ne "spatial";
786 900
787 $x += $self->{map}->ox; 901 $x += $self->{map}->ox;
788 $y += $self->{map}->oy; 902 $y += $self->{map}->oy;
789 903
790 $self->load_map ($hash, $x, $y) 904 $self->load_map ($hash, $x, $y, $w, $h)
791 unless $self->{neigh_map}{$hash}[5]++;#d# 905 unless $self->{neigh_map}{$hash}[5]++;#d#
792 906
793 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash]; 907 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
794 908
795 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 909 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
803 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 917 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
804 918
805 $self->flush_map; 919 $self->flush_map;
806 920
807 ++$self->{map_change_gen}; 921 ++$self->{map_change_gen};
808 $self->{map_cache_old} = delete $self->{map_cache_new}; 922 $self->{map_cache_old} = delete $self->{map_cache};
809 923
810 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 924 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
811 925
812 my $mapmapw = $self->{mapmap}->{w}; 926 my $mapmapw = $self->{mapmap}->{w};
813 my $mapmaph = $self->{mapmap}->{h}; 927 my $mapmaph = $self->{mapmap}->{h};
825 $self->{map_info} = [$hash, $x, $y, $w, $h]; 939 $self->{map_info} = [$hash, $x, $y, $w, $h];
826 940
827 (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/; 941 (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
828 $::STATWIDS->{map}->set_text ("Map: " . $map); 942 $::STATWIDS->{map}->set_text ("Map: " . $map);
829 943
830 $self->load_map ($hash, $x, $y); 944 $self->load_map ($hash, $x, $y, $w, $h);
831 $self->flood_fill (0, 0, 0, "", $hash, $flags); 945 $self->flood_fill (0, 0, 0, "", $hash, $flags);
832} 946}
833 947
834sub face_find { 948sub face_find {
835 my ($self, $facenum, $face, $cb) = @_; 949 my ($self, $facenum, $face, $cb) = @_;
906 1020
907 my $tex = $self->{texture}[$tile] ||= 1021 my $tex = $self->{texture}[$tile] ||=
908 new DC::Texture 1022 new DC::Texture
909 tile => $tile, 1023 tile => $tile,
910 image => $data, delete_image => 1, 1024 image => $data, delete_image => 1,
911 minify => 1, mipmap => 1; 1025 minify => 1;
912 1026
913 if (my $cbs = delete $self->{tile_cb}{$tile}) { 1027 if (my $cbs = delete $self->{tile_cb}{$tile}) {
914 $_->($tex) for @$cbs; 1028 $_->($tex) for @$cbs;
915 } 1029 }
916} 1030}
921 my ($self, $num, $cb) = @_; 1035 my ($self, $num, $cb) = @_;
922 1036
923 push @{$self->{face_cb}{$num}}, $cb; 1037 push @{$self->{face_cb}{$num}}, $cb;
924 1038
925 defined wantarray 1039 defined wantarray
926 ? DC::guard { 1040 ? Guard::guard {
927 @{$self->{face_cb}{$num}} 1041 @{$self->{face_cb}{$num}}
928 = grep $_ != $cb, 1042 = grep $_ != $cb,
929 @{$self->{face_cb}{$num}}; 1043 @{$self->{face_cb}{$num}};
930 } 1044 }
931 : () 1045 : ()
959 my ($self, $flags, $prompt) = @_; 1073 my ($self, $flags, $prompt) = @_;
960 1074
961 $prompt = $LAST_QUERY unless length $prompt; 1075 $prompt = $LAST_QUERY unless length $prompt;
962 $LAST_QUERY = $prompt; 1076 $LAST_QUERY = $prompt;
963 1077
964 $self->{query}-> ($self, $flags, $prompt); 1078 $self->{query}->($self, $flags, $prompt);
965} 1079}
966 1080
967sub sanitise_xml($) { 1081sub sanitise_xml($) {
968 local $_ = shift; 1082 local $_ = shift;
969 1083
977 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g; 1091 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
978 1092
979 # handle some elements 1093 # handle some elements
980 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs; 1094 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
981 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs; 1095 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
1096
1097 s/\s+$//;
982 1098
983 $_ 1099 $_
984} 1100}
985 1101
986our %NAME_TO_COLOR = ( 1102our %NAME_TO_COLOR = (
1029 $self->logprint ("msg: ", $text); 1145 $self->logprint ("msg: ", $text);
1030 return if $color < 0; # negative color == ignore if not understood 1146 return if $color < 0; # negative color == ignore if not understood
1031 1147
1032 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0]; 1148 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0];
1033 1149
1034 ## try to create single paragraphs of multiple lines sent by the server
1035 # no longer neecssary with TRT servers
1036 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
1037
1038 for (split /\n/, $text) {
1039 ::message ({ 1150 ::message ({
1040 fg => $fg, 1151 fg => $fg,
1041 markup => $_, 1152 markup => $text,
1042 type => $type, 1153 type => $type,
1043 extra => [@extra], 1154 extra => [@extra],
1044 color_flags => $color, #d# ugly, kill 1155 color_flags => $color, #d# ugly, kill
1045 }); 1156 });
1046 1157
1047 $color &= ~NDI_CLEAR; # only clear once for multiline messages 1158# $color &= ~NDI_CLEAR; # only clear once for multiline messages
1048 # actually, this is an ugly design. _we_ should control the channels, 1159# # actually, this is an ugly design. _we_ should control the channels,
1049 # not some random other widget, as the channels are clearly protocol-specific. 1160# # 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 1161# # then we could also react to flags such as CLEAR without resorting to
1051 # hacks such as color_flags, above. 1162# # hacks such as color_flags, above.
1052 }
1053 1163
1054 $self->{statusbox}->add ($text, 1164 $self->{statusbox}->add ($text,
1055 group => $text, 1165 group => $text,
1056 fg => $fg, 1166 fg => $fg,
1057 timeout => $color >= 2 ? 180 : 10, 1167 timeout => $color >= 2 ? 180 : 10,
1092 1202
1093 my %skill_help; 1203 my %skill_help;
1094 1204
1095 for my $node (DC::Pod::find skill_description => "*") { 1205 for my $node (DC::Pod::find skill_description => "*") {
1096 my (undef, @par) = DC::Pod::section_of $node; 1206 my (undef, @par) = DC::Pod::section_of $node;
1097 $skill_help{$node->{kw}[0]} = DC::Pod::as_label @par; 1207 $skill_help{$node->[DC::Pod::N_KW][0]} = DC::Pod::as_label @par;
1098 }; 1208 };
1099 1209
1100 for my $skill (values %{$self->{skill_info}}) { 1210 for my $skill (values %{$self->{skill_info}}) {
1101 $self->{map_widget}->add_command ("ready_skill $skill", 1211 $self->{map_widget}->add_command ("ready_skill $skill",
1102 (DC::asxml "Ready the skill '$skill'\n\n") 1212 (DC::asxml "Ready the skill '$skill'\n\n")
1122 $::FLOORBOX->clear; 1232 $::FLOORBOX->clear;
1123 1233
1124 my @add; 1234 my @add;
1125 1235
1126 my $row; 1236 my $row;
1127 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 1237 for (sort { $b->{count} <=> $a->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
1238 next if $_->{tag} & 0x80000000;
1128 if ($row < 6) { 1239 if ($row < 6) {
1129 local $_->{face_widget}; # hack to force recreation of widget 1240 local $_->{face_widget}; # hack to force recreation of widget
1130 local $_->{desc_widget}; # hack to force recreation of widget 1241 local $_->{desc_widget}; # hack to force recreation of widget
1131 DC::Item::update_widgets $_; 1242 DC::Item::update_widgets $_;
1132 1243
1159sub set_opencont { 1270sub set_opencont {
1160 my ($conn, $tag, $name) = @_; 1271 my ($conn, $tag, $name) = @_;
1161 $conn->{open_container} = $tag; 1272 $conn->{open_container} = $tag;
1162 update_floorbox; 1273 update_floorbox;
1163 1274
1164 $::INVR_HB->clear (); 1275 $::INVR_HB->clear;
1165 $::INVR_HB->add (new DC::UI::Label align => 0, expand => 1, text => $name); 1276 $::INVR_HB->add (new DC::UI::Label expand => 1, text => $name);
1166 1277
1167 if ($tag != 0) { # Floor isn't closable, is it? 1278 if ($tag != 0) { # Floor isn't closable, is it?
1168 $::INVR_HB->add (new DC::UI::Button 1279 $::INVR_HB->add (new DC::UI::Button
1169 text => "Close container", 1280 text => "Close container",
1170 tooltip => "Close the currently open container (if one is open)", 1281 tooltip => "Close the currently open container (if one is open)",
1225} 1336}
1226 1337
1227sub item_update { 1338sub item_update {
1228 my ($self, $item) = @_; 1339 my ($self, $item) = @_;
1229 1340
1230 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 1341 #print "item_update: $item->{tag} in $item->{container} pt($self->{player}{tag}) oc($::CONN->{open_container}) f($item->{flags})\n";
1231 1342
1232 DC::Item::update_widgets $item; 1343 DC::Item::update_widgets $item;
1233 1344
1234 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1345 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
1235 set_opencont ($::CONN, 0, "Floor"); 1346 set_opencont ($::CONN, 0, "Floor");
1236 1347
1237 } elsif ($item->{flags} & F_OPEN) { 1348 } elsif ($item->{flags} & F_OPEN) {
1238 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item); 1349 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item);
1254 1365
1255 my $weight = .001 * $self->{player}{weight}; 1366 my $weight = .001 * $self->{player}{weight};
1256 my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM}; 1367 my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM};
1257 1368
1258 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight); 1369 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight);
1259 $::STATWIDS->{m_weight}->set_text (sprintf "%.1fkg", $limit); 1370 $::STATWIDS->{m_weight}->set_text (sprintf "Max Weight: %.1fkg", $limit);
1260 $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit); 1371 $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit);
1261} 1372}
1262 1373
1263sub update_server_info { 1374sub update_server_info {
1264 my ($self) = @_; 1375 my ($self) = @_;
1265 1376
1266 my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>"); 1377 my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
1267 1378
1379 my $version = JSON::XS->new->encode ($self->{s_version});
1380
1268 $::SERVER_INFO->set_markup ( 1381 $::SERVER_INFO->set_markup (
1269 "server <tt>$self->{host}:$self->{port}</tt>\n" 1382 "server <tt>$self->{host}:$self->{port}</tt>\n"
1270 . "protocol version <tt>$self->{version}</tt>\n" 1383 . "protocol version <tt>$version</tt>\n"
1271 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 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" 1384 . "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"
1277 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1385 . "client support $yesno[$self->{cfplus_ext} > 0]"
1278 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1386 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
1279 . "map size $self->{mapw}×$self->{maph}\n" 1387 . "map size $self->{mapw}×$self->{maph}\n"
1280 ); 1388 );
1281 1389
1282} 1390}
1303 }); 1411 });
1304 1412
1305 $self->update_server_info; 1413 $self->update_server_info;
1306 1414
1307 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0; 1415 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
1308 $self->send_command ("pickup $::CFG->{pickup}"); 1416 $self->send_pickup ($::CFG->{pickup});
1309} 1417}
1310 1418
1311sub lookat { 1419sub lookat {
1312 my ($self, $x, $y) = @_; 1420 my ($self, $x, $y) = @_;
1313 1421
1315 $self->send_ext_req (lookat => $x, $y, sub { 1423 $self->send_ext_req (lookat => $x, $y, sub {
1316 my (%msg) = @_; 1424 my (%msg) = @_;
1317 1425
1318 if (exists $msg{npc_dialog}) { 1426 if (exists $msg{npc_dialog}) {
1319 # start npc chat dialog 1427 # start npc chat dialog
1320 $self->{npc_dialog} = new DC::NPCDialog:: 1428 $self->{w}{npc_dialog} = new DC::NPCDialog::
1321 token => $msg{npc_dialog}, 1429 token => $msg{npc_dialog},
1322 title => "$msg{npc_dialog}[0] (NPC)", 1430 title => "$msg{npc_dialog}[0] (NPC)",
1323 conn => $self, 1431 conn => $self,
1324 ; 1432 ;
1325 } 1433 }
1330} 1438}
1331 1439
1332sub destroy { 1440sub destroy {
1333 my ($self) = @_; 1441 my ($self) = @_;
1334 1442
1335 (delete $self->{npc_dialog})->destroy 1443 $_->destroy
1336 if $self->{npc_dialog}; 1444 for values %{ $self->{w} };
1337 1445
1338 $self->SUPER::destroy; 1446 $self->SUPER::destroy;
1339 1447
1340 %$self = (); 1448 %$self = ();
1341} 1449}
1477} 1585}
1478 1586
1479sub send { 1587sub send {
1480 my ($self, $msg) = @_; 1588 my ($self, $msg) = @_;
1481 1589
1482 $self->{textview}->add_paragraph ({ markup => "\n" . DC::asxml $msg }); 1590 $self->{textview}->add_paragraph ({
1591 markup =>
1592 "\n<span foreground='#ffff00'><b>"
1593 . (DC::asxml $msg)
1594 . "</b></span>"
1595 });
1483 $self->{textview}->scroll_to_bottom; 1596 $self->{textview}->scroll_to_bottom;
1484 1597
1485 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg); 1598 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
1486} 1599}
1487 1600
1490 1603
1491 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1604 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1492 1605
1493 if ($self->{conn}) { 1606 if ($self->{conn}) {
1494 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id}; 1607 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
1495 delete $self->{conn}{npc_dialog}; 1608 delete $self->{conn}{w}{npc_dialog};
1496 $self->{conn}->disconnect_ext ($self->{id}); 1609 $self->{conn}->disconnect_ext ($self->{id});
1497 } 1610 }
1498 1611
1499 $self->SUPER::destroy; 1612 $self->SUPER::destroy;
1500} 1613}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines