… | |
… | |
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 $MAX_QUEUE = 5; # the # of positions somebody else can lead |
|
|
10 | our %follow; # $followername => [$follower, $target, [$queue]] |
10 | |
11 | |
11 | my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
12 | our $CORO = cf::async { |
12 | while (my ($name, $v) = each %follow) { |
13 | $Coro::current->{desc} = "follow handler"; |
13 | my ($target, $his, $mine) = @$v; |
|
|
14 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
|
|
15 | |
14 | |
16 | if ($who && $other && $other->ob->map) { |
15 | while () { |
17 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
16 | cf::wait_for_tick; |
18 | |
17 | |
19 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
18 | for (values %follow) { |
20 | @$mine = @$his; |
19 | my ($who, $target, $queue) = @$_; |
|
|
20 | |
|
|
21 | my ($map, $x, $y) = ($target->map, $target->x, $target->y); |
|
|
22 | |
|
|
23 | # add new position to queue, if any |
|
|
24 | push @$queue, [$map, $x, $y] |
|
|
25 | if $map != $queue->[-1][0] |
|
|
26 | || $x != $queue->[-1][1] |
|
|
27 | || $y != $queue->[-1][2]; |
|
|
28 | |
|
|
29 | # try to move to oldest position |
|
|
30 | if (@$queue > $MAX_QUEUE) { |
|
|
31 | delete $follow{$who->name}; |
|
|
32 | $who->message ($target->name . " is too far away - you can't follow anymore!"); |
|
|
33 | } else { |
|
|
34 | my ($map, $x, $y) = @{ $queue->[0] }; |
|
|
35 | |
|
|
36 | $map->load; |
|
|
37 | |
|
|
38 | if ( |
|
|
39 | $map->path !~ /^(\{link\}|\/)/ |
|
|
40 | or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), |
|
|
41 | $map->at ($x, $y) |
|
|
42 | ) { |
|
|
43 | delete $follow{$who->name}; |
|
|
44 | $who->ob->message ("You can't follow " . $target->name . " anymore!"); |
|
|
45 | } elsif (!$who->blocked ($map, $x, $y)) { |
|
|
46 | shift @$queue; |
21 | @$his = ($map, $x, $y); |
47 | $who->goto ($map, $x, $y); |
|
|
48 | } |
22 | } |
49 | } |
|
|
50 | } |
23 | |
51 | |
24 | my $map; |
52 | Coro::schedule unless keys %follow; |
25 | |
|
|
26 | if ($map = cf::map::find $mine->[0] |
|
|
27 | and $map =~ /^\// # short-gap fix |
|
|
28 | and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
|
|
29 | $map->at ($mine->[1], $mine->[2])) { |
|
|
30 | $who->ob->goto (@$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 | } |
53 | } |
40 | |
|
|
41 | $_[0]->w->stop unless keys %follow; |
|
|
42 | }); |
54 | }; |
43 | |
55 | |
44 | cf::register_command follow => sub { |
56 | cf::register_command follow => sub { |
45 | my ($who, $args) = @_; |
57 | my ($who, $args) = @_; |
46 | |
58 | |
47 | my $name = $who->name; |
59 | my $name = $who->name; |
48 | |
60 | |
49 | if ($args ne "" && $name ne $args) { |
61 | if ($args ne "" && $name ne $args) { |
50 | if (my $other = cf::player::find_active $args) { |
62 | if (my $other = cf::player::find_active $args) { |
|
|
63 | $other = $other->ob; |
|
|
64 | |
51 | if ($other->ob->map == $who->map |
65 | if ($other->map == $who->map |
52 | && abs ($other->ob->x - $who->x) <= 1 |
66 | && abs ($other->x - $who->x) <= 1 |
53 | && abs ($other->ob->y - $who->y) <= 1) { |
67 | && abs ($other->y - $who->y) <= 1 |
|
|
68 | ) { |
54 | $who->message ("Following player '$args', to stop, type: 'follow"); |
69 | $who->message ("Following player '$args', to stop, type: 'follow"); |
55 | $other->ob->message ("$name is now following your every step..."); |
70 | $other->message ("$name is now following your every step..."); |
56 | $follow{$name} = [ |
71 | $follow{$name} = [ |
|
|
72 | $who, |
57 | $args, |
73 | $other, |
58 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
74 | [[$other->map, $other->x, $other->y]], |
59 | [$who->map->path, $who->x, $who->y], |
|
|
60 | ]; |
75 | ]; |
61 | $timer->start; |
76 | $CORO->ready; |
62 | } else { |
77 | } else { |
63 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
78 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
64 | delete $follow{$name}; |
79 | delete $follow{$name}; |
65 | } |
80 | } |
66 | } else { |
81 | } else { |
… | |
… | |
71 | $who->message ("follow mode off"); |
86 | $who->message ("follow mode off"); |
72 | delete $follow{$name}; |
87 | delete $follow{$name}; |
73 | } |
88 | } |
74 | }; |
89 | }; |
75 | |
90 | |
|
|
91 | sub unregister { |
|
|
92 | my ($pl) = @_; |
|
|
93 | my $name = $pl->ob->name; |
|
|
94 | delete $follow{$name}; |
|
|
95 | |
|
|
96 | warn "unfollow $name\n";#d# |
|
|
97 | |
|
|
98 | while (my ($k, $v) = each %follow) { |
|
|
99 | if ($v->[1]->name eq $name) { |
|
|
100 | warn "unfollow $k\n";#d# |
|
|
101 | delete $follow{$k}; |
|
|
102 | } |
|
|
103 | } |
|
|
104 | } |
|
|
105 | |
76 | cf::player->attach ( |
106 | cf::player->attach ( |
77 | on_death => sub { |
107 | on_death => \&unregister, |
78 | my ($pl) = @_; |
108 | on_logout => \&unregister, |
79 | |
|
|
80 | my $name = $pl->ob->name; |
|
|
81 | |
|
|
82 | delete $follow{$name}; |
|
|
83 | |
|
|
84 | while (my ($k, $v) = each %follow) { |
|
|
85 | if ($v->[0] eq $name) { |
|
|
86 | delete $follow{$k}; |
|
|
87 | } |
|
|
88 | } |
|
|
89 | }, |
|
|
90 | ); |
109 | ); |
91 | |
110 | |
92 | |
111 | |
93 | |
112 | |
94 | |
113 | |