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.6 by root, Sat Dec 30 20:32:30 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";
138 exit 1; 166 exit 1;
139 } 167 }
140})->prio (Coro::PRIO_MAX); 168})->prio (Coro::PRIO_MAX);
141 169
142our $SCHEDULER = cf::coro { 170our $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: $!";
172 } 201 }
173 } 202 }
174}; 203};
175$SCHEDULER->prio (-2); 204$SCHEDULER->prio (-2);
176 205
177sub sync_job(&) { 206sub 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
225sub parse_random_map_params {
178 my ($job) = @_; 226 my ($spec) = @_;
179 227
180 my $done; 228 my $rmp = { # defaults
181 my @res; 229 xsize => 10,
182 230 ysize => 10,
183 Carp::confess "nested sync_job" if $cf::FREEZE;
184
185 local $cf::FREEZE = 1;
186
187 (Coro::async {
188 @res = eval { $job->() };
189 warn $@ if $@;
190 $done = 1;
191 })->prio (Coro::PRIO_MAX);
192
193 while (!$done) {
194 Coro::cede_notself;
195 Event::one_event unless Coro::nready;
196 } 231 };
197 232
198 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
242sub 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);
199} 262}
200 263
201# and all this just because we cannot iterate over 264# and all this just because we cannot iterate over
202# all maps in C++... 265# all maps in C++...
203sub cf::map::change_all_map_light { 266sub cf::map::change_all_map_light {
225 $map->{load_path} = $path; 288 $map->{load_path} = $path;
226 289
227 $map 290 $map
228} 291}
229 292
230sub cf::map::find_map_nb { 293sub cf::map::find_map {
231 my ($path, $origin) = @_; 294 my ($path, $origin) = @_;
232 295
233 warn "find_map_nb<$path,$origin>\n";#d# 296 warn "find_map<$path,$origin>\n";#d#
234 297
235 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; 298 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
236 my $key = $path->as_string; 299 my $key = $path->as_string;
237 300
238 $cf::MAP{$key} || do { 301 $cf::MAP{$key} || do {
239 # do it the slow way 302 # do it the slow way
240 my $map = try_load_header $path->save_path; 303 my $map = try_load_header $path->save_path;
241 304
242 if (!$map) { 305 if (!$map) {
306 if (my $rmp = $path->random_map_params) {
307 $map = generate_random_map $key, $rmp;
308 } else {
243 $map = try_load_header $path->load_path 309 $map = try_load_header $path->load_path;
310 }
311
244 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};
245 } 319 }
246 320
247 $map or return;
248
249 $map->instantiate;
250 $map->path ($key); 321 $map->path ($key);
251 $map->{path} = $path; 322 $map->{path} = $path;
252 323
253 $map->per_player (0) if $path->{user_rel};
254
255 $map->reset if $map->should_reset; 324 $map->reset if $map->should_reset;
256 325
257 $cf::MAP{$key} = $map 326 $cf::MAP{$key} = $map
258 } 327 }
259} 328}
260 329
261sub cf::map::find_map {
262 my ($path, $origin) = @_;
263
264 $path = new cf::path $path, $origin && $origin->path;
265 my $key = $path->as_string;
266
267 warn "find_map<$path,$origin>\n";#d#
268
269 $cf::MAP{$key} || sync_job {
270 cf::map::find_map_nb $path;
271 }
272}
273
274sub cf::map::do_load_nb { 330sub cf::map::load {
275 my ($self) = @_; 331 my ($self) = @_;
276 332
277 return 0 unless $self->in_memory == cf::MAP_SWAPPED; 333 return if $self->in_memory != cf::MAP_SWAPPED;
278 334
279 $self->in_memory (cf::MAP_LOADING); 335 $self->in_memory (cf::MAP_LOADING);
280 336
281 my $path = $self->{path}; 337 my $path = $self->{path};
282 338
292 } 348 }
293 } 349 }
294 350
295 # now do the right thing for maps 351 # now do the right thing for maps
296 $self->link_multipart_objects; 352 $self->link_multipart_objects;
353
354 unless ($self->{path}->is_style_map) {
297 $self->fix_auto_apply; 355 $self->fix_auto_apply;
298 $self->decay_objects; 356 $self->decay_objects;
299 $self->update_buttons; 357 $self->update_buttons;
300 $self->set_darkness_map; 358 $self->set_darkness_map;
301 $self->difficulty ($self->estimate_difficulty) 359 $self->difficulty ($self->estimate_difficulty)
302 unless $self->difficulty; 360 unless $self->difficulty;
303 $self->activate; 361 $self->activate;
362 }
304 363
305 $self->in_memory (cf::MAP_IN_MEMORY); 364 $self->in_memory (cf::MAP_IN_MEMORY);
306} 365}
307 366
308sub cf::map::do_load { 367sub cf::map::load_map_sync {
309 my ($self) = @_; 368 my ($path, $origin) = @_;
310 369
311 warn "do_load<$self>\n";#d# 370 warn "load_map_sync<$path, $origin>\n";#d#
312 371
372 cf::abort if $path =~ /CVS/;#d#
373
313 sync_job { 374 cf::sync_job {
314 cf::map::do_load_nb $self; 375 my $map = cf::map::find_map $path, $origin
376 or return;
377 $map->load;
378 $map
315 }; 379 }
316} 380}
317 381
318sub cf::map::save { 382sub cf::map::save {
319 my ($self) = @_; 383 my ($self) = @_;
320 384
321 warn "saving map ", $self->path;
322
323 my $save = $self->{path}->save_path; utf8::encode $save; 385 my $save = $self->{path}->save_path; utf8::encode $save;
324 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;
325 397
326 if ($uniq) { 398 if ($uniq) {
327 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 399 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
328 $self->save_objects ($uniq, cf::IO_UNIQUES); 400 $self->save_objects ($uniq, cf::IO_UNIQUES);
329 } else { 401 } else {
330 $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);
331 } 403 }
332
333 $self->{load_path} = $save;
334 $self->{last_save} = $cf::RUNTIME;
335} 404}
336 405
337sub cf::map::swap_out { 406sub cf::map::swap_out {
338 my ($self) = @_; 407 my ($self) = @_;
339 408
340 return if $self->players; 409 return if $self->players;
341
342 $self->save if $self->in_memory == cf::MAP_IN_MEMORY; 410 return if $self->in_memory != cf::MAP_IN_MEMORY;
411
412 $self->save;
343 $self->clear; 413 $self->clear;
344 $self->in_memory (cf::MAP_SWAPPED); 414 $self->in_memory (cf::MAP_SWAPPED);
345} 415}
346 416
347sub cf::map::should_reset { 417sub cf::map::should_reset {
348 my ($map) = @_; 418 my ($map) = @_;
349 419
420 return;#d#
350 # TODO: safety, remove and allow resettable per-player maps 421 # TODO: safety, remove and allow resettable per-player maps
351 return if $map->{path}{user_rel};#d# 422 return if $map->{path}{user_rel};#d#
352 return if $map->per_player;
353 return unless $map->reset_timeout; 423 return unless $map->reset_timeout;
354 424
355 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 425 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
356 426
357 $time + $map->reset_timeout < $cf::RUNTIME 427 $time + $map->reset_timeout < $cf::RUNTIME
358} 428}
359 429
360sub cf::map::reset { 430sub cf::map::reset {
362 432
363 return if $self->players; 433 return if $self->players;
364 return if $self->{path}{user_rel};#d# 434 return if $self->{path}{user_rel};#d#
365 435
366 warn "resetting map ", $self->path;#d# 436 warn "resetting map ", $self->path;#d#
437 return;#d#
367 438
368 utf8::encode (my $save = $self->{path}->save_path); 439 utf8::encode (my $save = $self->{path}->save_path);
369 aioreq_pri 3; IO::AIO::aio_unlink $save; 440 aioreq_pri 3; IO::AIO::aio_unlink $save;
370 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; 441 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
371 442
377sub cf::object::player::enter_exit { 448sub cf::object::player::enter_exit {
378 my ($ob, $exit) = @_; 449 my ($ob, $exit) = @_;
379 450
380 return unless $ob->type == cf::PLAYER; 451 return unless $ob->type == cf::PLAYER;
381 452
382 # if at login, move to interim map immediately
383 unless ($exit) {
384 # used on login only
385 $ob->enter_map ($LINK_MAP, 0, 0);
386 }
387
388 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); 453 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
389 454
390 #TODO: do this in the background, freeze the player if required 455 $ob->enter_map ($LINK_MAP, 20, 20);
391 sync_job { 456 $ob->deactivate_recursive;
457
458 (Coro::async {
392 my ($map, $x, $y); 459 my ($map, $x, $y);
393
394 unless ($exit) { 460 unless (eval {
395 # used on login only(?)
396 $map = cf::map::find_map_nb $ob->contr->maplevel;
397 ($x, $y) = ($ob->x, $ob->y);
398 461
399 unless ($map) { 462 prepare_random_map $exit
400 $map = cf::map::find_map_nb $emergency_position->[0]
401 or die "FATAL: cannot load emergency map\n";
402 $x = $emergency_position->[1];
403 $y = $emergency_position->[2];
404 }
405
406 } elsif ($exit->slaying eq "!") { 463 if $exit->slaying eq "/!";
407 } else { 464
408 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;
409 466
410 $map = cf::map::find_map_nb $path->as_string; 467 $map = cf::map::find_map $path->as_string;
411 $map = $map->customise_for ($ob) if $map; 468 $map = $map->customise_for ($ob) if $map;
412 ($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: $@";
413 } 501 }
414 502 $ob->activate_recursive;
415 unless ($map) {
416 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
417 # restore original map position
418 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
419 }
420
421 warn "entering ", $map->path, " at ($x, $y)\n";#d#
422 $map->do_load_nb;
423 $ob->enter_map ($map, $x, $y); 503 $ob->enter_map ($map, $x, $y);
424 } 504 })->prio (1);
425} 505}
426 506
427sub cf::map::customise_for { 507sub cf::map::customise_for {
428 my ($map, $ob) = @_; 508 my ($map, $ob) = @_;
429 509
430 if ($map->per_player) { 510 if ($map->per_player) {
431 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 511 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
432 } 512 }
433 513
434 $map 514 $map
435} 515}
436 516
437sub cf::map::emergency_save { 517sub cf::map::emergency_save {
438 local $cf::FREEZE = 1; 518 local $cf::FREEZE = 1;
439 519
440 warn "enter emergency map save\n"; 520 warn "enter emergency map save\n";
441 521
442 my $saver = async { 522 cf::sync_job {
443 warn "begin emergency map save\n"; 523 warn "begin emergency map save\n";
444 $_->save for values %cf::MAP; 524 $_->save for values %cf::MAP;
445 }; 525 };
446 $saver->prio (Coro::PRIO_MAX);
447 $saver->join;
448 526
449 warn "emergency map save drain\n";
450 Event::one_event while IO::AIO::nreqs;
451 warn "end emergency map save\n"; 527 warn "end emergency map save\n";
452} 528}
453 529

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines