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.7 by root, Fri Jan 5 01:06:26 2007 UTC vs.
Revision 1.21 by root, Tue May 18 21:30:16 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 cf::sync_job { 13 my $name = shift;
13 while (my ($name, $v) = each %follow) {
14 my ($target, $his, $mine) = @$v;
15 my ($who, $other) = (cf::player::find $name, cf::player::find $target);
16 14
17 if ($who && $other && $other->ob->map) { 15 if (my $f = delete $follow{$name}) {
18 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}
19 22
20 if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { 23cf::player::attachment follow_aborter =>
21 @$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->valid
62 or $map->path !~ /^(\{link\}|\/)/
63 or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR),
64 $map->at ($x, $y))
65 ) {
66 $who->message ("You can't follow " . $target->name . " anymore!");
67 unfollow $who->name;
68 } elsif (!$who->blocked ($map, $x, $y)) {
69 shift @$queue;
22 @$his = ($map, $x, $y); 70 $who->goto ($map, $x, $y);
23 } 71 }
24
25 my $map;
26
27 if ($map = cf::map::find $mine->[0]
28 and !grep $_->flag (cf::FLAG_UNIQUE) && $_->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 } 72 }
39 } 73 }
40 };
41 74
42 $_[0]->w->stop unless keys %follow; 75 Coro::schedule unless keys %follow;
76 }
43}); 77};
44 78
45cf::register_command follow => sub { 79cf::register_command follow => sub {
46 my ($who, $args) = @_; 80 my ($who, $args) = @_;
47 81
48 my $name = $who->name; 82 my $name = $who->name;
49 83
50 if ($args ne "" && $name ne $args) { 84 if ($args ne "" && $name ne $args) {
51 if (my $other = cf::player::find $args) { 85 if (my $other = cf::player::find_active $args) {
86 $other = $other->ob;
87
52 if ($other->ob->map eq $who->map 88 if ($other->map == $who->map
53 && abs ($other->ob->x - $who->x) <= 1 89 && abs ($other->x - $who->x) <= 1
54 && abs ($other->ob->y - $who->y) <= 1) { 90 && abs ($other->y - $who->y) <= 1
91 ) {
55 $who->message ("Following player '$args', to stop, type: 'follow"); 92 $who->message ("Following player '$args', to stop, type: 'follow");
56 $other->ob->message ("$name is now following your every step..."); 93 $other->message ("$name is now following your every step...");
57 $follow{$name} = [ 94 $follow{$name} = [
95 $who,
58 $args, 96 $other,
59 [$other->ob->map->path, $other->ob->x, $other->ob->y], 97 [[$other->map, $other->x, $other->y]],
60 [$who->map->path, $who->x, $who->y],
61 ]; 98 ];
62 $timer->start; 99 $who->contr->attach ("follow_aborter");
100 $CORO->ready;
63 } else { 101 } else {
64 $who->message ("You must stand directly beside '$args' to follow her/him"); 102 $who->message ("You must stand directly beside '$args' to follow her/him");
65 delete $follow{$name}; 103 delete $follow{$name};
66 } 104 }
67 } else { 105 } else {
72 $who->message ("follow mode off"); 110 $who->message ("follow mode off");
73 delete $follow{$name}; 111 delete $follow{$name};
74 } 112 }
75}; 113};
76 114
115sub unregister {
116 my ($pl) = @_;
117
118 my $name = $pl->ob->name;
119
120 unfollow $name;
121
122 while (my ($k, $v) = each %follow) {
123 unfollow $k
124 if $v->[1]->name eq $name;
125 }
126}
127
77cf::player->attach ( 128cf::player->attach (
78 on_death => sub { 129 on_death => \&unregister,
79 my ($pl) = @_; 130 on_logout => \&unregister,
80
81 my $name = $pl->ob->name;
82
83 delete $follow{$name};
84
85 while (my ($k, $v) = each %follow) {
86 if ($v->[0] eq $name) {
87 delete $follow{$k};
88 }
89 }
90 },
91); 131);
92 132
93
94
95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines