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