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.192 by root, Thu Mar 27 20:42:23 2008 UTC vs.
Revision 1.229 by root, Mon Nov 26 13:02:28 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;
10use DC::UI; 11use DC::UI;
11use DC::Pod;
12use DC::Macro; 12use DC::Macro;
13use DC::Item; 13use DC::Item;
14 14
15use base 'Deliantra::Protocol::Base'; 15use base 'Deliantra::Protocol::Base';
16
17our $TEX_DIALOGUE = new_from_resource DC::Texture
18 "dialogue.png", minify => 1, mipmap => 1;
19
20our $TEX_NOFACE = new_from_resource DC::Texture
21 "noface.png", minify => 1, mipmap => 1, wrap => 1;
22
23sub MIN_TEXTURE_UNUSED() { 1 }#d#
16 24
17sub new { 25sub new {
18 my ($class, %arg) = @_; 26 my ($class, %arg) = @_;
19 27
20 my $self = $class->SUPER::new (%arg, 28 my $self = $class->SUPER::new (%arg,
21 setup_req => { 29 setup_req => {
22 extmap => 1, 30 extmap => 1,
23 excmd => 1,
24 widget => 2, 31 widget => 2,
25 %{$arg{setup_req} || {}}, 32 %{$arg{setup_req} || {}},
26 }, 33 },
27 ); 34 );
28 35
29 $self->{map_widget}->clr_commands; 36 $self->update_fx_want;
30 37
31 my @cmd_help = map { 38 my $exp_guard = $self->addme_guard;
32 $_->[DC::Pod::N_KW][0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 39 my $skl_guard = $self->addme_guard;
33 or die "unparseable command help: $_->[DC::Pod::N_KW][0]"; 40 my $spl_guard = $self->addme_guard;
41 $self->send_exti_req (resource => qw(exp_table skill_info spell_paths), sub {
42 my ($exp, $skl, $spl) = @_;
34 43
35 my $cmd = $1; 44 $self->register_face_handler ($exp, sub {
36 my @args = split /\|/, $2; 45 my ($face) = @_;
37 @args = (".*") unless @args;
38 46
39 my (undef, @par) = DC::Pod::section_of $_; 47 undef $exp_guard;
40 my $text = DC::Pod::as_label @par; 48 $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
49 $_->() for values %{ $self->{on_exp_update} };
50 });
41 51
42 $_ = $_ eq ".*" ? "" : " $_" 52 $self->register_face_handler ($skl, sub {
43 for @args; 53 my ($face) = @_;
44 54
45 map ["$cmd$_", $text], 55 undef $skl_guard;
46 sort { (length $a) <=> (length $b) } 56 my $info = $self->{json_coder}->decode (delete $face->{data});
47 @args 57 $self->{skill_info} = { map { CS_STAT_SKILLINFO + $_ => $info->[$_][0] } 0 .. $#$info };
48 } sort { $a->[DC::Pod::N_PAR] <=> $b->[DC::Pod::N_PAR] } 58 });
49 DC::Pod::find command => "*"; 59
60 $self->register_face_handler ($spl, sub {
61 my ($face) = @_;
62
63 undef $spl_guard;
64 my $info = $self->{json_coder}->decode (delete $face->{data});
65 $self->{spell_paths} = { map { (1 << $_) => $info->[$_][0] } 0 .. $#$info };
66 });
67
68 ()
69 });
70
71 $::COMPLETER->reset;
50 72
51 $self->{json_coder} 73 $self->{json_coder}
52 ->convert_blessed 74 ->convert_blessed
53 ->filter_json_single_key_object ("\fw" => sub { 75 ->filter_json_single_key_object ("\fw" => sub {
54 $self->{widget}{$_[0]} 76 $self->{widget}{$_[0]}
64 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { 86 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
65 for my $ws (values %{delete $self->{widgetset} || {}}) { 87 for my $ws (values %{delete $self->{widgetset} || {}}) {
66 $_->destroy 88 $_->destroy
67 for values %{delete $ws->{w} || {}}; 89 for values %{delete $ws->{w} || {}};
68 } 90 }
91
92 delete $self->{items};
93 $::INV->clear;
94 $::INVR->clear;
95 $::INVR_HB->clear;
96 $::FLOORBOX->clear;
69 }); 97 });
70 98
71 $self->{map_widget}->add_command (@$_)
72 for @cmd_help;
73
74 { 99 {
75 $self->{dialogue} = my $tex = new_from_file DC::Texture 100 $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}}); 101 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
78 } 102 }
79 103
80 { 104 {
81 $self->{noface} = my $tex = new_from_file DC::Texture 105 $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}}); 106 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
84 } 107 }
108
109# $self->{expire_count} = DC::DB::FIRST_TILE_ID; # minimum non-fixed tile id
110# $self->{expire_w} = AE::timer 1, 1, sub {
111# my $count = (int @{ $self->{texture} } / MIN_TEXTURE_UNUSED) || 1;
112#
113# for ($self->{map}->expire_textures ($self->{expire_count}, $count)) {
114# warn DC::SvREFCNT $self->{texture}[$_];
115# $self->{texture}[$_]->unload;
116# warn "expire texture $_\n";#d#
117# }
118#
119# ($self->{expire_count} += $count) < @{ $self->{texture} }
120# or $self->{expire_count} = DC::DB::FIRST_TILE_ID;
121# warn "count is $count\n";#d#
122# };
85 123
86 $self->{open_container} = 0; 124 $self->{open_container} = 0;
87 125
88 # per server 126 # per server
89 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}"; 127 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
90 128
91 $self 129 $self
92} 130}
131
132 #$self->send_exti_req (nickmon => 1, sub { use Data::Dump; ddx \@_ });#d#
133#sub ext_nicklist { shift; use Data::Dump; ddx \@_; } #d#
93 134
94sub update_fx_want { 135sub update_fx_want {
95 my ($self) = @_; 136 my ($self) = @_;
96 137
97 $self->send_exti_msg (fx_want => { 138 $self->send_exti_msg (fx_want => {
102} 143}
103 144
104sub ext_capabilities { 145sub ext_capabilities {
105 my ($self, %cap) = @_; 146 my ($self, %cap) = @_;
106 147
107 $self->update_fx_want;
108
109 $self->send_exti_req (resource => "exp_table", sub {
110 my ($exp_table) = @_;
111
112 $self->register_face_handler ($exp_table, sub {
113 my ($face) = @_;
114
115 $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
116 $_->() for values %{ $self->{on_exp_update} || {} };
117 });
118
119 ()
120 });
121
122 if (my $ts = $cap{tileset}) { 148 if (my $ts = $cap{tileset}) {
123 if (my ($default) = grep $_->[2] & 1, @$ts) { 149 if (my ($default) = grep $_->[2] & 1, @$ts) {
124 $self->{tileset} = $default; 150 $self->{tileset} = $default;
125 $self->{tilesize} = $default->[3]; 151 $self->{tilesize} = $default->[3];
126 $self->setup_req (tileset => $default->[0]); 152 $self->setup_req (tileset => $default->[0]);
134} 160}
135 161
136sub ext_ambient_music { 162sub ext_ambient_music {
137 my ($self, $songs) = @_; 163 my ($self, $songs) = @_;
138 &::audio_music_set_ambient ($songs); 164 &::audio_music_set_ambient ($songs);
165}
166
167sub ext_command_list {
168 my ($self, @faces) = @_;
169
170 my $handler = $self->{command_facehandler} = {};
171 my $commands = $::COMPLETER->{command_list} = {};
172
173 $::COMPLETER->{command_lists} = \@faces;
174
175 for my $idx (@faces) {
176 $handler->{$idx} = $self->register_face_handler ($idx, sub {
177 my ($face) = @_;
178
179 $commands->{$idx} =
180 $face->{cache} ||= $self->{json_coder}->decode ($face->{data});
181 });
182 }
139} 183}
140 184
141############################################################################# 185#############################################################################
142 186
143sub widget_associate { 187sub widget_associate {
250 my %wkw = ( 294 my %wkw = (
251 root => $DC::UI::ROOT, 295 root => $DC::UI::ROOT,
252 tooltip => $DC::UI::TOOLTIP, 296 tooltip => $DC::UI::TOOLTIP,
253 297
254 mapwidget => $::MAPWIDGET, 298 mapwidget => $::MAPWIDGET,
299 menubar => $::MENUBAR,
300 menupopup => $::MENUPOPUP,
301 pickup_enable => $::PICKUP_ENABLE,
255 buttonbar => $::BUTTONBAR, 302 buttonbar => $::BUTTONBAR,
256 metaserver => $::METASERVER, 303 metaserver => $::METASERVER,
257 buttonbar => $::BUTTONBAR, 304 buttonbar => $::BUTTONBAR,
258 login_button => $::LOGIN_BUTTON, 305 login_button => $::LOGIN_BUTTON,
259 quit_dialog => $::QUIT_DIALOG, 306 quit_dialog => $::QUIT_DIALOG,
276 323
277 floorbox => $::FLOORBOX, 324 floorbox => $::FLOORBOX,
278 help_window => $::HELP_WINDOW, 325 help_window => $::HELP_WINDOW,
279 message_window => $::MESSAGE_WINDOW, 326 message_window => $::MESSAGE_WINDOW,
280 message_dist => $::MESSAGE_DIST, 327 message_dist => $::MESSAGE_DIST,
281 statusbox => $::SDTATUSBOX, 328 statusbox => $::STATUSBOX,
282 329
283 inv => $::INV, 330 inv => $::INV,
284 invr => $::INVR, 331 invr => $::INVR,
285 invr_hb => $::INVR_HB, 332 invr_hb => $::INVR_HB,
286 ); 333 );
370 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_}) 417 ? (($new & $_ ? "+" : "-") . $self->{spell_paths}{$_})
371 : () 418 : ()
372 } 419 }
373 sort { $a <=> $b } keys %{$self->{spell_paths}}; 420 sort { $a <=> $b } keys %{$self->{spell_paths}};
374 421
375 join "", @diff 422 "\u$name: " . (join ", ", @diff)
376} 423}
377 424
378# all stats that are chacked against changes 425# all stats that are chacked against changes
379my @statchange = ( 426my @statchange = (
380 [&CS_STAT_STR => \&_stat_numdiff, "Str"], 427 [&CS_STAT_STR => \&_stat_numdiff, "Str"],
423 470
424 if (my @diffs = 471 if (my @diffs =
425 ( 472 (
426 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()), 473 ($stats->{+CS_STAT_EXP64} > $prev->{+CS_STAT_EXP64} ? ($stats->{+CS_STAT_EXP64} - $prev->{+CS_STAT_EXP64}) . " experience gained" : ()),
427 map { 474 map {
428 $stats->{$_} && $prev->{$_} 475 $stats->{$_} && $prev->{$_}
429 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : () 476 && $stats->{$_}[1] > $prev->{$_}[1] ? "($self->{skill_info}{$_}+" . ($stats->{$_}[1] - $prev->{$_}[1]) . ")" : ()
430 } sort { $a <=> $b } keys %{$self->{skill_info}} 477 } sort { $a <=> $b } keys %{$self->{skill_info}}
431 ) 478 )
432 ) { 479 ) {
433 my $msg = join " ", @diffs; 480 my $msg = join " ", @diffs;
435 } 482 }
436 483
437 if ( 484 if (
438 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange 485 my @diffs = map $_->[1]->($self, $_->[2], $prev->{$_->[0]}, $stats->{$_->[0]}), @statchange
439 ) { 486 ) {
440 my $msg = "<b>stat change</b>: " . (join " ", @diffs); 487 my $msg = "<b>stat change</b>: " . (join " ", map "($_)", @diffs);
441 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20); 488 $self->{statusbox}->add ($msg, group => "stat $msg", fg => [0.8, 1, 0.2, 1], timeout => 20);
442 } 489 }
443 490
444 $self->update_stats_window ($stats, $prev); 491 $self->update_stats_window ($stats, $prev);
445 492
483 530
484 $::GAUGES->{hp} ->set_value ($hp, $hp_m); 531 $::GAUGES->{hp} ->set_value ($hp, $hp_m);
485 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 532 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
486 $::GAUGES->{food} ->set_value ($fo, $fo_m); 533 $::GAUGES->{food} ->set_value ($fo, $fo_m);
487 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 534 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
488 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 535 $::GAUGES->{exp} ->set_label ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))#d#
489 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 536 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
490 $::GAUGES->{prg} ->set_value ($stats->{+CS_STAT_LEVEL}, $stats->{+CS_STAT_EXP64}); 537 $::GAUGES->{exp} ->set_value ($stats->{+CS_STAT_LEVEL}, $stats->{+CS_STAT_EXP64});
491 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE}); 538 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
492 my $title = $stats->{+CS_STAT_TITLE}; 539 my $title = $stats->{+CS_STAT_TITLE};
493 $title =~ s/^Player: //; 540 $title =~ s/^Player: //;
494 $::STATWIDS->{title} ->set_text ("Title: " . $title); 541 $::STATWIDS->{title} ->set_text ("Title: " . $title);
495 542
507 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED}); 554 $::STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{+CS_STAT_SPEED});
508 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP}); 555 $::STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{+CS_STAT_WEAP_SP});
509 556
510 $self->update_weight; 557 $self->update_weight;
511 558
512 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$RES_TBL{$_}}) 559 $::STATWIDS->{"res_$_"}->set_text (sprintf "%d%%", $stats->{$RES_TBL{$_}})
513 for keys %RES_TBL; 560 for keys %RES_TBL;
514 561
515 my $sktbl = $::STATWIDS->{skill_tbl}; 562 my $sktbl = $::STATWIDS->{skill_tbl};
516 my @skills = keys %{ $self->{skill_info} }; 563 my @skills = keys %{ $self->{skill_info} };
517 564
536 7, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0), 583 7, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0),
537 ]; 584 ];
538 585
539 my @add = @$sw; 586 my @add = @$sw;
540 587
541 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 588 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use skill\nRight click - further options</small>";
542 589
543 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 590 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
544 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 591 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
545 592
546 my ($x, $y) = (0, 1); 593 my ($x, $y) = (0, 1);
581 # progress 628 # progress
582 (new DC::UI::ExperienceProgress), 629 (new DC::UI::ExperienceProgress),
583 630
584 # label 631 # label
585 (new DC::UI::Label text => $name, on_button_down => $spell_cb, align => 0, 632 (new DC::UI::Label text => $name, on_button_down => $spell_cb, align => 0,
586 can_events => 1, can_hover => 1, tooltip => (DC::Pod::section_label skill_description => $name) . $TOOLTIP_ALL), 633 can_events => 1, can_hover => 1, tooltip => "#(skill/$name)$TOOLTIP_ALL"),
587 ]; 634 ];
588 635
589 push @add, 636 push @add,
590 $x * 4 + 0, $y, $sw->[0], 637 $x * 4 + 0, $y, $sw->[0],
591 $x * 4 + 1, $y, $sw->[1], 638 $x * 4 + 1, $y, $sw->[1],
608 my $sw = $self->{skillwid}{$idx}; 655 my $sw = $self->{skillwid}{$idx};
609 $sw->[0]->set_text (::formsep ($val->[1])); 656 $sw->[0]->set_text (::formsep ($val->[1]));
610 $sw->[1]->set_text ($val->[0] * 1); 657 $sw->[1]->set_text ($val->[0] * 1);
611 $sw->[2]->set_value (@$val); 658 $sw->[2]->set_value (@$val);
612 659
613 $::GAUGES->{sklprg}->set_label ("$name %d%%"); 660 $::GAUGES->{skillexp}->set_label ("$name %d%%");
614 $::GAUGES->{sklprg}->set_value (@$val); 661 $::GAUGES->{skillexp}->set_value (@$val);
615 } 662 }
616} 663}
617 664
618sub user_send { 665sub user_send {
619 my ($self, $command) = @_; 666 my ($self, $command) = @_;
632} 679}
633 680
634sub map_scroll { 681sub map_scroll {
635 my ($self, $dx, $dy) = @_; 682 my ($self, $dx, $dy) = @_;
636 683
637 $self->{map}->scroll ($dx, $dy); 684 $self->{map_widget}->scroll ($dx, $dy);
638} 685}
639 686
640sub feed_map1a { 687sub feed_map1a {
641 my ($self, $data) = @_; 688 my ($self, $data) = @_;
642 689
643 my $missing = $self->{map}->map1a_update ($data, $self->{setup}{extmap}); 690 my $missing = $self->{map}->map1a_update ($data);
644 my $delay; 691 my $delay;
645 692
646 for my $tile (@$missing) { 693 for my $tile (@$missing) {
647 next if $self->{delay}{$tile}; 694 next if $self->{delay}{$tile};
648 695
649 $delay = 1; 696 $delay = 1;
650 697
651 if (my $tex = $::CONN->{texture}[$tile]) { 698 if (my $tex = $self->{texture}[$tile]) {
652 $tex->upload; 699 $tex->upload;
653 } else { 700 } else {
654 $self->{delay}{$tile} = 1; 701 $self->{delay}{$tile} = 1;
655 702
656 # we assume the face is in-flight and will eventually come 703 # we assume the face is in-flight and will eventually arrive
657 push @{$self->{tile_cb}{$tile}}, sub { 704 push @{$self->{tile_cb}{$tile}}, sub {
658 delete $self->{delay}{$tile}; 705 delete $self->{delay}{$tile};
659 $_[0]->upload; 706 $_[0]->upload;
660 }; 707 };
661 } 708 }
662 } 709 }
663 710
664 if ($delay) { 711 if ($delay) {
665 # delay the map drawing a tiny bit in the hope of getting the missing fetched 712 # delay the map drawing a tiny bit in the hope of getting the missing tiles fetched
666 EV::once undef, 0, 0.03, sub { 713 EV::once undef, 0, 0.03, sub {
667 $self->{map_widget}->update 714 $self->{map_widget}->update
668 if $self->{map_widget}; 715 if $self->{map_widget};
669 }; 716 };
670 } else { 717 } else {
679} 726}
680 727
681sub flush_map { 728sub flush_map {
682 my ($self) = @_; 729 my ($self) = @_;
683 730
684 my $map_info = delete $self->{map_info} 731 return unless $self->{map_info};
685 or return;
686 732
733 for my $map_info (values %{ $self->{map_cache} || {} }) {
687 my ($hash, $x, $y, $w, $h) = @$map_info; 734 my ($hash, $rdata, $x, $y, $w, $h) = @$map_info;
688 735
689 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h); 736 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
690 $self->{map_cache_new}{$hash} = \$data; 737
738 if ($data ne $$rdata) {
739 $map_info->[1] = \$data;
740 my $cdata = Compress::LZF::compress $data;
691 DC::DB::put $self->{mapcache} => $hash => $data, sub { }; 741 DC::DB::put $self->{mapcache} => $hash => $cdata, sub { };
742 }
743 }
692} 744}
693 745
694sub map_clear { 746sub map_clear {
695 my ($self) = @_; 747 my ($self) = @_;
696 748
697 $self->flush_map; 749 $self->flush_map;
750 delete $self->{map_info};
698 delete $self->{neigh_map}; 751 delete $self->{neigh_map};
699 752
700 $self->{map}->clear; 753 $self->{map}->clear;
701 delete $self->{map_widget}{magicmap}; 754 delete $self->{map_widget}{magicmap};
702} 755}
723 776
724 $self->bg_fetch; 777 $self->bg_fetch;
725 }; 778 };
726} 779}
727 780
728sub load_map($$$) { 781sub load_map($$$$$$) {
729 my ($self, $hash, $x, $y) = @_; 782 my ($self, $hash, $x, $y, $w, $h) = @_;
730 783
731 my $gen = $self->{map_change_gen}; 784 my $map_info = $self->{map_cache}{$hash} = [$hash, \"", $x, $y, $w, $h];
732 785
733 my $cb = sub { 786 my $cb = sub {
734 return unless $gen == $self->{map_change_gen}; 787 $map_info->[1] = \$_[0];
735 788
736 my ($data) = @_;
737
738 if (defined $data) {
739 $self->{map_cache_new}{$hash} = \$data;
740
741 my $data = Compress::LZF::decompress $data;
742
743 my $inprogress = @{ $self->{bg_fetch} || [] }; 789 my $inprogress = @{ $self->{bg_fetch} || [] };
744 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data); 790 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $_[0]);
745 $self->bg_fetch unless $inprogress; 791 $self->bg_fetch unless $inprogress;
746 }
747 }; 792 };
748 793
749 if (my $rdata = $self->{map_cache_old}{$hash}) { 794 if (my $map_info = $self->{map_cache_old}{$hash}) {
750 $cb->($$rdata); 795 $cb->(${ $map_info->[1] });
751 } else { 796 } else {
797 my $gen = $self->{map_change_gen};
798
752 DC::DB::get $self->{mapcache} => $hash, $cb; 799 DC::DB::get $self->{mapcache} => $hash, sub {
800 return unless $gen == $self->{map_change_gen};
801 return unless defined $_[0];
802 $cb->(Compress::LZF::decompress $_[0]);
803 };
753 } 804 }
754} 805}
755 806
756# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 807# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
757# (server resource,s latency, bandwidth), so this hack is warranted. 808# (server resources, latency, bandwidth), so this hack is warranted.
758# the right fix is to make real tiled maps with an overview file 809# the right fix is to make real tiled maps with an overview file
759sub send_mapinfo { 810sub send_mapinfo {
760 my ($self, $data, $cb) = @_; 811 my ($self, $data, $cb) = @_;
761 812
762 if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) { 813 if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
787} 838}
788 839
789# this method does a "flood fill" into every tile direction 840# this method does a "flood fill" into every tile direction
790# it assumes that tiles are arranged in a rectangular grid, 841# it assumes that tiles are arranged in a rectangular grid,
791# i.e. a map is the same as the left of the right map etc. 842# i.e. a map is the same as the left of the right map etc.
792# failure to comply are harmless and result in display errors 843# failure to comply is harmless and results in display errors
793# at worst. 844# at worst.
794sub flood_fill { 845sub flood_fill {
795 my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_; 846 my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
796 847
797 # the server does not allow map paths > 6 848 # the server does not allow map paths > 6
833 return if $mode ne "spatial"; 884 return if $mode ne "spatial";
834 885
835 $x += $self->{map}->ox; 886 $x += $self->{map}->ox;
836 $y += $self->{map}->oy; 887 $y += $self->{map}->oy;
837 888
838 $self->load_map ($hash, $x, $y) 889 $self->load_map ($hash, $x, $y, $w, $h)
839 unless $self->{neigh_map}{$hash}[5]++;#d# 890 unless $self->{neigh_map}{$hash}[5]++;#d#
840 891
841 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash]; 892 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
842 893
843 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 894 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
851 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 902 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
852 903
853 $self->flush_map; 904 $self->flush_map;
854 905
855 ++$self->{map_change_gen}; 906 ++$self->{map_change_gen};
856 $self->{map_cache_old} = delete $self->{map_cache_new}; 907 $self->{map_cache_old} = delete $self->{map_cache};
857 908
858 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 909 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
859 910
860 my $mapmapw = $self->{mapmap}->{w}; 911 my $mapmapw = $self->{mapmap}->{w};
861 my $mapmaph = $self->{mapmap}->{h}; 912 my $mapmaph = $self->{mapmap}->{h};
873 $self->{map_info} = [$hash, $x, $y, $w, $h]; 924 $self->{map_info} = [$hash, $x, $y, $w, $h];
874 925
875 (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/; 926 (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
876 $::STATWIDS->{map}->set_text ("Map: " . $map); 927 $::STATWIDS->{map}->set_text ("Map: " . $map);
877 928
878 $self->load_map ($hash, $x, $y); 929 $self->load_map ($hash, $x, $y, $w, $h);
879 $self->flood_fill (0, 0, 0, "", $hash, $flags); 930 $self->flood_fill (0, 0, 0, "", $hash, $flags);
880} 931}
881 932
882sub face_find { 933sub face_find {
883 my ($self, $facenum, $face, $cb) = @_; 934 my ($self, $facenum, $face, $cb) = @_;
954 1005
955 my $tex = $self->{texture}[$tile] ||= 1006 my $tex = $self->{texture}[$tile] ||=
956 new DC::Texture 1007 new DC::Texture
957 tile => $tile, 1008 tile => $tile,
958 image => $data, delete_image => 1, 1009 image => $data, delete_image => 1,
959 minify => 1, mipmap => 1; 1010 minify => 1;
960 1011
961 if (my $cbs = delete $self->{tile_cb}{$tile}) { 1012 if (my $cbs = delete $self->{tile_cb}{$tile}) {
962 $_->($tex) for @$cbs; 1013 $_->($tex) for @$cbs;
963 } 1014 }
964} 1015}
969 my ($self, $num, $cb) = @_; 1020 my ($self, $num, $cb) = @_;
970 1021
971 push @{$self->{face_cb}{$num}}, $cb; 1022 push @{$self->{face_cb}{$num}}, $cb;
972 1023
973 defined wantarray 1024 defined wantarray
974 ? DC::guard { 1025 ? Guard::guard {
975 @{$self->{face_cb}{$num}} 1026 @{$self->{face_cb}{$num}}
976 = grep $_ != $cb, 1027 = grep $_ != $cb,
977 @{$self->{face_cb}{$num}}; 1028 @{$self->{face_cb}{$num}};
978 } 1029 }
979 : () 1030 : ()
1007 my ($self, $flags, $prompt) = @_; 1058 my ($self, $flags, $prompt) = @_;
1008 1059
1009 $prompt = $LAST_QUERY unless length $prompt; 1060 $prompt = $LAST_QUERY unless length $prompt;
1010 $LAST_QUERY = $prompt; 1061 $LAST_QUERY = $prompt;
1011 1062
1012 $self->{query}-> ($self, $flags, $prompt); 1063 $self->{query}->($self, $flags, $prompt);
1013}
1014
1015sub sanitise_xml($) {
1016 local $_ = shift;
1017
1018 # we now weed out all tags we do not support
1019 s{ <(?! /?i> | /?u> | /?b> | /?big | /?small | /?s | /?tt | fg\ | /fg>)
1020 }{
1021 "&lt;"
1022 }gex;
1023
1024 # now all entities
1025 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
1026
1027 # handle some elements
1028 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
1029 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
1030
1031 $_
1032} 1064}
1033 1065
1034our %NAME_TO_COLOR = ( 1066our %NAME_TO_COLOR = (
1035 black => 0, 1067 black => 0,
1036 white => 1, 1068 white => 1,
1047 tan => 12, 1079 tan => 12,
1048); 1080);
1049 1081
1050our @CF_COLOR = ( 1082our @CF_COLOR = (
1051 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 1083 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
1052 [1.00, 1.00, 1.00], 1084 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
1053 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 1085 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55],
1054 [1.00, 0.00, 0.00], 1086 [1.00, 0.00, 0.00],
1055 [1.00, 0.54, 0.00], 1087 [1.00, 0.54, 0.00],
1056 [0.11, 0.56, 1.00], 1088 [0.11, 0.56, 1.00],
1057 [0.93, 0.46, 0.00], 1089 [0.93, 0.46, 0.00],
1058 [0.18, 0.54, 0.34], 1090 [0.18, 0.54, 0.34],
1064); 1096);
1065 1097
1066sub msg { 1098sub msg {
1067 my ($self, $color, $type, $text, @extra) = @_; 1099 my ($self, $color, $type, $text, @extra) = @_;
1068 1100
1069 $text = sanitise_xml $text; 1101 $text = DC::sanitise_cfxml $text;
1070 1102
1071 if (my $cb = $self->{cb_msg}{$type}) { 1103 if (my $cb = $self->{cb_msg}{$type}) {
1072 $_->($self, $color, $type, $text, @extra) for values %$cb; 1104 $_->($self, $color, $type, $text, @extra) for values %$cb;
1073 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) { 1105 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
1074 $type =~ s/-/_/g; 1106 $type =~ s/-/_/g;
1077 $self->logprint ("msg: ", $text); 1109 $self->logprint ("msg: ", $text);
1078 return if $color < 0; # negative color == ignore if not understood 1110 return if $color < 0; # negative color == ignore if not understood
1079 1111
1080 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0]; 1112 my $fg = $CF_COLOR[$color & NDI_COLOR_MASK] || [1, 0, 0];
1081 1113
1082 ## try to create single paragraphs of multiple lines sent by the server
1083 # no longer neecssary with TRT servers
1084 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
1085
1086 for (split /\n/, $text) {
1087 ::message ({ 1114 ::message ({
1088 fg => $fg, 1115 fg => $fg,
1089 markup => $_, 1116 markup => $text,
1090 type => $type, 1117 type => $type,
1091 extra => [@extra], 1118 extra => [@extra],
1092 color_flags => $color, #d# ugly, kill 1119 color_flags => $color, #d# ugly, kill
1093 }); 1120 });
1094 1121
1095 $color &= ~NDI_CLEAR; # only clear once for multiline messages 1122# $color &= ~NDI_CLEAR; # only clear once for multiline messages
1096 # actually, this is an ugly design. _we_ should control the channels, 1123# # actually, this is an ugly design. _we_ should control the channels,
1097 # not some random other widget, as the channels are clearly protocol-specific. 1124# # not some random other widget, as the channels are clearly protocol-specific.
1098 # then we could also react to flags such as CLEAR without resorting to 1125# # then we could also react to flags such as CLEAR without resorting to
1099 # hacks such as color_flags, above. 1126# # hacks such as color_flags, above.
1100 }
1101 1127
1102 $self->{statusbox}->add ($text, 1128 $self->{statusbox}->add ($text,
1103 group => $text, 1129 group => $text,
1104 fg => $fg, 1130 fg => $fg,
1105 timeout => $color >= 2 ? 180 : 10, 1131 timeout => $color >= 2 ? 180 : 10,
1109} 1135}
1110 1136
1111sub spell_add { 1137sub spell_add {
1112 my ($self, $spell) = @_; 1138 my ($self, $spell) = @_;
1113 1139
1114 # try to create single paragraphs out of the multiple lines sent by the server
1115 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
1116 $spell->{message} =~ s/\n+$//;
1117 $spell->{message} ||= "Server did not provide a description for this spell.";
1118
1119 $::SPELL_LIST->add_spell ($spell); 1140 $::SPELL_LIST->add_spell ($spell);
1120 1141 delete $::COMPLETER->{command_list}{spells};
1121 $self->{map_widget}->add_command ("invoke $spell->{name}", DC::asxml $spell->{message});
1122 $self->{map_widget}->add_command ("cast $spell->{name}", DC::asxml $spell->{message});
1123} 1142}
1124 1143
1125sub spell_delete { 1144sub spell_delete {
1126 my ($self, $spell) = @_; 1145 my ($self, $spell) = @_;
1127 1146
1128 $::SPELL_LIST->remove_spell ($spell); 1147 $::SPELL_LIST->remove_spell ($spell);
1148 delete $::COMPLETER->{command_list}{spells};
1129} 1149}
1130 1150
1131sub setup { 1151sub setup {
1132 my ($self, $setup) = @_; 1152 my ($self, $setup) = @_;
1133 1153
1134 $self->{map_widget}->set_tilesize ($self->{tilesize}); 1154 $self->{map_widget}->set_tilesize ($self->{tilesize});
1135 $::MAP->resize ($self->{mapw}, $self->{maph}); 1155 $::MAP->resize ($self->{mapw}, $self->{maph});
1136} 1156}
1137 1157
1138sub addme_success {
1139 my ($self) = @_;
1140
1141 my %skill_help;
1142
1143 for my $node (DC::Pod::find skill_description => "*") {
1144 my (undef, @par) = DC::Pod::section_of $node;
1145 $skill_help{$node->[DC::Pod::N_KW][0]} = DC::Pod::as_label @par;
1146 };
1147
1148 for my $skill (values %{$self->{skill_info}}) {
1149 $self->{map_widget}->add_command ("ready_skill $skill",
1150 (DC::asxml "Ready the skill '$skill'\n\n")
1151 . $skill_help{$skill});
1152 $self->{map_widget}->add_command ("use_skill $skill",
1153 (DC::asxml "Immediately use the skill '$skill'\n\n")
1154 . $skill_help{$skill});
1155 }
1156}
1157
1158sub eof { 1158sub eof {
1159 my ($self) = @_; 1159 my ($self) = @_;
1160 1160
1161 $self->{map_widget}->clr_commands; 1161 $::COMPLETER->reset;
1162 1162
1163 ::stop_game (); 1163 ::stop_game ();
1164} 1164}
1165 1165
1166sub update_floorbox { 1166sub update_floorbox {
1170 $::FLOORBOX->clear; 1170 $::FLOORBOX->clear;
1171 1171
1172 my @add; 1172 my @add;
1173 1173
1174 my $row; 1174 my $row;
1175 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 1175 for (sort { $b->{count} <=> $a->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
1176 next if $_->{tag} & 0x80000000;
1176 if ($row < 6) { 1177 if ($row < 6) {
1177 local $_->{face_widget}; # hack to force recreation of widget 1178 local $_->{face_widget}; # hack to force recreation of widget
1178 local $_->{desc_widget}; # hack to force recreation of widget 1179 local $_->{desc_widget}; # hack to force recreation of widget
1179 DC::Item::update_widgets $_; 1180 DC::Item::update_widgets $_;
1180 1181
1207sub set_opencont { 1208sub set_opencont {
1208 my ($conn, $tag, $name) = @_; 1209 my ($conn, $tag, $name) = @_;
1209 $conn->{open_container} = $tag; 1210 $conn->{open_container} = $tag;
1210 update_floorbox; 1211 update_floorbox;
1211 1212
1212 $::INVR_HB->clear (); 1213 $::INVR_HB->clear;
1213 $::INVR_HB->add (new DC::UI::Label expand => 1, text => $name); 1214 $::INVR_HB->add (new DC::UI::Label expand => 1, text => $name);
1214 1215
1215 if ($tag != 0) { # Floor isn't closable, is it? 1216 if ($tag != 0) { # Floor isn't closable, is it?
1216 $::INVR_HB->add (new DC::UI::Button 1217 $::INVR_HB->add (new DC::UI::Button
1217 text => "Close container", 1218 text => "Close container",
1273} 1274}
1274 1275
1275sub item_update { 1276sub item_update {
1276 my ($self, $item) = @_; 1277 my ($self, $item) = @_;
1277 1278
1278 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 1279 #print "item_update: $item->{tag} in $item->{container} pt($self->{player}{tag}) oc($::CONN->{open_container}) f($item->{flags})\n";
1279 1280
1280 DC::Item::update_widgets $item; 1281 DC::Item::update_widgets $item;
1281 1282
1282 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1283 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
1283 set_opencont ($::CONN, 0, "Floor"); 1284 set_opencont ($::CONN, 0, "Floor");
1284 1285
1285 } elsif ($item->{flags} & F_OPEN) { 1286 } elsif ($item->{flags} & F_OPEN) {
1286 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item); 1287 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item);
1311sub update_server_info { 1312sub update_server_info {
1312 my ($self) = @_; 1313 my ($self) = @_;
1313 1314
1314 my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>"); 1315 my @yesno = ("<span foreground='red'>no</span>", "<span foreground='green'>yes</span>");
1315 1316
1317 my $version = JSON::XS->new->encode ($self->{s_version});
1318
1316 $::SERVER_INFO->set_markup ( 1319 $::SERVER_INFO->set_markup (
1317 "server <tt>$self->{host}:$self->{port}</tt>\n" 1320 "server <tt>$self->{host}:$self->{port}</tt>\n"
1318 . "protocol version <tt>$self->{version}</tt>\n" 1321 . "protocol version <tt>$version</tt>\n"
1319 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
1320 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1321 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
1322 . "editing support $yesno[!!$self->{editor_support}]\n" 1322 . "editing support $yesno[!!$self->{editor_support}]\n"
1323 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1324 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
1325 . "client support $yesno[$self->{cfplus_ext} > 0]" 1323 . "client support $yesno[$self->{cfplus_ext} > 0]"
1326 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1324 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
1327 . "map size $self->{mapw}×$self->{maph}\n" 1325 . "map size $self->{mapw}×$self->{maph}\n"
1328 ); 1326 );
1329 1327
1351 }); 1349 });
1352 1350
1353 $self->update_server_info; 1351 $self->update_server_info;
1354 1352
1355 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0; 1353 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
1356 $self->send_command ("pickup $::CFG->{pickup}"); 1354 $self->send_pickup ($::CFG->{pickup});
1357} 1355}
1358 1356
1359sub lookat { 1357sub lookat {
1360 my ($self, $x, $y) = @_; 1358 my ($self, $x, $y) = @_;
1361 1359
1363 $self->send_ext_req (lookat => $x, $y, sub { 1361 $self->send_ext_req (lookat => $x, $y, sub {
1364 my (%msg) = @_; 1362 my (%msg) = @_;
1365 1363
1366 if (exists $msg{npc_dialog}) { 1364 if (exists $msg{npc_dialog}) {
1367 # start npc chat dialog 1365 # start npc chat dialog
1368 $self->{npc_dialog} = new DC::NPCDialog:: 1366 $self->{w}{npc_dialog} = new DC::NPCDialog::
1369 token => $msg{npc_dialog}, 1367 token => $msg{npc_dialog},
1370 title => "$msg{npc_dialog}[0] (NPC)", 1368 title => "$msg{npc_dialog}[0] (NPC)",
1371 conn => $self, 1369 conn => $self,
1372 ; 1370 ;
1373 } 1371 }
1378} 1376}
1379 1377
1380sub destroy { 1378sub destroy {
1381 my ($self) = @_; 1379 my ($self) = @_;
1382 1380
1383 (delete $self->{npc_dialog})->destroy 1381 $_->destroy
1384 if $self->{npc_dialog}; 1382 for values %{ $self->{w} };
1385 1383
1386 $self->SUPER::destroy; 1384 $self->SUPER::destroy;
1387 1385
1388 %$self = (); 1386 %$self = ();
1389} 1387}
1489 1487
1490 $self->{kw}{$_} = 1 for @{$info{add_topics} || []}; 1488 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
1491 $self->{kw}{$_} = 0 for @{$info{del_topics} || []}; 1489 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
1492 1490
1493 if (exists $info{msg}) { 1491 if (exists $info{msg}) {
1494 my $text = "\n" . DC::Protocol::sanitise_xml $info{msg}; 1492 my $text = "\n" . DC::sanitise_cfxml $info{msg};
1495 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1493 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1496 my @link; 1494 my @link;
1497 $text =~ s{ 1495 $text =~ s{
1498 ($match) 1496 ($match)
1499 }{ 1497 }{
1525} 1523}
1526 1524
1527sub send { 1525sub send {
1528 my ($self, $msg) = @_; 1526 my ($self, $msg) = @_;
1529 1527
1530 $self->{textview}->add_paragraph ({ markup => "\n" . DC::asxml $msg }); 1528 $self->{textview}->add_paragraph ({
1529 markup =>
1530 "\n<span foreground='#ffff00'><b>"
1531 . (DC::asxml $msg)
1532 . "</b></span>"
1533 });
1531 $self->{textview}->scroll_to_bottom; 1534 $self->{textview}->scroll_to_bottom;
1532 1535
1533 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg); 1536 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
1534} 1537}
1535 1538
1538 1541
1539 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1 1542 #Carp::cluck "debug\n";#d# #todo# enable: destroy gets called twice because scalar keys {} is 1
1540 1543
1541 if ($self->{conn}) { 1544 if ($self->{conn}) {
1542 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id}; 1545 $self->{conn}->send_ext_msg (npc_dialog_end => $self->{id}) if $self->{id};
1543 delete $self->{conn}{npc_dialog}; 1546 delete $self->{conn}{w}{npc_dialog};
1544 $self->{conn}->disconnect_ext ($self->{id}); 1547 $self->{conn}->disconnect_ext ($self->{id});
1545 } 1548 }
1546 1549
1547 $self->SUPER::destroy; 1550 $self->SUPER::destroy;
1548} 1551}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines