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