1 | package CFClient::Protocol; |
1 | package CFClient::Protocol; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
|
|
6 | use Crossfire::Protocol::Constants; |
|
|
7 | |
|
|
8 | use CFClient; |
6 | use CFClient::UI; |
9 | use CFClient::UI; |
7 | |
10 | |
8 | use base 'Crossfire::Protocol::Base'; |
11 | use base 'Crossfire::Protocol::Base'; |
9 | |
12 | |
10 | sub new { |
13 | sub new { |
… | |
… | |
12 | |
15 | |
13 | my $self = $class->SUPER::new (@_); |
16 | my $self = $class->SUPER::new (@_); |
14 | |
17 | |
15 | $self->{map_widget}->clr_commands; |
18 | $self->{map_widget}->clr_commands; |
16 | |
19 | |
17 | my $parser = new Pod::POM; |
20 | my $cmd_help = CFClient::load_pod CFClient::find_rcfile "pod/command_help.pod", command_help => 1, sub { |
18 | my $pod = $parser->parse_file (CFClient::find_rcfile "pod/command_help.pod"); |
21 | my ($pom) = @_; |
19 | |
22 | |
|
|
23 | my @cmd_help; |
|
|
24 | |
20 | for my $head2 ($pod->head1->[-2]->head2) { |
25 | for my $head2 ($pom->head1->[-2]->head2) { |
21 | $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
26 | $head2->title =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x |
22 | or next; |
27 | or next; |
23 | |
28 | |
24 | my $cmd = $1; |
29 | my $cmd = $1; |
25 | my @args = split /\|/, $2; |
30 | my @args = split /\|/, $2; |
26 | @args = (".*") unless @args; |
31 | @args = (".*") unless @args; |
27 | |
32 | |
|
|
33 | $_ = $_ eq ".*" ? "" : " $_" |
|
|
34 | for @args; |
|
|
35 | |
28 | my $text = CFClient::pod_to_pango $head2->content; |
36 | my $text = CFClient::pod_to_pango $head2->content; |
29 | |
37 | |
30 | for my $arg (@args) { |
38 | push @cmd_help, ["$cmd$_", $text] |
31 | $arg = $arg eq ".*" ? "" : " $arg"; |
39 | for sort { (length $a) <=> (length $b) } |
32 | |
40 | @args; |
33 | $self->{map_widget}->add_command ("$cmd$arg", $text); |
|
|
34 | } |
41 | } |
|
|
42 | |
|
|
43 | \@cmd_help |
35 | } |
44 | }; |
|
|
45 | |
|
|
46 | $self->{map_widget}->add_command (@$_) |
|
|
47 | for @$cmd_help; |
36 | |
48 | |
37 | $self->{noface} = new_from_file CFClient::Texture |
49 | $self->{noface} = new_from_file CFClient::Texture |
38 | CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; |
50 | CFClient::find_rcfile "noface.png", minify => 1, mipmap => 1; |
39 | |
51 | |
40 | $self->{open_container} = 0; |
52 | $self->{open_container} = 0; |
… | |
… | |
50 | } |
62 | } |
51 | |
63 | |
52 | sub stats_update { |
64 | sub stats_update { |
53 | my ($self, $stats) = @_; |
65 | my ($self, $stats) = @_; |
54 | |
66 | |
55 | if (my $exp = $stats->{Crossfire::Protocol::Base::CS_STAT_EXP64}) { |
67 | if (my $exp = $stats->{+CS_STAT_EXP64}) { |
56 | my $diff = $exp - $self->{prev_exp}; |
68 | my $diff = $exp - $self->{prev_exp}; |
57 | $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5) |
69 | $self->{statusbox}->add ("$diff experience gained", group => "experience $diff", fg => [0.5, 1, 0.5, 0.8], timeout => 5) |
58 | if exists $self->{prev_exp} && $diff; |
70 | if exists $self->{prev_exp} && $diff; |
59 | $self->{prev_exp} = $exp; |
71 | $self->{prev_exp} = $exp; |
60 | } |
72 | } |
… | |
… | |
63 | } |
75 | } |
64 | |
76 | |
65 | sub user_send { |
77 | sub user_send { |
66 | my ($self, $command) = @_; |
78 | my ($self, $command) = @_; |
67 | |
79 | |
|
|
80 | if ($self->{record}) { |
|
|
81 | push @{$self->{record}}, $command; |
|
|
82 | } |
|
|
83 | |
68 | $self->send_command ($command); |
84 | $self->send_command ($command); |
69 | status $command; |
85 | ::status $command; |
|
|
86 | } |
|
|
87 | |
|
|
88 | sub start_record { |
|
|
89 | my ($self) = @_; |
|
|
90 | |
|
|
91 | $self->{record} = []; |
|
|
92 | } |
|
|
93 | |
|
|
94 | sub stop_record { |
|
|
95 | my ($self) = @_; |
|
|
96 | return delete $self->{record}; |
70 | } |
97 | } |
71 | |
98 | |
72 | sub map_scroll { |
99 | sub map_scroll { |
73 | my ($self, $dx, $dy) = @_; |
100 | my ($self, $dx, $dy) = @_; |
74 | |
101 | |
… | |
… | |
250 | unless ($id) { |
277 | unless ($id) { |
251 | # create new id for face |
278 | # create new id for face |
252 | # I love transactions |
279 | # I love transactions |
253 | for (1..100) { |
280 | for (1..100) { |
254 | my $txn = $CFClient::DB_ENV->txn_begin; |
281 | my $txn = $CFClient::DB_ENV->txn_begin; |
255 | my $status = $self->{facemap}->db_get (id => $id, BerkeleyDB::DB_RMW); |
282 | my $status = $self->{facemap}->db_get (id => $id); |
256 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
283 | if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) { |
257 | $id = ($id || 16) + 1; |
284 | $id = ($id || 16) + 1; |
258 | if ($self->{facemap}->put (id => $id) == 0 |
285 | if ($self->{facemap}->put (id => $id) == 0 |
259 | && $self->{facemap}->put ($hash => $id) == 0) { |
286 | && $self->{facemap}->put ($hash => $id) == 0) { |
260 | $txn->txn_commit; |
287 | $txn->txn_commit; |
261 | |
288 | |
262 | goto gotid; |
289 | goto gotid; |
263 | } |
290 | } |
264 | } |
291 | } |
265 | $txn->abort; |
292 | $txn->txn_abort; |
266 | } |
293 | } |
267 | |
294 | |
268 | CFClient::fatal "maximum number of transaction retries reached - database problems?"; |
295 | CFClient::fatal "maximum number of transaction retries reached - database problems?"; |
269 | } |
296 | } |
270 | |
297 | |
… | |
… | |
320 | my ($self, $flags, $prompt) = @_; |
347 | my ($self, $flags, $prompt) = @_; |
321 | |
348 | |
322 | $prompt = $LAST_QUERY unless length $prompt; |
349 | $prompt = $LAST_QUERY unless length $prompt; |
323 | $LAST_QUERY = $prompt; |
350 | $LAST_QUERY = $prompt; |
324 | |
351 | |
325 | my $dialog = new CFClient::UI::FancyFrame |
352 | $self->{query}-> ($self, $flags, $prompt); |
326 | title => "Query", |
|
|
327 | child => my $vbox = new CFClient::UI::VBox; |
|
|
328 | |
|
|
329 | $vbox->add (new CFClient::UI::Label |
|
|
330 | max_w => $::WIDTH * 0.4, |
|
|
331 | ellipsise => 0, |
|
|
332 | text => $prompt); |
|
|
333 | |
|
|
334 | if ($flags & Crossfire::Protocol::Base::CS_QUERY_YESNO) { |
|
|
335 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
336 | $hbox->add (new CFClient::UI::Button |
|
|
337 | text => "No", |
|
|
338 | connect_activate => sub { |
|
|
339 | $self->send ("reply n"); |
|
|
340 | $dialog->destroy; |
|
|
341 | $self->{map_widget}->focus_in; |
|
|
342 | } |
|
|
343 | ); |
|
|
344 | $hbox->add (new CFClient::UI::Button |
|
|
345 | text => "Yes", |
|
|
346 | connect_activate => sub { |
|
|
347 | $self->send ("reply y"); |
|
|
348 | $dialog->destroy; |
|
|
349 | }, |
|
|
350 | ); |
|
|
351 | |
|
|
352 | $dialog->focus_in; |
|
|
353 | |
|
|
354 | } elsif ($flags & Crossfire::Protocol::Base::CS_QUERY_SINGLECHAR) { |
|
|
355 | $dialog->{tooltip} = "Press a key (click on the entry to make sure it has keyboard focus)"; |
|
|
356 | $vbox->add (my $entry = new CFClient::UI::Entry |
|
|
357 | connect_changed => sub { |
|
|
358 | $self->send ("reply $_[1]"); |
|
|
359 | $dialog->destroy; |
|
|
360 | }, |
|
|
361 | ); |
|
|
362 | |
|
|
363 | $entry->focus_in; |
|
|
364 | |
|
|
365 | } else { |
|
|
366 | $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)"; |
|
|
367 | |
|
|
368 | $vbox->add (my $entry = new CFClient::UI::Entry |
|
|
369 | $flags & Crossfire::Protocol::Base::CS_QUERY_HIDEINPUT ? (hiddenchar => "*") : (), |
|
|
370 | connect_activate => sub { |
|
|
371 | $self->send ("reply $_[1]"); |
|
|
372 | $dialog->destroy; |
|
|
373 | }, |
|
|
374 | ); |
|
|
375 | |
|
|
376 | $entry->focus_in; |
|
|
377 | } |
|
|
378 | |
|
|
379 | $dialog->show_centered; |
|
|
380 | } |
353 | } |
381 | |
354 | |
382 | sub drawinfo { |
355 | sub drawinfo { |
383 | my ($self, $color, $text) = @_; |
356 | my ($self, $color, $text) = @_; |
384 | |
357 | |
… | |
… | |
408 | join "\n", map "$time $_", split /\n/, $text); |
381 | join "\n", map "$time $_", split /\n/, $text); |
409 | |
382 | |
410 | $self->{statusbox}->add ($text, |
383 | $self->{statusbox}->add ($text, |
411 | group => $text, |
384 | group => $text, |
412 | fg => $color[$color], |
385 | fg => $color[$color], |
413 | timeout => 10, |
386 | timeout => $color >= 2 ? 60 : 10, |
414 | tooltip_font => $::FONT_FIXED, |
387 | tooltip_font => $::FONT_FIXED, |
415 | ); |
388 | ); |
416 | } |
389 | } |
417 | |
390 | |
418 | sub drawextinfo { |
391 | sub drawextinfo { |
… | |
… | |
424 | sub spell_add { |
397 | sub spell_add { |
425 | my ($self, $spell) = @_; |
398 | my ($self, $spell) = @_; |
426 | |
399 | |
427 | # TODO |
400 | # TODO |
428 | # create a widget dynamically, using spell face (CF::Protocol downloads them) |
401 | # create a widget dynamically, using spell face (CF::Protocol downloads them) |
|
|
402 | $::SETUP_SPELLS->add_spell ($spell); |
|
|
403 | |
429 | $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
404 | $self->{map_widget}->add_command ("invoke $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
430 | $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
405 | $self->{map_widget}->add_command ("cast $spell->{name}", CFClient::UI::Label::escape $spell->{message}); |
431 | } |
406 | } |
432 | |
407 | |
433 | sub spell_delete { |
408 | sub spell_delete { |
434 | my ($self, $spell) = @_; |
409 | my ($self, $spell) = @_; |
|
|
410 | $::SETUP_SPELLS->remove_spell ($spell); |
435 | } |
411 | } |
436 | |
412 | |
437 | sub addme_success { |
413 | sub addme_success { |
438 | my ($self) = @_; |
414 | my ($self) = @_; |
439 | |
415 | |
440 | $self->send ("command output-sync $::CFG->{output_sync}"); |
416 | my $skill_help = CFClient::load_pod CFClient::find_rcfile "pod/skill_help.pod", skill_help => 1, sub { |
441 | $self->send ("command output-count $::CFG->{output_count}"); |
417 | my ($pom) = @_; |
442 | |
418 | |
443 | my $parser = new Pod::POM; |
|
|
444 | my $pod = $parser->parse_file (CFClient::find_rcfile "pod/skill_help.pod"); |
|
|
445 | |
|
|
446 | my %skill_tooltip; |
419 | my %skill_help; |
447 | |
420 | |
448 | for my $head2 ($pod->head1->[-2]->head2) { |
421 | for my $head2 ($pom->head1->[3]->head2) { |
449 | $skill_tooltip{$head2->title} = CFClient::pod_to_pango $head2->content; |
422 | $skill_help{$head2->title} = CFClient::pod_to_pango $head2->content; |
|
|
423 | } |
|
|
424 | |
|
|
425 | \%skill_help |
450 | } |
426 | }; |
451 | |
427 | |
452 | for my $skill (values %{$self->{skill_info}}) { |
428 | for my $skill (values %{$self->{skill_info}}) { |
453 | $self->{map_widget}->add_command ("ready_skill $skill", |
429 | $self->{map_widget}->add_command ("ready_skill $skill", |
454 | (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") |
430 | (CFClient::UI::Label::escape "Ready the skill '$skill'\n\n") |
455 | . $skill_tooltip{$skill}); |
431 | . $skill_help->{$skill}); |
456 | $self->{map_widget}->add_command ("use_skill $skill", |
432 | $self->{map_widget}->add_command ("use_skill $skill", |
457 | (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") |
433 | (CFClient::UI::Label::escape "Immediately use the skill '$skill'\n\n") |
458 | . $skill_tooltip{$skill}); |
434 | . $skill_help->{$skill}); |
459 | } |
435 | } |
460 | } |
436 | } |
461 | |
437 | |
462 | sub eof { |
438 | sub eof { |
463 | my ($self) = @_; |
439 | my ($self) = @_; |
… | |
… | |
488 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
464 | my ($face) = splice @{ $self->{face_prefetch} }, + rand @{ $self->{face_prefetch} }, 1, (); |
489 | |
465 | |
490 | $self->send ("requestinfo image_sums $face $face"); |
466 | $self->send ("requestinfo image_sums $face $face"); |
491 | |
467 | |
492 | $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", |
468 | $self->{statusbox}->add (CFClient::UI::Label::escape "prefetching $todo", |
493 | group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); |
469 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
494 | } elsif (!exists $self->{num_faces}) { |
470 | } elsif (!exists $self->{num_faces}) { |
495 | $self->send ("requestinfo image_info"); |
471 | $self->send ("requestinfo image_info"); |
496 | |
472 | |
497 | $self->{num_faces} = 0; |
473 | $self->{num_faces} = 0; |
498 | |
474 | |
499 | $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", |
475 | $self->{statusbox}->add (CFClient::UI::Label::escape "starting to prefetch", |
500 | group => "prefetch", timeout => 2, fg => [1, 1, 0, 0.5]); |
476 | group => "prefetch", timeout => 3, fg => [1, 1, 0, 0.5]); |
501 | } |
477 | } |
502 | } |
478 | } |
503 | |
479 | |
504 | sub update_floorbox { |
480 | sub update_floorbox { |
505 | $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
481 | $CFClient::UI::ROOT->on_refresh ($::FLOORBOX => sub { |
… | |
… | |
529 | } |
505 | } |
530 | |
506 | |
531 | sub set_opencont { |
507 | sub set_opencont { |
532 | my ($conn, $tag, $name) = @_; |
508 | my ($conn, $tag, $name) = @_; |
533 | $conn->{open_container} = $tag; |
509 | $conn->{open_container} = $tag; |
534 | $::INVR_LBL->set_text ($name); |
510 | |
|
|
511 | $::INV_RIGHT_HB->clear (); |
|
|
512 | $::INV_RIGHT_HB->add (new CFClient::UI::Label align => 0, expand => 1, text => $name); |
|
|
513 | |
|
|
514 | if ($tag != 0) { # Floor isn't closable, is it? |
|
|
515 | $::INV_RIGHT_HB->add (new CFClient::UI::Button |
|
|
516 | text => "Close container", |
|
|
517 | tooltip => "Close the currently open container (if one is open)", |
|
|
518 | on_activate => sub { |
|
|
519 | $::CONN->send ("apply $tag") # $::CONN->{open_container}") |
|
|
520 | if $tag != 0; |
|
|
521 | #if $CONN->{open_container} != 0; |
|
|
522 | }, |
|
|
523 | ); |
|
|
524 | } |
|
|
525 | |
535 | $::INVR->set_items ($conn->{container}{$tag}); |
526 | $::INVR->set_items ($conn->{container}{$tag}); |
536 | } |
527 | } |
537 | |
528 | |
538 | sub update_container { |
529 | sub update_container { |
539 | my ($tag) = @_; |
530 | my ($tag) = @_; |
… | |
… | |
568 | if ($tag == 0) { |
559 | if ($tag == 0) { |
569 | update_floorbox; |
560 | update_floorbox; |
570 | update_container (0); |
561 | update_container (0); |
571 | } elsif ($tag == $self->{player}{tag}) { |
562 | } elsif ($tag == $self->{player}{tag}) { |
572 | $::INV->set_items ($self->{container}{$tag}) |
563 | $::INV->set_items ($self->{container}{$tag}) |
|
|
564 | } else { |
|
|
565 | update_container ($tag); |
573 | } |
566 | } |
574 | |
567 | |
575 | # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0}; |
568 | # use PApp::Util; warn PApp::Util::dumpval $self->{container}{0}; |
576 | } |
569 | } |
577 | |
570 | |
… | |
… | |
585 | update_floorbox; |
578 | update_floorbox; |
586 | update_container ($_->{tag}); |
579 | update_container ($_->{tag}); |
587 | } elsif ($_->{container} == $self->{player}{tag}) { |
580 | } elsif ($_->{container} == $self->{player}{tag}) { |
588 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
581 | $::INV->set_items ($self->{container}{$self->{player}{tag}}) |
589 | } else { |
582 | } else { |
590 | update_container ($_->{tag}); |
583 | update_container ($_->{container}); |
591 | } |
584 | } |
592 | } |
585 | } |
593 | } |
586 | } |
594 | |
587 | |
595 | sub item_update { |
588 | sub item_update { |
… | |
… | |
602 | return; |
595 | return; |
603 | } |
596 | } |
604 | |
597 | |
605 | CFClient::Item::update_widgets $item; |
598 | CFClient::Item::update_widgets $item; |
606 | |
599 | |
607 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & Crossfire::Protocol::Base::F_OPEN)) { |
600 | if ($item->{tag} == $::CONN->{open_container} && not ($item->{flags} & F_OPEN)) { |
608 | set_opencont ($::CONN, 0, "Floor"); |
601 | set_opencont ($::CONN, 0, "Floor"); |
609 | |
602 | |
610 | } elsif ($item->{flags} & Crossfire::Protocol::Base::F_OPEN) { |
603 | } elsif ($item->{flags} & F_OPEN) { |
611 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
604 | set_opencont ($::CONN, $item->{tag}, CFClient::Item::desc_string $item); |
612 | } else { |
605 | } else { |
613 | if ($item->{container} == 0) { |
606 | if ($item->{container} == 0) { |
614 | update_floorbox; |
607 | update_floorbox; |
615 | update_container (0); |
608 | update_container (0); |
… | |
… | |
617 | $::INV->set_items ($self->{container}{$item->{container}}) |
610 | $::INV->set_items ($self->{container}{$item->{container}}) |
618 | } |
611 | } |
619 | } |
612 | } |
620 | } |
613 | } |
621 | |
614 | |
|
|
615 | sub player_update { |
|
|
616 | my ($self, $player) = @_; |
|
|
617 | $::STATWIDS->{weight}->set_text (sprintf "Weight: %.1fkg", $player->{weight} / 1000); |
|
|
618 | |
|
|
619 | # do it here because it is ignored earlier, and there is no "login" event |
|
|
620 | $self->send_command ("output-sync $::CFG->{output_sync}"); |
|
|
621 | $self->send_command ("output-count $::CFG->{output_count}"); |
|
|
622 | } |
|
|
623 | |
622 | 1; |
624 | 1; |