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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines