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 (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
5 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
6 | # 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 |
7 | # 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 |
8 | # Free Software Foundation, either version 3 of the License, or (at your |
9 | # option) any later version. |
9 | # option) any later version. |
… | |
… | |
53 | use Coro::Util (); |
53 | use Coro::Util (); |
54 | |
54 | |
55 | use JSON::XS 2.01 (); |
55 | use JSON::XS 2.01 (); |
56 | use BDB (); |
56 | use BDB (); |
57 | use Data::Dumper; |
57 | use Data::Dumper; |
58 | use Digest::MD5; |
|
|
59 | use Fcntl; |
58 | use Fcntl; |
60 | use YAML::XS (); |
59 | use YAML::XS (); |
61 | use IO::AIO (); |
60 | use IO::AIO (); |
62 | use Time::HiRes; |
61 | use Time::HiRes; |
63 | use Compress::LZF; |
62 | use Compress::LZF; |
… | |
… | |
424 | |
423 | |
425 | =cut |
424 | =cut |
426 | |
425 | |
427 | our @SLOT_QUEUE; |
426 | our @SLOT_QUEUE; |
428 | our $SLOT_QUEUE; |
427 | our $SLOT_QUEUE; |
|
|
428 | our $SLOT_DECAY = 0.9; |
429 | |
429 | |
430 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
430 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
431 | $SLOT_QUEUE = Coro::async { |
431 | $SLOT_QUEUE = Coro::async { |
432 | $Coro::current->desc ("timeslot manager"); |
432 | $Coro::current->desc ("timeslot manager"); |
433 | |
433 | |
434 | my $signal = new Coro::Signal; |
434 | my $signal = new Coro::Signal; |
|
|
435 | my $busy; |
435 | |
436 | |
436 | while () { |
437 | while () { |
437 | next_job: |
438 | next_job: |
|
|
439 | |
438 | my $avail = cf::till_tick; |
440 | my $avail = cf::till_tick; |
439 | if ($avail > 0.01) { |
441 | |
440 | for (0 .. $#SLOT_QUEUE) { |
442 | for (0 .. $#SLOT_QUEUE) { |
441 | if ($SLOT_QUEUE[$_][0] < $avail) { |
443 | if ($SLOT_QUEUE[$_][0] <= $avail) { |
|
|
444 | $busy = 0; |
442 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
445 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
443 | $job->[2]->send; |
446 | $job->[2]->send; |
444 | Coro::cede; |
447 | Coro::cede; |
445 | goto next_job; |
448 | goto next_job; |
446 | } |
449 | } else { |
|
|
450 | $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; |
447 | } |
451 | } |
448 | } |
452 | } |
449 | |
453 | |
450 | if (@SLOT_QUEUE) { |
454 | if (@SLOT_QUEUE) { |
451 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
455 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
452 | push @cf::WAIT_FOR_TICK, $signal; |
456 | push @cf::WAIT_FOR_TICK, $signal; |
453 | $signal->wait; |
457 | $signal->wait; |
454 | } else { |
458 | } else { |
|
|
459 | $busy = 0; |
455 | Coro::schedule; |
460 | Coro::schedule; |
456 | } |
461 | } |
457 | } |
462 | } |
458 | }; |
463 | }; |
459 | |
464 | |
460 | sub get_slot($;$$) { |
465 | sub get_slot($;$$) { |
461 | return if tick_inhibit || $Coro::current == $Coro::main; |
466 | return if tick_inhibit || $Coro::current == $Coro::main; |
462 | |
467 | |
463 | my ($time, $pri, $name) = @_; |
468 | my ($time, $pri, $name) = @_; |
464 | |
469 | |
465 | $time = $TICK * .6 if $time > $TICK * .6; |
470 | $time = clamp $time, 0.01, $TICK * .6; |
|
|
471 | |
466 | my $sig = new Coro::Signal; |
472 | my $sig = new Coro::Signal; |
467 | |
473 | |
468 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
474 | push @SLOT_QUEUE, [$time, $pri, $sig, $name]; |
469 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
475 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
470 | $SLOT_QUEUE->ready; |
476 | $SLOT_QUEUE->ready; |
… | |
… | |
1304 | } |
1310 | } |
1305 | |
1311 | |
1306 | use File::Glob (); |
1312 | use File::Glob (); |
1307 | |
1313 | |
1308 | cf::player->attach ( |
1314 | cf::player->attach ( |
1309 | on_command => sub { |
1315 | on_unknown_command => sub { |
1310 | my ($pl, $name, $params) = @_; |
1316 | my ($pl, $name, $params) = @_; |
1311 | |
1317 | |
1312 | my $cb = $COMMAND{$name} |
1318 | my $cb = $COMMAND{$name} |
1313 | or return; |
1319 | or return; |
1314 | |
1320 | |
… | |
… | |
1393 | . "\n};\n1"; |
1399 | . "\n};\n1"; |
1394 | |
1400 | |
1395 | $todo{$base} = \%ext; |
1401 | $todo{$base} = \%ext; |
1396 | } |
1402 | } |
1397 | |
1403 | |
|
|
1404 | my $pass = 0; |
1398 | my %done; |
1405 | my %done; |
1399 | while (%todo) { |
1406 | while (%todo) { |
1400 | my $progress; |
1407 | my $progress; |
1401 | |
1408 | |
|
|
1409 | ++$pass; |
|
|
1410 | |
|
|
1411 | ext: |
1402 | while (my ($k, $v) = each %todo) { |
1412 | while (my ($k, $v) = each %todo) { |
1403 | for (split /,\s*/, $v->{meta}{depends}) { |
1413 | for (split /,\s*/, $v->{meta}{depends}) { |
1404 | goto skip |
1414 | next ext |
1405 | unless exists $done{$_}; |
1415 | unless exists $done{$_}; |
1406 | } |
1416 | } |
1407 | |
1417 | |
1408 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1418 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1409 | |
1419 | |
1410 | unless (eval $v->{source}) { |
1420 | my $active = eval $v->{source}; |
|
|
1421 | |
|
|
1422 | if (length $@) { |
1411 | my $msg = $@ ? "$v->{path}: $@\n" |
1423 | warn "$v->{path}: $@\n"; |
1412 | : "$v->{base}: extension inactive.\n"; |
|
|
1413 | |
1424 | |
1414 | if (exists $v->{meta}{mandatory}) { |
|
|
1415 | warn $msg; |
|
|
1416 | cf::cleanup "mandatory extension failed to load, exiting."; |
1425 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1417 | } |
1426 | if exists $v->{meta}{mandatory}; |
1418 | |
1427 | } else { |
1419 | warn $msg; |
1428 | $done{$k} = delete $todo{$k}; |
|
|
1429 | push @EXTS, $v->{pkg}; |
|
|
1430 | $progress = 1; |
|
|
1431 | |
|
|
1432 | warn "$v->{base}: extension inactive.\n" |
|
|
1433 | unless $active; |
1420 | } |
1434 | } |
1421 | |
|
|
1422 | $done{$k} = delete $todo{$k}; |
|
|
1423 | push @EXTS, $v->{pkg}; |
|
|
1424 | $progress = 1; |
|
|
1425 | } |
1435 | } |
1426 | |
1436 | |
1427 | skip: |
1437 | unless ($progress) { |
1428 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1438 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1429 | unless $progress; |
1439 | |
|
|
1440 | while (my ($k, $v) = each %todo) { |
|
|
1441 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1442 | if exists $v->{meta}{mandatory}; |
|
|
1443 | } |
|
|
1444 | } |
1430 | } |
1445 | } |
1431 | }; |
1446 | }; |
1432 | } |
1447 | } |
1433 | |
1448 | |
1434 | ############################################################################# |
1449 | ############################################################################# |
… | |
… | |
1518 | $cf::PLAYER{$login} = $pl |
1533 | $cf::PLAYER{$login} = $pl |
1519 | } |
1534 | } |
1520 | } |
1535 | } |
1521 | } |
1536 | } |
1522 | |
1537 | |
|
|
1538 | cf::player->attach ( |
|
|
1539 | on_load => sub { |
|
|
1540 | my ($pl, $path) = @_; |
|
|
1541 | |
|
|
1542 | # restore slots saved in save, below |
|
|
1543 | my $slots = delete $pl->{_slots}; |
|
|
1544 | |
|
|
1545 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1546 | $pl->combat_ob ($slots->[1]); |
|
|
1547 | $pl->ranged_ob ($slots->[2]); |
|
|
1548 | }, |
|
|
1549 | ); |
|
|
1550 | |
1523 | sub save($) { |
1551 | sub save($) { |
1524 | my ($pl) = @_; |
1552 | my ($pl) = @_; |
1525 | |
1553 | |
1526 | return if $pl->{deny_save}; |
1554 | return if $pl->{deny_save}; |
1527 | |
1555 | |
… | |
… | |
1532 | |
1560 | |
1533 | aio_mkdir playerdir $pl, 0770; |
1561 | aio_mkdir playerdir $pl, 0770; |
1534 | $pl->{last_save} = $cf::RUNTIME; |
1562 | $pl->{last_save} = $cf::RUNTIME; |
1535 | |
1563 | |
1536 | cf::get_slot 0.01; |
1564 | cf::get_slot 0.01; |
|
|
1565 | |
|
|
1566 | # save slots, to be restored later |
|
|
1567 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1537 | |
1568 | |
1538 | $pl->save_pl ($path); |
1569 | $pl->save_pl ($path); |
1539 | cf::cede_to_tick; |
1570 | cf::cede_to_tick; |
1540 | } |
1571 | } |
1541 | |
1572 | |
… | |
… | |
1754 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1785 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1755 | |
1786 | |
1756 | # mit "rum" bekleckern, nicht |
1787 | # mit "rum" bekleckern, nicht |
1757 | $self->_create_random_map ( |
1788 | $self->_create_random_map ( |
1758 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1789 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1759 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1790 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1760 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1791 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1761 | $rmp->{exit_on_final_map}, |
1792 | $rmp->{exit_on_final_map}, |
1762 | $rmp->{xsize}, $rmp->{ysize}, |
1793 | $rmp->{xsize}, $rmp->{ysize}, |
1763 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1794 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1764 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1795 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
2010 | |
2041 | |
2011 | $cf::MAP{$path} = $map |
2042 | $cf::MAP{$path} = $map |
2012 | } |
2043 | } |
2013 | } |
2044 | } |
2014 | |
2045 | |
2015 | sub pre_load { } |
2046 | sub pre_load { } |
2016 | sub post_load { } |
2047 | #sub post_load { } # XS |
2017 | |
2048 | |
2018 | sub load { |
2049 | sub load { |
2019 | my ($self) = @_; |
2050 | my ($self) = @_; |
2020 | |
2051 | |
2021 | local $self->{deny_reset} = 1; # loading can take a long time |
2052 | local $self->{deny_reset} = 1; # loading can take a long time |
… | |
… | |
2078 | } |
2109 | } |
2079 | |
2110 | |
2080 | $self->post_load; |
2111 | $self->post_load; |
2081 | } |
2112 | } |
2082 | |
2113 | |
|
|
2114 | # customize the map for a given player, i.e. |
|
|
2115 | # return the _real_ map. used by e.g. per-player |
|
|
2116 | # maps to change the path to ~playername/mappath |
2083 | sub customise_for { |
2117 | sub customise_for { |
2084 | my ($self, $ob) = @_; |
2118 | my ($self, $ob) = @_; |
2085 | |
2119 | |
2086 | return find "~" . $ob->name . "/" . $self->{path} |
2120 | return find "~" . $ob->name . "/" . $self->{path} |
2087 | if $self->per_player; |
2121 | if $self->per_player; |
… | |
… | |
3116 | |
3150 | |
3117 | $coro |
3151 | $coro |
3118 | } |
3152 | } |
3119 | |
3153 | |
3120 | cf::client->attach ( |
3154 | cf::client->attach ( |
3121 | on_destroy => sub { |
3155 | on_client_destroy => sub { |
3122 | my ($ns) = @_; |
3156 | my ($ns) = @_; |
3123 | |
3157 | |
3124 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3158 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3125 | }, |
3159 | }, |
3126 | ); |
3160 | ); |
… | |
… | |
3142 | our $safe_hole = new Safe::Hole; |
3176 | our $safe_hole = new Safe::Hole; |
3143 | |
3177 | |
3144 | $SIG{FPE} = 'IGNORE'; |
3178 | $SIG{FPE} = 'IGNORE'; |
3145 | |
3179 | |
3146 | $safe->permit_only (Opcode::opset qw( |
3180 | $safe->permit_only (Opcode::opset qw( |
3147 | :base_core :base_mem :base_orig :base_math |
3181 | :base_core :base_mem :base_orig :base_math :base_loop |
3148 | grepstart grepwhile mapstart mapwhile |
3182 | grepstart grepwhile mapstart mapwhile |
3149 | sort time |
3183 | sort time |
3150 | )); |
3184 | )); |
3151 | |
3185 | |
3152 | # here we export the classes and methods available to script code |
3186 | # here we export the classes and methods available to script code |
… | |
… | |
3204 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3238 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3205 | $qcode =~ s/\n/\\n/g; |
3239 | $qcode =~ s/\n/\\n/g; |
3206 | |
3240 | |
3207 | %vars = (_dummy => 0) unless %vars; |
3241 | %vars = (_dummy => 0) unless %vars; |
3208 | |
3242 | |
|
|
3243 | my @res; |
3209 | local $_; |
3244 | local $_; |
3210 | local @safe::cf::_safe_eval_args = values %vars; |
|
|
3211 | |
3245 | |
3212 | my $eval = |
3246 | my $eval = |
3213 | "do {\n" |
3247 | "do {\n" |
3214 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3248 | . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" |
3215 | . "#line 0 \"{$qcode}\"\n" |
3249 | . "#line 0 \"{$qcode}\"\n" |
3216 | . $code |
3250 | . $code |
3217 | . "\n}" |
3251 | . "\n}" |
3218 | ; |
3252 | ; |
3219 | |
3253 | |
|
|
3254 | if ($CFG{safe_eval}) { |
3220 | sub_generation_inc; |
3255 | sub_generation_inc; |
|
|
3256 | local @safe::cf::_safe_eval_args = values %vars; |
3221 | my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3257 | @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); |
3222 | sub_generation_inc; |
3258 | sub_generation_inc; |
|
|
3259 | } else { |
|
|
3260 | local @cf::_safe_eval_args = values %vars; |
|
|
3261 | @res = wantarray ? eval eval : scalar eval $eval; |
|
|
3262 | } |
3223 | |
3263 | |
3224 | if ($@) { |
3264 | if ($@) { |
3225 | warn "$@"; |
3265 | warn "$@"; |
3226 | warn "while executing safe code '$code'\n"; |
3266 | warn "while executing safe code '$code'\n"; |
3227 | warn "with arguments " . (join " ", %vars) . "\n"; |
3267 | warn "with arguments " . (join " ", %vars) . "\n"; |
… | |
… | |
3246 | =cut |
3286 | =cut |
3247 | |
3287 | |
3248 | sub register_script_function { |
3288 | sub register_script_function { |
3249 | my ($fun, $cb) = @_; |
3289 | my ($fun, $cb) = @_; |
3250 | |
3290 | |
3251 | no strict 'refs'; |
3291 | $fun = "safe::$fun" if $CFG{safe_eval}; |
3252 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
3292 | *$fun = $safe_hole->wrap ($cb); |
3253 | } |
3293 | } |
3254 | |
3294 | |
3255 | =back |
3295 | =back |
3256 | |
3296 | |
3257 | =cut |
3297 | =cut |
… | |
… | |
3278 | |
3318 | |
3279 | $facedata->{version} == 2 |
3319 | $facedata->{version} == 2 |
3280 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3320 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3281 | |
3321 | |
3282 | # patch in the exptable |
3322 | # patch in the exptable |
|
|
3323 | my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]); |
3283 | $facedata->{resource}{"res/exp_table"} = { |
3324 | $facedata->{resource}{"res/exp_table"} = { |
3284 | type => FT_RSRC, |
3325 | type => FT_RSRC, |
3285 | data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), |
3326 | data => $exp_table, |
|
|
3327 | hash => (Digest::MD5::md5 $exp_table), |
3286 | }; |
3328 | }; |
3287 | cf::cede_to_tick; |
3329 | cf::cede_to_tick; |
3288 | |
3330 | |
3289 | { |
3331 | { |
3290 | my $faces = $facedata->{faceinfo}; |
3332 | my $faces = $facedata->{faceinfo}; |
… | |
… | |
3292 | while (my ($face, $info) = each %$faces) { |
3334 | while (my ($face, $info) = each %$faces) { |
3293 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3335 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3294 | |
3336 | |
3295 | cf::face::set_visibility $idx, $info->{visibility}; |
3337 | cf::face::set_visibility $idx, $info->{visibility}; |
3296 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3338 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3297 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3339 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3298 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3340 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3299 | |
3341 | |
3300 | cf::cede_to_tick; |
3342 | cf::cede_to_tick; |
3301 | } |
3343 | } |
3302 | |
3344 | |
3303 | while (my ($face, $info) = each %$faces) { |
3345 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3327 | |
3369 | |
3328 | cf::anim::invalidate_all; # d'oh |
3370 | cf::anim::invalidate_all; # d'oh |
3329 | } |
3371 | } |
3330 | |
3372 | |
3331 | { |
3373 | { |
3332 | # TODO: for gcfclient pleasure, we should give resources |
|
|
3333 | # that gcfclient doesn't grok a >10000 face index. |
|
|
3334 | my $res = $facedata->{resource}; |
3374 | my $res = $facedata->{resource}; |
3335 | |
3375 | |
3336 | while (my ($name, $info) = each %$res) { |
3376 | while (my ($name, $info) = each %$res) { |
3337 | if (defined $info->{type}) { |
3377 | if (defined $info->{type}) { |
3338 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3378 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3339 | my $data; |
|
|
3340 | |
3379 | |
3341 | if ($info->{type} & 1) { |
3380 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3342 | # prepend meta info |
|
|
3343 | |
|
|
3344 | my $meta = $enc->encode ({ |
|
|
3345 | name => $name, |
|
|
3346 | %{ $info->{meta} || {} }, |
|
|
3347 | }); |
|
|
3348 | |
|
|
3349 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3350 | } else { |
|
|
3351 | $data = $info->{data}; |
|
|
3352 | } |
|
|
3353 | |
|
|
3354 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3355 | cf::face::set_type $idx, $info->{type}; |
3381 | cf::face::set_type $idx, $info->{type}; |
3356 | } else { |
3382 | } else { |
3357 | $RESOURCE{$name} = $info; |
3383 | $RESOURCE{$name} = $info; |
3358 | } |
3384 | } |
3359 | |
3385 | |
… | |
… | |
3522 | }; |
3548 | }; |
3523 | |
3549 | |
3524 | evthread_start IO::AIO::poll_fileno; |
3550 | evthread_start IO::AIO::poll_fileno; |
3525 | |
3551 | |
3526 | cf::sync_job { |
3552 | cf::sync_job { |
|
|
3553 | cf::load_settings; |
|
|
3554 | cf::load_materials; |
|
|
3555 | |
3527 | reload_resources; |
3556 | reload_resources; |
3528 | reload_config; |
3557 | reload_config; |
3529 | db_init; |
3558 | db_init; |
3530 | |
3559 | |
3531 | cf::load_settings; |
|
|
3532 | cf::load_materials; |
|
|
3533 | cf::init_uuid; |
3560 | cf::init_uuid; |
3534 | cf::init_signals; |
3561 | cf::init_signals; |
3535 | cf::init_commands; |
|
|
3536 | cf::init_skills; |
3562 | cf::init_skills; |
3537 | |
3563 | |
3538 | cf::init_beforeplay; |
3564 | cf::init_beforeplay; |
3539 | |
3565 | |
3540 | atomic; |
3566 | atomic; |
… | |
… | |
3565 | }; |
3591 | }; |
3566 | } |
3592 | } |
3567 | } |
3593 | } |
3568 | |
3594 | |
3569 | sub write_runtime_sync { |
3595 | sub write_runtime_sync { |
|
|
3596 | my $t0 = EV::time; |
|
|
3597 | |
3570 | # first touch the runtime file to show we are still running: |
3598 | # first touch the runtime file to show we are still running: |
3571 | # the fsync below can take a very very long time. |
3599 | # the fsync below can take a very very long time. |
3572 | |
3600 | |
3573 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3601 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3574 | |
3602 | |
3575 | my $guard = cf::lock_acquire "write_runtime"; |
3603 | my $guard = cf::lock_acquire "write_runtime"; |
3576 | |
3604 | |
3577 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3605 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3578 | or return; |
3606 | or return; |
3579 | |
3607 | |
3580 | my $value = $cf::RUNTIME + 90 + 10; |
3608 | my $value = $cf::RUNTIME + 90 + 10; |
3581 | # 10 is the runtime save interval, for a monotonic clock |
3609 | # 10 is the runtime save interval, for a monotonic clock |
3582 | # 60 allows for the watchdog to kill the server. |
3610 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3595 | or return; |
3623 | or return; |
3596 | |
3624 | |
3597 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3625 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3598 | and return; |
3626 | and return; |
3599 | |
3627 | |
3600 | warn "runtime file written.\n"; |
3628 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3601 | |
3629 | |
3602 | 1 |
3630 | 1 |
3603 | } |
3631 | } |
3604 | |
3632 | |
3605 | our $uuid_lock; |
3633 | our $uuid_lock; |
… | |
… | |
3896 | |
3924 | |
3897 | our @WAIT_FOR_TICK; |
3925 | our @WAIT_FOR_TICK; |
3898 | our @WAIT_FOR_TICK_BEGIN; |
3926 | our @WAIT_FOR_TICK_BEGIN; |
3899 | |
3927 | |
3900 | sub wait_for_tick { |
3928 | sub wait_for_tick { |
3901 | return if tick_inhibit || $Coro::current == $Coro::main; |
3929 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3902 | |
3930 | |
3903 | my $signal = new Coro::Signal; |
3931 | my $signal = new Coro::Signal; |
3904 | push @WAIT_FOR_TICK, $signal; |
3932 | push @WAIT_FOR_TICK, $signal; |
3905 | $signal->wait; |
3933 | $signal->wait; |
3906 | } |
3934 | } |
3907 | |
3935 | |
3908 | sub wait_for_tick_begin { |
3936 | sub wait_for_tick_begin { |
3909 | return if tick_inhibit || $Coro::current == $Coro::main; |
3937 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3910 | |
3938 | |
3911 | my $signal = new Coro::Signal; |
3939 | my $signal = new Coro::Signal; |
3912 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3940 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3913 | $signal->wait; |
3941 | $signal->wait; |
3914 | } |
3942 | } |
… | |
… | |
3919 | unless ++$bug_warning > 10; |
3947 | unless ++$bug_warning > 10; |
3920 | return; |
3948 | return; |
3921 | } |
3949 | } |
3922 | |
3950 | |
3923 | cf::server_tick; # one server iteration |
3951 | cf::server_tick; # one server iteration |
|
|
3952 | |
|
|
3953 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3924 | |
3954 | |
3925 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3955 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3926 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3956 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3927 | Coro::async_pool { |
3957 | Coro::async_pool { |
3928 | $Coro::current->{desc} = "runtime saver"; |
3958 | $Coro::current->{desc} = "runtime saver"; |
… | |
… | |
3951 | } |
3981 | } |
3952 | |
3982 | |
3953 | { |
3983 | { |
3954 | # configure BDB |
3984 | # configure BDB |
3955 | |
3985 | |
3956 | BDB::min_parallel 8; |
3986 | BDB::min_parallel 16; |
3957 | BDB::max_poll_reqs $TICK * 0.1; |
3987 | BDB::max_poll_reqs $TICK * 0.1; |
3958 | $AnyEvent::BDB::WATCHER->priority (1); |
3988 | $AnyEvent::BDB::WATCHER->priority (1); |
3959 | |
3989 | |
3960 | unless ($DB_ENV) { |
3990 | unless ($DB_ENV) { |
3961 | $DB_ENV = BDB::db_env_create; |
3991 | $DB_ENV = BDB::db_env_create; |