… | |
… | |
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 %follow; |
10 | my ($pl, $map, $x, $y) = @_; |
|
|
11 | |
10 | |
12 | return if $pl->ob->map->path eq $map |
11 | our $CORO = cf::async { |
13 | && abs ($pl->ob->x - $x) <= 1 |
12 | $Coro::current->{desc} = "follow handler"; |
14 | && abs ($pl->ob->y - $y) <= 1; |
|
|
15 | |
13 | |
16 | my $portal = cf::object::new "exit"; |
14 | while () { eval {#d# |
17 | |
15 | |
18 | $portal->slaying ($map); |
16 | while () { |
19 | $portal->stats->hp ($x); |
17 | cf::wait_for_tick; |
20 | $portal->stats->sp ($y); |
|
|
21 | |
18 | |
22 | $portal->apply ($pl->ob); |
19 | while (my ($name, $v) = each %follow) { |
|
|
20 | my ($target, $his, $mine) = @$v; |
|
|
21 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
23 | |
22 | |
24 | $portal->destroy; |
23 | if ($who && $other && $other->ob->map) { |
25 | } |
24 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
26 | |
25 | |
27 | my %follow; |
26 | if ( |
|
|
27 | ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) |
|
|
28 | && $map !~ /^\{/ |
|
|
29 | ) { |
|
|
30 | @$mine = @$his; |
|
|
31 | @$his = ($map, $x, $y); |
|
|
32 | } |
28 | |
33 | |
29 | my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
34 | my $map; |
30 | while (my ($name, $v) = each %follow) { |
|
|
31 | my ($target, $his, $mine) = @$v; |
|
|
32 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
|
|
33 | |
35 | |
34 | if ($who && $other && $other->ob->map) { |
|
|
35 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
|
|
36 | |
|
|
37 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
|
|
38 | @$mine = @$his; |
|
|
39 | @$his = ($map, $x, $y); |
|
|
40 | } |
|
|
41 | |
|
|
42 | my $map; |
|
|
43 | |
|
|
44 | if ($map = cf::map::find $mine->[0] |
36 | if ($map = cf::map::find $mine->[0] |
|
|
37 | and $map =~ /^\// # short-gap fix |
45 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
38 | and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
46 | $map->at ($mine->[1], $mine->[2])) { |
39 | $map->at ($mine->[1], $mine->[2])) { |
47 | teleport $who, @$mine; |
40 | $who->ob->goto (@$mine); |
|
|
41 | } else { |
|
|
42 | delete $follow{$name}; |
|
|
43 | $who->ob->message ("You can't follow $target anymore!"); |
|
|
44 | } |
48 | } else { |
45 | } else { |
49 | delete $follow{$name}; |
46 | delete $follow{$name}; |
50 | $who->ob->message ("You can't follow $target anymore!"); |
47 | $who->ob->message ("$target is gone..."); |
51 | } |
48 | } |
52 | } else { |
|
|
53 | delete $follow{$name}; |
|
|
54 | $who->ob->message ("$target is gone..."); |
|
|
55 | } |
49 | } |
|
|
50 | |
|
|
51 | Coro::schedule unless keys %follow; |
56 | } |
52 | } |
57 | |
53 | }; warn "follow handler died <$@>\n"; }#d# |
58 | $_[0]->w->stop unless keys %follow; |
|
|
59 | }); |
54 | }; |
60 | |
55 | |
61 | cf::register_command follow => sub { |
56 | cf::register_command follow => sub { |
62 | my ($who, $args) = @_; |
57 | my ($who, $args) = @_; |
63 | |
58 | |
64 | my $name = $who->name; |
59 | my $name = $who->name; |
65 | |
60 | |
66 | if ($args ne "" && $name ne $args) { |
61 | if ($args ne "" && $name ne $args) { |
67 | if (my $other = cf::player::find $args) { |
62 | if (my $other = cf::player::find_active $args) { |
68 | if ($other->ob->map->path eq $who->map->path |
63 | if ($other->ob->map == $who->map |
69 | && abs ($other->ob->x - $who->x) <= 1 |
64 | && abs ($other->ob->x - $who->x) <= 1 |
70 | && abs ($other->ob->y - $who->y) <= 1) { |
65 | && abs ($other->ob->y - $who->y) <= 1) { |
71 | $who->message ("Following player '$args', to stop, type: 'follow"); |
66 | $who->message ("Following player '$args', to stop, type: 'follow"); |
72 | $other->ob->message ("$name is now following your every step..."); |
67 | $other->ob->message ("$name is now following your every step..."); |
73 | $follow{$name} = [ |
68 | $follow{$name} = [ |
74 | $args, |
69 | $args, |
75 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
70 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
76 | [$who->map->path, $who->x, $who->y], |
71 | [$who->map->path, $who->x, $who->y], |
77 | ]; |
72 | ]; |
78 | $timer->start; |
73 | $CORO->ready; |
79 | } else { |
74 | } else { |
80 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
75 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
81 | delete $follow{$name}; |
76 | delete $follow{$name}; |
82 | } |
77 | } |
83 | } else { |
78 | } else { |