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.11 by root, Sun Dec 31 21:02:04 2006 UTC

18 18
19$DEACTIVATE_TIMEOUT = 3;#d# 19$DEACTIVATE_TIMEOUT = 3;#d#
20$SWAP_TIMEOUT = 5;#d# 20$SWAP_TIMEOUT = 5;#d#
21$SCHEDULE_INTERVAL = 1; 21$SCHEDULE_INTERVAL = 1;
22 22
23$cf::LINK_MAP ||= do {
24 my $map = cf::map::new;
25
26 $map->width (1);
27 $map->height (1);
28 $map->alloc;
29 $map->path ("{link}");
30 $map->{path} = bless { path => "{link}" }, "cf::path";
31 $map->in_memory (cf::MAP_IN_MEMORY);
32
33 $map
34};
35
36{
37 package cf::path;
38
39 sub new {
40 my ($class, $path, $base) = @_;
41
42 my %res;
43
44 if ($path =~ s{^~([^/]+)?}{}) {
45 $res{user_rel} = 1;
46
47 if (defined $1) {
48 $res{user} = $1;
49 } elsif ($base =~ m{^~([^/]+)/}) {
50 $res{user} = $1;
51 } else {
52 warn "cannot resolve user-relative path without user <$path,$base>\n";
53 }
54 } elsif ($path =~ /^\//) {
55 # already absolute
56 } else {
57 $base =~ s{[^/]+/?$}{};
58 return $class->new ("$base/$path");
59 }
60
61 for ($path) {
62 redo if s{/\.?/}{/};
63 redo if s{/[^/]+/\.\./}{/};
64 }
65
66 $res{path} = $path;
67
68 bless \%res, $class
69 }
70
71 # the name / primary key / in-game path
72 sub as_string {
73 my ($self) = @_;
74
75 $self->{user_rel}
76 ? "~$self->{user}$self->{path}"
77 : $self->{path}
78 }
79
80 # escape the /'s in the path
81 sub escaped_path {
82 # ∕ is U+2215
83 (my $path = $_[0]{path}) =~ s/\//∕/g;
84 $path
85 }
86
87 # the original (read-only) location
88 sub load_path {
89 my ($self) = @_;
90
91 sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path}
92 }
93
94 # the temporary/swap location
95 sub save_path {
96 my ($self) = @_;
97
98 $self->{user_rel}
99 ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->escaped_path
100 : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->escaped_path
101 }
102
103 # the unique path, might be eq to save_path
104 sub uniq_path {
105 my ($self) = @_;
106
107 $self->{user_rel}
108 ? undef
109 : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->escaped_path
110 }
111}
112
113sub write_runtime {
114 my $runtime = cf::localdir . "/runtime";
115
116 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
117 or return;
118
119 my $value = $cf::RUNTIME;
120 (aio_write $fh, 0, (length $value), $value, 0) <= 0
121 and return;
122
123 aio_fsync $fh
124 and return;
125
126 close $fh
127 or return;
128
129 aio_rename "$runtime~", $runtime
130 and return;
131
132 1
133}
134
135(Coro::async {
136 unless (write_runtime) {
137 warn "unable to write runtime file: $!";
138 exit 1;
139 }
140})->prio (Coro::PRIO_MAX);
141
142our $SCHEDULER = cf::coro { 23our $SCHEDULER = cf::coro {
143 Coro::Timer::sleep 3600;#d#TODO#for debugging only 24 Coro::Timer::sleep 3600;#d#TODO#for debugging only
144 while () { 25 while () {
145 Coro::Timer::sleep $SCHEDULE_INTERVAL; 26 Coro::Timer::sleep $SCHEDULE_INTERVAL;
146
147 write_runtime
148 or warn "unable to write runtime file: $!";
149 27
150 for my $map (values %cf::MAP) { 28 for my $map (values %cf::MAP) {
151 eval { 29 eval {
152 next if $map->in_memory != cf::MAP_IN_MEMORY; 30 next if $map->in_memory != cf::MAP_IN_MEMORY;
153 next if $map->players; 31 next if $map->players;
176$SCHEDULER->prio (-2); 54$SCHEDULER->prio (-2);
177 55
178sub generate_random_map { 56sub generate_random_map {
179 my ($path, $rmp) = @_; 57 my ($path, $rmp) = @_;
180 58
181 # mit "rum" bekleckert, nicht 59 # mit "rum" bekleckern, nicht
182 cf::map::_create_random_map 60 cf::map::_create_random_map
183 $path, 61 $path,
184 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 62 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
185 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 63 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
186 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 64 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
189 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, 67 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
190 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, 68 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
191 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, 69 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
192 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, 70 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
193 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, 71 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
194 $rmp->{region} 72 (cf::region::find $rmp->{region})
195} 73}
196 74
197sub parse_random_map_params { 75sub parse_random_map_params {
198 my ($spec) = @_; 76 my ($spec) = @_;
199 77
200 my $rmp; 78 my $rmp = { # defaults
79 xsize => 10,
80 ysize => 10,
81 };
201 82
202 for (split /\n/, $spec) { 83 for (split /\n/, $spec) {
203 my ($k, $v) = split /\s+/, $_, 2; 84 my ($k, $v) = split /\s+/, $_, 2;
204 85
205 $rmp->{lc $k} = $v if (length $k) && (length $v); 86 $rmp->{lc $k} = $v if (length $k) && (length $v);
209} 90}
210 91
211sub prepare_random_map { 92sub prepare_random_map {
212 my ($exit) = @_; 93 my ($exit) = @_;
213 94
95 # all this does is basically rpelace the /! path by
96 # a new random map path (?random/...) with a seed
97 # that depends on the exit object
98
214 my $rmp = parse_random_map_params $exit->msg; 99 my $rmp = parse_random_map_params $exit->msg;
215 use Data::Dumper;
216 die Dumper $rmp;
217}
218 100
219sub sync_job(&) { 101 if ($exit->map) {
220 my ($job) = @_; 102 $rmp->{region} = $exit->map->region_name;
221 103 $rmp->{origin_map} = $exit->map->path;
222 my $done; 104 $rmp->{origin_x} = $exit->x;
223 my @res; 105 $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 } 106 }
239 107
240 warn "sync<@res>\n";#d# 108 $rmp->{random_seed} = $exit->random_seed;
241 wantarray ? @res : $res[0] 109
110 $exit->slaying ("?random/" . cf::to_json $rmp);
111 $exit->msg (undef);
242} 112}
243 113
244# and all this just because we cannot iterate over 114# and all this just because we cannot iterate over
245# all maps in C++... 115# all maps in C++...
246sub cf::map::change_all_map_light { 116sub cf::map::change_all_map_light {
281 $cf::MAP{$key} || do { 151 $cf::MAP{$key} || do {
282 # do it the slow way 152 # do it the slow way
283 my $map = try_load_header $path->save_path; 153 my $map = try_load_header $path->save_path;
284 154
285 if (!$map) { 155 if (!$map) {
156 if (my $rmp = $path->random_map_params) {
157 $map = generate_random_map $key, $rmp;
158 } else {
286 $map = try_load_header $path->load_path 159 $map = try_load_header $path->load_path;
160 }
161
287 or return; 162 $map or return;
288 163
164 $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout;
289 $map->instantiate; 165 $map->instantiate;
290 166
291 # per-player maps become, after loading, normal maps 167 # per-player maps become, after loading, normal maps
292 $map->per_player (0) if $path->{user_rel}; 168 $map->per_player (0) if $path->{user_rel};
293 } 169 }
322 } 198 }
323 } 199 }
324 200
325 # now do the right thing for maps 201 # now do the right thing for maps
326 $self->link_multipart_objects; 202 $self->link_multipart_objects;
203
204 unless ($self->{path}->is_style_map) {
327 $self->fix_auto_apply; 205 $self->fix_auto_apply;
328 $self->decay_objects; 206 $self->decay_objects;
329 $self->update_buttons; 207 $self->update_buttons;
330 $self->set_darkness_map; 208 $self->set_darkness_map;
331 $self->difficulty ($self->estimate_difficulty) 209 $self->difficulty ($self->estimate_difficulty)
332 unless $self->difficulty; 210 unless $self->difficulty;
333 $self->activate; 211 $self->activate;
212 }
334 213
335 $self->in_memory (cf::MAP_IN_MEMORY); 214 $self->in_memory (cf::MAP_IN_MEMORY);
336} 215}
337 216
338sub cf::map::load_map_sync { 217sub cf::map::load_map_sync {
339 my ($path, $origin) = @_; 218 my ($path, $origin) = @_;
340 219
341 warn "load_map_sync<$path, $origin>\n";#d# 220 warn "load_map_sync<$path, $origin>\n";#d#
342 221
222 cf::abort if $path =~ /CVS/;#d#
223
343 sync_job { 224 cf::sync_job {
344 my $map = cf::map::find_map $path, $origin 225 my $map = cf::map::find_map $path, $origin
345 or return; 226 or return;
346 $map->load; 227 $map->load;
347 $map 228 $map
348 } 229 }
349} 230}
350 231
351sub cf::map::save { 232sub cf::map::save {
352 my ($self) = @_; 233 my ($self) = @_;
353 234
354 warn "saving map ", $self->path;
355
356 my $save = $self->{path}->save_path; utf8::encode $save; 235 my $save = $self->{path}->save_path; utf8::encode $save;
357 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 236 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
237
238 $self->{last_save} = $cf::RUNTIME;
239
240 return unless $self->dirty;
241
242 $self->{load_path} = $save;
243
244 return if $self->{path}->is_style_map;
245
246 warn "saving map ", $self->path;
358 247
359 if ($uniq) { 248 if ($uniq) {
360 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 249 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
361 $self->save_objects ($uniq, cf::IO_UNIQUES); 250 $self->save_objects ($uniq, cf::IO_UNIQUES);
362 } else { 251 } else {
363 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 252 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
364 } 253 }
365
366 $self->{load_path} = $save;
367 $self->{last_save} = $cf::RUNTIME;
368} 254}
369 255
370sub cf::map::swap_out { 256sub cf::map::swap_out {
371 my ($self) = @_; 257 my ($self) = @_;
372 258
379} 265}
380 266
381sub cf::map::should_reset { 267sub cf::map::should_reset {
382 my ($map) = @_; 268 my ($map) = @_;
383 269
270 return;#d#
384 # TODO: safety, remove and allow resettable per-player maps 271 # TODO: safety, remove and allow resettable per-player maps
385 return if $map->{path}{user_rel};#d# 272 return if $map->{path}{user_rel};#d#
386 return unless $map->reset_timeout; 273 return unless $map->reset_timeout;
387 274
388 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 275 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
389 276
390 $time + $map->reset_timeout < $cf::RUNTIME 277 $time + $map->reset_timeout < $cf::RUNTIME
391} 278}
392 279
393sub cf::map::reset { 280sub cf::map::reset {
395 282
396 return if $self->players; 283 return if $self->players;
397 return if $self->{path}{user_rel};#d# 284 return if $self->{path}{user_rel};#d#
398 285
399 warn "resetting map ", $self->path;#d# 286 warn "resetting map ", $self->path;#d#
287 return;#d#
400 288
401 utf8::encode (my $save = $self->{path}->save_path); 289 utf8::encode (my $save = $self->{path}->save_path);
402 aioreq_pri 3; IO::AIO::aio_unlink $save; 290 aioreq_pri 3; IO::AIO::aio_unlink $save;
403 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; 291 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
404 292
412 300
413 return unless $ob->type == cf::PLAYER; 301 return unless $ob->type == cf::PLAYER;
414 302
415 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y); 303 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
416 304
417 #TODO: do this in the background, freeze the player if required 305 $ob->enter_map ($LINK_MAP, 20, 20);
418 sync_job { 306 $ob->deactivate_recursive;
307
308 (Coro::async {
419 my ($map, $x, $y); 309 my ($map, $x, $y);
310 unless (eval {
420 311
421 if ($exit->slaying eq "/!") {
422 prepare_random_map $exit; 312 prepare_random_map $exit
313 if $exit->slaying eq "/!";
314
315 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
316
317 $map = cf::map::find_map $path->as_string;
318 $map = $map->customise_for ($ob) if $map;
319 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
320
321 unless ($map) {
322 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
323
324 # restore original map position
325 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
326
327 unless ($map) {
328 $map = cf::map::find_map $emergency_position->[0]
329 or die "FATAL: cannot load emergency map\n";
330 $x = $emergency_position->[1];
331 $y = $emergency_position->[2];
332 }
333 }
334
335 # use -1, -1 as default coordinates, not 0, 0
336 ($x, $y) = ($map->enter_x, $map->enter_y)
337 if $x <=0 && $y <= 0;
338
339 warn "entering ", $map->path, " at ($x, $y)\n";#d#
340 $map->load;
341 1;
342 }) {
343 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
344
345 $ob->message ("Something went wrong within the server. "
346 . "I'll try to bring you back to the map you were before. "
347 . "Please report this to the dungeon master",
348 cf::NDI_UNIQUE | cf::NDI_RED);
349
350 warn "ERROR in enter_exit: $@";
423 } 351 }
424 352 $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); 353 $ob->enter_map ($map, $x, $y);
452 } 354 })->prio (1);
453} 355}
454 356
455sub cf::map::customise_for { 357sub cf::map::customise_for {
456 my ($map, $ob) = @_; 358 my ($map, $ob) = @_;
457 359
465sub cf::map::emergency_save { 367sub cf::map::emergency_save {
466 local $cf::FREEZE = 1; 368 local $cf::FREEZE = 1;
467 369
468 warn "enter emergency map save\n"; 370 warn "enter emergency map save\n";
469 371
470 my $saver = async { 372 cf::sync_job {
471 warn "begin emergency map save\n"; 373 warn "begin emergency map save\n";
472 $_->save for values %cf::MAP; 374 $_->save for values %cf::MAP;
473 }; 375 };
474 $saver->prio (Coro::PRIO_MAX);
475 $saver->join;
476 376
477 warn "emergency map save drain\n";
478 Event::one_event while IO::AIO::nreqs;
479 warn "end emergency map save\n"; 377 warn "end emergency map save\n";
480} 378}
481 379

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines