… | |
… | |
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 $MAX_QUEUE = 5; # the # of positions somebody else can lead |
9 | our $MAX_QUEUE = 5; # the # of positions somebody else can lead |
10 | our %follow; # $followername => [$follower, $target, [$queue]] |
10 | our %follow; # $followername => [$follower, $target, [$queue]] |
|
|
11 | |
|
|
12 | sub unfollow($) { |
|
|
13 | my $name = shift; |
|
|
14 | |
|
|
15 | if (my $f = delete $follow{$name}) { |
|
|
16 | my ($who, $target, undef) = @$f; |
|
|
17 | $who->contr->detach ("follow_aborter"); |
|
|
18 | $target->message ("$name no longer follows you."); |
|
|
19 | $who->message ("You no longer follow " . $target->name . "."); |
|
|
20 | } |
|
|
21 | } |
|
|
22 | |
|
|
23 | cf::player::attachment follow_aborter => |
|
|
24 | on_move => sub { |
|
|
25 | my ($pl, $dir) = @_; |
|
|
26 | unfollow $pl->ob->name; |
|
|
27 | }, |
|
|
28 | on_login => sub { |
|
|
29 | my ($pl, $dir) = @_; |
|
|
30 | $pl->detach ("follow_aborter"); |
|
|
31 | }, |
|
|
32 | ; |
11 | |
33 | |
12 | our $CORO = cf::async { |
34 | our $CORO = cf::async { |
13 | $Coro::current->{desc} = "follow handler"; |
35 | $Coro::current->{desc} = "follow handler"; |
14 | |
36 | |
15 | while () { |
37 | while () { |
… | |
… | |
26 | || $x != $queue->[-1][1] |
48 | || $x != $queue->[-1][1] |
27 | || $y != $queue->[-1][2]; |
49 | || $y != $queue->[-1][2]; |
28 | |
50 | |
29 | # try to move to oldest position |
51 | # try to move to oldest position |
30 | if (@$queue > $MAX_QUEUE) { |
52 | if (@$queue > $MAX_QUEUE) { |
31 | delete $follow{$who->name}; |
|
|
32 | $who->message ($target->name . " is too far away - you can't follow anymore!"); |
53 | $who->message ($target->name . " is too far away - you can't follow anymore!"); |
|
|
54 | unfollow $target->name; |
33 | } else { |
55 | } else { |
34 | my ($map, $x, $y) = @{ $queue->[0] }; |
56 | my ($map, $x, $y) = @{ $queue->[0] }; |
35 | |
57 | |
36 | $map->load; |
58 | $map->load; |
37 | |
59 | |
38 | if ( |
60 | if ( |
39 | $map->path !~ /^(\{link\}|\/)/ |
61 | $map->path !~ /^(\{link\}|\/)/ |
40 | or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), |
62 | or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), |
41 | $map->at ($x, $y) |
63 | $map->at ($x, $y) |
42 | ) { |
64 | ) { |
43 | delete $follow{$who->name}; |
|
|
44 | $who->ob->message ("You can't follow " . $target->name . " anymore!"); |
65 | $who->message ("You can't follow " . $target->name . " anymore!"); |
|
|
66 | unfollow $who->name; |
45 | } elsif (!$who->blocked ($map, $x, $y)) { |
67 | } elsif (!$who->blocked ($map, $x, $y)) { |
46 | shift @$queue; |
68 | shift @$queue; |
47 | $who->goto ($map, $x, $y); |
69 | $who->goto ($map, $x, $y); |
48 | } |
70 | } |
49 | } |
71 | } |
… | |
… | |
71 | $follow{$name} = [ |
93 | $follow{$name} = [ |
72 | $who, |
94 | $who, |
73 | $other, |
95 | $other, |
74 | [[$other->map, $other->x, $other->y]], |
96 | [[$other->map, $other->x, $other->y]], |
75 | ]; |
97 | ]; |
|
|
98 | $who->contr->attach ("follow_aborter"); |
76 | $CORO->ready; |
99 | $CORO->ready; |
77 | } else { |
100 | } else { |
78 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
101 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
79 | delete $follow{$name}; |
102 | delete $follow{$name}; |
80 | } |
103 | } |
… | |
… | |
88 | } |
111 | } |
89 | }; |
112 | }; |
90 | |
113 | |
91 | sub unregister { |
114 | sub unregister { |
92 | my ($pl) = @_; |
115 | my ($pl) = @_; |
|
|
116 | |
93 | my $name = $pl->ob->name; |
117 | my $name = $pl->ob->name; |
94 | delete $follow{$name}; |
|
|
95 | |
118 | |
96 | warn "unfollow $name\n";#d# |
119 | unfollow $name; |
97 | |
120 | |
98 | while (my ($k, $v) = each %follow) { |
121 | while (my ($k, $v) = each %follow) { |
|
|
122 | unfollow $k |
99 | if ($v->[1]->name eq $name) { |
123 | if $v->[1]->name eq $name; |
100 | warn "unfollow $k\n";#d# |
|
|
101 | delete $follow{$k}; |
|
|
102 | } |
|
|
103 | } |
124 | } |
104 | } |
125 | } |
105 | |
126 | |
106 | cf::player->attach ( |
127 | cf::player->attach ( |
107 | on_death => \&unregister, |
128 | on_death => \&unregister, |