--- deliantra/server/ext/follow.ext 2006/12/17 00:44:59 1.2 +++ deliantra/server/ext/follow.ext 2010/05/18 21:30:16 1.21 @@ -6,57 +6,75 @@ # implement a 'follow' command # don't follow on damned ground -sub teleport { - my ($pl, $map, $x, $y) = @_; +our $MAX_QUEUE = 5; # the # of positions somebody else can lead +our %follow; # $followername => [$follower, $target, [$queue]] - return if $pl->ob->map->path eq $map - && abs ($pl->ob->x - $x) <= 1 - && abs ($pl->ob->y - $y) <= 1; +sub unfollow($) { + my $name = shift; - my $portal = cf::object::new "exit"; + if (my $f = delete $follow{$name}) { + my ($who, $target, undef) = @$f; + $who->contr->detach ("follow_aborter"); + $target->message ("$name no longer follows you."); + $who->message ("You no longer follow " . $target->name . "."); + } +} - $portal->slaying ($map); - $portal->stats->hp ($x); - $portal->stats->sp ($y); +cf::player::attachment follow_aborter => + on_move => sub { + my ($pl, $dir) = @_; + unfollow $pl->ob->name; + }, + on_login => sub { + my ($pl, $dir) = @_; + $pl->detach ("follow_aborter"); + }, +; - $portal->apply ($pl->ob); +our $CORO = cf::async { + $Coro::current->{desc} = "follow handler"; - $portal->destroy; -} + while () { + cf::wait_for_tick; -my %follow; + for (values %follow) { + my ($who, $target, $queue) = @$_; -my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { - while (my ($name, $v) = each %follow) { - my ($target, $his, $mine) = @$v; - my ($who, $other) = (cf::player::find $name, cf::player::find $target); - - if ($who && $other && $other->ob->map) { - my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); - - if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { - @$mine = @$his; - @$his = ($map, $x, $y); - } + 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) { + $who->message ($target->name . " is too far away - you can't follow anymore!"); + unfollow $target->name; + } else { + my ($map, $x, $y) = @{ $queue->[0] }; - my $map; + $map->load; - if ($map = cf::map::find $mine->[0] - and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), - $map->at ($mine->[1], $mine->[2])) { - teleport $who, @$mine; - } else { - delete $follow{$name}; - $who->ob->message ("You can't follow $target anymore!"); + if ( + !$map->valid + or $map->path !~ /^(\{link\}|\/)/ + or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), + $map->at ($x, $y)) + ) { + $who->message ("You can't follow " . $target->name . " anymore!"); + unfollow $who->name; + } elsif (!$who->blocked ($map, $x, $y)) { + shift @$queue; + $who->goto ($map, $x, $y); + } } - } else { - delete $follow{$name}; - $who->ob->message ("$target is gone..."); } - } - $_[0]->w->stop unless keys %follow; -}); + Coro::schedule unless keys %follow; + } +}; cf::register_command follow => sub { my ($who, $args) = @_; @@ -64,18 +82,22 @@ my $name = $who->name; if ($args ne "" && $name ne $args) { - if (my $other = cf::player::find $args) { - if ($other->ob->map->path eq $who->map->path - && abs ($other->ob->x - $who->x) <= 1 - && abs ($other->ob->y - $who->y) <= 1) { + 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->ob->message ("$name is now following your every step..."); + $other->message ("$name is now following your every step..."); $follow{$name} = [ - $args, - [$other->ob->map->path, $other->ob->x, $other->ob->y], - [$who->map->path, $who->x, $who->y], + $who, + $other, + [[$other->map, $other->x, $other->y]], ]; - $timer->start; + $who->contr->attach ("follow_aborter"); + $CORO->ready; } else { $who->message ("You must stand directly beside '$args' to follow her/him"); delete $follow{$name}; @@ -90,22 +112,21 @@ } }; -cf::attach_to_players - on_death => sub { - my ($pl) = @_; - - my $name = $pl->ob->name; - - delete $follow{$name}; +sub unregister { + my ($pl) = @_; - while (my ($k, $v) = each %follow) { - if ($v->[0] eq $name) { - delete $follow{$k}; - } - } - }, -; + my $name = $pl->ob->name; + unfollow $name; + while (my ($k, $v) = each %follow) { + unfollow $k + if $v->[1]->name eq $name; + } +} +cf::player->attach ( + on_death => \&unregister, + on_logout => \&unregister, +);