#! perl # additional support for cfplus client use NPC_Dialogue; cf::register_extcmd cfplus_support => sub { my ($pl, $data) = @_; my ($token, $client_version) = split / /, $data, 2; $pl->send ("ext $token 1"); }; my %dialog; # currently active dialogs my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { while (my ($token, $dialog) = each %dialog) { my (undef, $dx, $dy) = $dialog->{ob}->rangevector ($dialog->{npc}); next if (abs $dx) <= 2 && (abs $dy) <= 2; $dialog->{ob}->contr->send ("ext $token out_of_range"); delete $dialog{$token}; } $_[0]->w->stop unless keys %dialog; }); sub dialog_tell { my ($token, $dialog, $msg) = @_; my $pl = $dialog->{ob}->contr; my ($reply, @kw) = $dialog->tell ($msg); $reply = "..." unless $reply; $pl->send ("ext $token msg " . join "\x00", $reply, @kw); } # return "interesting" information about the given tile # currently only returns the npc_dialog title when a dialog is possible cf::register_extcmd lookat => sub { my ($pl, $data) = @_; my ($token, $dx, $dy) = split / /, $data; my $near = (abs $dx) <= 2 && (abs $dy) <= 2; my %res; if ($pl->cell_visible ($dx, $dy)) { for my $ob ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { $res{npc_dialog} = $ob->name if $near && NPC_Dialogue::has_dialogue $ob; } } $pl->send ("ext $token " . join "\x00", %res); }; cf::register_extcmd npc_dialog_begin => sub { my ($pl, $data) = @_; my ($token, $dx, $dy) = split / /, $data; return unless (abs $dx) <= 2 && (abs $dy) <= 2; return unless $pl->cell_visible ($dx, $dy); for my $npc ($pl->ob->map->at ($pl->ob->x + $dx, $pl->ob->y + $dy)) { if (NPC_Dialogue::has_dialogue $npc) { $dialog{$token} = new NPC_Dialogue ob => $pl->ob, npc => $npc; dialog_tell $token, $dialog{$token}, "hi"; $timer->start; return; } } $pl->send ("ext $token error"); }; cf::register_extcmd npc_dialog_tell => sub { my ($pl, $data) = @_; my ($token, $msg) = split / /, $data, 2; dialog_tell $token, $dialog{$token}, $msg if $dialog{$token}; }; cf::register_extcmd npc_dialog_end => sub { my ($pl, $token) = @_; delete $dialog{$token}; }; sub on_logout { my ($pl, $host) = @_; delete $dialog{$_} for grep $pl->ob == $dialog{$_}{ob}, keys %dialog; 0 }