--- deliantra/server/ext/map-random.ext 2009/06/06 21:43:13 1.33 +++ deliantra/server/ext/map-random.ext 2010/03/27 20:12:24 1.39 @@ -56,26 +56,14 @@ 1 } -sub clean_random_maps { - return;#d# - my $files = Coro::AIO::aio_readdirx $cf::RANDOMDIR, IO::AIO::READDIR_STAT_ORDER - or return; +sub select_random_map { + my ($maps, $difficulty) = @_; - my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7; + # because I am lazy, I ignore the weighting - for my $file (@$files) { - next unless $file =~ /\.meta$/; + my @maps = keys %$maps; - Coro::AIO::aio_stat "$cf::RANDOMDIR/$file" - and next; - - my $age = $cf::NOW - (stat _)[8]; - - if ($age > $META_TIMEOUT) { - warn "resetting random meta data for $file"; - IO::AIO::aio_unlink "$cf::RANDOMDIR/$file"; - } - } + cf::map::find "/styles/$maps[cf::rmg_rndm scalar @maps]" } # called by the random map generator @@ -85,6 +73,16 @@ my $map; + # see if there is a metafile + if (0 < aio_load "$cf::MAPDIR/$path.rmg", my $meta) { + $meta = cf::decode_json $meta; + + # only "maps" is supported + if ($meta->{maps}) { + $map = select_random_map $meta->{maps}, $difficulty; + } + } + $map = cf::map::find $path unless aio_stat "$cf::MAPDIR/$path.map"; @@ -130,7 +128,14 @@ cf::cede_to_tick; - my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty; + my $map; + + if ($name) { + $map = find_style_ "$dir/$name", $difficulty; + } else { + $map = (find_style_ "$dir/default", $difficulty) + || (find_style_ $dir, $difficulty); + } if ($map) { $map->load; @@ -141,9 +146,34 @@ $map } -# clean up old temp maps regularly -our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub { - clean_random_maps; +cf::async_ext { + $Coro::current->{desc} = "random map meta file cleaner"; + $Coro::current->nice (1); + + while () { + my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7; + + Coro::AnyEvent::idle_upto $META_TIMEOUT / 10 * 2; + + my ($files) = Coro::AIO::aio_readdirx $cf::RANDOMDIR, IO::AIO::READDIR_STAT_ORDER + or return; + + for my $file (@$files) { + next unless $file =~ /\.meta$/; + + Coro::AIO::aio_stat "$cf::RANDOMDIR/$file" + and next; + + my $age = $cf::NOW - (stat _)[8]; + + if ($age > $META_TIMEOUT) { + warn "resetting random meta data for $file"; + IO::AIO::aio_unlink "$cf::RANDOMDIR/$file"; + } + } + + Coro::AnyEvent::sleep $META_TIMEOUT / 10; + } }; # map generator stresstest, NEVER enable under normal circumstances @@ -176,6 +206,26 @@ } }; } + +# save test +if (0) { + cf::async { + # 0.080 + Coro::Timer::sleep 2; + my $map = cf::map::find "/mlab/citydeclouds2"; + $map->load_header; + $map->load; + $map->post_load_original; + my $m=100; + for (1..50) { + my $t=EV::time; + $map->_save_objects ("/tmp/x", cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); + $t = EV::time-$t; + $m=$t if $m>$t; + warn $m; + } + }; +} 1