ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-random.ext
(Generate patch)

Comparing deliantra/server/ext/map-random.ext (file contents):
Revision 1.9 by root, Sun Jan 21 21:28:27 2007 UTC vs.
Revision 1.32 by root, Mon Sep 22 05:42:41 2008 UTC

1#! perl # MANDATORY 1#! perl # mandatory
2
3use Coro::AIO;
2 4
3cf::map->register (qr{^\?random/([0-9a-f]{32})}); 5cf::map->register (qr{^\?random/([0-9a-f]{32})});
4 6
5sub init { 7sub init {
6 my ($self) = @_; 8 my ($self) = @_;
7 9
8 $self->{random_id} = $1; 10 $self->{random_id} = $1;
9 11
10 if (0 < Coro::AIO::aio_load "$cf::RANDOM_MAPS/$self->{random_id}.meta", my $data) { 12 if (0 < Coro::AIO::aio_load "$cf::RANDOMDIR/$self->{random_id}.meta", my $data) {
11 $self->{random} = cf::from_json $data; 13 $self->{random} = cf::decode_json $data;
12 $self->{random}{custom} ||= "$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}"; 14 $self->{random}{custom} ||= "$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}";
13 } else { 15 } else {
14 warn "unable to read meta file for $self->{random_id}\n"; 16 warn "unable to read meta file for $self->{random_id}\n";
15 return 0; 17 return 0;
16 } 18 }
34} 36}
35 37
36sub save_path { 38sub save_path {
37 my ($self) = @_; 39 my ($self) = @_;
38 40
39 sprintf "%s/%s.map", $cf::RANDOM_MAPS, $self->{random_id} 41 sprintf "%s/%s.map", $cf::RANDOMDIR, $self->{random_id}
40} 42}
41 43
42sub uniq_path { 44sub uniq_path {
43 undef 45 undef
44} 46}
47 my ($self) = @_; 49 my ($self) = @_;
48 50
49 return unless $self->{random}; 51 return unless $self->{random};
50 52
51 $self->generate_random_map ($self->{random}); 53 $self->generate_random_map ($self->{random});
54 $self->activate;
52 55
53 1 56 1
54} 57}
55 58
56sub clean_random_maps { 59sub clean_random_maps {
60 return;#d#
61 my $files = Coro::AIO::aio_readdir $cf::RANDOMDIR
62 or return;
63
64 my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7;
65
66 for my $file (@$files) {
67 next unless $file =~ /\.meta$/;
68
69 Coro::AIO::aio_stat "$cf::RANDOMDIR/$file"
70 and next;
71
72 my $age = $cf::NOW - (stat _)[8];
73
74 if ($age > $META_TIMEOUT) {
75 warn "resetting random meta data for $file";
76 IO::AIO::aio_unlink "$cf::RANDOMDIR/$file";
77 }
78 }
79}
80
81# called by the random map generator
82sub find_style_;
83sub find_style_($$) {
84 my ($path, $difficulty) = @_;
85
86 my $map;
87
88 $map = cf::map::find $path
89 unless aio_stat "$cf::MAPDIR/$path.map";
90
91 unless ($map) {
92 # search files and/or dirs
93 if (my ($dirs, $nondirs) = aio_scandir "$cf::MAPDIR/$path/", 1) {
94 my @entries = sort grep s/\.map$//, @$nondirs;
95
96 if ($difficulty < 0) {
97 # pick a fully random map, but only a map, do not recurse
98 $map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]"
99 if @entries;
100 } else {
101 # pick a map with nearest difficulty value ("mapname_<difficulty>.map")
102 @entries = sort @$dirs
103 unless @entries;
104
105 my $min_diff = 1e99;
106
107 for my $name (@entries) {
108 if ($name =~ /_(\d+)$/) {
109 my $diff = abs $difficulty - $1 + 0.5; # prefer the more difficult version
110 ($map, $min_diff) = ($name, $diff) if $diff < $min_diff;
111 }
112 }
113
114 unless ($map) {
115 # no map with given pattern found, choose a random map
116 $map = $entries[cf::rmg_rndm scalar @entries];
117 }
118
119 $map = find_style_ "$path/$map", $difficulty
120 if $map;
121 }
122 }
123 }
124
125 $map
126}
127
128sub find_style($$$) {
129 my ($dir, $name, $difficulty) = @_;
130
131 cf::cede_to_tick;
132
133 my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty;
134
135 if ($map) {
136 $map->load;
137 $map->deactivate;
138 }
139
140 #warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d#
141 $map
142}
143
144# clean up old temp maps regularly
145our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub {
146 clean_random_maps;
147};
148
149# map generator stresstest, NEVER enable under normal circumstances
150if ($ENV{STRESSTEST}) {
57 cf::async { 151 cf::async {
58 my $files = Coro::AIO::aio_readdir $cf::RANDOM_MAPS 152 my $seed = 0;
59 or return; 153 while () {
154 my $map = cf::map::new;
155 $map->generate_random_map ({
156 region => "scorn",
157 random_seed => $seed++,
158 xsize => (int rand 100) + 1,
159 ysize => (int rand 100) + 1,
160 });
161 warn sprintf "%d: %dx%d o# %d\n", $seed, $map->width, $map->height, &cf::object::objects_size;#d#
162 $map->destroy;
163 }
164 };
165}
60 166
61 my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7; 167# prefetch test, load some ocean-maps
62 my $MAP_TIMEOUT = $cf::CFG{map_random_map_timeout} || 3600 * 6; 168if (0) {
63 169 cf::async {
64 for my $file (@$files) { 170 # 0.58
65 next if $file =~ /\.pst$/; 171 Coro::Timer::sleep 2;
66 172 for my $x (200..219) {
67 Coro::AIO::aio_stat "$cf::RANDOM_MAPS/$file" 173 for my $y (200..219) {
68 and next; 174 (cf::map::find "/world/world_$x\_$y")->load;
69
70 my $age = $cf::NOW - (stat _)[8];
71
72 if ($file =~ /\.meta$/) {
73 if ($age > $META_TIMEOUT) {
74 warn "resetting random meta data for $file";
75 IO::AIO::aio_unlink "$cf::RANDOM_MAPS/$file";
76 }
77 } else {
78 if ($age > $MAP_TIMEOUT) {
79 warn "resetting random map $file";
80 IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink "$cf::RANDOM_MAPS/$file";
81 IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink "$cf::RANDOM_MAPS/$file.pst";
82 }
83 } 175 }
84 } 176 }
85 }; 177 };
86} 178}
87 179
88# clean up old temp maps regularly
89Event->timer (
90 reentrant => 0,
91 data => cf::WF_AUTOCANCEL,
92 interval => 3600,
93 after => 60,
94 cb => \&clean_random_maps,
95);
96
971 1801
98 181

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines