ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/00_map_handling.ext
(Generate patch)

Comparing deliantra/server/ext/00_map_handling.ext (file contents):
Revision 1.8 by root, Sun Dec 31 10:28:36 2006 UTC vs.
Revision 1.10 by root, Sun Dec 31 18:10:40 2006 UTC

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
5use Digest::MD5;
5use Fcntl; 6use Fcntl;
6 7
7use Coro; 8use Coro;
8use Coro::AIO; 9use Coro::AIO;
9 10
14our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs 15our $SCHEDULE_INTERVAL = 8; # time the map scheduler sleeps between runs
15our $SAVE_TIMEOUT = 60; # save maps every n seconds 16our $SAVE_TIMEOUT = 60; # save maps every n seconds
16our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD 17our $SAVE_INTERVAL = 0.4; # save at max. one map every $SAVE_HOLD
17our $MAX_RESET = 7200; 18our $MAX_RESET = 7200;
18 19
20our $RANDOM_MAPS = cf::localdir . "/random";
21mkdir $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
113sub write_runtime { 141sub write_runtime {
114 my $runtime = cf::localdir . "/runtime"; 142 my $runtime = cf::localdir . "/runtime";
195} 223}
196 224
197sub parse_random_map_params { 225sub parse_random_map_params {
198 my ($spec) = @_; 226 my ($spec) = @_;
199 227
200 my $rmp; 228 my $rmp = { # defaults
229 xsize => 10,
230 ysize => 10,
231 };
201 232
202 for (split /\n/, $spec) { 233 for (split /\n/, $spec) {
203 my ($k, $v) = split /\s+/, $_, 2; 234 my ($k, $v) = split /\s+/, $_, 2;
204 235
205 $rmp->{lc $k} = $v if (length $k) && (length $v); 236 $rmp->{lc $k} = $v if (length $k) && (length $v);
209} 240}
210 241
211sub prepare_random_map { 242sub prepare_random_map {
212 my ($exit) = @_; 243 my ($exit) = @_;
213 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
214 my $rmp = parse_random_map_params $exit->msg; 249 my $rmp = parse_random_map_params $exit->msg;
215 use Data::Dumper;
216 die Dumper $rmp;
217}
218 250
219sub sync_job(&) { 251 if ($exit->map) {
220 my ($job) = @_; 252 $rmp->{region} = $exit->map->region ? $exit->map->region->name : undef;
221 253 $rmp->{origin_map} = $exit->map->path;
222 my $done; 254 $rmp->{origin_x} = $exit->x;
223 my @res; 255 $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 } 256 }
239 257
240 warn "sync<@res>\n";#d# 258 $rmp->{random_seed} = $exit->random_seed;
241 wantarray ? @res : $res[0] 259
260 $exit->slaying ("?random/" . cf::to_json $rmp);
261 $exit->msg (undef);
242} 262}
243 263
244# and all this just because we cannot iterate over 264# and all this just because we cannot iterate over
245# all maps in C++... 265# all maps in C++...
246sub cf::map::change_all_map_light { 266sub cf::map::change_all_map_light {
281 $cf::MAP{$key} || do { 301 $cf::MAP{$key} || do {
282 # do it the slow way 302 # do it the slow way
283 my $map = try_load_header $path->save_path; 303 my $map = try_load_header $path->save_path;
284 304
285 if (!$map) { 305 if (!$map) {
306 if (my $rmp = $path->random_map_params) {
307 $map = generate_random_map $key, $rmp;
308 } else {
286 $map = try_load_header $path->load_path 309 $map = try_load_header $path->load_path;
310 }
311
287 or return; 312 $map or return;
288 313
314 $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout;
289 $map->instantiate; 315 $map->instantiate;
290 316
291 # per-player maps become, after loading, normal maps 317 # per-player maps become, after loading, normal maps
292 $map->per_player (0) if $path->{user_rel}; 318 $map->per_player (0) if $path->{user_rel};
293 } 319 }
322 } 348 }
323 } 349 }
324 350
325 # now do the right thing for maps 351 # now do the right thing for maps
326 $self->link_multipart_objects; 352 $self->link_multipart_objects;
353
354 unless ($self->{path}->is_style_map) {
327 $self->fix_auto_apply; 355 $self->fix_auto_apply;
328 $self->decay_objects; 356 $self->decay_objects;
329 $self->update_buttons; 357 $self->update_buttons;
330 $self->set_darkness_map; 358 $self->set_darkness_map;
331 $self->difficulty ($self->estimate_difficulty) 359 $self->difficulty ($self->estimate_difficulty)
332 unless $self->difficulty; 360 unless $self->difficulty;
333 $self->activate; 361 $self->activate;
362 }
334 363
335 $self->in_memory (cf::MAP_IN_MEMORY); 364 $self->in_memory (cf::MAP_IN_MEMORY);
336} 365}
337 366
338sub cf::map::load_map_sync { 367sub cf::map::load_map_sync {
339 my ($path, $origin) = @_; 368 my ($path, $origin) = @_;
340 369
341 warn "load_map_sync<$path, $origin>\n";#d# 370 warn "load_map_sync<$path, $origin>\n";#d#
342 371
372 cf::abort if $path =~ /CVS/;#d#
373
343 sync_job { 374 cf::sync_job {
344 my $map = cf::map::find_map $path, $origin 375 my $map = cf::map::find_map $path, $origin
345 or return; 376 or return;
346 $map->load; 377 $map->load;
347 $map 378 $map
348 } 379 }
349} 380}
350 381
351sub cf::map::save { 382sub cf::map::save {
352 my ($self) = @_; 383 my ($self) = @_;
353 384
354 warn "saving map ", $self->path;
355
356 my $save = $self->{path}->save_path; utf8::encode $save; 385 my $save = $self->{path}->save_path; utf8::encode $save;
357 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;
358 397
359 if ($uniq) { 398 if ($uniq) {
360 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 399 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
361 $self->save_objects ($uniq, cf::IO_UNIQUES); 400 $self->save_objects ($uniq, cf::IO_UNIQUES);
362 } else { 401 } else {
363 $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);
364 } 403 }
365
366 $self->{load_path} = $save;
367 $self->{last_save} = $cf::RUNTIME;
368} 404}
369 405
370sub cf::map::swap_out { 406sub cf::map::swap_out {
371 my ($self) = @_; 407 my ($self) = @_;
372 408
379} 415}
380 416
381sub cf::map::should_reset { 417sub cf::map::should_reset {
382 my ($map) = @_; 418 my ($map) = @_;
383 419
420 return;#d#
384 # TODO: safety, remove and allow resettable per-player maps 421 # TODO: safety, remove and allow resettable per-player maps
385 return if $map->{path}{user_rel};#d# 422 return if $map->{path}{user_rel};#d#
386 return unless $map->reset_timeout; 423 return unless $map->reset_timeout;
387 424
388 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 425 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
389 426
390 $time + $map->reset_timeout < $cf::RUNTIME 427 $time + $map->reset_timeout < $cf::RUNTIME
391} 428}
392 429
393sub cf::map::reset { 430sub cf::map::reset {
395 432
396 return if $self->players; 433 return if $self->players;
397 return if $self->{path}{user_rel};#d# 434 return if $self->{path}{user_rel};#d#
398 435
399 warn "resetting map ", $self->path;#d# 436 warn "resetting map ", $self->path;#d#
437 return;#d#
400 438
401 utf8::encode (my $save = $self->{path}->save_path); 439 utf8::encode (my $save = $self->{path}->save_path);
402 aioreq_pri 3; IO::AIO::aio_unlink $save; 440 aioreq_pri 3; IO::AIO::aio_unlink $save;
403 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; 441 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
404 442
412 450
413 return unless $ob->type == cf::PLAYER; 451 return unless $ob->type == cf::PLAYER;
414 452
415 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); 453 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
416 454
417 #TODO: do this in the background, freeze the player if required 455 $ob->enter_map ($LINK_MAP, 20, 20);
418 sync_job { 456 $ob->deactivate_recursive;
457
458 (Coro::async {
419 my ($map, $x, $y); 459 my ($map, $x, $y);
460 unless (eval {
420 461
421 if ($exit->slaying eq "/!") {
422 prepare_random_map $exit; 462 prepare_random_map $exit
463 if $exit->slaying eq "/!";
464
465 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
466
467 $map = cf::map::find_map $path->as_string;
468 $map = $map->customise_for ($ob) if $map;
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: $@";
423 } 501 }
424 502 $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); 503 $ob->enter_map ($map, $x, $y);
452 } 504 })->prio (1);
453} 505}
454 506
455sub cf::map::customise_for { 507sub cf::map::customise_for {
456 my ($map, $ob) = @_; 508 my ($map, $ob) = @_;
457 509
465sub cf::map::emergency_save { 517sub cf::map::emergency_save {
466 local $cf::FREEZE = 1; 518 local $cf::FREEZE = 1;
467 519
468 warn "enter emergency map save\n"; 520 warn "enter emergency map save\n";
469 521
470 my $saver = async { 522 cf::sync_job {
471 warn "begin emergency map save\n"; 523 warn "begin emergency map save\n";
472 $_->save for values %cf::MAP; 524 $_->save for values %cf::MAP;
473 }; 525 };
474 $saver->prio (Coro::PRIO_MAX);
475 $saver->join;
476 526
477 warn "emergency map save drain\n";
478 Event::one_event while IO::AIO::nreqs;
479 warn "end emergency map save\n"; 527 warn "end emergency map save\n";
480} 528}
481 529

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines