ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.12
Committed: Tue May 30 14:59:26 2006 UTC (17 years, 11 months ago) by elmex
Branch: MAIN
Changes since 1.11: +3 -1 lines
Log Message:
fixed bug in inventory code

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.1 use CFClient::UI;
9    
10     use base 'Crossfire::Protocol::Base';
11    
12     sub new {
13     my $class = shift;
14    
15     my $self = $class->SUPER::new (@_);
16    
17     $self->{map_widget}->clr_commands;
18    
19     my $parser = new Pod::POM;
20     my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod");
21    
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     my $status = $self->{facemap}->db_get (id => $id, BerkeleyDB::DB_RMW);
274     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     $txn->abort;
284     }
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     timeout => 10,
435     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     $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message});
451     $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message});
452     }
453    
454     sub spell_delete {
455     my ($self, $spell) = @_;
456     }
457    
458     sub addme_success {
459     my ($self) = @_;
460    
461     $self->send ("command output-sync $::CFG->{output_sync}");
462     $self->send ("command output-count $::CFG->{output_count}");
463    
464     my $parser = new Pod::POM;
465     my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod");
466    
467     my %skill_tooltip;
468    
469     for my $head2 ($pod->head1->[-2]->head2) {
470     $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content;
471     }
472    
473     for my $skill (values %{$self->{skill_info}}) {
474     $self->{map_widget}->add_command ("ready_skill $skill",
475     (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
476     . $skill_tooltip{$skill});
477     $self->{map_widget}->add_command ("use_skill $skill",
478     (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
479     . $skill_tooltip{$skill});
480     }
481     }
482    
483     sub eof {
484     my ($self) = @_;
485    
486     $self->{map_widget}->clr_commands;
487    
488     ::stop_game ();
489     }
490    
491     sub image_info {
492     my ($self, $numfaces) = @_;
493    
494     $self->{num_faces} = $numfaces;
495     $self->{face_prefetch} = [1 .. $numfaces];
496     $self->face_prefetch;
497     }
498    
499     sub face_prefetch {
500     my ($self) = @_;
501    
502     return unless $::CFG->{face_prefetch};
503    
504     if ($self->{num_faces}) {
505     return if @{ $self->{send_queue} || [] };
506     my $todo = @{ $self->{face_prefetch} }
507     or return;
508    
509     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
510    
511     $self->send ("requestinfo image_sums $face $face");
512    
513 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
514 root 1.1 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
515     } elsif (!exists $self->{num_faces}) {
516     $self->send ("requestinfo image_info");
517    
518     $self->{num_faces} = 0;
519    
520 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
521 root 1.1 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
522     }
523     }
524    
525     sub update_floorbox {
526     $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
527     return unless $::CONN;
528    
529     $::FLOORBOX->clear;
530    
531     my $row;
532     for (@{ $::CONN->{container}{0} }) {
533     if ($row < 7) {
534     local $_->{face_widget}; # hack to force recreation of widget
535     local $_->{desc_widget}; # hack to force recreation of widget
536     CFClient::Item::update_widgets $_;
537    
538     $::FLOORBOX->add (0, $row, $_->{face_widget});
539     $::FLOORBOX->add (1, $row, $_->{desc_widget});
540    
541     $row++;
542     } else {
543     $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More...");
544     last;
545     }
546     }
547     });
548    
549     $::WANT_REFRESH++;
550     }
551    
552     sub set_opencont {
553     my ($conn, $tag, $name) = @_;
554     $conn->{open_container} = $tag;
555 elmex 1.10
556     $::INV_RIGHT_HB->clear ();
557     $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
558    
559     if ($tag != 0) { # Floor isn't closable, is it?
560     $::INV_RIGHT_HB->add (new CFClient::UI::Button
561     text => "Close container",
562     tooltip => "Close the currently open container (if one is open)",
563     on_activate => sub {
564     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
565     if $tag != 0;
566     #if $CONN->{open_container} != 0;
567     },
568     );
569     }
570    
571 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
572     }
573    
574     sub update_container {
575     my ($tag) = @_;
576    
577     $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
578     if $tag == $::CONN->{open_container};
579     }
580    
581     sub container_add {
582     my ($self, $tag, $items) = @_;
583    
584     #d# print "container_add: container $tag ($self->{player}{tag})\n";
585    
586     if ($tag == 0) {
587     update_floorbox;
588     update_container (0);
589     } elsif ($tag == $self->{player}{tag}) {
590     $::INV->set_items ($self->{container}{$self->{player}{tag}})
591     } else {
592     update_container ($tag);
593     }
594    
595     # $self-<{player}{tag} => player inv
596     #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
597     }
598    
599     sub container_clear {
600     my ($self, $tag) = @_;
601    
602     #d# print "container_clear: container $tag ($self->{player}{tag})\n";
603    
604     if ($tag == 0) {
605     update_floorbox;
606     update_container (0);
607     } elsif ($tag == $self->{player}{tag}) {
608     $::INV->set_items ($self->{container}{$tag})
609 elmex 1.12 } else {
610     update_container ($tag);
611 root 1.1 }
612    
613     # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0};
614     }
615    
616     sub item_delete {
617     my ($self, @items) = @_;
618    
619     for (@items) {
620     #d# print "item_delete: $_->{tag} from $_->{container} ($self->{player}{tag})\n";
621    
622     if ($_->{container} == 0) {
623     update_floorbox;
624     update_container ($_->{tag});
625     } elsif ($_->{container} == $self->{player}{tag}) {
626     $::INV->set_items ($self->{container}{$self->{player}{tag}})
627     } else {
628 elmex 1.12 update_container ($_->{container});
629 root 1.1 }
630     }
631     }
632    
633     sub item_update {
634     my ($self, $item) = @_;
635    
636     #d# print "item_update: $item->{tag} in $item->{container} ($self->{player}{tag}) ($::CONN->{open_container})\n";
637    
638     if ($item->{tag} == $self->{player}{tag}) {
639     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $item->{weight} / 1000);
640     return;
641     }
642    
643     CFClient::Item::update_widgets $item;
644    
645 root 1.2 if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) {
646 root 1.1 set_opencont ($::CONN, 0, "Floor");
647    
648 root 1.2 } elsif ($item->{flags} & F_OPEN) {
649 root 1.1 set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item);
650     } else {
651     if ($item->{container} == 0) {
652     update_floorbox;
653     update_container (0);
654     } elsif ($item->{container} == $self->{player}{tag}) {
655     $::INV->set_items ($self->{container}{$item->{container}})
656     }
657     }
658     }
659    
660 elmex 1.3 sub player_update {
661     my ($self, $player) = @_;
662     $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000);
663     }
664    
665 root 1.1 1;