… | |
… | |
2 | |
2 | |
3 | # TODO: skip arena |
3 | # TODO: skip arena |
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 |
|
|
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->hp ($x); |
|
|
20 | $portal->sp ($y); |
|
|
21 | |
|
|
22 | $portal->apply ($pl->ob); |
|
|
23 | |
|
|
24 | $portal->free; |
|
|
25 | } |
7 | |
26 | |
8 | 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::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 | } |
|
|
57 | |
|
|
58 | $_[0]->w->stop unless keys %follow; |
|
|
59 | }); |
9 | |
60 | |
10 | cf::register_command follow => 0, sub { |
61 | cf::register_command follow => 0, sub { |
11 | my ($who, $args) = @_; |
62 | my ($who, $args) = @_; |
12 | |
63 | |
13 | my $name = $who->name; |
64 | my $name = $who->name; |
… | |
… | |
22 | $follow{$name} = [ |
73 | $follow{$name} = [ |
23 | $args, |
74 | $args, |
24 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
75 | [$other->ob->map->path, $other->ob->x, $other->ob->y], |
25 | [$who->map->path, $who->x, $who->y], |
76 | [$who->map->path, $who->x, $who->y], |
26 | ]; |
77 | ]; |
|
|
78 | $timer->start; |
27 | } else { |
79 | } else { |
28 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
80 | $who->message ("You must stand directly beside '$args' to follow her/him"); |
29 | delete $follow{$name}; |
81 | delete $follow{$name}; |
30 | } |
82 | } |
31 | } else { |
83 | } else { |
… | |
… | |
36 | $who->message ("follow mode off"); |
88 | $who->message ("follow mode off"); |
37 | delete $follow{$name}; |
89 | delete $follow{$name}; |
38 | } |
90 | } |
39 | }; |
91 | }; |
40 | |
92 | |
41 | sub teleport { |
93 | cf::attach_to_players |
42 | my ($pl, $map, $x, $y) = @_; |
94 | on_death => sub { |
|
|
95 | my ($pl) = @_; |
43 | |
96 | |
44 | return if $pl->ob->map->path eq $map |
97 | my $name = $pl->ob->name; |
45 | && abs ($pl->ob->x - $x) <= 1 |
|
|
46 | && abs ($pl->ob->y - $y) <= 1; |
|
|
47 | |
98 | |
48 | my $portal = cf::object::new ("exit"); |
99 | delete $follow{$name}; |
49 | |
100 | |
50 | $portal->set_slaying ($map); |
|
|
51 | $portal->set_hp ($x); |
|
|
52 | $portal->set_sp ($y); |
|
|
53 | |
|
|
54 | $portal->apply ($pl->ob); |
|
|
55 | |
|
|
56 | $portal->free; |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub on_clock { |
|
|
60 | my ($event) = @_; |
|
|
61 | |
|
|
62 | return unless %follow; |
|
|
63 | |
|
|
64 | while (my ($name, $v) = each %follow) { |
101 | while (my ($k, $v) = each %follow) { |
65 | my ($target, $his, $mine) = @$v; |
102 | if ($v->[0] eq $name) { |
66 | my ($who, $other) = (cf::player::find $name, cf::player::find $target); |
103 | delete $follow{$k}; |
67 | |
|
|
68 | if ($who && $other && $other->ob->map) { |
|
|
69 | my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); |
|
|
70 | |
|
|
71 | if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { |
|
|
72 | @$mine = @$his; |
|
|
73 | @$his = ($map, $x, $y); |
|
|
74 | } |
104 | } |
75 | |
|
|
76 | my $map; |
|
|
77 | |
|
|
78 | if ($map = cf::map::map $mine->[0] |
|
|
79 | and !grep $_->flag (cf::FLAG_UNIQUE) && $_->flag (cf::FLAG_IS_FLOOR), |
|
|
80 | $map->at ($mine->[1], $mine->[2])) { |
|
|
81 | teleport $who, @$mine; |
|
|
82 | } else { |
|
|
83 | delete $follow{$name}; |
|
|
84 | $who->ob->message ("You can't follow $target anymore!"); |
|
|
85 | } |
|
|
86 | } else { |
|
|
87 | delete $follow{$name}; |
|
|
88 | $who->ob->message ("$target is gone..."); |
|
|
89 | } |
105 | } |
90 | } |
106 | }, |
91 | } |
107 | ; |
92 | |
108 | |
93 | |
109 | |
94 | |
110 | |
95 | |
111 | |