#! perl # TODO: skip arena # TODO: check for player leaving # implement a 'follow' command # don't follow on damned ground our $MAX_QUEUE = 5; # the # of positions somebody else can lead our %follow; # $followername => [$follower, $target, [$queue]] our $CORO = cf::async { $Coro::current->{desc} = "follow handler"; while () { cf::wait_for_tick; for (values %follow) { my ($who, $target, $queue) = @$_; my ($map, $x, $y) = ($target->map, $target->x, $target->y); # add new position to queue, if any push @$queue, [$map, $x, $y] if $map != $queue->[-1][0] || $x != $queue->[-1][1] || $y != $queue->[-1][2]; # try to move to oldest position if (@$queue > $MAX_QUEUE) { delete $follow{$who->name}; $who->message ($target->name . " is too far away - you can't follow anymore!"); } else { my ($map, $x, $y) = @{ $queue->[0] }; $map->load; if ( $map->path !~ /^(\{link\}|\/)/ or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), $map->at ($x, $y) ) { delete $follow{$who->name}; $who->ob->message ("You can't follow " . $target->name . " anymore!"); } elsif (!$who->blocked ($map, $x, $y)) { shift @$queue; $who->goto ($map, $x, $y); } } } Coro::schedule unless keys %follow; } }; cf::register_command follow => sub { my ($who, $args) = @_; my $name = $who->name; if ($args ne "" && $name ne $args) { if (my $other = cf::player::find_active $args) { $other = $other->ob; if ($other->map == $who->map && abs ($other->x - $who->x) <= 1 && abs ($other->y - $who->y) <= 1 ) { $who->message ("Following player '$args', to stop, type: 'follow"); $other->message ("$name is now following your every step..."); $follow{$name} = [ $who, $other, [[$other->map, $other->x, $other->y]], ]; $CORO->ready; } else { $who->message ("You must stand directly beside '$args' to follow her/him"); delete $follow{$name}; } } else { $who->message ("Cannot follow '$args': no such player"); delete $follow{$name}; } } else { $who->message ("follow mode off"); delete $follow{$name}; } }; sub unregister { my ($pl) = @_; my $name = $pl->ob->name; delete $follow{$name}; warn "unfollow $name\n";#d# while (my ($k, $v) = each %follow) { if ($v->[1]->name eq $name) { warn "unfollow $k\n";#d# delete $follow{$k}; } } } cf::player->attach ( on_death => \&unregister, on_logout => \&unregister, );