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.12 by root, Sun Dec 31 22:23:12 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 {
24 Coro::Timer::sleep 3600;#d#TODO#for debugging only
143 while () { 25 while () {
144 Coro::Timer::sleep $SCHEDULE_INTERVAL; 26 Coro::Timer::sleep $SCHEDULE_INTERVAL;
145
146 write_runtime
147 or warn "unable to write runtime file: $!";
148 27
149 for my $map (values %cf::MAP) { 28 for my $map (values %cf::MAP) {
150 eval { 29 eval {
151 next if $map->in_memory != cf::MAP_IN_MEMORY; 30 next if $map->in_memory != cf::MAP_IN_MEMORY;
152 next if $map->players; 31 next if $map->players;
172 } 51 }
173 } 52 }
174}; 53};
175$SCHEDULER->prio (-2); 54$SCHEDULER->prio (-2);
176 55
177sub sync_job(&) { 56sub generate_random_map {
57 my ($path, $rmp) = @_;
58
59 # mit "rum" bekleckern, nicht
60 cf::map::_create_random_map
61 $path,
62 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
63 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
64 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
65 $rmp->{exit_on_final_map},
66 $rmp->{xsize}, $rmp->{ysize},
67 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
68 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
69 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
70 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
71 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
72 (cf::region::find $rmp->{region})
73}
74
75sub parse_random_map_params {
178 my ($job) = @_; 76 my ($spec) = @_;
179 77
180 my $done; 78 my $rmp = { # defaults
181 my @res; 79 xsize => 10,
182 80 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 } 81 };
197 82
198 wantarray ? @res : $res[0] 83 for (split /\n/, $spec) {
84 my ($k, $v) = split /\s+/, $_, 2;
85
86 $rmp->{lc $k} = $v if (length $k) && (length $v);
87 }
88
89 $rmp
90}
91
92sub prepare_random_map {
93 my ($exit) = @_;
94
95 # all this does is basically replace the /! path by
96 # a new random map path (?random/...) with a seed
97 # that depends on the exit object
98
99 my $rmp = parse_random_map_params $exit->msg;
100
101 if ($exit->map) {
102 $rmp->{region} = $exit->map->region_name;
103 $rmp->{origin_map} = $exit->map->path;
104 $rmp->{origin_x} = $exit->x;
105 $rmp->{origin_y} = $exit->y;
106 }
107
108 $rmp->{random_seed} ||= $exit->random_seed;
109
110 my $data = cf::to_json $rmp;
111 my $md5 = Digest::MD5::md5_hex $data;
112
113 if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) {
114 aio_write $fh, 0, (length $data), $data, 0;
115
116 $exit->slaying ("?random/$md5");
117 $exit->msg (undef);
118 }
199} 119}
200 120
201# and all this just because we cannot iterate over 121# and all this just because we cannot iterate over
202# all maps in C++... 122# all maps in C++...
203sub cf::map::change_all_map_light { 123sub cf::map::change_all_map_light {
225 $map->{load_path} = $path; 145 $map->{load_path} = $path;
226 146
227 $map 147 $map
228} 148}
229 149
230sub cf::map::find_map_nb { 150sub cf::map::find_map {
231 my ($path, $origin) = @_; 151 my ($path, $origin) = @_;
232 152
233 warn "find_map_nb<$path,$origin>\n";#d# 153 warn "find_map<$path,$origin>\n";#d#
234 154
235 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path; 155 $path = ref $path ? $path : new cf::path $path, $origin && $origin->path;
236 my $key = $path->as_string; 156 my $key = $path->as_string;
237 157
238 $cf::MAP{$key} || do { 158 $cf::MAP{$key} || do {
239 # do it the slow way 159 # do it the slow way
240 my $map = try_load_header $path->save_path; 160 my $map = try_load_header $path->save_path;
241 161
242 if (!$map) { 162 if (!$map) {
163 if (my $rmp = $path->random_map_params) {
164 $map = generate_random_map $key, $rmp;
165 } else {
243 $map = try_load_header $path->load_path 166 $map = try_load_header $path->load_path;
167 }
168
244 or return; 169 $map or return;
170
171 $map->{reset_time} = $cf::RUNTIME + $map->reset_timeout;
172 $map->instantiate;
173
174 # per-player maps become, after loading, normal maps
175 $map->per_player (0) if $path->{user_rel};
245 } 176 }
246 177
247 $map or return;
248
249 $map->instantiate;
250 $map->path ($key); 178 $map->path ($key);
251 $map->{path} = $path; 179 $map->{path} = $path;
252 180
253 $map->per_player (0) if $path->{user_rel};
254
255 $map->reset if $map->should_reset; 181 $map->reset if $map->should_reset;
256 182
257 $cf::MAP{$key} = $map 183 $cf::MAP{$key} = $map
258 } 184 }
259} 185}
260 186
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 { 187sub cf::map::load {
275 my ($self) = @_; 188 my ($self) = @_;
276 189
277 return 0 unless $self->in_memory == cf::MAP_SWAPPED; 190 return if $self->in_memory != cf::MAP_SWAPPED;
278 191
279 $self->in_memory (cf::MAP_LOADING); 192 $self->in_memory (cf::MAP_LOADING);
280 193
281 my $path = $self->{path}; 194 my $path = $self->{path};
282 195
292 } 205 }
293 } 206 }
294 207
295 # now do the right thing for maps 208 # now do the right thing for maps
296 $self->link_multipart_objects; 209 $self->link_multipart_objects;
210
211 unless ($self->{path}->is_style_map) {
297 $self->fix_auto_apply; 212 $self->fix_auto_apply;
298 $self->decay_objects; 213 $self->decay_objects;
299 $self->update_buttons; 214 $self->update_buttons;
300 $self->set_darkness_map; 215 $self->set_darkness_map;
301 $self->difficulty ($self->estimate_difficulty) 216 $self->difficulty ($self->estimate_difficulty)
302 unless $self->difficulty; 217 unless $self->difficulty;
303 $self->activate; 218 $self->activate;
219 }
304 220
305 $self->in_memory (cf::MAP_IN_MEMORY); 221 $self->in_memory (cf::MAP_IN_MEMORY);
306} 222}
307 223
308sub cf::map::do_load { 224sub cf::map::load_map_sync {
309 my ($self) = @_; 225 my ($path, $origin) = @_;
310 226
311 warn "do_load<$self>\n";#d# 227 warn "load_map_sync<$path, $origin>\n";#d#
312 228
229 cf::abort if $path =~ /CVS/;#d#
230
313 sync_job { 231 cf::sync_job {
314 cf::map::do_load_nb $self; 232 my $map = cf::map::find_map $path, $origin
233 or return;
234 $map->load;
235 $map
315 }; 236 }
316} 237}
317 238
318sub cf::map::save { 239sub cf::map::save {
319 my ($self) = @_; 240 my ($self) = @_;
320 241
321 warn "saving map ", $self->path;
322
323 my $save = $self->{path}->save_path; utf8::encode $save; 242 my $save = $self->{path}->save_path; utf8::encode $save;
324 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 243 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
244
245 $self->{last_save} = $cf::RUNTIME;
246
247 return unless $self->dirty;
248
249 $self->{load_path} = $save;
250
251 return if $self->{path}->is_style_map;
252
253 warn "saving map ", $self->path;
325 254
326 if ($uniq) { 255 if ($uniq) {
327 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 256 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
328 $self->save_objects ($uniq, cf::IO_UNIQUES); 257 $self->save_objects ($uniq, cf::IO_UNIQUES);
329 } else { 258 } else {
330 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 259 $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
331 } 260 }
332
333 $self->{load_path} = $save;
334 $self->{last_save} = $cf::RUNTIME;
335} 261}
336 262
337sub cf::map::swap_out { 263sub cf::map::swap_out {
338 my ($self) = @_; 264 my ($self) = @_;
339 265
340 return if $self->players; 266 return if $self->players;
341
342 $self->save if $self->in_memory == cf::MAP_IN_MEMORY; 267 return if $self->in_memory != cf::MAP_IN_MEMORY;
268
269 $self->save;
343 $self->clear; 270 $self->clear;
344 $self->in_memory (cf::MAP_SWAPPED); 271 $self->in_memory (cf::MAP_SWAPPED);
345} 272}
346 273
347sub cf::map::should_reset { 274sub cf::map::should_reset {
348 my ($map) = @_; 275 my ($map) = @_;
349 276
277 return;#d#
350 # TODO: safety, remove and allow resettable per-player maps 278 # TODO: safety, remove and allow resettable per-player maps
351 return if $map->{path}{user_rel};#d# 279 return if $map->{path}{user_rel};#d#
352 return if $map->per_player;
353 return unless $map->reset_timeout; 280 return unless $map->reset_timeout;
354 281
355 my $time = $map->fixed_resettime ? $map->reset_time : $map->last_access; 282 my $time = $map->fixed_resettime ? $map->{reset_time} : $map->last_access;
356 283
357 $time + $map->reset_timeout < $cf::RUNTIME 284 $time + $map->reset_timeout < $cf::RUNTIME
358} 285}
359 286
360sub cf::map::reset { 287sub cf::map::reset {
362 289
363 return if $self->players; 290 return if $self->players;
364 return if $self->{path}{user_rel};#d# 291 return if $self->{path}{user_rel};#d#
365 292
366 warn "resetting map ", $self->path;#d# 293 warn "resetting map ", $self->path;#d#
294 return;#d#
367 295
368 utf8::encode (my $save = $self->{path}->save_path); 296 utf8::encode (my $save = $self->{path}->save_path);
369 aioreq_pri 3; IO::AIO::aio_unlink $save; 297 aioreq_pri 3; IO::AIO::aio_unlink $save;
370 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst"; 298 aioreq_pri 3; IO::AIO::aio_unlink "$save.pst";
371 299
377sub cf::object::player::enter_exit { 305sub cf::object::player::enter_exit {
378 my ($ob, $exit) = @_; 306 my ($ob, $exit) = @_;
379 307
380 return unless $ob->type == cf::PLAYER; 308 return unless $ob->type == cf::PLAYER;
381 309
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); 310 my ($oldmap, $oldx, $oldy) = ($ob->map, $ob->x, $ob->y);
389 311
390 #TODO: do this in the background, freeze the player if required 312 $ob->enter_map ($LINK_MAP, 20, 20);
391 sync_job { 313 $ob->deactivate_recursive;
314
315 (Coro::async {
392 my ($map, $x, $y); 316 my ($map, $x, $y);
393
394 unless ($exit) { 317 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 318
399 unless ($map) { 319 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 "!") { 320 if $exit->slaying eq "/!";
407 } else { 321
408 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 322 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
409 323
410 $map = cf::map::find_map_nb $path->as_string; 324 $map = cf::map::find_map $path->as_string;
411 $map = $map->customise_for ($ob) if $map; 325 $map = $map->customise_for ($ob) if $map;
412 ($x, $y) = ($exit->stats->hp, $exit->stats->sp); 326 ($x, $y) = ($exit->stats->hp, $exit->stats->sp);
327
328 unless ($map) {
329 $ob->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
330
331 # restore original map position
332 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
333
334 unless ($map) {
335 $map = cf::map::find_map $emergency_position->[0]
336 or die "FATAL: cannot load emergency map\n";
337 $x = $emergency_position->[1];
338 $y = $emergency_position->[2];
339 }
340 }
341
342 # use -1, -1 as default coordinates, not 0, 0
343 ($x, $y) = ($map->enter_x, $map->enter_y)
344 if $x <=0 && $y <= 0;
345
346 warn "entering ", $map->path, " at ($x, $y)\n";#d#
347 $map->load;
348 1;
349 }) {
350 ($map, $x, $y) = ($oldmap, $oldx, $oldy);
351
352 $ob->message ("Something went wrong within the server. "
353 . "I'll try to bring you back to the map you were before. "
354 . "Please report this to the dungeon master",
355 cf::NDI_UNIQUE | cf::NDI_RED);
356
357 warn "ERROR in enter_exit: $@";
413 } 358 }
414 359 $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); 360 $ob->enter_map ($map, $x, $y);
424 } 361 })->prio (1);
425} 362}
426 363
427sub cf::map::customise_for { 364sub cf::map::customise_for {
428 my ($map, $ob) = @_; 365 my ($map, $ob) = @_;
429 366
430 if ($map->per_player) { 367 if ($map->per_player) {
431 return cf::map::find_map_nb "~" . $ob->name . "/" . $map->{path}{path}; 368 return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path};
432 } 369 }
433 370
434 $map 371 $map
435} 372}
436 373
437sub cf::map::emergency_save { 374sub cf::map::emergency_save {
438 local $cf::FREEZE = 1; 375 local $cf::FREEZE = 1;
439 376
440 warn "enter emergency map save\n"; 377 warn "enter emergency map save\n";
441 378
442 my $saver = async { 379 cf::sync_job {
443 warn "begin emergency map save\n"; 380 warn "begin emergency map save\n";
444 $_->save for values %cf::MAP; 381 $_->save for values %cf::MAP;
445 }; 382 };
446 $saver->prio (Coro::PRIO_MAX);
447 $saver->join;
448 383
449 warn "emergency map save drain\n";
450 Event::one_event while IO::AIO::nreqs;
451 warn "end emergency map save\n"; 384 warn "end emergency map save\n";
452} 385}
453 386

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines