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 |
my %follow; |
10 |
|
11 |
my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
12 |
cf::sync_job { |
13 |
while (my ($name, $v) = each %follow) { |
14 |
my ($target, $his, $mine) = @$v; |
15 |
my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
16 |
|
17 |
if ($who && $other && $other->ob->map) { |
18 |
my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
19 |
|
20 |
if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
21 |
@$mine = @$his; |
22 |
@$his = ($map, $x, $y); |
23 |
} |
24 |
|
25 |
my $map; |
26 |
|
27 |
if ($map = cf::map::find $mine->[0] |
28 |
and $map =~ /^\// # short-gap fix |
29 |
and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
30 |
$map->at ($mine->[1], $mine->[2])) { |
31 |
$who->ob->goto (@$mine); |
32 |
} else { |
33 |
delete $follow{$name}; |
34 |
$who->ob->message ("You can't follow $target anymore!"); |
35 |
} |
36 |
} else { |
37 |
delete $follow{$name}; |
38 |
$who->ob->message ("$target is gone..."); |
39 |
} |
40 |
} |
41 |
}; |
42 |
|
43 |
$_[0]->w->stop unless keys %follow; |
44 |
}); |
45 |
|
46 |
cf::register_command follow => sub { |
47 |
my ($who, $args) = @_; |
48 |
|
49 |
my $name = $who->name; |
50 |
|
51 |
if ($args ne "" && $name ne $args) { |
52 |
if (my $other = cf::player::find_active $args) { |
53 |
if ($other->ob->map == $who->map |
54 |
&& abs ($other->ob->x - $who->x) <= 1 |
55 |
&& abs ($other->ob->y - $who->y) <= 1) { |
56 |
$who->message ("Following player '$args', to stop, type: 'follow"); |
57 |
$other->ob->message ("$name is now following your every step..."); |
58 |
$follow{$name} = [ |
59 |
$args, |
60 |
[$other->ob->map->path, $other->ob->x, $other->ob->y], |
61 |
[$who->map->path, $who->x, $who->y], |
62 |
]; |
63 |
$timer->start; |
64 |
} else { |
65 |
$who->message ("You must stand directly beside '$args' to follow her/him"); |
66 |
delete $follow{$name}; |
67 |
} |
68 |
} else { |
69 |
$who->message ("Cannot follow '$args': no such player"); |
70 |
delete $follow{$name}; |
71 |
} |
72 |
} else { |
73 |
$who->message ("follow mode off"); |
74 |
delete $follow{$name}; |
75 |
} |
76 |
}; |
77 |
|
78 |
cf::player->attach ( |
79 |
on_death => sub { |
80 |
my ($pl) = @_; |
81 |
|
82 |
my $name = $pl->ob->name; |
83 |
|
84 |
delete $follow{$name}; |
85 |
|
86 |
while (my ($k, $v) = each %follow) { |
87 |
if ($v->[0] eq $name) { |
88 |
delete $follow{$k}; |
89 |
} |
90 |
} |
91 |
}, |
92 |
); |
93 |
|
94 |
|
95 |
|
96 |
|