--- deliantra/maps/perl/follow.ext 2006/02/07 01:03:44 1.7 +++ deliantra/maps/perl/follow.ext 2006/08/25 15:12:21 1.21 @@ -4,38 +4,7 @@ # TODO: check for player leaving # implement a 'follow' command - -my %follow; - -cf::register_command follow => 0, sub { - my ($who, $args) = @_; - - 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) { - $who->message ("Following player '$args', to stop, type: 'follow"); - $follow{$name} = [ - $args, - [$other->ob->map->path, $other->ob->x, $other->ob->y], - [$who->map->path, $who->x, $who->y], - ]; - } 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}; - } -}; +# don't follow on damned ground sub teleport { my ($pl, $map, $x, $y) = @_; @@ -44,27 +13,25 @@ && abs ($pl->ob->x - $x) <= 1 && abs ($pl->ob->y - $y) <= 1; - my $portal = cf::object::new ("exit"); + my $portal = cf::object::new "exit"; $portal->set_slaying ($map); $portal->set_hp ($x); $portal->set_sp ($y); - $portal->apply ($pl->ob, 0); + $portal->apply ($pl->ob); $portal->free; } -sub on_clock { - my ($event) = @_; - - return unless %follow; +my %follow; +my $timer = Event->timer (interval => 0.2, parked => 1, 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) { + 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]) { @@ -80,14 +47,64 @@ teleport $who, @$mine; } else { delete $follow{$name}; - $who->ob->message ("$target is gone where you can't follow..."); + $who->ob->message ("You can't follow $target anymore!"); } } else { delete $follow{$name}; - $who->ob->message ("$name or $target is gone..."); + $who->ob->message ("$target is gone..."); } } -} + + $_[0]->w->stop unless keys %follow; +}); + +cf::register_command follow => 0, sub { + my ($who, $args) = @_; + + 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) { + $who->message ("Following player '$args', to stop, type: 'follow"); + $other->ob->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], + ]; + $timer->start; + } 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}; + } +}; + +cf::attach_to_players + 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}; + } + } + }, +;