--- cf.schmorp.de/maps/perl/follow.ext 2006/02/04 00:33:07 1.3 +++ cf.schmorp.de/maps/perl/follow.ext 2006/08/25 15:12:21 1.21 @@ -1,20 +1,88 @@ #! perl +# TODO: skip arena +# TODO: check for player leaving + # implement a 'follow' command +# don't follow on damned ground + +sub teleport { + my ($pl, $map, $x, $y) = @_; + + return if $pl->ob->map->path eq $map + && abs ($pl->ob->x - $x) <= 1 + && abs ($pl->ob->y - $y) <= 1; + + my $portal = cf::object::new "exit"; + + $portal->set_slaying ($map); + $portal->set_hp ($x); + $portal->set_sp ($y); + + $portal->apply ($pl->ob); + + $portal->free; +} 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 && $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; + + if ($map = cf::map::map $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!"); + } + } else { + delete $follow{$name}; + $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 (cf::player::find $args) { - $who->message ("following player '$args', to stop, type: 'follow"); - $follow{$name} = $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"); + $who->message ("Cannot follow '$args': no such player"); + delete $follow{$name}; } } else { $who->message ("follow mode off"); @@ -22,36 +90,21 @@ } }; -sub teleport { - my ($pl, $map, $x, $y) = @_; - - my $portal = cf::object::new ("exit"); +cf::attach_to_players + on_death => sub { + my ($pl) = @_; - $portal->set_slaying ($map->path); - $portal->set_hp ($x); - $portal->set_sp ($y); + my $name = $pl->ob->name; - $portal->apply ($pl->ob, 0); - - $portal->free; -} - -sub on_clock { - my ($event) = @_; - - return unless %follow; + delete $follow{$name}; - while (my ($name, $target) = each %follow) { - my ($who, $other) = (cf::player::find $name, cf::player::find $target); - if ($who && $other) { - teleport $who, $other->ob->map, $other->ob->x, $other->ob->y - if $who->ob->map->path ne $other->ob->map->path; - } else { - warn "follow: $name or $target is gone, removing from follow list\n"; - delete $follow{$name}; + while (my ($k, $v) = each %follow) { + if ($v->[0] eq $name) { + delete $follow{$k}; + } } - } -} + }, +;