ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/map-random.ext
Revision: 1.38
Committed: Fri Mar 26 00:26:48 2010 UTC (14 years, 2 months ago) by root
Branch: MAIN
Changes since 1.37: +28 -24 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.17 #! perl # mandatory
2 root 1.1
3 root 1.26 use Coro::AIO;
4    
5 root 1.5 cf::map->register (qr{^\?random/([0-9a-f]{32})});
6 root 1.1
7     sub init {
8     my ($self) = @_;
9    
10 root 1.5 $self->{random_id} = $1;
11    
12 root 1.14 if (0 < Coro::AIO::aio_load "$cf::RANDOMDIR/$self->{random_id}.meta", my $data) {
13 root 1.19 $self->{random} = cf::decode_json $data;
14 root 1.3 $self->{random}{custom} ||= "$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}";
15 root 1.7 } else {
16     warn "unable to read meta file for $self->{random_id}\n";
17     return 0;
18 root 1.3 }
19 root 1.5
20     1
21 root 1.1 }
22    
23 root 1.6 sub thawer_merge {
24     # we have to keep some variables in memory intact
25     local $_[0]{random_id};
26     local $_[0]{random};
27    
28     $_[0]->SUPER::thawer_merge ($_[1]);
29     }
30    
31 root 1.1 sub visible_name {
32     my ($self) = @_;
33    
34 root 1.2 my $rmp = $self->{random};
35 root 1.4 "random map at $rmp->{custom}, level $rmp->{dungeon_level}"
36 root 1.1 }
37    
38     sub save_path {
39     my ($self) = @_;
40    
41 root 1.14 sprintf "%s/%s.map", $cf::RANDOMDIR, $self->{random_id}
42 root 1.1 }
43    
44     sub uniq_path {
45     undef
46     }
47    
48 root 1.8 sub load_header_orig {
49 root 1.1 my ($self) = @_;
50    
51 root 1.5 return unless $self->{random};
52    
53     $self->generate_random_map ($self->{random});
54 root 1.15 $self->activate;
55 root 1.5
56     1
57 root 1.1 }
58    
59 root 1.26 # called by the random map generator
60     sub find_style_;
61     sub find_style_($$) {
62     my ($path, $difficulty) = @_;
63    
64     my $map;
65    
66     $map = cf::map::find $path
67 root 1.27 unless aio_stat "$cf::MAPDIR/$path.map";
68 root 1.26
69     unless ($map) {
70     # search files and/or dirs
71 root 1.27 if (my ($dirs, $nondirs) = aio_scandir "$cf::MAPDIR/$path/", 1) {
72 root 1.26 my @entries = sort grep s/\.map$//, @$nondirs;
73    
74     if ($difficulty < 0) {
75     # pick a fully random map, but only a map, do not recurse
76     $map = cf::map::find "$path/$entries[cf::rmg_rndm scalar @entries]"
77     if @entries;
78     } else {
79     # pick a map with nearest difficulty value ("mapname_<difficulty>.map")
80     @entries = sort @$dirs
81     unless @entries;
82    
83     my $min_diff = 1e99;
84    
85     for my $name (@entries) {
86     if ($name =~ /_(\d+)$/) {
87     my $diff = abs $difficulty - $1 + 0.5; # prefer the more difficult version
88     ($map, $min_diff) = ($name, $diff) if $diff < $min_diff;
89     }
90     }
91    
92     unless ($map) {
93     # no map with given pattern found, choose a random map
94     $map = $entries[cf::rmg_rndm scalar @entries];
95     }
96    
97     $map = find_style_ "$path/$map", $difficulty
98     if $map;
99     }
100     }
101     }
102    
103     $map
104     }
105    
106     sub find_style($$$) {
107     my ($dir, $name, $difficulty) = @_;
108    
109     cf::cede_to_tick;
110    
111     my $map = find_style_ $name ? "$dir/$name" : $dir, $difficulty;
112    
113     if ($map) {
114     $map->load;
115     $map->deactivate;
116     }
117    
118 root 1.28 #warn "return $dir,$name,$difficulty => $map\n" if $difficulty >= 0;#d#
119 root 1.26 $map
120     }
121    
122 root 1.38 cf::async_ext {
123     $Coro::current->{desc} = "random map meta file cleaner";
124     $Coro::current->nice (1);
125    
126     while () {
127     my $META_TIMEOUT = $cf::CFG{map_random_meta_timeout} || 86400 * 7;
128    
129     Coro::AnyEvent::idle_upto $META_TIMEOUT / 10 * 2;
130    
131     my ($files) = Coro::AIO::aio_readdirx $cf::RANDOMDIR, IO::AIO::READDIR_STAT_ORDER
132     or return;
133    
134     for my $file (@$files) {
135     next unless $file =~ /\.meta$/;
136    
137     Coro::AIO::aio_stat "$cf::RANDOMDIR/$file"
138     and next;
139    
140     my $age = $cf::NOW - (stat _)[8];
141    
142     if ($age > $META_TIMEOUT) {
143     warn "resetting random meta data for $file";
144     IO::AIO::aio_unlink "$cf::RANDOMDIR/$file";
145     }
146     }
147    
148     Coro::AnyEvent::sleep $META_TIMEOUT / 10;
149     }
150 root 1.18 };
151 root 1.3
152 root 1.20 # map generator stresstest, NEVER enable under normal circumstances
153 root 1.29 if ($ENV{STRESSTEST}) {
154 root 1.11 cf::async {
155     my $seed = 0;
156     while () {
157     my $map = cf::map::new;
158     $map->generate_random_map ({
159     region => "scorn",
160     random_seed => $seed++,
161 root 1.31 xsize => (int rand 100) + 1,
162     ysize => (int rand 100) + 1,
163 root 1.11 });
164 root 1.29 warn sprintf "%d: %dx%d o# %d\n", $seed, $map->width, $map->height, &cf::object::objects_size;#d#
165 root 1.11 $map->destroy;
166     }
167     };
168     }
169    
170 root 1.25 # prefetch test, load some ocean-maps
171     if (0) {
172     cf::async {
173     # 0.58
174     Coro::Timer::sleep 2;
175     for my $x (200..219) {
176     for my $y (200..219) {
177     (cf::map::find "/world/world_$x\_$y")->load;
178     }
179     }
180     };
181     }
182    
183 root 1.35 # save test
184 root 1.37 if (0) {
185 root 1.35 cf::async {
186 root 1.37 # 0.080
187 root 1.35 Coro::Timer::sleep 2;
188     my $map = cf::map::find "/mlab/citydeclouds2";
189     $map->load_header;
190     $map->load;
191     $map->post_load_original;
192     my $m=100;
193     for (1..50) {
194     my $t=EV::time;
195     $map->_save_objects ("/tmp/x", cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
196     $t = EV::time-$t;
197     $m=$t if $m>$t;
198     warn $m;
199     }
200     };
201     }
202    
203 root 1.1 1
204