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 |
|
|
my %follow; |
10 |
|
|
|
11 |
|
|
my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
12 |
root |
1.4 |
cf::sync_job { |
13 |
|
|
while (my ($name, $v) = each %follow) { |
14 |
|
|
my ($target, $his, $mine) = @$v; |
15 |
root |
1.8 |
my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
16 |
root |
1.4 |
|
17 |
|
|
if ($who && $other && $other->ob->map) { |
18 |
root |
1.5 |
my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
19 |
root |
1.4 |
|
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 |
root |
1.6 |
if ($map = cf::map::find $mine->[0] |
28 |
root |
1.10 |
and $map =~ /^\// # short-gap fix |
29 |
root |
1.11 |
and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
30 |
root |
1.4 |
$map->at ($mine->[1], $mine->[2])) { |
31 |
root |
1.7 |
$who->ob->goto (@$mine); |
32 |
root |
1.4 |
} else { |
33 |
|
|
delete $follow{$name}; |
34 |
|
|
$who->ob->message ("You can't follow $target anymore!"); |
35 |
|
|
} |
36 |
root |
1.1 |
} else { |
37 |
|
|
delete $follow{$name}; |
38 |
root |
1.4 |
$who->ob->message ("$target is gone..."); |
39 |
root |
1.1 |
} |
40 |
|
|
} |
41 |
root |
1.4 |
}; |
42 |
root |
1.1 |
|
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 |
root |
1.8 |
if (my $other = cf::player::find_active $args) { |
53 |
root |
1.9 |
if ($other->ob->map == $who->map |
54 |
root |
1.1 |
&& 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 |
root |
1.3 |
cf::player->attach ( |
79 |
root |
1.1 |
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 |
root |
1.3 |
); |
93 |
root |
1.1 |
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|