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.84 by root, Fri Sep 29 00:56:06 2006 UTC vs.
Revision 1.115 by root, Thu Jul 12 19:10:52 2007 UTC

4use strict; 4use strict;
5 5
6use Crossfire::Protocol::Constants; 6use Crossfire::Protocol::Constants;
7 7
8use CFPlus; 8use CFPlus;
9use CFPlus::DB;
9use CFPlus::UI; 10use CFPlus::UI;
10use CFPlus::Pod; 11use CFPlus::Pod;
12use CFPlus::Macro;
13use CFPlus::Item;
14
15use Crossfire::Protocol::Base 0.95;
11 16
12use base 'Crossfire::Protocol::Base'; 17use base 'Crossfire::Protocol::Base';
13 18
14sub new { 19sub new {
15 my $class = shift; 20 my ($class, %arg) = @_;
16 21
17 my $self = $class->SUPER::new (@_, setup_req => { extmap => 1 }); 22 my $self = $class->SUPER::new (%arg,
23 setup_req => {
24 extmap => 1,
25 excmd => 1,
26 ywidget => 1,
27 %{$arg{setup_req} || {}},
28 },
29 );
18 30
19 $self->{map_widget}->clr_commands; 31 $self->{map_widget}->clr_commands;
20 32
21 my @cmd_help = map { 33 my @cmd_help = map {
22 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x 34 $_->{kw}[0] =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
33 for @args; 45 for @args;
34 46
35 map ["$cmd$_", $text], 47 map ["$cmd$_", $text],
36 sort { (length $a) <=> (length $b) } 48 sort { (length $a) <=> (length $b) }
37 @args 49 @args
38 } sort { $a->{par} <=> $b->{par} } 50 } sort { $a->{par} <=> $b->{par} }
39 CFPlus::Pod::find command => "*"; 51 CFPlus::Pod::find command => "*";
40 52
53 $self->connect_ext (event_music => sub {
54 my ($ev) = @_;
55
56 return unless $::CFG->{bgm_enable};
57
58 my $faces = $ev->{faces};
59 my @songs;
60
61 # request music from server if appropriate
62 if (my $faces = $ev->{faces}) {
63 for (@$faces) {
64 my ($face, $pri, $chksum) = @$_;
65 utf8::downgrade $chksum;
66
67 $chksum = unpack "H*", $chksum;
68 $self->{music_map}{$face} = $chksum;
69
70 # check if already cached
71 CFPlus::DB::get resmap => $chksum, sub {
72 my ($meta) = @_;
73
74 if (defined $meta) {
75 $self->{music_meta}{$chksum} = $self->{json_coder}->decode ($meta);
76 } else {
77 ::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." });
78 $self->ask_face ($face, -100 - $pri, undef, sub {
79 my $len = length $_[1];
80 my ($meta, $data) = unpack "(w/a*)*", $_[1];
81
82 CFPlus::DB::write_file $chksum, $data, sub { };
83 CFPlus::DB::put resmap => $chksum => $meta, sub { };
84
85 $self->{music_meta}{$chksum} = $self->{json_coder}->decode ($meta);
86 ::message ({ markup => "downloaded song #$face, size $len octets" });
87
88 &::audio_music_set ($self->{songs});
89 });
90 }
91 }
92 }
93 }
94
95 if (my $play = $ev->{play}) {
96 &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]);
97 use Data::Dumper;
98 warn Dumper $self->{songs};#d#
99 }
100 });
101
102 $self->connect_ext (event_capabilities => sub {
103 my ($cap) = @_;
104
105 if (my $ts = $cap->{tileset}) {
106 if (my ($default) = grep $_->[2] & 1, @$ts) {
107 $self->{tileset} = $default;
108 $self->{tilesize} = $default->[3];
109 $self->setup_req (tileset => $default->[0]);
110
111 my $w = int $self->{mapw} * 32 / $self->{tilesize};
112 my $h = int $self->{maph} * 32 / $self->{tilesize};
113
114 $self->setup_req (mapsize => "${w}x${h}");
115 }
116 }
117 });
118
119 $self->{json_coder}
120 ->convert_blessed
121 ->filter_json_single_key_object (__widget_ref__ => sub {
122 $self->{widget}{$_[0]}
123 });
124
125 $self->connect_ext (ws_n => sub {
126 my ($arg) = @_;
127
128 $self->{widgetset}{$arg{id}} = {
129 w => {},
130 };
131 });
132
133 $self->connect_ext (ws_d => sub {
134 my ($arg) = @_;
135
136 my $ws = delete $self->{widgetset}{$arg{id}}
137 or return;
138
139 $_->destroy
140 for values %{$ws->{w}};
141 });
142
143 $self->connect_ext (ws_c => sub {
144 my ($arg) = @_;
145
146 my $args = $arg->{args} || {};
147
148 for my $ev (grep /^on_/, keys %$args) {
149 $args->{$ev} = sub {
150 my $id = shift->{s_id};
151 $self->send_exti_msg (w_e => id => $id, name => $ev, args => \@_);
152
153 1
154 };
155 }
156
157 if (my $widget = eval {
158 local $SIG{__DIE__};
159 "CFPlus::UI::$arg->{class}"->new (
160 %$args,
161 s_ws => $arg->{ws},
162 s_id => $arg->{id},
163 )
164 }
165 ) {
166 $self->{widget}{$arg->{id}}
167 = $self->{widgetset}{$arg->{ws}}{w}{$arg->{id}}
168 = $widget;
169
170 $widget->connect (on_destroy => sub {
171 my ($widget) = @_;
172
173 delete $self->{widget}{$widget->{s_id}};
174 delete $self->{widgetset}{$widget->{s_ws}}{$widget->{s_id}};
175 });
176 } else {
177 warn "server failed creating client-side widget " . (CFPlus::to_json $arg) . ": $@\n";
178 $self->send_exti_msg (w_e => id => $arg->{id}, name => "destroy");
179 }
180 });
181
182 $self->connect_ext (w_c => sub {
183 my ($arg) = @_;
184
185 my $w = $self->{widget}{$arg->{id}}
186 or return;
187 my $m = $arg->{name};
188
189 my $a = $arg->{args} || [];
190
191 if (exists $arg->{rid}) {
192 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@$a)]);
193 } else {
194 $w->$m (@$a);
195 }
196 });
197
198 $self->connect_ext (w_s => sub {
199 my ($arg) = @_;
200
201 my $w = $self->{widget}{$arg->{id}}
202 or return;
203
204 $w->{$arg->{name}} = $arg->{value};
205 });
206
207 $self->connect_ext (w_g => sub {
208 my ($arg) = @_;
209
210 my $w = $self->{widget}{$arg->{id}}
211 or return;
212
213 $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->{$arg->{name}}]);
214 });
215
216 $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub {
217 for my $ws (values %{delete $self->{widgetset} || {}}) {
218 $_->destroy
219 for values %{delete $ws->{w} || {}};
220 }
221 });
222
41 $self->{map_widget}->add_command (@$_) 223 $self->{map_widget}->add_command (@$_)
42 for @cmd_help; 224 for @cmd_help;
43
44 $self->{noface} = new_from_file CFPlus::Texture
45 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
46 225
47 { 226 {
48 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture 227 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
49 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1; 228 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
50 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 229 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
51 } 230 }
52 231
232 {
233 $self->{noface} = my $tex = new_from_file CFPlus::Texture
234 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1;
235 $self->{map}->set_texture (2, @$tex{qw(name w h s t)}, @{$tex->{minified}});
236 }
237
53 $self->{open_container} = 0; 238 $self->{open_container} = 0;
54 239
55 # "global"
56 $self->{tilecache} = CFPlus::db_table "tilecache"
57 or die "tilecache: unable to open database table";
58 $self->{facemap} = CFPlus::db_table "facemap"
59 or die "facemap: unable to open database table";
60
61 # per server 240 # per server
62 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}" 241 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
63 or die "mapcache_$self->{host}_$self->{port}: unable to open database table";
64 242
65 $self 243 $self
66} 244}
67 245
68sub logprint { 246sub logprint {
225 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 403 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
226 $::GAUGES->{food} ->set_value ($fo, $fo_m); 404 $::GAUGES->{food} ->set_value ($fo, $fo_m);
227 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 405 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
228 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 406 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
229 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 407 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
230 my $rng = $stats->{+CS_STAT_RANGE}; 408 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
231 $rng =~ s/^Range: //; # thank you so much dear server
232 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
233 my $title = $stats->{+CS_STAT_TITLE}; 409 my $title = $stats->{+CS_STAT_TITLE};
234 $title =~ s/^Player: //; 410 $title =~ s/^Player: //;
235 $::STATWIDS->{title} ->set_text ("Title: " . $title); 411 $::STATWIDS->{title} ->set_text ("Title: " . $title);
236 412
237 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR}); 413 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
286 if ($ev->{button} == 1) { 462 if ($ev->{button} == 1) {
287 $::CONN->user_send ("ready_skill $name"); 463 $::CONN->user_send ("ready_skill $name");
288 } elsif ($ev->{button} == 2) { 464 } elsif ($ev->{button} == 2) {
289 $::CONN->user_send ("use_skill $name"); 465 $::CONN->user_send ("use_skill $name");
290 } elsif ($ev->{button} == 3) { 466 } elsif ($ev->{button} == 3) {
467 my $shortname = CFPlus::shorten $name, 14;
291 (new CFPlus::UI::Menu 468 (new CFPlus::UI::Menu
292 items => [ 469 items => [
293 ["bind <i>ready_skill $name</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["ready_skill $name"]) }], 470 ["bind <i>ready_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["ready_skill $name"] }],
294 ["bind <i>use_skill $name</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["use_skill $name"]) }], 471 ["bind <i>use_skill $shortname</i> to a key" => sub { CFPlus::Macro::quick_macro ["use_skill $name"] }],
295 ], 472 ],
296 )->popup ($ev); 473 )->popup ($ev);
297 } else { 474 } else {
298 return 0; 475 return 0;
299 } 476 }
316 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 493 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
317 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 494 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
318 } 495 }
319} 496}
320 497
498sub macro_send {
499 my ($self, $macro) = @_;
500
501 for my $cmd (@{ $macro->{action} }) {
502 $self->send_command ($cmd);
503 }
504}
505
321sub user_send { 506sub user_send {
322 my ($self, $command) = @_; 507 my ($self, $command) = @_;
323 508
509 $self->{record}->($command)
324 if ($self->{record}) { 510 if $self->{record};
325 push @{$self->{record}}, $command;
326 }
327 511
328 $self->logprint ("send: ", $command); 512 $self->logprint ("send: ", $command);
329 $self->send_command ($command); 513 $self->send_command ($command);
330 ::status ($command); 514 ::status ($command);
331} 515}
332 516
333sub start_record { 517sub record {
334 my ($self) = @_; 518 my ($self, $cb) = @_;
335 519
336 $self->{record} = []; 520 $self->{record} = $cb;
337}
338
339sub stop_record {
340 my ($self) = @_;
341 return delete $self->{record};
342} 521}
343 522
344sub map_scroll { 523sub map_scroll {
345 my ($self, $dx, $dy) = @_; 524 my ($self, $dx, $dy) = @_;
346 525
367 or return; 546 or return;
368 547
369 my ($hash, $x, $y, $w, $h) = @$map_info; 548 my ($hash, $x, $y, $w, $h) = @$map_info;
370 549
371 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 550 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
372 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 551 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
373 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 552 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
374} 553}
375 554
376sub map_clear { 555sub map_clear {
377 my ($self) = @_; 556 my ($self) = @_;
381 560
382 $self->{map}->clear; 561 $self->{map}->clear;
383 delete $self->{map_widget}{magicmap}; 562 delete $self->{map_widget}{magicmap};
384} 563}
385 564
565sub bg_fetch {
566 my ($self) = @_;
567
568 my $id;
569
570 do {
571 $id = pop @{$self->{bg_fetch}}
572 or return;
573 } while $self->{texture}[$id];
574
575 CFPlus::DB::get tilecache => $id, sub {
576 my ($data) = @_;
577
578 return unless $self->{map}; # stop when destroyed
579
580 $self->set_texture ($id => $data)
581 if defined $data;
582
583 $self->bg_fetch;
584 };
585}
386 586
387sub load_map($$$) { 587sub load_map($$$) {
388 my ($self, $hash, $x, $y) = @_; 588 my ($self, $hash, $x, $y) = @_;
389 589
390 if (defined (my $data = $self->{mapcache}->get ($hash))) { 590 my $gen = $self->{map_change_gen};
591
592 CFPlus::DB::get $self->{mapcache} => $hash, sub {
593 return unless $gen == $self->{map_change_gen};
594
595 my ($data) = @_;
596
597 if (defined $data) {
391 $data = Compress::LZF::decompress $data; 598 $data = Compress::LZF::decompress $data;
392 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d# 599 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
393 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
394 my $data = $self->{tilecache}->get ($id)
395 or next;
396 600
397 $self->set_texture ($id => $data); 601 my $inprogress = @{ $self->{bg_fetch} || [] };
602 unshift @{ $self->{bg_fetch} }, $self->{map}->set_rect ($x, $y, $data);
603 $self->bg_fetch unless $inprogress;
398 } 604 }
399 } 605 };
400} 606}
401 607
402# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 608# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
403# (server resource,s latency, bandwidth), so this hack is warranted. 609# (server resource,s latency, bandwidth), so this hack is warranted.
404# the right fix is to make real tiled maps with an overview file 610# the right fix is to make real tiled maps with an overview file
468 674
469 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 675 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
470 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 676 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
471 677
472 } else { 678 } else {
679 my $gen = $self->{map_change_gen};
473 $self->send_mapinfo ("spatial $path$tile", sub { 680 $self->send_mapinfo ("spatial $path$tile", sub {
681 return unless $gen == $self->{map_change_gen};
682
474 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 683 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
475 684
476 return if $mode ne "spatial"; 685 return if $mode ne "spatial";
477 686
478 $x += $self->{map}->ox; 687 $x += $self->{map}->ox;
492 701
493sub map_change { 702sub map_change {
494 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 703 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
495 704
496 $self->flush_map; 705 $self->flush_map;
706
707 ++$self->{map_change_gen};
497 708
498 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 709 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
499 710
500 my $mapmapw = $self->{mapmap}->{w}; 711 my $mapmapw = $self->{mapmap}->{w};
501 my $mapmaph = $self->{mapmap}->{h}; 712 my $mapmaph = $self->{mapmap}->{h};
518 $self->load_map ($hash, $x, $y); 729 $self->load_map ($hash, $x, $y);
519 $self->flood_fill (0, 0, 0, "", $hash, $flags); 730 $self->flood_fill (0, 0, 0, "", $hash, $flags);
520} 731}
521 732
522sub face_find { 733sub face_find {
523 my ($self, $facenum, $face) = @_; 734 my ($self, $facenum, $face, $cb) = @_;
524 735
525 my $hash = "$face->{chksum},$face->{name}"; 736 my $hash = "$face->{chksum},$face->{name}";
526 737
527 my $id = $self->{facemap}->get ($hash); 738 my $id = CFPlus::DB::get_tile_id_sync $hash;
528 739
529 unless ($id) {
530 # create new id for face
531 # I love transactions
532 for (1..100) {
533 my $txn = $CFPlus::DB_ENV->txn_begin;
534 my $status = $self->{facemap}->db_get (id => $id);
535 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
536 $id = ($id || 64) + 1;
537 if ($self->{facemap}->put (id => $id) == 0
538 && $self->{facemap}->put ($hash => $id) == 0) {
539 $txn->txn_commit;
540
541 goto gotid;
542 }
543 }
544 $txn->txn_abort;
545 }
546
547 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
548 }
549
550gotid:
551 $face->{id} = $id; 740 $face->{id} = $id;
552 $self->{map}->set_face ($facenum => $id);
553 $self->{faceid}[$facenum] = $id;#d# 741 $self->{faceid}[$facenum] = $id;
554 742
555 my $face = $self->{tilecache}->get ($id); 743 $self->{map}->set_tileid ($facenum => $id);
556 744
557 if ($face) { 745 CFPlus::DB::get tilecache => $id, $cb;
558 #$self->face_prefetch;
559 $face
560 } else {
561 my $tex = $self->{noface};
562 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
563 undef
564 };
565} 746}
566 747
567sub face_update { 748sub face_update {
568 my ($self, $facenum, $face, $changed) = @_; 749 my ($self, $facenum, $face, $changed) = @_;
569 750
570 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 751 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
752 if $changed;
571 753
572 $self->set_texture ($face->{id} => delete $face->{image}); 754 $self->set_texture ($face->{id} => delete $face->{image});
755}
756
757sub smooth_update {
758 my ($self, $facenum, $face) = @_;
759
760 $self->{map}->set_smooth ($facenum, $face->{smoothface}, $face->{smoothlevel});
573} 761}
574 762
575sub set_texture { 763sub set_texture {
576 my ($self, $id, $data) = @_; 764 my ($self, $id, $data) = @_;
577 765
578 $self->{texture}[$id] ||= do { 766 $self->{texture}[$id] = my $tex =
579 my $tex =
580 new_from_image CFPlus::Texture 767 new_from_image CFPlus::Texture
581 $data, minify => 1, mipmap => 1; 768 $data, minify => 1, mipmap => 1;
582 769
583 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); 770 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
584 $self->{map_widget}->update; 771 $self->{map_widget}->update;
585
586 $tex
587 };
588} 772}
589 773
590sub sound_play { 774sub sound_play {
591 my ($self, $x, $y, $soundnum, $type) = @_; 775 my ($self, $x, $y, $soundnum, $type) = @_;
592 776
602 $LAST_QUERY = $prompt; 786 $LAST_QUERY = $prompt;
603 787
604 $self->{query}-> ($self, $flags, $prompt); 788 $self->{query}-> ($self, $flags, $prompt);
605} 789}
606 790
607sub drawinfo { 791sub sanitise_xml($) {
608 my ($self, $color, $text) = @_; 792 local $_ = shift;
609 793
610 my @color = ( 794 # we now weed out all tags we do not support
795 s%<(?!/?i>|/?u>|/?b>|fg |/fg>)%&lt;%g;
796 # now all entities
797 s/&(?!amp;|lt;|gt;|apos;|quot;|#[0-9]+;|#x[0-9a-fA-F]+;)/&amp;/g;
798
799 # handle some elements
800 s/<fg name='([^']*)'>(.*?)<\/fg>/<span foreground='$1'>$2<\/span>/gs;
801 s/<fg name="([^"]*)">(.*?)<\/fg>/<span foreground="$1">$2<\/span>/gs;
802
803 $_
804}
805
806our %NAME_TO_COLOR = (
807 black => 0,
808 white => 1,
809 darkblue => 2,
810 red => 3,
811 orange => 4,
812 lightblue => 5,
813 darkorange => 6,
814 green => 7,
815 darkgreen => 8,
816 grey => 9,
817 brown => 10,
818 yellow => 11,
819 tan => 12,
820);
821
822our @CF_COLOR = (
611 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00], 823 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
612 [1.00, 1.00, 1.00], 824 [1.00, 1.00, 1.00],
613 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55] 825 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
614 [1.00, 0.00, 0.00], 826 [1.00, 0.00, 0.00],
615 [1.00, 0.54, 0.00], 827 [1.00, 0.54, 0.00],
616 [0.11, 0.56, 1.00], 828 [0.11, 0.56, 1.00],
617 [0.93, 0.46, 0.00], 829 [0.93, 0.46, 0.00],
618 [0.18, 0.54, 0.34], 830 [0.18, 0.54, 0.34],
619 [0.56, 0.73, 0.56], 831 [0.56, 0.73, 0.56],
620 [0.80, 0.80, 0.80], 832 [0.80, 0.80, 0.80],
621 [0.75, 0.61, 0.20], 833 [0.75, 0.61, 0.20],
622 [0.99, 0.77, 0.26], 834 [0.99, 0.77, 0.26],
623 [0.74, 0.65, 0.41], 835 [0.74, 0.65, 0.41],
624 ); 836);
625 837
838sub msg {
839 my ($self, $color, $type, $text, @extra) = @_;
840
841 $text = sanitise_xml $text;
842
843 if (my $cb = $self->{cb_msg}{$type}) {
844 $_->($self, $color, $type, $text, @extra) for values %$cb;
845 } elsif ($type =~ /^(?:chargen-race-title|chargen-race-description)$/) {
846 $type =~ s/-/_/g;
847 $self->{$type} = $text;
848 } else {
626 $self->logprint ("info: ", $text); 849 $self->logprint ("msg: ", $text);
850 return if $color < 0; # negative color == ignore if not understood
627 851
628 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 852 my $fg = $CF_COLOR[$color % @CF_COLOR];
629 853
630 # try to create single paragraphs of multiple lines sent by the server 854 ## try to create single paragraphs of multiple lines sent by the server
855 # no longer neecssary with TRT servers
631 $text =~ s/(?<=\S)\n(?=\w)/ /g; 856 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
632 857
633 $text = CFPlus::asxml $text; 858 ::message ({ fg => $fg, markup => $_ })
634 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 859 for split /\n/, $text;
635 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
636 860
637 $self->{logview}->add_paragraph ({ fg => $color[$color], markup => $_ })
638 for map "<span foreground='#ffffff'>$time</span> $_", split /\n/, $text;
639 $self->{logview}->scroll_to_bottom;
640
641 $self->{statusbox}->add ($text, 861 $self->{statusbox}->add ($text,
642 group => $text, 862 group => $text,
643 fg => $color[$color], 863 fg => $fg,
644 timeout => $color >= 2 ? 180 : 10, 864 timeout => $color >= 2 ? 180 : 10,
645 tooltip_font => $::FONT_FIXED, 865 tooltip_font => $::FONT_FIXED,
646 ); 866 );
647} 867 }
648
649sub drawextinfo {
650 my ($self, $color, $type, $subtype, $message) = @_;
651
652 $self->drawinfo ($color, $message);
653} 868}
654 869
655sub spell_add { 870sub spell_add {
656 my ($self, $spell) = @_; 871 my ($self, $spell) = @_;
657 872
658 # try to create single paragraphs out of the multiple lines sent by the server 873 # try to create single paragraphs out of the multiple lines sent by the server
659 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 874 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
660 $spell->{message} =~ s/\n+$//; 875 $spell->{message} =~ s/\n+$//;
661 $spell->{message} ||= "Server did not provide a description for this spell."; 876 $spell->{message} ||= "Server did not provide a description for this spell.";
662 877
663 $::SPELL_PAGE->add_spell ($spell); 878 $::SPELL_LIST->add_spell ($spell);
664 879
665 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message}); 880 $self->{map_widget}->add_command ("invoke $spell->{name}", CFPlus::asxml $spell->{message});
666 $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message}); 881 $self->{map_widget}->add_command ("cast $spell->{name}", CFPlus::asxml $spell->{message});
667} 882}
668 883
669sub spell_delete { 884sub spell_delete {
670 my ($self, $spell) = @_; 885 my ($self, $spell) = @_;
671 886
672 $::SPELL_PAGE->remove_spell ($spell); 887 $::SPELL_LIST->remove_spell ($spell);
888}
889
890sub setup {
891 my ($self, $setup) = @_;
892
893 $self->{map_widget}->set_tilesize ($self->{tilesize});
894 $::MAP->resize ($self->{mapw}, $self->{maph});
673} 895}
674 896
675sub addme_success { 897sub addme_success {
676 my ($self) = @_; 898 my ($self) = @_;
677 899
883 $::SERVER_INFO->set_markup ( 1105 $::SERVER_INFO->set_markup (
884 "server <tt>$self->{host}:$self->{port}</tt>\n" 1106 "server <tt>$self->{host}:$self->{port}</tt>\n"
885 . "protocol version <tt>$self->{version}</tt>\n" 1107 . "protocol version <tt>$self->{version}</tt>\n"
886 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1108 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
887 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n" 1109 . "extended command support $yesno[$self->{setup}{extcmd} > 0]\n"
1110 . "examine command support $yesno[$self->{setup}{excmd} > 0]\n"
1111 . "editing support $yesno[!!$self->{editor_support}]\n"
888 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n" 1112 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1113 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
889 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1114 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
890 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1115 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
891 . "map size $self->{mapw}×$self->{maph}\n" 1116 . "map size $self->{mapw}×$self->{maph}\n"
892 ); 1117 );
1118
1119 ::setup_build_button ($self->{editor_support}->{builder_ui});
893} 1120}
894 1121
895sub logged_in { 1122sub logged_in {
896 my ($self) = @_; 1123 my ($self) = @_;
897 1124
898 $self->send_ext_req (cfplus_support => version => 1, sub { 1125 $self->send_ext_req (cfplus_support => version => 1, sub {
899 $self->{cfplus_ext} = $_[0]{version}; 1126 $self->{cfplus_ext} = $_[0]{version};
900 $self->update_server_info; 1127 $self->update_server_info;
901 1128
1129 if ($self->{cfplus_ext} >= 2) {
1130 $self->send_ext_req ("editor_support", sub {
1131 $self->{editor_support} = $_[0];
1132 $self->update_server_info;
1133
1134 0
1135 });
1136 }
1137
902 0 1138 0
903 }); 1139 });
904 1140
905 $self->update_server_info; 1141 $self->update_server_info;
906 1142
907 $self->send_command ("output-sync $::CFG->{output_sync}"); 1143 $self->send_command ("output-sync $::CFG->{output_sync}");
908 $self->send_command ("output-count $::CFG->{output_count}"); 1144 $self->send_command ("output-count $::CFG->{output_count}");
1145 $self->send_command ("output-rate $::CFG->{output_rate}") if $::CFG->{output_rate} > 0;
909 $self->send_command ("pickup $::CFG->{pickup}"); 1146 $self->send_command ("pickup $::CFG->{pickup}");
1147}
1148
1149sub buildat {
1150 my ($self, $builditem, $x, $y) = @_;
1151
1152 if ($self->{cfplus_ext}) {
1153 $self->send_ext_msg (builder_build => dx => $x, dy => $y, (ref ($builditem) eq 'HASH') ? %$builditem : (item => $builditem));
1154 }
910} 1155}
911 1156
912sub lookat { 1157sub lookat {
913 my ($self, $x, $y) = @_; 1158 my ($self, $x, $y) = @_;
914 1159
936 1181
937 (delete $self->{npc_dialog})->destroy 1182 (delete $self->{npc_dialog})->destroy
938 if $self->{npc_dialog}; 1183 if $self->{npc_dialog};
939 1184
940 $self->SUPER::destroy; 1185 $self->SUPER::destroy;
1186
1187 %$self = ();
941} 1188}
942 1189
943package CFPlus::NPCDialog; 1190package CFPlus::NPCDialog;
944 1191
945our @ISA = 'CFPlus::UI::Toplevel'; 1192our @ISA = 'CFPlus::UI::Toplevel';
957 kw => { hi => 0, yes => 0, no => 0 }, 1204 kw => { hi => 0, yes => 0, no => 0 },
958 has_close_button => 1, 1205 has_close_button => 1,
959 @_, 1206 @_,
960 ); 1207 );
961 1208
962 Scalar::Util::weaken (my $this = $self); 1209 CFPlus::weaken (my $this = $self);
963 1210
964 $self->connect (delete => sub { $this->destroy; 1 }); 1211 $self->connect (delete => sub { $this->destroy; 1 });
965 1212
966 # better use a pane... 1213 # better use a pane...
967 $self->add (my $hbox = new CFPlus::UI::HBox); 1214 $self->add (my $hbox = new CFPlus::UI::HBox);
1011}; 1258};
1012 1259
1013sub update_options { 1260sub update_options {
1014 my ($self) = @_; 1261 my ($self) = @_;
1015 1262
1016 Scalar::Util::weaken $self; 1263 CFPlus::weaken $self;
1017 1264
1018 $self->{options}->clear; 1265 $self->{options}->clear;
1019 $self->{options}->add ($self->{bye_button}); 1266 $self->{options}->add ($self->{bye_button});
1020 1267
1021 for my $kw (sort keys %{ $self->{kw} }) { 1268 for my $kw (sort keys %{ $self->{kw} }) {
1030} 1277}
1031 1278
1032sub feed { 1279sub feed {
1033 my ($self, $msg) = @_; 1280 my ($self, $msg) = @_;
1034 1281
1035 Scalar::Util::weaken $self; 1282 CFPlus::weaken $self;
1036 1283
1037 if ($msg->{msgtype} eq "reply") { 1284 if ($msg->{msgtype} eq "reply") {
1038 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1285 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1039 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1286 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1040 1287
1041 my $text = "\n" . CFPlus::asxml $msg->{msg}; 1288 my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg};
1042 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} }; 1289 my $match = join "|", map "\\b\Q$_\E\\b", sort { (length $b) <=> (length $a) } keys %{ $self->{kw} };
1043 my @link; 1290 my @link;
1044 $text =~ s{ 1291 $text =~ s{
1045 ($match) 1292 ($match)
1046 }{ 1293 }{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines