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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines