ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/follow.ext
(Generate patch)

Comparing deliantra/server/ext/follow.ext (file contents):
Revision 1.12 by root, Mon Oct 8 14:32:44 2007 UTC vs.
Revision 1.19 by root, Sun May 2 19:42:15 2010 UTC

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
9my %follow; 9our $MAX_QUEUE = 5; # the # of positions somebody else can lead
10our %follow; # $followername => [$follower, $target, [$queue]]
10 11
11my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { 12sub unfollow($) {
12 while (my ($name, $v) = each %follow) { 13 my $name = shift;
13 my ($target, $his, $mine) = @$v;
14 my ($who, $other) = (cf::player::find_active $name, cf::player::find_active $target);
15 14
16 if ($who && $other && $other->ob->map) { 15 if (my $f = delete $follow{$name}) {
17 my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); 16 my ($who, $target, undef) = @$f;
17 $who->contr->detach ("follow_aborter");
18 $target->message ("$name no longer follows you.");
19 $who->message ("You no longer follow " . $target->name . ".");
20 }
21}
18 22
19 if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { 23cf::player::attachment follow_aborter =>
20 @$mine = @$his; 24 on_move => sub {
25 my ($pl, $dir) = @_;
26 unfollow $pl->ob->name;
27 },
28 on_login => sub {
29 my ($pl, $dir) = @_;
30 $pl->detach ("follow_aborter");
31 },
32;
33
34our $CORO = cf::async {
35 $Coro::current->{desc} = "follow handler";
36
37 while () {
38 cf::wait_for_tick;
39
40 for (values %follow) {
41 my ($who, $target, $queue) = @$_;
42
43 my ($map, $x, $y) = ($target->map, $target->x, $target->y);
44
45 # add new position to queue, if any
46 push @$queue, [$map, $x, $y]
47 if $map != $queue->[-1][0]
48 || $x != $queue->[-1][1]
49 || $y != $queue->[-1][2];
50
51 # try to move to oldest position
52 if (@$queue > $MAX_QUEUE) {
53 $who->message ($target->name . " is too far away - you can't follow anymore!");
54 unfollow $target->name;
55 } else {
56 my ($map, $x, $y) = @{ $queue->[0] };
57
58 $map->load;
59
60 if (
61 $map->path !~ /^(\{link\}|\/)/
62 or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR),
63 $map->at ($x, $y)
64 ) {
65 $who->message ("You can't follow " . $target->name . " anymore!");
66 unfollow $who->name;
67 } elsif (!$who->blocked ($map, $x, $y)) {
68 shift @$queue;
21 @$his = ($map, $x, $y); 69 $who->goto ($map, $x, $y);
70 }
22 } 71 }
72 }
23 73
24 my $map; 74 Coro::schedule unless keys %follow;
25
26 if ($map = cf::map::find $mine->[0]
27 and $map =~ /^\// # short-gap fix
28 and !grep +($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR) && $_->flag (cf::FLAG_IS_FLOOR),
29 $map->at ($mine->[1], $mine->[2])) {
30 $who->ob->goto (@$mine);
31 } else {
32 delete $follow{$name};
33 $who->ob->message ("You can't follow $target anymore!");
34 }
35 } else {
36 delete $follow{$name};
37 $who->ob->message ("$target is gone...");
38 }
39 } 75 }
40
41 $_[0]->w->stop unless keys %follow;
42}); 76};
43 77
44cf::register_command follow => sub { 78cf::register_command follow => sub {
45 my ($who, $args) = @_; 79 my ($who, $args) = @_;
46 80
47 my $name = $who->name; 81 my $name = $who->name;
48 82
49 if ($args ne "" && $name ne $args) { 83 if ($args ne "" && $name ne $args) {
50 if (my $other = cf::player::find_active $args) { 84 if (my $other = cf::player::find_active $args) {
85 $other = $other->ob;
86
51 if ($other->ob->map == $who->map 87 if ($other->map == $who->map
52 && abs ($other->ob->x - $who->x) <= 1 88 && abs ($other->x - $who->x) <= 1
53 && abs ($other->ob->y - $who->y) <= 1) { 89 && abs ($other->y - $who->y) <= 1
90 ) {
54 $who->message ("Following player '$args', to stop, type: 'follow"); 91 $who->message ("Following player '$args', to stop, type: 'follow");
55 $other->ob->message ("$name is now following your every step..."); 92 $other->message ("$name is now following your every step...");
56 $follow{$name} = [ 93 $follow{$name} = [
94 $who,
57 $args, 95 $other,
58 [$other->ob->map->path, $other->ob->x, $other->ob->y], 96 [[$other->map, $other->x, $other->y]],
59 [$who->map->path, $who->x, $who->y],
60 ]; 97 ];
61 $timer->start; 98 $who->contr->attach ("follow_aborter");
99 $CORO->ready;
62 } else { 100 } else {
63 $who->message ("You must stand directly beside '$args' to follow her/him"); 101 $who->message ("You must stand directly beside '$args' to follow her/him");
64 delete $follow{$name}; 102 delete $follow{$name};
65 } 103 }
66 } else { 104 } else {
71 $who->message ("follow mode off"); 109 $who->message ("follow mode off");
72 delete $follow{$name}; 110 delete $follow{$name};
73 } 111 }
74}; 112};
75 113
114sub unregister {
115 my ($pl) = @_;
116
117 my $name = $pl->ob->name;
118
119 unfollow $name;
120
121 while (my ($k, $v) = each %follow) {
122 unfollow $k
123 if $v->[1]->name eq $name;
124 }
125}
126
76cf::player->attach ( 127cf::player->attach (
77 on_death => sub { 128 on_death => \&unregister,
78 my ($pl) = @_; 129 on_logout => \&unregister,
79
80 my $name = $pl->ob->name;
81
82 delete $follow{$name};
83
84 while (my ($k, $v) = each %follow) {
85 if ($v->[0] eq $name) {
86 delete $follow{$k};
87 }
88 }
89 },
90); 130);
91 131
92 132
93 133
94 134

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines