ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/follow.ext
Revision: 1.17
Committed: Sun May 2 14:54:06 2010 UTC (14 years ago) by root
Branch: MAIN
Changes since 1.16: +12 -9 lines
Log Message:
slightly fix follow

File Contents

# Content
1 #! perl
2
3 # TODO: skip arena
4 # TODO: check for player leaving
5
6 # implement a 'follow' command
7 # don't follow on damned ground
8
9 our %follow;
10
11 our $CORO = cf::async {
12 $Coro::current->{desc} = "follow handler";
13
14 while () {
15 cf::wait_for_tick;
16
17 while (my ($name, $v) = each %follow) {
18 my ($target, $his, $mine) = @$v;
19 my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target);
20
21 if ($who && $other && $other->ob->map) {
22 my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y);
23
24 if (
25 ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2])
26 && $map !~ /^\{/
27 ) {
28 @$mine = @$his;
29 @$his = ($map, $x, $y);
30 }
31
32 if (my $map = cf::map::find $mine->[0]) {
33 $map->load;
34
35 if ($map =~ /^\// # short-gap fix
36 and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR),
37 $map->at ($mine->[1], $mine->[2])
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 }
46 } else {
47 delete $follow{$name};
48 $who->ob->message ("$target is gone...")
49 if $who;
50 }
51 }
52
53 Coro::schedule unless keys %follow;
54 }
55 };
56
57 cf::register_command follow => sub {
58 my ($who, $args) = @_;
59
60 my $name = $who->name;
61
62 if ($args ne "" && $name ne $args) {
63 if (my $other = cf::player::find_active $args) {
64 if ($other->ob->map == $who->map
65 && abs ($other->ob->x - $who->x) <= 1
66 && abs ($other->ob->y - $who->y) <= 1) {
67 $who->message ("Following player '$args', to stop, type: 'follow");
68 $other->ob->message ("$name is now following your every step...");
69 $follow{$name} = [
70 $args,
71 [$other->ob->map->path, $other->ob->x, $other->ob->y],
72 [$who->map->path, $who->x, $who->y],
73 ];
74 $CORO->ready;
75 } else {
76 $who->message ("You must stand directly beside '$args' to follow her/him");
77 delete $follow{$name};
78 }
79 } else {
80 $who->message ("Cannot follow '$args': no such player");
81 delete $follow{$name};
82 }
83 } else {
84 $who->message ("follow mode off");
85 delete $follow{$name};
86 }
87 };
88
89 cf::player->attach (
90 on_death => sub {
91 my ($pl) = @_;
92
93 my $name = $pl->ob->name;
94
95 delete $follow{$name};
96
97 while (my ($k, $v) = each %follow) {
98 if ($v->[0] eq $name) {
99 delete $follow{$k};
100 }
101 }
102 },
103 );
104
105
106
107