ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Protocol.pm
Revision: 1.10
Committed: Mon May 29 20:22:33 2006 UTC (18 years ago) by elmex
Branch: MAIN
Changes since 1.9: +16 -1 lines
Log Message:
fixed inventory layout

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     title => "Query",
345 root 1.8 child => my $vbox = new CFClient::UI::VBox,
346     on_visibility_change => sub {
347     my ($self, $visible) = @_;
348     $self->center if $visible;
349     },
350     ;
351 root 1.1
352     $vbox->add (new CFClient::UI::Label
353     max_w => $::WIDTH * 0.4,
354     ellipsise => 0,
355     text => $prompt);
356    
357 root 1.2 if ($flags & CS_QUERY_YESNO) {
358 root 1.1 $vbox->add (my $hbox = new CFClient::UI::HBox);
359     $hbox->add (new CFClient::UI::Button
360     text => "No",
361 root 1.7 on_activate => sub {
362 root 1.1 $self->send ("reply n");
363     $dialog->destroy;
364     $self->{map_widget}->focus_in;
365     }
366     );
367     $hbox->add (new CFClient::UI::Button
368     text => "Yes",
369 root 1.7 on_activate => sub {
370 root 1.1 $self->send ("reply y");
371     $dialog->destroy;
372     },
373     );
374    
375     $dialog->focus_in;
376    
377 root 1.2 } elsif ($flags & CS_QUERY_SINGLECHAR) {
378 root 1.1 $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)";
379     $vbox->add (my $entry = new CFClient::UI::Entry
380 root 1.7 on_changed => sub {
381 root 1.1 $self->send ("reply $_[1]");
382     $dialog->destroy;
383     },
384     );
385    
386     $entry->focus_in;
387    
388     } else {
389     $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
390    
391     $vbox->add (my $entry = new CFClient::UI::Entry
392 root 1.2 $flags & CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (),
393 root 1.7 on_activate => sub {
394 root 1.1 $self->send ("reply $_[1]");
395     $dialog->destroy;
396     },
397     );
398    
399     $entry->focus_in;
400     }
401    
402 root 1.8 $dialog->show;
403 root 1.1 }
404    
405     sub drawinfo {
406     my ($self, $color, $text) = @_;
407    
408     my @color = (
409     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
410     [1.00, 1.00, 1.00],
411     [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
412     [1.00, 0.00, 0.00],
413     [1.00, 0.54, 0.00],
414     [0.11, 0.56, 1.00],
415     [0.93, 0.46, 0.00],
416     [0.18, 0.54, 0.34],
417     [0.56, 0.73, 0.56],
418     [0.80, 0.80, 0.80],
419     [0.55, 0.41, 0.13],
420     [0.99, 0.77, 0.26],
421     [0.74, 0.65, 0.41],
422     );
423    
424     my $time = sprintf "%02d:%02d:%02d", (localtime time)[2,1,0];
425    
426     $text = CFClient::UI::Label::escape $text;
427     $text =~ s/\[b\](.*?)\[\/b\]/<b>\1<\/b>/g;
428     $text =~ s/\[color=(.*?)\](.*?)\[\/color\]/<span foreground='\1'>\2<\/span>/g;
429    
430     $self->{logview}->add_paragraph ($color[$color],
431     join "\n", map "$time $_", split /\n/, $text);
432    
433 elmex 1.5 $self->{statusbox}->add ($text,
434 root 1.1 group => $text,
435     fg => $color[$color],
436     timeout => 10,
437     tooltip_font => $::FONT_FIXED,
438     );
439     }
440    
441     sub drawextinfo {
442     my ($self, $color, $type, $subtype, $message) = @_;
443    
444     $self->drawinfo ($color, $message);
445     }
446    
447     sub spell_add {
448     my ($self, $spell) = @_;
449    
450     # TODO
451     # create a widget dynamically, using spell face (CF::Protocol downloads them)
452     $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     }
459    
460     sub addme_success {
461     my ($self) = @_;
462    
463     $self->send ("command output-sync $::CFG->{output_sync}");
464     $self->send ("command output-count $::CFG->{output_count}");
465    
466     my $parser = new Pod::POM;
467     my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod");
468    
469     my %skill_tooltip;
470    
471     for my $head2 ($pod->head1->[-2]->head2) {
472     $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content;
473     }
474    
475     for my $skill (values %{$self->{skill_info}}) {
476     $self->{map_widget}->add_command ("ready_skill $skill",
477     (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n")
478     . $skill_tooltip{$skill});
479     $self->{map_widget}->add_command ("use_skill $skill",
480     (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n")
481     . $skill_tooltip{$skill});
482     }
483     }
484    
485     sub eof {
486     my ($self) = @_;
487    
488     $self->{map_widget}->clr_commands;
489    
490     ::stop_game ();
491     }
492    
493     sub image_info {
494     my ($self, $numfaces) = @_;
495    
496     $self->{num_faces} = $numfaces;
497     $self->{face_prefetch} = [1 .. $numfaces];
498     $self->face_prefetch;
499     }
500    
501     sub face_prefetch {
502     my ($self) = @_;
503    
504     return unless $::CFG->{face_prefetch};
505    
506     if ($self->{num_faces}) {
507     return if @{ $self->{send_queue} || [] };
508     my $todo = @{ $self->{face_prefetch} }
509     or return;
510    
511     my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, ();
512    
513     $self->send ("requestinfo image_sums $face $face");
514    
515 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo",
516 root 1.1 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
517     } elsif (!exists $self->{num_faces}) {
518     $self->send ("requestinfo image_info");
519    
520     $self->{num_faces} = 0;
521    
522 elmex 1.5 $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch",
523 root 1.1 group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]);
524     }
525     }
526    
527     sub update_floorbox {
528     $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub {
529     return unless $::CONN;
530    
531     $::FLOORBOX->clear;
532    
533     my $row;
534     for (@{ $::CONN->{container}{0} }) {
535     if ($row < 7) {
536     local $_->{face_widget}; # hack to force recreation of widget
537     local $_->{desc_widget}; # hack to force recreation of widget
538     CFClient::Item::update_widgets $_;
539    
540     $::FLOORBOX->add (0, $row, $_->{face_widget});
541     $::FLOORBOX->add (1, $row, $_->{desc_widget});
542    
543     $row++;
544     } else {
545     $::FLOORBOX->add (1, $row, new CFClient::UI::Label text => "More...");
546     last;
547     }
548     }
549     });
550    
551     $::WANT_REFRESH++;
552     }
553    
554     sub set_opencont {
555     my ($conn, $tag, $name) = @_;
556     $conn->{open_container} = $tag;
557 elmex 1.10
558     $::INV_RIGHT_HB->clear ();
559     $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name);
560    
561     if ($tag != 0) { # Floor isn't closable, is it?
562     $::INV_RIGHT_HB->add (new CFClient::UI::Button
563     text => "Close container",
564     tooltip => "Close the currently open container (if one is open)",
565     on_activate => sub {
566     $::CONN->send ("apply $tag") # $::CONN->{open_container}")
567     if $tag != 0;
568     #if $CONN->{open_container} != 0;
569     },
570     );
571     }
572    
573 root 1.1 $::INVR->set_items ($conn->{container}{$tag});
574     }
575    
576     sub update_container {
577     my ($tag) = @_;
578    
579     $::INVR->set_items ($::CONN->{container}{$::CONN->{open_container}})
580     if $tag == $::CONN->{open_container};
581     }
582    
583     sub container_add {
584     my ($self, $tag, $items) = @_;
585    
586     #d# print "container_add: container $tag ($self->{player}{tag})\n";
587    
588     if ($tag == 0) {
589     update_floorbox;
590     update_container (0);
591     } elsif ($tag == $self->{player}{tag}) {
592     $::INV->set_items ($self->{container}{$self->{player}{tag}})
593     } else {
594     update_container ($tag);
595     }
596    
597     # $self-<{player}{tag} => player inv
598     #use PApp::Util; warn PApp::Util::dumpval $self->{container}{$self->{player}{tag}};
599     }
600    
601     sub container_clear {
602     my ($self, $tag) = @_;
603    
604     #d# print "container_clear: container $tag ($self->{player}{tag})\n";
605    
606     if ($tag == 0) {
607     update_floorbox;
608     update_container (0);
609     } elsif ($tag == $self->{player}{tag}) {
610     $::INV->set_items ($self->{container}{$tag})
611     }
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     update_container ($_->{tag});
629     }
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;