ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.20
Committed: Mon Jun 5 05:31:13 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.19: +3 -4 lines
Log Message:
bugfixes, pod caching

File Contents

# User Rev Content
1 root 1.1 package CFClient::Protocol;
2    
3     use utf8;
4     use strict;
5    
6 root 1.2 use Crossfire::Protocol::Constants;
7    
8 root 1.20 use CFClient;
9 root 1.1 use CFClient::UI;
10    
11     use base 'Crossfire::Protocol::Base';
12    
13     sub new {
14     my $class = shift;
15    
16     my $self = $class->SUPER::new (@_);
17    
18     $self->{map_widget}->clr_commands;
19    
20 root 1.20 my $pod = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod";
21 root 1.1
22     for my $head2 ($pod->head1->[-2]->head2) {
23     $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x
24     or next;
25    
26     my $cmd = $1;
27     my @args = split /\|/, $2;
28     @args = (".*") unless @args;
29    
30 root 1.6 $_ = $_ eq ".*" ? "" : " $_"
31     for @args;
32    
33 root 1.1 my $text = CFClient::pod_to_pango $head2->content;
34    
35 root 1.6 $self->{map_widget}->add_command ("$cmd$_", $text)
36     for sort { (length $a) <=> (length $b) }
37     @args;
38 root 1.1 }
39    
40     $self->{noface} = new_from_file CFClient::Texture
41     CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1;
42    
43     $self->{open_container} = 0;
44    
45     # "global"
46     $self->{tilecache} = CFClient::db_table "tilecache";
47     $self->{facemap} = CFClient::db_table "facemap";
48    
49     # per server
50     $self->{mapcache} = CFClient::db_table "mapcache_$self->{host}_$self->{port}";
51    
52     $self
53     }
54    
55     sub stats_update {
56     my ($self, $stats) = @_;
57    
58 elmex 1.4 if (my $exp = $stats->{+CS_STAT_EXP64}) {
59 root 1.1 my $diff = $exp - $self->{prev_exp};
60 elmex 1.5 $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5)
61 root 1.1 if exists $self->{prev_exp} && $diff;
62     $self->{prev_exp} = $exp;
63     }
64    
65     ::update_stats_window ($stats);
66     }
67    
68     sub user_send {
69     my ($self, $command) = @_;
70    
71 elmex 1.9 if ($self->{record}) {
72     push @{$self->{record}}, $command;
73     }
74    
75 root 1.1 $self->send_command ($command);
76 root 1.2 ::status $command;
77 root 1.1 }
78    
79 elmex 1.9 sub start_record {
80     my ($self) = @_;
81    
82     $self->{record} = [];
83     }
84    
85     sub stop_record {
86     my ($self) = @_;
87     return delete $self->{record};
88     }
89    
90 root 1.1 sub map_scroll {
91     my ($self, $dx, $dy) = @_;
92    
93     $self->{map}->scroll ($dx, $dy);
94     }
95    
96     sub feed_map1a {
97     my ($self, $data) = @_;
98    
99     $self->{map}->map1a_update ($data);
100     $self->{map_widget}->update;
101     }
102    
103     sub flush_map {
104     my ($self) = @_;
105    
106     my $map_info = delete $self->{map_info}
107     or return;
108    
109     my ($hash, $x, $y, $w, $h) = @$map_info;
110    
111     my $data = $self->{map}->get_rect ($x, $y, $w, $h);
112     $self->{mapcache}->put ($hash => Compress::LZF::compress $data);
113     #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
114     }
115    
116     sub map_clear {
117     my ($self) = @_;
118    
119     $self->flush_map;
120     delete $self->{neigh_map};
121    
122     $self->{map}->clear;
123     }
124    
125    
126     sub load_map($$$) {
127     my ($self, $hash, $x, $y) = @_;
128    
129     if (defined (my $data = $self->{mapcache}->get ($hash))) {
130     $data = Compress::LZF::decompress $data;
131     #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
132     for my $id ($self->{map}->set_rect ($x, $y, $data)) {
133     my $data = $self->{tilecache}->get ($id)
134     or next;
135    
136     $self->set_texture ($id => $data);
137     }
138     }
139     }
140    
141     # hardcode /world/world_xxx_xxx map names, the savings are enourmous,
142     # (server resource,s latency, bandwidth), so this hack is warranted.
143     # the right fix is to make real tiled maps with an overview file
144     sub send_mapinfo {
145     my ($self, $data, $cb) = @_;
146    
147     if ($self->{map_info}[0] =~ m%^/world/world_(\d\d\d)_(\d\d\d)$%) {
148     my ($wx, $wy) = ($1, $2);
149    
150     if ($data =~ /^spatial ([1-4]+)$/) {
151     my @dx = (0, 0, 1, 0, -1);
152     my @dy = (0, -1, 0, 1, 0);
153     my ($dx, $dy);
154    
155     for (split //, $1) {
156     $dx += $dx[$_];
157     $dy += $dy[$_];
158     }
159    
160     $cb->(spatial => 15,
161     $self->{map_info}[1] - $self->{map}->ox + $dx * 50,
162     $self->{map_info}[2] - $self->{map}->oy + $dy * 50,
163     50, 50,
164     sprintf "/world/world_%03d_%03d", $wx + $dx, $wy + $dy
165     );
166    
167     return;
168     }
169     }
170    
171     $self->SUPER::send_mapinfo ($data, $cb);
172     }
173    
174     # this method does a "flood fill" into every tile direction
175     # it assumes that tiles are arranged in a rectangular grid,
176     # i.e. a map is the same as the left of the right map etc.
177     # failure to comply are harmless and result in display errors
178     # at worst.
179     sub flood_fill {
180     my ($self, $block, $gx, $gy, $path, $hash, $flags) = @_;
181    
182     # the server does not allow map paths > 6
183     return if 7 <= length $path;
184    
185     my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
186    
187     for (
188     [1, 3, 0, -1],
189     [2, 4, 1, 0],
190     [3, 1, 0, 1],
191     [4, 2, -1, 0],
192     ) {
193     my ($tile, $tile2, $dx, $dy) = @$_;
194    
195     next if $block & (1 << $tile);
196     my $block = $block | (1 << $tile2);
197    
198     my $gx = $gx + $dx;
199     my $gy = $gy + $dy;
200    
201     next unless $flags & (1 << ($tile - 1));
202     next if $self->{neigh_grid}{$gx, $gy}++;
203    
204     my $neigh = $self->{neigh_map}{$hash} ||= [];
205     if (my $info = $neigh->[$tile]) {
206     my ($flags, $x, $y, $w, $h, $hash) = @$info;
207    
208     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
209     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
210    
211     } else {
212     $self->send_mapinfo ("spatial $path$tile", sub {
213     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
214    
215     return if $mode ne "spatial";
216    
217     $x += $self->{map}->ox;
218     $y += $self->{map}->oy;
219    
220     $self->load_map ($hash, $x, $y)
221     unless $self->{neigh_map}{$hash}[5]++;#d#
222    
223     $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
224    
225     $self->flood_fill ($block, $gx, $gy, "$path$tile", $hash, $flags)
226     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
227     });
228     }
229     }
230     }
231    
232     sub map_change {
233     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
234    
235     $self->flush_map;
236    
237     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
238    
239     my $mapmapw = $self->{mapmap}->{w};
240     my $mapmaph = $self->{mapmap}->{h};
241    
242     $self->{neigh_rect} = [
243     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
244     $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
245     ];
246    
247     delete $self->{neigh_grid};
248    
249     $x += $ox;
250     $y += $oy;
251    
252     $self->{map_info} = [$hash, $x, $y, $w, $h];
253    
254     (my $map = $hash) =~ s/^.*?\/([^\/]+)$/\1/;
255     $::STATWIDS->{map}->set_text ("Map: " . $map);
256    
257     $self->load_map ($hash, $x, $y);
258     $self->flood_fill (0, 0, 0, "", $hash, $flags);
259     }
260    
261     sub face_find {
262     my ($self, $facenum, $face) = @_;
263    
264     my $hash = "$face->{chksum},$face->{name}";
265    
266     my $id = $self->{facemap}->get ($hash);
267    
268     unless ($id) {
269     # create new id for face
270     # I love transactions
271     for (1..100) {
272     my $txn = $CFClient::DB_ENV->txn_begin;
273 root 1.15 my $status = $self->{facemap}->db_get (id => $id);
274 root 1.1 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
275     $id = ($id || 16) + 1;
276     if ($self->{facemap}->put (id => $id) == 0
277     && $self->{facemap}->put ($hash => $id) == 0) {
278     $txn->txn_commit;
279    
280     goto gotid;
281     }
282     }
283 root 1.15 $txn->txn_abort;
284 root 1.1 }
285    
286     CFClient::fatal "maximum number of transaction retries reached - database problems?";
287     }
288    
289     gotid:
290     $face->{id} = $id;
291     $self->{map}->set_face ($facenum => $id);
292     $self->{faceid}[$facenum] = $id;#d#
293    
294     my $face = $self->{tilecache}->get ($id);
295    
296     if ($face) {
297     #$self->face_prefetch;
298     $face
299     } else {
300     my $tex = $self->{noface};
301     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
302     undef
303     };
304     }
305    
306     sub face_update {
307     my ($self, $facenum, $face) = @_;
308    
309     $self->{tilecache}->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
310    
311     $self->set_texture ($face->{id} => delete $face->{image});
312     }
313    
314     sub set_texture {
315     my ($self, $id, $data) = @_;
316    
317     $self->{texture}[$id] ||= do {
318     my $tex =
319     new_from_image CFClient::Texture
320     $data, minify => 1, mipmap => 1;
321    
322     $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
323     $self->{map_widget}->update;
324    
325     $tex
326     };
327     }
328    
329     sub sound_play {
330     my ($self, $x, $y, $soundnum, $type) = @_;
331    
332     $self->{sound_play}->($x, $y, $soundnum, $type);
333     }
334    
335     my $LAST_QUERY; # server is stupid, stupid, stupid
336    
337     sub query {
338     my ($self, $flags, $prompt) = @_;
339    
340     $prompt = $LAST_QUERY unless length $prompt;
341     $LAST_QUERY = $prompt;
342    
343     my $dialog = new CFClient::UI::FancyFrame
344 root 1.11 x => "center",
345     y => "center",
346 root 1.1 title => "Query",
347 root 1.8 child => my $vbox = new CFClient::UI::VBox,
348     ;
349 root 1.1
350     $vbox->add (new CFClient::UI::Label
351     max_w => $::WIDTH * 0.4,
352     ellipsise => 0,
353     text => $prompt);
354    
355 root 1.2 if ($flags & CS_QUERY_YESNO) {
356 root 1.1 $vbox->add (my $hbox = new CFClient::UI::HBox);
357     $hbox->add (new CFClient::UI::Button
358     text => "No",
359 root 1.7 on_activate => sub {
360 root 1.1 $self->send ("reply n");
361     $dialog->destroy;
362     $self->{map_widget}->focus_in;
363     }
364     );
365     $hbox->add (new CFClient::UI::Button
366     text => "Yes",
367 root 1.7 on_activate => sub {
368 root 1.1 $self->send ("reply y");
369     $dialog->destroy;
370     },
371     );
372    
373     $dialog->focus_in;
374    
375 root 1.2 } elsif ($flags & CS_QUERY_SINGLECHAR) {
376 root 1.1 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
377     $vbox->add (my $entry = new CFClient::UI::Entry
378 root 1.7 on_changed => sub {
379 root 1.1 $self->send ("reply $_[1]");
380     $dialog->destroy;
381     },
382     );
383    
384     $entry->focus_in;
385    
386     } else {
387     $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
388    
389     $vbox->add (my $entry = new CFClient::UI::Entry
390 root 1.2 $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
391 root 1.7 on_activate => sub {
392 root 1.1 $self->send ("reply $_[1]");
393     $dialog->destroy;
394     },
395     );
396    
397     $entry->focus_in;
398     }
399    
400 root 1.8 $dialog->show;
401 root 1.1 }
402    
403     sub drawinfo {
404     my ($self, $color, $text) = @_;
405    
406     my @color = (
407     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
408     [1.00, 1.00, 1.00],
409     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
410     [1.00, 0.00, 0.00],
411     [1.00, 0.54, 0.00],
412     [0.11, 0.56, 1.00],
413     [0.93, 0.46, 0.00],
414     [0.18, 0.54, 0.34],
415     [0.56, 0.73, 0.56],
416     [0.80, 0.80, 0.80],
417     [0.55, 0.41, 0.13],
418     [0.99, 0.77, 0.26],
419     [0.74, 0.65, 0.41],
420     );
421    
422     my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
423    
424     $text = CFClient::UI::Label::escape $text;
425     $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
426     $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
427    
428     $self->{logview}->add_paragraph ($color[$color],
429     join "\n", map "$time $_", split /\n/, $text);
430    
431 elmex 1.5 $self->{statusbox}->add ($text,
432 root 1.1 group => $text,
433     fg => $color[$color],
434 root 1.18 timeout => $color >= 2 ? 60 : 10,
435 root 1.1 tooltip_font => $::FONT_FIXED,
436     );
437     }
438    
439     sub drawextinfo {
440     my ($self, $color, $type, $subtype, $message) = @_;
441    
442     $self->drawinfo ($color, $message);
443     }
444    
445     sub spell_add {
446     my ($self, $spell) = @_;
447    
448     # TODO
449     # create a widget dynamically, using spell face (CF::Protocol downloads them)
450 root 1.16 $::SETUP_SPELLS->add_spell ($spell);
451 elmex 1.14
452 root 1.1 $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
453     $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
454     }
455    
456     sub spell_delete {
457     my ($self, $spell) = @_;
458 root 1.17 $::SETUP_SPELLS->remove_spell ($spell);
459 root 1.1 }
460    
461     sub addme_success {
462     my ($self) = @_;
463    
464 root 1.20 my $pod = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod";
465 root 1.1
466     my %skill_tooltip;
467    
468     for my $head2 ($pod->head1->[-2]->head2) {
469     $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content;
470     }
471    
472     for my $skill (values %{$self->{skill_info}}) {
473     $self->{map_widget}->add_command ("ready_skill $skill",
474     (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
475     . $skill_tooltip{$skill});
476     $self->{map_widget}->add_command ("use_skill $skill",
477     (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
478     . $skill_tooltip{$skill});
479     }
480     }
481    
482     sub eof {
483     my ($self) = @_;
484    
485     $self->{map_widget}->clr_commands;
486    
487     ::stop_game ();
488     }
489    
490     sub image_info {
491     my ($self, $numfaces) = @_;
492    
493     $self->{num_faces} = $numfaces;
494     $self->{face_prefetch} = [1 .. $numfaces];
495     $self->face_prefetch;
496     }
497    
498     sub face_prefetch {
499     my ($self) = @_;
500    
501     return unless $::CFG->{face_prefetch};
502    
503     if ($self->{num_faces}) {
504     return if @{ $self->{send_queue} || [] };
505     my $todo = @{ $self->{face_prefetch} }
506     or return;
507    
508     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
509    
510     $self->send ("requestinfo image_sums $face $face");
511    
512 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
513 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
514 root 1.1 } elsif (!exists $self->{num_faces}) {
515     $self->send ("requestinfo image_info");
516    
517     $self->{num_faces} = 0;
518    
519 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
520 root 1.19 group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]);
521 root 1.1 }
522     }
523    
524     sub update_floorbox {
525     $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
526     return unless $::CONN;
527    
528     $::FLOORBOX->clear;
529    
530     my $row;
531     for (@{ $::CONN->{container}{0} }) {
532     if ($row < 7) {
533     local $_->{face_widget}; # hack to force recreation of widget
534     local $_->{desc_widget}; # hack to force recreation of widget
535     CFClient::Item::update_widgets $_;
536    
537     $::FLOORBOX->add (0, $row, $_->{face_widget});
538     $::FLOORBOX->add (1, $row, $_->{desc_widget});
539    
540     $row++;
541     } else {
542     $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More...");
543     last;
544     }
545     }
546     });
547    
548     $::WANT_REFRESH++;
549     }
550    
551     sub set_opencont {
552     my ($conn, $tag, $name) = @_;
553     $conn->{open_container} = $tag;
554 elmex 1.10
555     $::INV_RIGHT_HB->clear ();
556     $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
557    
558     if ($tag != 0) { # Floor isn't closable, is it?
559     $::INV_RIGHT_HB->add (new CFClient::UI::Button
560     text => "Close container",
561     tooltip => "Close the currently open container (if one is open)",
562     on_activate => sub {
563     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
564     if $tag != 0;
565     #if $CONN->{open_container} != 0;
566     },
567     );
568     }
569    
570 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
571     }
572    
573     sub update_container {
574     my ($tag) = @_;
575    
576     $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
577     if $tag == $::CONN->{open_container};
578     }
579    
580     sub container_add {
581     my ($self, $tag, $items) = @_;
582    
583     #d# print "container_add: container $tag ($self->{player}{tag})\n";
584    
585     if ($tag == 0) {
586     update_floorbox;
587     update_container (0);
588     } elsif ($tag == $self->{player}{tag}) {
589     $::INV->set_items ($self->{container}{$self->{player}{tag}})
590     } else {
591     update_container ($tag);
592     }
593    
594     # $self-<{player}{tag} => player inv
595     #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
596     }
597    
598     sub container_clear {
599     my ($self, $tag) = @_;
600    
601     #d# print "container_clear: container $tag ($self->{player}{tag})\n";
602    
603     if ($tag == 0) {
604     update_floorbox;
605     update_container (0);
606     } elsif ($tag == $self->{player}{tag}) {
607     $::INV->set_items ($self->{container}{$tag})
608 elmex 1.12 } else {
609     update_container ($tag);
610 root 1.1 }
611    
612     # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
613     }
614    
615     sub item_delete {
616     my ($self, @items) = @_;
617    
618     for (@items) {
619     #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
620    
621     if ($_->{container} == 0) {
622     update_floorbox;
623     update_container ($_->{tag});
624     } elsif ($_->{container} == $self->{player}{tag}) {
625     $::INV->set_items ($self->{container}{$self->{player}{tag}})
626     } else {
627 elmex 1.12 update_container ($_->{container});
628 root 1.1 }
629     }
630     }
631    
632     sub item_update {
633     my ($self, $item) = @_;
634    
635     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
636    
637     if ($item->{tag} == $self->{player}{tag}) {
638     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
639     return;
640     }
641    
642     CFClient::Item::update_widgets $item;
643    
644 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
645 root 1.1 set_opencont ($::CONN, 0, "Floor");
646    
647 root 1.2 } elsif ($item->{flags} & F_OPEN) {
648 root 1.1 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
649     } else {
650     if ($item->{container} == 0) {
651     update_floorbox;
652     update_container (0);
653     } elsif ($item->{container} == $self->{player}{tag}) {
654     $::INV->set_items ($self->{container}{$item->{container}})
655     }
656     }
657     }
658    
659 elmex 1.3 sub player_update {
660     my ($self, $player) = @_;
661     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
662 root 1.13
663     # do it here because it is ignored earlier, and there is no "login" event
664     $self->send_command ("output-sync $::CFG->{output_sync}");
665     $self->send_command ("output-count $::CFG->{output_count}");
666 elmex 1.3 }
667    
668 root 1.1 1;