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.41 by root, Tue Apr 13 02:39:52 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, $recurse) = @_;
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) {
91 # search files and/or dirs 90 # search files and/or dirs
92 if (my ($dirs, $nondirs) = aio_scandir "$cf::MAPDIR/$path/", 1) { 91 if (my ($dirs, $nondirs) = aio_scandir "$cf::MAPDIR/$path/", 1) {
93 my @entries = sort grep s/\.map$//, @$nondirs; 92 my @entries = sort grep s/\.(?:map|rmg)$//, @$nondirs;
94 93
95 if ($difficulty < 0) { 94 if ($difficulty < 0) {
96 # pick a fully random map, but only a map, do not recurse 95 # pick a fully random map, but only a map, do not recurse
97 $map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]" 96 $map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]"
98 if @entries; 97 if @entries;
99 } else { 98 } else {
100 # pick a map with nearest difficulty value ("mapname_<difficulty>.map") 99 # pick a map with nearest difficulty value ("mapname_<difficulty>.map")
101 @entries = sort @$dirs 100 @entries = sort @$dirs
102 unless @entries; 101 unless @entries || !$recurse;
103 102
104 my $min_diff = 1e99; 103 my $min_diff = 1e99;
105 104
106 for my $name (@entries) { 105 for my $name (@entries) {
107 if ($name =~ /_(\d+)$/) { 106 if ($name =~ /_(\d+)$/) {
108 my $diff = abs $difficulty - $1 + 0.5; # prefer the more difficult version 107 my $diff = abs $difficulty - $1 + 0.25 + 0.25 * cf::rmg_rndm; # prefer the more difficult version
109 ($map, $min_diff) = ($name, $diff) if $diff < $min_diff; 108 ($map, $min_diff) = ($name, $diff) if $diff < $min_diff;
110 } 109 }
111 } 110 }
112 111
113 unless ($map) { 112 unless ($map) {
122 } 121 }
123 122
124 $map 123 $map
125} 124}
126 125
127sub find_style($$$) { 126sub find_style($$$$) {
128 my ($dir, $name, $difficulty) = @_; 127 my ($dir, $name, $difficulty, $recurse) = @_;
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, $recurse;
135 } else {
136 $map = (find_style_ "$dir/default", $difficulty, $recurse)
137 || (find_style_ $dir, $difficulty, $recurse);
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 eCoro::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=AE::time;
222 $map->_save_objects ("/tmp/x", cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
223 $t = AE::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