… | |
… | |
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]] |
|
|
11 | our $FOLLOW_HANDLER; |
10 | |
12 | |
11 | our $CORO = cf::async { |
13 | sub unfollow($) { |
12 | $Coro::current->{desc} = "follow handler"; |
14 | my $name = shift; |
13 | |
15 | |
14 | while () { |
16 | if (my $f = delete $FOLLOW{$name}) { |
15 | cf::wait_for_tick; |
17 | my ($who, $target, undef) = @$f; |
|
|
18 | $who->contr->detach ("follow_aborter"); |
|
|
19 | $target->message ("$name no longer follows you."); |
|
|
20 | $who->message ("You no longer follow " . $target->name . "."); |
|
|
21 | } |
|
|
22 | } |
16 | |
23 | |
17 | while (my ($name, $v) = each %follow) { |
24 | cf::player::attachment follow_aborter => |
18 | my ($target, $his, $mine) = @$v; |
25 | on_move => sub { |
19 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
26 | my ($pl, $dir) = @_; |
|
|
27 | unfollow $pl->ob->name; |
|
|
28 | }, |
|
|
29 | on_login => sub { |
|
|
30 | my ($pl, $dir) = @_; |
|
|
31 | $pl->detach ("follow_aborter"); |
|
|
32 | }, |
|
|
33 | ; |
20 | |
34 | |
21 | if ($who && $other && $other->ob->map) { |
35 | sub start_follow_handler { |
22 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
36 | $FOLLOW_HANDLER = cf::async_ext { |
|
|
37 | $Coro::current->{desc} = "follow handler"; |
23 | |
38 | |
|
|
39 | while () { |
|
|
40 | cf::wait_for_tick; |
|
|
41 | |
|
|
42 | for (values %FOLLOW) { |
|
|
43 | my ($who, $target, $queue) = @$_; |
|
|
44 | |
|
|
45 | $target->active |
|
|
46 | or next; |
|
|
47 | |
|
|
48 | my ($map, $x, $y) = ($target->map, $target->x, $target->y); |
|
|
49 | |
|
|
50 | # add new position to queue, if any |
|
|
51 | push @$queue, [$map, $x, $y] |
|
|
52 | if !@$queue |
|
|
53 | || $map != $queue->[-1][0] |
|
|
54 | || $x != $queue->[-1][1] |
|
|
55 | || $y != $queue->[-1][2]; |
|
|
56 | |
|
|
57 | # try to move to oldest position |
|
|
58 | if (@$queue > $MAX_QUEUE) { |
|
|
59 | $who->message ($target->name . " is too far away - you can't follow anymore!"); |
|
|
60 | unfollow $who->name; |
|
|
61 | } elsif (@$queue) { |
|
|
62 | my ($map, $x, $y) = @{ $queue->[0] }; |
|
|
63 | |
|
|
64 | $map->load; |
|
|
65 | |
24 | if ( |
66 | if ( |
25 | ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) |
67 | !$map->valid |
26 | && $map !~ /^\{/ |
68 | or $map->path !~ /^(\{link\}|\/)/ |
|
|
69 | or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR), |
|
|
70 | $map->at ($x, $y) |
27 | ) { |
71 | ) { |
28 | @$mine = @$his; |
72 | $who->message ("You can't follow " . $target->name . " anymore!"); |
|
|
73 | unfollow $who->name; |
|
|
74 | } elsif (!$who->blocked ($map, $x, $y)) { |
|
|
75 | shift @$queue; |
29 | @$his = ($map, $x, $y); |
76 | $who->goto ($map, $x, $y); |
|
|
77 | } |
30 | } |
78 | } |
|
|
79 | } |
31 | |
80 | |
32 | my $map; |
81 | Coro::schedule unless keys %FOLLOW; |
|
|
82 | } |
|
|
83 | }; |
|
|
84 | } |
33 | |
85 | |
34 | if ($map = cf::map::find $mine->[0] |
86 | start_follow_handler; |
35 | and $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 | $who->ob->goto (@$mine); |
|
|
39 | } else { |
|
|
40 | delete $follow{$name}; |
|
|
41 | $who->ob->message ("You can't follow $target anymore!"); |
|
|
42 | } |
|
|
43 | } else { |
|
|
44 | delete $follow{$name}; |
|
|
45 | $who->ob->message ("$target is gone..."); |
|
|
46 | } |
|
|
47 | } |
|
|
48 | |
|
|
49 | Coro::schedule unless keys %follow; |
|
|
50 | } |
|
|
51 | }; |
|
|
52 | |
87 | |
53 | cf::register_command follow => sub { |
88 | cf::register_command follow => sub { |
54 | my ($who, $args) = @_; |
89 | my ($who, $args) = @_; |
55 | |
90 | |
56 | my $name = $who->name; |
91 | my $name = $who->name; |
57 | |
92 | |
58 | if ($args ne "" && $name ne $args) { |
93 | if ($args ne "" && $name ne $args) { |
59 | if (my $other = cf::player::find_active $args) { |
94 | if (my $other = cf::player::find_active $args) { |
|
|
95 | $other = $other->ob; |
|
|
96 | |
60 | if ($other->ob->map == $who->map |
97 | if ($other->map == $who->map |
61 | && abs ($other->ob->x - $who->x) <= 1 |
98 | && abs ($other->x - $who->x) <= 1 |
62 | && abs ($other->ob->y - $who->y) <= 1) { |
99 | && abs ($other->y - $who->y) <= 1 |
|
|
100 | ) { |
63 | $who->message ("Following player '$args', to stop, type: 'follow"); |
101 | $who->message ("Following player '$args', to stop, type: 'follow"); |
64 | $other->ob->message ("$name is now following your every step..."); |
102 | $other->message ("$name is now following your every step..."); |
65 | $follow{$name} = [ |
103 | $FOLLOW{$name} = [ |
|
|
104 | $who, |
66 | $args, |
105 | $other, |
67 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
106 | [[$other->map, $other->x, $other->y]], |
68 | [$who->map->path, $who->x, $who->y], |
|
|
69 | ]; |
107 | ]; |
|
|
108 | $who->contr->attach ("follow_aborter"); |
70 | $CORO->ready; |
109 | $FOLLOW_HANDLER->ready; |
71 | } else { |
110 | } else { |
72 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
111 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
73 | delete $follow{$name}; |
112 | delete $FOLLOW{$name}; |
74 | } |
113 | } |
75 | } else { |
114 | } else { |
76 | $who->message ("Cannot follow '$args': no such player"); |
115 | $who->message ("Cannot follow '$args': no such player"); |
77 | delete $follow{$name}; |
116 | delete $FOLLOW{$name}; |
78 | } |
117 | } |
79 | } else { |
118 | } else { |
80 | $who->message ("follow mode off"); |
119 | $who->message ("follow mode off"); |
81 | delete $follow{$name}; |
120 | delete $FOLLOW{$name}; |
82 | } |
121 | } |
83 | }; |
122 | }; |
84 | |
123 | |
|
|
124 | sub unregister { |
|
|
125 | my ($pl) = @_; |
|
|
126 | |
|
|
127 | my $name = $pl->ob->name; |
|
|
128 | |
|
|
129 | unfollow $name; |
|
|
130 | |
|
|
131 | while (my ($k, $v) = each %FOLLOW) { |
|
|
132 | unfollow $k |
|
|
133 | if $v->[1]->name eq $name; |
|
|
134 | } |
|
|
135 | } |
|
|
136 | |
85 | cf::player->attach ( |
137 | cf::player->attach ( |
86 | on_death => sub { |
138 | on_death => \&unregister, |
87 | my ($pl) = @_; |
139 | on_logout => \&unregister, |
88 | |
|
|
89 | my $name = $pl->ob->name; |
|
|
90 | |
|
|
91 | delete $follow{$name}; |
|
|
92 | |
|
|
93 | while (my ($k, $v) = each %follow) { |
|
|
94 | if ($v->[0] eq $name) { |
|
|
95 | delete $follow{$k}; |
|
|
96 | } |
|
|
97 | } |
|
|
98 | }, |
|
|
99 | ); |
140 | ); |
100 | |
141 | |
101 | |
|
|
102 | |
|
|
103 | |
|
|