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,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011 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. |
… | |
… | |
93 | our @EVENT; |
93 | our @EVENT; |
94 | our @REFLECT; # set by XS |
94 | our @REFLECT; # set by XS |
95 | our %REFLECT; # set by us |
95 | our %REFLECT; # set by us |
96 | |
96 | |
97 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
|
|
98 | |
98 | our $DATADIR = datadir; |
99 | our $DATADIR = datadir; |
99 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $PODDIR = "$DATADIR/pod"; |
101 | our $PODDIR = "$DATADIR/pod"; |
101 | our $MAPDIR = "$DATADIR/" . mapdir; |
102 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
103 | |
102 | our $LOCALDIR = localdir; |
104 | our $LOCALDIR = localdir; |
103 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
105 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
104 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
106 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
105 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
107 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
… | |
… | |
312 | } |
314 | } |
313 | |
315 | |
314 | $EV::DIED = sub { |
316 | $EV::DIED = sub { |
315 | Carp::cluck "error in event callback: @_"; |
317 | Carp::cluck "error in event callback: @_"; |
316 | }; |
318 | }; |
|
|
319 | |
|
|
320 | ############################################################################# |
|
|
321 | |
|
|
322 | sub fork_call(&@); |
|
|
323 | sub get_slot($;$$); |
317 | |
324 | |
318 | ############################################################################# |
325 | ############################################################################# |
319 | |
326 | |
320 | =head2 UTILITY FUNCTIONS |
327 | =head2 UTILITY FUNCTIONS |
321 | |
328 | |
… | |
… | |
342 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
349 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
343 | $d |
350 | $d |
344 | } || "[unable to dump $_[0]: '$@']"; |
351 | } || "[unable to dump $_[0]: '$@']"; |
345 | } |
352 | } |
346 | |
353 | |
|
|
354 | =item $scalar = load_file $path |
|
|
355 | |
|
|
356 | Loads the given file from path and returns its contents. Croaks on error |
|
|
357 | and can block. |
|
|
358 | |
|
|
359 | =cut |
|
|
360 | |
|
|
361 | sub load_file($) { |
|
|
362 | 0 <= aio_load $_[0], my $data |
|
|
363 | or Carp::croak "$_[0]: $!"; |
|
|
364 | |
|
|
365 | $data |
|
|
366 | } |
|
|
367 | |
347 | =item $ref = cf::decode_json $json |
368 | =item $ref = cf::decode_json $json |
348 | |
369 | |
349 | Converts a JSON string into the corresponding perl data structure. |
370 | Converts a JSON string into the corresponding perl data structure. |
350 | |
371 | |
351 | =item $json = cf::encode_json $ref |
372 | =item $json = cf::encode_json $ref |
… | |
… | |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
378 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
358 | |
379 | |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
380 | sub encode_json($) { $json_coder->encode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
381 | sub decode_json($) { $json_coder->decode ($_[0]) } |
361 | |
382 | |
|
|
383 | =item $ref = cf::decode_storable $scalar |
|
|
384 | |
|
|
385 | Same as Coro::Storable::thaw, so blocks. |
|
|
386 | |
|
|
387 | =cut |
|
|
388 | |
|
|
389 | BEGIN { *decode_storable = \&Coro::Storable::thaw } |
|
|
390 | |
362 | =item $ref = cf::yaml_load $scalar |
391 | =item $ref = cf::decode_yaml $scalar |
363 | |
392 | |
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
393 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
365 | |
394 | |
366 | =cut |
395 | =cut |
367 | |
396 | |
368 | sub fork_call(&@); |
397 | sub decode_yaml($) { |
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
398 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
399 | } |
|
|
400 | |
|
|
401 | =item $scalar = cf::unlzf $scalar |
|
|
402 | |
|
|
403 | Same as Compress::LZF::compress, but takes server ticks into account, so |
|
|
404 | blocks. |
|
|
405 | |
|
|
406 | =cut |
|
|
407 | |
|
|
408 | sub unlzf($) { |
|
|
409 | # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine) |
|
|
410 | cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf"; |
|
|
411 | Compress::LZF::decompress $_[0] |
372 | } |
412 | } |
373 | |
413 | |
374 | =item cf::post_init { BLOCK } |
414 | =item cf::post_init { BLOCK } |
375 | |
415 | |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
416 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
… | |
… | |
460 | EV::periodic $start, $interval, 0, $cb |
500 | EV::periodic $start, $interval, 0, $cb |
461 | } |
501 | } |
462 | |
502 | |
463 | =item cf::get_slot $time[, $priority[, $name]] |
503 | =item cf::get_slot $time[, $priority[, $name]] |
464 | |
504 | |
465 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
505 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
466 | This call blocks and returns only when you have at least C<$time> seconds |
506 | (default: 0): This call blocks and returns only when you have at least |
467 | of cpu time till the next tick. The slot is only valid till the next cede. |
507 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
|
|
508 | till the next cede. |
|
|
509 | |
|
|
510 | Background jobs should use a priority les than zero, interactive jobs |
|
|
511 | should use 100 or more. |
468 | |
512 | |
469 | The optional C<$name> can be used to identify the job to run. It might be |
513 | The optional C<$name> can be used to identify the job to run. It might be |
470 | used for statistical purposes and should identify the same time-class. |
514 | used for statistical purposes and should identify the same time-class. |
471 | |
515 | |
472 | Useful for short background jobs. |
516 | Useful for short background jobs. |
… | |
… | |
767 | |
811 | |
768 | my @data; |
812 | my @data; |
769 | my $md5; |
813 | my $md5; |
770 | |
814 | |
771 | for (0 .. $#$src) { |
815 | for (0 .. $#$src) { |
772 | 0 <= aio_load $src->[$_], $data[$_] |
816 | $data[$_] = load_file $src->[$_]; |
773 | or Carp::croak "$src->[$_]: $!"; |
|
|
774 | } |
817 | } |
775 | |
818 | |
776 | # if processing is expensive, check |
819 | # if processing is expensive, check |
777 | # checksum first |
820 | # checksum first |
778 | if (1) { |
821 | if (1) { |
… | |
… | |
1512 | |
1555 | |
1513 | while (my ($k, $v) = each %todo) { |
1556 | while (my ($k, $v) = each %todo) { |
1514 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1557 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1515 | if exists $v->{meta}{mandatory}; |
1558 | if exists $v->{meta}{mandatory}; |
1516 | } |
1559 | } |
|
|
1560 | |
|
|
1561 | last; |
1517 | } |
1562 | } |
1518 | } |
1563 | } |
1519 | }; |
1564 | }; |
1520 | } |
1565 | } |
1521 | |
1566 | |
… | |
… | |
1861 | sub generate_random_map { |
1906 | sub generate_random_map { |
1862 | my ($self, $rmp) = @_; |
1907 | my ($self, $rmp) = @_; |
1863 | |
1908 | |
1864 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1909 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1865 | |
1910 | |
1866 | # mit "rum" bekleckern, nicht |
|
|
1867 | $self->_create_random_map ( |
1911 | $self->_create_random_map ($rmp); |
1868 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
|
|
1869 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
|
|
1870 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
|
|
1871 | $rmp->{exit_on_final_map}, |
|
|
1872 | $rmp->{xsize}, $rmp->{ysize}, |
|
|
1873 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
|
|
1874 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
|
|
1875 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
|
|
1876 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
|
|
1877 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
|
|
1878 | (cf::region::find $rmp->{region}), $rmp->{custom} |
|
|
1879 | ) |
|
|
1880 | } |
1912 | } |
1881 | |
1913 | |
1882 | =item cf::map->register ($regex, $prio) |
1914 | =item cf::map->register ($regex, $prio) |
1883 | |
1915 | |
1884 | Register a handler for the map path matching the given regex at the |
1916 | Register a handler for the map path matching the given regex at the |
… | |
… | |
1962 | |
1994 | |
1963 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1995 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1964 | () |
1996 | () |
1965 | } |
1997 | } |
1966 | |
1998 | |
|
|
1999 | # may re-bless or do other evil things |
1967 | sub init { |
2000 | sub init { |
1968 | my ($self) = @_; |
2001 | my ($self) = @_; |
1969 | |
2002 | |
1970 | $self |
2003 | $self |
1971 | } |
2004 | } |
… | |
… | |
2143 | $self->alloc; |
2176 | $self->alloc; |
2144 | |
2177 | |
2145 | $self->pre_load; |
2178 | $self->pre_load; |
2146 | cf::cede_to_tick; |
2179 | cf::cede_to_tick; |
2147 | |
2180 | |
|
|
2181 | if (exists $self->{load_path}) { |
2148 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2182 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2149 | $f->skip_block; |
2183 | $f->skip_block; |
2150 | $self->_load_objects ($f) |
2184 | $self->_load_objects ($f) |
2151 | or return; |
2185 | or return; |
2152 | |
2186 | |
2153 | $self->post_load_original |
2187 | $self->post_load_original |
2154 | if delete $self->{load_original}; |
2188 | if delete $self->{load_original}; |
2155 | |
2189 | |
2156 | if (my $uniq = $self->uniq_path) { |
2190 | if (my $uniq = $self->uniq_path) { |
2157 | utf8::encode $uniq; |
2191 | utf8::encode $uniq; |
2158 | unless (aio_stat $uniq) { |
2192 | unless (aio_stat $uniq) { |
2159 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2193 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2160 | $self->clear_unique_items; |
2194 | $self->clear_unique_items; |
2161 | $self->_load_objects ($f); |
2195 | $self->_load_objects ($f); |
2162 | $f->resolve_delayed_derefs; |
2196 | $f->resolve_delayed_derefs; |
|
|
2197 | } |
2163 | } |
2198 | } |
2164 | } |
2199 | } |
2165 | } |
|
|
2166 | |
2200 | |
2167 | $f->resolve_delayed_derefs; |
2201 | $f->resolve_delayed_derefs; |
|
|
2202 | } |
2168 | |
2203 | |
2169 | cf::cede_to_tick; |
2204 | cf::cede_to_tick; |
2170 | # now do the right thing for maps |
2205 | # now do the right thing for maps |
2171 | $self->link_multipart_objects; |
2206 | $self->link_multipart_objects; |
2172 | $self->difficulty ($self->estimate_difficulty) |
2207 | $self->difficulty ($self->estimate_difficulty) |
… | |
… | |
2186 | |
2221 | |
2187 | $self->in_memory (cf::MAP_ACTIVE); |
2222 | $self->in_memory (cf::MAP_ACTIVE); |
2188 | } |
2223 | } |
2189 | |
2224 | |
2190 | $self->post_load; |
2225 | $self->post_load; |
|
|
2226 | |
|
|
2227 | 1 |
2191 | } |
2228 | } |
2192 | |
2229 | |
2193 | # customize the map for a given player, i.e. |
2230 | # customize the map for a given player, i.e. |
2194 | # return the _real_ map. used by e.g. per-player |
2231 | # return the _real_ map. used by e.g. per-player |
2195 | # maps to change the path to ~playername/mappath |
2232 | # maps to change the path to ~playername/mappath |
… | |
… | |
3410 | |
3447 | |
3411 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3448 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3412 | |
3449 | |
3413 | trace "loading facedata from $path\n"; |
3450 | trace "loading facedata from $path\n"; |
3414 | |
3451 | |
3415 | 0 < aio_load $path, my $facedata |
3452 | my $facedata = decode_storable load_file $path; |
3416 | or die "$path: $!"; |
|
|
3417 | |
|
|
3418 | $facedata = Coro::Storable::thaw $facedata; |
|
|
3419 | |
3453 | |
3420 | $facedata->{version} == 2 |
3454 | $facedata->{version} == 2 |
3421 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3455 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3422 | |
3456 | |
3423 | # patch in the exptable |
3457 | # patch in the exptable |
… | |
… | |
3437 | |
3471 | |
3438 | cf::face::set_visibility $idx, $info->{visibility}; |
3472 | cf::face::set_visibility $idx, $info->{visibility}; |
3439 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3473 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3440 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3474 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3441 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3475 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
|
|
3476 | cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; |
3442 | |
3477 | |
3443 | cf::cede_to_tick; |
3478 | cf::cede_to_tick; |
3444 | } |
3479 | } |
3445 | |
3480 | |
3446 | while (my ($face, $info) = each %$faces) { |
3481 | while (my ($face, $info) = each %$faces) { |
… | |
… | |
3541 | } |
3576 | } |
3542 | |
3577 | |
3543 | sub reload_sound { |
3578 | sub reload_sound { |
3544 | trace "loading sound config from $DATADIR/sound\n"; |
3579 | trace "loading sound config from $DATADIR/sound\n"; |
3545 | |
3580 | |
3546 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3547 | or die "$DATADIR/sound $!"; |
|
|
3548 | |
|
|
3549 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
3581 | my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound"); |
3550 | |
3582 | |
3551 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3583 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3552 | my $sound = $soundconf->{compat}[$_] |
3584 | my $sound = $soundconf->{compat}[$_] |
3553 | or next; |
3585 | or next; |
3554 | |
3586 | |
… | |
… | |
3578 | } |
3610 | } |
3579 | |
3611 | |
3580 | sub reload_config { |
3612 | sub reload_config { |
3581 | trace "reloading config file...\n"; |
3613 | trace "reloading config file...\n"; |
3582 | |
3614 | |
3583 | 0 < aio_load "$CONFDIR/config", my $config |
3615 | my $config = load_file "$CONFDIR/config"; |
3584 | or die "$CONFDIR/config: $!"; |
|
|
3585 | |
|
|
3586 | utf8::decode $config; |
3616 | utf8::decode $config; |
3587 | *CFG = yaml_load $config; |
3617 | *CFG = decode_yaml $config; |
3588 | |
3618 | |
3589 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3619 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3590 | |
3620 | |
3591 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3621 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3592 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3622 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3630 | |
3660 | |
3631 | sub main { |
3661 | sub main { |
3632 | cf::init_globals; # initialise logging |
3662 | cf::init_globals; # initialise logging |
3633 | |
3663 | |
3634 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3664 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3635 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3665 | LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3636 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3666 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3637 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3667 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3638 | |
3668 | |
3639 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3669 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3640 | |
3670 | |
… | |
… | |
4149 | IO::AIO::min_parallel 8; |
4179 | IO::AIO::min_parallel 8; |
4150 | IO::AIO::max_poll_time $TICK * 0.1; |
4180 | IO::AIO::max_poll_time $TICK * 0.1; |
4151 | undef $AnyEvent::AIO::WATCHER; |
4181 | undef $AnyEvent::AIO::WATCHER; |
4152 | } |
4182 | } |
4153 | |
4183 | |
4154 | my $_log_backtrace; |
4184 | our $_log_backtrace; |
|
|
4185 | our $_log_backtrace_last; |
4155 | |
4186 | |
4156 | sub _log_backtrace { |
4187 | sub _log_backtrace { |
4157 | my ($msg, @addr) = @_; |
4188 | my ($msg, @addr) = @_; |
4158 | |
4189 | |
4159 | $msg =~ s/\n//; |
4190 | $msg =~ s/\n$//; |
4160 | |
4191 | |
|
|
4192 | if ($_log_backtrace_last eq $msg) { |
|
|
4193 | LOG llevInfo, "[ABT] $msg\n"; |
|
|
4194 | LOG llevInfo, "[ABT] [duplicate, suppressed]\n"; |
4161 | # limit the # of concurrent backtraces |
4195 | # limit the # of concurrent backtraces |
4162 | if ($_log_backtrace < 2) { |
4196 | } elsif ($_log_backtrace < 2) { |
|
|
4197 | $_log_backtrace_last = $msg; |
4163 | ++$_log_backtrace; |
4198 | ++$_log_backtrace; |
4164 | my $perl_bt = Carp::longmess $msg; |
4199 | my $perl_bt = Carp::longmess $msg; |
4165 | async { |
4200 | async { |
4166 | $Coro::current->{desc} = "abt $msg"; |
4201 | $Coro::current->{desc} = "abt $msg"; |
4167 | |
4202 | |
… | |
… | |
4187 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4222 | LOG llevInfo, "[ABT] $_\n" for @bt; |
4188 | --$_log_backtrace; |
4223 | --$_log_backtrace; |
4189 | }; |
4224 | }; |
4190 | } else { |
4225 | } else { |
4191 | LOG llevInfo, "[ABT] $msg\n"; |
4226 | LOG llevInfo, "[ABT] $msg\n"; |
4192 | LOG llevInfo, "[ABT] [suppressed]\n"; |
4227 | LOG llevInfo, "[ABT] [overload, suppressed]\n"; |
4193 | } |
4228 | } |
4194 | } |
4229 | } |
4195 | |
4230 | |
4196 | # load additional modules |
4231 | # load additional modules |
4197 | require "cf/$_.pm" for @EXTRA_MODULES; |
4232 | require "cf/$_.pm" for @EXTRA_MODULES; |