… | |
… | |
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 | cf::sync_job { |
13 | $Coro::current->{desc} = "follow handler"; |
13 | while (my ($name, $v) = each %follow) { |
|
|
14 | my ($target, $his, $mine) = @$v; |
|
|
15 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
|
|
16 | |
14 | |
17 | if ($who && $other && $other->ob->map) { |
15 | while () { |
18 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
16 | cf::wait_for_tick; |
19 | |
17 | |
20 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
18 | for (values %follow) { |
21 | @$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; |
22 | @$his = ($map, $x, $y); |
47 | $who->goto ($map, $x, $y); |
23 | } |
48 | } |
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 (@$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 | } |
49 | } |
39 | } |
50 | } |
40 | }; |
|
|
41 | |
51 | |
42 | $_[0]->w->stop unless keys %follow; |
52 | Coro::schedule unless keys %follow; |
|
|
53 | } |
43 | }); |
54 | }; |
44 | |
55 | |
45 | cf::register_command follow => sub { |
56 | cf::register_command follow => sub { |
46 | my ($who, $args) = @_; |
57 | my ($who, $args) = @_; |
47 | |
58 | |
48 | my $name = $who->name; |
59 | my $name = $who->name; |
49 | |
60 | |
50 | if ($args ne "" && $name ne $args) { |
61 | if ($args ne "" && $name ne $args) { |
51 | if (my $other = cf::player::find_active $args) { |
62 | if (my $other = cf::player::find_active $args) { |
|
|
63 | $other = $other->ob; |
|
|
64 | |
52 | if ($other->ob->map == $who->map |
65 | if ($other->map == $who->map |
53 | && abs ($other->ob->x - $who->x) <= 1 |
66 | && abs ($other->x - $who->x) <= 1 |
54 | && abs ($other->ob->y - $who->y) <= 1) { |
67 | && abs ($other->y - $who->y) <= 1 |
|
|
68 | ) { |
55 | $who->message ("Following player '$args', to stop, type: 'follow"); |
69 | $who->message ("Following player '$args', to stop, type: 'follow"); |
56 | $other->ob->message ("$name is now following your every step..."); |
70 | $other->message ("$name is now following your every step..."); |
57 | $follow{$name} = [ |
71 | $follow{$name} = [ |
|
|
72 | $who, |
58 | $args, |
73 | $other, |
59 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
74 | [[$other->map, $other->x, $other->y]], |
60 | [$who->map->path, $who->x, $who->y], |
|
|
61 | ]; |
75 | ]; |
62 | $timer->start; |
76 | $CORO->ready; |
63 | } else { |
77 | } else { |
64 | $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"); |
65 | delete $follow{$name}; |
79 | delete $follow{$name}; |
66 | } |
80 | } |
67 | } else { |
81 | } else { |
… | |
… | |
72 | $who->message ("follow mode off"); |
86 | $who->message ("follow mode off"); |
73 | delete $follow{$name}; |
87 | delete $follow{$name}; |
74 | } |
88 | } |
75 | }; |
89 | }; |
76 | |
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 | |
77 | cf::player->attach ( |
106 | cf::player->attach ( |
78 | on_death => sub { |
107 | on_death => \&unregister, |
79 | my ($pl) = @_; |
108 | on_logout => \&unregister, |
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 | ); |
109 | ); |
92 | |
110 | |
93 | |
111 | |
94 | |
112 | |
95 | |
113 | |