… | |
… | |
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 () { |
|
|
15 | cf::wait_for_tick; |
17 | |
16 | |
18 | $portal->slaying ($map); |
17 | while (my ($name, $v) = each %follow) { |
19 | $portal->stats->hp ($x); |
18 | my ($target, $his, $mine) = @$v; |
20 | $portal->stats->sp ($y); |
19 | my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target); |
21 | |
20 | |
22 | $portal->apply ($pl->ob); |
21 | if ($who && $other && $other->ob->map) { |
|
|
22 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
23 | |
23 | |
24 | $portal->destroy; |
24 | if ( |
25 | } |
25 | ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) |
|
|
26 | && $map !~ /^\{/ |
|
|
27 | ) { |
|
|
28 | @$mine = @$his; |
|
|
29 | @$his = ($map, $x, $y); |
|
|
30 | } |
26 | |
31 | |
27 | my %follow; |
32 | if (my $map = cf::map::find $mine->[0]) { |
|
|
33 | $map->load; |
28 | |
34 | |
29 | my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { |
35 | if ($map =~ /^\// # short-gap fix |
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 | |
|
|
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] |
|
|
45 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
36 | and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR), |
46 | $map->at ($mine->[1], $mine->[2])) { |
37 | $map->at ($mine->[1], $mine->[2]) |
47 | teleport $who, @$mine; |
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 | } |
48 | } else { |
46 | } else { |
49 | delete $follow{$name}; |
47 | delete $follow{$name}; |
50 | $who->ob->message ("You can't follow $target anymore!"); |
48 | $who->ob->message ("$target is gone...") |
|
|
49 | if $who; |
51 | } |
50 | } |
52 | } else { |
|
|
53 | delete $follow{$name}; |
|
|
54 | $who->ob->message ("$target is gone..."); |
|
|
55 | } |
51 | } |
|
|
52 | |
|
|
53 | Coro::schedule unless keys %follow; |
56 | } |
54 | } |
57 | |
|
|
58 | $_[0]->w->stop unless keys %follow; |
|
|
59 | }); |
55 | }; |
60 | |
56 | |
61 | cf::register_command follow => sub { |
57 | cf::register_command follow => sub { |
62 | my ($who, $args) = @_; |
58 | my ($who, $args) = @_; |
63 | |
59 | |
64 | my $name = $who->name; |
60 | my $name = $who->name; |
65 | |
61 | |
66 | if ($args ne "" && $name ne $args) { |
62 | if ($args ne "" && $name ne $args) { |
67 | if (my $other = cf::player::find $args) { |
63 | if (my $other = cf::player::find_active $args) { |
68 | if ($other->ob->map->path eq $who->map->path |
64 | if ($other->ob->map == $who->map |
69 | && abs ($other->ob->x - $who->x) <= 1 |
65 | && abs ($other->ob->x - $who->x) <= 1 |
70 | && abs ($other->ob->y - $who->y) <= 1) { |
66 | && abs ($other->ob->y - $who->y) <= 1) { |
71 | $who->message ("Following player '$args', to stop, type: 'follow"); |
67 | $who->message ("Following player '$args', to stop, type: 'follow"); |
72 | $other->ob->message ("$name is now following your every step..."); |
68 | $other->ob->message ("$name is now following your every step..."); |
73 | $follow{$name} = [ |
69 | $follow{$name} = [ |
74 | $args, |
70 | $args, |
75 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
71 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
76 | [$who->map->path, $who->x, $who->y], |
72 | [$who->map->path, $who->x, $who->y], |
77 | ]; |
73 | ]; |
78 | $timer->start; |
74 | $CORO->ready; |
79 | } else { |
75 | } else { |
80 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
76 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
81 | delete $follow{$name}; |
77 | delete $follow{$name}; |
82 | } |
78 | } |
83 | } else { |
79 | } else { |
… | |
… | |
88 | $who->message ("follow mode off"); |
84 | $who->message ("follow mode off"); |
89 | delete $follow{$name}; |
85 | delete $follow{$name}; |
90 | } |
86 | } |
91 | }; |
87 | }; |
92 | |
88 | |
93 | cf::attach_to_players |
89 | cf::player->attach ( |
94 | on_death => sub { |
90 | on_death => sub { |
95 | my ($pl) = @_; |
91 | my ($pl) = @_; |
96 | |
92 | |
97 | my $name = $pl->ob->name; |
93 | my $name = $pl->ob->name; |
98 | |
94 | |
… | |
… | |
102 | if ($v->[0] eq $name) { |
98 | if ($v->[0] eq $name) { |
103 | delete $follow{$k}; |
99 | delete $follow{$k}; |
104 | } |
100 | } |
105 | } |
101 | } |
106 | }, |
102 | }, |
107 | ; |
103 | ); |
108 | |
104 | |
109 | |
105 | |
110 | |
106 | |
111 | |
107 | |