1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # why this is an extension is a good question. next question. |
3 | # why this is an extension is a good question. next question. |
4 | |
4 | |
|
|
5 | use Digest::MD5; |
5 | use Fcntl; |
6 | use Fcntl; |
6 | |
7 | |
7 | use Coro; |
8 | use Coro; |
8 | use Coro::AIO; |
9 | use Coro::AIO; |
9 | |
10 | |
… | |
… | |
14 | our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs |
15 | our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs |
15 | our $SAVE_TIMEOUT = 60; # save maps every n seconds |
16 | our $SAVE_TIMEOUT = 60; # save maps every n seconds |
16 | our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD |
17 | our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD |
17 | our $MAX_RESET = 7200; |
18 | our $MAX_RESET = 7200; |
18 | |
19 | |
|
|
20 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
21 | mkdir $RANDOM_MAPS; |
|
|
22 | |
19 | $DEACTIVATE_TIMEOUT = 3;#d# |
23 | $DEACTIVATE_TIMEOUT = 3;#d# |
20 | $SWAP_TIMEOUT = 5;#d# |
24 | $SWAP_TIMEOUT = 5;#d# |
21 | $SCHEDULE_INTERVAL = 1; |
25 | $SCHEDULE_INTERVAL = 1; |
22 | |
26 | |
23 | $cf::LINK_MAP ||= do { |
27 | $cf::LINK_MAP ||= do { |
24 | my $map = cf::map::new; |
28 | my $map = cf::map::new; |
25 | |
29 | |
26 | $map->width (1); |
30 | $map->width (41); |
27 | $map->height (1); |
31 | $map->height (41); |
28 | $map->alloc; |
32 | $map->alloc; |
29 | $map->path ("{link}"); |
33 | $map->path ("{link}"); |
30 | $map->{path} = bless { path => "{link}" }, "cf::path"; |
34 | $map->{path} = bless { path => "{link}" }, "cf::path"; |
31 | $map->in_memory (cf::MAP_IN_MEMORY); |
35 | $map->in_memory (cf::MAP_IN_MEMORY); |
32 | |
36 | |
… | |
… | |
37 | package cf::path; |
41 | package cf::path; |
38 | |
42 | |
39 | sub new { |
43 | sub new { |
40 | my ($class, $path, $base) = @_; |
44 | my ($class, $path, $base) = @_; |
41 | |
45 | |
42 | my %res; |
46 | my $self = bless { }, $class; |
43 | |
47 | |
|
|
48 | if ($path =~ s{^\?random/}{}) { |
|
|
49 | $self->{random} = cf::from_json $path; |
|
|
50 | } else { |
44 | if ($path =~ s{^~([^/]+)?}{}) { |
51 | if ($path =~ s{^~([^/]+)?}{}) { |
45 | $res{user_rel} = 1; |
52 | $self->{user_rel} = 1; |
46 | |
53 | |
47 | if (defined $1) { |
54 | if (defined $1) { |
48 | $res{user} = $1; |
55 | $self->{user} = $1; |
49 | } elsif ($base =~ m{^~([^/]+)/}) { |
56 | } elsif ($base =~ m{^~([^/]+)/}) { |
50 | $res{user} = $1; |
57 | $self->{user} = $1; |
|
|
58 | } else { |
|
|
59 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
|
|
60 | } |
|
|
61 | } elsif ($path =~ /^\//) { |
|
|
62 | # already absolute |
51 | } else { |
63 | } else { |
52 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
64 | $base =~ s{[^/]+/?$}{}; |
|
|
65 | return $class->new ("$base/$path"); |
53 | } |
66 | } |
54 | } elsif ($path =~ /^\//) { |
67 | |
55 | # already absolute |
68 | for ($path) { |
56 | } else { |
69 | redo if s{/\.?/}{/}; |
57 | $base =~ s{[^/]+/?$}{}; |
70 | redo if s{/[^/]+/\.\./}{/}; |
58 | return $class->new ("$base/$path"); |
71 | } |
59 | } |
72 | } |
60 | |
73 | |
61 | for ($path) { |
|
|
62 | redo if s{/\.?/}{/}; |
|
|
63 | redo if s{/[^/]+/\.\./}{/}; |
|
|
64 | } |
|
|
65 | |
|
|
66 | $res{path} = $path; |
74 | $self->{path} = $path; |
67 | |
75 | |
68 | bless \%res, $class |
76 | $self |
69 | } |
77 | } |
70 | |
78 | |
71 | # the name / primary key / in-game path |
79 | # the name / primary key / in-game path |
72 | sub as_string { |
80 | sub as_string { |
73 | my ($self) = @_; |
81 | my ($self) = @_; |
74 | |
82 | |
75 | $self->{user_rel} |
|
|
76 | ? "~$self->{user}$self->{path}" |
83 | $self->{user_rel} ? "~$self->{user}$self->{path}" |
|
|
84 | : $self->{random} ? "?random/$self->{path}" |
77 | : $self->{path} |
85 | : $self->{path} |
|
|
86 | } |
|
|
87 | |
|
|
88 | # the displayed name, this is a one way mapping |
|
|
89 | sub visible_name { |
|
|
90 | my ($self) = @_; |
|
|
91 | |
|
|
92 | $self->{random} ? "?random/$self->{random}{origin_map}+$self->{random}{origin_x}+$self->{random}{origin_y}/$self->{random}{dungeon_level}" |
|
|
93 | : $self->as_string |
78 | } |
94 | } |
79 | |
95 | |
80 | # escape the /'s in the path |
96 | # escape the /'s in the path |
81 | sub escaped_path { |
97 | sub _escaped_path { |
82 | # ∕ is U+2215 |
98 | # ∕ is U+2215 |
83 | (my $path = $_[0]{path}) =~ s/\//∕/g; |
99 | (my $path = $_[0]{path}) =~ s/\//∕/g; |
84 | $path |
100 | $path |
85 | } |
101 | } |
86 | |
102 | |
… | |
… | |
93 | |
109 | |
94 | # the temporary/swap location |
110 | # the temporary/swap location |
95 | sub save_path { |
111 | sub save_path { |
96 | my ($self) = @_; |
112 | my ($self) = @_; |
97 | |
113 | |
98 | $self->{user_rel} |
|
|
99 | ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->escaped_path |
114 | $self->{user_rel} ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path |
|
|
115 | : $self->{random} ? sprintf "%s/%s", $RANDOM_MAPS, Digest::MD5::md5_hex $self->{path} |
100 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->escaped_path |
116 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path |
101 | } |
117 | } |
102 | |
118 | |
103 | # the unique path, might be eq to save_path |
119 | # the unique path, might be eq to save_path |
104 | sub uniq_path { |
120 | sub uniq_path { |
105 | my ($self) = @_; |
121 | my ($self) = @_; |
106 | |
122 | |
107 | $self->{user_rel} |
123 | $self->{user_rel} || $self->{random} |
108 | ? undef |
124 | ? undef |
109 | : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->escaped_path |
125 | : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
|
|
126 | } |
|
|
127 | |
|
|
128 | # return random map parameters, or undef |
|
|
129 | sub random_map_params { |
|
|
130 | my ($self) = @_; |
|
|
131 | |
|
|
132 | $self->{random} |
|
|
133 | } |
|
|
134 | |
|
|
135 | # this is somewhat ugly, but style maps do need special treatment |
|
|
136 | sub is_style_map { |
|
|
137 | $_[0]{path} =~ m{^/styles/} |
110 | } |
138 | } |
111 | } |
139 | } |
112 | |
140 | |
113 | sub write_runtime { |
141 | sub write_runtime { |
114 | my $runtime = cf::localdir . "/runtime"; |
142 | my $runtime = cf::localdir . "/runtime"; |
… | |
… | |
138 | exit 1; |
166 | exit 1; |
139 | } |
167 | } |
140 | })->prio (Coro::PRIO_MAX); |
168 | })->prio (Coro::PRIO_MAX); |
141 | |
169 | |
142 | our $SCHEDULER = cf::coro { |
170 | our $SCHEDULER = cf::coro { |
|
|
171 | Coro::Timer::sleep 3600;#d#TODO#for debugging only |
143 | while () { |
172 | while () { |
144 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
173 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
145 | |
174 | |
146 | write_runtime |
175 | write_runtime |
147 | or warn "unable to write runtime file: $!"; |
176 | or warn "unable to write runtime file: $!"; |
148 | |
177 | |
149 | for my $map (values %cf::MAP) { |
178 | for my $map (values %cf::MAP) { |
150 | eval { |
179 | eval { |
151 | next if $map->in_memory != cf::MAP_IN_MEMORY; |
180 | next if $map->in_memory != cf::MAP_IN_MEMORY; |
|
|
181 | next if $map->players; |
152 | my $last_access = $map->last_access; |
182 | my $last_access = $map->last_access; |
153 | # not yet, because maps might become visible to players nearby |
183 | # not yet, because maps might become visible to players nearby |
154 | # we need a tiled meta map for this to work |
184 | # we need a tiled meta map for this to work |
155 | # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { |
185 | # if ($last_access + $DEACTIVATE_TIMEOUT <= $cf::RUNTIME) { |
156 | # $map->deactivate; |
186 | # $map->deactivate; |
… | |
… | |
171 | } |
201 | } |
172 | } |
202 | } |
173 | }; |
203 | }; |
174 | $SCHEDULER->prio (-2); |
204 | $SCHEDULER->prio (-2); |
175 | |
205 | |
176 | sub sync_job(&) { |
206 | sub generate_random_map { |
|
|
207 | my ($path, $rmp) = @_; |
|
|
208 | |
|
|
209 | # mit "rum" bekleckert, nicht |
|
|
210 | cf::map::_create_random_map |
|
|
211 | $path, |
|
|
212 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
|
|
213 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
|
|
214 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
|
|
215 | $rmp->{exit_on_final_map}, |
|
|
216 | $rmp->{xsize}, $rmp->{ysize}, |
|
|
217 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
|
|
218 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
|
|
219 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
|
|
220 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
|
|
221 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
|
|
222 | $rmp->{region} |
|
|
223 | } |
|
|
224 | |
|
|
225 | sub parse_random_map_params { |
177 | my ($job) = @_; |
226 | my ($spec) = @_; |
178 | |
227 | |
179 | my $done; |
228 | my $rmp = { # defaults |
180 | my @res; |
229 | xsize => 10, |
181 | |
230 | ysize => 10, |
182 | Carp::confess "nested sync_job" if $cf::FREEZE; |
|
|
183 | |
|
|
184 | local $cf::FREEZE = 1; |
|
|
185 | |
|
|
186 | (Coro::async { |
|
|
187 | @res = eval { $job->() }; |
|
|
188 | warn $@ if $@; |
|
|
189 | $done = 1; |
|
|
190 | })->prio (Coro::PRIO_MAX); |
|
|
191 | |
|
|
192 | while (!$done) { |
|
|
193 | Coro::cede_notself; |
|
|
194 | Event::one_event unless Coro::nready; |
|
|
195 | } |
231 | }; |
196 | |
232 | |
197 | wantarray ? @res : $res[0] |
233 | for (split /\n/, $spec) { |
|
|
234 | my ($k, $v) = split /\s+/, $_, 2; |
|
|
235 | |
|
|
236 | $rmp->{lc $k} = $v if (length $k) && (length $v); |
|
|
237 | } |
|
|
238 | |
|
|
239 | $rmp |
|
|
240 | } |
|
|
241 | |
|
|
242 | sub prepare_random_map { |
|
|
243 | my ($exit) = @_; |
|
|
244 | |
|
|
245 | # all this does is basically rpelace the /! path by |
|
|
246 | # a new random map path (?random/...) with a seed |
|
|
247 | # that depends on the exit object |
|
|
248 | |
|
|
249 | my $rmp = parse_random_map_params $exit->msg; |
|
|
250 | |
|
|
251 | if ($exit->map) { |
|
|
252 | $rmp->{region} = $exit->map->region ? $exit->map->region->name : undef; |
|
|
253 | $rmp->{origin_map} = $exit->map->path; |
|
|
254 | $rmp->{origin_x} = $exit->x; |
|
|
255 | $rmp->{origin_y} = $exit->y; |
|
|
256 | } |
|
|
257 | |
|
|
258 | $rmp->{random_seed} = $exit->random_seed; |
|
|
259 | |
|
|
260 | $exit->slaying ("?random/" . cf::to_json $rmp); |
|
|
261 | $exit->msg (undef); |
198 | } |
262 | } |
199 | |
263 | |
200 | # and all this just because we cannot iterate over |
264 | # and all this just because we cannot iterate over |
201 | # all maps in C++... |
265 | # all maps in C++... |
202 | sub cf::map::change_all_map_light { |
266 | sub cf::map::change_all_map_light { |
… | |
… | |
224 | $map->{load_path} = $path; |
288 | $map->{load_path} = $path; |
225 | |
289 | |
226 | $map |
290 | $map |
227 | } |
291 | } |
228 | |
292 | |
229 | sub cf::map::find_map_nb { |
293 | sub cf::map::find_map { |
230 | my ($path, $origin) = @_; |
294 | my ($path, $origin) = @_; |
231 | |
295 | |
232 | warn "find_map_nb<$path,$origin>\n";#d# |
296 | warn "find_map<$path,$origin>\n";#d# |
233 | |
297 | |
234 | $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; |
298 | $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; |
235 | my $key = $path->as_string; |
299 | my $key = $path->as_string; |
236 | |
300 | |
237 | $cf::MAP{$key} || do { |
301 | $cf::MAP{$key} || do { |
238 | # do it the slow way |
302 | # do it the slow way |
239 | my $map = try_load_header $path->save_path; |
303 | my $map = try_load_header $path->save_path; |
240 | |
304 | |
241 | if (!$map) { |
305 | if (!$map) { |
|
|
306 | if (my $rmp = $path->random_map_params) { |
|
|
307 | $map = generate_random_map $key, $rmp; |
|
|
308 | } else { |
242 | $map = try_load_header $path->load_path |
309 | $map = try_load_header $path->load_path; |
|
|
310 | } |
|
|
311 | |
243 | or return; |
312 | $map or return; |
|
|
313 | |
|
|
314 | $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout; |
|
|
315 | $map->instantiate; |
|
|
316 | |
|
|
317 | # per-player maps become, after loading, normal maps |
|
|
318 | $map->per_player (0) if $path->{user_rel}; |
244 | } |
319 | } |
245 | |
320 | |
246 | $map or return; |
|
|
247 | |
|
|
248 | $map->instantiate; |
|
|
249 | $map->path ($key); |
321 | $map->path ($key); |
250 | $map->{path} = $path; |
322 | $map->{path} = $path; |
251 | |
323 | |
252 | $map->per_player (0) if $path->{user_rel}; |
|
|
253 | |
|
|
254 | $map->reset if $map->should_reset; |
324 | $map->reset if $map->should_reset; |
255 | |
325 | |
256 | $cf::MAP{$key} = $map |
326 | $cf::MAP{$key} = $map |
257 | } |
327 | } |
258 | } |
328 | } |
259 | |
329 | |
260 | sub cf::map::find_map { |
|
|
261 | my ($path, $origin) = @_; |
|
|
262 | |
|
|
263 | $path = new cf::path $path, $origin && $origin->path; |
|
|
264 | my $key = $path->as_string; |
|
|
265 | |
|
|
266 | warn "find_map<$path,$origin>\n";#d# |
|
|
267 | |
|
|
268 | $cf::MAP{$key} || sync_job { |
|
|
269 | cf::map::find_map_nb $path; |
|
|
270 | } |
|
|
271 | } |
|
|
272 | |
|
|
273 | sub cf::map::do_load_nb { |
330 | sub cf::map::load { |
274 | my ($self) = @_; |
331 | my ($self) = @_; |
275 | |
332 | |
276 | return 0 unless $self->in_memory == cf::MAP_SWAPPED; |
333 | return if $self->in_memory != cf::MAP_SWAPPED; |
277 | |
334 | |
278 | $self->in_memory (cf::MAP_LOADING); |
335 | $self->in_memory (cf::MAP_LOADING); |
279 | |
336 | |
280 | my $path = $self->{path}; |
337 | my $path = $self->{path}; |
281 | |
338 | |
… | |
… | |
291 | } |
348 | } |
292 | } |
349 | } |
293 | |
350 | |
294 | # now do the right thing for maps |
351 | # now do the right thing for maps |
295 | $self->link_multipart_objects; |
352 | $self->link_multipart_objects; |
|
|
353 | |
|
|
354 | unless ($self->{path}->is_style_map) { |
296 | $self->fix_auto_apply; |
355 | $self->fix_auto_apply; |
297 | $self->decay_objects; |
356 | $self->decay_objects; |
298 | $self->update_buttons; |
357 | $self->update_buttons; |
299 | $self->set_darkness_map; |
358 | $self->set_darkness_map; |
300 | $self->difficulty ($self->estimate_difficulty) |
359 | $self->difficulty ($self->estimate_difficulty) |
301 | unless $self->difficulty; |
360 | unless $self->difficulty; |
|
|
361 | $self->activate; |
|
|
362 | } |
302 | |
363 | |
303 | $self->in_memory (cf::MAP_IN_MEMORY); |
364 | $self->in_memory (cf::MAP_IN_MEMORY); |
304 | } |
365 | } |
305 | |
366 | |
306 | sub cf::map::do_load { |
367 | sub cf::map::load_map_sync { |
307 | my ($self) = @_; |
368 | my ($path, $origin) = @_; |
308 | |
369 | |
309 | warn "do_load<$self>\n";#d# |
370 | warn "load_map_sync<$path, $origin>\n";#d# |
310 | |
371 | |
|
|
372 | cf::abort if $path =~ /CVS/;#d# |
|
|
373 | |
311 | sync_job { |
374 | cf::sync_job { |
312 | cf::map::do_load_nb $self; |
375 | my $map = cf::map::find_map $path, $origin |
|
|
376 | or return; |
|
|
377 | $map->load; |
|
|
378 | $map |
313 | }; |
379 | } |
314 | } |
380 | } |
315 | |
381 | |
316 | sub cf::map::save { |
382 | sub cf::map::save { |
317 | my ($self) = @_; |
383 | my ($self) = @_; |
318 | |
384 | |
319 | warn "saving map ", $self->path; |
|
|
320 | |
|
|
321 | my $save = $self->{path}->save_path; utf8::encode $save; |
385 | my $save = $self->{path}->save_path; utf8::encode $save; |
322 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
386 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
|
|
387 | |
|
|
388 | $self->{last_save} = $cf::RUNTIME; |
|
|
389 | |
|
|
390 | return unless $self->dirty; |
|
|
391 | |
|
|
392 | $self->{load_path} = $save; |
|
|
393 | |
|
|
394 | return if $self->{path}->is_style_map; |
|
|
395 | |
|
|
396 | warn "saving map ", $self->path; |
323 | |
397 | |
324 | if ($uniq) { |
398 | if ($uniq) { |
325 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
399 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
326 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
400 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
327 | } else { |
401 | } else { |
328 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
402 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
329 | } |
403 | } |
330 | |
|
|
331 | $self->{load_path} = $save; |
|
|
332 | $self->{last_save} = $cf::RUNTIME; |
|
|
333 | } |
404 | } |
334 | |
405 | |
335 | sub cf::map::swap_out { |
406 | sub cf::map::swap_out { |
336 | my ($self) = @_; |
407 | my ($self) = @_; |
337 | |
408 | |
|
|
409 | return if $self->players; |
338 | $self->save if $self->in_memory == cf::MAP_IN_MEMORY; |
410 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
|
|
411 | |
|
|
412 | $self->save; |
339 | $self->clear; |
413 | $self->clear; |
340 | $self->in_memory (cf::MAP_SWAPPED); |
414 | $self->in_memory (cf::MAP_SWAPPED); |
341 | } |
415 | } |
342 | |
416 | |
343 | sub cf::map::should_reset { |
417 | sub cf::map::should_reset { |
344 | my ($map) = @_; |
418 | my ($map) = @_; |
345 | |
419 | |
|
|
420 | return;#d# |
346 | # TODO: safety, remove and allow resettable per-player maps |
421 | # TODO: safety, remove and allow resettable per-player maps |
347 | return if $map->{path}{user_rel};#d# |
422 | return if $map->{path}{user_rel};#d# |
348 | return if $map->per_player; |
|
|
349 | return unless $map->reset_timeout; |
423 | return unless $map->reset_timeout; |
350 | |
424 | |
351 | my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; |
425 | my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access; |
352 | |
426 | |
353 | $time + $map->reset_timeout < $cf::RUNTIME |
427 | $time + $map->reset_timeout < $cf::RUNTIME |
354 | } |
428 | } |
355 | |
429 | |
356 | sub cf::map::reset { |
430 | sub cf::map::reset { |
… | |
… | |
358 | |
432 | |
359 | return if $self->players; |
433 | return if $self->players; |
360 | return if $self->{path}{user_rel};#d# |
434 | return if $self->{path}{user_rel};#d# |
361 | |
435 | |
362 | warn "resetting map ", $self->path;#d# |
436 | warn "resetting map ", $self->path;#d# |
|
|
437 | return;#d# |
363 | |
438 | |
364 | utf8::encode (my $save = $self->{path}->save_path); |
439 | utf8::encode (my $save = $self->{path}->save_path); |
365 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
440 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
366 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
441 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
367 | |
442 | |
… | |
… | |
371 | } |
446 | } |
372 | |
447 | |
373 | sub cf::object::player::enter_exit { |
448 | sub cf::object::player::enter_exit { |
374 | my ($ob, $exit) = @_; |
449 | my ($ob, $exit) = @_; |
375 | |
450 | |
376 | # if at login, move to interim map immediately |
451 | return unless $ob->type == cf::PLAYER; |
377 | unless ($exit) { |
452 | |
378 | # used on login only |
453 | my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); |
|
|
454 | |
379 | $ob->enter_map ($LINK_MAP, 0, 0); |
455 | $ob->enter_map ($LINK_MAP, 20, 20); |
380 | } |
456 | $ob->deactivate_recursive; |
381 | |
457 | |
382 | #TODO: do this in the background, freeze the player if required |
458 | (Coro::async { |
383 | sync_job { |
|
|
384 | my ($map, $x, $y); |
459 | my ($map, $x, $y); |
385 | |
|
|
386 | unless ($exit) { |
460 | unless (eval { |
387 | # used on login only(?) |
461 | |
388 | $map = cf::map::find_map_nb $ob->contr->maplevel; |
462 | prepare_random_map $exit |
389 | ($x, $y) = ($ob->x, $ob->y); |
463 | if $exit->slaying eq "/!"; |
390 | } else { |
464 | |
391 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
465 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
392 | |
466 | |
393 | $map = cf::map::find_map_nb $path->as_string; |
467 | $map = cf::map::find_map $path->as_string; |
394 | $map = $map->customise_for ($ob) if $map; |
468 | $map = $map->customise_for ($ob) if $map; |
395 | ($x, $y) = ($exit->stats->hp, $exit->stats->sp); |
469 | ($x, $y) = ($exit->stats->hp, $exit->stats->sp); |
|
|
470 | |
|
|
471 | unless ($map) { |
|
|
472 | $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
473 | |
|
|
474 | # restore original map position |
|
|
475 | ($map, $x, $y) = ($oldmap, $oldx, $oldy); |
|
|
476 | |
|
|
477 | unless ($map) { |
|
|
478 | $map = cf::map::find_map $emergency_position->[0] |
|
|
479 | or die "FATAL: cannot load emergency map\n"; |
|
|
480 | $x = $emergency_position->[1]; |
|
|
481 | $y = $emergency_position->[2]; |
|
|
482 | } |
|
|
483 | } |
|
|
484 | |
|
|
485 | # use -1, -1 as default coordinates, not 0, 0 |
|
|
486 | ($x, $y) = ($map->enter_x, $map->enter_y) |
|
|
487 | if $x <=0 && $y <= 0; |
|
|
488 | |
|
|
489 | warn "entering ", $map->path, " at ($x, $y)\n";#d# |
|
|
490 | $map->load; |
|
|
491 | 1; |
|
|
492 | }) { |
|
|
493 | ($map, $x, $y) = ($oldmap, $oldx, $oldy); |
|
|
494 | |
|
|
495 | $ob->message ("Something went wrong within the server. " |
|
|
496 | . "I'll try to bring you back to the map you were before. " |
|
|
497 | . "Please report this to the dungeon master", |
|
|
498 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
499 | |
|
|
500 | warn "ERROR in enter_exit: $@"; |
396 | } |
501 | } |
397 | |
502 | $ob->activate_recursive; |
398 | unless ($map) { |
|
|
399 | $map = cf::map::find_map_nb $emergency_position->[0] |
|
|
400 | or die "FATAL: cannot load emergency map\n"; |
|
|
401 | $x = $emergency_position->[1]; |
|
|
402 | $y = $emergency_position->[2]; |
|
|
403 | } |
|
|
404 | |
|
|
405 | if ($map) { |
|
|
406 | warn "entering ", $map->path, " at ($x, $y)\n";#d# |
|
|
407 | $map->do_load_nb; |
|
|
408 | $ob->enter_map ($map, $x, $y); |
503 | $ob->enter_map ($map, $x, $y); |
409 | } else { |
504 | })->prio (1); |
410 | $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | } |
505 | } |
414 | |
506 | |
415 | sub cf::map::customise_for { |
507 | sub cf::map::customise_for { |
416 | my ($map, $ob) = @_; |
508 | my ($map, $ob) = @_; |
417 | |
509 | |
418 | if ($map->per_player) { |
510 | if ($map->per_player) { |
419 | return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; |
511 | return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; |
420 | } |
512 | } |
421 | |
513 | |
422 | $map |
514 | $map |
423 | } |
515 | } |
424 | |
516 | |
425 | sub cf::map::emergency_save { |
517 | sub cf::map::emergency_save { |
426 | local $cf::FREEZE = 1; |
518 | local $cf::FREEZE = 1; |
427 | |
519 | |
428 | warn "enter emergency map save\n"; |
520 | warn "enter emergency map save\n"; |
429 | |
521 | |
430 | my $saver = async { |
522 | cf::sync_job { |
431 | warn "begin emergency map save\n"; |
523 | warn "begin emergency map save\n"; |
432 | $_->save for values %cf::MAP; |
524 | $_->save for values %cf::MAP; |
433 | }; |
525 | }; |
434 | $saver->prio (Coro::PRIO_MAX); |
|
|
435 | $saver->join; |
|
|
436 | |
526 | |
437 | warn "emergency map save drain\n"; |
|
|
438 | Event::one_event while IO::AIO::nreqs; |
|
|
439 | warn "end emergency map save\n"; |
527 | warn "end emergency map save\n"; |
440 | } |
528 | } |
441 | |
529 | |