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.182 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.196 by root, Thu May 8 21:30:23 2008 UTC

1package dc::Protocol; 1package DC::Protocol;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Deliantra::Protocol::Constants; 6use Deliantra::Protocol::Constants;
7 7
8use dc; 8use DC;
9use dc::DB; 9use DC::DB;
10use dc::UI; 10use DC::UI;
11use dc::Pod; 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 16
17sub new { 17sub new {
18 my ($class, %arg) = @_; 18 my ($class, %arg) = @_;
19 19
20 my $self = $class->SUPER::new (%arg, 20 my $self = $class->SUPER::new (%arg,
21 setup_req => { 21 setup_req => {
22 extmap => 1, 22 extmap => 1,
23 excmd => 1, 23 excmd => 1,
24 widget => 1, 24 widget => 2,
25 %{$arg{setup_req} || {}}, 25 %{$arg{setup_req} || {}},
26 }, 26 },
27 ); 27 );
28 28
29 $self->{map_widget}->clr_commands; 29 $self->{map_widget}->clr_commands;
30 30
31 my @cmd_help = map { 31 my @cmd_help = map {
32 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 32 $_->[DC::Pod::N_KW][0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
33 or die "unparseable command help: $_->{kw}[0]"; 33 or die "unparseable command help: $_->[DC::Pod::N_KW][0]";
34 34
35 my $cmd = $1; 35 my $cmd = $1;
36 my @args = split /\|/, $2; 36 my @args = split /\|/, $2;
37 @args = (".*") unless @args; 37 @args = (".*") unless @args;
38 38
39 my (undef, @par) = dc::Pod::section_of $_; 39 my (undef, @par) = DC::Pod::section_of $_;
40 my $text = dc::Pod::as_label @par; 40 my $text = DC::Pod::as_label @par;
41 41
42 $_ = $_ eq ".*" ? "" : " $_" 42 $_ = $_ eq ".*" ? "" : " $_"
43 for @args; 43 for @args;
44 44
45 map ["$cmd$_", $text], 45 map ["$cmd$_", $text],
46 sort { (length $a) <=> (length $b) } 46 sort { (length $a) <=> (length $b) }
47 @args 47 @args
48 } sort { $a->{par} <=> $b->{par} } 48 } sort { $a->[DC::Pod::N_PAR] <=> $b->[DC::Pod::N_PAR] }
49 dc::Pod::find command => "*"; 49 DC::Pod::find command => "*";
50 50
51 $self->{json_coder} 51 $self->{json_coder}
52 ->convert_blessed 52 ->convert_blessed
53 ->filter_json_single_key_object ("\fw" => sub { 53 ->filter_json_single_key_object ("\fw" => sub {
54 $self->{widget}{$_[0]} 54 $self->{widget}{$_[0]}
64 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { 64 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
65 for my $ws (values %{delete $self->{widgetset} || {}}) { 65 for my $ws (values %{delete $self->{widgetset} || {}}) {
66 $_->destroy 66 $_->destroy
67 for values %{delete $ws->{w} || {}}; 67 for values %{delete $ws->{w} || {}};
68 } 68 }
69
70 delete $self->{items};
71 $::INV->clear;
72 $::INVR_HB->clear;
73 $::FLOORBOX->clear;
69 }); 74 });
70 75
71 $self->{map_widget}->add_command (@$_) 76 $self->{map_widget}->add_command (@$_)
72 for @cmd_help; 77 for @cmd_help;
73 78
74 { 79 {
75 $self->{dialogue} = my $tex = new_from_file dc::Texture 80 $self->{dialogue} = my $tex = new_from_file DC::Texture
76 dc::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 81 DC::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
77 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 82 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
78 } 83 }
79 84
80 { 85 {
81 $self->{noface} = my $tex = new_from_file dc::Texture 86 $self->{noface} = my $tex = new_from_file DC::Texture
82 dc::find_rcfile "noface.png", minify => 1, mipmap => 1; 87 DC::find_rcfile "noface.png", minify => 1, mipmap => 1;
83 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 88 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
84 } 89 }
85 90
86 $self->{open_container} = 0; 91 $self->{open_container} = 0;
87 92
102} 107}
103 108
104sub ext_capabilities { 109sub ext_capabilities {
105 my ($self, %cap) = @_; 110 my ($self, %cap) = @_;
106 111
107 #$self->send ("setup sound 0"); # we use a different protocol
108 $self->update_fx_want; 112 $self->update_fx_want;
109 113
110 $self->send_exti_req (resource => "exp_table", sub { 114 $self->send_exti_req (resource => "exp_table", sub {
111 my ($exp_table) = @_; 115 my ($exp_table) = @_;
112 116
142############################################################################# 146#############################################################################
143 147
144sub widget_associate { 148sub widget_associate {
145 my ($self, $ws, $id, $widget) = @_; 149 my ($self, $ws, $id, $widget) = @_;
146 150
147 $widget ||= new dc::UI::Bin; 151 $widget ||= new DC::UI::Bin;
148 152
149 $widget->{s_id} = $id; 153 $widget->{s_id} = $id;
150 $self->{widget}{$id} = $widget; 154 $self->{widget}{$id} = $widget;
151 155
152 if ($ws) { 156 if ($ws) {
188 my ($self, $ws, $id, $class, $args) = @_; 192 my ($self, $ws, $id, $class, $args) = @_;
189 193
190 $self->widget_associate ( 194 $self->widget_associate (
191 $ws, $id => scalar eval { 195 $ws, $id => scalar eval {
192 local $SIG{__DIE__}; 196 local $SIG{__DIE__};
193 "dc::UI::$class"->new (%$args) 197 "DC::UI::$class"->new (%$args)
194 } 198 }
195 ); 199 );
200}
201
202# widgetset create template
203sub ext_ws_ct {
204 my ($self, $ws, $type, $template, $done_cb, $cfg) = @_;
205
206 $done_cb ||= sub { };
207
208 my $parse_list; $parse_list = sub {
209 my ($list) = @_;
210 my @w;
211
212 while (@$list) {
213 my ($class, $args) = splice @$list, 0, 2;
214 my $name = delete $args->{s_id};
215 my $cl = delete $args->{s_cl};
216 my $cfg = delete $cfg->{$name};
217 my $id = delete $cfg->{id};
218 my $w = eval { "DC::UI::$class"->new (%$args, %{ $cfg || {} }) }
219 or next;
220
221 $self->widget_associate ($ws, $id, $w)
222 if $id;
223
224 $w->add ($parse_list->($cl))
225 if $cl;
226
227 push @w, $w;
228 }
229
230 @w
231 };
232
233 # either array reference, or face #
234 if ($type eq "inline") {
235 $done_cb->();
236 $parse_list->($template);
237 } elsif ($type eq "face") {
238 my $handler; $handler = $self->register_face_handler ($template, sub {
239 my ($face) = @_;
240
241 undef $handler;
242 $done_cb->();
243 $parse_list->($self->{json_coder}->decode ($face->{data}));
244 });
245 } else {
246 $done_cb->(0);
247 }
196} 248}
197 249
198# widgetset associate 250# widgetset associate
199sub ext_ws_a { 251sub ext_ws_a {
200 my ($self, %ass) = @_; 252 my ($self, %ass) = @_;
201 253
202 # everything that has a name, wether conceivably useful or not 254 # everything that has a name, wether conceivably useful or not
203 my %wkw = ( 255 my %wkw = (
204 root => $dc::UI::ROOT, 256 root => $DC::UI::ROOT,
205 tooltip => $dc::UI::TOOLTIP, 257 tooltip => $DC::UI::TOOLTIP,
206 258
207 mapwidget => $::MAPWIDGET, 259 mapwidget => $::MAPWIDGET,
208 buttonbar => $::BUTTONBAR, 260 buttonbar => $::BUTTONBAR,
209 metaserver => $::METASERVER, 261 metaserver => $::METASERVER,
210 buttonbar => $::BUTTONBAR, 262 buttonbar => $::BUTTONBAR,
228 spell_list => $::SPELL_LIST, 280 spell_list => $::SPELL_LIST,
229 281
230 floorbox => $::FLOORBOX, 282 floorbox => $::FLOORBOX,
231 help_window => $::HELP_WINDOW, 283 help_window => $::HELP_WINDOW,
232 message_window => $::MESSAGE_WINDOW, 284 message_window => $::MESSAGE_WINDOW,
285 message_dist => $::MESSAGE_DIST,
233 statusbox => $::SDTATUSBOX, 286 statusbox => $::SDTATUSBOX,
234 287
235 inv => $::INV, 288 inv => $::INV,
236 invr => $::INVR, 289 invr => $::INVR,
237 invr_hb => $::INVR_HB, 290 invr_hb => $::INVR_HB,
286 339
287# message window 340# message window
288sub ext_channel_info { 341sub ext_channel_info {
289 my ($self, $info) = @_; 342 my ($self, $info) = @_;
290 $self->{channels}->{$info->{id}} = $info; 343 $self->{channels}->{$info->{id}} = $info;
291 $::MESSAGE_WINDOW->add_channel ($info); 344 $::MESSAGE_DIST->add_channel ($info);
292} 345}
293 346
294############################################################################# 347#############################################################################
295 348
296sub logprint { 349sub logprint {
297 my ($self, @a) = @_; 350 my ($self, @a) = @_;
298 351
299 dc::DB::logprint "$Deliantra::VARDIR/log.$self->{host}" => (join "", @a), sub { }; 352 DC::DB::logprint "$Deliantra::VARDIR/log.$self->{host}" => (join "", @a), sub { };
300} 353}
301 354
302sub _stat_numdiff { 355sub _stat_numdiff {
303 my ($self, $name, $old, $new) = @_; 356 my ($self, $name, $old, $new) = @_;
304 357
476 $self->{stat_order} = join ",", map $_->[0], @order; 529 $self->{stat_order} = join ",", map $_->[0], @order;
477 530
478 $sktbl->clear; 531 $sktbl->clear;
479 532
480 my $sw = $self->{skillwid}{""} ||= [ 533 my $sw = $self->{skillwid}{""} ||= [
481 0, 0, (new dc::UI::Label text => "Experience", align => 1), 534 0, 0, (new DC::UI::Label text => "Experience", align => 1),
482 1, 0, (new dc::UI::Label text => "Lvl.", align => 1), 535 1, 0, (new DC::UI::Label text => "Lvl.", align => 1),
483 2, 0, (new dc::UI::Label text => "Progress", align => 0), 536 2, 0, (new DC::UI::Label text => "Progress"),
484 3, 0, (new dc::UI::Label text => "Skill", expand => 1), 537 3, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0),
485 4, 0, (new dc::UI::Label text => "Experience", align => 1), 538 4, 0, (new DC::UI::Label text => "Experience", align => 1),
486 5, 0, (new dc::UI::Label text => "Lvl.", align => 1), 539 5, 0, (new DC::UI::Label text => "Lvl.", align => 1),
487 6, 0, (new dc::UI::Label text => "Progress", align => 0), 540 6, 0, (new DC::UI::Label text => "Progress"),
488 7, 0, (new dc::UI::Label text => "Skill", expand => 1), 541 7, 0, (new DC::UI::Label text => "Skill", expand => 1, align => 0),
489 ]; 542 ];
490 543
491 my @add = @$sw; 544 my @add = @$sw;
492 545
493 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 546 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
505 if ($ev->{button} == 1) { 558 if ($ev->{button} == 1) {
506 $::CONN->user_send ("ready_skill $name"); 559 $::CONN->user_send ("ready_skill $name");
507 } elsif ($ev->{button} == 2) { 560 } elsif ($ev->{button} == 2) {
508 $::CONN->user_send ("use_skill $name"); 561 $::CONN->user_send ("use_skill $name");
509 } elsif ($ev->{button} == 3) { 562 } elsif ($ev->{button} == 3) {
510 my $shortname = dc::shorten $name, 14; 563 my $shortname = DC::shorten $name, 14;
511 (new dc::UI::Menu 564 (new DC::UI::Menu
512 items => [ 565 items => [
513 ["bind <i>ready_skill $shortname</i> to a key" => sub { dc::Macro::quick_macro ["ready_skill $name"] }], 566 ["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"] }], 567 ["bind <i>use_skill $shortname</i> to a key" => sub { DC::Macro::quick_macro ["use_skill $name"] }],
515 ], 568 ],
516 )->popup ($ev); 569 )->popup ($ev);
517 } else { 570 } else {
518 return 0; 571 return 0;
519 } 572 }
521 1 574 1
522 }; 575 };
523 576
524 my $sw = $self->{skillwid}{$idx} ||= [ 577 my $sw = $self->{skillwid}{$idx} ||= [
525 # exp 578 # exp
526 (new dc::UI::Label 579 (new DC::UI::Label
527 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP), 580 align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP),
528 581
529 # level 582 # level
530 (new dc::UI::Label 583 (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), 584 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL),
532 585
533 # progress 586 # progress
534 (new dc::UI::ExperienceProgress), 587 (new DC::UI::ExperienceProgress),
535 588
536 # label 589 # label
537 (new dc::UI::Label text => $name, on_button_down => $spell_cb, 590 (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), 591 can_events => 1, can_hover => 1, tooltip => (DC::Pod::section_label skill_description => $name) . $TOOLTIP_ALL),
539 ]; 592 ];
540 593
541 push @add, 594 push @add,
542 $x * 4 + 0, $y, $sw->[0], 595 $x * 4 + 0, $y, $sw->[0],
543 $x * 4 + 1, $y, $sw->[1], 596 $x * 4 + 1, $y, $sw->[1],
638 691
639 my ($hash, $x, $y, $w, $h) = @$map_info; 692 my ($hash, $x, $y, $w, $h) = @$map_info;
640 693
641 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h); 694 my $data = Compress::LZF::compress $self->{map}->get_rect ($x, $y, $w, $h);
642 $self->{map_cache_new}{$hash} = \$data; 695 $self->{map_cache_new}{$hash} = \$data;
643 dc::DB::put $self->{mapcache} => $hash => $data, sub { }; 696 DC::DB::put $self->{mapcache} => $hash => $data, sub { };
644} 697}
645 698
646sub map_clear { 699sub map_clear {
647 my ($self) = @_; 700 my ($self) = @_;
648 701
661 do { 714 do {
662 $tile = pop @{$self->{bg_fetch}} 715 $tile = pop @{$self->{bg_fetch}}
663 or return; 716 or return;
664 } while $self->{texture}[$tile]; 717 } while $self->{texture}[$tile];
665 718
666 dc::DB::get tilecache => $tile, sub { 719 DC::DB::get tilecache => $tile, sub {
667 my ($data) = @_; 720 my ($data) = @_;
668 721
669 return unless $self->{map}; # stop when destroyed 722 return unless $self->{map}; # stop when destroyed
670 723
671 if (defined $data) { 724 if (defined $data) {
699 }; 752 };
700 753
701 if (my $rdata = $self->{map_cache_old}{$hash}) { 754 if (my $rdata = $self->{map_cache_old}{$hash}) {
702 $cb->($$rdata); 755 $cb->($$rdata);
703 } else { 756 } else {
704 dc::DB::get $self->{mapcache} => $hash, $cb; 757 DC::DB::get $self->{mapcache} => $hash, $cb;
705 } 758 }
706} 759}
707 760
708# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 761# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
709# (server resource,s latency, bandwidth), so this hack is warranted. 762# (server resource,s latency, bandwidth), so this hack is warranted.
833 886
834sub face_find { 887sub face_find {
835 my ($self, $facenum, $face, $cb) = @_; 888 my ($self, $facenum, $face, $cb) = @_;
836 889
837 if ($face->{type} == 0) { # FT_FACE 890 if ($face->{type} == 0) { # FT_FACE
838 my $id = dc::DB::get_tile_id_sync $face->{name}; 891 my $id = DC::DB::get_tile_id_sync $face->{name};
839 892
840 $face->{id} = $id; 893 $face->{id} = $id;
841 $self->{map}->set_tileid ($facenum => $id); 894 $self->{map}->set_tileid ($facenum => $id);
842 895
843 dc::DB::get tilecache => $id, $cb; 896 DC::DB::get tilecache => $id, $cb;
844 897
845 } elsif ($face->{type} & 1) { # with metadata 898 } elsif ($face->{type} & 1) { # with metadata
846 dc::DB::get res_meta => $face->{name}, $cb; 899 DC::DB::get res_meta => $face->{name}, $cb;
847 900
848 } else { # no metadata 901 } else { # no metadata
849 dc::DB::get res_data => $face->{name}, $cb; 902 DC::DB::get res_data => $face->{name}, $cb;
850 } 903 }
851} 904}
852 905
853sub face_update { 906sub face_update {
854 my ($self, $facenum, $face, $changed) = @_; 907 my ($self, $facenum, $face, $changed) = @_;
855 908
856 if ($face->{type} == 0) { 909 if ($face->{type} == 0) {
857 # image, FT_FACE 910 # image, FT_FACE
858 dc::DB::put tilecache => $face->{id} => $face->{data}, sub { } 911 DC::DB::put tilecache => $face->{id} => $face->{data}, sub { }
859 if $changed; 912 if $changed;
860 913
861 $self->have_tile ($face->{id}, delete $face->{data}); 914 $self->have_tile ($face->{id}, delete $face->{data});
862 915
863 } elsif ($face->{type} & 1) { 916 } elsif ($face->{type} & 1) {
865 if ($changed) { # new data 918 if ($changed) { # new data
866 my ($meta, $data) = unpack "(w/a*)*", $face->{data}; 919 my ($meta, $data) = unpack "(w/a*)*", $face->{data};
867 $face->{data} = $meta; 920 $face->{data} = $meta;
868 921
869 # rely on strict ordering here and also on later fetch 922 # rely on strict ordering here and also on later fetch
870 dc::DB::put res_data => $face->{name} => $data, sub { }; 923 DC::DB::put res_data => $face->{name} => $data, sub { };
871 dc::DB::put res_meta => $face->{name} => $meta, sub { }; 924 DC::DB::put res_meta => $face->{name} => $meta, sub { };
872 } 925 }
873 926
874 $face->{data} = $self->{json_coder}->decode ($face->{data}); 927 $face->{data} = $self->{json_coder}->decode ($face->{data});
875 ::add_license ($face); 928 ::add_license ($face);
876 ::message ({ markup => dc::asxml "downloaded resource '$face->{data}{name}', type $face->{type}." }) 929 ::message ({ markup => DC::asxml "downloaded resource '$face->{data}{name}', type $face->{type}." })
877 if $changed; 930 if $changed;
878 931
879 if ($face->{type} == 3) { # FT_MUSIC 932 if ($face->{type} == 3) { # FT_MUSIC
880 &::audio_music_push ($facenum); 933 &::audio_music_push ($facenum);
881 } elsif ($face->{type} == 5) { # FT_SOUND 934 } elsif ($face->{type} == 5) { # FT_SOUND
882 &::audio_sound_push ($facenum); 935 &::audio_sound_push ($facenum);
883 } 936 }
884 937
885 } else { 938 } else {
886 # flat resource case, FT_RSRC 939 # flat resource case, FT_RSRC
887 dc::DB::put res_data => $face->{name} => $face->{data}, sub { } 940 DC::DB::put res_data => $face->{name} => $face->{data}, sub { }
888 if $changed; 941 if $changed;
889 } 942 }
890 943
891 if (my $cbs = $self->{face_cb}{$facenum}) { 944 if (my $cbs = $self->{face_cb}{$facenum}) {
892 $_->($face, $changed) for @$cbs; 945 $_->($face, $changed) for @$cbs;
903 my ($self, $tile, $data) = @_; 956 my ($self, $tile, $data) = @_;
904 957
905 return unless $self->{map}; 958 return unless $self->{map};
906 959
907 my $tex = $self->{texture}[$tile] ||= 960 my $tex = $self->{texture}[$tile] ||=
908 new dc::Texture 961 new DC::Texture
909 tile => $tile, 962 tile => $tile,
910 image => $data, delete_image => 1, 963 image => $data, delete_image => 1,
911 minify => 1, mipmap => 1; 964 minify => 1, mipmap => 1;
912 965
913 if (my $cbs = delete $self->{tile_cb}{$tile}) { 966 if (my $cbs = delete $self->{tile_cb}{$tile}) {
921 my ($self, $num, $cb) = @_; 974 my ($self, $num, $cb) = @_;
922 975
923 push @{$self->{face_cb}{$num}}, $cb; 976 push @{$self->{face_cb}{$num}}, $cb;
924 977
925 defined wantarray 978 defined wantarray
926 ? dc::guard { 979 ? DC::guard {
927 @{$self->{face_cb}{$num}} 980 @{$self->{face_cb}{$num}}
928 = grep $_ != $cb, 981 = grep $_ != $cb,
929 @{$self->{face_cb}{$num}}; 982 @{$self->{face_cb}{$num}};
930 } 983 }
931 : () 984 : ()
977 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g; 1030 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
978 1031
979 # handle some elements 1032 # handle some elements
980 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs; 1033 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
981 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs; 1034 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
1035
1036 s/\s+$//;
982 1037
983 $_ 1038 $_
984} 1039}
985 1040
986our %NAME_TO_COLOR = ( 1041our %NAME_TO_COLOR = (
1068 $spell->{message} =~ s/\n+$//; 1123 $spell->{message} =~ s/\n+$//;
1069 $spell->{message} ||= "Server did not provide a description for this spell."; 1124 $spell->{message} ||= "Server did not provide a description for this spell.";
1070 1125
1071 $::SPELL_LIST->add_spell ($spell); 1126 $::SPELL_LIST->add_spell ($spell);
1072 1127
1073 $self->{map_widget}->add_command ("invoke $spell->{name}", dc::asxml $spell->{message}); 1128 $self->{map_widget}->add_command ("invoke $spell->{name}", DC::asxml $spell->{message});
1074 $self->{map_widget}->add_command ("cast $spell->{name}", dc::asxml $spell->{message}); 1129 $self->{map_widget}->add_command ("cast $spell->{name}", DC::asxml $spell->{message});
1075} 1130}
1076 1131
1077sub spell_delete { 1132sub spell_delete {
1078 my ($self, $spell) = @_; 1133 my ($self, $spell) = @_;
1079 1134
1090sub addme_success { 1145sub addme_success {
1091 my ($self) = @_; 1146 my ($self) = @_;
1092 1147
1093 my %skill_help; 1148 my %skill_help;
1094 1149
1095 for my $node (dc::Pod::find skill_description => "*") { 1150 for my $node (DC::Pod::find skill_description => "*") {
1096 my (undef, @par) = dc::Pod::section_of $node; 1151 my (undef, @par) = DC::Pod::section_of $node;
1097 $skill_help{$node->{kw}[0]} = dc::Pod::as_label @par; 1152 $skill_help{$node->[DC::Pod::N_KW][0]} = DC::Pod::as_label @par;
1098 }; 1153 };
1099 1154
1100 for my $skill (values %{$self->{skill_info}}) { 1155 for my $skill (values %{$self->{skill_info}}) {
1101 $self->{map_widget}->add_command ("ready_skill $skill", 1156 $self->{map_widget}->add_command ("ready_skill $skill",
1102 (dc::asxml "Ready the skill '$skill'\n\n") 1157 (DC::asxml "Ready the skill '$skill'\n\n")
1103 . $skill_help{$skill}); 1158 . $skill_help{$skill});
1104 $self->{map_widget}->add_command ("use_skill $skill", 1159 $self->{map_widget}->add_command ("use_skill $skill",
1105 (dc::asxml "Immediately use the skill '$skill'\n\n") 1160 (DC::asxml "Immediately use the skill '$skill'\n\n")
1106 . $skill_help{$skill}); 1161 . $skill_help{$skill});
1107 } 1162 }
1108} 1163}
1109 1164
1110sub eof { 1165sub eof {
1114 1169
1115 ::stop_game (); 1170 ::stop_game ();
1116} 1171}
1117 1172
1118sub update_floorbox { 1173sub update_floorbox {
1119 $dc::UI::ROOT->on_refresh ($::FLOORBOX => sub { 1174 $DC::UI::ROOT->on_refresh ($::FLOORBOX => sub {
1120 return unless $::CONN; 1175 return unless $::CONN;
1121 1176
1122 $::FLOORBOX->clear; 1177 $::FLOORBOX->clear;
1123 1178
1124 my @add; 1179 my @add;
1125 1180
1126 my $row; 1181 my $row;
1127 for (sort { $a->{count} <=> $b->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) { 1182 for (sort { $b->{count} <=> $a->{count} } values %{ $::CONN->{container}{$::CONN->{open_container} || 0} }) {
1183 next if $_->{tag} & 0x80000000;
1128 if ($row < 6) { 1184 if ($row < 6) {
1129 local $_->{face_widget}; # hack to force recreation of widget 1185 local $_->{face_widget}; # hack to force recreation of widget
1130 local $_->{desc_widget}; # hack to force recreation of widget 1186 local $_->{desc_widget}; # hack to force recreation of widget
1131 dc::Item::update_widgets $_; 1187 DC::Item::update_widgets $_;
1132 1188
1133 push @add, 1189 push @add,
1134 0, $row, $_->{face_widget}, 1190 0, $row, $_->{face_widget},
1135 1, $row, $_->{desc_widget}; 1191 1, $row, $_->{desc_widget};
1136 1192
1137 $row++; 1193 $row++;
1138 } else { 1194 } else {
1139 push @add, 1, $row, new dc::UI::Button 1195 push @add, 1, $row, new DC::UI::Button
1140 text => "More...", 1196 text => "More...",
1141 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 }, 1197 on_activate => sub { ::toggle_player_page ($::INVENTORY_PAGE); 0 },
1142 ; 1198 ;
1143 last; 1199 last;
1144 } 1200 }
1145 } 1201 }
1146 if ($::CONN->{open_container}) { 1202 if ($::CONN->{open_container}) {
1147 push @add, 1, $row++, new dc::UI::Button 1203 push @add, 1, $row++, new DC::UI::Button
1148 text => "Close container", 1204 text => "Close container",
1149 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") } 1205 on_activate => sub { $::CONN->send ("apply $::CONN->{open_container}") }
1150 ; 1206 ;
1151 } 1207 }
1152 1208
1159sub set_opencont { 1215sub set_opencont {
1160 my ($conn, $tag, $name) = @_; 1216 my ($conn, $tag, $name) = @_;
1161 $conn->{open_container} = $tag; 1217 $conn->{open_container} = $tag;
1162 update_floorbox; 1218 update_floorbox;
1163 1219
1164 $::INVR_HB->clear (); 1220 $::INVR_HB->clear;
1165 $::INVR_HB->add (new dc::UI::Label align => 0, expand => 1, text => $name); 1221 $::INVR_HB->add (new DC::UI::Label expand => 1, text => $name);
1166 1222
1167 if ($tag != 0) { # Floor isn't closable, is it? 1223 if ($tag != 0) { # Floor isn't closable, is it?
1168 $::INVR_HB->add (new dc::UI::Button 1224 $::INVR_HB->add (new DC::UI::Button
1169 text => "Close container", 1225 text => "Close container",
1170 tooltip => "Close the currently open container (if one is open)", 1226 tooltip => "Close the currently open container (if one is open)",
1171 on_activate => sub { 1227 on_activate => sub {
1172 $::CONN->send ("apply $tag") # $::CONN->{open_container}") 1228 $::CONN->send ("apply $tag") # $::CONN->{open_container}")
1173 if $tag != 0; 1229 if $tag != 0;
1180} 1236}
1181 1237
1182sub update_containers { 1238sub update_containers {
1183 my ($self) = @_; 1239 my ($self) = @_;
1184 1240
1185 $dc::UI::ROOT->on_refresh ("update_containers_$self" => sub { 1241 $DC::UI::ROOT->on_refresh ("update_containers_$self" => sub {
1186 my $todo = delete $self->{update_container} 1242 my $todo = delete $self->{update_container}
1187 or return; 1243 or return;
1188 1244
1189 for my $tag (keys %$todo) { 1245 for my $tag (keys %$todo) {
1190 update_floorbox if $tag == 0 or $tag == $self->{open_container}; 1246 update_floorbox if $tag == 0 or $tag == $self->{open_container};
1225} 1281}
1226 1282
1227sub item_update { 1283sub item_update {
1228 my ($self, $item) = @_; 1284 my ($self, $item) = @_;
1229 1285
1230 #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n"; 1286 #print "item_update: $item->{tag} in $item->{container} pt($self->{player}{tag}) oc($::CONN->{open_container}) f($item->{flags})\n";
1231 1287
1232 dc::Item::update_widgets $item; 1288 DC::Item::update_widgets $item;
1233 1289
1234 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { 1290 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
1235 set_opencont ($::CONN, 0, "Floor"); 1291 set_opencont ($::CONN, 0, "Floor");
1236 1292
1237 } elsif ($item->{flags} & F_OPEN) { 1293 } elsif ($item->{flags} & F_OPEN) {
1238 set_opencont ($::CONN, $item->{tag}, dc::Item::desc_string $item); 1294 set_opencont ($::CONN, $item->{tag}, DC::Item::desc_string $item);
1239 1295
1240 } else { 1296 } else {
1241 $self->{update_container}{$item->{container}}++; 1297 $self->{update_container}{$item->{container}}++;
1242 $self->update_containers; 1298 $self->update_containers;
1243 } 1299 }
1254 1310
1255 my $weight = .001 * $self->{player}{weight}; 1311 my $weight = .001 * $self->{player}{weight};
1256 my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM}; 1312 my $limit = .001 * $self->{stat}{+CS_STAT_WEIGHT_LIM};
1257 1313
1258 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight); 1314 $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $weight);
1259 $::STATWIDS->{m_weight}->set_text (sprintf "%.1fkg", $limit); 1315 $::STATWIDS->{m_weight}->set_text (sprintf "Max Weight: %.1fkg", $limit);
1260 $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit); 1316 $::STATWIDS->{i_weight}->set_text (sprintf "%.1f/%.1fkg", $weight, $limit);
1261} 1317}
1262 1318
1263sub update_server_info { 1319sub update_server_info {
1264 my ($self) = @_; 1320 my ($self) = @_;
1272 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 1328 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1273 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n" 1329 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
1274 . "editing support $yesno[!!$self->{editor_support}]\n" 1330 . "editing support $yesno[!!$self->{editor_support}]\n"
1275 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n" 1331 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1276 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n" 1332 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
1277 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1333 . "client support $yesno[$self->{cfplus_ext} > 0]"
1278 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1334 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
1279 . "map size $self->{mapw}×$self->{maph}\n" 1335 . "map size $self->{mapw}×$self->{maph}\n"
1280 ); 1336 );
1281 1337
1282} 1338}
1315 $self->send_ext_req (lookat => $x, $y, sub { 1371 $self->send_ext_req (lookat => $x, $y, sub {
1316 my (%msg) = @_; 1372 my (%msg) = @_;
1317 1373
1318 if (exists $msg{npc_dialog}) { 1374 if (exists $msg{npc_dialog}) {
1319 # start npc chat dialog 1375 # start npc chat dialog
1320 $self->{npc_dialog} = new dc::NPCDialog:: 1376 $self->{npc_dialog} = new DC::NPCDialog::
1321 token => $msg{npc_dialog}, 1377 token => $msg{npc_dialog},
1322 title => "$msg{npc_dialog}[0] (NPC)", 1378 title => "$msg{npc_dialog}[0] (NPC)",
1323 conn => $self, 1379 conn => $self,
1324 ; 1380 ;
1325 } 1381 }
1338 $self->SUPER::destroy; 1394 $self->SUPER::destroy;
1339 1395
1340 %$self = (); 1396 %$self = ();
1341} 1397}
1342 1398
1343package dc::NPCDialog; 1399package DC::NPCDialog;
1344 1400
1345our @ISA = 'dc::UI::Toplevel'; 1401our @ISA = 'DC::UI::Toplevel';
1346 1402
1347sub new { 1403sub new {
1348 my $class = shift; 1404 my $class = shift;
1349 1405
1350 my $self = $class->SUPER::new ( 1406 my $self = $class->SUPER::new (
1357 kw => { hi => 0, yes => 0, no => 0 }, 1413 kw => { hi => 0, yes => 0, no => 0 },
1358 has_close_button => 1, 1414 has_close_button => 1,
1359 @_, 1415 @_,
1360 ); 1416 );
1361 1417
1362 dc::weaken (my $this = $self); 1418 DC::weaken (my $this = $self);
1363 1419
1364 $self->connect (delete => sub { $this->destroy; 1 }); 1420 $self->connect (delete => sub { $this->destroy; 1 });
1365 1421
1366 # better use a pane... 1422 # better use a pane...
1367 $self->add (my $hbox = new dc::UI::HBox); 1423 $self->add (my $hbox = new DC::UI::HBox);
1368 $hbox->add ($self->{textview} = new dc::UI::TextScroller expand => 1); 1424 $hbox->add ($self->{textview} = new DC::UI::TextScroller expand => 1);
1369 1425
1370 $hbox->add (my $vbox = new dc::UI::VBox); 1426 $hbox->add (my $vbox = new DC::UI::VBox);
1371 1427
1372 $vbox->add (new dc::UI::Label text => "Message Entry:"); 1428 $vbox->add (new DC::UI::Label text => "Message Entry:");
1373 $vbox->add ($self->{entry} = new dc::UI::Entry 1429 $vbox->add ($self->{entry} = new DC::UI::Entry
1374 tooltip => "#npc_message_entry", 1430 tooltip => "#npc_message_entry",
1375 on_activate => sub { 1431 on_activate => sub {
1376 my ($entry, $text) = @_; 1432 my ($entry, $text) = @_;
1377 1433
1378 return unless $text =~ /\S/; 1434 return unless $text =~ /\S/;
1382 1438
1383 0 1439 0
1384 }, 1440 },
1385 ); 1441 );
1386 1442
1387 $vbox->add ($self->{options} = new dc::UI::VBox); 1443 $vbox->add ($self->{options} = new DC::UI::VBox);
1388 1444
1389 $self->{bye_button} = new dc::UI::Button 1445 $self->{bye_button} = new DC::UI::Button
1390 text => "Bye (close)", 1446 text => "Bye (close)",
1391 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.", 1447 tooltip => "Use this button to end talking to the NPC. This also closes the dialog window.",
1392 on_activate => sub { $this->destroy; 1 }, 1448 on_activate => sub { $this->destroy; 1 },
1393 ; 1449 ;
1394 1450
1413}; 1469};
1414 1470
1415sub update_options { 1471sub update_options {
1416 my ($self) = @_; 1472 my ($self) = @_;
1417 1473
1418 dc::weaken $self; 1474 DC::weaken $self;
1419 1475
1420 $self->{options}->clear; 1476 $self->{options}->clear;
1421 $self->{options}->add ($self->{bye_button}); 1477 $self->{options}->add ($self->{bye_button});
1422 1478
1423 for my $kw (sort keys %{ $self->{kw} }) { 1479 for my $kw (sort keys %{ $self->{kw} }) {
1424 $self->{options}->add (new dc::UI::Button 1480 $self->{options}->add (new DC::UI::Button
1425 text => $kw, 1481 text => $kw,
1426 on_activate => sub { 1482 on_activate => sub {
1427 $self->send ($kw); 1483 $self->send ($kw);
1428 0 1484 0
1429 }, 1485 },
1432} 1488}
1433 1489
1434sub feed { 1490sub feed {
1435 my ($self, $type, @arg) = @_; 1491 my ($self, $type, @arg) = @_;
1436 1492
1437 dc::weaken $self; 1493 DC::weaken $self;
1438 1494
1439 if ($type eq "update") { 1495 if ($type eq "update") {
1440 my (%info) = @arg; 1496 my (%info) = @arg;
1441 1497
1442 $self->{kw}{$_} = 1 for @{$info{add_topics} || []}; 1498 $self->{kw}{$_} = 1 for @{$info{add_topics} || []};
1443 $self->{kw}{$_} = 0 for @{$info{del_topics} || []}; 1499 $self->{kw}{$_} = 0 for @{$info{del_topics} || []};
1444 1500
1445 if (exists $info{msg}) { 1501 if (exists $info{msg}) {
1446 my $text = "\n" . dc::Protocol::sanitise_xml $info{msg}; 1502 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} }; 1503 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1448 my @link; 1504 my @link;
1449 $text =~ s{ 1505 $text =~ s{
1450 ($match) 1506 ($match)
1451 }{ 1507 }{
1452 my $kw = $1; 1508 my $kw = $1;
1453 1509
1454 push @link, new dc::UI::Label 1510 push @link, new DC::UI::Label
1455 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>", 1511 markup => "<span foreground='#c0c0ff' underline='single'>$kw</span>",
1456 can_hover => 1, 1512 can_hover => 1,
1457 can_events => 1, 1513 can_events => 1,
1458 padding_x => 0, 1514 padding_x => 0,
1459 padding_y => 0, 1515 padding_y => 0,
1477} 1533}
1478 1534
1479sub send { 1535sub send {
1480 my ($self, $msg) = @_; 1536 my ($self, $msg) = @_;
1481 1537
1482 $self->{textview}->add_paragraph ({ markup => "\n" . dc::asxml $msg }); 1538 $self->{textview}->add_paragraph ({ markup => "\n" . DC::asxml $msg });
1483 $self->{textview}->scroll_to_bottom; 1539 $self->{textview}->scroll_to_bottom;
1484 1540
1485 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg); 1541 $self->{conn}->send_ext_msg (npc_dialog_tell => $self->{id}, $msg);
1486} 1542}
1487 1543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines