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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines