… | |
… | |
4 | # TODO: check for player leaving |
4 | # TODO: check for player leaving |
5 | |
5 | |
6 | # implement a 'follow' command |
6 | # implement a 'follow' command |
7 | # don't follow on damned ground |
7 | # don't follow on damned ground |
8 | |
8 | |
9 | my %follow; |
9 | our %follow; |
10 | |
10 | |
11 | my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
11 | our $CORO = cf::async { |
12 | cf::sync_job { |
12 | $Coro::current->{desc} = "follow handler"; |
|
|
13 | |
|
|
14 | while () { |
|
|
15 | cf::wait_for_tick; |
|
|
16 | |
13 | while (my ($name, $v) = each %follow) { |
17 | while (my ($name, $v) = each %follow) { |
14 | my ($target, $his, $mine) = @$v; |
18 | my ($target, $his, $mine) = @$v; |
15 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
19 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
16 | |
20 | |
17 | if ($who && $other && $other->ob->map) { |
21 | if ($who && $other && $other->ob->map) { |
18 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
22 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
19 | |
23 | |
|
|
24 | if ( |
20 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
25 | ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) |
|
|
26 | && $map !~ /^\{/ |
|
|
27 | ) { |
21 | @$mine = @$his; |
28 | @$mine = @$his; |
22 | @$his = ($map, $x, $y); |
29 | @$his = ($map, $x, $y); |
23 | } |
30 | } |
24 | |
31 | |
25 | my $map; |
32 | my $map; |
26 | |
33 | |
27 | if ($map = cf::map::find $mine->[0] |
34 | if ($map = cf::map::find $mine->[0] |
|
|
35 | and $map =~ /^\// # short-gap fix |
28 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
36 | and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
29 | $map->at ($mine->[1], $mine->[2])) { |
37 | $map->at ($mine->[1], $mine->[2])) { |
30 | $who->ob->goto_map (@$mine); |
38 | $who->ob->goto (@$mine); |
31 | } else { |
39 | } else { |
32 | delete $follow{$name}; |
40 | delete $follow{$name}; |
33 | $who->ob->message ("You can't follow $target anymore!"); |
41 | $who->ob->message ("You can't follow $target anymore!"); |
34 | } |
42 | } |
35 | } else { |
43 | } else { |
36 | delete $follow{$name}; |
44 | delete $follow{$name}; |
37 | $who->ob->message ("$target is gone..."); |
45 | $who->ob->message ("$target is gone..."); |
38 | } |
46 | } |
39 | } |
47 | } |
40 | }; |
|
|
41 | |
48 | |
42 | $_[0]->w->stop unless keys %follow; |
49 | Coro::schedule unless keys %follow; |
|
|
50 | } |
43 | }); |
51 | }; |
44 | |
52 | |
45 | cf::register_command follow => sub { |
53 | cf::register_command follow => sub { |
46 | my ($who, $args) = @_; |
54 | my ($who, $args) = @_; |
47 | |
55 | |
48 | my $name = $who->name; |
56 | my $name = $who->name; |
49 | |
57 | |
50 | if ($args ne "" && $name ne $args) { |
58 | if ($args ne "" && $name ne $args) { |
51 | if (my $other = cf::player::find $args) { |
59 | if (my $other = cf::player::find_active $args) { |
52 | if ($other->ob->map eq $who->map |
60 | if ($other->ob->map == $who->map |
53 | && abs ($other->ob->x - $who->x) <= 1 |
61 | && abs ($other->ob->x - $who->x) <= 1 |
54 | && abs ($other->ob->y - $who->y) <= 1) { |
62 | && abs ($other->ob->y - $who->y) <= 1) { |
55 | $who->message ("Following player '$args', to stop, type: 'follow"); |
63 | $who->message ("Following player '$args', to stop, type: 'follow"); |
56 | $other->ob->message ("$name is now following your every step..."); |
64 | $other->ob->message ("$name is now following your every step..."); |
57 | $follow{$name} = [ |
65 | $follow{$name} = [ |
58 | $args, |
66 | $args, |
59 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
67 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
60 | [$who->map->path, $who->x, $who->y], |
68 | [$who->map->path, $who->x, $who->y], |
61 | ]; |
69 | ]; |
62 | $timer->start; |
70 | $CORO->ready; |
63 | } else { |
71 | } else { |
64 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
72 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
65 | delete $follow{$name}; |
73 | delete $follow{$name}; |
66 | } |
74 | } |
67 | } else { |
75 | } else { |