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.29 by root, Thu May 8 13:47:19 2008 UTC vs.
Revision 1.39 by root, Sat Mar 27 20:12:24 2010 UTC

54 $self->activate; 54 $self->activate;
55 55
56 1 56 1
57} 57}
58 58
59sub clean_random_maps { 59sub select_random_map {
60 my $files = Coro::AIO::aio_readdir $cf::RANDOMDIR 60 my ($maps, $difficulty) = @_;
61 or return;
62 61
63 my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7; 62 # because I am lazy, I ignore the weighting
64 63
65 for my $file (@$files) { 64 my @maps = keys %$maps;
66 next unless $file =~ /\.meta$/;
67 65
68 Coro::AIO::aio_stat "$cf::RANDOMDIR/$file" 66 cf::map::find "/styles/$maps[cf::rmg_rndm scalar @maps]"
69 and next;
70
71 my $age = $cf::NOW - (stat _)[8];
72
73 if ($age > $META_TIMEOUT) {
74 warn "resetting random meta data for $file";
75 IO::AIO::aio_unlink "$cf::RANDOMDIR/$file";
76 }
77 }
78} 67}
79 68
80# called by the random map generator 69# called by the random map generator
81sub find_style_; 70sub find_style_;
82sub find_style_($$) { 71sub find_style_($$) {
83 my ($path, $difficulty) = @_; 72 my ($path, $difficulty) = @_;
84 73
85 my $map; 74 my $map;
75
76 # see if there is a metafile
77 if (0 < aio_load "$cf::MAPDIR/$path.rmg", my $meta) {
78 $meta = cf::decode_json $meta;
79
80 # only "maps" is supported
81 if ($meta->{maps}) {
82 $map = select_random_map $meta->{maps}, $difficulty;
83 }
84 }
86 85
87 $map = cf::map::find $path 86 $map = cf::map::find $path
88 unless aio_stat "$cf::MAPDIR/$path.map"; 87 unless aio_stat "$cf::MAPDIR/$path.map";
89 88
90 unless ($map) { 89 unless ($map) {
127sub find_style($$$) { 126sub find_style($$$) {
128 my ($dir, $name, $difficulty) = @_; 127 my ($dir, $name, $difficulty) = @_;
129 128
130 cf::cede_to_tick; 129 cf::cede_to_tick;
131 130
131 my $map;
132
133 if ($name) {
132 my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty; 134 $map = find_style_ "$dir/$name", $difficulty;
135 } else {
136 $map = (find_style_ "$dir/default", $difficulty)
137 || (find_style_ $dir, $difficulty);
138 }
133 139
134 if ($map) { 140 if ($map) {
135 $map->load; 141 $map->load;
136 $map->deactivate; 142 $map->deactivate;
137 } 143 }
138 144
139 #warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d# 145 #warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d#
140 $map 146 $map
141} 147}
142 148
143# clean up old temp maps regularly 149cf::async_ext {
144our $CLEAN_RANDOM_MAPS = cf::periodic 3600, Coro::unblock_sub { 150 $Coro::current->{desc} = "random map meta file cleaner";
145 clean_random_maps; 151 $Coro::current->nice (1);
152
153 while () {
154 my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7;
155
156 Coro::AnyEvent::idle_upto $META_TIMEOUT / 10 * 2;
157
158 my ($files) = Coro::AIO::aio_readdirx $cf::RANDOMDIR, IO::AIO::READDIR_STAT_ORDER
159 or return;
160
161 for my $file (@$files) {
162 next unless $file =~ /\.meta$/;
163
164 Coro::AIO::aio_stat "$cf::RANDOMDIR/$file"
165 and next;
166
167 my $age = $cf::NOW - (stat _)[8];
168
169 if ($age > $META_TIMEOUT) {
170 warn "resetting random meta data for $file";
171 IO::AIO::aio_unlink "$cf::RANDOMDIR/$file";
172 }
173 }
174
175 Coro::AnyEvent::sleep $META_TIMEOUT / 10;
176 }
146}; 177};
147 178
148# map generator stresstest, NEVER enable under normal circumstances 179# map generator stresstest, NEVER enable under normal circumstances
149if ($ENV{STRESSTEST}) { 180if ($ENV{STRESSTEST}) {
150 cf::async { 181 cf::async {
152 while () { 183 while () {
153 my $map = cf::map::new; 184 my $map = cf::map::new;
154 $map->generate_random_map ({ 185 $map->generate_random_map ({
155 region => "scorn", 186 region => "scorn",
156 random_seed => $seed++, 187 random_seed => $seed++,
157 xsize => (int rand 50) + 3, 188 xsize => (int rand 100) + 1,
158 ysize => (int rand 50) + 3, 189 ysize => (int rand 100) + 1,
159 }); 190 });
160 warn sprintf "%d: %dx%d o# %d\n", $seed, $map->width, $map->height, &cf::object::objects_size;#d# 191 warn sprintf "%d: %dx%d o# %d\n", $seed, $map->width, $map->height, &cf::object::objects_size;#d#
161 $map->destroy; 192 $map->destroy;
162 } 193 }
163 }; 194 };
174 } 205 }
175 } 206 }
176 }; 207 };
177} 208}
178 209
210# save test
211if (0) {
212 cf::async {
213 # 0.080
214 Coro::Timer::sleep 2;
215 my $map = cf::map::find "/mlab/citydeclouds2";
216 $map->load_header;
217 $map->load;
218 $map->post_load_original;
219 my $m=100;
220 for (1..50) {
221 my $t=EV::time;
222 $map->_save_objects ("/tmp/x", cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
223 $t = EV::time-$t;
224 $m=$t if $m>$t;
225 warn $m;
226 }
227 };
228}
229
1791 2301
180 231

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines