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.3 by root, Thu Dec 21 22:41:34 2006 UTC vs.
Revision 1.18 by root, Sun May 2 19:04:01 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]]
11 11
12 return if $pl->ob->map->path eq $map 12our $CORO = cf::async {
13 && abs ($pl->ob->x - $x) <= 1 13 $Coro::current->{desc} = "follow handler";
14 && abs ($pl->ob->y - $y) <= 1;
15 14
16 my $portal = cf::object::new "exit"; 15 while () {
16 cf::wait_for_tick;
17 17
18 $portal->slaying ($map); 18 for (values %follow) {
19 $portal->stats->hp ($x); 19 my ($who, $target, $queue) = @$_;
20 $portal->stats->sp ($y);
21 20
22 $portal->apply ($pl->ob); 21 my ($map, $x, $y) = ($target->map, $target->x, $target->y);
23 22
24 $portal->destroy; 23 # add new position to queue, if any
25} 24 push @$queue, [$map, $x, $y]
25 if $map != $queue->[-1][0]
26 || $x != $queue->[-1][1]
27 || $y != $queue->[-1][2];
26 28
27my %follow; 29 # try to move to oldest position
30 if (@$queue > $MAX_QUEUE) {
31 delete $follow{$who->name};
32 $who->message ($target->name . " is too far away - you can't follow anymore!");
33 } else {
34 my ($map, $x, $y) = @{ $queue->[0] };
28 35
29my $timer = Event->timer (interval => 0.2, parked => 1, data => cf::WF_AUTOCANCEL, cb => sub { 36 $map->load;
30 while (my ($name, $v) = each %follow) {
31 my ($target, $his, $mine) = @$v;
32 my ($who, $other) = (cf::player::find $name, cf::player::find $target);
33 37
34 if ($who && $other && $other->ob->map) { 38 if (
35 my ($map, $x, $y) = ($other->ob->map->path, $other->ob->x, $other->ob->y); 39 $map->path !~ /^(\{link\}|\/)/
40 or grep $_->flag (cf::FLAG_IS_FLOOR) && ($_->flag (cf::FLAG_UNIQUE) || $_->type == cf::SHOP_FLOOR),
41 $map->at ($x, $y)
42 ) {
43 delete $follow{$who->name};
44 $who->ob->message ("You can't follow " . $target->name . " anymore!");
45 } elsif (!$who->blocked ($map, $x, $y)) {
46 shift @$queue;
47 $who->goto ($map, $x, $y);
48 }
49 }
50 }
36 51
37 if ($map ne $his->[0] || $x != $his->[1] || $y != $his->[2]) { 52 Coro::schedule unless keys %follow;
38 @$mine = @$his;
39 @$his = ($map, $x, $y);
40 }
41
42 my $map;
43
44 if ($map = cf::map::find $mine->[0]
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 } 53 }
57
58 $_[0]->w->stop unless keys %follow;
59}); 54};
60 55
61cf::register_command follow => sub { 56cf::register_command follow => sub {
62 my ($who, $args) = @_; 57 my ($who, $args) = @_;
63 58
64 my $name = $who->name; 59 my $name = $who->name;
65 60
66 if ($args ne "" && $name ne $args) { 61 if ($args ne "" && $name ne $args) {
67 if (my $other = cf::player::find $args) { 62 if (my $other = cf::player::find_active $args) {
63 $other = $other->ob;
64
68 if ($other->ob->map->path eq $who->map->path 65 if ($other->map == $who->map
69 && abs ($other->ob->x - $who->x) <= 1 66 && abs ($other->x - $who->x) <= 1
70 && abs ($other->ob->y - $who->y) <= 1) { 67 && abs ($other->y - $who->y) <= 1
68 ) {
71 $who->message ("Following player '$args', to stop, type: 'follow"); 69 $who->message ("Following player '$args', to stop, type: 'follow");
72 $other->ob->message ("$name is now following your every step..."); 70 $other->message ("$name is now following your every step...");
73 $follow{$name} = [ 71 $follow{$name} = [
72 $who,
74 $args, 73 $other,
75 [$other->ob->map->path, $other->ob->x, $other->ob->y], 74 [[$other->map, $other->x, $other->y]],
76 [$who->map->path, $who->x, $who->y],
77 ]; 75 ];
78 $timer->start; 76 $CORO->ready;
79 } else { 77 } else {
80 $who->message ("You must stand directly beside '$args' to follow her/him"); 78 $who->message ("You must stand directly beside '$args' to follow her/him");
81 delete $follow{$name}; 79 delete $follow{$name};
82 } 80 }
83 } else { 81 } else {
88 $who->message ("follow mode off"); 86 $who->message ("follow mode off");
89 delete $follow{$name}; 87 delete $follow{$name};
90 } 88 }
91}; 89};
92 90
91sub unregister {
92 my ($pl) = @_;
93 my $name = $pl->ob->name;
94 delete $follow{$name};
95
96warn "unfollow $name\n";#d#
97
98 while (my ($k, $v) = each %follow) {
99 if ($v->[1]->name eq $name) {
100warn "unfollow $k\n";#d#
101 delete $follow{$k};
102 }
103 }
104}
105
93cf::player->attach ( 106cf::player->attach (
94 on_death => sub { 107 on_death => \&unregister,
95 my ($pl) = @_; 108 on_logout => \&unregister,
96
97 my $name = $pl->ob->name;
98
99 delete $follow{$name};
100
101 while (my ($k, $v) = each %follow) {
102 if ($v->[0] eq $name) {
103 delete $follow{$k};
104 }
105 }
106 },
107); 109);
108 110
109 111
110 112
111 113

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines