| 1 |
root |
1.1 |
#! perl |
| 2 |
|
|
|
| 3 |
|
|
# TODO: skip arena |
| 4 |
|
|
# TODO: check for player leaving |
| 5 |
|
|
|
| 6 |
|
|
# implement a 'follow' command |
| 7 |
|
|
# don't follow on damned ground |
| 8 |
|
|
|
| 9 |
root |
1.13 |
our %follow; |
| 10 |
root |
1.1 |
|
| 11 |
root |
1.13 |
our $CORO = cf::async { |
| 12 |
|
|
$Coro::current->{desc} = "follow handler"; |
| 13 |
root |
1.12 |
|
| 14 |
root |
1.13 |
while () { |
| 15 |
|
|
cf::wait_for_tick; |
| 16 |
root |
1.12 |
|
| 17 |
root |
1.13 |
while (my ($name, $v) = each %follow) { |
| 18 |
|
|
my ($target, $his, $mine) = @$v; |
| 19 |
|
|
my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
| 20 |
|
|
|
| 21 |
|
|
if ($who && $other && $other->ob->map) { |
| 22 |
|
|
my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
| 23 |
|
|
|
| 24 |
|
|
if ( |
| 25 |
|
|
($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) |
| 26 |
|
|
&& $map !~ /^\{/ |
| 27 |
|
|
) { |
| 28 |
|
|
@$mine = @$his; |
| 29 |
|
|
@$his = ($map, $x, $y); |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
|
my $map; |
| 33 |
|
|
|
| 34 |
|
|
if ($map = cf::map::find $mine->[0] |
| 35 |
|
|
and $map =~ /^\// # short-gap fix |
| 36 |
|
|
and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
| 37 |
|
|
$map->at ($mine->[1], $mine->[2])) { |
| 38 |
|
|
$who->ob->goto (@$mine); |
| 39 |
|
|
} else { |
| 40 |
|
|
delete $follow{$name}; |
| 41 |
|
|
$who->ob->message ("You can't follow $target anymore!"); |
| 42 |
|
|
} |
| 43 |
root |
1.1 |
} else { |
| 44 |
|
|
delete $follow{$name}; |
| 45 |
root |
1.13 |
$who->ob->message ("$target is gone..."); |
| 46 |
root |
1.1 |
} |
| 47 |
|
|
} |
| 48 |
root |
1.13 |
|
| 49 |
|
|
Coro::schedule unless keys %follow; |
| 50 |
root |
1.12 |
} |
| 51 |
root |
1.13 |
}; |
| 52 |
root |
1.1 |
|
| 53 |
|
|
cf::register_command follow => sub { |
| 54 |
|
|
my ($who, $args) = @_; |
| 55 |
|
|
|
| 56 |
|
|
my $name = $who->name; |
| 57 |
|
|
|
| 58 |
|
|
if ($args ne "" && $name ne $args) { |
| 59 |
root |
1.8 |
if (my $other = cf::player::find_active $args) { |
| 60 |
root |
1.9 |
if ($other->ob->map == $who->map |
| 61 |
root |
1.1 |
&& abs ($other->ob->x - $who->x) <= 1 |
| 62 |
|
|
&& abs ($other->ob->y - $who->y) <= 1) { |
| 63 |
|
|
$who->message ("Following player '$args', to stop, type: 'follow"); |
| 64 |
|
|
$other->ob->message ("$name is now following your every step..."); |
| 65 |
|
|
$follow{$name} = [ |
| 66 |
|
|
$args, |
| 67 |
|
|
[$other->ob->map->path, $other->ob->x, $other->ob->y], |
| 68 |
|
|
[$who->map->path, $who->x, $who->y], |
| 69 |
|
|
]; |
| 70 |
root |
1.13 |
$CORO->ready; |
| 71 |
root |
1.1 |
} else { |
| 72 |
|
|
$who->message ("You must stand directly beside '$args' to follow her/him"); |
| 73 |
|
|
delete $follow{$name}; |
| 74 |
|
|
} |
| 75 |
|
|
} else { |
| 76 |
|
|
$who->message ("Cannot follow '$args': no such player"); |
| 77 |
|
|
delete $follow{$name}; |
| 78 |
|
|
} |
| 79 |
|
|
} else { |
| 80 |
|
|
$who->message ("follow mode off"); |
| 81 |
|
|
delete $follow{$name}; |
| 82 |
|
|
} |
| 83 |
|
|
}; |
| 84 |
|
|
|
| 85 |
root |
1.3 |
cf::player->attach ( |
| 86 |
root |
1.1 |
on_death => sub { |
| 87 |
|
|
my ($pl) = @_; |
| 88 |
|
|
|
| 89 |
|
|
my $name = $pl->ob->name; |
| 90 |
|
|
|
| 91 |
|
|
delete $follow{$name}; |
| 92 |
|
|
|
| 93 |
|
|
while (my ($k, $v) = each %follow) { |
| 94 |
|
|
if ($v->[0] eq $name) { |
| 95 |
|
|
delete $follow{$k}; |
| 96 |
|
|
} |
| 97 |
|
|
} |
| 98 |
|
|
}, |
| 99 |
root |
1.3 |
); |
| 100 |
root |
1.1 |
|
| 101 |
|
|
|
| 102 |
|
|
|
| 103 |
|
|
|