1 | #! perl |
1 | #! perl |
2 | |
2 | |
|
|
3 | # TODO: skip arena |
|
|
4 | # TODO: check for player leaving |
|
|
5 | |
3 | # implement a 'follow' command |
6 | # implement a 'follow' command |
|
|
7 | # don't follow on damned ground |
|
|
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->set_slaying ($map); |
|
|
19 | $portal->set_hp ($x); |
|
|
20 | $portal->set_sp ($y); |
|
|
21 | |
|
|
22 | $portal->apply ($pl->ob); |
|
|
23 | |
|
|
24 | $portal->free; |
|
|
25 | } |
4 | |
26 | |
5 | my %follow; |
27 | my %follow; |
|
|
28 | |
|
|
29 | my $timer = Event->timer (interval => 0.2, parked => 1, cb => sub { |
|
|
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::map $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 | } |
|
|
57 | |
|
|
58 | $_[0]->w->stop unless keys %follow; |
|
|
59 | }); |
6 | |
60 | |
7 | cf::register_command follow => 0, sub { |
61 | cf::register_command follow => 0, sub { |
8 | my ($who, $args) = @_; |
62 | my ($who, $args) = @_; |
9 | |
63 | |
10 | my $name = $who->name; |
64 | my $name = $who->name; |
11 | |
65 | |
12 | if ($args ne "" && $name ne $args) { |
66 | if ($args ne "" && $name ne $args) { |
13 | if (cf::player::find $args) { |
67 | if (my $other = cf::player::find $args) { |
|
|
68 | if ($other->ob->map->path eq $who->map->path |
|
|
69 | && abs ($other->ob->x - $who->x) <= 1 |
|
|
70 | && abs ($other->ob->y - $who->y) <= 1) { |
14 | $who->message ("following player '$args', to stop, type: 'follow"); |
71 | $who->message ("Following player '$args', to stop, type: 'follow"); |
|
|
72 | $other->ob->message ("$name is now following your every step..."); |
15 | $follow{$name} = $args; |
73 | $follow{$name} = [ |
|
|
74 | $args, |
|
|
75 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
|
|
76 | [$who->map->path, $who->x, $who->y], |
|
|
77 | ]; |
|
|
78 | $timer->start; |
|
|
79 | } else { |
|
|
80 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
|
|
81 | delete $follow{$name}; |
|
|
82 | } |
16 | } else { |
83 | } else { |
17 | $who->message ("cannot follow '$args': no such player"); |
84 | $who->message ("Cannot follow '$args': no such player"); |
|
|
85 | delete $follow{$name}; |
18 | } |
86 | } |
19 | } else { |
87 | } else { |
20 | $who->message ("follow mode off"); |
88 | $who->message ("follow mode off"); |
21 | delete $follow{$name}; |
89 | delete $follow{$name}; |
22 | } |
90 | } |
23 | }; |
91 | }; |
24 | |
92 | |
25 | sub teleport { |
93 | cf::attach_to_players |
26 | my ($pl, $map, $x, $y) = @_; |
94 | on_death => sub { |
|
|
95 | my ($pl) = @_; |
27 | |
96 | |
28 | my $portal = cf::object::new ("exit"); |
97 | my $name = $pl->ob->name; |
29 | |
98 | |
30 | $portal->set_slaying ($map->path); |
99 | delete $follow{$name}; |
31 | $portal->set_hp ($x); |
|
|
32 | $portal->set_sp ($y); |
|
|
33 | |
100 | |
34 | $portal->apply ($pl->ob, 0); |
|
|
35 | |
|
|
36 | $portal->free; |
|
|
37 | } |
|
|
38 | |
|
|
39 | sub on_clock { |
|
|
40 | my ($event) = @_; |
|
|
41 | |
|
|
42 | return unless %follow; |
|
|
43 | |
|
|
44 | while (my ($name, $target) = each %follow) { |
101 | while (my ($k, $v) = each %follow) { |
45 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
102 | if ($v->[0] eq $name) { |
46 | if ($who && $other) { |
|
|
47 | teleport $who, $other->ob->map, $other->ob->x, $other->ob->y |
|
|
48 | if $who->ob->map->path ne $other->ob->map->path; |
|
|
49 | } else { |
|
|
50 | warn "follow: $name or $target is gone, removing from follow list\n"; |
|
|
51 | delete $follow{$name}; |
103 | delete $follow{$k}; |
|
|
104 | } |
52 | } |
105 | } |
53 | } |
106 | }, |
54 | } |
107 | ; |
55 | |
108 | |
56 | |
109 | |
57 | |
110 | |
58 | |
111 | |