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.73 by root, Sun Aug 13 15:14:17 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 (@_); 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
50 } sort { $a->{par} <=> $b->{par} }
38 } CFPlus::Pod::find command_help => "*"; 51 CFPlus::Pod::find command => "*";
39 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
40 $self->{map_widget}->add_command (@$_) 223 $self->{map_widget}->add_command (@$_)
41 for @cmd_help; 224 for @cmd_help;
42 225
226 {
227 $self->{dialogue} = my $tex = new_from_file CFPlus::Texture
228 CFPlus::find_rcfile "dialogue.png", minify => 1, mipmap => 1;
229 $self->{map}->set_texture (1, @$tex{qw(name w h s t)}, @{$tex->{minified}});
230 }
231
232 {
43 $self->{noface} = new_from_file CFPlus::Texture 233 $self->{noface} = my $tex = new_from_file CFPlus::Texture
44 CFPlus::find_rcfile "noface.png", minify => 1, mipmap => 1; 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 }
45 237
46 $self->{open_container} = 0; 238 $self->{open_container} = 0;
47 239
48 # "global"
49 $self->{tilecache} = CFPlus::db_table "tilecache";
50 $self->{facemap} = CFPlus::db_table "facemap";
51
52 # per server 240 # per server
53 $self->{mapcache} = CFPlus::db_table "mapcache_$self->{host}_$self->{port}"; 241 $self->{mapcache} = "mapcache_$self->{host}_$self->{port}";
54 242
55 $self 243 $self
56} 244}
57 245
58sub logprint { 246sub logprint {
215 $::GAUGES->{mana} ->set_value ($sp, $sp_m); 403 $::GAUGES->{mana} ->set_value ($sp, $sp_m);
216 $::GAUGES->{food} ->set_value ($fo, $fo_m); 404 $::GAUGES->{food} ->set_value ($fo, $fo_m);
217 $::GAUGES->{grace} ->set_value ($gr, $gr_m); 405 $::GAUGES->{grace} ->set_value ($gr, $gr_m);
218 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64})) 406 $::GAUGES->{exp} ->set_text ("Exp: " . (::formsep ($stats->{+CS_STAT_EXP64}))
219 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")"); 407 . " (lvl " . ($stats->{+CS_STAT_LEVEL} * 1) . ")");
220 my $rng = $stats->{+CS_STAT_RANGE}; 408 $::GAUGES->{range} ->set_text ($stats->{+CS_STAT_RANGE});
221 $rng =~ s/^Range: //; # thank you so much dear server
222 $::GAUGES->{range} ->set_text ("Rng: " . $rng);
223 my $title = $stats->{+CS_STAT_TITLE}; 409 my $title = $stats->{+CS_STAT_TITLE};
224 $title =~ s/^Player: //; 410 $title =~ s/^Player: //;
225 $::STATWIDS->{title} ->set_text ("Title: " . $title); 411 $::STATWIDS->{title} ->set_text ("Title: " . $title);
226 412
227 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR}); 413 $::STATWIDS->{st_str} ->set_text (sprintf "%d" , $stats->{+CS_STAT_STR});
258 444
259 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>"; 445 my $TOOLTIP_ALL = "\n\n<small>Left click - ready skill\nMiddle click - use spell\nRight click - further options</small>";
260 446
261 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 447 my @TOOLTIP_LVL = (tooltip => "<b>Level</b>. The level of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
262 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1); 448 my @TOOLTIP_EXP = (tooltip => "<b>Experience</b>. The experience points you have in this skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
263 my @TOOLTIP_NAME = (tooltip => "<b>Name</b>. The name of the skill.$TOOLTIP_ALL", can_events => 1, can_hover => 1);
264 449
265 my ($x, $y) = (0, 1); 450 my ($x, $y) = (0, 1);
266 for ( 451 for (
267 sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] } 452 sort { $stats->{$b->[0]}[1] <=> $stats->{$a->[0]}[1] or $a->[1] cmp $b->[1] }
268 map [$_, $self->{skill_info}{$_}], 453 map [$_, $self->{skill_info}{$_}],
277 if ($ev->{button} == 1) { 462 if ($ev->{button} == 1) {
278 $::CONN->user_send ("ready_skill $name"); 463 $::CONN->user_send ("ready_skill $name");
279 } elsif ($ev->{button} == 2) { 464 } elsif ($ev->{button} == 2) {
280 $::CONN->user_send ("use_skill $name"); 465 $::CONN->user_send ("use_skill $name");
281 } elsif ($ev->{button} == 3) { 466 } elsif ($ev->{button} == 3) {
467 my $shortname = CFPlus::shorten $name, 14;
282 (new CFPlus::UI::Menu 468 (new CFPlus::UI::Menu
283 items => [ 469 items => [
284 ["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"] }],
285 ["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"] }],
286 ], 472 ],
287 )->popup ($ev); 473 )->popup ($ev);
288 } else { 474 } else {
289 return 0; 475 return 0;
290 } 476 }
294 480
295 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label 481 $sktbl->add ($x * 3 + 0, $y, $self->{stat_widget_exp}{$idx} = new CFPlus::UI::Label
296 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP); 482 text => "0", align => 1, font => $::FONT_FIXED, fg => [1, 1, 0], on_button_down => $spell_cb, @TOOLTIP_EXP);
297 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label 483 $sktbl->add ($x * 3 + 1, $y, $self->{stat_widget_lvl}{$idx} = new CFPlus::UI::Label
298 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL); 484 text => "0", align => 1, font => $::FONT_FIXED, fg => [0, 1, 0], padding_x => 4, on_button_down => $spell_cb, @TOOLTIP_LVL);
299 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb, @TOOLTIP_NAME); 485 $sktbl->add ($x * 3 + 2, $y, new CFPlus::UI::Label text => $name, on_button_down => $spell_cb,
486 can_events => 1, can_hover => 1, tooltip => (CFPlus::Pod::section_label skill_description => $name) . $TOOLTIP_ALL);
300 487
301 $x++ and ($x, $y) = (0, $y + 1); 488 $x++ and ($x, $y) = (0, $y + 1);
302 } 489 }
303 } 490 }
304 491
306 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1])); 493 $self->{stat_widget_exp}{$_}->set_text (::formsep ($stats->{$_}[1]));
307 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1); 494 $self->{stat_widget_lvl}{$_}->set_text ($stats->{$_}[0] * 1);
308 } 495 }
309} 496}
310 497
498sub macro_send {
499 my ($self, $macro) = @_;
500
501 for my $cmd (@{ $macro->{action} }) {
502 $self->send_command ($cmd);
503 }
504}
505
311sub user_send { 506sub user_send {
312 my ($self, $command) = @_; 507 my ($self, $command) = @_;
313 508
509 $self->{record}->($command)
314 if ($self->{record}) { 510 if $self->{record};
315 push @{$self->{record}}, $command;
316 }
317 511
318 $self->logprint ("send: ", $command); 512 $self->logprint ("send: ", $command);
319 $self->send_command ($command); 513 $self->send_command ($command);
320 ::status ($command); 514 ::status ($command);
321} 515}
322 516
323sub start_record { 517sub record {
324 my ($self) = @_; 518 my ($self, $cb) = @_;
325 519
326 $self->{record} = []; 520 $self->{record} = $cb;
327}
328
329sub stop_record {
330 my ($self) = @_;
331 return delete $self->{record};
332} 521}
333 522
334sub map_scroll { 523sub map_scroll {
335 my ($self, $dx, $dy) = @_; 524 my ($self, $dx, $dy) = @_;
336 525
338} 527}
339 528
340sub feed_map1a { 529sub feed_map1a {
341 my ($self, $data) = @_; 530 my ($self, $data) = @_;
342 531
343 $self->{map}->map1a_update ($data); 532 $self->{map}->map1a_update ($data, $self->{setup}{extmap});
344 $self->{map_widget}->update; 533 $self->{map_widget}->update;
345} 534}
346 535
347sub magicmap { 536sub magicmap {
348 my ($self, $w, $h, $x, $y, $data) = @_; 537 my ($self, $w, $h, $x, $y, $data) = @_;
357 or return; 546 or return;
358 547
359 my ($hash, $x, $y, $w, $h) = @$map_info; 548 my ($hash, $x, $y, $w, $h) = @$map_info;
360 549
361 my $data = $self->{map}->get_rect ($x, $y, $w, $h); 550 my $data = $self->{map}->get_rect ($x, $y, $w, $h);
362 $self->{mapcache}->put ($hash => Compress::LZF::compress $data); 551 CFPlus::DB::put $self->{mapcache} => $hash => Compress::LZF::compress $data, sub { };
363 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d# 552 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
364} 553}
365 554
366sub map_clear { 555sub map_clear {
367 my ($self) = @_; 556 my ($self) = @_;
371 560
372 $self->{map}->clear; 561 $self->{map}->clear;
373 delete $self->{map_widget}{magicmap}; 562 delete $self->{map_widget}{magicmap};
374} 563}
375 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}
376 586
377sub load_map($$$) { 587sub load_map($$$) {
378 my ($self, $hash, $x, $y) = @_; 588 my ($self, $hash, $x, $y) = @_;
379 589
380 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) {
381 $data = Compress::LZF::decompress $data; 598 $data = Compress::LZF::decompress $data;
382 #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#
383 for my $id ($self->{map}->set_rect ($x, $y, $data)) {
384 my $data = $self->{tilecache}->get ($id)
385 or next;
386 600
387 $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;
388 } 604 }
389 } 605 };
390} 606}
391 607
392# hardcode /world/world_xxx_xxx map names, the savings are enourmous, 608# hardcode /world/world_xxx_xxx map names, the savings are enourmous,
393# (server resource,s latency, bandwidth), so this hack is warranted. 609# (server resource,s latency, bandwidth), so this hack is warranted.
394# 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
458 674
459 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags) 675 $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
460 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 676 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
461 677
462 } else { 678 } else {
679 my $gen = $self->{map_change_gen};
463 $self->send_mapinfo ("spatial $path$tile", sub { 680 $self->send_mapinfo ("spatial $path$tile", sub {
681 return unless $gen == $self->{map_change_gen};
682
464 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 683 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
465 684
466 return if $mode ne "spatial"; 685 return if $mode ne "spatial";
467 686
468 $x += $self->{map}->ox; 687 $x += $self->{map}->ox;
482 701
483sub map_change { 702sub map_change {
484 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 703 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
485 704
486 $self->flush_map; 705 $self->flush_map;
706
707 ++$self->{map_change_gen};
487 708
488 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 709 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
489 710
490 my $mapmapw = $self->{mapmap}->{w}; 711 my $mapmapw = $self->{mapmap}->{w};
491 my $mapmaph = $self->{mapmap}->{h}; 712 my $mapmaph = $self->{mapmap}->{h};
508 $self->load_map ($hash, $x, $y); 729 $self->load_map ($hash, $x, $y);
509 $self->flood_fill (0, 0, 0, "", $hash, $flags); 730 $self->flood_fill (0, 0, 0, "", $hash, $flags);
510} 731}
511 732
512sub face_find { 733sub face_find {
513 my ($self, $facenum, $face) = @_; 734 my ($self, $facenum, $face, $cb) = @_;
514 735
515 my $hash = "$face->{chksum},$face->{name}"; 736 my $hash = "$face->{chksum},$face->{name}";
516 737
517 my $id = $self->{facemap}->get ($hash); 738 my $id = CFPlus::DB::get_tile_id_sync $hash;
518 739
519 unless ($id) {
520 # create new id for face
521 # I love transactions
522 for (1..100) {
523 my $txn = $CFPlus::DB_ENV->txn_begin;
524 my $status = $self->{facemap}->db_get (id => $id);
525 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
526 $id = ($id || 64) + 1;
527 if ($self->{facemap}->put (id => $id) == 0
528 && $self->{facemap}->put ($hash => $id) == 0) {
529 $txn->txn_commit;
530
531 goto gotid;
532 }
533 }
534 $txn->txn_abort;
535 }
536
537 CFPlus::fatal "maximum number of transaction retries reached - database problems?";
538 }
539
540gotid:
541 $face->{id} = $id; 740 $face->{id} = $id;
542 $self->{map}->set_face ($facenum => $id);
543 $self->{faceid}[$facenum] = $id;#d# 741 $self->{faceid}[$facenum] = $id;
544 742
545 my $face = $self->{tilecache}->get ($id); 743 $self->{map}->set_tileid ($facenum => $id);
546 744
547 if ($face) { 745 CFPlus::DB::get tilecache => $id, $cb;
548 #$self->face_prefetch;
549 $face
550 } else {
551 my $tex = $self->{noface};
552 $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
553 undef
554 };
555} 746}
556 747
557sub face_update { 748sub face_update {
558 my ($self, $facenum, $face, $changed) = @_; 749 my ($self, $facenum, $face, $changed) = @_;
559 750
560 $self->{tilecache}->put ($face->{id} => $face->{image}) if $changed; 751 CFPlus::DB::put tilecache => $face->{id} => $face->{image}, sub { }
752 if $changed;
561 753
562 $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});
563} 761}
564 762
565sub set_texture { 763sub set_texture {
566 my ($self, $id, $data) = @_; 764 my ($self, $id, $data) = @_;
567 765
568 $self->{texture}[$id] ||= do { 766 $self->{texture}[$id] = my $tex =
569 my $tex =
570 new_from_image CFPlus::Texture 767 new_from_image CFPlus::Texture
571 $data, minify => 1, mipmap => 1; 768 $data, minify => 1, mipmap => 1;
572 769
573 $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}});
574 $self->{map_widget}->update; 771 $self->{map_widget}->update;
575
576 $tex
577 };
578} 772}
579 773
580sub sound_play { 774sub sound_play {
581 my ($self, $x, $y, $soundnum, $type) = @_; 775 my ($self, $x, $y, $soundnum, $type) = @_;
582 776
592 $LAST_QUERY = $prompt; 786 $LAST_QUERY = $prompt;
593 787
594 $self->{query}-> ($self, $flags, $prompt); 788 $self->{query}-> ($self, $flags, $prompt);
595} 789}
596 790
597sub drawinfo { 791sub sanitise_xml($) {
598 my ($self, $color, $text) = @_; 792 local $_ = shift;
599 793
600 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 = (
601 [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],
602 [1.00, 1.00, 1.00], 824 [1.00, 1.00, 1.00],
603 [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]
604 [1.00, 0.00, 0.00], 826 [1.00, 0.00, 0.00],
605 [1.00, 0.54, 0.00], 827 [1.00, 0.54, 0.00],
606 [0.11, 0.56, 1.00], 828 [0.11, 0.56, 1.00],
607 [0.93, 0.46, 0.00], 829 [0.93, 0.46, 0.00],
608 [0.18, 0.54, 0.34], 830 [0.18, 0.54, 0.34],
609 [0.56, 0.73, 0.56], 831 [0.56, 0.73, 0.56],
610 [0.80, 0.80, 0.80], 832 [0.80, 0.80, 0.80],
611 [0.75, 0.61, 0.20], 833 [0.75, 0.61, 0.20],
612 [0.99, 0.77, 0.26], 834 [0.99, 0.77, 0.26],
613 [0.74, 0.65, 0.41], 835 [0.74, 0.65, 0.41],
614 ); 836);
615 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 {
616 $self->logprint ("info: ", $text); 849 $self->logprint ("msg: ", $text);
850 return if $color < 0; # negative color == ignore if not understood
617 851
618 my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0]; 852 my $fg = $CF_COLOR[$color % @CF_COLOR];
619 853
620 # 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
621 $text =~ s/(?<=\S)\n(?=\w)/ /g; 856 #$text =~ s/(?<=\S)\n(?=\w)/ /g;
622 857
623 $text = CFPlus::asxml $text; 858 ::message ({ fg => $fg, markup => $_ })
624 $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g; 859 for split /\n/, $text;
625 $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
626 860
627 $self->{logview}->add_paragraph ({ fg => $color[$color], markup => $_ })
628 for map "<span foreground='#ffffff'>$time</span> $_", split /\n/, $text;
629 $self->{logview}->scroll_to_bottom;
630
631 $self->{statusbox}->add ($text, 861 $self->{statusbox}->add ($text,
632 group => $text, 862 group => $text,
633 fg => $color[$color], 863 fg => $fg,
634 timeout => $color >= 2 ? 180 : 10, 864 timeout => $color >= 2 ? 180 : 10,
635 tooltip_font => $::FONT_FIXED, 865 tooltip_font => $::FONT_FIXED,
636 ); 866 );
637} 867 }
638
639sub drawextinfo {
640 my ($self, $color, $type, $subtype, $message) = @_;
641
642 $self->drawinfo ($color, $message);
643} 868}
644 869
645sub spell_add { 870sub spell_add {
646 my ($self, $spell) = @_; 871 my ($self, $spell) = @_;
647 872
648 # 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
649 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g; 874 $spell->{message} =~ s/(?<=\S)\n(?=\w)/ /g;
650 $spell->{message} =~ s/\n+$//; 875 $spell->{message} =~ s/\n+$//;
651 $spell->{message} ||= "Server did not provide a description for this spell."; 876 $spell->{message} ||= "Server did not provide a description for this spell.";
652 877
653 $::SPELL_PAGE->add_spell ($spell); 878 $::SPELL_LIST->add_spell ($spell);
654 879
655 $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});
656 $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});
657} 882}
658 883
659sub spell_delete { 884sub spell_delete {
660 my ($self, $spell) = @_; 885 my ($self, $spell) = @_;
661 886
662 $::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});
663} 895}
664 896
665sub addme_success { 897sub addme_success {
666 my ($self) = @_; 898 my ($self) = @_;
667 899
873 $::SERVER_INFO->set_markup ( 1105 $::SERVER_INFO->set_markup (
874 "server <tt>$self->{host}:$self->{port}</tt>\n" 1106 "server <tt>$self->{host}:$self->{port}</tt>\n"
875 . "protocol version <tt>$self->{version}</tt>\n" 1107 . "protocol version <tt>$self->{version}</tt>\n"
876 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n" 1108 . "minimap support $yesno[$self->{setup}{mapinfocmd} > 0]\n"
877 . "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"
1112 . "map attributes $yesno[$self->{setup}{extmap} > 0]\n"
1113 . "big image protocol support $yesno[$self->{setup}{fxix} > 0]\n"
878 . "cfplus support $yesno[$self->{cfplus_ext} > 0]" 1114 . "cfplus support $yesno[$self->{cfplus_ext} > 0]"
879 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n" 1115 . ($self->{cfplus_ext} > 0 ? ", version $self->{cfplus_ext}" : "") ."\n"
880 . "map size $self->{mapw}×$self->{maph}\n" 1116 . "map size $self->{mapw}×$self->{maph}\n"
881 ); 1117 );
1118
1119 ::setup_build_button ($self->{editor_support}->{builder_ui});
882} 1120}
883 1121
884sub logged_in { 1122sub logged_in {
885 my ($self) = @_; 1123 my ($self) = @_;
886 1124
887 $self->send_ext_req (cfplus_support => version => 1, sub { 1125 $self->send_ext_req (cfplus_support => version => 1, sub {
888 $self->{cfplus_ext} = $_[0]{version}; 1126 $self->{cfplus_ext} = $_[0]{version};
889 $self->update_server_info; 1127 $self->update_server_info;
890 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
891 0 1138 0
892 }); 1139 });
893 1140
894 $self->update_server_info; 1141 $self->update_server_info;
895 1142
896 $self->send_command ("output-sync $::CFG->{output_sync}"); 1143 $self->send_command ("output-sync $::CFG->{output_sync}");
897 $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;
898 $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 }
899} 1155}
900 1156
901sub lookat { 1157sub lookat {
902 my ($self, $x, $y) = @_; 1158 my ($self, $x, $y) = @_;
903 1159
921} 1177}
922 1178
923sub destroy { 1179sub destroy {
924 my ($self) = @_; 1180 my ($self) = @_;
925 1181
926 $self->{npc_dialog}->destroy 1182 (delete $self->{npc_dialog})->destroy
927 if $self->{npc_dialog}; 1183 if $self->{npc_dialog};
928 1184
929 $self->SUPER::destroy; 1185 $self->SUPER::destroy;
1186
1187 %$self = ();
930} 1188}
931 1189
932package CFPlus::NPCDialog; 1190package CFPlus::NPCDialog;
933 1191
934our @ISA = 'CFPlus::UI::FancyFrame'; 1192our @ISA = 'CFPlus::UI::Toplevel';
935 1193
936sub new { 1194sub new {
937 my $class = shift; 1195 my $class = shift;
938 1196
939 my $self = $class->SUPER::new ( 1197 my $self = $class->SUPER::new (
946 kw => { hi => 0, yes => 0, no => 0 }, 1204 kw => { hi => 0, yes => 0, no => 0 },
947 has_close_button => 1, 1205 has_close_button => 1,
948 @_, 1206 @_,
949 ); 1207 );
950 1208
951 Scalar::Util::weaken (my $this = $self); 1209 CFPlus::weaken (my $this = $self);
952 1210
953 $self->connect (delete => sub { $this->destroy; 1 }); 1211 $self->connect (delete => sub { $this->destroy; 1 });
954 1212
955 # better use a pane... 1213 # better use a pane...
956 $self->add (my $hbox = new CFPlus::UI::HBox); 1214 $self->add (my $hbox = new CFPlus::UI::HBox);
1000}; 1258};
1001 1259
1002sub update_options { 1260sub update_options {
1003 my ($self) = @_; 1261 my ($self) = @_;
1004 1262
1005 Scalar::Util::weaken $self; 1263 CFPlus::weaken $self;
1006 1264
1007 $self->{options}->clear; 1265 $self->{options}->clear;
1008 $self->{options}->add ($self->{bye_button}); 1266 $self->{options}->add ($self->{bye_button});
1009 1267
1010 for my $kw (sort keys %{ $self->{kw} }) { 1268 for my $kw (sort keys %{ $self->{kw} }) {
1019} 1277}
1020 1278
1021sub feed { 1279sub feed {
1022 my ($self, $msg) = @_; 1280 my ($self, $msg) = @_;
1023 1281
1024 Scalar::Util::weaken $self; 1282 CFPlus::weaken $self;
1025 1283
1026 if ($msg->{msgtype} eq "reply") { 1284 if ($msg->{msgtype} eq "reply") {
1027 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []}; 1285 $self->{kw}{$_} = 1 for @{$msg->{add_topics} || []};
1028 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []}; 1286 $self->{kw}{$_} = 0 for @{$msg->{del_topics} || []};
1029 1287
1030 my $text = "\n" . CFPlus::asxml $msg->{msg}; 1288 my $text = "\n" . CFPlus::Protocol::sanitise_xml $msg->{msg};
1031 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} };
1032 my @link; 1290 my @link;
1033 $text =~ s{ 1291 $text =~ s{
1034 ($match) 1292 ($match)
1035 }{ 1293 }{
1043 padding_y => 0, 1301 padding_y => 0,
1044 on_button_up => sub { 1302 on_button_up => sub {
1045 $self->send ($kw); 1303 $self->send ($kw);
1046 }; 1304 };
1047 1305
1048 chr 0xfffc 1306 "\x{fffc}"
1049 }giex; 1307 }giex;
1050 1308
1051 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link }); 1309 $self->{textview}->add_paragraph ({ markup => $text, widget => \@link });
1052 $self->{textview}->scroll_to_bottom; 1310 $self->{textview}->scroll_to_bottom;
1053 $self->update_options; 1311 $self->update_options;
1080 1338
1081 $self->SUPER::destroy; 1339 $self->SUPER::destroy;
1082} 1340}
1083 1341
10841 13421
1343

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines