ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.31 by elmex, Mon Aug 14 04:22:04 2006 UTC vs.
Revision 1.34 by root, Thu Aug 24 13:13:49 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use Time::HiRes;
10use Event; 11use Event;
11$Event::Eval = 1; # no idea why this is required, but it is 12$Event::Eval = 1; # no idea why this is required, but it is
12 13
13use strict; 14use strict;
14 15
384}; 385};
385 386
386############################################################################# 387#############################################################################
387# load/save perl data associated with player->ob objects 388# load/save perl data associated with player->ob objects
388 389
390sub all_objects(@) {
391 @_, map all_objects ($_->inv), @_
392}
393
389*on_player_load = sub { 394*on_player_load = sub {
390 my ($ob, $path) = @_; 395 my ($ob, $path) = @_;
391 396
392 for my $o ($ob, $ob->inv) { 397 for my $o (all_objects $ob) {
393 if (my $value = $o->get_ob_key_value ("_perl_data")) { 398 if (my $value = $o->get_ob_key_value ("_perl_data")) {
394 $o->set_ob_key_value ("_perl_data"); 399 $o->set_ob_key_value ("_perl_data");
395 400
396 %$o = %{ Storable::thaw pack "H*", $value }; 401 %$o = %{ Storable::thaw pack "H*", $value };
397 } 402 }
400 405
401*on_player_save = sub { 406*on_player_save = sub {
402 my ($ob, $path) = @_; 407 my ($ob, $path) = @_;
403 408
404 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_) 409 $_->set_ob_key_value (_perl_data => unpack "H*", Storable::nfreeze $_)
405 for grep %$_, $ob, $ob->inv; 410 for grep %$_, all_objects $ob;
406}; 411};
407 412
408############################################################################# 413#############################################################################
409# core extensions - in perl 414# core extensions - in perl
410 415
510 no strict 'refs'; 515 no strict 'refs';
511 *{"ext::$fun"} = $safe_hole->wrap ($cb); 516 *{"ext::$fun"} = $safe_hole->wrap ($cb);
512} 517}
513 518
514############################################################################# 519#############################################################################
520# the server's main()
521
522sub run {
523 my $tick = MAX_TIME * 1e-6;
524 my $next = Event::time;
525 my $timer = Event->timer (at => $next, cb => sub {
526 cf::server_tick; # one server iteration
527
528 $next += $tick;
529 my $NOW = Event::time;
530
531 # if we are delayd by > 0.25 second, skip ticks
532 $next = $NOW if $NOW >= $next + .25;
533
534 $_[0]->w->at ($next);
535 $_[0]->w->start;
536 });
537
538 Event::loop;
539}
540
541#############################################################################
515# initialisation 542# initialisation
516 543
517register "<global>", __PACKAGE__; 544register "<global>", __PACKAGE__;
518 545
519unshift @INC, $LIBDIR; 546unshift @INC, $LIBDIR;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines