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