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.21 by root, Tue May 18 21:30:16 2010 UTC vs.
Revision 1.25 by root, Wed May 26 20:07:17 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines