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} |
110 | } |
133 | } |
111 | } |
134 | } |
112 | |
135 | |
113 | sub write_runtime { |
136 | sub write_runtime { |
114 | my $runtime = cf::localdir . "/runtime"; |
137 | my $runtime = cf::localdir . "/runtime"; |
… | |
… | |
195 | } |
218 | } |
196 | |
219 | |
197 | sub parse_random_map_params { |
220 | sub parse_random_map_params { |
198 | my ($spec) = @_; |
221 | my ($spec) = @_; |
199 | |
222 | |
200 | my $rmp; |
223 | my $rmp = { # defaults |
|
|
224 | xsize => 10, |
|
|
225 | ysize => 10, |
|
|
226 | }; |
201 | |
227 | |
202 | for (split /\n/, $spec) { |
228 | for (split /\n/, $spec) { |
203 | my ($k, $v) = split /\s+/, $_, 2; |
229 | my ($k, $v) = split /\s+/, $_, 2; |
204 | |
230 | |
205 | $rmp->{lc $k} = $v if (length $k) && (length $v); |
231 | $rmp->{lc $k} = $v if (length $k) && (length $v); |
… | |
… | |
209 | } |
235 | } |
210 | |
236 | |
211 | sub prepare_random_map { |
237 | sub prepare_random_map { |
212 | my ($exit) = @_; |
238 | my ($exit) = @_; |
213 | |
239 | |
|
|
240 | # all this does is basically rpelace the /! path by |
|
|
241 | # a new random map path (?random/...) with a seed |
|
|
242 | # that depends on the exit object |
|
|
243 | |
214 | my $rmp = parse_random_map_params $exit->msg; |
244 | my $rmp = parse_random_map_params $exit->msg; |
215 | use Data::Dumper; |
|
|
216 | die Dumper $rmp; |
|
|
217 | } |
|
|
218 | |
245 | |
219 | sub sync_job(&) { |
246 | if ($exit->map) { |
220 | my ($job) = @_; |
247 | $rmp->{region} = $exit->map->region ? $exit->map->region->name : undef; |
221 | |
248 | $rmp->{origin_map} = $exit->map->path; |
222 | my $done; |
249 | $rmp->{origin_x} = $exit->x; |
223 | my @res; |
250 | $rmp->{origin_y} = $exit->y; |
224 | |
|
|
225 | Carp::confess "nested sync_job" if $cf::FREEZE; |
|
|
226 | |
|
|
227 | local $cf::FREEZE = 1; |
|
|
228 | |
|
|
229 | (Coro::async { |
|
|
230 | @res = eval { $job->() }; |
|
|
231 | warn $@ if $@; |
|
|
232 | $done = 1; |
|
|
233 | })->prio (Coro::PRIO_MAX); |
|
|
234 | |
|
|
235 | while (!$done) { |
|
|
236 | Coro::cede_notself; |
|
|
237 | Event::one_event unless Coro::nready; |
|
|
238 | } |
251 | } |
239 | |
252 | |
240 | warn "sync<@res>\n";#d# |
253 | $rmp->{random_seed} = $exit->random_seed; |
241 | wantarray ? @res : $res[0] |
254 | |
|
|
255 | $exit->slaying ("?random/" . cf::to_json $rmp); |
|
|
256 | $exit->msg (undef); |
242 | } |
257 | } |
243 | |
258 | |
244 | # and all this just because we cannot iterate over |
259 | # and all this just because we cannot iterate over |
245 | # all maps in C++... |
260 | # all maps in C++... |
246 | sub cf::map::change_all_map_light { |
261 | sub cf::map::change_all_map_light { |
… | |
… | |
281 | $cf::MAP{$key} || do { |
296 | $cf::MAP{$key} || do { |
282 | # do it the slow way |
297 | # do it the slow way |
283 | my $map = try_load_header $path->save_path; |
298 | my $map = try_load_header $path->save_path; |
284 | |
299 | |
285 | if (!$map) { |
300 | if (!$map) { |
|
|
301 | if (my $rmp = $path->random_map_params) { |
|
|
302 | $map = generate_random_map $key, $rmp; |
|
|
303 | } else { |
286 | $map = try_load_header $path->load_path |
304 | $map = try_load_header $path->load_path; |
|
|
305 | } |
|
|
306 | |
287 | or return; |
307 | $map or return; |
288 | |
308 | |
|
|
309 | $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout; |
289 | $map->instantiate; |
310 | $map->instantiate; |
290 | |
311 | |
291 | # per-player maps become, after loading, normal maps |
312 | # per-player maps become, after loading, normal maps |
292 | $map->per_player (0) if $path->{user_rel}; |
313 | $map->per_player (0) if $path->{user_rel}; |
293 | } |
314 | } |
… | |
… | |
338 | sub cf::map::load_map_sync { |
359 | sub cf::map::load_map_sync { |
339 | my ($path, $origin) = @_; |
360 | my ($path, $origin) = @_; |
340 | |
361 | |
341 | warn "load_map_sync<$path, $origin>\n";#d# |
362 | warn "load_map_sync<$path, $origin>\n";#d# |
342 | |
363 | |
|
|
364 | cf::abort if $path =~ /CVS/;#d# |
|
|
365 | |
343 | sync_job { |
366 | cf::sync_job { |
344 | my $map = cf::map::find_map $path, $origin |
367 | my $map = cf::map::find_map $path, $origin |
345 | or return; |
368 | or return; |
346 | $map->load; |
369 | $map->load; |
347 | $map |
370 | $map |
348 | } |
371 | } |
349 | } |
372 | } |
350 | |
373 | |
351 | sub cf::map::save { |
374 | sub cf::map::save { |
352 | my ($self) = @_; |
375 | my ($self) = @_; |
353 | |
376 | |
354 | warn "saving map ", $self->path; |
|
|
355 | |
|
|
356 | my $save = $self->{path}->save_path; utf8::encode $save; |
377 | my $save = $self->{path}->save_path; utf8::encode $save; |
357 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
378 | my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; |
|
|
379 | |
|
|
380 | $self->{last_save} = $cf::RUNTIME; |
|
|
381 | |
|
|
382 | return unless $self->dirty; |
|
|
383 | |
|
|
384 | $self->{load_path} = $save; |
|
|
385 | |
|
|
386 | return if $self->{path}{path} =~ /^\/styles\//; |
|
|
387 | |
|
|
388 | warn "saving map ", $self->path; |
358 | |
389 | |
359 | if ($uniq) { |
390 | if ($uniq) { |
360 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
391 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
361 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
392 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
362 | } else { |
393 | } else { |
363 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
394 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
364 | } |
395 | } |
365 | |
|
|
366 | $self->{load_path} = $save; |
|
|
367 | $self->{last_save} = $cf::RUNTIME; |
|
|
368 | } |
396 | } |
369 | |
397 | |
370 | sub cf::map::swap_out { |
398 | sub cf::map::swap_out { |
371 | my ($self) = @_; |
399 | my ($self) = @_; |
372 | |
400 | |
… | |
… | |
379 | } |
407 | } |
380 | |
408 | |
381 | sub cf::map::should_reset { |
409 | sub cf::map::should_reset { |
382 | my ($map) = @_; |
410 | my ($map) = @_; |
383 | |
411 | |
|
|
412 | return;#d# |
384 | # TODO: safety, remove and allow resettable per-player maps |
413 | # TODO: safety, remove and allow resettable per-player maps |
385 | return if $map->{path}{user_rel};#d# |
414 | return if $map->{path}{user_rel};#d# |
386 | return unless $map->reset_timeout; |
415 | return unless $map->reset_timeout; |
387 | |
416 | |
388 | my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; |
417 | my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access; |
389 | |
418 | |
390 | $time + $map->reset_timeout < $cf::RUNTIME |
419 | $time + $map->reset_timeout < $cf::RUNTIME |
391 | } |
420 | } |
392 | |
421 | |
393 | sub cf::map::reset { |
422 | sub cf::map::reset { |
… | |
… | |
395 | |
424 | |
396 | return if $self->players; |
425 | return if $self->players; |
397 | return if $self->{path}{user_rel};#d# |
426 | return if $self->{path}{user_rel};#d# |
398 | |
427 | |
399 | warn "resetting map ", $self->path;#d# |
428 | warn "resetting map ", $self->path;#d# |
|
|
429 | return;#d# |
400 | |
430 | |
401 | utf8::encode (my $save = $self->{path}->save_path); |
431 | utf8::encode (my $save = $self->{path}->save_path); |
402 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
432 | aioreq_pri 3; IO::AIO::aio_unlink $save; |
403 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
433 | aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; |
404 | |
434 | |
… | |
… | |
412 | |
442 | |
413 | return unless $ob->type == cf::PLAYER; |
443 | return unless $ob->type == cf::PLAYER; |
414 | |
444 | |
415 | my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); |
445 | my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); |
416 | |
446 | |
417 | #TODO: do this in the background, freeze the player if required |
447 | $ob->enter_map ($LINK_MAP, 20, 20); |
418 | sync_job { |
448 | $ob->deactivate_recursive; |
|
|
449 | |
|
|
450 | (Coro::async { |
419 | my ($map, $x, $y); |
451 | my ($map, $x, $y); |
|
|
452 | unless (eval { |
420 | |
453 | |
421 | if ($exit->slaying eq "/!") { |
|
|
422 | prepare_random_map $exit; |
454 | prepare_random_map $exit |
|
|
455 | if $exit->slaying eq "/!"; |
|
|
456 | |
|
|
457 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
|
|
458 | |
|
|
459 | $map = cf::map::find_map $path->as_string; |
|
|
460 | $map = $map->customise_for ($ob) if $map; |
|
|
461 | ($x, $y) = ($exit->stats->hp, $exit->stats->sp); |
|
|
462 | |
|
|
463 | unless ($map) { |
|
|
464 | $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
465 | |
|
|
466 | # restore original map position |
|
|
467 | ($map, $x, $y) = ($oldmap, $oldx, $oldy); |
|
|
468 | |
|
|
469 | unless ($map) { |
|
|
470 | $map = cf::map::find_map $emergency_position->[0] |
|
|
471 | or die "FATAL: cannot load emergency map\n"; |
|
|
472 | $x = $emergency_position->[1]; |
|
|
473 | $y = $emergency_position->[2]; |
|
|
474 | } |
|
|
475 | } |
|
|
476 | |
|
|
477 | # use -1, -1 as default coordinates, not 0, 0 |
|
|
478 | ($x, $y) = ($map->enter_x, $map->enter_y) |
|
|
479 | if $x <=0 && $y <= 0; |
|
|
480 | |
|
|
481 | warn "entering ", $map->path, " at ($x, $y)\n";#d# |
|
|
482 | $map->load; |
|
|
483 | 1; |
|
|
484 | }) { |
|
|
485 | ($map, $x, $y) = ($oldmap, $oldx, $oldy); |
|
|
486 | |
|
|
487 | $ob->message ("Something went wrong within the server. " |
|
|
488 | . "I'll try to bring you back to the map you were before. " |
|
|
489 | . "Please report this to the dungeon master", |
|
|
490 | cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
491 | |
|
|
492 | warn "ERROR in enter_exit: $@"; |
423 | } |
493 | } |
424 | |
494 | $ob->activate_recursive; |
425 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
|
|
426 | |
|
|
427 | $map = cf::map::find_map $path->as_string; |
|
|
428 | $map = $map->customise_for ($ob) if $map; |
|
|
429 | ($x, $y) = ($exit->stats->hp, $exit->stats->sp); |
|
|
430 | |
|
|
431 | unless ($map) { |
|
|
432 | $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
|
|
433 | |
|
|
434 | # restore original map position |
|
|
435 | ($map, $x, $y) = ($oldmap, $oldx, $oldy); |
|
|
436 | |
|
|
437 | unless ($map) { |
|
|
438 | $map = cf::map::find_map $emergency_position->[0] |
|
|
439 | or die "FATAL: cannot load emergency map\n"; |
|
|
440 | $x = $emergency_position->[1]; |
|
|
441 | $y = $emergency_position->[2]; |
|
|
442 | } |
|
|
443 | } |
|
|
444 | |
|
|
445 | # use -1, -1 as default coordinates, not 0, 0 |
|
|
446 | ($x, $y) = ($map->enter_x, $map->enter_y) |
|
|
447 | if $x <=0 && $y <= 0; |
|
|
448 | |
|
|
449 | warn "entering ", $map->path, " at ($x, $y)\n";#d# |
|
|
450 | $map->load; |
|
|
451 | $ob->enter_map ($map, $x, $y); |
495 | $ob->enter_map ($map, $x, $y); |
452 | } |
496 | })->prio (1); |
453 | } |
497 | } |
454 | |
498 | |
455 | sub cf::map::customise_for { |
499 | sub cf::map::customise_for { |
456 | my ($map, $ob) = @_; |
500 | my ($map, $ob) = @_; |
457 | |
501 | |
… | |
… | |
465 | sub cf::map::emergency_save { |
509 | sub cf::map::emergency_save { |
466 | local $cf::FREEZE = 1; |
510 | local $cf::FREEZE = 1; |
467 | |
511 | |
468 | warn "enter emergency map save\n"; |
512 | warn "enter emergency map save\n"; |
469 | |
513 | |
470 | my $saver = async { |
514 | cf::sync_job { |
471 | warn "begin emergency map save\n"; |
515 | warn "begin emergency map save\n"; |
472 | $_->save for values %cf::MAP; |
516 | $_->save for values %cf::MAP; |
473 | }; |
517 | }; |
474 | $saver->prio (Coro::PRIO_MAX); |
|
|
475 | $saver->join; |
|
|
476 | |
518 | |
477 | warn "emergency map save drain\n"; |
|
|
478 | Event::one_event while IO::AIO::nreqs; |
|
|
479 | warn "end emergency map save\n"; |
519 | warn "end emergency map save\n"; |
480 | } |
520 | } |
481 | |
521 | |