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