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