… | |
… | |
9 | use Event; |
9 | use Event; |
10 | use Opcode; |
10 | use Opcode; |
11 | use Safe; |
11 | use Safe; |
12 | use Safe::Hole; |
12 | use Safe::Hole; |
13 | |
13 | |
14 | use Coro 3.52 (); |
14 | use Coro 3.61 (); |
15 | use Coro::State; |
15 | use Coro::State; |
16 | use Coro::Event; |
16 | use Coro::Event; |
17 | use Coro::Timer; |
17 | use Coro::Timer; |
18 | use Coro::Signal; |
18 | use Coro::Signal; |
19 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
… | |
… | |
54 | |
54 | |
55 | our $RELOAD; # number of reloads so far |
55 | our $RELOAD; # number of reloads so far |
56 | our @EVENT; |
56 | our @EVENT; |
57 | our $LIBDIR = datadir . "/ext"; |
57 | our $LIBDIR = datadir . "/ext"; |
58 | |
58 | |
59 | our $TICK = MAX_TIME * 1e-6; |
59 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
60 | our $TICK_WATCHER; |
60 | our $TICK_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
63 | our $NEXT_TICK; |
63 | our $NEXT_TICK; |
64 | our $NOW; |
64 | our $NOW; |
… | |
… | |
2384 | } |
2384 | } |
2385 | |
2385 | |
2386 | 1 |
2386 | 1 |
2387 | } |
2387 | } |
2388 | |
2388 | |
|
|
2389 | sub load_archetypes { |
|
|
2390 | load_archetype_file sprintf "%s/archetypes", cf::datadir; # remove when stable |
|
|
2391 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | sub load_treasures { |
|
|
2395 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2396 | } |
|
|
2397 | |
2389 | sub reload_resources { |
2398 | sub reload_resources { |
|
|
2399 | warn "reloading resource files...\n"; |
|
|
2400 | |
2390 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2401 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2391 | or die "unable to load regions file\n";#d# |
2402 | or die "unable to load regions file\n"; |
2392 | load_facedata |
2403 | load_facedata |
2393 | or die "unable to load facedata\n";#d# |
2404 | or die "unable to load facedata\n"; |
|
|
2405 | load_archetypes |
|
|
2406 | or die "unable to load archetypes\n"; |
|
|
2407 | load_treasures |
|
|
2408 | or die "unable to load treasurelists\n"; |
|
|
2409 | |
|
|
2410 | warn "finished reloading resource files\n"; |
2394 | } |
2411 | } |
2395 | |
2412 | |
2396 | sub init { |
2413 | sub init { |
2397 | reload_resources; |
2414 | reload_resources; |
2398 | } |
2415 | } |
… | |
… | |
2646 | |
2663 | |
2647 | our @WAIT_FOR_TICK; |
2664 | our @WAIT_FOR_TICK; |
2648 | our @WAIT_FOR_TICK_BEGIN; |
2665 | our @WAIT_FOR_TICK_BEGIN; |
2649 | |
2666 | |
2650 | sub wait_for_tick { |
2667 | sub wait_for_tick { |
|
|
2668 | return unless $TICK_WATCHER->is_active; |
|
|
2669 | return if $Coro::current == $Coro::main; |
|
|
2670 | |
2651 | my $signal = new Coro::Signal; |
2671 | my $signal = new Coro::Signal; |
2652 | push @WAIT_FOR_TICK, $signal; |
2672 | push @WAIT_FOR_TICK, $signal; |
2653 | $signal->wait; |
2673 | $signal->wait; |
2654 | } |
2674 | } |
2655 | |
2675 | |
2656 | sub wait_for_tick_begin { |
2676 | sub wait_for_tick_begin { |
|
|
2677 | return unless $TICK_WATCHER->is_active; |
|
|
2678 | return if $Coro::current == $Coro::main; |
|
|
2679 | |
2657 | my $signal = new Coro::Signal; |
2680 | my $signal = new Coro::Signal; |
2658 | push @WAIT_FOR_TICK_BEGIN, $signal; |
2681 | push @WAIT_FOR_TICK_BEGIN, $signal; |
2659 | $signal->wait; |
2682 | $signal->wait; |
2660 | } |
2683 | } |
2661 | |
2684 | |
… | |
… | |
2673 | } |
2696 | } |
2674 | |
2697 | |
2675 | $NOW = Event::time; |
2698 | $NOW = Event::time; |
2676 | |
2699 | |
2677 | cf::server_tick; # one server iteration |
2700 | cf::server_tick; # one server iteration |
|
|
2701 | |
2678 | $RUNTIME += $TICK; |
2702 | $RUNTIME += $TICK; |
2679 | $NEXT_TICK += $TICK; |
2703 | $NEXT_TICK += $TICK; |
2680 | |
2704 | |
2681 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2705 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2682 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2706 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2684 | write_runtime |
2708 | write_runtime |
2685 | or warn "ERROR: unable to write runtime file: $!"; |
2709 | or warn "ERROR: unable to write runtime file: $!"; |
2686 | }; |
2710 | }; |
2687 | } |
2711 | } |
2688 | |
2712 | |
|
|
2713 | # my $AFTER = Event::time; |
|
|
2714 | # warn $AFTER - $NOW;#d# |
|
|
2715 | |
|
|
2716 | # if we are delayed by four ticks or more, skip them all |
|
|
2717 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2718 | |
|
|
2719 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2720 | $TICK_WATCHER->start; |
|
|
2721 | |
2689 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2722 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2690 | $sig->send; |
2723 | $sig->send; |
2691 | } |
2724 | } |
2692 | while (my $sig = shift @WAIT_FOR_TICK) { |
2725 | while (my $sig = shift @WAIT_FOR_TICK) { |
2693 | $sig->send; |
2726 | $sig->send; |
2694 | } |
2727 | } |
2695 | |
2728 | |
2696 | # my $AFTER = Event::time; |
2729 | _post_tick; |
2697 | # warn $AFTER - $NOW;#d# |
|
|
2698 | |
|
|
2699 | # if we are delayed by four ticks or more, skip them all |
|
|
2700 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2701 | |
|
|
2702 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2703 | $TICK_WATCHER->start; |
|
|
2704 | }, |
2730 | }, |
2705 | ); |
2731 | ); |
2706 | |
2732 | |
2707 | { |
2733 | { |
2708 | BDB::max_poll_time $TICK * 0.1; |
2734 | BDB::max_poll_time $TICK * 0.1; |