--- deliantra/server/ext/follow.ext 2007/01/14 18:00:34 1.9 +++ deliantra/server/ext/follow.ext 2010/05/02 19:04:01 1.18 @@ -6,41 +6,52 @@ # implement a 'follow' command # don't follow on damned ground -my %follow; +our $MAX_QUEUE = 5; # the # of positions somebody else can lead +our %follow; # $followername => [$follower, $target, [$queue]] -my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { - cf::sync_job { - while (my ($name, $v) = each %follow) { - my ($target, $his, $mine) = @$v; - my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $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); - } +our $CORO = cf::async { + $Coro::current->{desc} = "follow handler"; - my $map; + while () { + cf::wait_for_tick; - if ($map = cf::map::find $mine->[0] - and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), - $map->at ($mine->[1], $mine->[2])) { - $who->ob->goto (@$mine); - } else { - delete $follow{$name}; - $who->ob->message ("You can't follow $target anymore!"); - } + 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 { - delete $follow{$name}; - $who->ob->message ("$target is gone..."); + 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); + } } } - }; - $_[0]->w->stop unless keys %follow; -}); + Coro::schedule unless keys %follow; + } +}; cf::register_command follow => sub { my ($who, $args) = @_; @@ -49,17 +60,20 @@ if ($args ne "" && $name ne $args) { if (my $other = cf::player::find_active $args) { - if ($other->ob->map == $who->map - && abs ($other->ob->x - $who->x) <= 1 - && abs ($other->ob->y - $who->y) <= 1) { + $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; + $CORO->ready; } else { $who->message ("You must stand directly beside '$args' to follow her/him"); delete $follow{$name}; @@ -74,20 +88,24 @@ } }; -cf::player->attach ( - on_death => sub { - my ($pl) = @_; - - my $name = $pl->ob->name; - - delete $follow{$name}; - - while (my ($k, $v) = each %follow) { - if ($v->[0] eq $name) { - delete $follow{$k}; - } +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, );