ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/player-scheduler.ext
Revision: 1.4
Committed: Tue Apr 6 23:34:57 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
Changes since 1.3: +16 -0 lines
Log Message:
slots

File Contents

# User Rev Content
1 root 1.1 #! perl # mandatory
2    
3     # player scheduler, evoking players from ram
4    
5     our $SCHEDULE_INTERVAL = $cf::CFG{player_schedule_interval} || 10; # time the player scheduler sleeps between runs
6     our $SAVE_TIMEOUT = $cf::CFG{player_save_interval} || 20; # save players every n seconds
7    
8 root 1.4 cf::player->attach (
9     on_load => sub {
10     my ($pl, $path) = @_;
11    
12     my $slots = delete $pl->{_slots};
13    
14     $pl->ob->current_weapon ($slots->[0]);
15     $pl->combat_ob ($slots->[1]);
16     $pl->ranged_ob ($slots->[2]);
17     },
18     on_save => sub {
19     my ($pl, $path) = @_;
20     $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob];
21     },
22     );
23    
24 root 1.1 our $SCHEDULER = cf::async_ext {
25     $Coro::current->{desc} = "player scheduler";
26    
27     while () {
28     Coro::EV::timer_once $SCHEDULE_INTERVAL;
29    
30     # this weird form of iteration over values is used because
31     # the hash changes underneath us frequently, and for
32     # keeps a direct reference to the value without (in 5.8 perls)
33     # keeping a reference, so this is prone to crashes or worse.
34     my @players = keys %cf::PLAYER;
35     for (@players) {
36     my $pl = $cf::PLAYER{$_}
37     or next;
38     $pl->valid or next;
39    
40     eval {
41     if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
42     $pl->save;
43    
44     unless ($pl->active || $pl->ns) {
45     # check refcounts, this is tricky and needs to be adjusted to fit server internals
46     my $ob = $pl->ob;
47    
48     my $pl_ref = $pl->refcnt_cnt;
49     my $ob_ref = $ob->refcnt_cnt;
50    
51 root 1.2 ## pl_ref == (P)$pl + (P)%cf::PLAYER + (C)ob->contr
52 root 1.3 ## ob_ref == (P)$ob + (C)pl->observe + (C)pl->viewpoint + (C)simply being an object
53     ## !$pl->ns ensures that ob == viewpoint == observe
54     if ($pl_ref == 3 && $ob_ref == 4) {
55 root 1.1 warn "player-scheduler destroy ", $ob->name;#d#
56    
57     # remove from sight and get fresh "copies"
58     $pl = delete $cf::PLAYER{$ob->name};
59     $ob = $pl->ob;
60    
61     $pl->destroy; # destroys $ob
62     } else {
63     my $a_ = $pl->refcnt;#d#
64     my $b_ = $ob->refcnt;#d#
65    
66     warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref/3 ob $ob_ref/3 (C pl $a_/1 ob $b_/2)\n";#d#
67     }
68     }
69     }
70     };
71     warn $@ if $@;
72     cf::cede_to_tick;
73     };
74     }
75     };
76    
77     $SCHEDULER->prio (1);
78