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