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.609 by root, Tue Nov 20 14:30:22 2012 UTC vs.
Revision 1.624 by root, Sat Nov 17 23:40:02 2018 UTC

1# 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG. 2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3# 3#
4# Copyright (©) 2018 Marc Alexander Lehmann / the Deliantra team
4# Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 5# Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 6#
6# Deliantra is free software: you can redistribute it and/or modify it under 7# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 8# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 9# Free Software Foundation, either version 3 of the License, or (at your
57use JSON::XS 2.01 (); 58use JSON::XS 2.01 ();
58use BDB (); 59use BDB ();
59use Data::Dumper; 60use Data::Dumper;
60use Fcntl; 61use Fcntl;
61use YAML::XS (); 62use YAML::XS ();
63use CBOR::XS ();
62use IO::AIO (); 64use IO::AIO ();
63use Compress::LZF; 65use Compress::LZF;
64use Digest::MD5 (); 66use Digest::MD5 ();
65 67
66AnyEvent::detect; 68AnyEvent::detect;
225=item $cf::RUNTIME 227=item $cf::RUNTIME
226 228
227The time this server has run, starts at 0 and is increased by $cf::TICK on 229The time this server has run, starts at 0 and is increased by $cf::TICK on
228every server tick. 230every server tick.
229 231
230=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR 232=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR
231$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR 233$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR
232$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR 234$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR
233 235
234Various directories - "/etc", read-only install directory, perl-library 236Various directories - "/etc", read-only install directory, perl-library
235directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 237directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
464 466
465sub decode_yaml($) { 467sub decode_yaml($) {
466 fork_call { YAML::XS::Load $_[0] } @_ 468 fork_call { YAML::XS::Load $_[0] } @_
467} 469}
468 470
471=item $scalar = cf::decode_cbor $scalar
472
473Same as CBOR::XS::decode_cbor, but takes server ticks into account, so
474blocks. For small amounts of data, C<CBOR::XS::decode_cbor> is the better
475alternative.
476
477=cut
478
479sub decode_cbor($) {
480 # we assume 10mb/s minimum decoding speed (on a ~2ghz machine)
481 cf::get_slot +(length $_[0]) / 10_000_000, 0, "decode_cbor";
482 CBOR::XS::decode_cbor $_[0]
483}
484
469=item $scalar = cf::unlzf $scalar 485=item $scalar = cf::unlzf $scalar
470 486
471Same as Compress::LZF::compress, but takes server ticks into account, so 487Same as Compress::LZF::compress, but takes server ticks into account, so
472blocks. 488blocks.
473 489
510 } 526 }
511} 527}
512 528
513=item cf::lock_wait $string 529=item cf::lock_wait $string
514 530
515Wait until the given lock is available. See cf::lock_acquire. 531Wait until the given lock is available. See cf::lock_acquire.
516 532
517=item my $lock = cf::lock_acquire $string 533=item my $lock = cf::lock_acquire $string
518 534
519Wait until the given lock is available and then acquires it and returns 535Wait until the given lock is available and then acquires it and returns
520a L<Guard> object. If the guard object gets destroyed (goes out of scope, 536a L<Guard> object. If the guard object gets destroyed (goes out of scope,
596 my $signal = new Coro::Signal; 612 my $signal = new Coro::Signal;
597 my $busy; 613 my $busy;
598 614
599 while () { 615 while () {
600 next_job: 616 next_job:
617
618 Coro::cede;
601 619
602 my $avail = cf::till_tick; 620 my $avail = cf::till_tick;
603 621
604 for (0 .. $#SLOT_QUEUE) { 622 for (0 .. $#SLOT_QUEUE) {
605 if ($SLOT_QUEUE[$_][0] <= $avail) { 623 if ($SLOT_QUEUE[$_][0] <= $avail) {
606 $busy = 0; 624 $busy = 0;
607 my $job = splice @SLOT_QUEUE, $_, 1, (); 625 my $job = splice @SLOT_QUEUE, $_, 1, ();
608 $job->[2]->send; 626 $job->[2]->send;
609 Coro::cede;
610 goto next_job; 627 goto next_job;
611 } else { 628 } else {
612 $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; 629 $SLOT_QUEUE[$_][0] *= $SLOT_DECAY;
613 } 630 }
614 } 631 }
615 632
616 if (@SLOT_QUEUE) { 633 if (@SLOT_QUEUE) {
617 # we do not use wait_for_tick() as it returns immediately when tick is inactive 634 wait_for_tick;
618 $WAIT_FOR_TICK->wait;
619 } else { 635 } else {
620 $busy = 0; 636 $busy = 0;
621 Coro::schedule; 637 Coro::schedule;
622 } 638 }
623 } 639 }
649BEGIN { *async = \&Coro::async_pool } 665BEGIN { *async = \&Coro::async_pool }
650 666
651=item cf::sync_job { BLOCK } 667=item cf::sync_job { BLOCK }
652 668
653The design of Deliantra requires that the main coroutine ($Coro::main) 669The design of Deliantra requires that the main coroutine ($Coro::main)
654is always able to handle events or runnable, as Deliantra is only 670is always able to handle events or is runnable, as Deliantra is only
655partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not 671partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not
656acceptable. 672acceptable.
657 673
658If it must be done, put the blocking parts into C<sync_job>. This will run 674If it must be done, put the blocking parts into C<sync_job>. This will run
659the given BLOCK in another coroutine while waiting for the result. The 675the given BLOCK in another coroutine while waiting for the result. The
687 703
688 while ($busy) { 704 while ($busy) {
689 if (Coro::nready) { 705 if (Coro::nready) {
690 Coro::cede_notself; 706 Coro::cede_notself;
691 } else { 707 } else {
692 EV::loop EV::LOOP_ONESHOT; 708 EV::run EV::RUN_ONCE;
693 } 709 }
694 } 710 }
695 711
696 my $time = AE::time - $time; 712 my $time = AE::time - $time;
697 713
2384 2400
2385 $MAP_PREFETCHER ||= cf::async { 2401 $MAP_PREFETCHER ||= cf::async {
2386 $Coro::current->{desc} = "map prefetcher"; 2402 $Coro::current->{desc} = "map prefetcher";
2387 2403
2388 while (%MAP_PREFETCH) { 2404 while (%MAP_PREFETCH) {
2389 while (my ($k, $v) = each %MAP_PREFETCH) { 2405 for my $k (keys %MAP_PREFETCH) {
2390 if (my $map = find $k) { 2406 if (my $map = find $k) {
2391 $map->load if $v; 2407 $map->load if $MAP_PREFETCH{$k};
2392 } 2408 }
2393 2409
2394 delete $MAP_PREFETCH{$k}; 2410 delete $MAP_PREFETCH{$k};
2395 } 2411 }
2396 } 2412 }
2742=item $player_object->may ("access") 2758=item $player_object->may ("access")
2743 2759
2744Returns wether the given player is authorized to access resource "access" 2760Returns wether the given player is authorized to access resource "access"
2745(e.g. "command_wizcast"). 2761(e.g. "command_wizcast").
2746 2762
2763This is implemented by checking a config setting of C<may_access> where
2764C<access> is replaced by the access string. The following alternatives are
2765possible (and are tested in order):
2766
2767=over 4
2768
2769=item * Player is DM
2770
2771The request will succeed.
2772
2773=item * may_access is an array reference
2774
2775If either the player nickname or UUID is in the array, the request will
2776succeed, otherwise it will fail.
2777
2778=item * may_access is a true value
2779
2780The request will succeed.
2781
2782=item * may_access is missing or false
2783
2784The request will fail.
2785
2786=back
2787
2747=cut 2788=cut
2748 2789
2749sub cf::object::player::may { 2790sub cf::object::player::may {
2750 my ($self, $access) = @_; 2791 my ($self, $access) = @_;
2751 2792
2752 $self->flag (cf::FLAG_WIZ) || 2793 $self->flag (cf::FLAG_WIZ) ||
2753 (ref $cf::CFG{"may_$access"} 2794 (ref $cf::CFG{"may_$access"}
2754 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 2795 ? scalar grep $self->name eq $_ || $self->uuid eq $_, @{$cf::CFG{"may_$access"}}
2755 : $cf::CFG{"may_$access"}) 2796 : $cf::CFG{"may_$access"})
2756} 2797}
2757 2798
2758=item $player_object->enter_link 2799=item $player_object->enter_link
2759 2800
2762The player should be reasonably safe there for short amounts of time (e.g. 2803The player should be reasonably safe there for short amounts of time (e.g.
2763for loading a map). You I<MUST> call C<leave_link> as soon as possible, 2804for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2764though, as the player cannot control the character while it is on the link 2805though, as the player cannot control the character while it is on the link
2765map. 2806map.
2766 2807
2767Will never block. 2808This method will never block, which is the whole reaosn for it's
2809existance: you can I<always> put a player onto the link map, which is the
2810only place to put objects that is guaranteed to exist.
2811
2812A typical usage pattern is to call C<enter_link> synchronously from the
2813server, then start a new thread, do your blocking stuff there and then
2814call C<leave_link> from that thread.
2768 2815
2769=item $player_object->leave_link ($map, $x, $y) 2816=item $player_object->leave_link ($map, $x, $y)
2770 2817
2771Moves the player out of the special C<{link}> map onto the specified 2818Moves the player out of the special C<{link}> map onto the specified
2772map. If the map is not valid (or omitted), the player will be moved back 2819map. If the map is not valid (or omitted), the player will be moved back
3124 }, 3171 },
3125 "c/body" => { 3172 "c/body" => {
3126 id => "infobox", 3173 id => "infobox",
3127 title => "Body Parts", 3174 title => "Body Parts",
3128 reply => undef, 3175 reply => undef,
3129 tooltip => "Shows which body parts you posess and are available", 3176 tooltip => "Shows which body parts you possess and are available",
3130 }, 3177 },
3131 "c/statistics" => { 3178 "c/statistics" => {
3132 id => "infobox", 3179 id => "infobox",
3133 title => "Statistics", 3180 title => "Statistics",
3134 reply => undef, 3181 reply => undef,
3280 3327
3281=item $client->update_command_faces 3328=item $client->update_command_faces
3282 3329
3283=cut 3330=cut
3284 3331
3285our @COMMAND_FACES; # [normal commands, emotes, dm commands] 3332our %COMMAND_FACE;
3286 3333
3287sub cf::client::update_command_faces { 3334sub cf::client::update_command_faces {
3288 my ($self) = @_; 3335 my ($self) = @_;
3289 3336
3290 my @faces = ( 3337 my @faces = grep $_,
3338 $COMMAND_FACE{preferred},
3339 $COMMAND_FACE{standard},
3291 $COMMAND_FACES[0], 3340 $COMMAND_FACE{skill},
3292 $COMMAND_FACES[1],
3293 $self->pl->ob->flag (cf::FLAG_WIZ) ? $COMMAND_FACES[2] : () 3341 $self->pl->ob->flag (cf::FLAG_WIZ) ? $COMMAND_FACE{dm} : (),
3342 $COMMAND_FACE{emote},
3294 ); 3343 ;
3295 3344
3296 $self->send_face ($_) 3345 $self->send_face ($_)
3297 for @faces; 3346 for @faces;
3298 $self->flush_fx; 3347 $self->flush_fx;
3299 3348
3300 $self->ext_msg (command_list => @faces); 3349 $self->ext_msg (command_list => @faces);
3301} 3350}
3302 3351
3303=item cf::client::set_command_faces $normal_commands, $emotes, $dm_commands 3352=item cf::client::set_command_face $type, $commands
3304 3353
3305=cut 3354=cut
3306 3355
3307sub cf::client::set_command_faces { 3356sub cf::client::set_command_face {
3308 @COMMAND_FACES = @_; 3357 my ($type, $list) = @_;
3358
3359 my $idx = &cf::face::set ( #d# ugly forward reference
3360 "command_list/$type" => cf::FT_RSRC,
3361 JSON::XS->new->utf8->encode ([ sort @$list ])
3362 );
3363
3364 $COMMAND_FACE{$type} = $idx;
3309} 3365}
3310 3366
3311cf::client->attach ( 3367cf::client->attach (
3312 on_connect => sub { 3368 on_connect => sub {
3313 my ($ns) = @_; 3369 my ($ns) = @_;
3779 cf::face::set 3835 cf::face::set
3780 "res/spell_paths" => FT_RSRC, 3836 "res/spell_paths" => FT_RSRC,
3781 JSON::XS->new->utf8->canonical->encode ( 3837 JSON::XS->new->utf8->canonical->encode (
3782 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] 3838 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1]
3783 ); 3839 );
3840
3841 # command completion
3842 my @commands;
3843
3844 for (0..cf::arch::skillvec_size - 1) {
3845 my $skill = cf::arch::skillvec $_;
3846 my $name = $skill->name;
3847 my $flags = cf::skill_flags $skill->subtype;
3848
3849 push @commands, "ready_skill $name" if $flags & (SF_COMBAT | SF_RANGED | SF_GRACE);
3850 push @commands, "use_skill $name" if $flags & (SF_USE | SF_AUTARK | SF_GRACE);
3851 }
3852
3853 cf::client::set_command_face skill => \@commands;
3784} 3854}
3785 3855
3786sub reload_treasures { 3856sub reload_treasures {
3787 load_resource_file "$DATADIR/treasures" 3857 load_resource_file "$DATADIR/treasures"
3788 or die "unable to load treasurelists\n"; 3858 or die "unable to load treasurelists\n";
3861 seek $fh, 0, 0; 3931 seek $fh, 0, 0;
3862 print $fh $$; 3932 print $fh $$;
3863} 3933}
3864 3934
3865sub main_loop { 3935sub main_loop {
3866 trace "EV::loop starting\n"; 3936 trace "EV::run starting\n";
3867 if (1) { 3937 if (1) {
3868 EV::loop; 3938 EV::run;
3869 } 3939 }
3870 trace "EV::loop returned\n"; 3940 trace "EV::run returned\n";
3871 goto &main_loop unless $REALLY_UNLOOP; 3941 goto &main_loop unless $REALLY_UNLOOP;
3872} 3942}
3873 3943
3874sub main { 3944sub main {
3875 cf::init_globals; # initialise logging 3945 cf::init_globals; # initialise logging
3876 3946
3877 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3947 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3948 LOG llevInfo, "Copyright (C) 2017-2018 Marc Alexander Lehmann / the Deliantra team.";
3878 LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3949 LOG llevInfo, "Copyright (C) 2005-2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3879 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3950 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3880 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3951 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3881 3952
3882 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3953 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3883 3954
3884 # we must not ever block the main coroutine 3955 # we must not ever block the main coroutine
3885 $Coro::idle = sub { 3956 $Coro::idle = sub {
3886 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3957 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3887 (async { 3958 (async {
3888 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3959 $Coro::current->{desc} = "IDLE BUG HANDLER";
3889 EV::loop EV::LOOP_ONESHOT; 3960 EV::run EV::RUN_ONCE;
3890 })->prio (Coro::PRIO_MAX); 3961 })->prio (Coro::PRIO_MAX);
3891 }; 3962 };
3892 3963
3893 evthread_start IO::AIO::poll_fileno; 3964 evthread_start IO::AIO::poll_fileno;
3894 3965

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines